descr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #include <features.h>
  17. #include <sched.h>
  18. #include <setjmp.h>
  19. #include <signal.h>
  20. #include <stdint.h>
  21. #include <sys/types.h>
  22. #include <tls.h>
  23. #if 1
  24. # include <bits/libc-tsd.h>
  25. #else
  26. /* Fast thread-specific data internal to libc. */
  27. enum __libc_tsd_key_t { _LIBC_TSD_KEY_MALLOC = 0,
  28. _LIBC_TSD_KEY_DL_ERROR,
  29. _LIBC_TSD_KEY_RPC_VARS,
  30. _LIBC_TSD_KEY_LOCALE,
  31. _LIBC_TSD_KEY_CTYPE_B,
  32. _LIBC_TSD_KEY_CTYPE_TOLOWER,
  33. _LIBC_TSD_KEY_CTYPE_TOUPPER,
  34. _LIBC_TSD_KEY_N };
  35. #endif
  36. /* The type of thread descriptors */
  37. typedef struct _pthread_descr_struct *pthread_descr;
  38. /* Some more includes. */
  39. #include <pt-machine.h>
  40. #include "../linuxthreads_db/thread_dbP.h"
  41. /* Arguments passed to thread creation routine */
  42. struct pthread_start_args {
  43. void *(*start_routine)(void *); /* function to run */
  44. void *arg; /* its argument */
  45. sigset_t mask; /* initial signal mask for thread */
  46. int schedpolicy; /* initial scheduling policy (if any) */
  47. struct sched_param schedparam; /* initial scheduling parameters (if any) */
  48. };
  49. /* Callback interface for removing the thread from waiting on an
  50. object if it is cancelled while waiting or about to wait.
  51. This hold a pointer to the object, and a pointer to a function
  52. which ``extricates'' the thread from its enqueued state.
  53. The function takes two arguments: pointer to the wait object,
  54. and a pointer to the thread. It returns 1 if an extrication
  55. actually occured, and hence the thread must also be signalled.
  56. It returns 0 if the thread had already been extricated. */
  57. typedef struct _pthread_extricate_struct {
  58. void *pu_object;
  59. int (*pu_extricate_func)(void *, pthread_descr);
  60. } pthread_extricate_if;
  61. /* Atomic counter made possible by compare_and_swap */
  62. struct pthread_atomic {
  63. long p_count;
  64. int p_spinlock;
  65. };
  66. /* Context info for read write locks. The pthread_rwlock_info structure
  67. is information about a lock that has been read-locked by the thread
  68. in whose list this structure appears. The pthread_rwlock_context
  69. is embedded in the thread context and contains a pointer to the
  70. head of the list of lock info structures, as well as a count of
  71. read locks that are untracked, because no info structure could be
  72. allocated for them. */
  73. struct _pthread_rwlock_t;
  74. typedef struct _pthread_rwlock_info {
  75. struct _pthread_rwlock_info *pr_next;
  76. struct _pthread_rwlock_t *pr_lock;
  77. int pr_lock_count;
  78. } pthread_readlock_info;
  79. /* We keep thread specific data in a special data structure, a two-level
  80. array. The top-level array contains pointers to dynamically allocated
  81. arrays of a certain number of data pointers. So we can implement a
  82. sparse array. Each dynamic second-level array has
  83. PTHREAD_KEY_2NDLEVEL_SIZE
  84. entries. This value shouldn't be too large. */
  85. #define PTHREAD_KEY_2NDLEVEL_SIZE 32
  86. /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
  87. keys in each subarray. */
  88. #define PTHREAD_KEY_1STLEVEL_SIZE \
  89. ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
  90. / PTHREAD_KEY_2NDLEVEL_SIZE)
  91. union dtv;
  92. struct _pthread_descr_struct
  93. {
  94. #if !defined USE_TLS || !TLS_DTV_AT_TP
  95. /* This overlaps tcbhead_t (see tls.h), as used for TLS without threads. */
  96. union
  97. {
  98. struct
  99. {
  100. void *tcb; /* Pointer to the TCB. This is not always
  101. the address of this thread descriptor. */
  102. union dtv *dtvp;
  103. pthread_descr self; /* Pointer to this structure */
  104. int multiple_threads;
  105. # ifdef NEED_DL_SYSINFO
  106. uintptr_t sysinfo;
  107. # endif
  108. } data;
  109. void *__padding[16];
  110. } p_header;
  111. # define p_multiple_threads p_header.data.multiple_threads
  112. #elif TLS_MULTIPLE_THREADS_IN_TCB
  113. int p_multiple_threads;
  114. #endif
  115. pthread_descr p_nextlive, p_prevlive;
  116. /* Double chaining of active threads */
  117. pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
  118. pthread_descr p_nextlock; /* can be on a queue and waiting on a lock */
  119. pthread_t p_tid; /* Thread identifier */
  120. int p_pid; /* PID of Unix process */
  121. int p_priority; /* Thread priority (== 0 if not realtime) */
  122. struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
  123. int p_signal; /* last signal received */
  124. sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
  125. sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
  126. char p_terminated; /* true if terminated e.g. by pthread_exit */
  127. char p_detached; /* true if detached */
  128. char p_exited; /* true if the assoc. process terminated */
  129. void * p_retval; /* placeholder for return value */
  130. int p_retcode; /* placeholder for return code */
  131. pthread_descr p_joining; /* thread joining on that thread or NULL */
  132. struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
  133. char p_cancelstate; /* cancellation state */
  134. char p_canceltype; /* cancellation type (deferred/async) */
  135. char p_canceled; /* cancellation request pending */
  136. char * p_in_sighandler; /* stack address of sighandler, or NULL */
  137. char p_sigwaiting; /* true if a sigwait() is in progress */
  138. struct pthread_start_args p_start_args; /* arguments for thread creation */
  139. void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
  140. #if !(USE_TLS && HAVE___THREAD)
  141. void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
  142. int * p_errnop; /* pointer to used errno variable */
  143. int p_errno; /* error returned by last system call */
  144. int * p_h_errnop; /* pointer to used h_errno variable */
  145. int p_h_errno; /* error returned by last netdb function */
  146. #endif
  147. int p_userstack; /* nonzero if the user provided the stack */
  148. void *p_guardaddr; /* address of guard area or NULL */
  149. size_t p_guardsize; /* size of guard area */
  150. pthread_descr p_self; /* Pointer to this structure */
  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. #ifdef __UCLIBC_HAS_XLOCALE__
  174. __locale_t locale; /* thread-specific locale from uselocale() only! */
  175. #endif /* __UCLIBC_HAS_XLOCALE__ */
  176. } __attribute__ ((aligned(32))); /* We need to align the structure so that
  177. doubles are aligned properly. This is 8
  178. bytes on MIPS and 16 bytes on MIPS64.
  179. 32 bytes might give better cache
  180. utilization. */
  181. /* Limit between the stack of the initial thread (above) and the
  182. stacks of other threads (below). Aligned on a STACK_SIZE boundary.
  183. Initially 0, meaning that the current thread is (by definition)
  184. the initial thread. */
  185. /* For non-MMU systems also remember to stack top of the initial thread.
  186. * This is adapted when other stacks are malloc'ed since we don't know
  187. * the bounds a-priori. -StS */
  188. extern char *__pthread_initial_thread_bos;
  189. #ifndef __ARCH_HAS_MMU__
  190. extern char *__pthread_initial_thread_tos;
  191. #define NOMMU_INITIAL_THREAD_BOUNDS(tos,bos) \
  192. if ((tos)>=__pthread_initial_thread_bos \
  193. && (bos)<__pthread_initial_thread_tos) \
  194. __pthread_initial_thread_bos = (tos)+1
  195. #else
  196. #define NOMMU_INITIAL_THREAD_BOUNDS(tos,bos) /* empty */
  197. #endif /* __ARCH_HAS_MMU__ */
  198. /* Descriptor of the initial thread */
  199. extern struct _pthread_descr_struct __pthread_initial_thread;
  200. /* Limits of the thread manager stack. */
  201. extern char *__pthread_manager_thread_bos;
  202. extern char *__pthread_manager_thread_tos;
  203. /* Descriptor of the manager thread */
  204. extern struct _pthread_descr_struct __pthread_manager_thread;
  205. extern pthread_descr __pthread_manager_threadp attribute_hidden;
  206. /* Indicate whether at least one thread has a user-defined stack (if 1),
  207. or all threads have stacks supplied by LinuxThreads (if 0). */
  208. extern int __pthread_nonstandard_stacks;
  209. /* The max size of the thread stack segments. If the default
  210. THREAD_SELF implementation is used, this must be a power of two and
  211. a multiple of PAGE_SIZE. */
  212. #ifndef STACK_SIZE
  213. #ifdef __ARCH_HAS_MMU__
  214. #define STACK_SIZE (2 * 1024 * 1024)
  215. #else
  216. #define STACK_SIZE (4 * __pagesize)
  217. #endif
  218. #endif
  219. /* Get some notion of the current stack. Need not be exactly the top
  220. of the stack, just something somewhere in the current frame. */
  221. #ifndef CURRENT_STACK_FRAME
  222. #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
  223. #endif
  224. /* Recover thread descriptor for the current thread */
  225. extern pthread_descr __pthread_find_self (void) __attribute__ ((pure));
  226. static inline pthread_descr thread_self (void) __attribute__ ((pure));
  227. static inline pthread_descr thread_self (void)
  228. {
  229. #ifdef THREAD_SELF
  230. return THREAD_SELF;
  231. #else
  232. char *sp = CURRENT_STACK_FRAME;
  233. #ifdef __ARCH_HAS_MMU__
  234. if (sp >= __pthread_initial_thread_bos)
  235. return &__pthread_initial_thread;
  236. else if (sp >= __pthread_manager_thread_bos
  237. && sp < __pthread_manager_thread_tos)
  238. return &__pthread_manager_thread;
  239. else if (__pthread_nonstandard_stacks)
  240. return __pthread_find_self();
  241. else
  242. #ifdef _STACK_GROWS_DOWN
  243. return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
  244. #else
  245. return (pthread_descr)((unsigned long)sp &~ (STACK_SIZE-1));
  246. #endif
  247. #else /* !__ARCH_HAS_MMU__ */
  248. /* For non-MMU we need to be more careful about the initial thread stack.
  249. * We refine the initial thread stack bounds dynamically as we allocate
  250. * the other stack frame such that it doesn't overlap with them. Then
  251. * we can be sure to pick the right thread according to the current SP */
  252. /* Since we allow other stack frames to be above or below, we need to
  253. * treat this case special. When pthread_initialize() wasn't called yet,
  254. * only the initial thread is there. */
  255. if (__pthread_initial_thread_bos == NULL) {
  256. return &__pthread_initial_thread;
  257. }
  258. else if (sp >= __pthread_initial_thread_bos
  259. && sp < __pthread_initial_thread_tos) {
  260. return &__pthread_initial_thread;
  261. }
  262. else if (sp >= __pthread_manager_thread_bos
  263. && sp < __pthread_manager_thread_tos) {
  264. return &__pthread_manager_thread;
  265. }
  266. else {
  267. return __pthread_find_self();
  268. }
  269. #endif /* __ARCH_HAS_MMU__ */
  270. #endif
  271. }
  272. #endif /* descr.h */