descr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #ifndef _DESCR_H
  15. #define _DESCR_H 1
  16. #define __need_res_state
  17. #include <resolv.h>
  18. #include <sched.h>
  19. #include <setjmp.h>
  20. #include <signal.h>
  21. #include <stdint.h>
  22. #include <sys/types.h>
  23. #include <hp-timing.h>
  24. #ifdef USE_TLS
  25. #include <tls.h>
  26. #endif
  27. #include "uClibc-glue.h"
  28. /* Fast thread-specific data internal to libc. */
  29. enum __libc_tsd_key_t { _LIBC_TSD_KEY_MALLOC = 0,
  30. _LIBC_TSD_KEY_DL_ERROR,
  31. _LIBC_TSD_KEY_RPC_VARS,
  32. _LIBC_TSD_KEY_LOCALE,
  33. _LIBC_TSD_KEY_CTYPE_B,
  34. _LIBC_TSD_KEY_CTYPE_TOLOWER,
  35. _LIBC_TSD_KEY_CTYPE_TOUPPER,
  36. _LIBC_TSD_KEY_N };
  37. /* The type of thread descriptors */
  38. typedef struct _pthread_descr_struct *pthread_descr;
  39. /* Some more includes. */
  40. #include <pt-machine.h>
  41. #include <linuxthreads_db/thread_dbP.h>
  42. /* Arguments passed to thread creation routine */
  43. struct pthread_start_args {
  44. void *(*start_routine)(void *); /* function to run */
  45. void *arg; /* its argument */
  46. sigset_t mask; /* initial signal mask for thread */
  47. int schedpolicy; /* initial scheduling policy (if any) */
  48. struct sched_param schedparam; /* initial scheduling parameters (if any) */
  49. };
  50. /* Callback interface for removing the thread from waiting on an
  51. object if it is cancelled while waiting or about to wait.
  52. This hold a pointer to the object, and a pointer to a function
  53. which ``extricates'' the thread from its enqueued state.
  54. The function takes two arguments: pointer to the wait object,
  55. and a pointer to the thread. It returns 1 if an extrication
  56. actually occured, and hence the thread must also be signalled.
  57. It returns 0 if the thread had already been extricated. */
  58. typedef struct _pthread_extricate_struct {
  59. void *pu_object;
  60. int (*pu_extricate_func)(void *, pthread_descr);
  61. } pthread_extricate_if;
  62. /* Atomic counter made possible by compare_and_swap */
  63. struct pthread_atomic {
  64. long p_count;
  65. int p_spinlock;
  66. };
  67. /* Context info for read write locks. The pthread_rwlock_info structure
  68. is information about a lock that has been read-locked by the thread
  69. in whose list this structure appears. The pthread_rwlock_context
  70. is embedded in the thread context and contains a pointer to the
  71. head of the list of lock info structures, as well as a count of
  72. read locks that are untracked, because no info structure could be
  73. allocated for them. */
  74. struct _pthread_rwlock_t;
  75. typedef struct _pthread_rwlock_info {
  76. struct _pthread_rwlock_info *pr_next;
  77. struct _pthread_rwlock_t *pr_lock;
  78. int pr_lock_count;
  79. } pthread_readlock_info;
  80. /* We keep thread specific data in a special data structure, a two-level
  81. array. The top-level array contains pointers to dynamically allocated
  82. arrays of a certain number of data pointers. So we can implement a
  83. sparse array. Each dynamic second-level array has
  84. PTHREAD_KEY_2NDLEVEL_SIZE
  85. entries. This value shouldn't be too large. */
  86. #define PTHREAD_KEY_2NDLEVEL_SIZE 32
  87. /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
  88. keys in each subarray. */
  89. #define PTHREAD_KEY_1STLEVEL_SIZE \
  90. ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
  91. / PTHREAD_KEY_2NDLEVEL_SIZE)
  92. union dtv;
  93. struct _pthread_descr_struct
  94. {
  95. #if !defined USE_TLS || !TLS_DTV_AT_TP || INCLUDE_TLS_PADDING
  96. /* This overlaps tcbhead_t (see tls.h), as used for TLS without threads. */
  97. union
  98. {
  99. struct
  100. {
  101. void *tcb; /* Pointer to the TCB. This is not always
  102. the address of this thread descriptor. */
  103. union dtv *dtvp;
  104. pthread_descr self; /* Pointer to this structure */
  105. int multiple_threads;
  106. uintptr_t sysinfo;
  107. } data;
  108. void *__padding[16];
  109. } p_header;
  110. # define p_multiple_threads p_header.data.multiple_threads
  111. #elif defined TLS_MULTIPLE_THREADS_IN_TCB && TLS_MULTIPLE_THREADS_IN_TCB
  112. int p_multiple_threads;
  113. #endif
  114. pthread_descr p_nextlive, p_prevlive;
  115. /* Double chaining of active threads */
  116. pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
  117. pthread_descr p_nextlock; /* can be on a queue and waiting on a lock */
  118. pthread_t p_tid; /* Thread identifier */
  119. int p_pid; /* PID of Unix process */
  120. int p_priority; /* Thread priority (== 0 if not realtime) */
  121. struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
  122. int p_signal; /* last signal received */
  123. sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
  124. sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
  125. char p_terminated; /* true if terminated e.g. by pthread_exit */
  126. char p_detached; /* true if detached */
  127. char p_exited; /* true if the assoc. process terminated */
  128. void * p_retval; /* placeholder for return value */
  129. int p_retcode; /* placeholder for return code */
  130. pthread_descr p_joining; /* thread joining on that thread or NULL */
  131. struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
  132. char p_cancelstate; /* cancellation state */
  133. char p_canceltype; /* cancellation type (deferred/async) */
  134. char p_canceled; /* cancellation request pending */
  135. char * p_in_sighandler; /* stack address of sighandler, or NULL */
  136. char p_sigwaiting; /* true if a sigwait() is in progress */
  137. struct pthread_start_args p_start_args; /* arguments for thread creation */
  138. void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
  139. #if !(USE_TLS && HAVE___THREAD)
  140. void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
  141. int * p_errnop; /* pointer to used errno variable */
  142. int p_errno; /* error returned by last system call */
  143. int * p_h_errnop; /* pointer to used h_errno variable */
  144. int p_h_errno; /* error returned by last netdb function */
  145. struct __res_state *p_resp; /* Pointer to resolver state */
  146. #endif
  147. struct __res_state p_res; /* per-thread resolver state */
  148. int p_userstack; /* nonzero if the user provided the stack */
  149. void *p_guardaddr; /* address of guard area or NULL */
  150. size_t p_guardsize; /* size of guard area */
  151. int p_nr; /* Index of descriptor in __pthread_handles */
  152. int p_report_events; /* Nonzero if events must be reported. */
  153. td_eventbuf_t p_eventbuf; /* Data for event. */
  154. struct pthread_atomic p_resume_count; /* number of times restart() was
  155. called on thread */
  156. char p_woken_by_cancel; /* cancellation performed wakeup */
  157. char p_condvar_avail; /* flag if conditional variable became avail */
  158. char p_sem_avail; /* flag if semaphore became available */
  159. pthread_extricate_if *p_extricate; /* See above */
  160. pthread_readlock_info *p_readlock_list; /* List of readlock info structs */
  161. pthread_readlock_info *p_readlock_free; /* Free list of structs */
  162. int p_untracked_readlock_count; /* Readlocks not tracked by list */
  163. int p_inheritsched; /* copied from the thread attribute */
  164. #if HP_TIMING_AVAIL
  165. hp_timing_t p_cpuclock_offset; /* Initial CPU clock for thread. */
  166. #endif
  167. #ifdef USE_TLS
  168. char *p_stackaddr; /* Stack address. */
  169. #endif
  170. size_t p_alloca_cutoff; /* Maximum size which should be allocated
  171. using alloca() instead of malloc(). */
  172. /* New elements must be added at the end. */
  173. } __attribute__ ((aligned(32))); /* We need to align the structure so that
  174. doubles are aligned properly. This is 8
  175. bytes on MIPS and 16 bytes on MIPS64.
  176. 32 bytes might give better cache
  177. utilization. */
  178. /* Limit between the stack of the initial thread (above) and the
  179. stacks of other threads (below). Aligned on a STACK_SIZE boundary.
  180. Initially 0, meaning that the current thread is (by definition)
  181. the initial thread. */
  182. extern char *__pthread_initial_thread_bos;
  183. /* Descriptor of the initial thread */
  184. extern struct _pthread_descr_struct __pthread_initial_thread;
  185. /* Limits of the thread manager stack. */
  186. extern char *__pthread_manager_thread_bos;
  187. extern char *__pthread_manager_thread_tos;
  188. /* Descriptor of the manager thread */
  189. extern struct _pthread_descr_struct __pthread_manager_thread;
  190. extern pthread_descr __pthread_manager_threadp attribute_hidden;
  191. /* Indicate whether at least one thread has a user-defined stack (if 1),
  192. or all threads have stacks supplied by LinuxThreads (if 0). */
  193. extern int __pthread_nonstandard_stacks;
  194. /* The max size of the thread stack segments. If the default
  195. THREAD_SELF implementation is used, this must be a power of two and
  196. a multiple of PAGE_SIZE. */
  197. #ifndef STACK_SIZE
  198. #define STACK_SIZE (2 * 1024 * 1024)
  199. #endif
  200. /* Get some notion of the current stack. Need not be exactly the top
  201. of the stack, just something somewhere in the current frame. */
  202. #ifndef CURRENT_STACK_FRAME
  203. #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
  204. #endif
  205. /* Recover thread descriptor for the current thread */
  206. extern pthread_descr __pthread_find_self (void) __attribute__ ((pure));
  207. static __inline__ pthread_descr thread_self (void) __attribute__ ((pure));
  208. static __inline__ pthread_descr thread_self (void)
  209. {
  210. #ifdef THREAD_SELF
  211. return THREAD_SELF;
  212. #else
  213. char *sp = CURRENT_STACK_FRAME;
  214. if (sp >= __pthread_initial_thread_bos)
  215. return &__pthread_initial_thread;
  216. else if (sp >= __pthread_manager_thread_bos
  217. && sp < __pthread_manager_thread_tos)
  218. return &__pthread_manager_thread;
  219. else if (__pthread_nonstandard_stacks)
  220. return __pthread_find_self();
  221. else
  222. #ifdef _STACK_GROWS_DOWN
  223. return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
  224. #else
  225. return (pthread_descr)((unsigned long)sp &~ (STACK_SIZE-1));
  226. #endif
  227. #endif
  228. }
  229. #endif /* descr.h */