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