pthread.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. /* Thread creation, initialization, and basic low-level routines */
  15. #define __FORCE_GLIBC
  16. #include <features.h>
  17. #include <errno.h>
  18. #include <netdb.h> /* for h_errno */
  19. #include <stddef.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <sys/wait.h>
  26. #include <sys/resource.h>
  27. #include "pthread.h"
  28. #include "internals.h"
  29. #include "spinlock.h"
  30. #include "restart.h"
  31. #include "debug.h" /* added to linuxthreads -StS */
  32. /* Mods for uClibc: Some includes */
  33. #include <signal.h>
  34. #include <sys/types.h>
  35. #include <sys/syscall.h>
  36. /* mods for uClibc: __libc_sigaction is not in any standard headers */
  37. extern __typeof(sigaction) __libc_sigaction;
  38. libpthread_hidden_proto(waitpid)
  39. libpthread_hidden_proto(raise)
  40. /* These variables are used by the setup code. */
  41. extern int _errno;
  42. extern int _h_errno;
  43. /* Descriptor of the initial thread */
  44. struct _pthread_descr_struct __pthread_initial_thread = {
  45. &__pthread_initial_thread, /* pthread_descr p_nextlive */
  46. &__pthread_initial_thread, /* pthread_descr p_prevlive */
  47. NULL, /* pthread_descr p_nextwaiting */
  48. NULL, /* pthread_descr p_nextlock */
  49. PTHREAD_THREADS_MAX, /* pthread_t p_tid */
  50. 0, /* int p_pid */
  51. 0, /* int p_priority */
  52. &__pthread_handles[0].h_lock, /* struct _pthread_fastlock * p_lock */
  53. 0, /* int p_signal */
  54. NULL, /* sigjmp_buf * p_signal_buf */
  55. NULL, /* sigjmp_buf * p_cancel_buf */
  56. 0, /* char p_terminated */
  57. 0, /* char p_detached */
  58. 0, /* char p_exited */
  59. NULL, /* void * p_retval */
  60. 0, /* int p_retval */
  61. NULL, /* pthread_descr p_joining */
  62. NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
  63. 0, /* char p_cancelstate */
  64. 0, /* char p_canceltype */
  65. 0, /* char p_canceled */
  66. &_errno, /* int *p_errnop */
  67. 0, /* int p_errno */
  68. &_h_errno, /* int *p_h_errnop */
  69. 0, /* int p_h_errno */
  70. NULL, /* char * p_in_sighandler */
  71. 0, /* char p_sigwaiting */
  72. PTHREAD_START_ARGS_INITIALIZER, /* struct pthread_start_args p_start_args */
  73. {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
  74. {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
  75. 0, /* int p_userstack */
  76. NULL, /* void * p_guardaddr */
  77. 0, /* size_t p_guardsize */
  78. &__pthread_initial_thread, /* pthread_descr p_self */
  79. 0, /* Always index 0 */
  80. 0, /* int p_report_events */
  81. {{{0, }}, 0, NULL}, /* td_eventbuf_t p_eventbuf */
  82. __ATOMIC_INITIALIZER, /* struct pthread_atomic p_resume_count */
  83. 0, /* char p_woken_by_cancel */
  84. 0, /* char p_condvar_avail */
  85. 0, /* char p_sem_avail */
  86. NULL, /* struct pthread_extricate_if *p_extricate */
  87. NULL, /* pthread_readlock_info *p_readlock_list; */
  88. NULL, /* pthread_readlock_info *p_readlock_free; */
  89. 0 /* int p_untracked_readlock_count; */
  90. #ifdef __UCLIBC_HAS_XLOCALE__
  91. ,
  92. &__global_locale_data, /* __locale_t locale; */
  93. #endif /* __UCLIBC_HAS_XLOCALE__ */
  94. };
  95. /* Descriptor of the manager thread; none of this is used but the error
  96. variables, the p_pid and p_priority fields,
  97. and the address for identification. */
  98. #define manager_thread (&__pthread_manager_thread)
  99. struct _pthread_descr_struct __pthread_manager_thread = {
  100. NULL, /* pthread_descr p_nextlive */
  101. NULL, /* pthread_descr p_prevlive */
  102. NULL, /* pthread_descr p_nextwaiting */
  103. NULL, /* pthread_descr p_nextlock */
  104. 0, /* int p_tid */
  105. 0, /* int p_pid */
  106. 0, /* int p_priority */
  107. &__pthread_handles[1].h_lock, /* struct _pthread_fastlock * p_lock */
  108. 0, /* int p_signal */
  109. NULL, /* sigjmp_buf * p_signal_buf */
  110. NULL, /* sigjmp_buf * p_cancel_buf */
  111. 0, /* char p_terminated */
  112. 0, /* char p_detached */
  113. 0, /* char p_exited */
  114. NULL, /* void * p_retval */
  115. 0, /* int p_retval */
  116. NULL, /* pthread_descr p_joining */
  117. NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
  118. 0, /* char p_cancelstate */
  119. 0, /* char p_canceltype */
  120. 0, /* char p_canceled */
  121. &__pthread_manager_thread.p_errno, /* int *p_errnop */
  122. 0, /* int p_errno */
  123. NULL, /* int *p_h_errnop */
  124. 0, /* int p_h_errno */
  125. NULL, /* char * p_in_sighandler */
  126. 0, /* char p_sigwaiting */
  127. PTHREAD_START_ARGS_INITIALIZER, /* struct pthread_start_args p_start_args */
  128. {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
  129. {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
  130. 0, /* int p_userstack */
  131. NULL, /* void * p_guardaddr */
  132. 0, /* size_t p_guardsize */
  133. &__pthread_manager_thread, /* pthread_descr p_self */
  134. 1, /* Always index 1 */
  135. 0, /* int p_report_events */
  136. {{{0, }}, 0, NULL}, /* td_eventbuf_t p_eventbuf */
  137. __ATOMIC_INITIALIZER, /* struct pthread_atomic p_resume_count */
  138. 0, /* char p_woken_by_cancel */
  139. 0, /* char p_condvar_avail */
  140. 0, /* char p_sem_avail */
  141. NULL, /* struct pthread_extricate_if *p_extricate */
  142. NULL, /* pthread_readlock_info *p_readlock_list; */
  143. NULL, /* pthread_readlock_info *p_readlock_free; */
  144. 0 /* int p_untracked_readlock_count; */
  145. #ifdef __UCLIBC_HAS_XLOCALE__
  146. ,
  147. &__global_locale_data, /* __locale_t locale; */
  148. #endif /* __UCLIBC_HAS_XLOCALE__ */
  149. };
  150. /* Pointer to the main thread (the father of the thread manager thread) */
  151. /* Originally, this is the initial thread, but this changes after fork() */
  152. pthread_descr __pthread_main_thread = &__pthread_initial_thread;
  153. /* Limit between the stack of the initial thread (above) and the
  154. stacks of other threads (below). Aligned on a STACK_SIZE boundary. */
  155. char *__pthread_initial_thread_bos = NULL;
  156. /* For non-MMU systems also remember to stack top of the initial thread.
  157. * This is adapted when other stacks are malloc'ed since we don't know
  158. * the bounds a-priori. -StS */
  159. #ifndef __ARCH_HAS_MMU__
  160. char *__pthread_initial_thread_tos = NULL;
  161. #endif /* __ARCH_HAS_MMU__ */
  162. /* File descriptor for sending requests to the thread manager. */
  163. /* Initially -1, meaning that the thread manager is not running. */
  164. int __pthread_manager_request = -1;
  165. /* Other end of the pipe for sending requests to the thread manager. */
  166. int __pthread_manager_reader;
  167. /* Limits of the thread manager stack */
  168. char *__pthread_manager_thread_bos = NULL;
  169. char *__pthread_manager_thread_tos = NULL;
  170. /* For process-wide exit() */
  171. int __pthread_exit_requested = 0;
  172. int __pthread_exit_code = 0;
  173. /* Communicate relevant LinuxThreads constants to gdb */
  174. const int __pthread_threads_max = PTHREAD_THREADS_MAX;
  175. const int __pthread_sizeof_handle = sizeof(struct pthread_handle_struct);
  176. const int __pthread_offsetof_descr = offsetof(struct pthread_handle_struct, h_descr);
  177. const int __pthread_offsetof_pid = offsetof(struct _pthread_descr_struct,
  178. p_pid);
  179. const int __linuxthreads_pthread_sizeof_descr
  180. = sizeof(struct _pthread_descr_struct);
  181. const int __linuxthreads_initial_report_events;
  182. const char __linuxthreads_version[] = VERSION;
  183. /* Forward declarations */
  184. static void pthread_onexit_process(int retcode, void *arg);
  185. static void pthread_handle_sigcancel(int sig);
  186. static void pthread_handle_sigrestart(int sig);
  187. static void pthread_handle_sigdebug(int sig);
  188. int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime);
  189. /* Signal numbers used for the communication.
  190. In these variables we keep track of the used variables. If the
  191. platform does not support any real-time signals we will define the
  192. values to some unreasonable value which will signal failing of all
  193. the functions below. */
  194. #ifndef __NR_rt_sigaction
  195. static int current_rtmin = -1;
  196. static int current_rtmax = -1;
  197. int __pthread_sig_restart = SIGUSR1;
  198. int __pthread_sig_cancel = SIGUSR2;
  199. int __pthread_sig_debug;
  200. #else
  201. #if __SIGRTMAX - __SIGRTMIN >= 3
  202. static int current_rtmin = __SIGRTMIN + 3;
  203. static int current_rtmax = __SIGRTMAX;
  204. int __pthread_sig_restart = __SIGRTMIN;
  205. int __pthread_sig_cancel = __SIGRTMIN + 1;
  206. int __pthread_sig_debug = __SIGRTMIN + 2;
  207. void (*__pthread_restart)(pthread_descr) = __pthread_restart_new;
  208. void (*__pthread_suspend)(pthread_descr) = __pthread_wait_for_restart_signal;
  209. int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *) = __pthread_timedsuspend_new;
  210. #else
  211. static int current_rtmin = __SIGRTMIN;
  212. static int current_rtmax = __SIGRTMAX;
  213. int __pthread_sig_restart = SIGUSR1;
  214. int __pthread_sig_cancel = SIGUSR2;
  215. int __pthread_sig_debug;
  216. void (*__pthread_restart)(pthread_descr) = __pthread_restart_old;
  217. void (*__pthread_suspend)(pthread_descr) = __pthread_suspend_old;
  218. int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *) = __pthread_timedsuspend_old;
  219. #endif
  220. /* Return number of available real-time signal with highest priority. */
  221. int __libc_current_sigrtmin (void)
  222. {
  223. return current_rtmin;
  224. }
  225. /* Return number of available real-time signal with lowest priority. */
  226. int __libc_current_sigrtmax (void)
  227. {
  228. return current_rtmax;
  229. }
  230. /* Allocate real-time signal with highest/lowest available
  231. priority. Please note that we don't use a lock since we assume
  232. this function to be called at program start. */
  233. int __libc_allocate_rtsig (int high);
  234. int __libc_allocate_rtsig (int high)
  235. {
  236. if (current_rtmin == -1 || current_rtmin > current_rtmax)
  237. /* We don't have anymore signal available. */
  238. return -1;
  239. return high ? current_rtmin++ : current_rtmax--;
  240. }
  241. #endif
  242. /* Initialize the pthread library.
  243. Initialization is split in two functions:
  244. - a constructor function that blocks the __pthread_sig_restart signal
  245. (must do this very early, since the program could capture the signal
  246. mask with e.g. sigsetjmp before creating the first thread);
  247. - a regular function called from pthread_create when needed. */
  248. static void pthread_initialize(void) __attribute__((constructor));
  249. struct pthread_functions __pthread_functions =
  250. {
  251. #if !(USE_TLS && HAVE___THREAD)
  252. .ptr_pthread_internal_tsd_set = __pthread_internal_tsd_set,
  253. .ptr_pthread_internal_tsd_get = __pthread_internal_tsd_get,
  254. .ptr_pthread_internal_tsd_address = __pthread_internal_tsd_address,
  255. #endif
  256. /*
  257. .ptr_pthread_fork = __pthread_fork,
  258. */
  259. .ptr_pthread_attr_destroy = pthread_attr_destroy,
  260. .ptr_pthread_attr_init = pthread_attr_init,
  261. .ptr_pthread_attr_getdetachstate = pthread_attr_getdetachstate,
  262. .ptr_pthread_attr_setdetachstate = pthread_attr_setdetachstate,
  263. .ptr_pthread_attr_getinheritsched = pthread_attr_getinheritsched,
  264. .ptr_pthread_attr_setinheritsched = pthread_attr_setinheritsched,
  265. .ptr_pthread_attr_getschedparam = pthread_attr_getschedparam,
  266. .ptr_pthread_attr_setschedparam = pthread_attr_setschedparam,
  267. .ptr_pthread_attr_getschedpolicy = pthread_attr_getschedpolicy,
  268. .ptr_pthread_attr_setschedpolicy = pthread_attr_setschedpolicy,
  269. .ptr_pthread_attr_getscope = pthread_attr_getscope,
  270. .ptr_pthread_attr_setscope = pthread_attr_setscope,
  271. .ptr_pthread_condattr_destroy = pthread_condattr_destroy,
  272. .ptr_pthread_condattr_init = pthread_condattr_init,
  273. .ptr_pthread_cond_broadcast = pthread_cond_broadcast,
  274. .ptr_pthread_cond_destroy = pthread_cond_destroy,
  275. .ptr_pthread_cond_init = pthread_cond_init,
  276. .ptr_pthread_cond_signal = pthread_cond_signal,
  277. .ptr_pthread_cond_wait = pthread_cond_wait,
  278. .ptr_pthread_cond_timedwait = pthread_cond_timedwait,
  279. .ptr_pthread_equal = pthread_equal,
  280. .ptr___pthread_exit = pthread_exit,
  281. .ptr_pthread_getschedparam = pthread_getschedparam,
  282. .ptr_pthread_setschedparam = pthread_setschedparam,
  283. .ptr_pthread_mutex_destroy = __pthread_mutex_destroy,
  284. .ptr_pthread_mutex_init = __pthread_mutex_init,
  285. .ptr_pthread_mutex_lock = __pthread_mutex_lock,
  286. .ptr_pthread_mutex_trylock = __pthread_mutex_trylock,
  287. .ptr_pthread_mutex_unlock = __pthread_mutex_unlock,
  288. .ptr_pthread_self = pthread_self,
  289. .ptr_pthread_setcancelstate = pthread_setcancelstate,
  290. .ptr_pthread_setcanceltype = pthread_setcanceltype,
  291. /*
  292. .ptr_pthread_do_exit = pthread_do_exit,
  293. .ptr_pthread_thread_self = pthread_thread_self,
  294. .ptr_pthread_cleanup_upto = pthread_cleanup_upto,
  295. .ptr_pthread_sigaction = pthread_sigaction,
  296. .ptr_pthread_sigwait = pthread_sigwait,
  297. .ptr_pthread_raise = pthread_raise,
  298. .ptr__pthread_cleanup_push = _pthread_cleanup_push,
  299. .ptr__pthread_cleanup_pop = _pthread_cleanup_pop
  300. */
  301. };
  302. #ifdef SHARED
  303. # define ptr_pthread_functions &__pthread_functions
  304. #else
  305. # define ptr_pthread_functions NULL
  306. #endif
  307. static int *__libc_multiple_threads_ptr;
  308. /* Do some minimal initialization which has to be done during the
  309. startup of the C library. */
  310. void __pthread_initialize_minimal(void)
  311. {
  312. /* If we have special thread_self processing, initialize
  313. * that for the main thread now. */
  314. #ifdef INIT_THREAD_SELF
  315. INIT_THREAD_SELF(&__pthread_initial_thread, 0);
  316. #endif
  317. __libc_multiple_threads_ptr = __libc_pthread_init (ptr_pthread_functions);
  318. }
  319. static void pthread_initialize(void)
  320. {
  321. struct sigaction sa;
  322. sigset_t mask;
  323. struct rlimit limit;
  324. rlim_t max_stack;
  325. /* If already done (e.g. by a constructor called earlier!), bail out */
  326. if (__pthread_initial_thread_bos != NULL) return;
  327. #ifdef TEST_FOR_COMPARE_AND_SWAP
  328. /* Test if compare-and-swap is available */
  329. __pthread_has_cas = compare_and_swap_is_available();
  330. #endif
  331. /* For the initial stack, reserve at least STACK_SIZE bytes of stack
  332. below the current stack address, and align that on a
  333. STACK_SIZE boundary. */
  334. __pthread_initial_thread_bos =
  335. (char *)(((long)CURRENT_STACK_FRAME - 2 * STACK_SIZE) & ~(STACK_SIZE - 1));
  336. /* Update the descriptor for the initial thread. */
  337. __pthread_initial_thread.p_pid = getpid();
  338. /* If we have special thread_self processing, initialize that for the
  339. main thread now. */
  340. #ifdef INIT_THREAD_SELF
  341. INIT_THREAD_SELF(&__pthread_initial_thread, 0);
  342. #endif
  343. /* The errno/h_errno variable of the main thread are the global ones. */
  344. __pthread_initial_thread.p_errnop = &_errno;
  345. __pthread_initial_thread.p_h_errnop = &_h_errno;
  346. #ifdef __UCLIBC_HAS_XLOCALE__
  347. /* The locale of the main thread is the current locale in use. */
  348. __pthread_initial_thread.locale = __curlocale_var;
  349. #endif /* __UCLIBC_HAS_XLOCALE__ */
  350. { /* uClibc-specific stdio initialization for threads. */
  351. FILE *fp;
  352. _stdio_user_locking = 0; /* 2 if threading not initialized */
  353. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
  354. if (fp->__user_locking != 1) {
  355. fp->__user_locking = 0;
  356. }
  357. }
  358. }
  359. /* Play with the stack size limit to make sure that no stack ever grows
  360. beyond STACK_SIZE minus two pages (one page for the thread descriptor
  361. immediately beyond, and one page to act as a guard page). */
  362. #ifdef __ARCH_HAS_MMU__
  363. /* We cannot allocate a huge chunk of memory to mmap all thread stacks later
  364. * on a non-MMU system. Thus, we don't need the rlimit either. -StS */
  365. getrlimit(RLIMIT_STACK, &limit);
  366. max_stack = STACK_SIZE - 2 * getpagesize();
  367. if (limit.rlim_cur > max_stack) {
  368. limit.rlim_cur = max_stack;
  369. setrlimit(RLIMIT_STACK, &limit);
  370. }
  371. #else
  372. /* For non-MMU assume __pthread_initial_thread_tos at upper page boundary, and
  373. * __pthread_initial_thread_bos at address 0. These bounds are refined as we
  374. * malloc other stack frames such that they don't overlap. -StS
  375. */
  376. __pthread_initial_thread_tos =
  377. (char *)(((long)CURRENT_STACK_FRAME + getpagesize()) & ~(getpagesize() - 1));
  378. __pthread_initial_thread_bos = (char *) 1; /* set it non-zero so we know we have been here */
  379. PDEBUG("initial thread stack bounds: bos=%p, tos=%p\n",
  380. __pthread_initial_thread_bos, __pthread_initial_thread_tos);
  381. #endif /* __ARCH_HAS_MMU__ */
  382. /* Setup signal handlers for the initial thread.
  383. Since signal handlers are shared between threads, these settings
  384. will be inherited by all other threads. */
  385. sa.sa_handler = pthread_handle_sigrestart;
  386. sigemptyset(&sa.sa_mask);
  387. sa.sa_flags = 0;
  388. __libc_sigaction(__pthread_sig_restart, &sa, NULL);
  389. sa.sa_handler = pthread_handle_sigcancel;
  390. sigaddset(&sa.sa_mask, __pthread_sig_restart);
  391. // sa.sa_flags = 0;
  392. __libc_sigaction(__pthread_sig_cancel, &sa, NULL);
  393. if (__pthread_sig_debug > 0) {
  394. sa.sa_handler = pthread_handle_sigdebug;
  395. sigemptyset(&sa.sa_mask);
  396. // sa.sa_flags = 0;
  397. __libc_sigaction(__pthread_sig_debug, &sa, NULL);
  398. }
  399. /* Initially, block __pthread_sig_restart. Will be unblocked on demand. */
  400. sigemptyset(&mask);
  401. sigaddset(&mask, __pthread_sig_restart);
  402. sigprocmask(SIG_BLOCK, &mask, NULL);
  403. /* And unblock __pthread_sig_cancel if it has been blocked. */
  404. sigdelset(&mask, __pthread_sig_restart);
  405. sigaddset(&mask, __pthread_sig_cancel);
  406. sigprocmask(SIG_UNBLOCK, &mask, NULL);
  407. /* Register an exit function to kill all other threads. */
  408. /* Do it early so that user-registered atexit functions are called
  409. before pthread_onexit_process. */
  410. on_exit(pthread_onexit_process, NULL);
  411. }
  412. void __pthread_initialize(void);
  413. void __pthread_initialize(void)
  414. {
  415. pthread_initialize();
  416. }
  417. int __pthread_initialize_manager(void)
  418. {
  419. int manager_pipe[2];
  420. int pid;
  421. int report_events;
  422. struct pthread_request request;
  423. *__libc_multiple_threads_ptr = 1;
  424. /* If basic initialization not done yet (e.g. we're called from a
  425. constructor run before our constructor), do it now */
  426. if (__pthread_initial_thread_bos == NULL) pthread_initialize();
  427. /* Setup stack for thread manager */
  428. __pthread_manager_thread_bos = malloc(THREAD_MANAGER_STACK_SIZE);
  429. if (__pthread_manager_thread_bos == NULL) return -1;
  430. __pthread_manager_thread_tos =
  431. __pthread_manager_thread_bos + THREAD_MANAGER_STACK_SIZE;
  432. /* On non-MMU systems we make sure that the initial thread bounds don't overlap
  433. * with the manager stack frame */
  434. NOMMU_INITIAL_THREAD_BOUNDS(__pthread_manager_thread_tos,__pthread_manager_thread_bos);
  435. PDEBUG("manager stack: size=%d, bos=%p, tos=%p\n", THREAD_MANAGER_STACK_SIZE,
  436. __pthread_manager_thread_bos, __pthread_manager_thread_tos);
  437. #if 0
  438. PDEBUG("initial stack: estimate bos=%p, tos=%p\n",
  439. __pthread_initial_thread_bos, __pthread_initial_thread_tos);
  440. #endif
  441. /* Setup pipe to communicate with thread manager */
  442. if (pipe(manager_pipe) == -1) {
  443. free(__pthread_manager_thread_bos);
  444. return -1;
  445. }
  446. /* Start the thread manager */
  447. pid = 0;
  448. #ifdef USE_TLS
  449. if (__linuxthreads_initial_report_events != 0)
  450. THREAD_SETMEM (((pthread_descr) NULL), p_report_events,
  451. __linuxthreads_initial_report_events);
  452. report_events = THREAD_GETMEM (((pthread_descr) NULL), p_report_events);
  453. #else
  454. if (__linuxthreads_initial_report_events != 0)
  455. __pthread_initial_thread.p_report_events
  456. = __linuxthreads_initial_report_events;
  457. report_events = __pthread_initial_thread.p_report_events;
  458. #endif
  459. if (__builtin_expect (report_events, 0))
  460. {
  461. /* It's a bit more complicated. We have to report the creation of
  462. the manager thread. */
  463. int idx = __td_eventword (TD_CREATE);
  464. uint32_t mask = __td_eventmask (TD_CREATE);
  465. if ((mask & (__pthread_threads_events.event_bits[idx]
  466. | __pthread_initial_thread.p_eventbuf.eventmask.event_bits[idx]))
  467. != 0)
  468. {
  469. __pthread_lock(__pthread_manager_thread.p_lock, NULL);
  470. pid = clone(__pthread_manager_event,
  471. (void **) __pthread_manager_thread_tos,
  472. CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
  473. (void *)(long)manager_pipe[0]);
  474. if (pid != -1)
  475. {
  476. /* Now fill in the information about the new thread in
  477. the newly created thread's data structure. We cannot let
  478. the new thread do this since we don't know whether it was
  479. already scheduled when we send the event. */
  480. __pthread_manager_thread.p_eventbuf.eventdata =
  481. &__pthread_manager_thread;
  482. __pthread_manager_thread.p_eventbuf.eventnum = TD_CREATE;
  483. __pthread_last_event = &__pthread_manager_thread;
  484. __pthread_manager_thread.p_tid = 2* PTHREAD_THREADS_MAX + 1;
  485. __pthread_manager_thread.p_pid = pid;
  486. /* Now call the function which signals the event. */
  487. __linuxthreads_create_event ();
  488. }
  489. /* Now restart the thread. */
  490. __pthread_unlock(__pthread_manager_thread.p_lock);
  491. }
  492. }
  493. if (pid == 0) {
  494. pid = clone(__pthread_manager, (void **) __pthread_manager_thread_tos,
  495. CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
  496. (void *)(long)manager_pipe[0]);
  497. }
  498. if (pid == -1) {
  499. free(__pthread_manager_thread_bos);
  500. __libc_close(manager_pipe[0]);
  501. __libc_close(manager_pipe[1]);
  502. return -1;
  503. }
  504. __pthread_manager_request = manager_pipe[1]; /* writing end */
  505. __pthread_manager_reader = manager_pipe[0]; /* reading end */
  506. __pthread_manager_thread.p_tid = 2* PTHREAD_THREADS_MAX + 1;
  507. __pthread_manager_thread.p_pid = pid;
  508. /* Make gdb aware of new thread manager */
  509. if (__pthread_threads_debug && __pthread_sig_debug > 0)
  510. {
  511. raise(__pthread_sig_debug);
  512. /* We suspend ourself and gdb will wake us up when it is
  513. ready to handle us. */
  514. __pthread_wait_for_restart_signal(thread_self());
  515. }
  516. /* Synchronize debugging of the thread manager */
  517. PDEBUG("send REQ_DEBUG to manager thread\n");
  518. request.req_kind = REQ_DEBUG;
  519. TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
  520. (char *) &request, sizeof(request)));
  521. return 0;
  522. }
  523. /* Thread creation */
  524. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
  525. void * (*start_routine)(void *), void *arg)
  526. {
  527. pthread_descr self = thread_self();
  528. struct pthread_request request;
  529. if (__pthread_manager_request < 0) {
  530. if (__pthread_initialize_manager() < 0) return EAGAIN;
  531. }
  532. request.req_thread = self;
  533. request.req_kind = REQ_CREATE;
  534. request.req_args.create.attr = attr;
  535. request.req_args.create.fn = start_routine;
  536. request.req_args.create.arg = arg;
  537. sigprocmask(SIG_SETMASK, (const sigset_t *) NULL,
  538. &request.req_args.create.mask);
  539. PDEBUG("write REQ_CREATE to manager thread\n");
  540. TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
  541. (char *) &request, sizeof(request)));
  542. PDEBUG("before suspend(self)\n");
  543. suspend(self);
  544. PDEBUG("after suspend(self)\n");
  545. if (THREAD_GETMEM(self, p_retcode) == 0)
  546. *thread = (pthread_t) THREAD_GETMEM(self, p_retval);
  547. return THREAD_GETMEM(self, p_retcode);
  548. }
  549. /* Simple operations on thread identifiers */
  550. pthread_t pthread_self(void)
  551. {
  552. pthread_descr self = thread_self();
  553. return THREAD_GETMEM(self, p_tid);
  554. }
  555. int pthread_equal(pthread_t thread1, pthread_t thread2)
  556. {
  557. return thread1 == thread2;
  558. }
  559. /* Helper function for thread_self in the case of user-provided stacks */
  560. #ifndef THREAD_SELF
  561. pthread_descr __pthread_find_self()
  562. {
  563. char * sp = CURRENT_STACK_FRAME;
  564. pthread_handle h;
  565. /* __pthread_handles[0] is the initial thread, __pthread_handles[1] is
  566. the manager threads handled specially in thread_self(), so start at 2 */
  567. h = __pthread_handles + 2;
  568. while (! (sp <= (char *) h->h_descr && sp >= h->h_bottom)) h++;
  569. #ifdef DEBUG_PT
  570. if (h->h_descr == NULL) {
  571. printf("*** %s ERROR descriptor is NULL!!!!! ***\n\n", __FUNCTION__);
  572. _exit(1);
  573. }
  574. #endif
  575. return h->h_descr;
  576. }
  577. #else
  578. static pthread_descr thread_self_stack(void)
  579. {
  580. char *sp = CURRENT_STACK_FRAME;
  581. pthread_handle h;
  582. if (sp >= __pthread_manager_thread_bos && sp < __pthread_manager_thread_tos)
  583. return manager_thread;
  584. h = __pthread_handles + 2;
  585. # ifdef USE_TLS
  586. while (h->h_descr == NULL
  587. || ! (sp <= (char *) h->h_descr->p_stackaddr && sp >= h->h_bottom))
  588. h++;
  589. # else
  590. while (! (sp <= (char *) h->h_descr && sp >= h->h_bottom))
  591. h++;
  592. # endif
  593. return h->h_descr;
  594. }
  595. #endif
  596. /* Thread scheduling */
  597. int pthread_setschedparam(pthread_t thread, int policy,
  598. const struct sched_param *param)
  599. {
  600. pthread_handle handle = thread_handle(thread);
  601. pthread_descr th;
  602. __pthread_lock(&handle->h_lock, NULL);
  603. if (invalid_handle(handle, thread)) {
  604. __pthread_unlock(&handle->h_lock);
  605. return ESRCH;
  606. }
  607. th = handle->h_descr;
  608. if (sched_setscheduler(th->p_pid, policy, param) == -1) {
  609. __pthread_unlock(&handle->h_lock);
  610. return errno;
  611. }
  612. th->p_priority = policy == SCHED_OTHER ? 0 : param->sched_priority;
  613. __pthread_unlock(&handle->h_lock);
  614. if (__pthread_manager_request >= 0)
  615. __pthread_manager_adjust_prio(th->p_priority);
  616. return 0;
  617. }
  618. int pthread_getschedparam(pthread_t thread, int *policy,
  619. struct sched_param *param)
  620. {
  621. pthread_handle handle = thread_handle(thread);
  622. int pid, pol;
  623. __pthread_lock(&handle->h_lock, NULL);
  624. if (invalid_handle(handle, thread)) {
  625. __pthread_unlock(&handle->h_lock);
  626. return ESRCH;
  627. }
  628. pid = handle->h_descr->p_pid;
  629. __pthread_unlock(&handle->h_lock);
  630. pol = sched_getscheduler(pid);
  631. if (pol == -1) return errno;
  632. if (sched_getparam(pid, param) == -1) return errno;
  633. *policy = pol;
  634. return 0;
  635. }
  636. /* Process-wide exit() request */
  637. static void pthread_onexit_process(int retcode, void *arg attribute_unused)
  638. {
  639. struct pthread_request request;
  640. pthread_descr self = thread_self();
  641. if (__pthread_manager_request >= 0) {
  642. request.req_thread = self;
  643. request.req_kind = REQ_PROCESS_EXIT;
  644. request.req_args.exit.code = retcode;
  645. TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
  646. (char *) &request, sizeof(request)));
  647. suspend(self);
  648. /* Main thread should accumulate times for thread manager and its
  649. children, so that timings for main thread account for all threads. */
  650. if (self == __pthread_main_thread) {
  651. waitpid(__pthread_manager_thread.p_pid, NULL, __WCLONE);
  652. /* Since all threads have been asynchronously terminated
  653. * (possibly holding locks), free cannot be used any more. */
  654. __pthread_manager_thread_bos = __pthread_manager_thread_tos = NULL;
  655. }
  656. }
  657. }
  658. /* The handler for the RESTART signal just records the signal received
  659. in the thread descriptor, and optionally performs a siglongjmp
  660. (for pthread_cond_timedwait). */
  661. static void pthread_handle_sigrestart(int sig)
  662. {
  663. pthread_descr self = thread_self();
  664. THREAD_SETMEM(self, p_signal, sig);
  665. if (THREAD_GETMEM(self, p_signal_jmp) != NULL)
  666. siglongjmp(*THREAD_GETMEM(self, p_signal_jmp), 1);
  667. }
  668. /* The handler for the CANCEL signal checks for cancellation
  669. (in asynchronous mode), for process-wide exit and exec requests.
  670. For the thread manager thread, redirect the signal to
  671. __pthread_manager_sighandler. */
  672. static void pthread_handle_sigcancel(int sig)
  673. {
  674. pthread_descr self = thread_self();
  675. sigjmp_buf * jmpbuf;
  676. if (self == &__pthread_manager_thread)
  677. {
  678. #ifdef THREAD_SELF
  679. /* A new thread might get a cancel signal before it is fully
  680. initialized, so that the thread register might still point to the
  681. manager thread. Double check that this is really the manager
  682. thread. */
  683. pthread_descr real_self = thread_self_stack();
  684. if (real_self == &__pthread_manager_thread)
  685. {
  686. __pthread_manager_sighandler(sig);
  687. return;
  688. }
  689. /* Oops, thread_self() isn't working yet.. */
  690. self = real_self;
  691. # ifdef INIT_THREAD_SELF
  692. INIT_THREAD_SELF(self, self->p_nr);
  693. # endif
  694. #else
  695. __pthread_manager_sighandler(sig);
  696. return;
  697. #endif
  698. }
  699. if (__builtin_expect (__pthread_exit_requested, 0)) {
  700. /* Main thread should accumulate times for thread manager and its
  701. children, so that timings for main thread account for all threads. */
  702. if (self == __pthread_main_thread) {
  703. #ifdef USE_TLS
  704. waitpid(__pthread_manager_thread->p_pid, NULL, __WCLONE);
  705. #else
  706. waitpid(__pthread_manager_thread.p_pid, NULL, __WCLONE);
  707. #endif
  708. }
  709. _exit(__pthread_exit_code);
  710. }
  711. if (__builtin_expect (THREAD_GETMEM(self, p_canceled), 0)
  712. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  713. if (THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
  714. pthread_exit(PTHREAD_CANCELED);
  715. jmpbuf = THREAD_GETMEM(self, p_cancel_jmp);
  716. if (jmpbuf != NULL) {
  717. THREAD_SETMEM(self, p_cancel_jmp, NULL);
  718. siglongjmp(*jmpbuf, 1);
  719. }
  720. }
  721. }
  722. /* Handler for the DEBUG signal.
  723. The debugging strategy is as follows:
  724. On reception of a REQ_DEBUG request (sent by new threads created to
  725. the thread manager under debugging mode), the thread manager throws
  726. __pthread_sig_debug to itself. The debugger (if active) intercepts
  727. this signal, takes into account new threads and continue execution
  728. of the thread manager by propagating the signal because it doesn't
  729. know what it is specifically done for. In the current implementation,
  730. the thread manager simply discards it. */
  731. static void pthread_handle_sigdebug(int sig attribute_unused)
  732. {
  733. /* Nothing */
  734. }
  735. /* Reset the state of the thread machinery after a fork().
  736. Close the pipe used for requests and set the main thread to the forked
  737. thread.
  738. Notice that we can't free the stack segments, as the forked thread
  739. may hold pointers into them. */
  740. void __pthread_reset_main_thread()
  741. {
  742. pthread_descr self = thread_self();
  743. if (__pthread_manager_request != -1) {
  744. /* Free the thread manager stack */
  745. free(__pthread_manager_thread_bos);
  746. __pthread_manager_thread_bos = __pthread_manager_thread_tos = NULL;
  747. /* Close the two ends of the pipe */
  748. __libc_close(__pthread_manager_request);
  749. __libc_close(__pthread_manager_reader);
  750. __pthread_manager_request = __pthread_manager_reader = -1;
  751. }
  752. /* Update the pid of the main thread */
  753. THREAD_SETMEM(self, p_pid, getpid());
  754. /* Make the forked thread the main thread */
  755. __pthread_main_thread = self;
  756. THREAD_SETMEM(self, p_nextlive, self);
  757. THREAD_SETMEM(self, p_prevlive, self);
  758. /* Now this thread modifies the global variables. */
  759. THREAD_SETMEM(self, p_errnop, &_errno);
  760. THREAD_SETMEM(self, p_h_errnop, &_h_errno);
  761. }
  762. /* Process-wide exec() request */
  763. void __pthread_kill_other_threads_np(void)
  764. {
  765. struct sigaction sa;
  766. /* Terminate all other threads and thread manager */
  767. pthread_onexit_process(0, NULL);
  768. /* Make current thread the main thread in case the calling thread
  769. changes its mind, does not exec(), and creates new threads instead. */
  770. __pthread_reset_main_thread();
  771. /* Reset the signal handlers behaviour for the signals the
  772. implementation uses since this would be passed to the new
  773. process. */
  774. sigemptyset(&sa.sa_mask);
  775. sa.sa_flags = 0;
  776. sa.sa_handler = SIG_DFL;
  777. __libc_sigaction(__pthread_sig_restart, &sa, NULL);
  778. __libc_sigaction(__pthread_sig_cancel, &sa, NULL);
  779. if (__pthread_sig_debug > 0)
  780. __libc_sigaction(__pthread_sig_debug, &sa, NULL);
  781. }
  782. weak_alias (__pthread_kill_other_threads_np, pthread_kill_other_threads_np)
  783. /* Concurrency symbol level. */
  784. static int current_level;
  785. int __pthread_setconcurrency(int level)
  786. {
  787. /* We don't do anything unless we have found a useful interpretation. */
  788. current_level = level;
  789. return 0;
  790. }
  791. weak_alias (__pthread_setconcurrency, pthread_setconcurrency)
  792. int __pthread_getconcurrency(void)
  793. {
  794. return current_level;
  795. }
  796. weak_alias (__pthread_getconcurrency, pthread_getconcurrency)
  797. /* Primitives for controlling thread execution */
  798. void __pthread_wait_for_restart_signal(pthread_descr self)
  799. {
  800. sigset_t mask;
  801. sigprocmask(SIG_SETMASK, NULL, &mask); /* Get current signal mask */
  802. sigdelset(&mask, __pthread_sig_restart); /* Unblock the restart signal */
  803. THREAD_SETMEM(self, p_signal, 0);
  804. do {
  805. sigsuspend(&mask); /* Wait for signal */
  806. } while (THREAD_GETMEM(self, p_signal) !=__pthread_sig_restart);
  807. READ_MEMORY_BARRIER(); /* See comment in __pthread_restart_new */
  808. }
  809. #ifndef __NR_rt_sigaction
  810. /* The _old variants are for 2.0 and early 2.1 kernels which don't have RT
  811. signals.
  812. On these kernels, we use SIGUSR1 and SIGUSR2 for restart and cancellation.
  813. Since the restart signal does not queue, we use an atomic counter to create
  814. queuing semantics. This is needed to resolve a rare race condition in
  815. pthread_cond_timedwait_relative. */
  816. void __pthread_restart_old(pthread_descr th)
  817. {
  818. if (atomic_increment(&th->p_resume_count) == -1)
  819. kill(th->p_pid, __pthread_sig_restart);
  820. }
  821. void __pthread_suspend_old(pthread_descr self)
  822. {
  823. if (atomic_decrement(&self->p_resume_count) <= 0)
  824. __pthread_wait_for_restart_signal(self);
  825. }
  826. int
  827. __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abstime)
  828. {
  829. sigset_t unblock, initial_mask;
  830. int was_signalled = 0;
  831. sigjmp_buf jmpbuf;
  832. if (atomic_decrement(&self->p_resume_count) == 0) {
  833. /* Set up a longjmp handler for the restart signal, unblock
  834. the signal and sleep. */
  835. if (sigsetjmp(jmpbuf, 1) == 0) {
  836. THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
  837. THREAD_SETMEM(self, p_signal, 0);
  838. /* Unblock the restart signal */
  839. sigemptyset(&unblock);
  840. sigaddset(&unblock, __pthread_sig_restart);
  841. sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
  842. while (1) {
  843. struct timeval now;
  844. struct timespec reltime;
  845. /* Compute a time offset relative to now. */
  846. gettimeofday (&now, NULL);
  847. reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
  848. reltime.tv_sec = abstime->tv_sec - now.tv_sec;
  849. if (reltime.tv_nsec < 0) {
  850. reltime.tv_nsec += 1000000000;
  851. reltime.tv_sec -= 1;
  852. }
  853. /* Sleep for the required duration. If woken by a signal,
  854. resume waiting as required by Single Unix Specification. */
  855. if (reltime.tv_sec < 0 || __libc_nanosleep(&reltime, NULL) == 0)
  856. break;
  857. }
  858. /* Block the restart signal again */
  859. sigprocmask(SIG_SETMASK, &initial_mask, NULL);
  860. was_signalled = 0;
  861. } else {
  862. was_signalled = 1;
  863. }
  864. THREAD_SETMEM(self, p_signal_jmp, NULL);
  865. }
  866. /* Now was_signalled is true if we exited the above code
  867. due to the delivery of a restart signal. In that case,
  868. we know we have been dequeued and resumed and that the
  869. resume count is balanced. Otherwise, there are some
  870. cases to consider. First, try to bump up the resume count
  871. back to zero. If it goes to 1, it means restart() was
  872. invoked on this thread. The signal must be consumed
  873. and the count bumped down and everything is cool. We
  874. can return a 1 to the caller.
  875. Otherwise, no restart was delivered yet, so a potential
  876. race exists; we return a 0 to the caller which must deal
  877. with this race in an appropriate way; for example by
  878. atomically removing the thread from consideration for a
  879. wakeup---if such a thing fails, it means a restart is
  880. being delivered. */
  881. if (!was_signalled) {
  882. if (atomic_increment(&self->p_resume_count) != -1) {
  883. __pthread_wait_for_restart_signal(self);
  884. atomic_decrement(&self->p_resume_count); /* should be zero now! */
  885. /* woke spontaneously and consumed restart signal */
  886. return 1;
  887. }
  888. /* woke spontaneously but did not consume restart---caller must resolve */
  889. return 0;
  890. }
  891. /* woken due to restart signal */
  892. return 1;
  893. }
  894. #endif /* __NR_rt_sigaction */
  895. #ifdef __NR_rt_sigaction
  896. void __pthread_restart_new(pthread_descr th)
  897. {
  898. /* The barrier is proabably not needed, in which case it still documents
  899. our assumptions. The intent is to commit previous writes to shared
  900. memory so the woken thread will have a consistent view. Complementary
  901. read barriers are present to the suspend functions. */
  902. WRITE_MEMORY_BARRIER();
  903. kill(th->p_pid, __pthread_sig_restart);
  904. }
  905. int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime)
  906. {
  907. sigset_t unblock, initial_mask;
  908. int was_signalled = 0;
  909. sigjmp_buf jmpbuf;
  910. if (sigsetjmp(jmpbuf, 1) == 0) {
  911. THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
  912. THREAD_SETMEM(self, p_signal, 0);
  913. /* Unblock the restart signal */
  914. sigemptyset(&unblock);
  915. sigaddset(&unblock, __pthread_sig_restart);
  916. sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
  917. while (1) {
  918. struct timeval now;
  919. struct timespec reltime;
  920. /* Compute a time offset relative to now. */
  921. gettimeofday (&now, NULL);
  922. reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
  923. reltime.tv_sec = abstime->tv_sec - now.tv_sec;
  924. if (reltime.tv_nsec < 0) {
  925. reltime.tv_nsec += 1000000000;
  926. reltime.tv_sec -= 1;
  927. }
  928. /* Sleep for the required duration. If woken by a signal,
  929. resume waiting as required by Single Unix Specification. */
  930. if (reltime.tv_sec < 0 || __libc_nanosleep(&reltime, NULL) == 0)
  931. break;
  932. }
  933. /* Block the restart signal again */
  934. sigprocmask(SIG_SETMASK, &initial_mask, NULL);
  935. was_signalled = 0;
  936. } else {
  937. was_signalled = 1;
  938. }
  939. THREAD_SETMEM(self, p_signal_jmp, NULL);
  940. /* Now was_signalled is true if we exited the above code
  941. due to the delivery of a restart signal. In that case,
  942. everything is cool. We have been removed from whatever
  943. we were waiting on by the other thread, and consumed its signal.
  944. Otherwise we this thread woke up spontaneously, or due to a signal other
  945. than restart. This is an ambiguous case that must be resolved by
  946. the caller; the thread is still eligible for a restart wakeup
  947. so there is a race. */
  948. READ_MEMORY_BARRIER(); /* See comment in __pthread_restart_new */
  949. return was_signalled;
  950. }
  951. #endif
  952. /* Debugging aid */
  953. #ifdef DEBUG_PT
  954. #include <stdarg.h>
  955. void __pthread_message(char * fmt, ...)
  956. {
  957. char buffer[1024];
  958. va_list args;
  959. sprintf(buffer, "%05d : ", getpid());
  960. va_start(args, fmt);
  961. vsnprintf(buffer + 8, sizeof(buffer) - 8, fmt, args);
  962. va_end(args);
  963. TEMP_FAILURE_RETRY(__libc_write(2, buffer, strlen(buffer)));
  964. }
  965. #endif
  966. #ifndef __PIC__
  967. /* We need a hook to force the cancelation wrappers to be linked in when
  968. static libpthread is used. */
  969. extern const int __pthread_provide_wrappers;
  970. static const int *const __pthread_require_wrappers =
  971. &__pthread_provide_wrappers;
  972. #endif