Fix parameter name.
[glibc/pb-stable.git] / linuxthreads / internals.h
blobd3b8bbe5e68217318c48d2a3a7e30cf2f620758f
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;
261 /* Debugging */
263 #ifdef DEBUG
264 #include <assert.h>
265 #define ASSERT assert
266 #define MSG __pthread_message
267 #else
268 #define ASSERT(x)
269 #define MSG(msg,arg...)
270 #endif
272 /* Internal global functions */
274 extern void __pthread_do_exit (void *retval, char *currentframe)
275 __attribute__ ((__noreturn__));
276 extern void __pthread_destroy_specifics (void);
277 extern void __pthread_perform_cleanup (char *currentframe);
278 extern void __pthread_init_max_stacksize (void);
279 extern int __pthread_initialize_manager (void);
280 extern void __pthread_message (const char * fmt, ...);
281 extern int __pthread_manager (void *reqfd);
282 extern int __pthread_manager_event (void *reqfd);
283 extern void __pthread_manager_sighandler (int sig);
284 extern void __pthread_reset_main_thread (void);
285 extern void __pthread_once_fork_prepare (void);
286 extern void __pthread_once_fork_parent (void);
287 extern void __pthread_once_fork_child (void);
288 extern void __flockfilelist (void);
289 extern void __funlockfilelist (void);
290 extern void __fresetlockfiles (void);
291 extern void __pthread_manager_adjust_prio (int thread_prio);
292 extern void __pthread_initialize_minimal (void);
294 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
295 size_t __guardsize);
296 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
297 size_t *__guardsize);
298 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
299 void *__stackaddr);
300 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
301 void **__stackaddr);
302 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
303 size_t __stacksize);
304 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
305 size_t *__stacksize);
306 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
307 size_t __stacksize);
308 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
309 size_t *__stacksize);
310 extern int __pthread_attr_destroy (pthread_attr_t *attr);
311 extern int __pthread_attr_setdetachstate (pthread_attr_t *attr,
312 int detachstate);
313 extern int __pthread_attr_getdetachstate (const pthread_attr_t *attr,
314 int *detachstate);
315 extern int __pthread_attr_setschedparam (pthread_attr_t *attr,
316 const struct sched_param *param);
317 extern int __pthread_attr_getschedparam (const pthread_attr_t *attr,
318 struct sched_param *param);
319 extern int __pthread_attr_setschedpolicy (pthread_attr_t *attr, int policy);
320 extern int __pthread_attr_getschedpolicy (const pthread_attr_t *attr,
321 int *policy);
322 extern int __pthread_attr_setinheritsched (pthread_attr_t *attr, int inherit);
323 extern int __pthread_attr_getinheritsched (const pthread_attr_t *attr,
324 int *inherit);
325 extern int __pthread_attr_setscope (pthread_attr_t *attr, int scope);
326 extern int __pthread_attr_getscope (const pthread_attr_t *attr, int *scope);
328 extern int __pthread_getconcurrency (void);
329 extern int __pthread_setconcurrency (int __level);
330 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
331 const struct timespec *__abstime);
332 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
333 int *__pshared);
334 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
335 int __pshared);
336 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
337 int *__kind);
338 extern void __pthread_kill_other_threads_np (void);
339 extern int __pthread_mutex_init (pthread_mutex_t *__mutex,
340 __const pthread_mutexattr_t *__mutex_attr);
341 extern int __pthread_mutex_destroy (pthread_mutex_t *__mutex);
342 extern int __pthread_mutex_lock (pthread_mutex_t *__mutex);
343 extern int __pthread_mutex_trylock (pthread_mutex_t *__mutex);
344 extern int __pthread_mutex_unlock (pthread_mutex_t *__mutex);
345 #if defined NOT_IN_libc && defined IS_IN_libpthread
346 hidden_proto (__pthread_mutex_init)
347 hidden_proto (__pthread_mutex_destroy)
348 hidden_proto (__pthread_mutex_lock)
349 hidden_proto (__pthread_mutex_trylock)
350 hidden_proto (__pthread_mutex_unlock)
351 #endif
352 extern int __pthread_cond_init (pthread_cond_t *cond,
353 const pthread_condattr_t *cond_attr);
354 extern int __pthread_cond_destroy (pthread_cond_t *cond);
355 extern int __pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex);
356 extern int __pthread_cond_signal (pthread_cond_t *cond);
357 extern int __pthread_cond_broadcast (pthread_cond_t *cond);
358 extern int __pthread_condattr_init (pthread_condattr_t *attr);
359 extern int __pthread_condattr_destroy (pthread_condattr_t *attr);
360 extern pthread_t __pthread_self (void);
361 extern pthread_descr __pthread_thread_self (void);
362 extern int __pthread_equal (pthread_t thread1, pthread_t thread2);
363 extern void __pthread_exit (void *retval);
364 extern int __pthread_getschedparam (pthread_t thread, int *policy,
365 struct sched_param *param);
366 extern int __pthread_setschedparam (pthread_t thread, int policy,
367 const struct sched_param *param);
368 extern int __pthread_setcancelstate (int state, int * oldstate);
369 extern int __pthread_setcanceltype (int type, int * oldtype);
371 extern void __pthread_restart_old(pthread_descr th);
372 extern void __pthread_suspend_old(pthread_descr self);
373 extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abs);
375 extern void __pthread_restart_new(pthread_descr th);
376 extern void __pthread_suspend_new(pthread_descr self);
377 extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abs);
379 extern void __pthread_wait_for_restart_signal(pthread_descr self);
381 extern void __pthread_sigsuspend (const sigset_t *mask) attribute_hidden;
383 extern int __pthread_yield (void);
385 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
386 __const struct timespec *__restrict
387 __abstime);
388 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
389 __const struct timespec *__restrict
390 __abstime);
391 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
393 extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
394 __restrict __attr,
395 int *__restrict __pshared);
397 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
398 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
399 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
400 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
401 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
403 extern int __pthread_clock_gettime (hp_timing_t freq, struct timespec *tp);
404 extern void __pthread_clock_settime (hp_timing_t offset);
406 /* Global pointers to old or new suspend functions */
408 extern void (*__pthread_restart)(pthread_descr);
409 extern void (*__pthread_suspend)(pthread_descr);
410 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
412 /* Prototypes for the function without cancelation support when the
413 normal version has it. */
414 extern int __libc_close (int fd);
415 extern int __libc_nanosleep (const struct timespec *requested_time,
416 struct timespec *remaining);
417 /* Prototypes for some of the new semaphore functions. */
418 extern int __new_sem_post (sem_t * sem);
419 extern int __new_sem_init (sem_t *__sem, int __pshared, unsigned int __value);
420 extern int __new_sem_wait (sem_t *__sem);
421 extern int __new_sem_trywait (sem_t *__sem);
422 extern int __new_sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
423 extern int __new_sem_destroy (sem_t *__sem);
425 /* Prototypes for compatibility functions. */
426 extern int __pthread_attr_init_2_1 (pthread_attr_t *__attr);
427 extern int __pthread_attr_init_2_0 (pthread_attr_t *__attr);
428 extern int __pthread_create_2_1 (pthread_t *__restrict __threadp,
429 const pthread_attr_t *__attr,
430 void *(*__start_routine) (void *),
431 void *__restrict __arg);
432 extern int __pthread_create_2_0 (pthread_t *__restrict thread,
433 const pthread_attr_t *__attr,
434 void *(*__start_routine) (void *),
435 void *__restrict arg);
437 /* The functions called the signal events. */
438 extern void __linuxthreads_create_event (void);
439 extern void __linuxthreads_death_event (void);
440 extern void __linuxthreads_reap_event (void);
442 /* This function is called to initialize the pthread library. */
443 extern void __pthread_initialize (void);
445 /* TSD. */
446 extern int __pthread_internal_tsd_set (int key, const void * pointer);
447 extern void * __pthread_internal_tsd_get (int key);
448 extern void ** __attribute__ ((__const__))
449 __pthread_internal_tsd_address (int key);
451 /* Sighandler wrappers. */
452 extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
453 extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
454 struct ucontext *uc);
455 extern void __pthread_null_sighandler(int sig);
456 extern int __pthread_sigaction (int sig, const struct sigaction *act,
457 struct sigaction *oact);
458 extern int __pthread_sigwait (const sigset_t *set, int *sig);
459 extern int __pthread_raise (int sig);
461 /* Cancellation. */
462 extern int __pthread_enable_asynccancel (void) attribute_hidden;
463 extern void __pthread_disable_asynccancel (int oldtype)
464 internal_function attribute_hidden;
466 /* The two functions are in libc.so and not exported. */
467 extern int __libc_enable_asynccancel (void) attribute_hidden;
468 extern void __libc_disable_asynccancel (int oldtype)
469 internal_function attribute_hidden;
471 extern void __pthread_cleanup_upto (__jmp_buf target,
472 char *targetframe) attribute_hidden;
473 extern pid_t __pthread_fork (struct fork_block *b) attribute_hidden;
475 #if !defined NOT_IN_libc
476 # define LIBC_CANCEL_ASYNC() \
477 __libc_enable_asynccancel ()
478 # define LIBC_CANCEL_RESET(oldtype) \
479 __libc_disable_asynccancel (oldtype)
480 # define LIBC_CANCEL_HANDLED() \
481 __asm (".globl " __SYMBOL_PREFIX "__libc_enable_asynccancel"); \
482 __asm (".globl " __SYMBOL_PREFIX "__libc_disable_asynccancel")
483 #elif defined NOT_IN_libc && defined IS_IN_libpthread
484 # define LIBC_CANCEL_ASYNC() \
485 __pthread_enable_asynccancel ()
486 # define LIBC_CANCEL_RESET(oldtype) \
487 __pthread_disable_asynccancel (oldtype)
488 # define LIBC_CANCEL_HANDLED() \
489 __asm (".globl " __SYMBOL_PREFIX "__pthread_enable_asynccancel"); \
490 __asm (".globl " __SYMBOL_PREFIX "__pthread_disable_asynccancel")
491 #else
492 # define LIBC_CANCEL_ASYNC() 0 /* Just a dummy value. */
493 # define LIBC_CANCEL_RESET(val) ((void)(val)) /* Nothing, but evaluate it. */
494 # define LIBC_CANCEL_HANDLED() /* Nothing. */
495 #endif
497 extern int * __libc_pthread_init (const struct pthread_functions *functions);
499 #if !defined NOT_IN_libc && !defined FLOATING_STACKS
500 # ifdef SHARED
501 # define thread_self() \
502 (*__libc_pthread_functions.ptr_pthread_thread_self) ()
503 # else
504 weak_extern (__pthread_thread_self)
505 # define thread_self() __pthread_thread_self ()
506 # endif
507 #endif
509 #endif /* internals.h */