OSDN Git Service

ab227d6ccf03f8c351fc6e067f566bf684fc455a
[uclinux-h8/uClibc.git] / libpthread / linuxthreads.old / internals.h
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.                 */
14
15 #ifndef _INTERNALS_H
16 #define _INTERNALS_H   1
17
18 /* Internal data structures */
19
20 /* Includes */
21
22 #include <bits/libc-tsd.h> /* for _LIBC_TSD_KEY_N */
23 #include <limits.h>
24 #include <setjmp.h>
25 #include <signal.h>
26 #include <unistd.h>
27 #include <bits/stackinfo.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include "pt-machine.h"
31 #include "semaphore.h"
32 #include "../linuxthreads.old_db/thread_dbP.h"
33 #ifdef __UCLIBC_HAS_XLOCALE__
34 #include <bits/uClibc_locale.h>
35 #endif /* __UCLIBC_HAS_XLOCALE__ */
36
37 /* Use a funky version in a probably vein attempt at preventing gdb 
38  * from dlopen()'ing glibc's libthread_db library... */
39 #define STRINGIFY(s) STRINGIFY2 (s)
40 #define STRINGIFY2(s) #s
41 #define VERSION STRINGIFY(__UCLIBC_MAJOR__) "." STRINGIFY(__UCLIBC_MINOR__) "." STRINGIFY(__UCLIBC_SUBLEVEL__)
42
43 #ifndef THREAD_GETMEM
44 # define THREAD_GETMEM(descr, member) descr->member
45 #endif
46 #ifndef THREAD_GETMEM_NC
47 # define THREAD_GETMEM_NC(descr, member) descr->member
48 #endif
49 #ifndef THREAD_SETMEM
50 # define THREAD_SETMEM(descr, member, value) descr->member = (value)
51 #endif
52 #ifndef THREAD_SETMEM_NC
53 # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
54 #endif
55
56 /* Arguments passed to thread creation routine */
57
58 struct pthread_start_args {
59   void * (*start_routine)(void *); /* function to run */
60   void * arg;                   /* its argument */
61   sigset_t mask;                /* initial signal mask for thread */
62   int schedpolicy;              /* initial scheduling policy (if any) */
63   struct sched_param schedparam; /* initial scheduling parameters (if any) */
64 };
65
66
67 /* We keep thread specific data in a special data structure, a two-level
68    array.  The top-level array contains pointers to dynamically allocated
69    arrays of a certain number of data pointers.  So we can implement a
70    sparse array.  Each dynamic second-level array has
71         PTHREAD_KEY_2NDLEVEL_SIZE
72    entries.  This value shouldn't be too large.  */
73 #define PTHREAD_KEY_2NDLEVEL_SIZE       32
74
75 /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
76    keys in each subarray.  */
77 #define PTHREAD_KEY_1STLEVEL_SIZE \
78   ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
79    / PTHREAD_KEY_2NDLEVEL_SIZE)
80
81 typedef void (*destr_function)(void *);
82
83 struct pthread_key_struct {
84   int in_use;                   /* already allocated? */
85   destr_function destr;         /* destruction routine */
86 };
87
88
89 #define PTHREAD_START_ARGS_INITIALIZER { NULL, NULL, {{0, }}, 0, { 0 } }
90
91 /* The type of thread descriptors */
92
93 typedef struct _pthread_descr_struct * pthread_descr;
94
95 /* Callback interface for removing the thread from waiting on an
96    object if it is cancelled while waiting or about to wait.
97    This hold a pointer to the object, and a pointer to a function
98    which ``extricates'' the thread from its enqueued state.
99    The function takes two arguments: pointer to the wait object,
100    and a pointer to the thread. It returns 1 if an extrication
101    actually occured, and hence the thread must also be signalled.
102    It returns 0 if the thread had already been extricated. */
103
104 typedef struct _pthread_extricate_struct {
105     void *pu_object;
106     int (*pu_extricate_func)(void *, pthread_descr);
107 } pthread_extricate_if;
108
109 /* Atomic counter made possible by compare_and_swap */
110
111 struct pthread_atomic {
112   long p_count;
113   int p_spinlock;
114 };
115
116 /* Context info for read write locks. The pthread_rwlock_info structure
117    is information about a lock that has been read-locked by the thread
118    in whose list this structure appears. The pthread_rwlock_context
119    is embedded in the thread context and contains a pointer to the
120    head of the list of lock info structures, as well as a count of
121    read locks that are untracked, because no info structure could be
122    allocated for them. */
123
124 struct _pthread_rwlock_t;
125
126 typedef struct _pthread_rwlock_info {
127   struct _pthread_rwlock_info *pr_next;
128   struct _pthread_rwlock_t *pr_lock;
129   int pr_lock_count;
130 } pthread_readlock_info;
131
132 struct _pthread_descr_struct {
133   pthread_descr p_nextlive, p_prevlive;
134                                 /* Double chaining of active threads */
135   pthread_descr p_nextwaiting;  /* Next element in the queue holding the thr */
136   pthread_descr p_nextlock;     /* can be on a queue and waiting on a lock */
137   pthread_t p_tid;              /* Thread identifier */
138   int p_pid;                    /* PID of Unix process */
139   int p_priority;               /* Thread priority (== 0 if not realtime) */
140   struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
141   int p_signal;                 /* last signal received */
142   sigjmp_buf * p_signal_jmp;    /* where to siglongjmp on a signal or NULL */
143   sigjmp_buf * p_cancel_jmp;    /* where to siglongjmp on a cancel or NULL */
144   char p_terminated;            /* true if terminated e.g. by pthread_exit */
145   char p_detached;              /* true if detached */
146   char p_exited;                /* true if the assoc. process terminated */
147   void * p_retval;              /* placeholder for return value */
148   int p_retcode;                /* placeholder for return code */
149   pthread_descr p_joining;      /* thread joining on that thread or NULL */
150   struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
151   char p_cancelstate;           /* cancellation state */
152   char p_canceltype;            /* cancellation type (deferred/async) */
153   char p_canceled;              /* cancellation request pending */
154   int * p_errnop;               /* pointer to used errno variable */
155   int p_errno;                  /* error returned by last system call */
156   int * p_h_errnop;             /* pointer to used h_errno variable */
157   int p_h_errno;                /* error returned by last netdb function */
158   char * p_in_sighandler;       /* stack address of sighandler, or NULL */
159   char p_sigwaiting;            /* true if a sigwait() is in progress */
160   struct pthread_start_args p_start_args; /* arguments for thread creation */
161   void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
162   void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
163   int p_userstack;              /* nonzero if the user provided the stack */
164   void *p_guardaddr;            /* address of guard area or NULL */
165   size_t p_guardsize;           /* size of guard area */
166   pthread_descr p_self;         /* Pointer to this structure */
167   int p_nr;                     /* Index of descriptor in __pthread_handles */
168   int p_report_events;         /* Nonzero if events must be reported.  */
169   td_eventbuf_t p_eventbuf;     /* Data for event.  */
170   struct pthread_atomic p_resume_count; /* number of times restart() was
171                                            called on thread */
172   char p_woken_by_cancel;       /* cancellation performed wakeup */
173   char p_condvar_avail;         /* flag if conditional variable became avail */
174   char p_sem_avail;             /* flag if semaphore became available */
175   pthread_extricate_if *p_extricate; /* See above */
176   pthread_readlock_info *p_readlock_list;  /* List of readlock info structs */
177   pthread_readlock_info *p_readlock_free;  /* Free list of structs */
178   int p_untracked_readlock_count;       /* Readlocks not tracked by list */
179   /* New elements must be added at the end.  */
180 #ifdef __UCLIBC_HAS_XLOCALE__
181   __locale_t locale; /* thread-specific locale from uselocale() only! */
182 #endif /* __UCLIBC_HAS_XLOCALE__ */
183 } __attribute__ ((aligned(32))); /* We need to align the structure so that
184                                     doubles are aligned properly.  This is 8
185                                     bytes on MIPS and 16 bytes on MIPS64.
186                                     32 bytes might give better cache
187                                     utilization.  */
188
189 /* The type of thread handles. */
190
191 typedef struct pthread_handle_struct * pthread_handle;
192
193 struct pthread_handle_struct {
194   struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
195   pthread_descr h_descr;        /* Thread descriptor or NULL if invalid */
196   char * h_bottom;              /* Lowest address in the stack thread */
197 };
198
199 /* The type of messages sent to the thread manager thread */
200
201 struct pthread_request {
202   pthread_descr req_thread;     /* Thread doing the request */
203   enum {                        /* Request kind */
204     REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
205     REQ_POST, REQ_DEBUG, REQ_KICK
206   } req_kind;
207   union {                       /* Arguments for request */
208     struct {                    /* For REQ_CREATE: */
209       const pthread_attr_t * attr; /* thread attributes */
210       void * (*fn)(void *);     /*   start function */
211       void * arg;               /*   argument to start function */
212       sigset_t mask;            /*   signal mask */
213     } create;
214     struct {                    /* For REQ_FREE: */
215       pthread_t thread_id;      /*   identifier of thread to free */
216     } free;
217     struct {                    /* For REQ_PROCESS_EXIT: */
218       int code;                 /*   exit status */
219     } exit;
220     void * post;                /* For REQ_POST: the semaphore */
221   } req_args;
222 };
223
224
225 /* Signals used for suspend/restart and for cancellation notification.  */
226
227 extern int __pthread_sig_restart;
228 extern int __pthread_sig_cancel;
229
230 /* Signal used for interfacing with gdb */
231
232 extern int __pthread_sig_debug;
233
234 /* Global array of thread handles, used for validating a thread id
235    and retrieving the corresponding thread descriptor. Also used for
236    mapping the available stack segments. */
237
238 extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
239
240 /* Descriptor of the initial thread */
241
242 extern struct _pthread_descr_struct __pthread_initial_thread;
243
244 /* Descriptor of the manager thread */
245
246 extern struct _pthread_descr_struct __pthread_manager_thread;
247
248 /* Descriptor of the main thread */
249
250 extern pthread_descr __pthread_main_thread;
251
252 /* Limit between the stack of the initial thread (above) and the
253    stacks of other threads (below). Aligned on a STACK_SIZE boundary.
254    Initially 0, meaning that the current thread is (by definition)
255    the initial thread. */
256
257 /* For non-MMU systems also remember to stack top of the initial thread.
258  * This is adapted when other stacks are malloc'ed since we don't know
259  * the bounds a-priori. -StS */
260
261 extern char *__pthread_initial_thread_bos;
262 #ifndef __ARCH_USE_MMU__
263 extern char *__pthread_initial_thread_tos;
264 #define NOMMU_INITIAL_THREAD_BOUNDS(tos,bos) \
265         if ((tos)>=__pthread_initial_thread_bos \
266             && (bos)<__pthread_initial_thread_tos) \
267                 __pthread_initial_thread_bos = (tos)+1
268 #else
269 #define NOMMU_INITIAL_THREAD_BOUNDS(tos,bos) /* empty */
270 #endif /* __ARCH_USE_MMU__ */
271
272
273 /* Indicate whether at least one thread has a user-defined stack (if 1),
274    or all threads have stacks supplied by LinuxThreads (if 0). */
275
276 extern int __pthread_nonstandard_stacks;
277
278 /* File descriptor for sending requests to the thread manager.
279    Initially -1, meaning that __pthread_initialize_manager must be called. */
280
281 extern int __pthread_manager_request;
282
283 /* Other end of the pipe for sending requests to the thread manager. */
284
285 extern int __pthread_manager_reader;
286
287 /* Limits of the thread manager stack. */
288
289 extern char *__pthread_manager_thread_bos;
290 extern char *__pthread_manager_thread_tos;
291
292 /* Pending request for a process-wide exit */
293
294 extern int __pthread_exit_requested, __pthread_exit_code;
295
296 /* Set to 1 by gdb if we're debugging */
297
298 extern volatile int __pthread_threads_debug;
299
300 /* Globally enabled events.  */
301 extern volatile td_thr_events_t __pthread_threads_events;
302
303 /* Pointer to descriptor of thread with last event.  */
304 extern volatile pthread_descr __pthread_last_event;
305
306 /* Return the handle corresponding to a thread id */
307
308 static inline pthread_handle thread_handle(pthread_t id)
309 {
310   return &__pthread_handles[id % PTHREAD_THREADS_MAX];
311 }
312
313 /* Validate a thread handle. Must have acquired h->h_spinlock before. */
314
315 static inline int invalid_handle(pthread_handle h, pthread_t id)
316 {
317   return h->h_descr == NULL || h->h_descr->p_tid != id;
318 }
319
320 /* Fill in defaults left unspecified by pt-machine.h.  */
321
322 /* The page size we can get from the system.  This should likely not be
323    changed by the machine file but, you never know.  */
324 extern size_t __pagesize;
325 #include <bits/uClibc_page.h>
326 #ifndef PAGE_SIZE
327 #define PAGE_SIZE  (sysconf (_SC_PAGESIZE))
328 #endif
329
330 /* The max size of the thread stack segments.  If the default
331    THREAD_SELF implementation is used, this must be a power of two and
332    a multiple of PAGE_SIZE.  */
333 #ifndef STACK_SIZE
334 #ifdef __ARCH_USE_MMU__
335 #define STACK_SIZE  (2 * 1024 * 1024)
336 #else
337 #define STACK_SIZE  (4 * __pagesize)
338 #endif
339 #endif
340
341 /* The initial size of the thread stack.  Must be a multiple of PAGE_SIZE.  */
342 #ifndef INITIAL_STACK_SIZE
343 #define INITIAL_STACK_SIZE  (4 * __pagesize)
344 #endif
345
346 /* Size of the thread manager stack. The "- 32" avoids wasting space
347    with some malloc() implementations. */
348 #ifndef THREAD_MANAGER_STACK_SIZE
349 #define THREAD_MANAGER_STACK_SIZE  (2 * __pagesize - 32)
350 #endif
351
352 /* The base of the "array" of thread stacks.  The array will grow down from
353    here.  Defaults to the calculated bottom of the initial application
354    stack.  */
355 #ifndef THREAD_STACK_START_ADDRESS
356 #define THREAD_STACK_START_ADDRESS  __pthread_initial_thread_bos
357 #endif
358
359 /* Get some notion of the current stack.  Need not be exactly the top
360    of the stack, just something somewhere in the current frame.  */
361 #ifndef CURRENT_STACK_FRAME
362 #define CURRENT_STACK_FRAME  ({ char __csf; &__csf; })
363 #endif
364
365 /* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the
366    architecture doesn't need a memory barrier instruction (e.g. Intel
367    x86).  Still we need the compiler to respect the barrier and emit
368    all outstanding operations which modify memory.  Some architectures
369    distinguish between full, read and write barriers.  */
370 #ifndef MEMORY_BARRIER
371 #define MEMORY_BARRIER() __asm__ ("" : : : "memory")
372 #endif
373 #ifndef READ_MEMORY_BARRIER
374 #define READ_MEMORY_BARRIER() MEMORY_BARRIER()
375 #endif
376 #ifndef WRITE_MEMORY_BARRIER
377 #define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
378 #endif
379
380 /* Recover thread descriptor for the current thread */
381
382 extern pthread_descr __pthread_find_self (void) __attribute__ ((const));
383
384 static inline pthread_descr thread_self (void) __attribute__ ((const));
385 static inline pthread_descr thread_self (void)
386 {
387 #ifdef THREAD_SELF
388   return THREAD_SELF;
389 #else
390   char *sp = CURRENT_STACK_FRAME;
391 #ifdef __ARCH_USE_MMU__
392   if (sp >= __pthread_initial_thread_bos)
393     return &__pthread_initial_thread;
394   else if (sp >= __pthread_manager_thread_bos
395            && sp < __pthread_manager_thread_tos)
396     return &__pthread_manager_thread;
397   else if (__pthread_nonstandard_stacks)
398     return __pthread_find_self();
399   else
400     return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
401 #else
402   /* For non-MMU we need to be more careful about the initial thread stack.
403    * We refine the initial thread stack bounds dynamically as we allocate
404    * the other stack frame such that it doesn't overlap with them. Then
405    * we can be sure to pick the right thread according to the current SP */
406
407   /* Since we allow other stack frames to be above or below, we need to
408    * treat this case special. When pthread_initialize() wasn't called yet,
409    * only the initial thread is there. */
410   if (__pthread_initial_thread_bos == NULL) {
411       return &__pthread_initial_thread;
412   }
413   else if (sp >= __pthread_initial_thread_bos
414            && sp < __pthread_initial_thread_tos) {
415       return &__pthread_initial_thread;
416   }
417   else if (sp >= __pthread_manager_thread_bos
418            && sp < __pthread_manager_thread_tos) {
419       return &__pthread_manager_thread;
420   }
421   else {
422       return __pthread_find_self();
423   }
424 #endif /* __ARCH_USE_MMU__ */
425 #endif
426 }
427
428 /* Max number of times we must spin on a spinlock calling sched_yield().
429    After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
430
431 #ifndef MAX_SPIN_COUNT
432 #define MAX_SPIN_COUNT 50
433 #endif
434
435 /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
436    after MAX_SPIN_COUNT iterations of sched_yield().
437    With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
438    (Otherwise the kernel does busy-waiting for realtime threads,
439     giving other threads no chance to run.) */
440
441 #ifndef SPIN_SLEEP_DURATION
442 #define SPIN_SLEEP_DURATION 2000001
443 #endif
444
445 /* Defined and used in libc.so.  */
446 extern int __libc_multiple_threads attribute_hidden;
447 extern int __librt_multiple_threads;
448
449 /* Internal global functions */
450
451 void __pthread_do_exit (void *retval, char *currentframe)
452      __attribute__ ((__noreturn__));
453 void __pthread_destroy_specifics(void);
454 void __pthread_perform_cleanup(char *currentframe);
455 int __pthread_initialize_manager(void);
456 void __pthread_message(char * fmt, ...);
457 int __pthread_manager(void *reqfd);
458 int __pthread_manager_event(void *reqfd);
459 void __pthread_manager_sighandler(int sig);
460 void __pthread_reset_main_thread(void);
461 void __fresetlockfiles(void);
462 void __pthread_manager_adjust_prio(int thread_prio);
463 void __pthread_initialize_minimal (void);
464
465 extern void __pthread_exit (void *retval)
466 #if defined NOT_IN_libc && defined IS_IN_libpthread
467         attribute_noreturn
468 #endif
469         ;
470
471 extern int __pthread_attr_setguardsize __P ((pthread_attr_t *__attr,
472                                              size_t __guardsize));
473 extern int __pthread_attr_getguardsize __P ((__const pthread_attr_t *__attr,
474                                              size_t *__guardsize));
475 extern int __pthread_attr_setstackaddr __P ((pthread_attr_t *__attr,
476                                              void *__stackaddr));
477 extern int __pthread_attr_getstackaddr __P ((__const pthread_attr_t *__attr,
478                                              void **__stackaddr));
479 extern int __pthread_attr_setstacksize __P ((pthread_attr_t *__attr,
480                                              size_t __stacksize));
481 extern int __pthread_attr_getstacksize __P ((__const pthread_attr_t *__attr,
482                                              size_t *__stacksize));
483 extern int __pthread_getconcurrency __P ((void));
484 extern int __pthread_setconcurrency __P ((int __level));
485 extern void __pthread_kill_other_threads_np __P ((void));
486
487 extern void __pthread_restart_old(pthread_descr th);
488 extern void __pthread_suspend_old(pthread_descr self);
489 extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abstime);
490
491 extern void __pthread_restart_new(pthread_descr th);
492 extern void __pthread_suspend_new(pthread_descr self);
493 extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime);
494
495 extern void __pthread_wait_for_restart_signal(pthread_descr self);
496
497 /* Global pointers to old or new suspend functions */
498
499 extern void (*__pthread_restart)(pthread_descr);
500 extern void (*__pthread_suspend)(pthread_descr);
501
502 /* Prototypes for the function without cancelation support when the
503    normal version has it.  */
504 extern __typeof(close) __libc_close;
505 extern __typeof(nanosleep) __libc_nanosleep;
506 extern __typeof(read) __libc_read;
507 extern __typeof(waitpid) __libc_waitpid;
508 extern __typeof(write) __libc_write;
509
510 extern __typeof(pthread_mutex_init) __pthread_mutex_init attribute_hidden;
511 extern __typeof(pthread_mutex_destroy) __pthread_mutex_destroy attribute_hidden;
512 extern __typeof(pthread_mutex_lock) __pthread_mutex_lock attribute_hidden;
513 extern __typeof(pthread_mutex_trylock) __pthread_mutex_trylock attribute_hidden;
514 extern __typeof(pthread_mutex_unlock) __pthread_mutex_attribute_hidden;
515
516 /* Prototypes for some of the new semaphore functions.  */
517 extern int __new_sem_post (sem_t * sem);
518
519 /* TSD.  */
520 extern int __pthread_internal_tsd_set (int key, const void * pointer);
521 extern void * __pthread_internal_tsd_get (int key);
522 extern void ** __attribute__ ((__const__))
523   __pthread_internal_tsd_address (int key);
524
525 /* The functions called the signal events.  */
526 extern void __linuxthreads_create_event (void);
527 extern void __linuxthreads_death_event (void);
528 extern void __linuxthreads_reap_event (void);
529
530 #include <pthread-functions.h>
531
532 extern int * __libc_pthread_init (const struct pthread_functions *functions);
533
534 #endif /* internals.h */