(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / internals.h
blob605021766c0a0d25daad4e8430061af15c9b11df
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 #ifndef _INTERNALS_H
16 #define _INTERNALS_H 1
18 /* Internal data structures */
20 /* Includes */
22 #include <limits.h>
23 #include <signal.h>
24 #include <stdbool.h>
25 #include <unistd.h>
26 #include <stackinfo.h>
27 #include <sigcontextinfo.h>
29 #include <tls.h>
30 #include "descr.h"
32 #include "semaphore.h"
33 #include <pthread-functions.h>
35 #ifndef THREAD_GETMEM
36 # define THREAD_GETMEM(descr, member) descr->member
37 #endif
38 #ifndef THREAD_GETMEM_NC
39 # define THREAD_GETMEM_NC(descr, member) descr->member
40 #endif
41 #ifndef THREAD_SETMEM
42 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
43 #endif
44 #ifndef THREAD_SETMEM_NC
45 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
46 #endif
48 #if !defined NOT_IN_libc && defined FLOATING_STACKS
49 # define LIBC_THREAD_GETMEM(descr, member) THREAD_GETMEM (descr, member)
50 # define LIBC_THREAD_SETMEM(descr, member, value) \
51 THREAD_SETMEM (descr, member, value)
52 #else
53 # define LIBC_THREAD_GETMEM(descr, member) descr->member
54 # define LIBC_THREAD_SETMEM(descr, member, value) descr->member = (value)
55 #endif
57 typedef void (*destr_function)(void *);
59 struct pthread_key_struct {
60 int in_use; /* already allocated? */
61 destr_function destr; /* destruction routine */
65 #define PTHREAD_START_ARGS_INITIALIZER(fct) \
66 { (void *(*) (void *)) fct, NULL, {{0, }}, 0, { 0 } }
69 /* The type of thread handles. */
71 typedef struct pthread_handle_struct * pthread_handle;
73 struct pthread_handle_struct {
74 struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
75 pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
76 char * h_bottom; /* Lowest address in the stack thread */
79 /* The type of messages sent to the thread manager thread */
81 struct pthread_request {
82 pthread_descr req_thread; /* Thread doing the request */
83 enum { /* Request kind */
84 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
85 REQ_POST, REQ_DEBUG, REQ_KICK, REQ_FOR_EACH_THREAD
86 } req_kind;
87 union { /* Arguments for request */
88 struct { /* For REQ_CREATE: */
89 const pthread_attr_t * attr; /* thread attributes */
90 void * (*fn)(void *); /* start function */
91 void * arg; /* argument to start function */
92 sigset_t mask; /* signal mask */
93 } create;
94 struct { /* For REQ_FREE: */
95 pthread_t thread_id; /* identifier of thread to free */
96 } free;
97 struct { /* For REQ_PROCESS_EXIT: */
98 int code; /* exit status */
99 } exit;
100 void * post; /* For REQ_POST: the semaphore */
101 struct { /* For REQ_FOR_EACH_THREAD: callback */
102 void (*fn)(void *, pthread_descr);
103 void *arg;
104 } for_each;
105 } req_args;
110 typedef void (*arch_sighandler_t) (int, SIGCONTEXT);
111 union sighandler
113 arch_sighandler_t old;
114 void (*rt) (int, struct siginfo *, struct ucontext *);
116 extern union sighandler __sighandler[NSIG];
119 /* Signals used for suspend/restart and for cancellation notification. */
121 extern int __pthread_sig_restart;
122 extern int __pthread_sig_cancel;
124 /* Signal used for interfacing with gdb */
126 extern int __pthread_sig_debug;
128 /* Global array of thread handles, used for validating a thread id
129 and retrieving the corresponding thread descriptor. Also used for
130 mapping the available stack segments. */
132 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
134 /* Descriptor of the main thread */
136 extern pthread_descr __pthread_main_thread;
138 /* File descriptor for sending requests to the thread manager.
139 Initially -1, meaning that __pthread_initialize_manager must be called. */
141 extern int __pthread_manager_request;
143 /* Other end of the pipe for sending requests to the thread manager. */
145 extern int __pthread_manager_reader;
147 #ifdef FLOATING_STACKS
148 /* Maximum stack size. */
149 extern size_t __pthread_max_stacksize;
150 #endif
152 /* Pending request for a process-wide exit */
154 extern int __pthread_exit_requested, __pthread_exit_code;
156 /* Set to 1 by gdb if we're debugging */
158 extern volatile int __pthread_threads_debug;
160 /* Globally enabled events. */
161 extern volatile td_thr_events_t __pthread_threads_events;
163 /* Pointer to descriptor of thread with last event. */
164 extern volatile pthread_descr __pthread_last_event;
166 /* Flag which tells whether we are executing on SMP kernel. */
167 extern int __pthread_smp_kernel;
169 /* Return the handle corresponding to a thread id */
171 static inline pthread_handle thread_handle(pthread_t id)
173 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
176 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
178 static inline int invalid_handle(pthread_handle h, pthread_t id)
180 return h->h_descr == NULL || h->h_descr->p_tid != id || h->h_descr->p_terminated;
183 static inline int nonexisting_handle(pthread_handle h, pthread_t id)
185 return h->h_descr == NULL || h->h_descr->p_tid != id;
188 /* Fill in defaults left unspecified by pt-machine.h. */
190 /* We round up a value with page size. */
191 #ifndef page_roundup
192 #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
193 #endif
195 /* The page size we can get from the system. This should likely not be
196 changed by the machine file but, you never know. */
197 #ifndef PAGE_SIZE
198 #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
199 #endif
201 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
202 #ifndef INITIAL_STACK_SIZE
203 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
204 #endif
206 /* Size of the thread manager stack. The "- 32" avoids wasting space
207 with some malloc() implementations. */
208 #ifndef THREAD_MANAGER_STACK_SIZE
209 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
210 #endif
212 /* The base of the "array" of thread stacks. The array will grow down from
213 here. Defaults to the calculated bottom of the initial application
214 stack. */
215 #ifndef THREAD_STACK_START_ADDRESS
216 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
217 #endif
219 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
220 architecture doesn't need a memory barrier instruction (e.g. Intel
221 x86). Still we need the compiler to respect the barrier and emit
222 all outstanding operations which modify memory. Some architectures
223 distinguish between full, read and write barriers. */
225 #ifndef MEMORY_BARRIER
226 #define MEMORY_BARRIER() asm ("" : : : "memory")
227 #endif
228 #ifndef READ_MEMORY_BARRIER
229 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
230 #endif
231 #ifndef WRITE_MEMORY_BARRIER
232 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
233 #endif
235 /* Max number of times we must spin on a spinlock calling sched_yield().
236 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
238 #ifndef MAX_SPIN_COUNT
239 #define MAX_SPIN_COUNT 50
240 #endif
242 /* Max number of times the spinlock in the adaptive mutex implementation
243 spins actively on SMP systems. */
245 #ifndef MAX_ADAPTIVE_SPIN_COUNT
246 #define MAX_ADAPTIVE_SPIN_COUNT 100
247 #endif
249 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
250 after MAX_SPIN_COUNT iterations of sched_yield().
251 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
252 (Otherwise the kernel does busy-waiting for realtime threads,
253 giving other threads no chance to run.) */
255 #ifndef SPIN_SLEEP_DURATION
256 #define SPIN_SLEEP_DURATION 2000001
257 #endif
259 /* Defined and used in libc.so. */
260 extern int __libc_multiple_threads attribute_hidden;
261 extern int __librt_multiple_threads;
263 /* Debugging */
265 #ifdef DEBUG
266 #include <assert.h>
267 #define ASSERT assert
268 #define MSG __pthread_message
269 #else
270 #define ASSERT(x)
271 #define MSG(msg,arg...)
272 #endif
274 /* Internal global functions */
276 extern void __pthread_do_exit (void *retval, char *currentframe)
277 __attribute__ ((__noreturn__));
278 extern void __pthread_destroy_specifics (void);
279 extern void __pthread_perform_cleanup (char *currentframe);
280 extern void __pthread_init_max_stacksize (void);
281 extern int __pthread_initialize_manager (void);
282 extern void __pthread_message (const char * fmt, ...);
283 extern int __pthread_manager (void *reqfd);
284 extern int __pthread_manager_event (void *reqfd);
285 extern void __pthread_manager_sighandler (int sig);
286 extern void __pthread_reset_main_thread (void);
287 extern void __pthread_once_fork_prepare (void);
288 extern void __pthread_once_fork_parent (void);
289 extern void __pthread_once_fork_child (void);
290 extern void __flockfilelist (void);
291 extern void __funlockfilelist (void);
292 extern void __fresetlockfiles (void);
293 extern void __pthread_manager_adjust_prio (int thread_prio);
294 extern void __pthread_initialize_minimal (void);
296 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
297 size_t __guardsize);
298 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
299 size_t *__guardsize);
300 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
301 void *__stackaddr);
302 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
303 void **__stackaddr);
304 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
305 size_t __stacksize);
306 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
307 size_t *__stacksize);
308 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
309 size_t __stacksize);
310 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
311 size_t *__stacksize);
312 extern int __pthread_attr_destroy (pthread_attr_t *attr);
313 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
314 int detachstate);
315 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
316 int *detachstate);
317 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
318 const struct sched_param *param);
319 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
320 struct sched_param *param);
321 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
322 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
323 int *policy);
324 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
325 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
326 int *inherit);
327 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
328 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
330 extern int __pthread_getconcurrency (void);
331 extern int __pthread_setconcurrency (int __level);
332 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
333 const struct timespec *__abstime);
334 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
335 int *__pshared);
336 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
337 int __pshared);
338 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
339 int *__kind);
340 extern void __pthread_kill_other_threads_np (void);
341 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
342 __const pthread_mutexattr_t *__mutex_attr);
343 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
344 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
345 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
346 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
347 #if defined NOT_IN_libc && defined IS_IN_libpthread
348 hidden_proto (__pthread_mutex_init)
349 hidden_proto (__pthread_mutex_destroy)
350 hidden_proto (__pthread_mutex_lock)
351 hidden_proto (__pthread_mutex_trylock)
352 hidden_proto (__pthread_mutex_unlock)
353 #endif
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 extern int __pthread_getschedparam (pthread_t thread, int *policy,
371 struct sched_param *param);
372 extern int __pthread_setschedparam (pthread_t thread, int policy,
373 const struct sched_param *param);
374 extern int __pthread_setcancelstate (int state, int * oldstate);
375 extern int __pthread_setcanceltype (int type, int * oldtype);
377 extern void __pthread_restart_old(pthread_descr th);
378 extern void __pthread_suspend_old(pthread_descr self);
379 extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abs);
381 extern void __pthread_restart_new(pthread_descr th);
382 extern void __pthread_suspend_new(pthread_descr self);
383 extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abs);
385 extern void __pthread_wait_for_restart_signal(pthread_descr self);
387 extern void __pthread_sigsuspend (const sigset_t *mask) attribute_hidden;
389 extern int __pthread_yield (void);
391 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
392 __const struct timespec *__restrict
393 __abstime);
394 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
395 __const struct timespec *__restrict
396 __abstime);
397 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
399 extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
400 __restrict __attr,
401 int *__restrict __pshared);
403 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
404 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
405 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
406 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
407 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
409 /* Global pointers to old or new suspend functions */
411 extern void (*__pthread_restart)(pthread_descr);
412 extern void (*__pthread_suspend)(pthread_descr);
413 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
415 /* Prototypes for the function without cancelation support when the
416 normal version has it. */
417 extern int __libc_close (int fd);
418 extern int __libc_nanosleep (const struct timespec *requested_time,
419 struct timespec *remaining);
420 /* Prototypes for some of the new semaphore functions. */
421 extern int __new_sem_post (sem_t * sem);
422 extern int __new_sem_init (sem_t *__sem, int __pshared, unsigned int __value);
423 extern int __new_sem_wait (sem_t *__sem);
424 extern int __new_sem_trywait (sem_t *__sem);
425 extern int __new_sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
426 extern int __new_sem_destroy (sem_t *__sem);
428 /* Prototypes for compatibility functions. */
429 extern int __pthread_attr_init_2_1 (pthread_attr_t *__attr);
430 extern int __pthread_attr_init_2_0 (pthread_attr_t *__attr);
431 extern int __pthread_create_2_1 (pthread_t *__restrict __threadp,
432 const pthread_attr_t *__attr,
433 void *(*__start_routine) (void *),
434 void *__restrict __arg);
435 extern int __pthread_create_2_0 (pthread_t *__restrict thread,
436 const pthread_attr_t *__attr,
437 void *(*__start_routine) (void *),
438 void *__restrict arg);
440 /* The functions called the signal events. */
441 extern void __linuxthreads_create_event (void);
442 extern void __linuxthreads_death_event (void);
443 extern void __linuxthreads_reap_event (void);
445 /* This function is called to initialize the pthread library. */
446 extern void __pthread_initialize (void);
448 /* TSD. */
449 extern int __pthread_internal_tsd_set (int key, const void * pointer);
450 extern void * __pthread_internal_tsd_get (int key);
451 extern void ** __attribute__ ((__const__))
452 __pthread_internal_tsd_address (int key);
454 /* Sighandler wrappers. */
455 extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
456 extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
457 struct ucontext *uc);
458 extern void __pthread_null_sighandler(int sig);
459 extern int __pthread_sigaction (int sig, const struct sigaction *act,
460 struct sigaction *oact);
461 extern int __pthread_sigwait (const sigset_t *set, int *sig);
462 extern int __pthread_raise (int sig);
464 /* Cancellation. */
465 extern int __pthread_enable_asynccancel (void) attribute_hidden;
466 extern void __pthread_disable_asynccancel (int oldtype)
467 internal_function attribute_hidden;
469 /* The two functions are in libc.so and not exported. */
470 extern int __libc_enable_asynccancel (void) attribute_hidden;
471 extern void __libc_disable_asynccancel (int oldtype)
472 internal_function attribute_hidden;
474 /* The two functions are in libc.so and are exported. */
475 extern int __librt_enable_asynccancel (void);
476 extern void __librt_disable_asynccancel (int oldtype) internal_function;
478 extern void __pthread_cleanup_upto (__jmp_buf target,
479 char *targetframe) attribute_hidden;
480 extern pid_t __pthread_fork (struct fork_block *b) attribute_hidden;
482 #if !defined NOT_IN_libc
483 # define LIBC_CANCEL_ASYNC() \
484 __libc_enable_asynccancel ()
485 # define LIBC_CANCEL_RESET(oldtype) \
486 __libc_disable_asynccancel (oldtype)
487 # define LIBC_CANCEL_HANDLED() \
488 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
489 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
490 #elif defined IS_IN_libpthread
491 # define LIBC_CANCEL_ASYNC() \
492 __pthread_enable_asynccancel ()
493 # define LIBC_CANCEL_RESET(oldtype) \
494 __pthread_disable_asynccancel (oldtype)
495 # define LIBC_CANCEL_HANDLED() \
496 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
497 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
498 #elif defined IS_IN_librt
499 # define LIBC_CANCEL_ASYNC() \
500 __librt_enable_asynccancel ()
501 # define LIBC_CANCEL_RESET(oldtype) \
502 __librt_disable_asynccancel (oldtype)
503 # define LIBC_CANCEL_HANDLED() \
504 __asm (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \
505 __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel")
506 #else
507 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
508 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
509 # define LIBC_CANCEL_HANDLED() /* Nothing. */
510 #endif
512 extern int * __libc_pthread_init (const struct pthread_functions *functions);
514 #if !defined NOT_IN_libc && !defined FLOATING_STACKS
515 # ifdef SHARED
516 # define thread_self() \
517 (*__libc_pthread_functions.ptr_pthread_thread_self) ()
518 # else
519 weak_extern (__pthread_thread_self)
520 # define thread_self() __pthread_thread_self ()
521 # endif
522 #endif
524 #ifndef USE_TLS
525 # define __manager_thread (&__pthread_manager_thread)
526 #else
527 # define __manager_thread __pthread_manager_threadp
528 #endif
530 extern inline __attribute__((always_inline)) pthread_descr
531 check_thread_self (void)
533 pthread_descr self = thread_self ();
534 #if defined THREAD_SELF && defined INIT_THREAD_SELF
535 if (self == __manager_thread)
537 /* A new thread might get a cancel signal before it is fully
538 initialized, so that the thread register might still point to the
539 manager thread. Double check that this is really the manager
540 thread. */
541 self = __pthread_self_stack();
542 if (self != __manager_thread)
543 /* Oops, thread_self() isn't working yet.. */
544 INIT_THREAD_SELF(self, self->p_nr);
546 #endif
547 return self;
550 #endif /* internals.h */