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 #ifndef _INTERNALS_H
16 #define _INTERNALS_H 1
17
18 /* Internal data structures */
19
20 /* Includes */
21
22 #include <bits/libc-tsd.h> /* for _LIBC_TSD_KEY_N */
23 #include <limits.h>
24 #include <setjmp.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <bits/stackinfo.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include "pt-machine.h"
31 #include "semaphore.h"
32 #include "../linuxthreads_db/thread_dbP.h"
33 #ifdef __UCLIBC_HAS_XLOCALE__
34 #include <bits/uClibc_locale.h>
35 #endif /* __UCLIBC_HAS_XLOCALE__ */
36
37 /* Use a funky version in a probably vein attempt at preventing gdb
38 * from dlopen()'ing glibc's libthread_db library... */
39 #define VERSION __stringify(__UCLIBC_MAJOR__) "." __stringify(__UCLIBC_MINOR__) "." __stringify(__UCLIBC_SUBLEVEL__)
40
41 #ifndef THREAD_GETMEM
42 # define THREAD_GETMEM(descr, member) descr->member
43 #endif
44 #ifndef THREAD_GETMEM_NC
45 # define THREAD_GETMEM_NC(descr, member) descr->member
46 #endif
47 #ifndef THREAD_SETMEM
48 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
49 #endif
50 #ifndef THREAD_SETMEM_NC
51 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
52 #endif
53
54 /* Conditional variable attribute data structure. */
55 struct pthread_condattr
56 {
57 /* Combination of values:
58
59 Bit 0 : flag whether coditional variable will be shareable between
60 processes.
61
62 Bit 1-7: clock ID. */
63 int value;
64 };
65
66 /* The __NWAITERS field is used as a counter and to house the number
67 of bits for other purposes. COND_CLOCK_BITS is the number
68 of bits needed to represent the ID of the clock. COND_NWAITERS_SHIFT
69 is the number of bits reserved for other purposes like the clock. */
70 #define COND_CLOCK_BITS 1
71 #define COND_NWAITERS_SHIFT 1
72
73 /* Arguments passed to thread creation routine */
74
75 struct pthread_start_args {
76 void * (*start_routine)(void *); /* function to run */
77 void * arg; /* its argument */
78 sigset_t mask; /* initial signal mask for thread */
79 int schedpolicy; /* initial scheduling policy (if any) */
80 struct sched_param schedparam; /* initial scheduling parameters (if any) */
81 };
82
83
84 /* We keep thread specific data in a special data structure, a two-level
85 array. The top-level array contains pointers to dynamically allocated
86 arrays of a certain number of data pointers. So we can implement a
87 sparse array. Each dynamic second-level array has
88 PTHREAD_KEY_2NDLEVEL_SIZE
89 entries. This value shouldn't be too large. */
90 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
91
92 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
93 keys in each subarray. */
94 #define PTHREAD_KEY_1STLEVEL_SIZE \
95 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
96 / PTHREAD_KEY_2NDLEVEL_SIZE)
97
98 typedef void (*destr_function)(void *);
99
100 struct pthread_key_struct {
101 int in_use; /* already allocated? */
102 destr_function destr; /* destruction routine */
103 };
104
105
106 #define PTHREAD_START_ARGS_INITIALIZER { NULL, NULL, {{0, }}, 0, { 0 } }
107
108 /* The type of thread descriptors */
109
110 typedef struct _pthread_descr_struct * pthread_descr;
111
112 /* Callback interface for removing the thread from waiting on an
113 object if it is cancelled while waiting or about to wait.
114 This hold a pointer to the object, and a pointer to a function
115 which ``extricates'' the thread from its enqueued state.
116 The function takes two arguments: pointer to the wait object,
117 and a pointer to the thread. It returns 1 if an extrication
118 actually occured, and hence the thread must also be signalled.
119 It returns 0 if the thread had already been extricated. */
120
121 typedef struct _pthread_extricate_struct {
122 void *pu_object;
123 int (*pu_extricate_func)(void *, pthread_descr);
124 } pthread_extricate_if;
125
126 /* Atomic counter made possible by compare_and_swap */
127
128 struct pthread_atomic {
129 long p_count;
130 int p_spinlock;
131 };
132
133 /* Context info for read write locks. The pthread_rwlock_info structure
134 is information about a lock that has been read-locked by the thread
135 in whose list this structure appears. The pthread_rwlock_context
136 is embedded in the thread context and contains a pointer to the
137 head of the list of lock info structures, as well as a count of
138 read locks that are untracked, because no info structure could be
139 allocated for them. */
140
141 struct _pthread_rwlock_t;
142
143 typedef struct _pthread_rwlock_info {
144 struct _pthread_rwlock_info *pr_next;
145 struct _pthread_rwlock_t *pr_lock;
146 int pr_lock_count;
147 } pthread_readlock_info;
148
149 struct _pthread_descr_struct {
150 pthread_descr p_nextlive, p_prevlive;
151 /* Double chaining of active threads */
152 pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
153 pthread_descr p_nextlock; /* can be on a queue and waiting on a lock */
154 pthread_t p_tid; /* Thread identifier */
155 int p_pid; /* PID of Unix process */
156 int p_priority; /* Thread priority (== 0 if not realtime) */
157 struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
158 int p_signal; /* last signal received */
159 sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
160 sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
161 char p_terminated; /* true if terminated e.g. by pthread_exit */
162 char p_detached; /* true if detached */
163 char p_exited; /* true if the assoc. process terminated */
164 void * p_retval; /* placeholder for return value */
165 int p_retcode; /* placeholder for return code */
166 pthread_descr p_joining; /* thread joining on that thread or NULL */
167 struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
168 char p_cancelstate; /* cancellation state */
169 char p_canceltype; /* cancellation type (deferred/async) */
170 char p_canceled; /* cancellation request pending */
171 int * p_errnop; /* pointer to used errno variable */
172 int p_errno; /* error returned by last system call */
173 int * p_h_errnop; /* pointer to used h_errno variable */
174 int p_h_errno; /* error returned by last netdb function */
175 char * p_in_sighandler; /* stack address of sighandler, or NULL */
176 char p_sigwaiting; /* true if a sigwait() is in progress */
177 struct pthread_start_args p_start_args; /* arguments for thread creation */
178 void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
179 void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
180 int p_userstack; /* nonzero if the user provided the stack */
181 void *p_guardaddr; /* address of guard area or NULL */
182 size_t p_guardsize; /* size of guard area */
183 pthread_descr p_self; /* Pointer to this structure */
184 int p_nr; /* Index of descriptor in __pthread_handles */
185 int p_report_events; /* Nonzero if events must be reported. */
186 td_eventbuf_t p_eventbuf; /* Data for event. */
187 struct pthread_atomic p_resume_count; /* number of times restart() was
188 called on thread */
189 char p_woken_by_cancel; /* cancellation performed wakeup */
190 char p_condvar_avail; /* flag if conditional variable became avail */
191 char p_sem_avail; /* flag if semaphore became available */
192 pthread_extricate_if *p_extricate; /* See above */
193 pthread_readlock_info *p_readlock_list; /* List of readlock info structs */
194 pthread_readlock_info *p_readlock_free; /* Free list of structs */
195 int p_untracked_readlock_count; /* Readlocks not tracked by list */
196 /* New elements must be added at the end. */
197 #ifdef __UCLIBC_HAS_XLOCALE__
198 __locale_t locale; /* thread-specific locale from uselocale() only! */
199 #endif /* __UCLIBC_HAS_XLOCALE__ */
200 } __attribute__ ((aligned(32))); /* We need to align the structure so that
201 doubles are aligned properly. This is 8
202 bytes on MIPS and 16 bytes on MIPS64.
203 32 bytes might give better cache
204 utilization. */
205
206 /* The type of thread handles. */
207
208 typedef struct pthread_handle_struct * pthread_handle;
209
210 struct pthread_handle_struct {
211 struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
212 pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
213 char * h_bottom; /* Lowest address in the stack thread */
214 };
215
216 /* The type of messages sent to the thread manager thread */
217
218 struct pthread_request {
219 pthread_descr req_thread; /* Thread doing the request */
220 enum { /* Request kind */
221 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
222 REQ_POST, REQ_DEBUG, REQ_KICK
223 } req_kind;
224 union { /* Arguments for request */
225 struct { /* For REQ_CREATE: */
226 const pthread_attr_t * attr; /* thread attributes */
227 void * (*fn)(void *); /* start function */
228 void * arg; /* argument to start function */
229 sigset_t mask; /* signal mask */
230 } create;
231 struct { /* For REQ_FREE: */
232 pthread_t thread_id; /* identifier of thread to free */
233 } free;
234 struct { /* For REQ_PROCESS_EXIT: */
235 int code; /* exit status */
236 } exit;
237 void * post; /* For REQ_POST: the semaphore */
238 } req_args;
239 };
240
241
242 /* Signals used for suspend/restart and for cancellation notification. */
243
244 extern int __pthread_sig_restart;
245 extern int __pthread_sig_cancel;
246
247 /* Signal used for interfacing with gdb */
248
249 extern int __pthread_sig_debug;
250
251 /* Global array of thread handles, used for validating a thread id
252 and retrieving the corresponding thread descriptor. Also used for
253 mapping the available stack segments. */
254
255 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
256
257 /* Descriptor of the initial thread */
258
259 extern struct _pthread_descr_struct __pthread_initial_thread;
260
261 /* Descriptor of the manager thread */
262
263 extern struct _pthread_descr_struct __pthread_manager_thread;
264
265 /* Descriptor of the main thread */
266
267 extern pthread_descr __pthread_main_thread;
268
269 /* Limit between the stack of the initial thread (above) and the
270 stacks of other threads (below). Aligned on a STACK_SIZE boundary.
271 Initially 0, meaning that the current thread is (by definition)
272 the initial thread. */
273
274 extern char *__pthread_initial_thread_bos;
275 #ifndef __ARCH_USE_MMU__
276 /* For non-MMU systems, we have no idea the bounds of the initial thread
277 * stack, so we have to track it on the fly relative to other stacks. Do
278 * so by scaling back our assumptions on the limits of the bos/tos relative
279 * to the known mid point. See also the comments in pthread_initialize(). */
280 extern char *__pthread_initial_thread_tos, *__pthread_initial_thread_mid;
281 #define NOMMU_INITIAL_THREAD_BOUNDS(tos,bos) \
282 do { \
283 char *__tos = (tos); \
284 char *__bos = (bos); \
285 if (__tos >= __pthread_initial_thread_bos && \
286 __bos < __pthread_initial_thread_tos) { \
287 if (__bos < __pthread_initial_thread_mid) \
288 __pthread_initial_thread_bos = __tos; \
289 else \
290 __pthread_initial_thread_tos = __bos; \
291 } \
292 } while (0)
293 #else
294 #define NOMMU_INITIAL_THREAD_BOUNDS(tos,bos) /* empty */
295 #endif /* __ARCH_USE_MMU__ */
296
297
298 /* Indicate whether at least one thread has a user-defined stack (if 1),
299 or all threads have stacks supplied by LinuxThreads (if 0). */
300
301 extern int __pthread_nonstandard_stacks;
302
303 /* File descriptor for sending requests to the thread manager.
304 Initially -1, meaning that __pthread_initialize_manager must be called. */
305
306 extern int __pthread_manager_request;
307
308 /* Other end of the pipe for sending requests to the thread manager. */
309
310 extern int __pthread_manager_reader;
311
312 /* Limits of the thread manager stack. */
313
314 extern char *__pthread_manager_thread_bos;
315 extern char *__pthread_manager_thread_tos;
316
317 /* Pending request for a process-wide exit */
318
319 extern int __pthread_exit_requested, __pthread_exit_code;
320
321 /* Set to 1 by gdb if we're debugging */
322
323 extern volatile int __pthread_threads_debug;
324
325 /* Globally enabled events. */
326 extern volatile td_thr_events_t __pthread_threads_events;
327
328 /* Pointer to descriptor of thread with last event. */
329 extern volatile pthread_descr __pthread_last_event;
330
331 /* Return the handle corresponding to a thread id */
332
thread_handle(pthread_t id)333 static __inline__ pthread_handle thread_handle(pthread_t id)
334 {
335 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
336 }
337
338 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
339
invalid_handle(pthread_handle h,pthread_t id)340 static __inline__ int invalid_handle(pthread_handle h, pthread_t id)
341 {
342 return h->h_descr == NULL || h->h_descr->p_tid != id;
343 }
344
345 /* Fill in defaults left unspecified by pt-machine.h. */
346
347 /* The page size we can get from the system. This should likely not be
348 changed by the machine file but, you never know. */
349 #define __PAGE_SIZE (sysconf (_SC_PAGESIZE))
350
351 /* The max size of the thread stack segments. If the default
352 THREAD_SELF implementation is used, this must be a power of two and
353 a multiple of __PAGE_SIZE. */
354 #ifndef STACK_SIZE
355 #ifdef __ARCH_USE_MMU__
356 #define STACK_SIZE (2 * 1024 * 1024)
357 #else
358 #define STACK_SIZE (4 * __PAGE_SIZE)
359 #endif
360 #endif
361
362 /* The initial size of the thread stack. Must be a multiple of __PAGE_SIZE. */
363 #ifndef INITIAL_STACK_SIZE
364 #define INITIAL_STACK_SIZE (4 * __PAGE_SIZE)
365 #endif
366
367 /* Size of the thread manager stack. The "- 32" avoids wasting space
368 with some malloc() implementations. */
369 #ifndef THREAD_MANAGER_STACK_SIZE
370 #define THREAD_MANAGER_STACK_SIZE (2 * __PAGE_SIZE - 32)
371 #endif
372
373 /* The base of the "array" of thread stacks. The array will grow down from
374 here. Defaults to the calculated bottom of the initial application
375 stack. */
376 #ifndef THREAD_STACK_START_ADDRESS
377 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
378 #endif
379
380 /* Get some notion of the current stack. Need not be exactly the top
381 of the stack, just something somewhere in the current frame. */
382 #ifndef CURRENT_STACK_FRAME
383 #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
384 #endif
385
386 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
387 architecture doesn't need a memory barrier instruction (e.g. Intel
388 x86). Still we need the compiler to respect the barrier and emit
389 all outstanding operations which modify memory. Some architectures
390 distinguish between full, read and write barriers. */
391 #ifndef MEMORY_BARRIER
392 #define MEMORY_BARRIER() __asm__ ("" : : : "memory")
393 #endif
394 #ifndef READ_MEMORY_BARRIER
395 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
396 #endif
397 #ifndef WRITE_MEMORY_BARRIER
398 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
399 #endif
400
401 /* Recover thread descriptor for the current thread */
402
403 extern pthread_descr __pthread_find_self (void) __attribute__ ((const)) attribute_hidden;
404
405 static __inline__ pthread_descr thread_self (void) __attribute__ ((const));
thread_self(void)406 static __inline__ pthread_descr thread_self (void)
407 {
408 #ifdef THREAD_SELF
409 return THREAD_SELF;
410 #else
411 char *sp = CURRENT_STACK_FRAME;
412 #ifdef __ARCH_USE_MMU__
413 if (sp >= __pthread_initial_thread_bos)
414 return &__pthread_initial_thread;
415 else if (sp >= __pthread_manager_thread_bos
416 && sp < __pthread_manager_thread_tos)
417 return &__pthread_manager_thread;
418 else if (__pthread_nonstandard_stacks)
419 return __pthread_find_self();
420 else
421 return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
422 #else
423 /* For non-MMU we need to be more careful about the initial thread stack.
424 * We refine the initial thread stack bounds dynamically as we allocate
425 * the other stack frame such that it doesn't overlap with them. Then
426 * we can be sure to pick the right thread according to the current SP */
427
428 /* Since we allow other stack frames to be above or below, we need to
429 * treat this case special. When pthread_initialize() wasn't called yet,
430 * only the initial thread is there. */
431 if (__pthread_initial_thread_bos == NULL) {
432 return &__pthread_initial_thread;
433 }
434 else if (sp >= __pthread_initial_thread_bos
435 && sp < __pthread_initial_thread_tos) {
436 return &__pthread_initial_thread;
437 }
438 else if (sp >= __pthread_manager_thread_bos
439 && sp < __pthread_manager_thread_tos) {
440 return &__pthread_manager_thread;
441 }
442 else {
443 return __pthread_find_self();
444 }
445 #endif /* __ARCH_USE_MMU__ */
446 #endif
447 }
448
449 /* Max number of times we must spin on a spinlock calling sched_yield().
450 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
451
452 #ifndef MAX_SPIN_COUNT
453 #define MAX_SPIN_COUNT 50
454 #endif
455
456 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
457 after MAX_SPIN_COUNT iterations of sched_yield().
458 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
459 (Otherwise the kernel does busy-waiting for realtime threads,
460 giving other threads no chance to run.) */
461
462 #ifndef SPIN_SLEEP_DURATION
463 #define SPIN_SLEEP_DURATION 2000001
464 #endif
465
466 /* Defined and used in libc.so. */
467 extern int __libc_multiple_threads attribute_hidden;
468
469 /* Internal global functions */
470
471 void __pthread_do_exit (void *retval, char *currentframe)
472 __attribute__ ((__noreturn__)) attribute_hidden;
473 void __pthread_destroy_specifics(void) attribute_hidden;
474 void __pthread_perform_cleanup(char *currentframe) attribute_hidden;
475 int __pthread_initialize_manager(void) attribute_hidden;
476 void __pthread_message(char * fmt, ...)
477 __attribute__ ((__format__ (printf, 1, 2))) attribute_hidden;
478 int __pthread_manager(void *reqfd) attribute_hidden;
479 int __pthread_manager_event(void *reqfd) attribute_hidden;
480 void __pthread_manager_sighandler(int sig) attribute_hidden;
481 void __pthread_reset_main_thread(void) attribute_hidden;
482 void __fresetlockfiles(void) attribute_hidden;
483 void __pthread_manager_adjust_prio(int thread_prio) attribute_hidden;
484 void __pthread_initialize_minimal (void);
485
486 extern void __pthread_exit (void *retval)
487 #if defined NOT_IN_libc && defined IS_IN_libpthread
488 attribute_noreturn
489 #endif
490 ;
491
492 extern int __pthread_attr_setguardsize(pthread_attr_t *__attr,
493 size_t __guardsize) attribute_hidden;
494 extern int __pthread_attr_getguardsize(const pthread_attr_t *__attr,
495 size_t *__guardsize) attribute_hidden;
496 extern int __pthread_attr_setstackaddr(pthread_attr_t *__attr,
497 void *__stackaddr) attribute_hidden;
498 extern int __pthread_attr_getstackaddr(const pthread_attr_t *__attr,
499 void **__stackaddr) attribute_hidden;
500 extern int __pthread_attr_setstacksize(pthread_attr_t *__attr,
501 size_t __stacksize) attribute_hidden;
502 extern int __pthread_attr_getstacksize(const pthread_attr_t *__attr,
503 size_t *__stacksize) attribute_hidden;
504 extern int __pthread_getconcurrency(void) attribute_hidden;
505 extern int __pthread_setconcurrency(int __level) attribute_hidden;
506 extern void __pthread_kill_other_threads_np(void) attribute_hidden;
507
508 extern void __pthread_restart_old(pthread_descr th) attribute_hidden;
509 extern void __pthread_suspend_old(pthread_descr self) attribute_hidden;
510 extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abstime) attribute_hidden;
511
512 extern void __pthread_restart_new(pthread_descr th) attribute_hidden;
513 extern void __pthread_suspend_new(pthread_descr self) attribute_hidden;
514 extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime) attribute_hidden;
515
516 extern void __pthread_wait_for_restart_signal(pthread_descr self) attribute_hidden;
517
518 /* Global pointers to old or new suspend functions */
519
520 extern void (*__pthread_restart)(pthread_descr) attribute_hidden;
521 extern void (*__pthread_suspend)(pthread_descr) attribute_hidden;
522
523 #if defined NOT_IN_libc && defined IS_IN_libpthread
524 extern __typeof(pthread_mutex_init) __pthread_mutex_init attribute_hidden;
525 extern __typeof(pthread_mutex_destroy) __pthread_mutex_destroy attribute_hidden;
526 extern __typeof(pthread_mutex_lock) __pthread_mutex_lock attribute_hidden;
527 extern __typeof(pthread_mutex_trylock) __pthread_mutex_trylock attribute_hidden;
528 extern __typeof(pthread_mutex_unlock) __pthread_mutex_unlock attribute_hidden;
529 #endif
530
531 /* Prototypes for some of the new semaphore functions. */
532 /*extern int __new_sem_post (sem_t * sem);*/
533
534 /* TSD. */
535 extern int __pthread_internal_tsd_set (int key, const void * pointer);
536 extern void * __pthread_internal_tsd_get (int key);
537 extern void ** __attribute__ ((__const__))
538 __pthread_internal_tsd_address (int key);
539
540 /* The functions called the signal events. */
541 extern void __linuxthreads_create_event (void) attribute_hidden;
542 extern void __linuxthreads_death_event (void) attribute_hidden;
543 extern void __linuxthreads_reap_event (void) attribute_hidden;
544
545 extern int * __libc_pthread_init (void);
546
547 #endif /* internals.h */
548