Update.
[glibc.git] / linuxthreads / internals.h
blobd5b469b347b2b8236ec81c02befc8e6489013a94
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 <resolv.h>
24 #include <setjmp.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <stackinfo.h>
28 #include <sys/types.h>
29 #include <bits/libc-tsd.h> /* for _LIBC_TSD_KEY_N */
31 extern long int testandset (int *spinlock);
32 extern int __compare_and_swap (long int *p, long int oldval, long int newval);
34 #include "pt-machine.h"
35 #include "semaphore.h"
36 #include "../linuxthreads_db/thread_dbP.h"
37 #include <hp-timing.h>
39 #ifndef THREAD_GETMEM
40 # define THREAD_GETMEM(descr, member) descr->member
41 #endif
42 #ifndef THREAD_GETMEM_NC
43 # define THREAD_GETMEM_NC(descr, member) descr->member
44 #endif
45 #ifndef THREAD_SETMEM
46 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
47 #endif
48 #ifndef THREAD_SETMEM_NC
49 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
50 #endif
52 /* Arguments passed to thread creation routine */
54 struct pthread_start_args {
55 void * (*start_routine)(void *); /* function to run */
56 void * arg; /* its argument */
57 sigset_t mask; /* initial signal mask for thread */
58 int schedpolicy; /* initial scheduling policy (if any) */
59 struct sched_param schedparam; /* initial scheduling parameters (if any) */
63 /* We keep thread specific data in a special data structure, a two-level
64 array. The top-level array contains pointers to dynamically allocated
65 arrays of a certain number of data pointers. So we can implement a
66 sparse array. Each dynamic second-level array has
67 PTHREAD_KEY_2NDLEVEL_SIZE
68 entries. This value shouldn't be too large. */
69 #define PTHREAD_KEY_2NDLEVEL_SIZE 32
71 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
72 keys in each subarray. */
73 #define PTHREAD_KEY_1STLEVEL_SIZE \
74 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
75 / PTHREAD_KEY_2NDLEVEL_SIZE)
77 typedef void (*destr_function)(void *);
79 struct pthread_key_struct {
80 int in_use; /* already allocated? */
81 destr_function destr; /* destruction routine */
85 #define PTHREAD_START_ARGS_INITIALIZER(fct) \
86 { (void *(*) (void *)) fct, NULL, {{0, }}, 0, { 0 } }
88 /* The type of thread descriptors */
90 typedef struct _pthread_descr_struct * pthread_descr;
92 /* Callback interface for removing the thread from waiting on an
93 object if it is cancelled while waiting or about to wait.
94 This hold a pointer to the object, and a pointer to a function
95 which ``extricates'' the thread from its enqueued state.
96 The function takes two arguments: pointer to the wait object,
97 and a pointer to the thread. It returns 1 if an extrication
98 actually occured, and hence the thread must also be signalled.
99 It returns 0 if the thread had already been extricated. */
101 typedef struct _pthread_extricate_struct {
102 void *pu_object;
103 int (*pu_extricate_func)(void *, pthread_descr);
104 } pthread_extricate_if;
106 /* Atomic counter made possible by compare_and_swap */
108 struct pthread_atomic {
109 long p_count;
110 int p_spinlock;
113 /* Context info for read write locks. The pthread_rwlock_info structure
114 is information about a lock that has been read-locked by the thread
115 in whose list this structure appears. The pthread_rwlock_context
116 is embedded in the thread context and contains a pointer to the
117 head of the list of lock info structures, as well as a count of
118 read locks that are untracked, because no info structure could be
119 allocated for them. */
121 struct _pthread_rwlock_t;
123 typedef struct _pthread_rwlock_info {
124 struct _pthread_rwlock_info *pr_next;
125 struct _pthread_rwlock_t *pr_lock;
126 int pr_lock_count;
127 } pthread_readlock_info;
129 struct _pthread_descr_struct {
130 union {
131 struct {
132 pthread_descr self; /* Pointer to this structure */
133 } data;
134 void *__padding[16];
135 } p_header;
136 pthread_descr p_nextlive, p_prevlive;
137 /* Double chaining of active threads */
138 pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
139 pthread_descr p_nextlock; /* can be on a queue and waiting on a lock */
140 pthread_t p_tid; /* Thread identifier */
141 int p_pid; /* PID of Unix process */
142 int p_priority; /* Thread priority (== 0 if not realtime) */
143 struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
144 int p_signal; /* last signal received */
145 sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
146 sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
147 char p_terminated; /* true if terminated e.g. by pthread_exit */
148 char p_detached; /* true if detached */
149 char p_exited; /* true if the assoc. process terminated */
150 void * p_retval; /* placeholder for return value */
151 int p_retcode; /* placeholder for return code */
152 pthread_descr p_joining; /* thread joining on that thread or NULL */
153 struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
154 char p_cancelstate; /* cancellation state */
155 char p_canceltype; /* cancellation type (deferred/async) */
156 char p_canceled; /* cancellation request pending */
157 int * p_errnop; /* pointer to used errno variable */
158 int p_errno; /* error returned by last system call */
159 int * p_h_errnop; /* pointer to used h_errno variable */
160 int p_h_errno; /* error returned by last netdb function */
161 char * p_in_sighandler; /* stack address of sighandler, or NULL */
162 char p_sigwaiting; /* true if a sigwait() is in progress */
163 struct pthread_start_args p_start_args; /* arguments for thread creation */
164 void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
165 void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
166 int p_userstack; /* nonzero if the user provided the stack */
167 void *p_guardaddr; /* address of guard area or NULL */
168 size_t p_guardsize; /* size of guard area */
169 int p_nr; /* Index of descriptor in __pthread_handles */
170 int p_report_events; /* Nonzero if events must be reported. */
171 td_eventbuf_t p_eventbuf; /* Data for event. */
172 struct pthread_atomic p_resume_count; /* number of times restart() was
173 called on thread */
174 char p_woken_by_cancel; /* cancellation performed wakeup */
175 char p_condvar_avail; /* flag if conditional variable became avail */
176 char p_sem_avail; /* flag if semaphore became available */
177 pthread_extricate_if *p_extricate; /* See above */
178 pthread_readlock_info *p_readlock_list; /* List of readlock info structs */
179 pthread_readlock_info *p_readlock_free; /* Free list of structs */
180 int p_untracked_readlock_count; /* Readlocks not tracked by list */
181 struct __res_state *p_resp; /* Pointer to resolver state */
182 struct __res_state p_res; /* per-thread resolver state */
183 int p_inheritsched; /* copied from the thread attribute */
184 #if HP_TIMING_AVAIL
185 hp_timing_t p_cpuclock_offset; /* Initial CPU clock for thread. */
186 #endif
187 /* New elements must be added at the end. */
188 } __attribute__ ((aligned(32))); /* We need to align the structure so that
189 doubles are aligned properly. This is 8
190 bytes on MIPS and 16 bytes on MIPS64.
191 32 bytes might give better cache
192 utilization. */
195 /* The type of thread handles. */
197 typedef struct pthread_handle_struct * pthread_handle;
199 struct pthread_handle_struct {
200 struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
201 pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
202 char * h_bottom; /* Lowest address in the stack thread */
205 /* The type of messages sent to the thread manager thread */
207 struct pthread_request {
208 pthread_descr req_thread; /* Thread doing the request */
209 enum { /* Request kind */
210 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
211 REQ_POST, REQ_DEBUG, REQ_KICK
212 } req_kind;
213 union { /* Arguments for request */
214 struct { /* For REQ_CREATE: */
215 const pthread_attr_t * attr; /* thread attributes */
216 void * (*fn)(void *); /* start function */
217 void * arg; /* argument to start function */
218 sigset_t mask; /* signal mask */
219 } create;
220 struct { /* For REQ_FREE: */
221 pthread_t thread_id; /* identifier of thread to free */
222 } free;
223 struct { /* For REQ_PROCESS_EXIT: */
224 int code; /* exit status */
225 } exit;
226 void * post; /* For REQ_POST: the semaphore */
227 } req_args;
231 /* Signals used for suspend/restart and for cancellation notification. */
233 extern int __pthread_sig_restart;
234 extern int __pthread_sig_cancel;
236 /* Signal used for interfacing with gdb */
238 extern int __pthread_sig_debug;
240 /* Global array of thread handles, used for validating a thread id
241 and retrieving the corresponding thread descriptor. Also used for
242 mapping the available stack segments. */
244 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
246 /* Descriptor of the initial thread */
248 extern struct _pthread_descr_struct __pthread_initial_thread;
250 /* Descriptor of the manager thread */
252 extern struct _pthread_descr_struct __pthread_manager_thread;
254 /* Descriptor of the main thread */
256 extern pthread_descr __pthread_main_thread;
258 /* Limit between the stack of the initial thread (above) and the
259 stacks of other threads (below). Aligned on a STACK_SIZE boundary.
260 Initially 0, meaning that the current thread is (by definition)
261 the initial thread. */
263 extern char *__pthread_initial_thread_bos;
265 /* Indicate whether at least one thread has a user-defined stack (if 1),
266 or all threads have stacks supplied by LinuxThreads (if 0). */
268 extern int __pthread_nonstandard_stacks;
270 /* File descriptor for sending requests to the thread manager.
271 Initially -1, meaning that __pthread_initialize_manager must be called. */
273 extern int __pthread_manager_request;
275 /* Other end of the pipe for sending requests to the thread manager. */
277 extern int __pthread_manager_reader;
279 /* Limits of the thread manager stack. */
281 extern char *__pthread_manager_thread_bos;
282 extern char *__pthread_manager_thread_tos;
284 #ifdef FLOATING_STACKS
285 /* Maximum stack size. */
286 extern size_t __pthread_max_stacksize;
287 #endif
289 /* Pending request for a process-wide exit */
291 extern int __pthread_exit_requested, __pthread_exit_code;
293 /* Set to 1 by gdb if we're debugging */
295 extern volatile int __pthread_threads_debug;
297 /* Globally enabled events. */
298 extern volatile td_thr_events_t __pthread_threads_events;
300 /* Pointer to descriptor of thread with last event. */
301 extern volatile pthread_descr __pthread_last_event;
303 /* Flag which tells whether we are executing on SMP kernel. */
304 extern int __pthread_smp_kernel;
306 /* Return the handle corresponding to a thread id */
308 static inline pthread_handle thread_handle(pthread_t id)
310 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
313 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
315 static inline int invalid_handle(pthread_handle h, pthread_t id)
317 return h->h_descr == NULL || h->h_descr->p_tid != id || h->h_descr->p_terminated;
320 static inline int nonexisting_handle(pthread_handle h, pthread_t id)
322 return h->h_descr == NULL || h->h_descr->p_tid != id;
325 /* Fill in defaults left unspecified by pt-machine.h. */
327 /* We round up a value with page size. */
328 #ifndef page_roundup
329 #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
330 #endif
332 /* The page size we can get from the system. This should likely not be
333 changed by the machine file but, you never know. */
334 #ifndef PAGE_SIZE
335 #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
336 #endif
338 /* The max size of the thread stack segments. If the default
339 THREAD_SELF implementation is used, this must be a power of two and
340 a multiple of PAGE_SIZE. */
341 #ifndef STACK_SIZE
342 #define STACK_SIZE (2 * 1024 * 1024)
343 #endif
345 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
346 #ifndef INITIAL_STACK_SIZE
347 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
348 #endif
350 /* Size of the thread manager stack. The "- 32" avoids wasting space
351 with some malloc() implementations. */
352 #ifndef THREAD_MANAGER_STACK_SIZE
353 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
354 #endif
356 /* The base of the "array" of thread stacks. The array will grow down from
357 here. Defaults to the calculated bottom of the initial application
358 stack. */
359 #ifndef THREAD_STACK_START_ADDRESS
360 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
361 #endif
363 /* Get some notion of the current stack. Need not be exactly the top
364 of the stack, just something somewhere in the current frame. */
365 #ifndef CURRENT_STACK_FRAME
366 #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
367 #endif
369 /* Recover thread descriptor for the current thread */
371 extern pthread_descr __pthread_find_self (void) __attribute__ ((const));
373 static inline pthread_descr thread_self (void) __attribute__ ((const));
374 static inline pthread_descr thread_self (void)
376 #ifdef THREAD_SELF
377 return THREAD_SELF;
378 #else
379 char *sp = CURRENT_STACK_FRAME;
380 if (sp >= __pthread_initial_thread_bos)
381 return &__pthread_initial_thread;
382 else if (sp >= __pthread_manager_thread_bos
383 && sp < __pthread_manager_thread_tos)
384 return &__pthread_manager_thread;
385 else if (__pthread_nonstandard_stacks)
386 return __pthread_find_self();
387 else
388 #ifdef _STACK_GROWS_DOWN
389 return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
390 #else
391 return (pthread_descr)((unsigned long)sp &~ (STACK_SIZE-1));
392 #endif
393 #endif
396 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the architecture
397 doesn't need a memory barrier instruction (e.g. Intel x86). Some
398 architectures distinguish between full, read and write barriers. */
400 #ifndef MEMORY_BARRIER
401 #define MEMORY_BARRIER()
402 #endif
403 #ifndef READ_MEMORY_BARRIER
404 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
405 #endif
406 #ifndef WRITE_MEMORY_BARRIER
407 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
408 #endif
410 /* Max number of times we must spin on a spinlock calling sched_yield().
411 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
413 #ifndef MAX_SPIN_COUNT
414 #define MAX_SPIN_COUNT 50
415 #endif
417 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
418 after MAX_SPIN_COUNT iterations of sched_yield().
419 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
420 (Otherwise the kernel does busy-waiting for realtime threads,
421 giving other threads no chance to run.) */
423 #ifndef SPIN_SLEEP_DURATION
424 #define SPIN_SLEEP_DURATION 2000001
425 #endif
427 /* Debugging */
429 #ifdef DEBUG
430 #include <assert.h>
431 #define ASSERT assert
432 #define MSG __pthread_message
433 #else
434 #define ASSERT(x)
435 #define MSG(msg,arg...)
436 #endif
438 /* Internal global functions */
440 extern void __pthread_do_exit (void *retval, char *currentframe)
441 __attribute__ ((__noreturn__));
442 extern void __pthread_destroy_specifics (void);
443 extern void __pthread_perform_cleanup (char *currentframe);
444 extern void __pthread_init_max_stacksize (void);
445 extern int __pthread_initialize_manager (void);
446 extern void __pthread_message (char * fmt, ...);
447 extern int __pthread_manager (void *reqfd);
448 extern int __pthread_manager_event (void *reqfd);
449 extern void __pthread_manager_sighandler (int sig);
450 extern void __pthread_reset_main_thread (void);
451 extern void __pthread_once_fork_prepare (void);
452 extern void __pthread_once_fork_parent (void);
453 extern void __pthread_once_fork_child (void);
454 extern void __flockfilelist (void);
455 extern void __funlockfilelist (void);
456 extern void __fresetlockfiles (void);
457 extern void __pthread_manager_adjust_prio (int thread_prio);
458 extern void __pthread_initialize_minimal (void);
460 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
461 size_t __guardsize);
462 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
463 size_t *__guardsize);
464 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
465 void *__stackaddr);
466 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
467 void **__stackaddr);
468 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
469 size_t __stacksize);
470 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
471 size_t *__stacksize);
472 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
473 size_t __stacksize);
474 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
475 size_t *__stacksize);
476 extern int __pthread_getconcurrency (void);
477 extern int __pthread_setconcurrency (int __level);
478 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
479 const struct timespec *__abstime);
480 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
481 int *__pshared);
482 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
483 int __pshared);
484 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
485 int *__kind);
486 extern void __pthread_kill_other_threads_np (void);
488 extern void __pthread_restart_old(pthread_descr th);
489 extern void __pthread_suspend_old(pthread_descr self);
490 extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abs);
492 extern void __pthread_restart_new(pthread_descr th);
493 extern void __pthread_suspend_new(pthread_descr self);
494 extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abs);
496 extern void __pthread_wait_for_restart_signal(pthread_descr self);
498 extern int __pthread_yield (void);
500 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
501 __const struct timespec *__restrict
502 __abstime);
503 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
504 __const struct timespec *__restrict
505 __abstime);
506 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
508 extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
509 __restrict __attr,
510 int *__restrict __pshared);
512 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
513 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
514 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
515 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
516 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
518 extern int __pthread_clock_gettime (hp_timing_t freq, struct timespec *tp);
519 extern void __pthread_clock_settime (hp_timing_t offset);
522 /* Global pointers to old or new suspend functions */
524 extern void (*__pthread_restart)(pthread_descr);
525 extern void (*__pthread_suspend)(pthread_descr);
526 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
528 /* Prototypes for the function without cancelation support when the
529 normal version has it. */
530 extern int __libc_close (int fd);
531 extern int __libc_nanosleep (const struct timespec *requested_time,
532 struct timespec *remaining);
533 /* Prototypes for some of the new semaphore functions. */
534 extern int __new_sem_post (sem_t * sem);
535 extern int __new_sem_init (sem_t *__sem, int __pshared, unsigned int __value);
536 extern int __new_sem_wait (sem_t *__sem);
537 extern int __new_sem_trywait (sem_t *__sem);
538 extern int __new_sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
539 extern int __new_sem_destroy (sem_t *__sem);
541 /* Prototypes for compatibility functions. */
542 extern int __pthread_attr_init_2_1 (pthread_attr_t *__attr);
543 extern int __pthread_attr_init_2_0 (pthread_attr_t *__attr);
544 extern int __pthread_create_2_1 (pthread_t *__restrict __thread,
545 const pthread_attr_t *__attr,
546 void *(*__start_routine) (void *),
547 void *__restrict __arg);
548 extern int __pthread_create_2_0 (pthread_t *__restrict thread,
549 const pthread_attr_t *__attr,
550 void *(*__start_routine) (void *),
551 void *__restrict arg);
553 /* The functions called the signal events. */
554 extern void __linuxthreads_create_event (void);
555 extern void __linuxthreads_death_event (void);
556 extern void __linuxthreads_reap_event (void);
558 /* This function is called to initialize the pthread library. */
559 extern void __pthread_initialize (void);
561 #endif /* internals.h */