pthread.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. #define __USE_GNU
  18. #include <errno.h>
  19. #include <netdb.h> /* for h_errno */
  20. #include <stddef.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <sys/wait.h>
  27. #include <sys/resource.h>
  28. #include "pthread.h"
  29. #include "internals.h"
  30. #include "spinlock.h"
  31. #include "restart.h"
  32. #include "debug.h" /* added to linuxthreads -StS */
  33. /* Mods for uClibc: Some includes */
  34. #include <signal.h>
  35. #include <sys/types.h>
  36. #include <sys/syscall.h>
  37. /* mods for uClibc: getpwd and getpagesize are the syscalls */
  38. #define __getpid getpid
  39. #define __getpagesize getpagesize
  40. /* mods for uClibc: __libc_sigaction is not in any standard headers */
  41. extern int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact);
  42. /* These variables are used by the setup code. */
  43. extern int _errno;
  44. extern int _h_errno;
  45. /* Descriptor of the initial thread */
  46. struct _pthread_descr_struct __pthread_initial_thread = {
  47. &__pthread_initial_thread, /* pthread_descr p_nextlive */
  48. &__pthread_initial_thread, /* pthread_descr p_prevlive */
  49. NULL, /* pthread_descr p_nextwaiting */
  50. NULL, /* pthread_descr p_nextlock */
  51. PTHREAD_THREADS_MAX, /* pthread_t p_tid */
  52. 0, /* int p_pid */
  53. 0, /* int p_priority */
  54. &__pthread_handles[0].h_lock, /* struct _pthread_fastlock * p_lock */
  55. 0, /* int p_signal */
  56. NULL, /* sigjmp_buf * p_signal_buf */
  57. NULL, /* sigjmp_buf * p_cancel_buf */
  58. 0, /* char p_terminated */
  59. 0, /* char p_detached */
  60. 0, /* char p_exited */
  61. NULL, /* void * p_retval */
  62. 0, /* int p_retval */
  63. NULL, /* pthread_descr p_joining */
  64. NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
  65. 0, /* char p_cancelstate */
  66. 0, /* char p_canceltype */
  67. 0, /* char p_canceled */
  68. &_errno, /* int *p_errnop */
  69. 0, /* int p_errno */
  70. &_h_errno, /* int *p_h_errnop */
  71. 0, /* int p_h_errno */
  72. NULL, /* char * p_in_sighandler */
  73. 0, /* char p_sigwaiting */
  74. PTHREAD_START_ARGS_INITIALIZER, /* struct pthread_start_args p_start_args */
  75. {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
  76. {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
  77. 0, /* int p_userstack */
  78. NULL, /* void * p_guardaddr */
  79. 0, /* size_t p_guardsize */
  80. &__pthread_initial_thread, /* pthread_descr p_self */
  81. 0, /* Always index 0 */
  82. 0, /* int p_report_events */
  83. {{{0, }}, 0, NULL}, /* td_eventbuf_t p_eventbuf */
  84. __ATOMIC_INITIALIZER, /* struct pthread_atomic p_resume_count */
  85. 0, /* char p_woken_by_cancel */
  86. 0, /* char p_condvar_avail */
  87. 0, /* char p_sem_avail */
  88. NULL, /* struct pthread_extricate_if *p_extricate */
  89. NULL, /* pthread_readlock_info *p_readlock_list; */
  90. NULL, /* pthread_readlock_info *p_readlock_free; */
  91. 0 /* int p_untracked_readlock_count; */
  92. #ifdef __UCLIBC_HAS_XLOCALE__
  93. ,
  94. &__global_locale_data, /* __locale_t locale; */
  95. #endif /* __UCLIBC_HAS_XLOCALE__ */
  96. };
  97. /* Descriptor of the manager thread; none of this is used but the error
  98. variables, the p_pid and p_priority fields,
  99. and the address for identification. */
  100. #define manager_thread (&__pthread_manager_thread)
  101. struct _pthread_descr_struct __pthread_manager_thread = {
  102. NULL, /* pthread_descr p_nextlive */
  103. NULL, /* pthread_descr p_prevlive */
  104. NULL, /* pthread_descr p_nextwaiting */
  105. NULL, /* pthread_descr p_nextlock */
  106. 0, /* int p_tid */
  107. 0, /* int p_pid */
  108. 0, /* int p_priority */
  109. &__pthread_handles[1].h_lock, /* struct _pthread_fastlock * p_lock */
  110. 0, /* int p_signal */
  111. NULL, /* sigjmp_buf * p_signal_buf */
  112. NULL, /* sigjmp_buf * p_cancel_buf */
  113. 0, /* char p_terminated */
  114. 0, /* char p_detached */
  115. 0, /* char p_exited */
  116. NULL, /* void * p_retval */
  117. 0, /* int p_retval */
  118. NULL, /* pthread_descr p_joining */
  119. NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
  120. 0, /* char p_cancelstate */
  121. 0, /* char p_canceltype */
  122. 0, /* char p_canceled */
  123. &__pthread_manager_thread.p_errno, /* int *p_errnop */
  124. 0, /* int p_errno */
  125. NULL, /* int *p_h_errnop */
  126. 0, /* int p_h_errno */
  127. NULL, /* char * p_in_sighandler */
  128. 0, /* char p_sigwaiting */
  129. PTHREAD_START_ARGS_INITIALIZER, /* struct pthread_start_args p_start_args */
  130. {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
  131. {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
  132. 0, /* int p_userstack */
  133. NULL, /* void * p_guardaddr */
  134. 0, /* size_t p_guardsize */
  135. &__pthread_manager_thread, /* pthread_descr p_self */
  136. 1, /* Always index 1 */
  137. 0, /* int p_report_events */
  138. {{{0, }}, 0, NULL}, /* td_eventbuf_t p_eventbuf */
  139. __ATOMIC_INITIALIZER, /* struct pthread_atomic p_resume_count */
  140. 0, /* char p_woken_by_cancel */
  141. 0, /* char p_condvar_avail */
  142. 0, /* char p_sem_avail */
  143. NULL, /* struct pthread_extricate_if *p_extricate */
  144. NULL, /* pthread_readlock_info *p_readlock_list; */
  145. NULL, /* pthread_readlock_info *p_readlock_free; */
  146. 0 /* int p_untracked_readlock_count; */
  147. #ifdef __UCLIBC_HAS_XLOCALE__
  148. ,
  149. &__global_locale_data, /* __locale_t locale; */
  150. #endif /* __UCLIBC_HAS_XLOCALE__ */
  151. };
  152. /* Pointer to the main thread (the father of the thread manager thread) */
  153. /* Originally, this is the initial thread, but this changes after fork() */
  154. pthread_descr __pthread_main_thread = &__pthread_initial_thread;
  155. /* Limit between the stack of the initial thread (above) and the
  156. stacks of other threads (below). Aligned on a STACK_SIZE boundary. */
  157. char *__pthread_initial_thread_bos = NULL;
  158. /* For non-MMU systems also remember to stack top of the initial thread.
  159. * This is adapted when other stacks are malloc'ed since we don't know
  160. * the bounds a-priori. -StS */
  161. #ifndef __ARCH_HAS_MMU__
  162. char *__pthread_initial_thread_tos = NULL;
  163. #endif /* __ARCH_HAS_MMU__ */
  164. /* File descriptor for sending requests to the thread manager. */
  165. /* Initially -1, meaning that the thread manager is not running. */
  166. int __pthread_manager_request = -1;
  167. /* Other end of the pipe for sending requests to the thread manager. */
  168. int __pthread_manager_reader;
  169. /* Limits of the thread manager stack */
  170. char *__pthread_manager_thread_bos = NULL;
  171. char *__pthread_manager_thread_tos = NULL;
  172. /* For process-wide exit() */
  173. int __pthread_exit_requested = 0;
  174. int __pthread_exit_code = 0;
  175. /* Communicate relevant LinuxThreads constants to gdb */
  176. const int __pthread_threads_max = PTHREAD_THREADS_MAX;
  177. const int __pthread_sizeof_handle = sizeof(struct pthread_handle_struct);
  178. const int __pthread_offsetof_descr = offsetof(struct pthread_handle_struct, h_descr);
  179. const int __pthread_offsetof_pid = offsetof(struct _pthread_descr_struct,
  180. p_pid);
  181. const int __linuxthreads_pthread_sizeof_descr
  182. = sizeof(struct _pthread_descr_struct);
  183. const int __linuxthreads_initial_report_events;
  184. const char __linuxthreads_version[] = VERSION;
  185. /* Forward declarations */
  186. static void pthread_onexit_process(int retcode, void *arg);
  187. static void pthread_handle_sigcancel(int sig);
  188. static void pthread_handle_sigrestart(int sig);
  189. static void pthread_handle_sigdebug(int sig);
  190. int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime);
  191. /* Signal numbers used for the communication.
  192. In these variables we keep track of the used variables. If the
  193. platform does not support any real-time signals we will define the
  194. values to some unreasonable value which will signal failing of all
  195. the functions below. */
  196. #ifndef __NR_rt_sigaction
  197. static int current_rtmin = -1;
  198. static int current_rtmax = -1;
  199. int __pthread_sig_restart = SIGUSR1;
  200. int __pthread_sig_cancel = SIGUSR2;
  201. int __pthread_sig_debug;
  202. #else
  203. #if __SIGRTMAX - __SIGRTMIN >= 3
  204. static int current_rtmin = __SIGRTMIN + 3;
  205. static int current_rtmax = __SIGRTMAX;
  206. int __pthread_sig_restart = __SIGRTMIN;
  207. int __pthread_sig_cancel = __SIGRTMIN + 1;
  208. int __pthread_sig_debug = __SIGRTMIN + 2;
  209. void (*__pthread_restart)(pthread_descr) = __pthread_restart_new;
  210. void (*__pthread_suspend)(pthread_descr) = __pthread_wait_for_restart_signal;
  211. int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *) = __pthread_timedsuspend_new;
  212. #else
  213. static int current_rtmin = __SIGRTMIN;
  214. static int current_rtmax = __SIGRTMAX;
  215. int __pthread_sig_restart = SIGUSR1;
  216. int __pthread_sig_cancel = SIGUSR2;
  217. int __pthread_sig_debug;
  218. void (*__pthread_restart)(pthread_descr) = __pthread_restart_old;
  219. void (*__pthread_suspend)(pthread_descr) = __pthread_suspend_old;
  220. int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *) = __pthread_timedsuspend_old;
  221. #endif
  222. /* Return number of available real-time signal with highest priority. */
  223. int __libc_current_sigrtmin (void)
  224. {
  225. return current_rtmin;
  226. }
  227. /* Return number of available real-time signal with lowest priority. */
  228. int __libc_current_sigrtmax (void)
  229. {
  230. return current_rtmax;
  231. }
  232. /* Allocate real-time signal with highest/lowest available
  233. priority. Please note that we don't use a lock since we assume
  234. this function to be called at program start. */
  235. int __libc_allocate_rtsig (int high)
  236. {
  237. if (current_rtmin == -1 || current_rtmin > current_rtmax)
  238. /* We don't have anymore signal available. */
  239. return -1;
  240. return high ? current_rtmin++ : current_rtmax--;
  241. }
  242. #endif
  243. /* Initialize the pthread library.
  244. Initialization is split in two functions:
  245. - a constructor function that blocks the __pthread_sig_restart signal
  246. (must do this very early, since the program could capture the signal
  247. mask with e.g. sigsetjmp before creating the first thread);
  248. - a regular function called from pthread_create when needed. */
  249. static void pthread_initialize(void) __attribute__((constructor));
  250. struct pthread_functions __pthread_functions =
  251. {
  252. #if !(USE_TLS && HAVE___THREAD)
  253. .ptr_pthread_internal_tsd_set = __pthread_internal_tsd_set,
  254. .ptr_pthread_internal_tsd_get = __pthread_internal_tsd_get,
  255. .ptr_pthread_internal_tsd_address = __pthread_internal_tsd_address,
  256. #endif
  257. /*
  258. .ptr_pthread_fork = __pthread_fork,
  259. */
  260. .ptr_pthread_attr_destroy = pthread_attr_destroy,
  261. .ptr_pthread_attr_init = pthread_attr_init,
  262. .ptr_pthread_attr_getdetachstate = pthread_attr_getdetachstate,
  263. .ptr_pthread_attr_setdetachstate = pthread_attr_setdetachstate,
  264. .ptr_pthread_attr_getinheritsched = pthread_attr_getinheritsched,
  265. .ptr_pthread_attr_setinheritsched = pthread_attr_setinheritsched,
  266. .ptr_pthread_attr_getschedparam = pthread_attr_getschedparam,
  267. .ptr_pthread_attr_setschedparam = pthread_attr_setschedparam,
  268. .ptr_pthread_attr_getschedpolicy = pthread_attr_getschedpolicy,
  269. .ptr_pthread_attr_setschedpolicy = pthread_attr_setschedpolicy,
  270. .ptr_pthread_attr_getscope = pthread_attr_getscope,
  271. .ptr_pthread_attr_setscope = pthread_attr_setscope,
  272. .ptr_pthread_condattr_destroy = pthread_condattr_destroy,
  273. .ptr_pthread_condattr_init = pthread_condattr_init,
  274. .ptr_pthread_cond_broadcast = pthread_cond_broadcast,
  275. .ptr_pthread_cond_destroy = pthread_cond_destroy,
  276. .ptr_pthread_cond_init = pthread_cond_init,
  277. .ptr_pthread_cond_signal = pthread_cond_signal,
  278. .ptr_pthread_cond_wait = pthread_cond_wait,
  279. .ptr_pthread_cond_timedwait = pthread_cond_timedwait,
  280. .ptr_pthread_equal = pthread_equal,
  281. .ptr___pthread_exit = pthread_exit,
  282. .ptr_pthread_getschedparam = pthread_getschedparam,
  283. .ptr_pthread_setschedparam = pthread_setschedparam,
  284. .ptr_pthread_mutex_destroy = pthread_mutex_destroy,
  285. .ptr_pthread_mutex_init = pthread_mutex_init,
  286. .ptr_pthread_mutex_lock = pthread_mutex_lock,
  287. .ptr_pthread_mutex_trylock = pthread_mutex_trylock,
  288. .ptr_pthread_mutex_unlock = pthread_mutex_unlock,
  289. .ptr_pthread_self = pthread_self,
  290. .ptr_pthread_setcancelstate = pthread_setcancelstate,
  291. .ptr_pthread_setcanceltype = pthread_setcanceltype,
  292. /*
  293. .ptr_pthread_do_exit = pthread_do_exit,
  294. .ptr_pthread_thread_self = pthread_thread_self,
  295. .ptr_pthread_cleanup_upto = pthread_cleanup_upto,
  296. .ptr_pthread_sigaction = pthread_sigaction,
  297. .ptr_pthread_sigwait = pthread_sigwait,
  298. .ptr_pthread_raise = pthread_raise,
  299. .ptr__pthread_cleanup_push = _pthread_cleanup_push,
  300. .ptr__pthread_cleanup_pop = _pthread_cleanup_pop
  301. */
  302. };
  303. #ifdef SHARED
  304. # define ptr_pthread_functions &__pthread_functions
  305. #else
  306. # define ptr_pthread_functions NULL
  307. #endif
  308. static int *__libc_multiple_threads_ptr;
  309. /* Do some minimal initialization which has to be done during the
  310. startup of the C library. */
  311. void __pthread_initialize_minimal(void)
  312. {
  313. /* If we have special thread_self processing, initialize
  314. * that for the main thread now. */
  315. #ifdef INIT_THREAD_SELF
  316. INIT_THREAD_SELF(&__pthread_initial_thread, 0);
  317. #endif
  318. __libc_multiple_threads_ptr = __libc_pthread_init (ptr_pthread_functions);
  319. }
  320. static void pthread_initialize(void)
  321. {
  322. struct sigaction sa;
  323. sigset_t mask;
  324. struct rlimit limit;
  325. rlim_t max_stack;
  326. /* If already done (e.g. by a constructor called earlier!), bail out */
  327. if (__pthread_initial_thread_bos != NULL) return;
  328. #ifdef TEST_FOR_COMPARE_AND_SWAP
  329. /* Test if compare-and-swap is available */
  330. __pthread_has_cas = compare_and_swap_is_available();
  331. #endif
  332. /* For the initial stack, reserve at least STACK_SIZE bytes of stack
  333. below the current stack address, and align that on a
  334. STACK_SIZE boundary. */
  335. __pthread_initial_thread_bos =
  336. (char *)(((long)CURRENT_STACK_FRAME - 2 * STACK_SIZE) & ~(STACK_SIZE - 1));
  337. /* Update the descriptor for the initial thread. */
  338. __pthread_initial_thread.p_pid = __getpid();
  339. /* If we have special thread_self processing, initialize that for the
  340. main thread now. */
  341. #ifdef INIT_THREAD_SELF
  342. INIT_THREAD_SELF(&__pthread_initial_thread, 0);
  343. #endif
  344. /* The errno/h_errno variable of the main thread are the global ones. */
  345. __pthread_initial_thread.p_errnop = &_errno;
  346. __pthread_initial_thread.p_h_errnop = &_h_errno;
  347. #ifdef __UCLIBC_HAS_XLOCALE__
  348. /* The locale of the main thread is the current locale in use. */
  349. __pthread_initial_thread.locale = __curlocale_var;
  350. #endif /* __UCLIBC_HAS_XLOCALE__ */
  351. { /* uClibc-specific stdio initialization for threads. */
  352. FILE *fp;
  353. _stdio_user_locking = 0; /* 2 if threading not initialized */
  354. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
  355. if (fp->__user_locking != 1) {
  356. fp->__user_locking = 0;
  357. }
  358. }
  359. }
  360. /* Play with the stack size limit to make sure that no stack ever grows
  361. beyond STACK_SIZE minus two pages (one page for the thread descriptor
  362. immediately beyond, and one page to act as a guard page). */
  363. #ifdef __ARCH_HAS_MMU__
  364. /* We cannot allocate a huge chunk of memory to mmap all thread stacks later
  365. * on a non-MMU system. Thus, we don't need the rlimit either. -StS */
  366. getrlimit(RLIMIT_STACK, &limit);
  367. max_stack = STACK_SIZE - 2 * __getpagesize();
  368. if (limit.rlim_cur > max_stack) {
  369. limit.rlim_cur = max_stack;
  370. setrlimit(RLIMIT_STACK, &limit);
  371. }
  372. #else
  373. /* For non-MMU assume __pthread_initial_thread_tos at upper page boundary, and
  374. * __pthread_initial_thread_bos at address 0. These bounds are refined as we
  375. * malloc other stack frames such that they don't overlap. -StS
  376. */
  377. __pthread_initial_thread_tos =
  378. (char *)(((long)CURRENT_STACK_FRAME + __getpagesize()) & ~(__getpagesize() - 1));
  379. __pthread_initial_thread_bos = (char *) 1; /* set it non-zero so we know we have been here */
  380. PDEBUG("initial thread stack bounds: bos=%p, tos=%p\n",
  381. __pthread_initial_thread_bos, __pthread_initial_thread_tos);
  382. #endif /* __ARCH_HAS_MMU__ */
  383. /* Setup signal handlers for the initial thread.
  384. Since signal handlers are shared between threads, these settings
  385. will be inherited by all other threads. */
  386. sa.sa_handler = pthread_handle_sigrestart;
  387. sigemptyset(&sa.sa_mask);
  388. sa.sa_flags = 0;
  389. __libc_sigaction(__pthread_sig_restart, &sa, NULL);
  390. sa.sa_handler = pthread_handle_sigcancel;
  391. sigaddset(&sa.sa_mask, __pthread_sig_restart);
  392. // sa.sa_flags = 0;
  393. __libc_sigaction(__pthread_sig_cancel, &sa, NULL);
  394. if (__pthread_sig_debug > 0) {
  395. sa.sa_handler = pthread_handle_sigdebug;
  396. sigemptyset(&sa.sa_mask);
  397. // sa.sa_flags = 0;
  398. __libc_sigaction(__pthread_sig_debug, &sa, NULL);
  399. }
  400. /* Initially, block __pthread_sig_restart. Will be unblocked on demand. */
  401. sigemptyset(&mask);
  402. sigaddset(&mask, __pthread_sig_restart);
  403. sigprocmask(SIG_BLOCK, &mask, NULL);
  404. /* And unblock __pthread_sig_cancel if it has been blocked. */
  405. sigdelset(&mask, __pthread_sig_restart);
  406. sigaddset(&mask, __pthread_sig_cancel);
  407. sigprocmask(SIG_UNBLOCK, &mask, NULL);
  408. /* Register an exit function to kill all other threads. */
  409. /* Do it early so that user-registered atexit functions are called
  410. before pthread_onexit_process. */
  411. on_exit(pthread_onexit_process, NULL);
  412. }
  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