Update.
[glibc.git] / linuxthreads / internals.h
blobe1768b4a1ff6bd5a087384ed4d463df1c13b76e3
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 <unistd.h>
24 #include <stackinfo.h>
26 #include "descr.h"
28 extern long int testandset (int *spinlock);
29 extern int __compare_and_swap (long int *p, long int oldval, long int newval);
31 #include "semaphore.h"
33 #ifndef THREAD_GETMEM
34 # define THREAD_GETMEM(descr, member) descr->member
35 #endif
36 #ifndef THREAD_GETMEM_NC
37 # define THREAD_GETMEM_NC(descr, member) descr->member
38 #endif
39 #ifndef THREAD_SETMEM
40 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
41 #endif
42 #ifndef THREAD_SETMEM_NC
43 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
44 #endif
46 typedef void (*destr_function)(void *);
48 struct pthread_key_struct {
49 int in_use; /* already allocated? */
50 destr_function destr; /* destruction routine */
54 #define PTHREAD_START_ARGS_INITIALIZER(fct) \
55 { (void *(*) (void *)) fct, NULL, {{0, }}, 0, { 0 } }
58 /* The type of thread handles. */
60 typedef struct pthread_handle_struct * pthread_handle;
62 struct pthread_handle_struct {
63 struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
64 pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
65 char * h_bottom; /* Lowest address in the stack thread */
68 /* The type of messages sent to the thread manager thread */
70 struct pthread_request {
71 pthread_descr req_thread; /* Thread doing the request */
72 enum { /* Request kind */
73 REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
74 REQ_POST, REQ_DEBUG, REQ_KICK, REQ_FOR_EACH_THREAD
75 } req_kind;
76 union { /* Arguments for request */
77 struct { /* For REQ_CREATE: */
78 const pthread_attr_t * attr; /* thread attributes */
79 void * (*fn)(void *); /* start function */
80 void * arg; /* argument to start function */
81 sigset_t mask; /* signal mask */
82 } create;
83 struct { /* For REQ_FREE: */
84 pthread_t thread_id; /* identifier of thread to free */
85 } free;
86 struct { /* For REQ_PROCESS_EXIT: */
87 int code; /* exit status */
88 } exit;
89 void * post; /* For REQ_POST: the semaphore */
90 struct { /* For REQ_FOR_EACH_THREAD: callback */
91 void (*fn)(void *, pthread_descr);
92 void *arg;
93 } for_each;
94 } req_args;
98 /* Signals used for suspend/restart and for cancellation notification. */
100 extern int __pthread_sig_restart;
101 extern int __pthread_sig_cancel;
103 /* Signal used for interfacing with gdb */
105 extern int __pthread_sig_debug;
107 /* Global array of thread handles, used for validating a thread id
108 and retrieving the corresponding thread descriptor. Also used for
109 mapping the available stack segments. */
111 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
113 /* Descriptor of the initial thread */
115 extern struct _pthread_descr_struct __pthread_initial_thread;
117 /* Descriptor of the manager thread */
119 extern struct _pthread_descr_struct __pthread_manager_thread;
121 /* Descriptor of the main thread */
123 extern pthread_descr __pthread_main_thread;
125 /* Limit between the stack of the initial thread (above) and the
126 stacks of other threads (below). Aligned on a STACK_SIZE boundary.
127 Initially 0, meaning that the current thread is (by definition)
128 the initial thread. */
130 extern char *__pthread_initial_thread_bos;
132 /* Indicate whether at least one thread has a user-defined stack (if 1),
133 or all threads have stacks supplied by LinuxThreads (if 0). */
135 extern int __pthread_nonstandard_stacks;
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 /* Limits of the thread manager stack. */
148 extern char *__pthread_manager_thread_bos;
149 extern char *__pthread_manager_thread_tos;
151 #ifdef FLOATING_STACKS
152 /* Maximum stack size. */
153 extern size_t __pthread_max_stacksize;
154 #endif
156 /* Pending request for a process-wide exit */
158 extern int __pthread_exit_requested, __pthread_exit_code;
160 /* Set to 1 by gdb if we're debugging */
162 extern volatile int __pthread_threads_debug;
164 /* Globally enabled events. */
165 extern volatile td_thr_events_t __pthread_threads_events;
167 /* Pointer to descriptor of thread with last event. */
168 extern volatile pthread_descr __pthread_last_event;
170 /* Flag which tells whether we are executing on SMP kernel. */
171 extern int __pthread_smp_kernel;
173 /* Return the handle corresponding to a thread id */
175 static inline pthread_handle thread_handle(pthread_t id)
177 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
180 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
182 static inline int invalid_handle(pthread_handle h, pthread_t id)
184 return h->h_descr == NULL || h->h_descr->p_tid != id || h->h_descr->p_terminated;
187 static inline int nonexisting_handle(pthread_handle h, pthread_t id)
189 return h->h_descr == NULL || h->h_descr->p_tid != id;
192 /* Fill in defaults left unspecified by pt-machine.h. */
194 /* We round up a value with page size. */
195 #ifndef page_roundup
196 #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
197 #endif
199 /* The page size we can get from the system. This should likely not be
200 changed by the machine file but, you never know. */
201 #ifndef PAGE_SIZE
202 #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
203 #endif
205 /* The max size of the thread stack segments. If the default
206 THREAD_SELF implementation is used, this must be a power of two and
207 a multiple of PAGE_SIZE. */
208 #ifndef STACK_SIZE
209 #define STACK_SIZE (2 * 1024 * 1024)
210 #endif
212 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
213 #ifndef INITIAL_STACK_SIZE
214 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
215 #endif
217 /* Size of the thread manager stack. The "- 32" avoids wasting space
218 with some malloc() implementations. */
219 #ifndef THREAD_MANAGER_STACK_SIZE
220 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
221 #endif
223 /* The base of the "array" of thread stacks. The array will grow down from
224 here. Defaults to the calculated bottom of the initial application
225 stack. */
226 #ifndef THREAD_STACK_START_ADDRESS
227 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
228 #endif
230 /* Get some notion of the current stack. Need not be exactly the top
231 of the stack, just something somewhere in the current frame. */
232 #ifndef CURRENT_STACK_FRAME
233 #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
234 #endif
236 /* Recover thread descriptor for the current thread */
238 extern pthread_descr __pthread_find_self (void) __attribute__ ((const));
240 static inline pthread_descr thread_self (void) __attribute__ ((const));
241 static inline pthread_descr thread_self (void)
243 #ifdef THREAD_SELF
244 return THREAD_SELF;
245 #else
246 char *sp = CURRENT_STACK_FRAME;
247 if (sp >= __pthread_initial_thread_bos)
248 return &__pthread_initial_thread;
249 else if (sp >= __pthread_manager_thread_bos
250 && sp < __pthread_manager_thread_tos)
251 return &__pthread_manager_thread;
252 else if (__pthread_nonstandard_stacks)
253 return __pthread_find_self();
254 else
255 #ifdef _STACK_GROWS_DOWN
256 return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
257 #else
258 return (pthread_descr)((unsigned long)sp &~ (STACK_SIZE-1));
259 #endif
260 #endif
263 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the architecture
264 doesn't need a memory barrier instruction (e.g. Intel x86). Some
265 architectures distinguish between full, read and write barriers. */
267 #ifndef MEMORY_BARRIER
268 #define MEMORY_BARRIER()
269 #endif
270 #ifndef READ_MEMORY_BARRIER
271 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
272 #endif
273 #ifndef WRITE_MEMORY_BARRIER
274 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
275 #endif
277 /* Max number of times we must spin on a spinlock calling sched_yield().
278 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
280 #ifndef MAX_SPIN_COUNT
281 #define MAX_SPIN_COUNT 50
282 #endif
284 /* Max number of times the spinlock in the adaptive mutex implementation
285 spins actively on SMP systems. */
287 #ifndef MAX_ADAPTIVE_SPIN_COUNT
288 #define MAX_ADAPTIVE_SPIN_COUNT 100
289 #endif
291 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
292 after MAX_SPIN_COUNT iterations of sched_yield().
293 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
294 (Otherwise the kernel does busy-waiting for realtime threads,
295 giving other threads no chance to run.) */
297 #ifndef SPIN_SLEEP_DURATION
298 #define SPIN_SLEEP_DURATION 2000001
299 #endif
301 /* Debugging */
303 #ifdef DEBUG
304 #include <assert.h>
305 #define ASSERT assert
306 #define MSG __pthread_message
307 #else
308 #define ASSERT(x)
309 #define MSG(msg,arg...)
310 #endif
312 /* Internal global functions */
314 extern void __pthread_do_exit (void *retval, char *currentframe)
315 __attribute__ ((__noreturn__));
316 extern void __pthread_destroy_specifics (void);
317 extern void __pthread_perform_cleanup (char *currentframe);
318 extern void __pthread_init_max_stacksize (void);
319 extern int __pthread_initialize_manager (void);
320 extern void __pthread_message (char * fmt, ...);
321 extern int __pthread_manager (void *reqfd);
322 extern int __pthread_manager_event (void *reqfd);
323 extern void __pthread_manager_sighandler (int sig);
324 extern void __pthread_reset_main_thread (void);
325 extern void __pthread_once_fork_prepare (void);
326 extern void __pthread_once_fork_parent (void);
327 extern void __pthread_once_fork_child (void);
328 extern void __flockfilelist (void);
329 extern void __funlockfilelist (void);
330 extern void __fresetlockfiles (void);
331 extern void __pthread_manager_adjust_prio (int thread_prio);
332 extern void __pthread_initialize_minimal (void);
334 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
335 size_t __guardsize);
336 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
337 size_t *__guardsize);
338 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
339 void *__stackaddr);
340 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
341 void **__stackaddr);
342 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
343 size_t __stacksize);
344 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
345 size_t *__stacksize);
346 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
347 size_t __stacksize);
348 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
349 size_t *__stacksize);
350 extern int __pthread_getconcurrency (void);
351 extern int __pthread_setconcurrency (int __level);
352 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
353 const struct timespec *__abstime);
354 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
355 int *__pshared);
356 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
357 int __pshared);
358 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
359 int *__kind);
360 extern void __pthread_kill_other_threads_np (void);
362 extern void __pthread_restart_old(pthread_descr th);
363 extern void __pthread_suspend_old(pthread_descr self);
364 extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abs);
366 extern void __pthread_restart_new(pthread_descr th);
367 extern void __pthread_suspend_new(pthread_descr self);
368 extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abs);
370 extern void __pthread_wait_for_restart_signal(pthread_descr self);
372 extern int __pthread_yield (void);
374 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
375 __const struct timespec *__restrict
376 __abstime);
377 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
378 __const struct timespec *__restrict
379 __abstime);
380 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
382 extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
383 __restrict __attr,
384 int *__restrict __pshared);
386 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
387 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
388 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
389 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
390 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
392 extern int __pthread_clock_gettime (hp_timing_t freq, struct timespec *tp);
393 extern void __pthread_clock_settime (hp_timing_t offset);
396 /* Global pointers to old or new suspend functions */
398 extern void (*__pthread_restart)(pthread_descr);
399 extern void (*__pthread_suspend)(pthread_descr);
400 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
402 /* Prototypes for the function without cancelation support when the
403 normal version has it. */
404 extern int __libc_close (int fd);
405 extern int __libc_nanosleep (const struct timespec *requested_time,
406 struct timespec *remaining);
407 /* Prototypes for some of the new semaphore functions. */
408 extern int __new_sem_post (sem_t * sem);
409 extern int __new_sem_init (sem_t *__sem, int __pshared, unsigned int __value);
410 extern int __new_sem_wait (sem_t *__sem);
411 extern int __new_sem_trywait (sem_t *__sem);
412 extern int __new_sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
413 extern int __new_sem_destroy (sem_t *__sem);
415 /* Prototypes for compatibility functions. */
416 extern int __pthread_attr_init_2_1 (pthread_attr_t *__attr);
417 extern int __pthread_attr_init_2_0 (pthread_attr_t *__attr);
418 extern int __pthread_create_2_1 (pthread_t *__restrict __thread,
419 const pthread_attr_t *__attr,
420 void *(*__start_routine) (void *),
421 void *__restrict __arg);
422 extern int __pthread_create_2_0 (pthread_t *__restrict thread,
423 const pthread_attr_t *__attr,
424 void *(*__start_routine) (void *),
425 void *__restrict arg);
427 /* The functions called the signal events. */
428 extern void __linuxthreads_create_event (void);
429 extern void __linuxthreads_death_event (void);
430 extern void __linuxthreads_reap_event (void);
432 /* This function is called to initialize the pthread library. */
433 extern void __pthread_initialize (void);
435 #endif /* internals.h */