pthread.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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_USE_MMU__
  160. char *__pthread_initial_thread_tos = NULL;
  161. #endif /* __ARCH_USE_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. .ptr__pthread_cleanup_push_defer = _pthread_cleanup_push_defer,
  302. .ptr__pthread_cleanup_pop_restore = _pthread_cleanup_pop_restore,
  303. };
  304. #ifdef SHARED
  305. # define ptr_pthread_functions &__pthread_functions
  306. #else
  307. # define ptr_pthread_functions NULL
  308. #endif
  309. static int *__libc_multiple_threads_ptr;
  310. /* Do some minimal initialization which has to be done during the
  311. startup of the C library. */
  312. void __pthread_initialize_minimal(void)
  313. {
  314. /* If we have special thread_self processing, initialize
  315. * that for the main thread now. */
  316. #ifdef INIT_THREAD_SELF
  317. INIT_THREAD_SELF(&__pthread_initial_thread, 0);
  318. #endif
  319. __libc_multiple_threads_ptr = __libc_pthread_init (ptr_pthread_functions);
  320. }
  321. static void pthread_initialize(void)
  322. {
  323. struct sigaction sa;
  324. sigset_t mask;
  325. #ifdef __ARCH_USE_MMU__
  326. struct rlimit limit;
  327. rlim_t max_stack;
  328. #endif
  329. /* If already done (e.g. by a constructor called earlier!), bail out */
  330. if (__pthread_initial_thread_bos != NULL) return;
  331. #ifdef TEST_FOR_COMPARE_AND_SWAP
  332. /* Test if compare-and-swap is available */
  333. __pthread_has_cas = compare_and_swap_is_available();
  334. #endif
  335. /* For the initial stack, reserve at least STACK_SIZE bytes of stack
  336. below the current stack address, and align that on a
  337. STACK_SIZE boundary. */
  338. __pthread_initial_thread_bos =
  339. (char *)(((long)CURRENT_STACK_FRAME - 2 * STACK_SIZE) & ~(STACK_SIZE - 1));
  340. /* Update the descriptor for the initial thread. */
  341. __pthread_initial_thread.p_pid = getpid();
  342. /* If we have special thread_self processing, initialize that for the
  343. main thread now. */
  344. #ifdef INIT_THREAD_SELF
  345. INIT_THREAD_SELF(&__pthread_initial_thread, 0);
  346. #endif
  347. /* The errno/h_errno variable of the main thread are the global ones. */
  348. __pthread_initial_thread.p_errnop = &_errno;
  349. __pthread_initial_thread.p_h_errnop = &_h_errno;
  350. #ifdef __UCLIBC_HAS_XLOCALE__
  351. /* The locale of the main thread is the current locale in use. */
  352. __pthread_initial_thread.locale = __curlocale_var;
  353. #endif /* __UCLIBC_HAS_XLOCALE__ */
  354. { /* uClibc-specific stdio initialization for threads. */
  355. FILE *fp;
  356. _stdio_user_locking = 0; /* 2 if threading not initialized */
  357. for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
  358. if (fp->__user_locking != 1) {
  359. fp->__user_locking = 0;
  360. }
  361. }
  362. }
  363. /* Play with the stack size limit to make sure that no stack ever grows
  364. beyond STACK_SIZE minus two pages (one page for the thread descriptor
  365. immediately beyond, and one page to act as a guard page). */
  366. #ifdef __ARCH_USE_MMU__
  367. /* We cannot allocate a huge chunk of memory to mmap all thread stacks later
  368. * on a non-MMU system. Thus, we don't need the rlimit either. -StS */
  369. getrlimit(RLIMIT_STACK, &limit);
  370. max_stack = STACK_SIZE - 2 * getpagesize();
  371. if (limit.rlim_cur > max_stack) {
  372. limit.rlim_cur = max_stack;
  373. setrlimit(RLIMIT_STACK, &limit);
  374. }
  375. #else
  376. /* For non-MMU assume __pthread_initial_thread_tos at upper page boundary, and
  377. * __pthread_initial_thread_bos at address 0. These bounds are refined as we
  378. * malloc other stack frames such that they don't overlap. -StS
  379. */
  380. __pthread_initial_thread_tos =
  381. (char *)(((long)CURRENT_STACK_FRAME + getpagesize()) & ~(getpagesize() - 1));
  382. __pthread_initial_thread_bos = (char *) 1; /* set it non-zero so we know we have been here */
  383. PDEBUG("initial thread stack bounds: bos=%p, tos=%p\n",
  384. __pthread_initial_thread_bos, __pthread_initial_thread_tos);
  385. #endif /* __ARCH_USE_MMU__ */
  386. /* Setup signal handlers for the initial thread.
  387. Since signal handlers are shared between threads, these settings
  388. will be inherited by all other threads. */
  389. sa.sa_handler = pthread_handle_sigrestart;
  390. sigemptyset(&sa.sa_mask);
  391. sa.sa_flags = 0;
  392. __libc_sigaction(__pthread_sig_restart, &sa, NULL);
  393. sa.sa_handler = pthread_handle_sigcancel;
  394. sigaddset(&sa.sa_mask, __pthread_sig_restart);
  395. // sa.sa_flags = 0;
  396. __libc_sigaction(__pthread_sig_cancel, &sa, NULL);
  397. if (__pthread_sig_debug > 0) {
  398. sa.sa_handler = pthread_handle_sigdebug;
  399. sigemptyset(&sa.sa_mask);
  400. // sa.sa_flags = 0;
  401. __libc_sigaction(__pthread_sig_debug, &sa, NULL);
  402. }
  403. /* Initially, block __pthread_sig_restart. Will be unblocked on demand. */
  404. sigemptyset(&mask);
  405. sigaddset(&mask, __pthread_sig_restart);
  406. sigprocmask(SIG_BLOCK, &mask, NULL);
  407. /* And unblock __pthread_sig_cancel if it has been blocked. */
  408. sigdelset(&mask, __pthread_sig_restart);
  409. sigaddset(&mask, __pthread_sig_cancel);
  410. sigprocmask(SIG_UNBLOCK, &mask, NULL);
  411. /* Register an exit function to kill all other threads. */
  412. /* Do it early so that user-registered atexit functions are called
  413. before pthread_onexit_process. */
  414. on_exit(pthread_onexit_process, NULL);
  415. }
  416. void __pthread_initialize(void);
  417. void __pthread_initialize(void)
  418. {
  419. pthread_initialize();
  420. }
  421. int __pthread_initialize_manager(void)
  422. {
  423. int manager_pipe[2];
  424. int pid;
  425. int report_events;
  426. struct pthread_request request;
  427. *__libc_multiple_threads_ptr = 1;
  428. /* If basic initialization not done yet (e.g. we're called from a
  429. constructor run before our constructor), do it now */
  430. if (__pthread_initial_thread_bos == NULL) pthread_initialize();
  431. /* Setup stack for thread manager */
  432. __pthread_manager_thread_bos = malloc(THREAD_MANAGER_STACK_SIZE);
  433. if (__pthread_manager_thread_bos == NULL) return -1;
  434. __pthread_manager_thread_tos =
  435. __pthread_manager_thread_bos + THREAD_MANAGER_STACK_SIZE;
  436. /* On non-MMU systems we make sure that the initial thread bounds don't overlap
  437. * with the manager stack frame */
  438. NOMMU_INITIAL_THREAD_BOUNDS(__pthread_manager_thread_tos,__pthread_manager_thread_bos);
  439. PDEBUG("manager stack: size=%d, bos=%p, tos=%p\n", THREAD_MANAGER_STACK_SIZE,
  440. __pthread_manager_thread_bos, __pthread_manager_thread_tos);
  441. #if 0
  442. PDEBUG("initial stack: estimate bos=%p, tos=%p\n",
  443. __pthread_initial_thread_bos, __pthread_initial_thread_tos);
  444. #endif
  445. /* Setup pipe to communicate with thread manager */
  446. if (pipe(manager_pipe) == -1) {
  447. free(__pthread_manager_thread_bos);
  448. return -1;
  449. }
  450. /* Start the thread manager */
  451. pid = 0;
  452. #ifdef USE_TLS
  453. if (__linuxthreads_initial_report_events != 0)
  454. THREAD_SETMEM (((pthread_descr) NULL), p_report_events,
  455. __linuxthreads_initial_report_events);
  456. report_events = THREAD_GETMEM (((pthread_descr) NULL), p_report_events);
  457. #else
  458. if (__linuxthreads_initial_report_events != 0)
  459. __pthread_initial_thread.p_report_events
  460. = __linuxthreads_initial_report_events;
  461. report_events = __pthread_initial_thread.p_report_events;
  462. #endif
  463. if (__builtin_expect (report_events, 0))
  464. {
  465. /* It's a bit more complicated. We have to report the creation of
  466. the manager thread. */
  467. int idx = __td_eventword (TD_CREATE);
  468. uint32_t mask = __td_eventmask (TD_CREATE);
  469. if ((mask & (__pthread_threads_events.event_bits[idx]
  470. | __pthread_initial_thread.p_eventbuf.eventmask.event_bits[idx]))
  471. != 0)
  472. {
  473. __pthread_lock(__pthread_manager_thread.p_lock, NULL);
  474. #ifdef __ia64__
  475. pid = __clone2(__pthread_manager_event,
  476. (void **) __pthread_manager_thread_tos,
  477. THREAD_MANAGER_STACK_SIZE,
  478. CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
  479. (void *)(long)manager_pipe[0]);
  480. #else
  481. pid = clone(__pthread_manager_event,
  482. (void **) __pthread_manager_thread_tos,
  483. CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
  484. (void *)(long)manager_pipe[0]);
  485. #endif
  486. if (pid != -1)
  487. {
  488. /* Now fill in the information about the new thread in
  489. the newly created thread's data structure. We cannot let
  490. the new thread do this since we don't know whether it was
  491. already scheduled when we send the event. */
  492. __pthread_manager_thread.p_eventbuf.eventdata =
  493. &__pthread_manager_thread;
  494. __pthread_manager_thread.p_eventbuf.eventnum = TD_CREATE;
  495. __pthread_last_event = &__pthread_manager_thread;
  496. __pthread_manager_thread.p_tid = 2* PTHREAD_THREADS_MAX + 1;
  497. __pthread_manager_thread.p_pid = pid;
  498. /* Now call the function which signals the event. */
  499. __linuxthreads_create_event ();
  500. }
  501. /* Now restart the thread. */
  502. __pthread_unlock(__pthread_manager_thread.p_lock);
  503. }
  504. }
  505. if (pid == 0) {
  506. #ifdef __ia64__
  507. pid = __clone2(__pthread_manager, (void **) __pthread_manager_thread_tos,
  508. THREAD_MANAGER_STACK_SIZE,
  509. CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
  510. (void *)(long)manager_pipe[0]);
  511. #else
  512. pid = clone(__pthread_manager, (void **) __pthread_manager_thread_tos,
  513. CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
  514. (void *)(long)manager_pipe[0]);
  515. #endif
  516. }
  517. if (pid == -1) {
  518. free(__pthread_manager_thread_bos);
  519. __libc_close(manager_pipe[0]);
  520. __libc_close(manager_pipe[1]);
  521. return -1;
  522. }
  523. __pthread_manager_request = manager_pipe[1]; /* writing end */
  524. __pthread_manager_reader = manager_pipe[0]; /* reading end */
  525. __pthread_manager_thread.p_tid = 2* PTHREAD_THREADS_MAX + 1;
  526. __pthread_manager_thread.p_pid = pid;
  527. /* Make gdb aware of new thread manager */
  528. if (__pthread_threads_debug && __pthread_sig_debug > 0)
  529. {
  530. raise(__pthread_sig_debug);
  531. /* We suspend ourself and gdb will wake us up when it is
  532. ready to handle us. */
  533. __pthread_wait_for_restart_signal(thread_self());
  534. }
  535. /* Synchronize debugging of the thread manager */
  536. PDEBUG("send REQ_DEBUG to manager thread\n");
  537. request.req_kind = REQ_DEBUG;
  538. TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
  539. (char *) &request, sizeof(request)));
  540. return 0;
  541. }
  542. /* Thread creation */
  543. int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
  544. void * (*start_routine)(void *), void *arg)
  545. {
  546. pthread_descr self = thread_self();
  547. struct pthread_request request;
  548. if (__pthread_manager_request < 0) {
  549. if (__pthread_initialize_manager() < 0) return EAGAIN;
  550. }
  551. request.req_thread = self;
  552. request.req_kind = REQ_CREATE;
  553. request.req_args.create.attr = attr;
  554. request.req_args.create.fn = start_routine;
  555. request.req_args.create.arg = arg;
  556. sigprocmask(SIG_SETMASK, (const sigset_t *) NULL,
  557. &request.req_args.create.mask);
  558. PDEBUG("write REQ_CREATE to manager thread\n");
  559. TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
  560. (char *) &request, sizeof(request)));
  561. PDEBUG("before suspend(self)\n");
  562. suspend(self);
  563. PDEBUG("after suspend(self)\n");
  564. if (THREAD_GETMEM(self, p_retcode) == 0)
  565. *thread = (pthread_t) THREAD_GETMEM(self, p_retval);
  566. return THREAD_GETMEM(self, p_retcode);
  567. }
  568. /* Simple operations on thread identifiers */
  569. pthread_t pthread_self(void)
  570. {
  571. pthread_descr self = thread_self();
  572. return THREAD_GETMEM(self, p_tid);
  573. }
  574. int pthread_equal(pthread_t thread1, pthread_t thread2)
  575. {
  576. return thread1 == thread2;
  577. }
  578. /* Helper function for thread_self in the case of user-provided stacks */
  579. #ifndef THREAD_SELF
  580. pthread_descr __pthread_find_self()
  581. {
  582. char * sp = CURRENT_STACK_FRAME;
  583. pthread_handle h;
  584. /* __pthread_handles[0] is the initial thread, __pthread_handles[1] is
  585. the manager threads handled specially in thread_self(), so start at 2 */
  586. h = __pthread_handles + 2;
  587. while (! (sp <= (char *) h->h_descr && sp >= h->h_bottom)) h++;
  588. #ifdef DEBUG_PT
  589. if (h->h_descr == NULL) {
  590. printf("*** %s ERROR descriptor is NULL!!!!! ***\n\n", __FUNCTION__);
  591. _exit(1);
  592. }
  593. #endif
  594. return h->h_descr;
  595. }
  596. #else
  597. static pthread_descr thread_self_stack(void)
  598. {
  599. char *sp = CURRENT_STACK_FRAME;
  600. pthread_handle h;
  601. if (sp >= __pthread_manager_thread_bos && sp < __pthread_manager_thread_tos)
  602. return manager_thread;
  603. h = __pthread_handles + 2;
  604. # ifdef USE_TLS
  605. while (h->h_descr == NULL
  606. || ! (sp <= (char *) h->h_descr->p_stackaddr && sp >= h->h_bottom))
  607. h++;
  608. # else
  609. while (! (sp <= (char *) h->h_descr && sp >= h->h_bottom))
  610. h++;
  611. # endif
  612. return h->h_descr;
  613. }
  614. #endif
  615. /* Thread scheduling */
  616. int pthread_setschedparam(pthread_t thread, int policy,
  617. const struct sched_param *param)
  618. {
  619. pthread_handle handle = thread_handle(thread);
  620. pthread_descr th;
  621. __pthread_lock(&handle->h_lock, NULL);
  622. if (invalid_handle(handle, thread)) {
  623. __pthread_unlock(&handle->h_lock);
  624. return ESRCH;
  625. }
  626. th = handle->h_descr;
  627. if (sched_setscheduler(th->p_pid, policy, param) == -1) {
  628. __pthread_unlock(&handle->h_lock);
  629. return errno;
  630. }
  631. th->p_priority = policy == SCHED_OTHER ? 0 : param->sched_priority;
  632. __pthread_unlock(&handle->h_lock);
  633. if (__pthread_manager_request >= 0)
  634. __pthread_manager_adjust_prio(th->p_priority);
  635. return 0;
  636. }
  637. int pthread_getschedparam(pthread_t thread, int *policy,
  638. struct sched_param *param)
  639. {
  640. pthread_handle handle = thread_handle(thread);
  641. int pid, pol;
  642. __pthread_lock(&handle->h_lock, NULL);
  643. if (invalid_handle(handle, thread)) {
  644. __pthread_unlock(&handle->h_lock);
  645. return ESRCH;
  646. }
  647. pid = handle->h_descr->p_pid;
  648. __pthread_unlock(&handle->h_lock);
  649. pol = sched_getscheduler(pid);
  650. if (pol == -1) return errno;
  651. if (sched_getparam(pid, param) == -1) return errno;
  652. *policy = pol;
  653. return 0;
  654. }
  655. /* Process-wide exit() request */
  656. static void pthread_onexit_process(int retcode, void *arg attribute_unused)
  657. {
  658. struct pthread_request request;
  659. pthread_descr self = thread_self();
  660. if (__pthread_manager_request >= 0) {
  661. request.req_thread = self;
  662. request.req_kind = REQ_PROCESS_EXIT;
  663. request.req_args.exit.code = retcode;
  664. TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
  665. (char *) &request, sizeof(request)));
  666. suspend(self);
  667. /* Main thread should accumulate times for thread manager and its
  668. children, so that timings for main thread account for all threads. */
  669. if (self == __pthread_main_thread) {
  670. waitpid(__pthread_manager_thread.p_pid, NULL, __WCLONE);
  671. /* Since all threads have been asynchronously terminated
  672. * (possibly holding locks), free cannot be used any more. */
  673. __pthread_manager_thread_bos = __pthread_manager_thread_tos = NULL;
  674. }
  675. }
  676. }
  677. /* The handler for the RESTART signal just records the signal received
  678. in the thread descriptor, and optionally performs a siglongjmp
  679. (for pthread_cond_timedwait). */
  680. static void pthread_handle_sigrestart(int sig)
  681. {
  682. pthread_descr self = thread_self();
  683. THREAD_SETMEM(self, p_signal, sig);
  684. if (THREAD_GETMEM(self, p_signal_jmp) != NULL)
  685. siglongjmp(*THREAD_GETMEM(self, p_signal_jmp), 1);
  686. }
  687. /* The handler for the CANCEL signal checks for cancellation
  688. (in asynchronous mode), for process-wide exit and exec requests.
  689. For the thread manager thread, redirect the signal to
  690. __pthread_manager_sighandler. */
  691. static void pthread_handle_sigcancel(int sig)
  692. {
  693. pthread_descr self = thread_self();
  694. sigjmp_buf * jmpbuf;
  695. if (self == &__pthread_manager_thread)
  696. {
  697. #ifdef THREAD_SELF
  698. /* A new thread might get a cancel signal before it is fully
  699. initialized, so that the thread register might still point to the
  700. manager thread. Double check that this is really the manager
  701. thread. */
  702. pthread_descr real_self = thread_self_stack();
  703. if (real_self == &__pthread_manager_thread)
  704. {
  705. __pthread_manager_sighandler(sig);
  706. return;
  707. }
  708. /* Oops, thread_self() isn't working yet.. */
  709. self = real_self;
  710. # ifdef INIT_THREAD_SELF
  711. INIT_THREAD_SELF(self, self->p_nr);
  712. # endif
  713. #else
  714. __pthread_manager_sighandler(sig);
  715. return;
  716. #endif
  717. }
  718. if (__builtin_expect (__pthread_exit_requested, 0)) {
  719. /* Main thread should accumulate times for thread manager and its
  720. children, so that timings for main thread account for all threads. */
  721. if (self == __pthread_main_thread) {
  722. #ifdef USE_TLS
  723. waitpid(__pthread_manager_thread->p_pid, NULL, __WCLONE);
  724. #else
  725. waitpid(__pthread_manager_thread.p_pid, NULL, __WCLONE);
  726. #endif
  727. }
  728. _exit(__pthread_exit_code);
  729. }
  730. if (__builtin_expect (THREAD_GETMEM(self, p_canceled), 0)
  731. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
  732. if (THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
  733. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  734. jmpbuf = THREAD_GETMEM(self, p_cancel_jmp);
  735. if (jmpbuf != NULL) {
  736. THREAD_SETMEM(self, p_cancel_jmp, NULL);
  737. siglongjmp(*jmpbuf, 1);
  738. }
  739. }
  740. }
  741. /* Handler for the DEBUG signal.
  742. The debugging strategy is as follows:
  743. On reception of a REQ_DEBUG request (sent by new threads created to
  744. the thread manager under debugging mode), the thread manager throws
  745. __pthread_sig_debug to itself. The debugger (if active) intercepts
  746. this signal, takes into account new threads and continue execution
  747. of the thread manager by propagating the signal because it doesn't
  748. know what it is specifically done for. In the current implementation,
  749. the thread manager simply discards it. */
  750. static void pthread_handle_sigdebug(int sig attribute_unused)
  751. {
  752. /* Nothing */
  753. }
  754. /* Reset the state of the thread machinery after a fork().
  755. Close the pipe used for requests and set the main thread to the forked
  756. thread.
  757. Notice that we can't free the stack segments, as the forked thread
  758. may hold pointers into them. */
  759. void __pthread_reset_main_thread()
  760. {
  761. pthread_descr self = thread_self();
  762. if (__pthread_manager_request != -1) {
  763. /* Free the thread manager stack */
  764. free(__pthread_manager_thread_bos);
  765. __pthread_manager_thread_bos = __pthread_manager_thread_tos = NULL;
  766. /* Close the two ends of the pipe */
  767. __libc_close(__pthread_manager_request);
  768. __libc_close(__pthread_manager_reader);
  769. __pthread_manager_request = __pthread_manager_reader = -1;
  770. }
  771. /* Update the pid of the main thread */
  772. THREAD_SETMEM(self, p_pid, getpid());
  773. /* Make the forked thread the main thread */
  774. __pthread_main_thread = self;
  775. THREAD_SETMEM(self, p_nextlive, self);
  776. THREAD_SETMEM(self, p_prevlive, self);
  777. /* Now this thread modifies the global variables. */
  778. THREAD_SETMEM(self, p_errnop, &_errno);
  779. THREAD_SETMEM(self, p_h_errnop, &_h_errno);
  780. }
  781. /* Process-wide exec() request */
  782. void __pthread_kill_other_threads_np(void)
  783. {
  784. struct sigaction sa;
  785. /* Terminate all other threads and thread manager */
  786. pthread_onexit_process(0, NULL);
  787. /* Make current thread the main thread in case the calling thread
  788. changes its mind, does not exec(), and creates new threads instead. */
  789. __pthread_reset_main_thread();
  790. /* Reset the signal handlers behaviour for the signals the
  791. implementation uses since this would be passed to the new
  792. process. */
  793. sigemptyset(&sa.sa_mask);
  794. sa.sa_flags = 0;
  795. sa.sa_handler = SIG_DFL;
  796. __libc_sigaction(__pthread_sig_restart, &sa, NULL);
  797. __libc_sigaction(__pthread_sig_cancel, &sa, NULL);
  798. if (__pthread_sig_debug > 0)
  799. __libc_sigaction(__pthread_sig_debug, &sa, NULL);
  800. }
  801. weak_alias (__pthread_kill_other_threads_np, pthread_kill_other_threads_np)
  802. /* Concurrency symbol level. */
  803. static int current_level;
  804. int __pthread_setconcurrency(int level)
  805. {
  806. /* We don't do anything unless we have found a useful interpretation. */
  807. current_level = level;
  808. return 0;
  809. }
  810. weak_alias (__pthread_setconcurrency, pthread_setconcurrency)
  811. int __pthread_getconcurrency(void)
  812. {
  813. return current_level;
  814. }
  815. weak_alias (__pthread_getconcurrency, pthread_getconcurrency)
  816. /* Primitives for controlling thread execution */
  817. void __pthread_wait_for_restart_signal(pthread_descr self)
  818. {
  819. sigset_t mask;
  820. sigprocmask(SIG_SETMASK, NULL, &mask); /* Get current signal mask */
  821. sigdelset(&mask, __pthread_sig_restart); /* Unblock the restart signal */
  822. THREAD_SETMEM(self, p_signal, 0);
  823. do {
  824. sigsuspend(&mask); /* Wait for signal */
  825. } while (THREAD_GETMEM(self, p_signal) !=__pthread_sig_restart);
  826. READ_MEMORY_BARRIER(); /* See comment in __pthread_restart_new */
  827. }
  828. #ifndef __NR_rt_sigaction
  829. /* The _old variants are for 2.0 and early 2.1 kernels which don't have RT
  830. signals.
  831. On these kernels, we use SIGUSR1 and SIGUSR2 for restart and cancellation.
  832. Since the restart signal does not queue, we use an atomic counter to create
  833. queuing semantics. This is needed to resolve a rare race condition in
  834. pthread_cond_timedwait_relative. */
  835. void __pthread_restart_old(pthread_descr th)
  836. {
  837. if (atomic_increment(&th->p_resume_count) == -1)
  838. kill(th->p_pid, __pthread_sig_restart);
  839. }
  840. void __pthread_suspend_old(pthread_descr self)
  841. {
  842. if (atomic_decrement(&self->p_resume_count) <= 0)
  843. __pthread_wait_for_restart_signal(self);
  844. }
  845. int
  846. __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abstime)
  847. {
  848. sigset_t unblock, initial_mask;
  849. int was_signalled = 0;
  850. sigjmp_buf jmpbuf;
  851. if (atomic_decrement(&self->p_resume_count) == 0) {
  852. /* Set up a longjmp handler for the restart signal, unblock
  853. the signal and sleep. */
  854. if (sigsetjmp(jmpbuf, 1) == 0) {
  855. THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
  856. THREAD_SETMEM(self, p_signal, 0);
  857. /* Unblock the restart signal */
  858. sigemptyset(&unblock);
  859. sigaddset(&unblock, __pthread_sig_restart);
  860. sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
  861. while (1) {
  862. struct timeval now;
  863. struct timespec reltime;
  864. /* Compute a time offset relative to now. */
  865. gettimeofday (&now, NULL);
  866. reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
  867. reltime.tv_sec = abstime->tv_sec - now.tv_sec;
  868. if (reltime.tv_nsec < 0) {
  869. reltime.tv_nsec += 1000000000;
  870. reltime.tv_sec -= 1;
  871. }
  872. /* Sleep for the required duration. If woken by a signal,
  873. resume waiting as required by Single Unix Specification. */
  874. if (reltime.tv_sec < 0 || __libc_nanosleep(&reltime, NULL) == 0)
  875. break;
  876. }
  877. /* Block the restart signal again */
  878. sigprocmask(SIG_SETMASK, &initial_mask, NULL);
  879. was_signalled = 0;
  880. } else {
  881. was_signalled = 1;
  882. }
  883. THREAD_SETMEM(self, p_signal_jmp, NULL);
  884. }
  885. /* Now was_signalled is true if we exited the above code
  886. due to the delivery of a restart signal. In that case,
  887. we know we have been dequeued and resumed and that the
  888. resume count is balanced. Otherwise, there are some
  889. cases to consider. First, try to bump up the resume count
  890. back to zero. If it goes to 1, it means restart() was
  891. invoked on this thread. The signal must be consumed
  892. and the count bumped down and everything is cool. We
  893. can return a 1 to the caller.
  894. Otherwise, no restart was delivered yet, so a potential
  895. race exists; we return a 0 to the caller which must deal
  896. with this race in an appropriate way; for example by
  897. atomically removing the thread from consideration for a
  898. wakeup---if such a thing fails, it means a restart is
  899. being delivered. */
  900. if (!was_signalled) {
  901. if (atomic_increment(&self->p_resume_count) != -1) {
  902. __pthread_wait_for_restart_signal(self);
  903. atomic_decrement(&self->p_resume_count); /* should be zero now! */
  904. /* woke spontaneously and consumed restart signal */
  905. return 1;
  906. }
  907. /* woke spontaneously but did not consume restart---caller must resolve */
  908. return 0;
  909. }
  910. /* woken due to restart signal */
  911. return 1;
  912. }
  913. #endif /* __NR_rt_sigaction */
  914. #ifdef __NR_rt_sigaction
  915. void __pthread_restart_new(pthread_descr th)
  916. {
  917. /* The barrier is proabably not needed, in which case it still documents
  918. our assumptions. The intent is to commit previous writes to shared
  919. memory so the woken thread will have a consistent view. Complementary
  920. read barriers are present to the suspend functions. */
  921. WRITE_MEMORY_BARRIER();
  922. kill(th->p_pid, __pthread_sig_restart);
  923. }
  924. int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime)
  925. {
  926. sigset_t unblock, initial_mask;
  927. int was_signalled = 0;
  928. sigjmp_buf jmpbuf;
  929. if (sigsetjmp(jmpbuf, 1) == 0) {
  930. THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
  931. THREAD_SETMEM(self, p_signal, 0);
  932. /* Unblock the restart signal */
  933. sigemptyset(&unblock);
  934. sigaddset(&unblock, __pthread_sig_restart);
  935. sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
  936. while (1) {
  937. struct timeval now;
  938. struct timespec reltime;
  939. /* Compute a time offset relative to now. */
  940. gettimeofday (&now, NULL);
  941. reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
  942. reltime.tv_sec = abstime->tv_sec - now.tv_sec;
  943. if (reltime.tv_nsec < 0) {
  944. reltime.tv_nsec += 1000000000;
  945. reltime.tv_sec -= 1;
  946. }
  947. /* Sleep for the required duration. If woken by a signal,
  948. resume waiting as required by Single Unix Specification. */
  949. if (reltime.tv_sec < 0 || __libc_nanosleep(&reltime, NULL) == 0)
  950. break;
  951. }
  952. /* Block the restart signal again */
  953. sigprocmask(SIG_SETMASK, &initial_mask, NULL);
  954. was_signalled = 0;
  955. } else {
  956. was_signalled = 1;
  957. }
  958. THREAD_SETMEM(self, p_signal_jmp, NULL);
  959. /* Now was_signalled is true if we exited the above code
  960. due to the delivery of a restart signal. In that case,
  961. everything is cool. We have been removed from whatever
  962. we were waiting on by the other thread, and consumed its signal.
  963. Otherwise we this thread woke up spontaneously, or due to a signal other
  964. than restart. This is an ambiguous case that must be resolved by
  965. the caller; the thread is still eligible for a restart wakeup
  966. so there is a race. */
  967. READ_MEMORY_BARRIER(); /* See comment in __pthread_restart_new */
  968. return was_signalled;
  969. }
  970. #endif
  971. /* Debugging aid */
  972. #ifdef DEBUG_PT
  973. #include <stdarg.h>
  974. void __pthread_message(char * fmt, ...)
  975. {
  976. char buffer[1024];
  977. va_list args;
  978. sprintf(buffer, "%05d : ", getpid());
  979. va_start(args, fmt);
  980. vsnprintf(buffer + 8, sizeof(buffer) - 8, fmt, args);
  981. va_end(args);
  982. TEMP_FAILURE_RETRY(__libc_write(2, buffer, strlen(buffer)));
  983. }
  984. #endif
  985. #ifndef __PIC__
  986. /* We need a hook to force the cancelation wrappers to be linked in when
  987. static libpthread is used. */
  988. extern const int __pthread_provide_wrappers;
  989. static const int *const __pthread_require_wrappers =
  990. &__pthread_provide_wrappers;
  991. #endif