Update.
[glibc.git] / linuxthreads / internals.h
blob0654c32758871468c24610c30ddd82ddfc124c52
1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
4 /* */
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. */
9 /* */
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 */
17 /* Includes */
19 #include <bits/libc-tsd.h> /* for _LIBC_TSD_KEY_N */
20 #include <limits.h>
21 #include <setjmp.h>
22 #include <signal.h>
23 #include <unistd.h>
24 #include <sys/types.h>
26 #include "pt-machine.h"
28 #ifndef THREAD_GETMEM
29 # define THREAD_GETMEM(descr, member) descr->member
30 #endif
31 #ifndef THREAD_GETMEM_NC
32 # define THREAD_GETMEM_NC(descr, member) descr->member
33 #endif
34 #ifndef THREAD_SETMEM
35 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
36 #endif
37 #ifndef THREAD_SETMEM_NC
38 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
39 #endif
41 /* Arguments passed to thread creation routine */
43 struct pthread_start_args {
44 void * (*start_routine)(void *); /* function to run */
45 void * arg; /* its argument */
46 sigset_t mask; /* initial signal mask for thread */
47 int schedpolicy; /* initial scheduling policy (if any) */
48 struct sched_param schedparam; /* initial scheduling parameters (if any) */
52 /* We keep thread specific data in a special data structure, a two-level
53 array. The top-level array contains pointers to dynamically allocated
54 arrays of a certain number of data pointers. So we can implement a
55 sparse array. Each dynamic second-level array has
56 PTHREAD_KEY_2NDLEVEL_SIZE
57 entries. This value shouldn't be too large. */
58 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
60 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
61 keys in each subarray. */
62 #define PTHREAD_KEY_1STLEVEL_SIZE \
63 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
64 / PTHREAD_KEY_2NDLEVEL_SIZE)
67 #define PTHREAD_START_ARGS_INITIALIZER { NULL, NULL, {{0, }}, 0, { 0 } }
69 /* The type of thread descriptors */
71 typedef struct _pthread_descr_struct * pthread_descr;
73 struct _pthread_descr_struct {
74 pthread_descr p_nextlive, p_prevlive;
75 /* Double chaining of active threads */
76 pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
77 pthread_t p_tid; /* Thread identifier */
78 int p_pid; /* PID of Unix process */
79 int p_priority; /* Thread priority (== 0 if not realtime) */
80 struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
81 int p_signal; /* last signal received */
82 sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
83 sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
84 char p_terminated; /* true if terminated e.g. by pthread_exit */
85 char p_detached; /* true if detached */
86 char p_exited; /* true if the assoc. process terminated */
87 void * p_retval; /* placeholder for return value */
88 int p_retcode; /* placeholder for return code */
89 pthread_descr p_joining; /* thread joining on that thread or NULL */
90 struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
91 char p_cancelstate; /* cancellation state */
92 char p_canceltype; /* cancellation type (deferred/async) */
93 char p_canceled; /* cancellation request pending */
94 int * p_errnop; /* pointer to used errno variable */
95 int p_errno; /* error returned by last system call */
96 int * p_h_errnop; /* pointer to used h_errno variable */
97 int p_h_errno; /* error returned by last netdb function */
98 char * p_in_sighandler; /* stack address of sighandler, or NULL */
99 char p_sigwaiting; /* true if a sigwait() is in progress */
100 struct pthread_start_args p_start_args; /* arguments for thread creation */
101 void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
102 void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
103 int p_userstack; /* nonzero if the user provided the stack */
104 void *p_guardaddr; /* address of guard area or NULL */
105 size_t p_guardsize; /* size of guard area */
106 pthread_descr p_self; /* Pointer to this structure */
107 int p_nr; /* Index of descriptor in __pthread_handles */
110 /* The type of thread handles. */
112 typedef struct pthread_handle_struct * pthread_handle;
114 struct pthread_handle_struct {
115 struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
116 pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
117 char * h_bottom; /* Lowest address in the stack thread */
120 /* The type of messages sent to the thread manager thread */
122 struct pthread_request {
123 pthread_descr req_thread; /* Thread doing the request */
124 enum { /* Request kind */
125 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
126 REQ_POST, REQ_DEBUG
127 } req_kind;
128 union { /* Arguments for request */
129 struct { /* For REQ_CREATE: */
130 const pthread_attr_t * attr; /* thread attributes */
131 void * (*fn)(void *); /* start function */
132 void * arg; /* argument to start function */
133 sigset_t mask; /* signal mask */
134 } create;
135 struct { /* For REQ_FREE: */
136 pthread_t thread_id; /* identifier of thread to free */
137 } free;
138 struct { /* For REQ_PROCESS_EXIT: */
139 int code; /* exit status */
140 } exit;
141 void * post; /* For REQ_POST: the semaphore */
142 } req_args;
146 /* Signals used for suspend/restart and for cancellation notification. */
148 extern int __pthread_sig_restart;
149 extern int __pthread_sig_cancel;
151 /* Signal used for interfacing with gdb */
153 extern int __pthread_sig_debug;
155 /* Default signals used if we don't have realtime signals */
157 #define DEFAULT_SIG_RESTART SIGUSR1
158 #define DEFAULT_SIG_CANCEL SIGUSR2
160 /* Global array of thread handles, used for validating a thread id
161 and retrieving the corresponding thread descriptor. Also used for
162 mapping the available stack segments. */
164 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
166 /* Descriptor of the initial thread */
168 extern struct _pthread_descr_struct __pthread_initial_thread;
170 /* Descriptor of the manager thread */
172 extern struct _pthread_descr_struct __pthread_manager_thread;
174 /* Descriptor of the main thread */
176 extern pthread_descr __pthread_main_thread;
178 /* Limit between the stack of the initial thread (above) and the
179 stacks of other threads (below). Aligned on a STACK_SIZE boundary.
180 Initially 0, meaning that the current thread is (by definition)
181 the initial thread. */
183 extern char *__pthread_initial_thread_bos;
185 /* Indicate whether at least one thread has a user-defined stack (if 1),
186 or all threads have stacks supplied by LinuxThreads (if 0). */
188 extern int __pthread_nonstandard_stacks;
190 /* File descriptor for sending requests to the thread manager.
191 Initially -1, meaning that __pthread_initialize_manager must be called. */
193 extern int __pthread_manager_request;
195 /* Other end of the pipe for sending requests to the thread manager. */
197 extern int __pthread_manager_reader;
199 /* Limits of the thread manager stack. */
201 extern char *__pthread_manager_thread_bos;
202 extern char *__pthread_manager_thread_tos;
204 /* Pending request for a process-wide exit */
206 extern int __pthread_exit_requested, __pthread_exit_code;
208 /* Set to 1 by gdb if we're debugging */
210 extern volatile int __pthread_threads_debug;
212 /* Return the handle corresponding to a thread id */
214 static inline pthread_handle thread_handle(pthread_t id)
216 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
219 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
221 static inline int invalid_handle(pthread_handle h, pthread_t id)
223 return h->h_descr == NULL || h->h_descr->p_tid != id;
226 /* Fill in defaults left unspecified by pt-machine.h. */
228 /* The page size we can get from the system. This should likely not be
229 changed by the machine file but, you never know. */
230 #ifndef PAGE_SIZE
231 #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
232 #endif
234 /* The max size of the thread stack segments. If the default
235 THREAD_SELF implementation is used, this must be a power of two and
236 a multiple of PAGE_SIZE. */
237 #ifndef STACK_SIZE
238 #define STACK_SIZE (2 * 1024 * 1024)
239 #endif
241 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
242 #ifndef INITIAL_STACK_SIZE
243 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
244 #endif
246 /* Size of the thread manager stack. The "- 32" avoids wasting space
247 with some malloc() implementations. */
248 #ifndef THREAD_MANAGER_STACK_SIZE
249 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
250 #endif
252 /* The base of the "array" of thread stacks. The array will grow down from
253 here. Defaults to the calculated bottom of the initial application
254 stack. */
255 #ifndef THREAD_STACK_START_ADDRESS
256 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
257 #endif
259 /* Get some notion of the current stack. Need not be exactly the top
260 of the stack, just something somewhere in the current frame. */
261 #ifndef CURRENT_STACK_FRAME
262 #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
263 #endif
265 /* Recover thread descriptor for the current thread */
267 extern pthread_descr __pthread_find_self (void) __attribute__ ((const));
269 static inline pthread_descr thread_self (void) __attribute__ ((const));
270 static inline pthread_descr thread_self (void)
272 #ifdef THREAD_SELF
273 return THREAD_SELF;
274 #else
275 char *sp = CURRENT_STACK_FRAME;
276 if (sp >= __pthread_initial_thread_bos)
277 return &__pthread_initial_thread;
278 else if (sp >= __pthread_manager_thread_bos
279 && sp < __pthread_manager_thread_tos)
280 return &__pthread_manager_thread;
281 else if (__pthread_nonstandard_stacks)
282 return __pthread_find_self();
283 else
284 return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
285 #endif
288 /* Max number of times we must spin on a spinlock calling sched_yield().
289 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
291 #ifndef MAX_SPIN_COUNT
292 #define MAX_SPIN_COUNT 50
293 #endif
295 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
296 after MAX_SPIN_COUNT iterations of sched_yield().
297 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
298 (Otherwise the kernel does busy-waiting for realtime threads,
299 giving other threads no chance to run.) */
301 #ifndef SPIN_SLEEP_DURATION
302 #define SPIN_SLEEP_DURATION 2000001
303 #endif
305 /* Debugging */
307 #ifdef DEBUG
308 #include <assert.h>
309 #define ASSERT assert
310 #define MSG __pthread_message
311 #else
312 #define ASSERT(x)
313 #define MSG(msg,arg...)
314 #endif
316 /* Internal global functions */
318 void __pthread_destroy_specifics(void);
319 void __pthread_perform_cleanup(void);
320 int __pthread_initialize_manager(void);
321 void __pthread_message(char * fmt, ...);
322 int __pthread_manager(void *reqfd);
323 void __pthread_manager_sighandler(int sig);
324 void __pthread_reset_main_thread(void);
325 void __fresetlockfiles(void);
326 void __pthread_manager_adjust_prio(int thread_prio);
328 extern int __pthread_attr_setguardsize __P ((pthread_attr_t *__attr,
329 size_t __guardsize));
330 extern int __pthread_attr_getguardsize __P ((__const pthread_attr_t *__attr,
331 size_t *__guardsize));
332 extern int __pthread_attr_setstackaddr __P ((pthread_attr_t *__attr,
333 void *__stackaddr));
334 extern int __pthread_attr_getstackaddr __P ((__const pthread_attr_t *__attr,
335 void **__stackaddr));
336 extern int __pthread_attr_setstacksize __P ((pthread_attr_t *__attr,
337 size_t __stacksize));
338 extern int __pthread_attr_getstacksize __P ((__const pthread_attr_t *__attr,
339 size_t *__stacksize));
340 extern int __pthread_getconcurrency __P ((void));
341 extern int __pthread_setconcurrency __P ((int __level));
342 extern int __pthread_mutexattr_gettype __P ((__const pthread_mutexattr_t *__attr,
343 int *__kind));
344 extern void __pthread_kill_other_threads_np __P ((void));
346 /* Prototypes for the function without cancelation support when the
347 normal version has it. */
348 extern int __libc_close (int fd);
349 extern int __libc_nanosleep (const struct timespec *requested_time,
350 struct timespec *remaining);
351 extern int __libc_read (int fd, void *buf, size_t count);
352 extern pid_t __libc_waitpid (pid_t pid, int *stat_loc, int options);
353 extern int __libc_write (int fd, const void *buf, size_t count);