descr.h 11 KB

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