pthread.c 42 KB

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