* sysdeps/gnu/Makefile ($(objpfx)errlist-compat.c):
[glibc.git] / linuxthreads / internals.h
blobb35a430c6912169548268087f0ac03d2053604b2
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 <unistd.h>
25 #include <stackinfo.h>
26 #include <sigcontextinfo.h>
28 #include <tls.h>
29 #include "descr.h"
31 #include "semaphore.h"
32 #include <pthread-functions.h>
34 #ifndef THREAD_GETMEM
35 # define THREAD_GETMEM(descr, member) descr->member
36 #endif
37 #ifndef THREAD_GETMEM_NC
38 # define THREAD_GETMEM_NC(descr, member) descr->member
39 #endif
40 #ifndef THREAD_SETMEM
41 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
42 #endif
43 #ifndef THREAD_SETMEM_NC
44 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
45 #endif
47 #if !defined NOT_IN_libc && defined FLOATING_STACKS
48 # define LIBC_THREAD_GETMEM(descr, member) THREAD_GETMEM (descr, member)
49 # define LIBC_THREAD_SETMEM(descr, member, value) \
50 THREAD_SETMEM (descr, member, value)
51 #else
52 # define LIBC_THREAD_GETMEM(descr, member) descr->member
53 # define LIBC_THREAD_SETMEM(descr, member, value) descr->member = (value)
54 #endif
56 typedef void (*destr_function)(void *);
58 struct pthread_key_struct {
59 int in_use; /* already allocated? */
60 destr_function destr; /* destruction routine */
64 #define PTHREAD_START_ARGS_INITIALIZER(fct) \
65 { (void *(*) (void *)) fct, NULL, {{0, }}, 0, { 0 } }
68 /* The type of thread handles. */
70 typedef struct pthread_handle_struct * pthread_handle;
72 struct pthread_handle_struct {
73 struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
74 pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
75 char * h_bottom; /* Lowest address in the stack thread */
78 /* The type of messages sent to the thread manager thread */
80 struct pthread_request {
81 pthread_descr req_thread; /* Thread doing the request */
82 enum { /* Request kind */
83 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
84 REQ_POST, REQ_DEBUG, REQ_KICK, REQ_FOR_EACH_THREAD
85 } req_kind;
86 union { /* Arguments for request */
87 struct { /* For REQ_CREATE: */
88 const pthread_attr_t * attr; /* thread attributes */
89 void * (*fn)(void *); /* start function */
90 void * arg; /* argument to start function */
91 sigset_t mask; /* signal mask */
92 } create;
93 struct { /* For REQ_FREE: */
94 pthread_t thread_id; /* identifier of thread to free */
95 } free;
96 struct { /* For REQ_PROCESS_EXIT: */
97 int code; /* exit status */
98 } exit;
99 void * post; /* For REQ_POST: the semaphore */
100 struct { /* For REQ_FOR_EACH_THREAD: callback */
101 void (*fn)(void *, pthread_descr);
102 void *arg;
103 } for_each;
104 } req_args;
109 typedef void (*arch_sighandler_t) (int, SIGCONTEXT);
110 union sighandler
112 arch_sighandler_t old;
113 void (*rt) (int, struct siginfo *, struct ucontext *);
115 extern union sighandler __sighandler[NSIG];
118 /* Signals used for suspend/restart and for cancellation notification. */
120 extern int __pthread_sig_restart;
121 extern int __pthread_sig_cancel;
123 /* Signal used for interfacing with gdb */
125 extern int __pthread_sig_debug;
127 /* Global array of thread handles, used for validating a thread id
128 and retrieving the corresponding thread descriptor. Also used for
129 mapping the available stack segments. */
131 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
133 /* Descriptor of the main thread */
135 extern pthread_descr __pthread_main_thread;
137 /* File descriptor for sending requests to the thread manager.
138 Initially -1, meaning that __pthread_initialize_manager must be called. */
140 extern int __pthread_manager_request;
142 /* Other end of the pipe for sending requests to the thread manager. */
144 extern int __pthread_manager_reader;
146 #ifdef FLOATING_STACKS
147 /* Maximum stack size. */
148 extern size_t __pthread_max_stacksize;
149 #endif
151 /* Pending request for a process-wide exit */
153 extern int __pthread_exit_requested, __pthread_exit_code;
155 /* Set to 1 by gdb if we're debugging */
157 extern volatile int __pthread_threads_debug;
159 /* Globally enabled events. */
160 extern volatile td_thr_events_t __pthread_threads_events;
162 /* Pointer to descriptor of thread with last event. */
163 extern volatile pthread_descr __pthread_last_event;
165 /* Flag which tells whether we are executing on SMP kernel. */
166 extern int __pthread_smp_kernel;
168 /* Return the handle corresponding to a thread id */
170 static inline pthread_handle thread_handle(pthread_t id)
172 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
175 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
177 static inline int invalid_handle(pthread_handle h, pthread_t id)
179 return h->h_descr == NULL || h->h_descr->p_tid != id || h->h_descr->p_terminated;
182 static inline int nonexisting_handle(pthread_handle h, pthread_t id)
184 return h->h_descr == NULL || h->h_descr->p_tid != id;
187 /* Fill in defaults left unspecified by pt-machine.h. */
189 /* We round up a value with page size. */
190 #ifndef page_roundup
191 #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
192 #endif
194 /* The page size we can get from the system. This should likely not be
195 changed by the machine file but, you never know. */
196 #ifndef PAGE_SIZE
197 #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
198 #endif
200 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
201 #ifndef INITIAL_STACK_SIZE
202 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
203 #endif
205 /* Size of the thread manager stack. The "- 32" avoids wasting space
206 with some malloc() implementations. */
207 #ifndef THREAD_MANAGER_STACK_SIZE
208 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
209 #endif
211 /* The base of the "array" of thread stacks. The array will grow down from
212 here. Defaults to the calculated bottom of the initial application
213 stack. */
214 #ifndef THREAD_STACK_START_ADDRESS
215 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
216 #endif
218 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
219 architecture doesn't need a memory barrier instruction (e.g. Intel
220 x86). Still we need the compiler to respect the barrier and emit
221 all outstanding operations which modify memory. Some architectures
222 distinguish between full, read and write barriers. */
224 #ifndef MEMORY_BARRIER
225 #define MEMORY_BARRIER() asm ("" : : : "memory")
226 #endif
227 #ifndef READ_MEMORY_BARRIER
228 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
229 #endif
230 #ifndef WRITE_MEMORY_BARRIER
231 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
232 #endif
234 /* Max number of times we must spin on a spinlock calling sched_yield().
235 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
237 #ifndef MAX_SPIN_COUNT
238 #define MAX_SPIN_COUNT 50
239 #endif
241 /* Max number of times the spinlock in the adaptive mutex implementation
242 spins actively on SMP systems. */
244 #ifndef MAX_ADAPTIVE_SPIN_COUNT
245 #define MAX_ADAPTIVE_SPIN_COUNT 100
246 #endif
248 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
249 after MAX_SPIN_COUNT iterations of sched_yield().
250 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
251 (Otherwise the kernel does busy-waiting for realtime threads,
252 giving other threads no chance to run.) */
254 #ifndef SPIN_SLEEP_DURATION
255 #define SPIN_SLEEP_DURATION 2000001
256 #endif
258 /* Defined and used in libc.so. */
259 extern int __libc_multiple_threads attribute_hidden;
260 extern int __librt_multiple_threads;
262 /* Debugging */
264 #ifdef DEBUG
265 #include <assert.h>
266 #define ASSERT assert
267 #define MSG __pthread_message
268 #else
269 #define ASSERT(x)
270 #define MSG(msg,arg...)
271 #endif
273 /* Internal global functions */
275 extern void __pthread_do_exit (void *retval, char *currentframe)
276 __attribute__ ((__noreturn__));
277 extern void __pthread_destroy_specifics (void);
278 extern void __pthread_perform_cleanup (char *currentframe);
279 extern void __pthread_init_max_stacksize (void);
280 extern int __pthread_initialize_manager (void);
281 extern void __pthread_message (const char * fmt, ...);
282 extern int __pthread_manager (void *reqfd);
283 extern int __pthread_manager_event (void *reqfd);
284 extern void __pthread_manager_sighandler (int sig);
285 extern void __pthread_reset_main_thread (void);
286 extern void __pthread_once_fork_prepare (void);
287 extern void __pthread_once_fork_parent (void);
288 extern void __pthread_once_fork_child (void);
289 extern void __flockfilelist (void);
290 extern void __funlockfilelist (void);
291 extern void __fresetlockfiles (void);
292 extern void __pthread_manager_adjust_prio (int thread_prio);
293 extern void __pthread_initialize_minimal (void);
295 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
296 size_t __guardsize);
297 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
298 size_t *__guardsize);
299 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
300 void *__stackaddr);
301 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
302 void **__stackaddr);
303 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
304 size_t __stacksize);
305 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
306 size_t *__stacksize);
307 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
308 size_t __stacksize);
309 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
310 size_t *__stacksize);
311 extern int __pthread_attr_destroy (pthread_attr_t *attr);
312 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
313 int detachstate);
314 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
315 int *detachstate);
316 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
317 const struct sched_param *param);
318 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
319 struct sched_param *param);
320 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
321 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
322 int *policy);
323 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
324 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
325 int *inherit);
326 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
327 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
329 extern int __pthread_getconcurrency (void);
330 extern int __pthread_setconcurrency (int __level);
331 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
332 const struct timespec *__abstime);
333 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
334 int *__pshared);
335 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
336 int __pshared);
337 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
338 int *__kind);
339 extern void __pthread_kill_other_threads_np (void);
340 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
341 __const pthread_mutexattr_t *__mutex_attr);
342 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
343 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
344 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
345 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
346 #if defined NOT_IN_libc && defined IS_IN_libpthread
347 hidden_proto (__pthread_mutex_init)
348 hidden_proto (__pthread_mutex_destroy)
349 hidden_proto (__pthread_mutex_lock)
350 hidden_proto (__pthread_mutex_trylock)
351 hidden_proto (__pthread_mutex_unlock)
352 #endif
353 extern int __pthread_cond_init (pthread_cond_t *cond,
354 const pthread_condattr_t *cond_attr);
355 extern int __pthread_cond_destroy (pthread_cond_t *cond);
356 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
357 extern int __pthread_cond_timedwait (pthread_cond_t *cond,
358 pthread_mutex_t *mutex,
359 const struct timespec *abstime);
360 extern int __pthread_cond_signal (pthread_cond_t *cond);
361 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
362 extern int __pthread_condattr_init (pthread_condattr_t *attr);
363 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
364 extern pthread_t __pthread_self (void);
365 extern pthread_descr __pthread_thread_self (void);
366 extern pthread_descr __pthread_self_stack (void) attribute_hidden;
367 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
368 extern void __pthread_exit (void *retval);
369 extern int __pthread_getschedparam (pthread_t thread, int *policy,
370 struct sched_param *param);
371 extern int __pthread_setschedparam (pthread_t thread, int policy,
372 const struct sched_param *param);
373 extern int __pthread_setcancelstate (int state, int * oldstate);
374 extern int __pthread_setcanceltype (int type, int * oldtype);
376 extern void __pthread_restart_old(pthread_descr th);
377 extern void __pthread_suspend_old(pthread_descr self);
378 extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abs);
380 extern void __pthread_restart_new(pthread_descr th);
381 extern void __pthread_suspend_new(pthread_descr self);
382 extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abs);
384 extern void __pthread_wait_for_restart_signal(pthread_descr self);
386 extern void __pthread_sigsuspend (const sigset_t *mask) attribute_hidden;
388 extern int __pthread_yield (void);
390 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
391 __const struct timespec *__restrict
392 __abstime);
393 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
394 __const struct timespec *__restrict
395 __abstime);
396 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
398 extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
399 __restrict __attr,
400 int *__restrict __pshared);
402 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
403 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
404 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
405 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
406 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
408 /* Global pointers to old or new suspend functions */
410 extern void (*__pthread_restart)(pthread_descr);
411 extern void (*__pthread_suspend)(pthread_descr);
412 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
414 /* Prototypes for the function without cancelation support when the
415 normal version has it. */
416 extern int __libc_close (int fd);
417 extern int __libc_nanosleep (const struct timespec *requested_time,
418 struct timespec *remaining);
419 /* Prototypes for some of the new semaphore functions. */
420 extern int __new_sem_post (sem_t * sem);
421 extern int __new_sem_init (sem_t *__sem, int __pshared, unsigned int __value);
422 extern int __new_sem_wait (sem_t *__sem);
423 extern int __new_sem_trywait (sem_t *__sem);
424 extern int __new_sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
425 extern int __new_sem_destroy (sem_t *__sem);
427 /* Prototypes for compatibility functions. */
428 extern int __pthread_attr_init_2_1 (pthread_attr_t *__attr);
429 extern int __pthread_attr_init_2_0 (pthread_attr_t *__attr);
430 extern int __pthread_create_2_1 (pthread_t *__restrict __threadp,
431 const pthread_attr_t *__attr,
432 void *(*__start_routine) (void *),
433 void *__restrict __arg);
434 extern int __pthread_create_2_0 (pthread_t *__restrict thread,
435 const pthread_attr_t *__attr,
436 void *(*__start_routine) (void *),
437 void *__restrict arg);
439 /* The functions called the signal events. */
440 extern void __linuxthreads_create_event (void);
441 extern void __linuxthreads_death_event (void);
442 extern void __linuxthreads_reap_event (void);
444 /* This function is called to initialize the pthread library. */
445 extern void __pthread_initialize (void);
447 /* TSD. */
448 extern int __pthread_internal_tsd_set (int key, const void * pointer);
449 extern void * __pthread_internal_tsd_get (int key);
450 extern void ** __attribute__ ((__const__))
451 __pthread_internal_tsd_address (int key);
453 /* Sighandler wrappers. */
454 extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
455 extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
456 struct ucontext *uc);
457 extern void __pthread_null_sighandler(int sig);
458 extern int __pthread_sigaction (int sig, const struct sigaction *act,
459 struct sigaction *oact);
460 extern int __pthread_sigwait (const sigset_t *set, int *sig);
461 extern int __pthread_raise (int sig);
463 /* Cancellation. */
464 extern int __pthread_enable_asynccancel (void) attribute_hidden;
465 extern void __pthread_disable_asynccancel (int oldtype)
466 internal_function attribute_hidden;
468 /* The two functions are in libc.so and not exported. */
469 extern int __libc_enable_asynccancel (void) attribute_hidden;
470 extern void __libc_disable_asynccancel (int oldtype)
471 internal_function attribute_hidden;
473 /* The two functions are in libc.so and are exported. */
474 extern int __librt_enable_asynccancel (void);
475 extern void __librt_disable_asynccancel (int oldtype) internal_function;
477 extern void __pthread_cleanup_upto (__jmp_buf target,
478 char *targetframe) attribute_hidden;
479 extern pid_t __pthread_fork (struct fork_block *b) attribute_hidden;
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 (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
488 __asm (".globl " __SYMBOL_PREFIX "__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 (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
496 __asm (".globl " __SYMBOL_PREFIX "__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 (".globl " __SYMBOL_PREFIX "__librt_enable_asynccancel"); \
504 __asm (".globl " __SYMBOL_PREFIX "__librt_disable_asynccancel")
505 #else
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. */
509 #endif
511 extern int * __libc_pthread_init (const struct pthread_functions *functions);
513 #if !defined NOT_IN_libc && !defined FLOATING_STACKS
514 # ifdef SHARED
515 # define thread_self() \
516 (*__libc_pthread_functions.ptr_pthread_thread_self) ()
517 # else
518 weak_extern (__pthread_thread_self)
519 # define thread_self() __pthread_thread_self ()
520 # endif
521 #endif
523 #ifndef USE_TLS
524 # define __manager_thread (&__pthread_manager_thread)
525 #else
526 # define __manager_thread __pthread_manager_threadp
527 #endif
529 extern inline __attribute__((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
539 thread. */
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);
545 #endif
546 return self;
549 #endif /* internals.h */