descr.h 12 KB

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