pthread.c 42 KB

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