descr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* Copyright (C) 2002-2006, 2007, 2008, 2009 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _DESCR_H
  16. #define _DESCR_H 1
  17. #include <limits.h>
  18. #include <sched.h>
  19. #include <setjmp.h>
  20. #include <stdbool.h>
  21. #include <sys/types.h>
  22. #include <hp-timing.h>
  23. #include <list.h>
  24. #include <lowlevellock.h>
  25. #include <pthreaddef.h>
  26. #include "../nptl_db/thread_db.h"
  27. #include <tls.h>
  28. #ifdef HAVE_FORCED_UNWIND
  29. # include <unwind.h>
  30. #endif
  31. #define __need_res_state
  32. #include <resolv.h>
  33. #include <bits/kernel-features.h>
  34. #include "uClibc-glue.h"
  35. #ifndef TCB_ALIGNMENT
  36. # define TCB_ALIGNMENT sizeof (double)
  37. #endif
  38. /* We keep thread specific data in a special data structure, a two-level
  39. array. The top-level array contains pointers to dynamically allocated
  40. arrays of a certain number of data pointers. So we can implement a
  41. sparse array. Each dynamic second-level array has
  42. PTHREAD_KEY_2NDLEVEL_SIZE
  43. entries. This value shouldn't be too large. */
  44. #define PTHREAD_KEY_2NDLEVEL_SIZE 32
  45. /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
  46. keys in each subarray. */
  47. #define PTHREAD_KEY_1STLEVEL_SIZE \
  48. ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
  49. / PTHREAD_KEY_2NDLEVEL_SIZE)
  50. /* Internal version of the buffer to store cancellation handler
  51. information. */
  52. struct pthread_unwind_buf
  53. {
  54. struct
  55. {
  56. __jmp_buf jmp_buf;
  57. int mask_was_saved;
  58. } cancel_jmp_buf[1];
  59. union
  60. {
  61. /* This is the placeholder of the public version. */
  62. void *pad[4];
  63. struct
  64. {
  65. /* Pointer to the previous cleanup buffer. */
  66. struct pthread_unwind_buf *prev;
  67. /* Backward compatibility: state of the old-style cleanup
  68. handler at the time of the previous new-style cleanup handler
  69. installment. */
  70. struct _pthread_cleanup_buffer *cleanup;
  71. /* Cancellation type before the push call. */
  72. int canceltype;
  73. } data;
  74. } priv;
  75. };
  76. /* Opcodes and data types for communication with the signal handler to
  77. change user/group IDs. */
  78. struct xid_command
  79. {
  80. int syscall_no;
  81. long int id[3];
  82. volatile int cntr;
  83. };
  84. /* Data structure used by the kernel to find robust futexes. */
  85. struct robust_list_head
  86. {
  87. void *list;
  88. long int futex_offset;
  89. void *list_op_pending;
  90. };
  91. /* Data strcture used to handle thread priority protection. */
  92. struct priority_protection_data
  93. {
  94. int priomax;
  95. unsigned int priomap[];
  96. };
  97. /* Thread descriptor data structure. */
  98. struct pthread
  99. {
  100. union
  101. {
  102. #if !defined(TLS_DTV_AT_TP)
  103. /* This overlaps the TCB as used for TLS without threads (see tls.h). */
  104. tcbhead_t header;
  105. #else
  106. struct
  107. {
  108. int multiple_threads;
  109. int gscope_flag;
  110. # ifndef __ASSUME_PRIVATE_FUTEX
  111. int private_futex;
  112. # endif
  113. } header;
  114. #endif
  115. /* This extra padding has no special purpose, and this structure layout
  116. is private and subject to change without affecting the official ABI.
  117. We just have it here in case it might be convenient for some
  118. implementation-specific instrumentation hack or suchlike. */
  119. void *__padding[24];
  120. };
  121. /* This descriptor's link on the `stack_used' or `__stack_user' list. */
  122. list_t list;
  123. /* Thread ID - which is also a 'is this thread descriptor (and
  124. therefore stack) used' flag. */
  125. pid_t tid;
  126. /* List of robust mutexes the thread is holding. */
  127. #ifdef __PTHREAD_MUTEX_HAVE_PREV
  128. void *robust_prev;
  129. struct robust_list_head robust_head;
  130. /* The list above is strange. It is basically a double linked list
  131. but the pointer to the next/previous element of the list points
  132. in the middle of the object, the __next element. Whenever
  133. casting to __pthread_list_t we need to adjust the pointer
  134. first. */
  135. # define QUEUE_PTR_ADJUST (offsetof (__pthread_list_t, __next))
  136. # define ENQUEUE_MUTEX_BOTH(mutex, val) \
  137. do { \
  138. __pthread_list_t *next = (__pthread_list_t *) \
  139. ((((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_head.list)) & ~1ul) \
  140. - QUEUE_PTR_ADJUST); \
  141. next->__prev = (void *) &mutex->__data.__list.__next; \
  142. mutex->__data.__list.__next = THREAD_GETMEM (THREAD_SELF, \
  143. robust_head.list); \
  144. mutex->__data.__list.__prev = (void *) &THREAD_SELF->robust_head; \
  145. THREAD_SETMEM (THREAD_SELF, robust_head.list, \
  146. (void *) (((uintptr_t) &mutex->__data.__list.__next) \
  147. | val)); \
  148. } while (0)
  149. # define DEQUEUE_MUTEX(mutex) \
  150. do { \
  151. __pthread_list_t *next = (__pthread_list_t *) \
  152. ((char *) (((uintptr_t) mutex->__data.__list.__next) & ~1ul) \
  153. - QUEUE_PTR_ADJUST); \
  154. next->__prev = mutex->__data.__list.__prev; \
  155. __pthread_list_t *prev = (__pthread_list_t *) \
  156. ((char *) (((uintptr_t) mutex->__data.__list.__prev) & ~1ul) \
  157. - QUEUE_PTR_ADJUST); \
  158. prev->__next = mutex->__data.__list.__next; \
  159. mutex->__data.__list.__prev = NULL; \
  160. mutex->__data.__list.__next = NULL; \
  161. } while (0)
  162. #else
  163. union
  164. {
  165. __pthread_slist_t robust_list;
  166. struct robust_list_head robust_head;
  167. };
  168. # define ENQUEUE_MUTEX_BOTH(mutex, val) \
  169. do { \
  170. mutex->__data.__list.__next \
  171. = THREAD_GETMEM (THREAD_SELF, robust_list.__next); \
  172. THREAD_SETMEM (THREAD_SELF, robust_list.__next, \
  173. (void *) (((uintptr_t) &mutex->__data.__list) | val)); \
  174. } while (0)
  175. # define DEQUEUE_MUTEX(mutex) \
  176. do { \
  177. __pthread_slist_t *runp = (__pthread_slist_t *) \
  178. (((uintptr_t) THREAD_GETMEM (THREAD_SELF, robust_list.__next)) & ~1ul); \
  179. if (runp == &mutex->__data.__list) \
  180. THREAD_SETMEM (THREAD_SELF, robust_list.__next, runp->__next); \
  181. else \
  182. { \
  183. __pthread_slist_t *next = (__pthread_slist_t *) \
  184. (((uintptr_t) runp->__next) & ~1ul); \
  185. while (next != &mutex->__data.__list) \
  186. { \
  187. runp = next; \
  188. next = (__pthread_slist_t *) (((uintptr_t) runp->__next) & ~1ul); \
  189. } \
  190. \
  191. runp->__next = next->__next; \
  192. mutex->__data.__list.__next = NULL; \
  193. } \
  194. } while (0)
  195. #endif
  196. #define ENQUEUE_MUTEX(mutex) ENQUEUE_MUTEX_BOTH (mutex, 0)
  197. #define ENQUEUE_MUTEX_PI(mutex) ENQUEUE_MUTEX_BOTH (mutex, 1)
  198. /* List of cleanup buffers. */
  199. struct _pthread_cleanup_buffer *cleanup;
  200. /* Unwind information. */
  201. struct pthread_unwind_buf *cleanup_jmp_buf;
  202. #define HAVE_CLEANUP_JMP_BUF
  203. /* Flags determining processing of cancellation. */
  204. int cancelhandling;
  205. /* Bit set if cancellation is disabled. */
  206. #define CANCELSTATE_BIT 0
  207. #define CANCELSTATE_BITMASK (0x01 << CANCELSTATE_BIT)
  208. /* Bit set if asynchronous cancellation mode is selected. */
  209. #define CANCELTYPE_BIT 1
  210. #define CANCELTYPE_BITMASK (0x01 << CANCELTYPE_BIT)
  211. /* Bit set if canceling has been initiated. */
  212. #define CANCELING_BIT 2
  213. #define CANCELING_BITMASK (0x01 << CANCELING_BIT)
  214. /* Bit set if canceled. */
  215. #define CANCELED_BIT 3
  216. #define CANCELED_BITMASK (0x01 << CANCELED_BIT)
  217. /* Bit set if thread is exiting. */
  218. #define EXITING_BIT 4
  219. #define EXITING_BITMASK (0x01 << EXITING_BIT)
  220. /* Bit set if thread terminated and TCB is freed. */
  221. #define TERMINATED_BIT 5
  222. #define TERMINATED_BITMASK (0x01 << TERMINATED_BIT)
  223. /* Bit set if thread is supposed to change XID. */
  224. #define SETXID_BIT 6
  225. #define SETXID_BITMASK (0x01 << SETXID_BIT)
  226. /* Mask for the rest. Helps the compiler to optimize. */
  227. #define CANCEL_RESTMASK 0xffffff80
  228. #define CANCEL_ENABLED_AND_CANCELED(value) \
  229. (((value) & (CANCELSTATE_BITMASK | CANCELED_BITMASK | EXITING_BITMASK \
  230. | CANCEL_RESTMASK | TERMINATED_BITMASK)) == CANCELED_BITMASK)
  231. #define CANCEL_ENABLED_AND_CANCELED_AND_ASYNCHRONOUS(value) \
  232. (((value) & (CANCELSTATE_BITMASK | CANCELTYPE_BITMASK | CANCELED_BITMASK \
  233. | EXITING_BITMASK | CANCEL_RESTMASK | TERMINATED_BITMASK)) \
  234. == (CANCELTYPE_BITMASK | CANCELED_BITMASK))
  235. /* Flags. Including those copied from the thread attribute. */
  236. int flags;
  237. /* We allocate one block of references here. This should be enough
  238. to avoid allocating any memory dynamically for most applications. */
  239. struct pthread_key_data
  240. {
  241. /* Sequence number. We use uintptr_t to not require padding on
  242. 32- and 64-bit machines. On 64-bit machines it helps to avoid
  243. wrapping, too. */
  244. uintptr_t seq;
  245. /* Data pointer. */
  246. void *data;
  247. } specific_1stblock[PTHREAD_KEY_2NDLEVEL_SIZE];
  248. /* Two-level array for the thread-specific data. */
  249. struct pthread_key_data *specific[PTHREAD_KEY_1STLEVEL_SIZE];
  250. /* Flag which is set when specific data is set. */
  251. bool specific_used;
  252. /* True if events must be reported. */
  253. bool report_events;
  254. /* True if the user provided the stack. */
  255. bool user_stack;
  256. /* True if thread must stop at startup time. */
  257. bool stopped_start;
  258. /* The parent's cancel handling at the time of the pthread_create
  259. call. This might be needed to undo the effects of a cancellation. */
  260. int parent_cancelhandling;
  261. /* Lock to synchronize access to the descriptor. */
  262. int lock;
  263. /* Lock for synchronizing setxid calls. */
  264. int setxid_futex;
  265. #if HP_TIMING_AVAIL
  266. /* Offset of the CPU clock at start thread start time. */
  267. hp_timing_t cpuclock_offset;
  268. #endif
  269. /* If the thread waits to join another one the ID of the latter is
  270. stored here.
  271. In case a thread is detached this field contains a pointer of the
  272. TCB if the thread itself. This is something which cannot happen
  273. in normal operation. */
  274. struct pthread *joinid;
  275. /* Check whether a thread is detached. */
  276. #define IS_DETACHED(pd) ((pd)->joinid == (pd))
  277. /* The result of the thread function. */
  278. void *result;
  279. /* Scheduling parameters for the new thread. */
  280. struct sched_param schedparam;
  281. int schedpolicy;
  282. /* Start position of the code to be executed and the argument passed
  283. to the function. */
  284. void *(*start_routine) (void *);
  285. void *arg;
  286. /* Debug state. */
  287. td_eventbuf_t eventbuf;
  288. /* Next descriptor with a pending event. */
  289. struct pthread *nextevent;
  290. #ifdef HAVE_FORCED_UNWIND
  291. /* Machine-specific unwind info. */
  292. struct _Unwind_Exception exc;
  293. #endif
  294. /* If nonzero pointer to area allocated for the stack and its
  295. size. */
  296. void *stackblock;
  297. size_t stackblock_size;
  298. /* Size of the included guard area. */
  299. size_t guardsize;
  300. /* This is what the user specified and what we will report. */
  301. size_t reported_guardsize;
  302. /* Thread Priority Protection data. */
  303. struct priority_protection_data *tpp;
  304. /* Resolver state. */
  305. struct __res_state res;
  306. /* This member must be last. */
  307. char end_padding[];
  308. #define PTHREAD_STRUCT_END_PADDING \
  309. (sizeof (struct pthread) - offsetof (struct pthread, end_padding))
  310. } __attribute ((aligned (TCB_ALIGNMENT)));
  311. #endif /* descr.h */