descr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. # ifdef NEED_DL_SYSINFO
  107. uintptr_t sysinfo;
  108. # endif
  109. } data;
  110. void *__padding[16];
  111. } p_header;
  112. # define p_multiple_threads p_header.data.multiple_threads
  113. #elif defined TLS_MULTIPLE_THREADS_IN_TCB && TLS_MULTIPLE_THREADS_IN_TCB
  114. int p_multiple_threads;
  115. #endif
  116. pthread_descr p_nextlive, p_prevlive;
  117. /* Double chaining of active threads */
  118. pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
  119. pthread_descr p_nextlock; /* can be on a queue and waiting on a lock */
  120. pthread_t p_tid; /* Thread identifier */
  121. int p_pid; /* PID of Unix process */
  122. int p_priority; /* Thread priority (== 0 if not realtime) */
  123. struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
  124. int p_signal; /* last signal received */
  125. sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
  126. sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
  127. char p_terminated; /* true if terminated e.g. by pthread_exit */
  128. char p_detached; /* true if detached */
  129. char p_exited; /* true if the assoc. process terminated */
  130. void * p_retval; /* placeholder for return value */
  131. int p_retcode; /* placeholder for return code */
  132. pthread_descr p_joining; /* thread joining on that thread or NULL */
  133. struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
  134. char p_cancelstate; /* cancellation state */
  135. char p_canceltype; /* cancellation type (deferred/async) */
  136. char p_canceled; /* cancellation request pending */
  137. char * p_in_sighandler; /* stack address of sighandler, or NULL */
  138. char p_sigwaiting; /* true if a sigwait() is in progress */
  139. struct pthread_start_args p_start_args; /* arguments for thread creation */
  140. void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
  141. #if !(USE_TLS && HAVE___THREAD)
  142. void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
  143. int * p_errnop; /* pointer to used errno variable */
  144. int p_errno; /* error returned by last system call */
  145. int * p_h_errnop; /* pointer to used h_errno variable */
  146. int p_h_errno; /* error returned by last netdb function */
  147. struct __res_state *p_resp; /* Pointer to resolver state */
  148. #endif
  149. struct __res_state p_res; /* per-thread resolver state */
  150. int p_userstack; /* nonzero if the user provided the stack */
  151. void *p_guardaddr; /* address of guard area or NULL */
  152. size_t p_guardsize; /* size of guard area */
  153. int p_nr; /* Index of descriptor in __pthread_handles */
  154. int p_report_events; /* Nonzero if events must be reported. */
  155. td_eventbuf_t p_eventbuf; /* Data for event. */
  156. struct pthread_atomic p_resume_count; /* number of times restart() was
  157. called on thread */
  158. char p_woken_by_cancel; /* cancellation performed wakeup */
  159. char p_condvar_avail; /* flag if conditional variable became avail */
  160. char p_sem_avail; /* flag if semaphore became available */
  161. pthread_extricate_if *p_extricate; /* See above */
  162. pthread_readlock_info *p_readlock_list; /* List of readlock info structs */
  163. pthread_readlock_info *p_readlock_free; /* Free list of structs */
  164. int p_untracked_readlock_count; /* Readlocks not tracked by list */
  165. int p_inheritsched; /* copied from the thread attribute */
  166. #if HP_TIMING_AVAIL
  167. hp_timing_t p_cpuclock_offset; /* Initial CPU clock for thread. */
  168. #endif
  169. #ifdef USE_TLS
  170. char *p_stackaddr; /* Stack address. */
  171. #endif
  172. size_t p_alloca_cutoff; /* Maximum size which should be allocated
  173. using alloca() instead of malloc(). */
  174. /* New elements must be added at the end. */
  175. } __attribute__ ((aligned(32))); /* We need to align the structure so that
  176. doubles are aligned properly. This is 8
  177. bytes on MIPS and 16 bytes on MIPS64.
  178. 32 bytes might give better cache
  179. utilization. */
  180. /* Limit between the stack of the initial thread (above) and the
  181. stacks of other threads (below). Aligned on a STACK_SIZE boundary.
  182. Initially 0, meaning that the current thread is (by definition)
  183. the initial thread. */
  184. extern char *__pthread_initial_thread_bos;
  185. /* Descriptor of the initial thread */
  186. extern struct _pthread_descr_struct __pthread_initial_thread;
  187. /* Limits of the thread manager stack. */
  188. extern char *__pthread_manager_thread_bos;
  189. extern char *__pthread_manager_thread_tos;
  190. /* Descriptor of the manager thread */
  191. extern struct _pthread_descr_struct __pthread_manager_thread;
  192. extern pthread_descr __pthread_manager_threadp attribute_hidden;
  193. /* Indicate whether at least one thread has a user-defined stack (if 1),
  194. or all threads have stacks supplied by LinuxThreads (if 0). */
  195. extern int __pthread_nonstandard_stacks;
  196. /* The max size of the thread stack segments. If the default
  197. THREAD_SELF implementation is used, this must be a power of two and
  198. a multiple of PAGE_SIZE. */
  199. #ifndef STACK_SIZE
  200. #define STACK_SIZE (2 * 1024 * 1024)
  201. #endif
  202. /* Get some notion of the current stack. Need not be exactly the top
  203. of the stack, just something somewhere in the current frame. */
  204. #ifndef CURRENT_STACK_FRAME
  205. #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
  206. #endif
  207. /* Recover thread descriptor for the current thread */
  208. extern pthread_descr __pthread_find_self (void) __attribute__ ((pure));
  209. static inline pthread_descr thread_self (void) __attribute__ ((pure));
  210. static inline pthread_descr thread_self (void)
  211. {
  212. #ifdef THREAD_SELF
  213. return THREAD_SELF;
  214. #else
  215. char *sp = CURRENT_STACK_FRAME;
  216. if (sp >= __pthread_initial_thread_bos)
  217. return &__pthread_initial_thread;
  218. else if (sp >= __pthread_manager_thread_bos
  219. && sp < __pthread_manager_thread_tos)
  220. return &__pthread_manager_thread;
  221. else if (__pthread_nonstandard_stacks)
  222. return __pthread_find_self();
  223. else
  224. #ifdef _STACK_GROWS_DOWN
  225. return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
  226. #else
  227. return (pthread_descr)((unsigned long)sp &~ (STACK_SIZE-1));
  228. #endif
  229. #endif
  230. }
  231. #endif /* descr.h */