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. */
16 #define _INTERNALS_H 1
18 #include "uClibc-glue.h"
20 /* Internal data structures */
28 #include <sys/ucontext.h>
29 #include <bits/stackinfo.h>
30 #include <bits/sigcontextinfo.h>
31 #include <bits/pthreadtypes.h>
33 #ifdef __UCLIBC_HAS_TLS__
38 #include "semaphore.h"
39 #include <pthread-functions.h>
42 # define THREAD_GETMEM(descr, member) descr->member
44 #ifndef THREAD_GETMEM_NC
45 # define THREAD_GETMEM_NC(descr, member) descr->member
48 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
50 #ifndef THREAD_SETMEM_NC
51 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
54 #if !defined NOT_IN_libc && defined FLOATING_STACKS
55 # define LIBC_THREAD_GETMEM(descr, member) THREAD_GETMEM (descr, member)
56 # define LIBC_THREAD_SETMEM(descr, member, value) \
57 THREAD_SETMEM (descr, member, value)
59 # define LIBC_THREAD_GETMEM(descr, member) descr->member
60 # define LIBC_THREAD_SETMEM(descr, member, value) descr->member = (value)
63 typedef void (*destr_function)(void *);
65 struct pthread_key_struct {
66 int in_use; /* already allocated? */
67 destr_function destr; /* destruction routine */
71 #define PTHREAD_START_ARGS_INITIALIZER(fct) \
72 { (void *(*) (void *)) fct, NULL, {{0, }}, 0, { 0 } }
75 /* The type of thread handles. */
77 typedef struct pthread_handle_struct * pthread_handle;
79 struct pthread_handle_struct {
80 struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
81 pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
82 char * h_bottom; /* Lowest address in the stack thread */
85 /* The type of messages sent to the thread manager thread */
87 struct pthread_request {
88 pthread_descr req_thread; /* Thread doing the request */
89 enum { /* Request kind */
90 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
91 REQ_POST, REQ_DEBUG, REQ_KICK, REQ_FOR_EACH_THREAD
93 union { /* Arguments for request */
94 struct { /* For REQ_CREATE: */
95 const pthread_attr_t * attr; /* thread attributes */
96 void * (*fn)(void *); /* start function */
97 void * arg; /* argument to start function */
98 sigset_t mask; /* signal mask */
100 struct { /* For REQ_FREE: */
101 pthread_t thread_id; /* identifier of thread to free */
103 struct { /* For REQ_PROCESS_EXIT: */
104 int code; /* exit status */
106 void * post; /* For REQ_POST: the semaphore */
107 struct { /* For REQ_FOR_EACH_THREAD: callback */
108 void (*fn)(void *, pthread_descr);
116 typedef void (*arch_sighandler_t) (int, SIGCONTEXT);
119 arch_sighandler_t old;
120 void (*rt) (int, struct siginfo *, struct ucontext *);
122 extern union sighandler __sighandler[NSIG];
125 /* Signals used for suspend/restart and for cancellation notification. */
127 extern int __pthread_sig_restart;
128 extern int __pthread_sig_cancel;
130 /* Signal used for interfacing with gdb */
132 extern int __pthread_sig_debug;
134 /* Global array of thread handles, used for validating a thread id
135 and retrieving the corresponding thread descriptor. Also used for
136 mapping the available stack segments. */
138 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
140 /* Descriptor of the main thread */
142 extern pthread_descr __pthread_main_thread;
144 /* File descriptor for sending requests to the thread manager.
145 Initially -1, meaning that __pthread_initialize_manager must be called. */
147 extern int __pthread_manager_request;
149 /* Other end of the pipe for sending requests to the thread manager. */
151 extern int __pthread_manager_reader;
153 #ifdef FLOATING_STACKS
154 /* Maximum stack size. */
155 extern size_t __pthread_max_stacksize;
158 /* Pending request for a process-wide exit */
160 extern int __pthread_exit_requested, __pthread_exit_code;
162 /* Set to 1 by gdb if we're debugging */
164 extern __volatile__ int __pthread_threads_debug;
166 /* Globally enabled events. */
167 extern __volatile__ td_thr_events_t __pthread_threads_events;
169 /* Pointer to descriptor of thread with last event. */
170 extern __volatile__ pthread_descr __pthread_last_event;
172 /* Flag which tells whether we are executing on SMP kernel. */
173 extern int __pthread_smp_kernel;
175 /* Return the handle corresponding to a thread id */
177 static __inline__ pthread_handle thread_handle(pthread_t id)
179 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
182 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
184 static __inline__ int invalid_handle(pthread_handle h, pthread_t id)
186 return h->h_descr == NULL || h->h_descr->p_tid != id || h->h_descr->p_terminated;
189 static __inline__ int nonexisting_handle(pthread_handle h, pthread_t id)
191 return h->h_descr == NULL || h->h_descr->p_tid != id;
194 /* Fill in defaults left unspecified by pt-machine.h. */
196 /* We round up a value with page size. */
198 #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
201 /* The page size we can get from the system. This should likely not be
202 changed by the machine file but, you never know. */
203 #define __PAGE_SIZE (sysconf (_SC_PAGESIZE))
205 /* The initial size of the thread stack. Must be a multiple of __PAGE_SIZE. */
206 #ifndef INITIAL_STACK_SIZE
207 #define INITIAL_STACK_SIZE (4 * __PAGE_SIZE)
210 /* Size of the thread manager stack. The "- 32" avoids wasting space
211 with some malloc() implementations. */
212 #ifndef THREAD_MANAGER_STACK_SIZE
213 #define THREAD_MANAGER_STACK_SIZE (2 * __PAGE_SIZE - 32)
216 /* The base of the "array" of thread stacks. The array will grow down from
217 here. Defaults to the calculated bottom of the initial application
219 #ifndef THREAD_STACK_START_ADDRESS
220 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
223 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
224 architecture doesn't need a memory barrier instruction (e.g. Intel
225 x86). Still we need the compiler to respect the barrier and emit
226 all outstanding operations which modify memory. Some architectures
227 distinguish between full, read and write barriers. */
229 #ifndef MEMORY_BARRIER
230 #define MEMORY_BARRIER() __asm__ ("" : : : "memory")
232 #ifndef READ_MEMORY_BARRIER
233 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
235 #ifndef WRITE_MEMORY_BARRIER
236 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
239 /* Max number of times we must spin on a spinlock calling sched_yield().
240 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
242 #ifndef MAX_SPIN_COUNT
243 #define MAX_SPIN_COUNT 50
246 /* Max number of times the spinlock in the adaptive mutex implementation
247 spins actively on SMP systems. */
249 #ifndef MAX_ADAPTIVE_SPIN_COUNT
250 #define MAX_ADAPTIVE_SPIN_COUNT 100
253 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
254 after MAX_SPIN_COUNT iterations of sched_yield().
255 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
256 (Otherwise the kernel does busy-waiting for realtime threads,
257 giving other threads no chance to run.) */
259 #ifndef SPIN_SLEEP_DURATION
260 #define SPIN_SLEEP_DURATION 2000001
263 /* Defined and used in libc.so. */
264 extern int __libc_multiple_threads attribute_hidden;
265 extern int __librt_multiple_threads;
271 #define ASSERT assert
272 #define MSG __pthread_message
275 #define MSG(msg,arg...)
278 /* Internal global functions */
280 extern void __pthread_do_exit (void *retval, char *currentframe)
281 __attribute__ ((__noreturn__));
282 extern void __pthread_destroy_specifics (void);
283 extern void __pthread_perform_cleanup (char *currentframe);
284 extern void __pthread_init_max_stacksize (void);
285 extern int __pthread_initialize_manager (void);
286 extern void __pthread_message (const char * fmt, ...) attribute_hidden;
287 extern int __pthread_manager (void *reqfd);
288 extern int __pthread_manager_event (void *reqfd);
289 extern void __pthread_manager_sighandler (int sig);
290 extern void __pthread_reset_main_thread (void);
291 extern void __pthread_once_fork_prepare (void);
292 extern void __pthread_once_fork_parent (void);
293 extern void __pthread_once_fork_child (void);
294 extern void __flockfilelist (void);
295 extern void __funlockfilelist (void);
296 extern void __fresetlockfiles (void);
297 extern void __pthread_manager_adjust_prio (int thread_prio);
298 extern void __pthread_initialize_minimal (void);
300 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
302 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
303 size_t *__guardsize);
304 #if 0 /* uClibc: deprecated stuff disabled */
305 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
307 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
310 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
312 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
313 size_t *__stacksize);
314 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
316 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
317 size_t *__stacksize);
318 extern int __pthread_attr_destroy (pthread_attr_t *attr);
319 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
321 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
323 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
324 const struct sched_param *param);
325 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
326 struct sched_param *param);
327 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
328 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
330 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
331 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
333 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
334 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
336 extern int __pthread_getconcurrency (void);
337 extern int __pthread_setconcurrency (int __level);
338 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
339 const struct timespec *__abstime);
340 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
342 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
344 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
346 extern void __pthread_kill_other_threads_np (void);
347 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
348 const pthread_mutexattr_t *__mutex_attr);
349 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
350 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
351 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
352 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
354 extern int __pthread_cond_init (pthread_cond_t *cond,
355 const pthread_condattr_t *cond_attr);
356 extern int __pthread_cond_destroy (pthread_cond_t *cond);
357 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
358 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
359 pthread_mutex_t *mutex,
360 const struct timespec *abstime);
361 extern int __pthread_cond_signal (pthread_cond_t *cond);
362 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
363 extern int __pthread_condattr_init (pthread_condattr_t *attr);
364 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
365 extern pthread_t __pthread_self (void);
366 extern pthread_descr __pthread_thread_self (void);
367 extern pthread_descr __pthread_self_stack (void) attribute_hidden;
368 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
369 extern void __pthread_exit (void *retval)
370 #if defined NOT_IN_libc && defined IS_IN_libpthread
374 extern int __pthread_getschedparam (pthread_t thread, int *policy,
375 struct sched_param *param);
376 extern int __pthread_setschedparam (pthread_t thread, int policy,
377 const struct sched_param *param);
378 extern int __pthread_setcancelstate (int state, int * oldstate);
379 extern int __pthread_setcanceltype (int type, int * oldtype);
381 extern void __pthread_restart_old(pthread_descr th);
382 extern void __pthread_suspend_old(pthread_descr self);
383 extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abstime);
385 extern void __pthread_restart_new(pthread_descr th);
386 extern void __pthread_suspend_new(pthread_descr self);
387 extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime);
389 extern void __pthread_wait_for_restart_signal(pthread_descr self);
391 extern void __pthread_sigsuspend (const sigset_t *mask) attribute_hidden;
393 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
394 const struct timespec *__restrict
396 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
397 const struct timespec *__restrict
399 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
401 extern int __pthread_barrierattr_getpshared (const pthread_barrierattr_t *
403 int *__restrict __pshared);
405 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
406 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
407 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
408 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
409 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
411 /* Global pointers to old or new suspend functions */
413 extern void (*__pthread_restart)(pthread_descr);
414 extern void (*__pthread_suspend)(pthread_descr);
415 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
417 /* Prototypes for some of the new semaphore functions. */
418 extern int sem_post (sem_t * sem);
419 extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value);
420 extern int sem_wait (sem_t *__sem);
421 extern int sem_trywait (sem_t *__sem);
422 extern int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
423 extern int sem_destroy (sem_t *__sem);
425 /* Prototypes for compatibility functions. */
426 extern int __pthread_attr_init (pthread_attr_t *__attr);
427 extern int __pthread_create (pthread_t *__restrict __threadp,
428 const pthread_attr_t *__attr,
429 void *(*__start_routine) (void *),
430 void *__restrict __arg);
432 /* The functions called the signal events. */
433 extern void __linuxthreads_create_event (void);
434 extern void __linuxthreads_death_event (void);
435 extern void __linuxthreads_reap_event (void);
437 /* This function is called to initialize the pthread library. */
438 extern void __pthread_initialize (void);
441 #if !defined __UCLIBC_HAS_TLS__ && defined __UCLIBC_HAS_RPC__
442 extern int __pthread_internal_tsd_set (int key, const void * pointer);
443 extern void * __pthread_internal_tsd_get (int key);
444 extern void ** __attribute__ ((__const__))
445 __pthread_internal_tsd_address (int key);
448 /* Sighandler wrappers. */
449 extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
450 extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
451 struct ucontext *uc);
452 extern void __pthread_null_sighandler(int sig);
453 extern int __pthread_sigaction (int sig, const struct sigaction *act,
454 struct sigaction *oact);
455 extern int __pthread_sigwait (const sigset_t *set, int *sig);
456 extern int __pthread_raise (int sig);
459 extern int __pthread_enable_asynccancel (void) attribute_hidden;
460 extern void __pthread_disable_asynccancel (int oldtype)
461 internal_function attribute_hidden;
463 /* The two functions are in libc.so and not exported. */
464 extern int __libc_enable_asynccancel (void) attribute_hidden;
465 extern void __libc_disable_asynccancel (int oldtype)
466 internal_function attribute_hidden;
468 /* The two functions are in libc.so and are exported. */
469 extern int __librt_enable_asynccancel (void);
470 extern void __librt_disable_asynccancel (int oldtype) internal_function;
472 extern void __pthread_cleanup_upto (__jmp_buf target,
473 char *targetframe) attribute_hidden;
474 extern pid_t __pthread_fork (struct fork_block *b) attribute_hidden;
476 #define asm_handle(name) _asm_handle(name)
477 #define _asm_handle(name) #name
478 #define ASM_GLOBAL asm_handle(ASM_GLOBAL_DIRECTIVE)
479 #define ASM_CANCEL(name) asm_handle(C_SYMBOL_NAME(name))
481 #if !defined NOT_IN_libc
482 # define LIBC_CANCEL_ASYNC() \
483 __libc_enable_asynccancel ()
484 # define LIBC_CANCEL_RESET(oldtype) \
485 __libc_disable_asynccancel (oldtype)
486 # define LIBC_CANCEL_HANDLED() \
487 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_enable_asynccancel)); \
488 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__libc_disable_asynccancel))
489 #elif defined IS_IN_libpthread
490 # define LIBC_CANCEL_ASYNC() \
491 __pthread_enable_asynccancel ()
492 # define LIBC_CANCEL_RESET(oldtype) \
493 __pthread_disable_asynccancel (oldtype)
494 # define LIBC_CANCEL_HANDLED() \
495 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_enable_asynccancel)); \
496 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__pthread_disable_asynccancel))
497 #elif defined IS_IN_librt
498 # define LIBC_CANCEL_ASYNC() \
499 __librt_enable_asynccancel ()
500 # define LIBC_CANCEL_RESET(oldtype) \
501 __librt_disable_asynccancel (oldtype)
502 # define LIBC_CANCEL_HANDLED() \
503 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_enable_asynccancel)); \
504 __asm__ (ASM_GLOBAL " " ASM_CANCEL(__librt_disable_asynccancel))
506 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
507 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
508 # define LIBC_CANCEL_HANDLED() /* Nothing. */
511 #if !defined NOT_IN_libc && !defined FLOATING_STACKS
513 # define thread_self() \
514 (*__libc_pthread_functions.ptr_pthread_thread_self) ()
516 weak_extern (__pthread_thread_self)
517 # define thread_self() __pthread_thread_self ()
521 #ifndef __UCLIBC_HAS_TLS__
522 # define __manager_thread (&__pthread_manager_thread)
524 # define __manager_thread __pthread_manager_threadp
527 static __always_inline pthread_descr
528 check_thread_self (void);
529 static __always_inline pthread_descr
530 check_thread_self (void)
532 pthread_descr self = thread_self ();
533 #if defined THREAD_SELF && defined INIT_THREAD_SELF
534 if (self == __manager_thread)
536 /* A new thread might get a cancel signal before it is fully
537 initialized, so that the thread register might still point to the
538 manager thread. Double check that this is really the manager
540 self = __pthread_self_stack();
541 if (self != __manager_thread)
542 /* Oops, thread_self() isn't working yet.. */
543 INIT_THREAD_SELF(self, self->p_nr);
549 #endif /* internals.h */