* sysdeps/m68k/fpu/s_isinf.c (INTDEFX): Define to get correct
[glibc.git] / linuxthreads / internals.h
blob64fb6bb602309b1676be7130d364fee9617672d4
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"
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;
99 typedef void (*arch_sighandler_t) (int, SIGCONTEXT);
100 union sighandler
102 arch_sighandler_t old;
103 void (*rt) (int, struct siginfo *, struct ucontext *);
105 extern union sighandler __sighandler[NSIG];
108 /* Signals used for suspend/restart and for cancellation notification. */
110 extern int __pthread_sig_restart;
111 extern int __pthread_sig_cancel;
113 /* Signal used for interfacing with gdb */
115 extern int __pthread_sig_debug;
117 /* Global array of thread handles, used for validating a thread id
118 and retrieving the corresponding thread descriptor. Also used for
119 mapping the available stack segments. */
121 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
123 /* Descriptor of the main thread */
125 extern pthread_descr __pthread_main_thread;
127 /* File descriptor for sending requests to the thread manager.
128 Initially -1, meaning that __pthread_initialize_manager must be called. */
130 extern int __pthread_manager_request;
132 /* Other end of the pipe for sending requests to the thread manager. */
134 extern int __pthread_manager_reader;
136 #ifdef FLOATING_STACKS
137 /* Maximum stack size. */
138 extern size_t __pthread_max_stacksize;
139 #endif
141 /* Pending request for a process-wide exit */
143 extern int __pthread_exit_requested, __pthread_exit_code;
145 /* Set to 1 by gdb if we're debugging */
147 extern volatile int __pthread_threads_debug;
149 /* Globally enabled events. */
150 extern volatile td_thr_events_t __pthread_threads_events;
152 /* Pointer to descriptor of thread with last event. */
153 extern volatile pthread_descr __pthread_last_event;
155 /* Flag which tells whether we are executing on SMP kernel. */
156 extern int __pthread_smp_kernel;
158 /* Return the handle corresponding to a thread id */
160 static inline pthread_handle thread_handle(pthread_t id)
162 return &__pthread_handles[id % PTHREAD_THREADS_MAX];
165 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
167 static inline int invalid_handle(pthread_handle h, pthread_t id)
169 return h->h_descr == NULL || h->h_descr->p_tid != id || h->h_descr->p_terminated;
172 static inline int nonexisting_handle(pthread_handle h, pthread_t id)
174 return h->h_descr == NULL || h->h_descr->p_tid != id;
177 /* Fill in defaults left unspecified by pt-machine.h. */
179 /* We round up a value with page size. */
180 #ifndef page_roundup
181 #define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
182 #endif
184 /* The page size we can get from the system. This should likely not be
185 changed by the machine file but, you never know. */
186 #ifndef PAGE_SIZE
187 #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
188 #endif
190 /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
191 #ifndef INITIAL_STACK_SIZE
192 #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
193 #endif
195 /* Size of the thread manager stack. The "- 32" avoids wasting space
196 with some malloc() implementations. */
197 #ifndef THREAD_MANAGER_STACK_SIZE
198 #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
199 #endif
201 /* The base of the "array" of thread stacks. The array will grow down from
202 here. Defaults to the calculated bottom of the initial application
203 stack. */
204 #ifndef THREAD_STACK_START_ADDRESS
205 #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
206 #endif
208 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
209 architecture doesn't need a memory barrier instruction (e.g. Intel
210 x86). Still we need the compiler to respect the barrier and emit
211 all outstanding operations which modify memory. Some architectures
212 distinguish between full, read and write barriers. */
214 #ifndef MEMORY_BARRIER
215 #define MEMORY_BARRIER() asm ("" : : : "memory")
216 #endif
217 #ifndef READ_MEMORY_BARRIER
218 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
219 #endif
220 #ifndef WRITE_MEMORY_BARRIER
221 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
222 #endif
224 /* Max number of times we must spin on a spinlock calling sched_yield().
225 After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
227 #ifndef MAX_SPIN_COUNT
228 #define MAX_SPIN_COUNT 50
229 #endif
231 /* Max number of times the spinlock in the adaptive mutex implementation
232 spins actively on SMP systems. */
234 #ifndef MAX_ADAPTIVE_SPIN_COUNT
235 #define MAX_ADAPTIVE_SPIN_COUNT 100
236 #endif
238 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
239 after MAX_SPIN_COUNT iterations of sched_yield().
240 With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
241 (Otherwise the kernel does busy-waiting for realtime threads,
242 giving other threads no chance to run.) */
244 #ifndef SPIN_SLEEP_DURATION
245 #define SPIN_SLEEP_DURATION 2000001
246 #endif
248 /* Debugging */
250 #ifdef DEBUG
251 #include <assert.h>
252 #define ASSERT assert
253 #define MSG __pthread_message
254 #else
255 #define ASSERT(x)
256 #define MSG(msg,arg...)
257 #endif
259 /* Internal global functions */
261 extern void __pthread_do_exit (void *retval, char *currentframe)
262 __attribute__ ((__noreturn__));
263 extern void __pthread_destroy_specifics (void);
264 extern void __pthread_perform_cleanup (char *currentframe);
265 extern void __pthread_init_max_stacksize (void);
266 extern int __pthread_initialize_manager (void);
267 extern void __pthread_message (char * fmt, ...);
268 extern int __pthread_manager (void *reqfd);
269 extern int __pthread_manager_event (void *reqfd);
270 extern void __pthread_manager_sighandler (int sig);
271 extern void __pthread_reset_main_thread (void);
272 extern void __pthread_once_fork_prepare (void);
273 extern void __pthread_once_fork_parent (void);
274 extern void __pthread_once_fork_child (void);
275 extern void __flockfilelist (void);
276 extern void __funlockfilelist (void);
277 extern void __fresetlockfiles (void);
278 extern void __pthread_manager_adjust_prio (int thread_prio);
279 extern void __pthread_initialize_minimal (void);
281 extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
282 size_t __guardsize);
283 extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
284 size_t *__guardsize);
285 extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
286 void *__stackaddr);
287 extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
288 void **__stackaddr);
289 extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
290 size_t __stacksize);
291 extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
292 size_t *__stacksize);
293 extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
294 size_t __stacksize);
295 extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
296 size_t *__stacksize);
297 extern int __pthread_getconcurrency (void);
298 extern int __pthread_setconcurrency (int __level);
299 extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
300 const struct timespec *__abstime);
301 extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
302 int *__pshared);
303 extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
304 int __pshared);
305 extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
306 int *__kind);
307 extern void __pthread_kill_other_threads_np (void);
309 extern void __pthread_restart_old(pthread_descr th);
310 extern void __pthread_suspend_old(pthread_descr self);
311 extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abs);
313 extern void __pthread_restart_new(pthread_descr th);
314 extern void __pthread_suspend_new(pthread_descr self);
315 extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abs);
317 extern void __pthread_wait_for_restart_signal(pthread_descr self);
319 extern int __pthread_yield (void);
321 extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
322 __const struct timespec *__restrict
323 __abstime);
324 extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
325 __const struct timespec *__restrict
326 __abstime);
327 extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
329 extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
330 __restrict __attr,
331 int *__restrict __pshared);
333 extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
334 extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
335 extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
336 extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
337 extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
339 extern int __pthread_clock_gettime (hp_timing_t freq, struct timespec *tp);
340 extern void __pthread_clock_settime (hp_timing_t offset);
343 /* Global pointers to old or new suspend functions */
345 extern void (*__pthread_restart)(pthread_descr);
346 extern void (*__pthread_suspend)(pthread_descr);
347 extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
349 /* Prototypes for the function without cancelation support when the
350 normal version has it. */
351 extern int __libc_close (int fd);
352 extern int __libc_nanosleep (const struct timespec *requested_time,
353 struct timespec *remaining);
354 /* Prototypes for some of the new semaphore functions. */
355 extern int __new_sem_post (sem_t * sem);
356 extern int __new_sem_init (sem_t *__sem, int __pshared, unsigned int __value);
357 extern int __new_sem_wait (sem_t *__sem);
358 extern int __new_sem_trywait (sem_t *__sem);
359 extern int __new_sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
360 extern int __new_sem_destroy (sem_t *__sem);
362 /* Prototypes for compatibility functions. */
363 extern int __pthread_attr_init_2_1 (pthread_attr_t *__attr);
364 extern int __pthread_attr_init_2_0 (pthread_attr_t *__attr);
365 extern int __pthread_create_2_1 (pthread_t *__restrict __threadp,
366 const pthread_attr_t *__attr,
367 void *(*__start_routine) (void *),
368 void *__restrict __arg);
369 extern int __pthread_create_2_0 (pthread_t *__restrict thread,
370 const pthread_attr_t *__attr,
371 void *(*__start_routine) (void *),
372 void *__restrict arg);
374 /* The functions called the signal events. */
375 extern void __linuxthreads_create_event (void);
376 extern void __linuxthreads_death_event (void);
377 extern void __linuxthreads_reap_event (void);
379 /* This function is called to initialize the pthread library. */
380 extern void __pthread_initialize (void);
383 /* Sighandler wrappers. */
384 extern void __pthread_sighandler(int signo, SIGCONTEXT ctx);
385 extern void __pthread_sighandler_rt(int signo, struct siginfo *si,
386 struct ucontext *uc);
387 extern void __pthread_null_sighandler(int sig);
389 #endif /* internals.h */