internals.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 _INTERNALS_H
  15. #define _INTERNALS_H 1
  16. /* Internal data structures */
  17. /* Includes */
  18. #include <bits/libc-tsd.h> /* for _LIBC_TSD_KEY_N */
  19. #include <limits.h>
  20. #include <setjmp.h>
  21. #include <signal.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include "pt-machine.h"
  25. #include "semaphore.h"
  26. #include "../linuxthreads_db/thread_dbP.h"
  27. #ifndef THREAD_GETMEM
  28. # define THREAD_GETMEM(descr, member) descr->member
  29. #endif
  30. #ifndef THREAD_GETMEM_NC
  31. # define THREAD_GETMEM_NC(descr, member) descr->member
  32. #endif
  33. #ifndef THREAD_SETMEM
  34. # define THREAD_SETMEM(descr, member, value) descr->member = (value)
  35. #endif
  36. #ifndef THREAD_SETMEM_NC
  37. # define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
  38. #endif
  39. /* Arguments passed to thread creation routine */
  40. struct pthread_start_args {
  41. void * (*start_routine)(void *); /* function to run */
  42. void * arg; /* its argument */
  43. sigset_t mask; /* initial signal mask for thread */
  44. int schedpolicy; /* initial scheduling policy (if any) */
  45. struct sched_param schedparam; /* initial scheduling parameters (if any) */
  46. };
  47. /* We keep thread specific data in a special data structure, a two-level
  48. array. The top-level array contains pointers to dynamically allocated
  49. arrays of a certain number of data pointers. So we can implement a
  50. sparse array. Each dynamic second-level array has
  51. PTHREAD_KEY_2NDLEVEL_SIZE
  52. entries. This value shouldn't be too large. */
  53. #define PTHREAD_KEY_2NDLEVEL_SIZE 32
  54. /* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
  55. keys in each subarray. */
  56. #define PTHREAD_KEY_1STLEVEL_SIZE \
  57. ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
  58. / PTHREAD_KEY_2NDLEVEL_SIZE)
  59. typedef void (*destr_function)(void *);
  60. struct pthread_key_struct {
  61. int in_use; /* already allocated? */
  62. destr_function destr; /* destruction routine */
  63. };
  64. #define PTHREAD_START_ARGS_INITIALIZER { NULL, NULL, {{0, }}, 0, { 0 } }
  65. /* The type of thread descriptors */
  66. typedef struct _pthread_descr_struct * pthread_descr;
  67. /* Callback interface for removing the thread from waiting on an
  68. object if it is cancelled while waiting or about to wait.
  69. This hold a pointer to the object, and a pointer to a function
  70. which ``extricates'' the thread from its enqueued state.
  71. The function takes two arguments: pointer to the wait object,
  72. and a pointer to the thread. It returns 1 if an extrication
  73. actually occured, and hence the thread must also be signalled.
  74. It returns 0 if the thread had already been extricated. */
  75. typedef struct _pthread_extricate_struct {
  76. void *pu_object;
  77. int (*pu_extricate_func)(void *, pthread_descr);
  78. } pthread_extricate_if;
  79. /* Atomic counter made possible by compare_and_swap */
  80. struct pthread_atomic {
  81. long p_count;
  82. int p_spinlock;
  83. };
  84. /* Context info for read write locks. The pthread_rwlock_info structure
  85. is information about a lock that has been read-locked by the thread
  86. in whose list this structure appears. The pthread_rwlock_context
  87. is embedded in the thread context and contains a pointer to the
  88. head of the list of lock info structures, as well as a count of
  89. read locks that are untracked, because no info structure could be
  90. allocated for them. */
  91. struct _pthread_rwlock_t;
  92. typedef struct _pthread_rwlock_info {
  93. struct _pthread_rwlock_info *pr_next;
  94. struct _pthread_rwlock_t *pr_lock;
  95. int pr_lock_count;
  96. } pthread_readlock_info;
  97. struct _pthread_descr_struct {
  98. pthread_descr p_nextlive, p_prevlive;
  99. /* Double chaining of active threads */
  100. pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
  101. pthread_descr p_nextlock; /* can be on a queue and waiting on a lock */
  102. pthread_t p_tid; /* Thread identifier */
  103. int p_pid; /* PID of Unix process */
  104. int p_priority; /* Thread priority (== 0 if not realtime) */
  105. struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
  106. int p_signal; /* last signal received */
  107. sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
  108. sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
  109. char p_terminated; /* true if terminated e.g. by pthread_exit */
  110. char p_detached; /* true if detached */
  111. char p_exited; /* true if the assoc. process terminated */
  112. void * p_retval; /* placeholder for return value */
  113. int p_retcode; /* placeholder for return code */
  114. pthread_descr p_joining; /* thread joining on that thread or NULL */
  115. struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
  116. char p_cancelstate; /* cancellation state */
  117. char p_canceltype; /* cancellation type (deferred/async) */
  118. char p_canceled; /* cancellation request pending */
  119. int * p_errnop; /* pointer to used errno variable */
  120. int p_errno; /* error returned by last system call */
  121. int * p_h_errnop; /* pointer to used h_errno variable */
  122. int p_h_errno; /* error returned by last netdb function */
  123. char * p_in_sighandler; /* stack address of sighandler, or NULL */
  124. char p_sigwaiting; /* true if a sigwait() is in progress */
  125. struct pthread_start_args p_start_args; /* arguments for thread creation */
  126. void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
  127. void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
  128. int p_userstack; /* nonzero if the user provided the stack */
  129. void *p_guardaddr; /* address of guard area or NULL */
  130. size_t p_guardsize; /* size of guard area */
  131. pthread_descr p_self; /* Pointer to this structure */
  132. int p_nr; /* Index of descriptor in __pthread_handles */
  133. int p_report_events; /* Nonzero if events must be reported. */
  134. td_eventbuf_t p_eventbuf; /* Data for event. */
  135. struct pthread_atomic p_resume_count; /* number of times restart() was
  136. called on thread */
  137. char p_woken_by_cancel; /* cancellation performed wakeup */
  138. pthread_extricate_if *p_extricate; /* See above */
  139. pthread_readlock_info *p_readlock_list; /* List of readlock info structs */
  140. pthread_readlock_info *p_readlock_free; /* Free list of structs */
  141. int p_untracked_readlock_count; /* Readlocks not tracked by list */
  142. /* New elements must be added at the end. */
  143. } __attribute__ ((aligned(32))); /* We need to align the structure so that
  144. doubles are aligned properly. This is 8
  145. bytes on MIPS and 16 bytes on MIPS64.
  146. 32 bytes might give better cache
  147. utilization. */
  148. /* The type of thread handles. */
  149. typedef struct pthread_handle_struct * pthread_handle;
  150. struct pthread_handle_struct {
  151. struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
  152. pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
  153. char * h_bottom; /* Lowest address in the stack thread */
  154. };
  155. /* The type of messages sent to the thread manager thread */
  156. struct pthread_request {
  157. pthread_descr req_thread; /* Thread doing the request */
  158. enum { /* Request kind */
  159. REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
  160. REQ_POST, REQ_DEBUG
  161. } req_kind;
  162. union { /* Arguments for request */
  163. struct { /* For REQ_CREATE: */
  164. const pthread_attr_t * attr; /* thread attributes */
  165. void * (*fn)(void *); /* start function */
  166. void * arg; /* argument to start function */
  167. sigset_t mask; /* signal mask */
  168. } create;
  169. struct { /* For REQ_FREE: */
  170. pthread_t thread_id; /* identifier of thread to free */
  171. } free;
  172. struct { /* For REQ_PROCESS_EXIT: */
  173. int code; /* exit status */
  174. } exit;
  175. void * post; /* For REQ_POST: the semaphore */
  176. } req_args;
  177. };
  178. /* Signals used for suspend/restart and for cancellation notification. */
  179. extern int __pthread_sig_restart;
  180. extern int __pthread_sig_cancel;
  181. /* Signal used for interfacing with gdb */
  182. extern int __pthread_sig_debug;
  183. /* Global array of thread handles, used for validating a thread id
  184. and retrieving the corresponding thread descriptor. Also used for
  185. mapping the available stack segments. */
  186. extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
  187. /* Descriptor of the initial thread */
  188. extern struct _pthread_descr_struct __pthread_initial_thread;
  189. /* Descriptor of the manager thread */
  190. extern struct _pthread_descr_struct __pthread_manager_thread;
  191. /* Descriptor of the main thread */
  192. extern pthread_descr __pthread_main_thread;
  193. /* Limit between the stack of the initial thread (above) and the
  194. stacks of other threads (below). Aligned on a STACK_SIZE boundary.
  195. Initially 0, meaning that the current thread is (by definition)
  196. the initial thread. */
  197. /* For non-MMU systems also remember to stack top of the initial thread.
  198. * This is adapted when other stacks are malloc'ed since we don't know
  199. * the bounds a-priori. -StS */
  200. extern char *__pthread_initial_thread_bos;
  201. #ifndef __UCLIBC_HAS_MMU__
  202. extern char *__pthread_initial_thread_tos;
  203. #define NOMMU_INITIAL_THREAD_BOUNDS(tos,bos) if ((tos)>=__pthread_initial_thread_bos && (bos)<=__pthread_initial_thread_tos) __pthread_initial_thread_bos = (tos)+1
  204. #else
  205. #define NOMMU_INITIAL_THREAD_BOUNDS(tos,bos) /* empty */
  206. #endif /* __UCLIBC_HAS_MMU__ */
  207. /* Indicate whether at least one thread has a user-defined stack (if 1),
  208. or all threads have stacks supplied by LinuxThreads (if 0). */
  209. extern int __pthread_nonstandard_stacks;
  210. /* File descriptor for sending requests to the thread manager.
  211. Initially -1, meaning that __pthread_initialize_manager must be called. */
  212. extern int __pthread_manager_request;
  213. /* Other end of the pipe for sending requests to the thread manager. */
  214. extern int __pthread_manager_reader;
  215. /* Limits of the thread manager stack. */
  216. extern char *__pthread_manager_thread_bos;
  217. extern char *__pthread_manager_thread_tos;
  218. /* Pending request for a process-wide exit */
  219. extern int __pthread_exit_requested, __pthread_exit_code;
  220. /* Set to 1 by gdb if we're debugging */
  221. extern volatile int __pthread_threads_debug;
  222. /* Globally enabled events. */
  223. extern volatile td_thr_events_t __pthread_threads_events;
  224. /* Pointer to descriptor of thread with last event. */
  225. extern volatile pthread_descr __pthread_last_event;
  226. /* Return the handle corresponding to a thread id */
  227. static inline pthread_handle thread_handle(pthread_t id)
  228. {
  229. return &__pthread_handles[id % PTHREAD_THREADS_MAX];
  230. }
  231. /* Validate a thread handle. Must have acquired h->h_spinlock before. */
  232. static inline int invalid_handle(pthread_handle h, pthread_t id)
  233. {
  234. return h->h_descr == NULL || h->h_descr->p_tid != id;
  235. }
  236. /* Fill in defaults left unspecified by pt-machine.h. */
  237. /* The page size we can get from the system. This should likely not be
  238. changed by the machine file but, you never know. */
  239. #ifndef PAGE_SIZE
  240. #define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
  241. #endif
  242. /* The max size of the thread stack segments. If the default
  243. THREAD_SELF implementation is used, this must be a power of two and
  244. a multiple of PAGE_SIZE. */
  245. #ifndef STACK_SIZE
  246. #define STACK_SIZE (2 * 1024 * 1024)
  247. #endif
  248. /* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
  249. #ifndef INITIAL_STACK_SIZE
  250. #define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
  251. #endif
  252. /* Size of the thread manager stack. The "- 32" avoids wasting space
  253. with some malloc() implementations. */
  254. #ifndef THREAD_MANAGER_STACK_SIZE
  255. #define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
  256. #endif
  257. /* The base of the "array" of thread stacks. The array will grow down from
  258. here. Defaults to the calculated bottom of the initial application
  259. stack. */
  260. #ifndef THREAD_STACK_START_ADDRESS
  261. #define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
  262. #endif
  263. /* Get some notion of the current stack. Need not be exactly the top
  264. of the stack, just something somewhere in the current frame. */
  265. #ifndef CURRENT_STACK_FRAME
  266. #define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
  267. #endif
  268. /* Recover thread descriptor for the current thread */
  269. extern pthread_descr __pthread_find_self (void) __attribute__ ((const));
  270. static inline pthread_descr thread_self (void) __attribute__ ((const));
  271. static inline pthread_descr thread_self (void)
  272. {
  273. #ifdef THREAD_SELF
  274. return THREAD_SELF;
  275. #else
  276. char *sp = CURRENT_STACK_FRAME;
  277. #ifdef __UCLIBC_HAS_MMU__
  278. if (sp >= __pthread_initial_thread_bos)
  279. return &__pthread_initial_thread;
  280. else if (sp >= __pthread_manager_thread_bos
  281. && sp < __pthread_manager_thread_tos)
  282. return &__pthread_manager_thread;
  283. else if (__pthread_nonstandard_stacks)
  284. return __pthread_find_self();
  285. else
  286. return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
  287. #else
  288. /* For non-MMU we need to be more careful about the initial thread stack.
  289. * We refine the initial thread stack bounds dynamically as we allocate
  290. * the other stack frame such that it doesn't overlap with them. Then
  291. * we can be sure to pick the right thread according to the current SP */
  292. /* Since we allow other stack frames to be above or below, we need to
  293. * treat this case special. When pthread_initialize() wasn't called yet,
  294. * only the initial thread is there. */
  295. if (__pthread_initial_thread_bos == NULL) {
  296. return &__pthread_initial_thread;
  297. }
  298. else if (sp >= __pthread_initial_thread_bos
  299. && sp < __pthread_initial_thread_tos) {
  300. return &__pthread_initial_thread;
  301. }
  302. else if (sp >= __pthread_manager_thread_bos
  303. && sp < __pthread_manager_thread_tos) {
  304. return &__pthread_manager_thread;
  305. }
  306. else {
  307. return __pthread_find_self();
  308. }
  309. #endif /* __UCLIBC_HAS_MMU__ */
  310. #endif
  311. }
  312. /* Max number of times we must spin on a spinlock calling sched_yield().
  313. After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
  314. #ifndef MAX_SPIN_COUNT
  315. #define MAX_SPIN_COUNT 50
  316. #endif
  317. /* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
  318. after MAX_SPIN_COUNT iterations of sched_yield().
  319. With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
  320. (Otherwise the kernel does busy-waiting for realtime threads,
  321. giving other threads no chance to run.) */
  322. #ifndef SPIN_SLEEP_DURATION
  323. #define SPIN_SLEEP_DURATION 2000001
  324. #endif
  325. /* Debugging */
  326. #ifdef DEBUG
  327. #include <assert.h>
  328. #define ASSERT assert
  329. #define MSG __pthread_message
  330. #else
  331. #define ASSERT(x)
  332. #define MSG(msg,arg...)
  333. #endif
  334. /* Internal global functions */
  335. void __pthread_destroy_specifics(void);
  336. void __pthread_perform_cleanup(void);
  337. int __pthread_initialize_manager(void);
  338. void __pthread_message(char * fmt, ...);
  339. int __pthread_manager(void *reqfd);
  340. int __pthread_manager_event(void *reqfd);
  341. void __pthread_manager_sighandler(int sig);
  342. void __pthread_reset_main_thread(void);
  343. void __fresetlockfiles(void);
  344. void __pthread_manager_adjust_prio(int thread_prio);
  345. void __pthread_set_own_extricate_if(pthread_descr self, pthread_extricate_if *peif);
  346. extern int __pthread_attr_setguardsize __P ((pthread_attr_t *__attr,
  347. size_t __guardsize));
  348. extern int __pthread_attr_getguardsize __P ((__const pthread_attr_t *__attr,
  349. size_t *__guardsize));
  350. extern int __pthread_attr_setstackaddr __P ((pthread_attr_t *__attr,
  351. void *__stackaddr));
  352. extern int __pthread_attr_getstackaddr __P ((__const pthread_attr_t *__attr,
  353. void **__stackaddr));
  354. extern int __pthread_attr_setstacksize __P ((pthread_attr_t *__attr,
  355. size_t __stacksize));
  356. extern int __pthread_attr_getstacksize __P ((__const pthread_attr_t *__attr,
  357. size_t *__stacksize));
  358. extern int __pthread_getconcurrency __P ((void));
  359. extern int __pthread_setconcurrency __P ((int __level));
  360. extern int __pthread_mutexattr_gettype __P ((__const pthread_mutexattr_t *__attr,
  361. int *__kind));
  362. extern void __pthread_kill_other_threads_np __P ((void));
  363. void __pthread_restart_old(pthread_descr th);
  364. void __pthread_suspend_old(pthread_descr self);
  365. void __pthread_restart_new(pthread_descr th);
  366. void __pthread_suspend_new(pthread_descr self);
  367. void __pthread_wait_for_restart_signal(pthread_descr self);
  368. void __pthread_init_condvar(int rt_sig_available);
  369. /* Global pointers to old or new suspend functions */
  370. extern void (*__pthread_restart)(pthread_descr);
  371. extern void (*__pthread_suspend)(pthread_descr);
  372. /* Prototypes for the function without cancelation support when the
  373. normal version has it. */
  374. extern int __libc_close (int fd);
  375. extern int __libc_nanosleep (const struct timespec *requested_time,
  376. struct timespec *remaining);
  377. extern ssize_t __libc_read (int fd, void *buf, size_t count);
  378. extern pid_t __libc_waitpid (pid_t pid, int *stat_loc, int options);
  379. extern ssize_t __libc_write (int fd, const void *buf, size_t count);
  380. /* Prototypes for some of the new semaphore functions. */
  381. extern int __new_sem_post (sem_t * sem);
  382. /* The functions called the signal events. */
  383. extern void __linuxthreads_create_event (void);
  384. extern void __linuxthreads_death_event (void);
  385. extern void __linuxthreads_reap_event (void);
  386. #endif /* internals.h */