1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
10 /* This program is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU Library General Public License for more details. */
15 /* Internal data structures */
19 #include <bits/libc-lock.h> /* for _LIBC_TSD_KEY_N */
23 #include <sys/types.h>
25 #include "pt-machine.h"
27 /* Arguments passed to thread creation routine */
29 struct pthread_start_args {
30 void * (*start_routine)(void *); /* function to run */
31 void * arg; /* its argument */
32 sigset_t mask; /* initial signal mask for thread */
33 int schedpolicy; /* initial scheduling policy (if any) */
34 struct sched_param schedparam; /* initial scheduling parameters (if any) */
38 /* We keep thread specific data in a special data structure, a two-level
39 array. The top-level array contains pointers to dynamically allocated
40 arrays of a certain number of data pointers. So we can implement a
41 sparse array. Each dynamic second-level array has
42 PTHREAD_KEY_2NDLEVEL_SIZE
43 entries. This value shouldn't be too large. */
44 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
46 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
47 keys in each subarray. */
48 #define PTHREAD_KEY_1STLEVEL_SIZE \
49 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
50 / PTHREAD_KEY_2NDLEVEL_SIZE)
53 #define PTHREAD_START_ARGS_INITIALIZER { NULL, NULL, {{0, }}, 0, { 0 } }
55 /* The type of thread descriptors */
57 typedef struct _pthread_descr_struct * pthread_descr;
59 struct _pthread_descr_struct {
60 pthread_descr p_nextlive, p_prevlive;
61 /* Double chaining of active threads */
62 pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
63 pthread_t p_tid; /* Thread identifier */
64 int p_pid; /* PID of Unix process */
65 int p_priority; /* Thread priority (== 0 if not realtime) */
66 int * p_spinlock; /* Spinlock for synchronized accesses */
67 int p_signal; /* last signal received */
68 sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
69 sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
70 char p_terminated; /* true if terminated e.g. by pthread_exit */
71 char p_detached; /* true if detached */
72 char p_exited; /* true if the assoc. process terminated */
73 void * p_retval; /* placeholder for return value */
74 int p_retcode; /* placeholder for return code */
75 pthread_descr p_joining; /* thread joining on that thread or NULL */
76 struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
77 char p_cancelstate; /* cancellation state */
78 char p_canceltype; /* cancellation type (deferred/async) */
79 char p_canceled; /* cancellation request pending */
80 int * p_errnop; /* pointer to used errno variable */
81 int p_errno; /* error returned by last system call */
82 int * p_h_errnop; /* pointer to used h_errno variable */
83 int p_h_errno; /* error returned by last netdb function */
84 struct pthread_start_args p_start_args; /* arguments for thread creation */
85 void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
86 void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
87 int p_userstack; /* nonzero if the user provided the thread */
88 void *p_guardaddr; /* address of guard area or NULL */
89 size_t p_guardsize; /* size of guard area */
92 /* The type of thread handles. */
94 typedef struct pthread_handle_struct * pthread_handle;
96 struct pthread_handle_struct {
97 int h_spinlock; /* Spinlock for sychronized access */
98 pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
101 /* The type of messages sent to the thread manager thread */
103 struct pthread_request {
104 pthread_descr req_thread; /* Thread doing the request */
105 enum { /* Request kind */
106 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT
108 union { /* Arguments for request */
109 struct { /* For REQ_CREATE: */
110 const pthread_attr_t * attr; /* thread attributes */
111 void * (*fn)(void *); /* start function */
112 void * arg; /* argument to start function */
113 sigset_t mask; /* signal mask */
115 struct { /* For REQ_FREE: */
116 pthread_descr thread; /* descriptor of thread to free */
118 struct { /* For REQ_PROCESS_EXIT: */
119 int code; /* exit status */
125 /* Signals used for suspend/restart and for cancellation notification. */
128 /* The have real-time signals. */
129 extern int __pthread_sig_restart;
130 extern int __pthread_sig_cancel;
131 # define PTHREAD_SIG_RESTART __pthread_sig_restart
132 # define PTHREAD_SIG_CANCEL __pthread_sig_cancel
134 # define PTHREAD_SIG_RESTART SIGUSR1
135 # define PTHREAD_SIG_CANCEL SIGUSR2
138 /* Global array of thread handles, used for validating a thread id
139 and retrieving the corresponding thread descriptor. Also used for
140 mapping the available stack segments. */
142 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
144 /* Descriptor of the initial thread */
146 extern struct _pthread_descr_struct __pthread_initial_thread;
148 /* Descriptor of the manager thread */
150 extern struct _pthread_descr_struct __pthread_manager_thread;
152 /* Descriptor of the main thread */
154 extern pthread_descr __pthread_main_thread;
156 /* Limit between the stack of the initial thread (above) and the
157 stacks of other threads (below). Aligned on a STACK_SIZE boundary.
158 Initially 0, meaning that the current thread is (by definition)
159 the initial thread. */
161 extern char *__pthread_initial_thread_bos;
163 /* File descriptor for sending requests to the thread manager.
164 Initially -1, meaning that pthread_initialize must be called. */
166 extern int __pthread_manager_request;
168 /* Other end of the pipe for sending requests to the thread manager. */
170 extern int __pthread_manager_reader;
172 /* Limits of the thread manager stack. */
174 extern char *__pthread_manager_thread_bos;
175 extern char *__pthread_manager_thread_tos;
177 /* Pending request for a process-wide exit */
179 extern int __pthread_exit_requested, __pthread_exit_code;
181 /* Return the handle corresponding to a thread id */
183 static inline pthread_handle thread_handle(pthread_t id)
185 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
188 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
190 static inline int invalid_handle(pthread_handle h, pthread_t id)
192 return h->h_descr == NULL || h->h_descr->p_tid != id;
195 /* Fill in defaults left unspecified by pt-machine.h. */
197 /* The page size we can get from the system. This should likely not be
198 changed by the machine file but, you never know. */
200 #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
203 /* The max size of the thread stack segments. If the default
204 THREAD_SELF implementation is used, this must be a power of two and
205 a multiple of PAGE_SIZE. */
207 #define STACK_SIZE (2 * 1024 * 1024)
210 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
211 #ifndef INITIAL_STACK_SIZE
212 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
215 /* Size of the thread manager stack. The "- 32" avoids wasting space
216 with some malloc() implementations. */
217 #ifndef THREAD_MANAGER_STACK_SIZE
218 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
221 /* The base of the "array" of thread stacks. The array will grow down from
222 here. Defaults to the calculated bottom of the initial application
224 #ifndef THREAD_STACK_START_ADDRESS
225 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
228 /* Get some notion of the current stack. Need not be exactly the top
229 of the stack, just something somewhere in the current frame. */
230 #ifndef CURRENT_STACK_FRAME
231 #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
234 /* Recover thread descriptor for the current thread */
236 static inline pthread_descr thread_self (void) __attribute__ ((const));
237 static inline pthread_descr thread_self (void)
242 char *sp = CURRENT_STACK_FRAME;
243 if (sp >= __pthread_initial_thread_bos)
244 return &__pthread_initial_thread;
245 else if (sp >= __pthread_manager_thread_bos
246 && sp < __pthread_manager_thread_tos)
247 return &__pthread_manager_thread;
249 return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
257 #define ASSERT assert
258 #define MSG __pthread_message
261 #define MSG(msg,arg...)
264 /* Internal global functions */
266 void __pthread_destroy_specifics(void);
267 void __pthread_perform_cleanup(void);
268 void __pthread_sighandler(int sig);
269 void __pthread_message(char * fmt, long arg, ...);
270 int __pthread_manager(void *reqfd);
271 void __pthread_manager_sighandler(int sig);
272 void __pthread_reset_main_thread(void);
273 void __fresetlockfiles(void);
276 /* Prototypes for the function without cancelation support when the
277 normal version has it. */
278 extern int __libc_close (int fd);
279 extern int __libc_nanosleep (const struct timespec *requested_time,
280 struct timespec *remaining);
281 extern int __libc_read (int fd, void *buf, size_t count);
282 extern pid_t __libc_waitpid (pid_t pid, int *stat_loc, int options);
283 extern int __libc_write (int fd, const void *buf, size_t count);