2 /* Linuxthreads - a simple clone()-based implementation of Posix */
3 /* threads for Linux. */
4 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
6 /* This program is free software; you can redistribute it and/or */
7 /* modify it under the terms of the GNU Library General Public License */
8 /* as published by the Free Software Foundation; either version 2 */
9 /* of the License, or (at your option) any later version. */
11 /* This program is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Library General Public License for more details. */
16 /* Thread creation, initialization, and basic low-level routines */
26 #include <sys/resource.h>
28 #include <shlib-compat.h>
30 #include "internals.h"
37 #include <not-cancel.h>
40 #if !defined __SIGRTMIN || (__SIGRTMAX - __SIGRTMIN) < 3
41 # error "This must not happen"
44 #if !(USE_TLS && HAVE___THREAD)
45 /* These variables are used by the setup code. */
49 /* We need the global/static resolver state here. */
53 extern struct __res_state _res
;
58 /* We need only a few variables. */
59 #define manager_thread __pthread_manager_threadp
60 pthread_descr __pthread_manager_threadp attribute_hidden
;
64 /* Descriptor of the initial thread */
66 struct _pthread_descr_struct __pthread_initial_thread
= {
67 .p_header
.data
.self
= &__pthread_initial_thread
,
68 .p_nextlive
= &__pthread_initial_thread
,
69 .p_prevlive
= &__pthread_initial_thread
,
70 .p_tid
= PTHREAD_THREADS_MAX
,
71 .p_lock
= &__pthread_handles
[0].h_lock
,
72 .p_start_args
= PTHREAD_START_ARGS_INITIALIZER(NULL
),
73 #if !(USE_TLS && HAVE___THREAD)
75 .p_h_errnop
= &_h_errno
,
79 .p_resume_count
= __ATOMIC_INITIALIZER
,
80 .p_alloca_cutoff
= __MAX_ALLOCA_CUTOFF
83 /* Descriptor of the manager thread; none of this is used but the error
84 variables, the p_pid and p_priority fields,
85 and the address for identification. */
87 #define manager_thread (&__pthread_manager_thread)
88 struct _pthread_descr_struct __pthread_manager_thread
= {
89 .p_header
.data
.self
= &__pthread_manager_thread
,
90 .p_header
.data
.multiple_threads
= 1,
91 .p_lock
= &__pthread_handles
[1].h_lock
,
92 .p_start_args
= PTHREAD_START_ARGS_INITIALIZER(__pthread_manager
),
93 #if !(USE_TLS && HAVE___THREAD)
94 .p_errnop
= &__pthread_manager_thread
.p_errno
,
97 .p_resume_count
= __ATOMIC_INITIALIZER
,
98 .p_alloca_cutoff
= PTHREAD_STACK_MIN
/ 4
102 /* Pointer to the main thread (the father of the thread manager thread) */
103 /* Originally, this is the initial thread, but this changes after fork() */
106 pthread_descr __pthread_main_thread
;
108 pthread_descr __pthread_main_thread
= &__pthread_initial_thread
;
111 /* Limit between the stack of the initial thread (above) and the
112 stacks of other threads (below). Aligned on a STACK_SIZE boundary. */
114 char *__pthread_initial_thread_bos
;
116 /* File descriptor for sending requests to the thread manager. */
117 /* Initially -1, meaning that the thread manager is not running. */
119 int __pthread_manager_request
= -1;
121 int __pthread_multiple_threads attribute_hidden
;
123 /* Other end of the pipe for sending requests to the thread manager. */
125 int __pthread_manager_reader
;
127 /* Limits of the thread manager stack */
129 char *__pthread_manager_thread_bos
;
130 char *__pthread_manager_thread_tos
;
132 /* For process-wide exit() */
134 int __pthread_exit_requested
;
135 int __pthread_exit_code
;
137 /* Maximum stack size. */
138 size_t __pthread_max_stacksize
;
140 /* Nozero if the machine has more than one processor. */
141 int __pthread_smp_kernel
;
144 #if !__ASSUME_REALTIME_SIGNALS
145 /* Pointers that select new or old suspend/resume functions
146 based on availability of rt signals. */
148 void (*__pthread_restart
)(pthread_descr
) = __pthread_restart_old
;
149 void (*__pthread_suspend
)(pthread_descr
) = __pthread_suspend_old
;
150 int (*__pthread_timedsuspend
)(pthread_descr
, const struct timespec
*) = __pthread_timedsuspend_old
;
151 #endif /* __ASSUME_REALTIME_SIGNALS */
153 /* Communicate relevant LinuxThreads constants to gdb */
155 const int __pthread_threads_max
= PTHREAD_THREADS_MAX
;
156 const int __pthread_sizeof_handle
= sizeof(struct pthread_handle_struct
);
157 const int __pthread_offsetof_descr
= offsetof(struct pthread_handle_struct
,
159 const int __pthread_offsetof_pid
= offsetof(struct _pthread_descr_struct
,
161 const int __linuxthreads_pthread_sizeof_descr
162 = sizeof(struct _pthread_descr_struct
);
164 const int __linuxthreads_initial_report_events
;
166 const char __linuxthreads_version
[] = VERSION
;
168 /* Forward declarations */
170 static void pthread_onexit_process(int retcode
, void *arg
);
171 #ifndef HAVE_Z_NODELETE
172 static void pthread_atexit_process(void *arg
, int retcode
);
173 static void pthread_atexit_retcode(void *arg
, int retcode
);
175 static void pthread_handle_sigcancel(int sig
);
176 static void pthread_handle_sigrestart(int sig
);
177 static void pthread_handle_sigdebug(int sig
);
179 /* Signal numbers used for the communication.
180 In these variables we keep track of the used variables. If the
181 platform does not support any real-time signals we will define the
182 values to some unreasonable value which will signal failing of all
183 the functions below. */
184 int __pthread_sig_restart
= __SIGRTMIN
;
185 int __pthread_sig_cancel
= __SIGRTMIN
+ 1;
186 int __pthread_sig_debug
= __SIGRTMIN
+ 2;
188 extern int __libc_current_sigrtmin_private (void);
190 #if !__ASSUME_REALTIME_SIGNALS
191 static int rtsigs_initialized
;
196 if (rtsigs_initialized
)
199 if (__libc_current_sigrtmin_private () == -1)
201 __pthread_sig_restart
= SIGUSR1
;
202 __pthread_sig_cancel
= SIGUSR2
;
203 __pthread_sig_debug
= 0;
207 __pthread_restart
= __pthread_restart_new
;
208 __pthread_suspend
= __pthread_wait_for_restart_signal
;
209 __pthread_timedsuspend
= __pthread_timedsuspend_new
;
212 rtsigs_initialized
= 1;
217 /* Initialize the pthread library.
218 Initialization is split in two functions:
219 - a constructor function that blocks the __pthread_sig_restart signal
220 (must do this very early, since the program could capture the signal
221 mask with e.g. sigsetjmp before creating the first thread);
222 - a regular function called from pthread_create when needed. */
224 static void pthread_initialize(void) __attribute__((constructor
));
226 #ifndef HAVE_Z_NODELETE
227 extern void *__dso_handle
__attribute__ ((weak
));
231 #if defined USE_TLS && !defined SHARED
232 extern void __libc_setup_tls (size_t tcbsize
, size_t tcbalign
);
235 struct pthread_functions __pthread_functions
=
237 #if !(USE_TLS && HAVE___THREAD)
238 .ptr_pthread_internal_tsd_set
= __pthread_internal_tsd_set
,
239 .ptr_pthread_internal_tsd_get
= __pthread_internal_tsd_get
,
240 .ptr_pthread_internal_tsd_address
= __pthread_internal_tsd_address
,
242 .ptr_pthread_fork
= __pthread_fork
,
243 .ptr_pthread_attr_destroy
= __pthread_attr_destroy
,
244 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
245 .ptr___pthread_attr_init_2_0
= __pthread_attr_init_2_0
,
247 .ptr___pthread_attr_init_2_1
= __pthread_attr_init_2_1
,
248 .ptr_pthread_attr_getdetachstate
= __pthread_attr_getdetachstate
,
249 .ptr_pthread_attr_setdetachstate
= __pthread_attr_setdetachstate
,
250 .ptr_pthread_attr_getinheritsched
= __pthread_attr_getinheritsched
,
251 .ptr_pthread_attr_setinheritsched
= __pthread_attr_setinheritsched
,
252 .ptr_pthread_attr_getschedparam
= __pthread_attr_getschedparam
,
253 .ptr_pthread_attr_setschedparam
= __pthread_attr_setschedparam
,
254 .ptr_pthread_attr_getschedpolicy
= __pthread_attr_getschedpolicy
,
255 .ptr_pthread_attr_setschedpolicy
= __pthread_attr_setschedpolicy
,
256 .ptr_pthread_attr_getscope
= __pthread_attr_getscope
,
257 .ptr_pthread_attr_setscope
= __pthread_attr_setscope
,
258 .ptr_pthread_condattr_destroy
= __pthread_condattr_destroy
,
259 .ptr_pthread_condattr_init
= __pthread_condattr_init
,
260 .ptr___pthread_cond_broadcast
= __pthread_cond_broadcast
,
261 .ptr___pthread_cond_destroy
= __pthread_cond_destroy
,
262 .ptr___pthread_cond_init
= __pthread_cond_init
,
263 .ptr___pthread_cond_signal
= __pthread_cond_signal
,
264 .ptr___pthread_cond_wait
= __pthread_cond_wait
,
265 .ptr___pthread_cond_timedwait
= __pthread_cond_timedwait
,
266 .ptr_pthread_equal
= __pthread_equal
,
267 .ptr___pthread_exit
= __pthread_exit
,
268 .ptr_pthread_getschedparam
= __pthread_getschedparam
,
269 .ptr_pthread_setschedparam
= __pthread_setschedparam
,
270 .ptr_pthread_mutex_destroy
= __pthread_mutex_destroy
,
271 .ptr_pthread_mutex_init
= __pthread_mutex_init
,
272 .ptr_pthread_mutex_lock
= __pthread_mutex_lock
,
273 .ptr_pthread_mutex_trylock
= __pthread_mutex_trylock
,
274 .ptr_pthread_mutex_unlock
= __pthread_mutex_unlock
,
275 .ptr_pthread_self
= __pthread_self
,
276 .ptr_pthread_setcancelstate
= __pthread_setcancelstate
,
277 .ptr_pthread_setcanceltype
= __pthread_setcanceltype
,
278 .ptr_pthread_do_exit
= __pthread_do_exit
,
279 .ptr_pthread_thread_self
= __pthread_thread_self
,
280 .ptr_pthread_cleanup_upto
= __pthread_cleanup_upto
,
281 .ptr_pthread_sigaction
= __pthread_sigaction
,
282 .ptr_pthread_sigwait
= __pthread_sigwait
,
283 .ptr_pthread_raise
= __pthread_raise
,
284 .ptr__pthread_cleanup_push
= _pthread_cleanup_push
,
285 .ptr__pthread_cleanup_pop
= _pthread_cleanup_pop
288 # define ptr_pthread_functions &__pthread_functions
290 # define ptr_pthread_functions NULL
293 static int *__libc_multiple_threads_ptr
;
295 /* Do some minimal initialization which has to be done during the
296 startup of the C library. */
298 __pthread_initialize_minimal(void)
303 /* First of all init __pthread_handles[0] and [1] if needed. */
304 # if __LT_SPINLOCK_INIT != 0
305 __pthread_handles
[0].h_lock
= __LOCK_INITIALIZER
;
306 __pthread_handles
[1].h_lock
= __LOCK_INITIALIZER
;
309 /* Unlike in the dynamically linked case the dynamic linker has not
310 taken care of initializing the TLS data structures. */
311 __libc_setup_tls (TLS_TCB_SIZE
, TLS_TCB_ALIGN
);
313 if (__builtin_expect (GL(dl_tls_dtv_slotinfo_list
) == NULL
, 0))
317 /* There is no actual TLS being used, so the thread register
318 was not initialized in the dynamic linker. */
320 /* We need to install special hooks so that the malloc and memalign
321 calls in _dl_tls_setup and _dl_allocate_tls won't cause full
322 malloc initialization that will try to set up its thread state. */
324 extern void __libc_malloc_pthread_startup (bool first_time
);
325 __libc_malloc_pthread_startup (true);
327 if (__builtin_expect (_dl_tls_setup (), 0)
328 || __builtin_expect ((tcbp
= _dl_allocate_tls (NULL
)) == NULL
, 0))
330 static const char msg
[] = "\
331 cannot allocate TLS data structures for initial thread\n";
332 TEMP_FAILURE_RETRY (write_not_cancel (STDERR_FILENO
,
333 msg
, sizeof msg
- 1));
336 const char *lossage
= TLS_INIT_TP (tcbp
, 0);
337 if (__builtin_expect (lossage
!= NULL
, 0))
339 static const char msg
[] = "cannot set up thread-local storage: ";
340 const char nl
= '\n';
341 TEMP_FAILURE_RETRY (write_not_cancel (STDERR_FILENO
,
342 msg
, sizeof msg
- 1));
343 TEMP_FAILURE_RETRY (write_not_cancel (STDERR_FILENO
,
344 lossage
, strlen (lossage
)));
345 TEMP_FAILURE_RETRY (write_not_cancel (STDERR_FILENO
, &nl
, 1));
348 /* Though it was allocated with libc's malloc, that was done without
349 the user's __malloc_hook installed. A later realloc that uses
350 the hooks might not work with that block from the plain malloc.
351 So we record this block as unfreeable just as the dynamic linker
352 does when it allocates the DTV before the libc malloc exists. */
353 GL(dl_initial_dtv
) = GET_DTV (tcbp
);
355 __libc_malloc_pthread_startup (false);
361 /* The memory for the thread descriptor was allocated elsewhere as
362 part of the TLS allocation. We have to initialize the data
363 structure by hand. This initialization must mirror the struct
365 self
->p_nextlive
= self
->p_prevlive
= self
;
366 self
->p_tid
= PTHREAD_THREADS_MAX
;
367 self
->p_lock
= &__pthread_handles
[0].h_lock
;
368 # ifndef HAVE___THREAD
369 self
->p_errnop
= &_errno
;
370 self
->p_h_errnop
= &_h_errno
;
372 /* self->p_start_args need not be initialized, it's all zero. */
373 self
->p_userstack
= 1;
374 # if __LT_SPINLOCK_INIT != 0
375 self
->p_resume_count
= (struct pthread_atomic
) __ATOMIC_INITIALIZER
;
377 self
->p_alloca_cutoff
= __MAX_ALLOCA_CUTOFF
;
379 /* Another variable which points to the thread descriptor. */
380 __pthread_main_thread
= self
;
382 /* And fill in the pointer the the thread __pthread_handles array. */
383 __pthread_handles
[0].h_descr
= self
;
387 /* First of all init __pthread_handles[0] and [1]. */
388 # if __LT_SPINLOCK_INIT != 0
389 __pthread_handles
[0].h_lock
= __LOCK_INITIALIZER
;
390 __pthread_handles
[1].h_lock
= __LOCK_INITIALIZER
;
392 __pthread_handles
[0].h_descr
= &__pthread_initial_thread
;
393 __pthread_handles
[1].h_descr
= &__pthread_manager_thread
;
395 /* If we have special thread_self processing, initialize that for the
397 # ifdef INIT_THREAD_SELF
398 INIT_THREAD_SELF(&__pthread_initial_thread
, 0);
404 self
->p_cpuclock_offset
= GL(dl_cpuclock_offset
);
406 __pthread_initial_thread
.p_cpuclock_offset
= GL(dl_cpuclock_offset
);
410 __libc_multiple_threads_ptr
= __libc_pthread_init (ptr_pthread_functions
);
415 __pthread_init_max_stacksize(void)
420 getrlimit(RLIMIT_STACK
, &limit
);
421 #ifdef FLOATING_STACKS
422 if (limit
.rlim_cur
== RLIM_INFINITY
)
423 limit
.rlim_cur
= ARCH_STACK_MAX_SIZE
;
424 # ifdef NEED_SEPARATE_REGISTER_STACK
425 max_stack
= limit
.rlim_cur
/ 2;
427 max_stack
= limit
.rlim_cur
;
430 /* Play with the stack size limit to make sure that no stack ever grows
431 beyond STACK_SIZE minus one page (to act as a guard page). */
432 # ifdef NEED_SEPARATE_REGISTER_STACK
433 /* STACK_SIZE bytes hold both the main stack and register backing
434 store. The rlimit value applies to each individually. */
435 max_stack
= STACK_SIZE
/2 - __getpagesize ();
437 max_stack
= STACK_SIZE
- __getpagesize();
439 if (limit
.rlim_cur
> max_stack
) {
440 limit
.rlim_cur
= max_stack
;
441 setrlimit(RLIMIT_STACK
, &limit
);
444 __pthread_max_stacksize
= max_stack
;
445 if (max_stack
/ 4 < __MAX_ALLOCA_CUTOFF
)
448 pthread_descr self
= THREAD_SELF
;
449 self
->p_alloca_cutoff
= max_stack
/ 4;
451 __pthread_initial_thread
.p_alloca_cutoff
= max_stack
/ 4;
458 /* When using __thread for this, we do it in libc so as not
459 to give libpthread its own TLS segment just for this. */
460 extern void **__libc_dl_error_tsd (void) __attribute__ ((const));
462 static void ** __attribute__ ((const))
463 __libc_dl_error_tsd (void)
465 return &thread_self ()->p_libc_specific
[_LIBC_TSD_KEY_DL_ERROR
];
471 static inline void __attribute__((always_inline
))
472 init_one_static_tls (pthread_descr descr
, struct link_map
*map
)
475 dtv_t
*dtv
= GET_DTV (descr
);
476 void *dest
= (char *) descr
- map
->l_tls_offset
;
478 dtv_t
*dtv
= GET_DTV ((pthread_descr
) ((char *) descr
+ TLS_PRE_TCB_SIZE
));
479 void *dest
= (char *) descr
+ map
->l_tls_offset
+ TLS_PRE_TCB_SIZE
;
481 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
484 /* Fill in the DTV slot so that a later LD/GD access will find it. */
485 dtv
[map
->l_tls_modid
].pointer
= dest
;
487 /* Initialize the memory. */
488 memset (__mempcpy (dest
, map
->l_tls_initimage
, map
->l_tls_initimage_size
),
489 '\0', map
->l_tls_blocksize
- map
->l_tls_initimage_size
);
493 __pthread_init_static_tls (struct link_map
*map
)
497 for (i
= 0; i
< PTHREAD_THREADS_MAX
; ++i
)
498 if (__pthread_handles
[i
].h_descr
!= NULL
&& i
!= 1)
500 __pthread_lock (&__pthread_handles
[i
].h_lock
, NULL
);
501 if (__pthread_handles
[i
].h_descr
!= NULL
)
502 init_one_static_tls (__pthread_handles
[i
].h_descr
, map
);
503 __pthread_unlock (&__pthread_handles
[i
].h_lock
);
508 static void pthread_initialize(void)
513 /* If already done (e.g. by a constructor called earlier!), bail out */
514 if (__pthread_initial_thread_bos
!= NULL
) return;
515 #ifdef TEST_FOR_COMPARE_AND_SWAP
516 /* Test if compare-and-swap is available */
517 __pthread_has_cas
= compare_and_swap_is_available();
519 #ifdef FLOATING_STACKS
520 /* We don't need to know the bottom of the stack. Give the pointer some
521 value to signal that initialization happened. */
522 __pthread_initial_thread_bos
= (void *) -1l;
524 /* Determine stack size limits . */
525 __pthread_init_max_stacksize ();
526 # ifdef _STACK_GROWS_UP
527 /* The initial thread already has all the stack it needs */
528 __pthread_initial_thread_bos
= (char *)
529 ((long)CURRENT_STACK_FRAME
&~ (STACK_SIZE
- 1));
531 /* For the initial stack, reserve at least STACK_SIZE bytes of stack
532 below the current stack address, and align that on a
533 STACK_SIZE boundary. */
534 __pthread_initial_thread_bos
=
535 (char *)(((long)CURRENT_STACK_FRAME
- 2 * STACK_SIZE
) & ~(STACK_SIZE
- 1));
539 /* Update the descriptor for the initial thread. */
540 THREAD_SETMEM (((pthread_descr
) NULL
), p_pid
, __getpid());
541 # ifndef HAVE___THREAD
542 /* Likewise for the resolver state _res. */
543 THREAD_SETMEM (((pthread_descr
) NULL
), p_resp
, &_res
);
546 /* Update the descriptor for the initial thread. */
547 __pthread_initial_thread
.p_pid
= __getpid();
548 /* Likewise for the resolver state _res. */
549 __pthread_initial_thread
.p_resp
= &_res
;
551 #if !__ASSUME_REALTIME_SIGNALS
552 /* Initialize real-time signals. */
555 /* Setup signal handlers for the initial thread.
556 Since signal handlers are shared between threads, these settings
557 will be inherited by all other threads. */
558 sa
.sa_handler
= pthread_handle_sigrestart
;
559 sigemptyset(&sa
.sa_mask
);
561 __libc_sigaction(__pthread_sig_restart
, &sa
, NULL
);
562 sa
.sa_handler
= pthread_handle_sigcancel
;
564 __libc_sigaction(__pthread_sig_cancel
, &sa
, NULL
);
565 if (__pthread_sig_debug
> 0) {
566 sa
.sa_handler
= pthread_handle_sigdebug
;
567 sigemptyset(&sa
.sa_mask
);
569 __libc_sigaction(__pthread_sig_debug
, &sa
, NULL
);
571 /* Initially, block __pthread_sig_restart. Will be unblocked on demand. */
573 sigaddset(&mask
, __pthread_sig_restart
);
574 sigprocmask(SIG_BLOCK
, &mask
, NULL
);
575 /* And unblock __pthread_sig_cancel if it has been blocked. */
576 sigdelset(&mask
, __pthread_sig_restart
);
577 sigaddset(&mask
, __pthread_sig_cancel
);
578 sigprocmask(SIG_UNBLOCK
, &mask
, NULL
);
579 /* Register an exit function to kill all other threads. */
580 /* Do it early so that user-registered atexit functions are called
581 before pthread_*exit_process. */
582 #ifndef HAVE_Z_NODELETE
583 if (__builtin_expect (&__dso_handle
!= NULL
, 1))
584 __cxa_atexit ((void (*) (void *)) pthread_atexit_process
, NULL
,
588 __on_exit (pthread_onexit_process
, NULL
);
589 /* How many processors. */
590 __pthread_smp_kernel
= is_smp_system ();
593 /* Transfer the old value from the dynamic linker's internal location. */
594 *__libc_dl_error_tsd () = *(*GL(dl_error_catch_tsd
)) ();
595 GL(dl_error_catch_tsd
) = &__libc_dl_error_tsd
;
597 /* Make __rtld_lock_{,un}lock_recursive use pthread_mutex_{,un}lock,
598 keep the lock count from the ld.so implementation. */
599 GL(dl_rtld_lock_recursive
) = (void *) __pthread_mutex_lock
;
600 GL(dl_rtld_unlock_recursive
) = (void *) __pthread_mutex_unlock
;
601 unsigned int rtld_lock_count
= GL(dl_load_lock
).mutex
.__m_count
;
602 GL(dl_load_lock
).mutex
.__m_count
= 0;
603 while (rtld_lock_count
-- > 0)
604 __pthread_mutex_lock (&GL(dl_load_lock
).mutex
);
608 GL(dl_init_static_tls
) = &__pthread_init_static_tls
;
612 void __pthread_initialize(void)
614 pthread_initialize();
617 int __pthread_initialize_manager(void)
621 struct pthread_request request
;
628 __pthread_multiple_threads
= 1;
629 #if TLS_MULTIPLE_THREADS_IN_TCB || !defined USE_TLS || !TLS_DTV_AT_TP
630 __pthread_main_thread
->p_multiple_threads
= 1;
632 *__libc_multiple_threads_ptr
= 1;
634 #ifndef HAVE_Z_NODELETE
635 if (__builtin_expect (&__dso_handle
!= NULL
, 1))
636 __cxa_atexit ((void (*) (void *)) pthread_atexit_retcode
, NULL
,
640 if (__pthread_max_stacksize
== 0)
641 __pthread_init_max_stacksize ();
642 /* If basic initialization not done yet (e.g. we're called from a
643 constructor run before our constructor), do it now */
644 if (__pthread_initial_thread_bos
== NULL
) pthread_initialize();
645 /* Setup stack for thread manager */
646 __pthread_manager_thread_bos
= malloc(THREAD_MANAGER_STACK_SIZE
);
647 if (__pthread_manager_thread_bos
== NULL
) return -1;
648 __pthread_manager_thread_tos
=
649 __pthread_manager_thread_bos
+ THREAD_MANAGER_STACK_SIZE
;
650 /* Setup pipe to communicate with thread manager */
651 if (pipe(manager_pipe
) == -1) {
652 free(__pthread_manager_thread_bos
);
657 /* Allocate memory for the thread descriptor and the dtv. */
658 tcbp
= _dl_allocate_tls (NULL
);
660 free(__pthread_manager_thread_bos
);
661 close_not_cancel(manager_pipe
[0]);
662 close_not_cancel(manager_pipe
[1]);
667 mgr
= (pthread_descr
) tcbp
;
669 /* pthread_descr is located right below tcbhead_t which _dl_allocate_tls
671 mgr
= (pthread_descr
) ((char *) tcbp
- TLS_PRE_TCB_SIZE
);
673 __pthread_handles
[1].h_descr
= manager_thread
= mgr
;
675 /* Initialize the descriptor. */
676 #if !defined USE_TLS || !TLS_DTV_AT_TP
677 mgr
->p_header
.data
.tcb
= tcbp
;
678 mgr
->p_header
.data
.self
= mgr
;
679 mgr
->p_header
.data
.multiple_threads
= 1;
680 #elif TLS_MULTIPLE_THREADS_IN_TCB
681 mgr
->p_multiple_threads
= 1;
683 mgr
->p_lock
= &__pthread_handles
[1].h_lock
;
684 # ifndef HAVE___THREAD
685 mgr
->p_errnop
= &mgr
->p_errno
;
687 mgr
->p_start_args
= (struct pthread_start_args
) PTHREAD_START_ARGS_INITIALIZER(__pthread_manager
);
689 # if __LT_SPINLOCK_INIT != 0
690 self
->p_resume_count
= (struct pthread_atomic
) __ATOMIC_INITIALIZER
;
692 mgr
->p_alloca_cutoff
= PTHREAD_STACK_MIN
/ 4;
694 mgr
= &__pthread_manager_thread
;
697 __pthread_manager_request
= manager_pipe
[1]; /* writing end */
698 __pthread_manager_reader
= manager_pipe
[0]; /* reading end */
700 /* Start the thread manager */
703 if (__linuxthreads_initial_report_events
!= 0)
704 THREAD_SETMEM (((pthread_descr
) NULL
), p_report_events
,
705 __linuxthreads_initial_report_events
);
706 report_events
= THREAD_GETMEM (((pthread_descr
) NULL
), p_report_events
);
708 if (__linuxthreads_initial_report_events
!= 0)
709 __pthread_initial_thread
.p_report_events
710 = __linuxthreads_initial_report_events
;
711 report_events
= __pthread_initial_thread
.p_report_events
;
713 if (__builtin_expect (report_events
, 0))
715 /* It's a bit more complicated. We have to report the creation of
716 the manager thread. */
717 int idx
= __td_eventword (TD_CREATE
);
718 uint32_t mask
= __td_eventmask (TD_CREATE
);
722 event_bits
= THREAD_GETMEM_NC (((pthread_descr
) NULL
),
723 p_eventbuf
.eventmask
.event_bits
[idx
]);
725 event_bits
= __pthread_initial_thread
.p_eventbuf
.eventmask
.event_bits
[idx
];
728 if ((mask
& (__pthread_threads_events
.event_bits
[idx
] | event_bits
))
731 __pthread_lock(mgr
->p_lock
, NULL
);
733 #ifdef NEED_SEPARATE_REGISTER_STACK
734 pid
= __clone2(__pthread_manager_event
,
735 (void **) __pthread_manager_thread_bos
,
736 THREAD_MANAGER_STACK_SIZE
,
737 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
,
739 #elif _STACK_GROWS_UP
740 pid
= __clone(__pthread_manager_event
,
741 (void **) __pthread_manager_thread_bos
,
742 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
,
745 pid
= __clone(__pthread_manager_event
,
746 (void **) __pthread_manager_thread_tos
,
747 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
,
753 /* Now fill in the information about the new thread in
754 the newly created thread's data structure. We cannot let
755 the new thread do this since we don't know whether it was
756 already scheduled when we send the event. */
757 mgr
->p_eventbuf
.eventdata
= mgr
;
758 mgr
->p_eventbuf
.eventnum
= TD_CREATE
;
759 __pthread_last_event
= mgr
;
760 mgr
->p_tid
= 2* PTHREAD_THREADS_MAX
+ 1;
763 /* Now call the function which signals the event. */
764 __linuxthreads_create_event ();
767 /* Now restart the thread. */
768 __pthread_unlock(mgr
->p_lock
);
772 if (__builtin_expect (pid
, 0) == 0)
774 #ifdef NEED_SEPARATE_REGISTER_STACK
775 pid
= __clone2(__pthread_manager
, (void **) __pthread_manager_thread_bos
,
776 THREAD_MANAGER_STACK_SIZE
,
777 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
, mgr
);
778 #elif _STACK_GROWS_UP
779 pid
= __clone(__pthread_manager
, (void **) __pthread_manager_thread_bos
,
780 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
, mgr
);
782 pid
= __clone(__pthread_manager
, (void **) __pthread_manager_thread_tos
,
783 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
, mgr
);
786 if (__builtin_expect (pid
, 0) == -1) {
788 _dl_deallocate_tls (tcbp
, true);
790 free(__pthread_manager_thread_bos
);
791 close_not_cancel(manager_pipe
[0]);
792 close_not_cancel(manager_pipe
[1]);
795 mgr
->p_tid
= 2* PTHREAD_THREADS_MAX
+ 1;
797 /* Make gdb aware of new thread manager */
798 if (__builtin_expect (__pthread_threads_debug
, 0) && __pthread_sig_debug
> 0)
800 raise(__pthread_sig_debug
);
801 /* We suspend ourself and gdb will wake us up when it is
802 ready to handle us. */
803 __pthread_wait_for_restart_signal(thread_self());
805 /* Synchronize debugging of the thread manager */
806 request
.req_kind
= REQ_DEBUG
;
807 TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request
,
808 (char *) &request
, sizeof(request
)));
812 /* Thread creation */
814 int __pthread_create_2_1(pthread_t
*thread
, const pthread_attr_t
*attr
,
815 void * (*start_routine
)(void *), void *arg
)
817 pthread_descr self
= thread_self();
818 struct pthread_request request
;
820 if (__builtin_expect (__pthread_manager_request
, 0) < 0) {
821 if (__pthread_initialize_manager() < 0) return EAGAIN
;
823 request
.req_thread
= self
;
824 request
.req_kind
= REQ_CREATE
;
825 request
.req_args
.create
.attr
= attr
;
826 request
.req_args
.create
.fn
= start_routine
;
827 request
.req_args
.create
.arg
= arg
;
828 sigprocmask(SIG_SETMASK
, (const sigset_t
*) NULL
,
829 &request
.req_args
.create
.mask
);
830 TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request
,
831 (char *) &request
, sizeof(request
)));
833 retval
= THREAD_GETMEM(self
, p_retcode
);
834 if (__builtin_expect (retval
, 0) == 0)
835 *thread
= (pthread_t
) THREAD_GETMEM(self
, p_retval
);
839 versioned_symbol (libpthread
, __pthread_create_2_1
, pthread_create
, GLIBC_2_1
);
841 #if SHLIB_COMPAT (libpthread, GLIBC_2_0, GLIBC_2_1)
843 int __pthread_create_2_0(pthread_t
*thread
, const pthread_attr_t
*attr
,
844 void * (*start_routine
)(void *), void *arg
)
846 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
847 the old size and access to the new members might crash the program.
848 We convert the struct now. */
849 pthread_attr_t new_attr
;
853 size_t ps
= __getpagesize ();
855 memcpy (&new_attr
, attr
,
856 (size_t) &(((pthread_attr_t
*)NULL
)->__guardsize
));
857 new_attr
.__guardsize
= ps
;
858 new_attr
.__stackaddr_set
= 0;
859 new_attr
.__stackaddr
= NULL
;
860 new_attr
.__stacksize
= STACK_SIZE
- ps
;
863 return __pthread_create_2_1 (thread
, attr
, start_routine
, arg
);
865 compat_symbol (libpthread
, __pthread_create_2_0
, pthread_create
, GLIBC_2_0
);
868 /* Simple operations on thread identifiers */
870 pthread_descr
__pthread_thread_self(void)
872 return thread_self();
875 pthread_t
__pthread_self(void)
877 pthread_descr self
= thread_self();
878 return THREAD_GETMEM(self
, p_tid
);
880 strong_alias (__pthread_self
, pthread_self
);
882 int __pthread_equal(pthread_t thread1
, pthread_t thread2
)
884 return thread1
== thread2
;
886 strong_alias (__pthread_equal
, pthread_equal
);
888 /* Helper function for thread_self in the case of user-provided stacks */
892 pthread_descr
__pthread_find_self(void)
894 char * sp
= CURRENT_STACK_FRAME
;
897 /* __pthread_handles[0] is the initial thread, __pthread_handles[1] is
898 the manager threads handled specially in thread_self(), so start at 2 */
899 h
= __pthread_handles
+ 2;
900 # ifdef _STACK_GROWS_UP
901 while (! (sp
>= (char *) h
->h_descr
&& sp
< h
->h_descr
->p_guardaddr
)) h
++;
903 while (! (sp
<= (char *) h
->h_descr
&& sp
>= h
->h_bottom
)) h
++;
910 pthread_descr
__pthread_self_stack(void)
912 char *sp
= CURRENT_STACK_FRAME
;
915 if (sp
>= __pthread_manager_thread_bos
&& sp
< __pthread_manager_thread_tos
)
916 return manager_thread
;
917 h
= __pthread_handles
+ 2;
919 # ifdef _STACK_GROWS_UP
920 while (h
->h_descr
== NULL
921 || ! (sp
>= h
->h_descr
->p_stackaddr
&& sp
< h
->h_descr
->p_guardaddr
))
924 while (h
->h_descr
== NULL
925 || ! (sp
<= (char *) h
->h_descr
->p_stackaddr
&& sp
>= h
->h_bottom
))
929 # ifdef _STACK_GROWS_UP
930 while (! (sp
>= (char *) h
->h_descr
&& sp
< h
->h_descr
->p_guardaddr
))
933 while (! (sp
<= (char *) h
->h_descr
&& sp
>= h
->h_bottom
))
942 /* Thread scheduling */
944 int __pthread_setschedparam(pthread_t thread
, int policy
,
945 const struct sched_param
*param
)
947 pthread_handle handle
= thread_handle(thread
);
950 __pthread_lock(&handle
->h_lock
, NULL
);
951 if (__builtin_expect (invalid_handle(handle
, thread
), 0)) {
952 __pthread_unlock(&handle
->h_lock
);
955 th
= handle
->h_descr
;
956 if (__builtin_expect (__sched_setscheduler(th
->p_pid
, policy
, param
) == -1,
958 __pthread_unlock(&handle
->h_lock
);
961 th
->p_priority
= policy
== SCHED_OTHER
? 0 : param
->sched_priority
;
962 __pthread_unlock(&handle
->h_lock
);
963 if (__pthread_manager_request
>= 0)
964 __pthread_manager_adjust_prio(th
->p_priority
);
967 strong_alias (__pthread_setschedparam
, pthread_setschedparam
);
969 int __pthread_getschedparam(pthread_t thread
, int *policy
,
970 struct sched_param
*param
)
972 pthread_handle handle
= thread_handle(thread
);
975 __pthread_lock(&handle
->h_lock
, NULL
);
976 if (__builtin_expect (invalid_handle(handle
, thread
), 0)) {
977 __pthread_unlock(&handle
->h_lock
);
980 pid
= handle
->h_descr
->p_pid
;
981 __pthread_unlock(&handle
->h_lock
);
982 pol
= __sched_getscheduler(pid
);
983 if (__builtin_expect (pol
, 0) == -1) return errno
;
984 if (__sched_getparam(pid
, param
) == -1) return errno
;
988 strong_alias (__pthread_getschedparam
, pthread_getschedparam
);
990 int __pthread_yield (void)
992 /* For now this is equivalent with the POSIX call. */
993 return sched_yield ();
995 weak_alias (__pthread_yield
, pthread_yield
)
997 /* Process-wide exit() request */
999 static void pthread_onexit_process(int retcode
, void *arg
)
1001 if (__builtin_expect (__pthread_manager_request
, 0) >= 0) {
1002 struct pthread_request request
;
1003 pthread_descr self
= thread_self();
1005 request
.req_thread
= self
;
1006 request
.req_kind
= REQ_PROCESS_EXIT
;
1007 request
.req_args
.exit
.code
= retcode
;
1008 TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request
,
1009 (char *) &request
, sizeof(request
)));
1011 /* Main thread should accumulate times for thread manager and its
1012 children, so that timings for main thread account for all threads. */
1013 if (self
== __pthread_main_thread
)
1016 waitpid(manager_thread
->p_pid
, NULL
, __WCLONE
);
1018 waitpid(__pthread_manager_thread
.p_pid
, NULL
, __WCLONE
);
1020 /* Since all threads have been asynchronously terminated
1021 (possibly holding locks), free cannot be used any more.
1022 For mtrace, we'd like to print something though. */
1024 tcbhead_t *tcbp = (tcbhead_t *) manager_thread;
1026 tcbp = (tcbhead_t) ((char *) tcbp + TLS_PRE_TCB_SIZE);
1028 _dl_deallocate_tls (tcbp, true);
1030 free (__pthread_manager_thread_bos); */
1031 __pthread_manager_thread_bos
= __pthread_manager_thread_tos
= NULL
;
1036 #ifndef HAVE_Z_NODELETE
1037 static int __pthread_atexit_retcode
;
1039 static void pthread_atexit_process(void *arg
, int retcode
)
1041 pthread_onexit_process (retcode
?: __pthread_atexit_retcode
, arg
);
1044 static void pthread_atexit_retcode(void *arg
, int retcode
)
1046 __pthread_atexit_retcode
= retcode
;
1050 /* The handler for the RESTART signal just records the signal received
1051 in the thread descriptor, and optionally performs a siglongjmp
1052 (for pthread_cond_timedwait). */
1054 static void pthread_handle_sigrestart(int sig
)
1056 pthread_descr self
= check_thread_self();
1057 THREAD_SETMEM(self
, p_signal
, sig
);
1058 if (THREAD_GETMEM(self
, p_signal_jmp
) != NULL
)
1059 siglongjmp(*THREAD_GETMEM(self
, p_signal_jmp
), 1);
1062 /* The handler for the CANCEL signal checks for cancellation
1063 (in asynchronous mode), for process-wide exit and exec requests.
1064 For the thread manager thread, redirect the signal to
1065 __pthread_manager_sighandler. */
1067 static void pthread_handle_sigcancel(int sig
)
1069 pthread_descr self
= check_thread_self();
1070 sigjmp_buf
* jmpbuf
;
1072 if (self
== manager_thread
)
1074 __pthread_manager_sighandler(sig
);
1077 if (__builtin_expect (__pthread_exit_requested
, 0)) {
1078 /* Main thread should accumulate times for thread manager and its
1079 children, so that timings for main thread account for all threads. */
1080 if (self
== __pthread_main_thread
) {
1082 waitpid(manager_thread
->p_pid
, NULL
, __WCLONE
);
1084 waitpid(__pthread_manager_thread
.p_pid
, NULL
, __WCLONE
);
1087 _exit(__pthread_exit_code
);
1089 if (__builtin_expect (THREAD_GETMEM(self
, p_canceled
), 0)
1090 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
) {
1091 if (THREAD_GETMEM(self
, p_canceltype
) == PTHREAD_CANCEL_ASYNCHRONOUS
)
1092 __pthread_do_exit(PTHREAD_CANCELED
, CURRENT_STACK_FRAME
);
1093 jmpbuf
= THREAD_GETMEM(self
, p_cancel_jmp
);
1094 if (jmpbuf
!= NULL
) {
1095 THREAD_SETMEM(self
, p_cancel_jmp
, NULL
);
1096 siglongjmp(*jmpbuf
, 1);
1101 /* Handler for the DEBUG signal.
1102 The debugging strategy is as follows:
1103 On reception of a REQ_DEBUG request (sent by new threads created to
1104 the thread manager under debugging mode), the thread manager throws
1105 __pthread_sig_debug to itself. The debugger (if active) intercepts
1106 this signal, takes into account new threads and continue execution
1107 of the thread manager by propagating the signal because it doesn't
1108 know what it is specifically done for. In the current implementation,
1109 the thread manager simply discards it. */
1111 static void pthread_handle_sigdebug(int sig
)
1116 /* Reset the state of the thread machinery after a fork().
1117 Close the pipe used for requests and set the main thread to the forked
1119 Notice that we can't free the stack segments, as the forked thread
1120 may hold pointers into them. */
1122 void __pthread_reset_main_thread(void)
1124 pthread_descr self
= thread_self();
1126 if (__pthread_manager_request
!= -1) {
1127 /* Free the thread manager stack */
1128 free(__pthread_manager_thread_bos
);
1129 __pthread_manager_thread_bos
= __pthread_manager_thread_tos
= NULL
;
1130 /* Close the two ends of the pipe */
1131 close_not_cancel(__pthread_manager_request
);
1132 close_not_cancel(__pthread_manager_reader
);
1133 __pthread_manager_request
= __pthread_manager_reader
= -1;
1136 /* Update the pid of the main thread */
1137 THREAD_SETMEM(self
, p_pid
, __getpid());
1138 /* Make the forked thread the main thread */
1139 __pthread_main_thread
= self
;
1140 THREAD_SETMEM(self
, p_nextlive
, self
);
1141 THREAD_SETMEM(self
, p_prevlive
, self
);
1142 #if !(USE_TLS && HAVE___THREAD)
1143 /* Now this thread modifies the global variables. */
1144 THREAD_SETMEM(self
, p_errnop
, &_errno
);
1145 THREAD_SETMEM(self
, p_h_errnop
, &_h_errno
);
1146 THREAD_SETMEM(self
, p_resp
, &_res
);
1149 #ifndef FLOATING_STACKS
1150 /* This is to undo the setrlimit call in __pthread_init_max_stacksize.
1151 XXX This can be wrong if the user set the limit during the run. */
1153 struct rlimit limit
;
1154 if (getrlimit (RLIMIT_STACK
, &limit
) == 0
1155 && limit
.rlim_cur
!= limit
.rlim_max
)
1157 limit
.rlim_cur
= limit
.rlim_max
;
1158 setrlimit(RLIMIT_STACK
, &limit
);
1164 /* Process-wide exec() request */
1166 void __pthread_kill_other_threads_np(void)
1168 struct sigaction sa
;
1169 /* Terminate all other threads and thread manager */
1170 pthread_onexit_process(0, NULL
);
1171 /* Make current thread the main thread in case the calling thread
1172 changes its mind, does not exec(), and creates new threads instead. */
1173 __pthread_reset_main_thread();
1175 /* Reset the signal handlers behaviour for the signals the
1176 implementation uses since this would be passed to the new
1178 sigemptyset(&sa
.sa_mask
);
1180 sa
.sa_handler
= SIG_DFL
;
1181 __libc_sigaction(__pthread_sig_restart
, &sa
, NULL
);
1182 __libc_sigaction(__pthread_sig_cancel
, &sa
, NULL
);
1183 if (__pthread_sig_debug
> 0)
1184 __libc_sigaction(__pthread_sig_debug
, &sa
, NULL
);
1186 weak_alias (__pthread_kill_other_threads_np
, pthread_kill_other_threads_np
)
1188 /* Concurrency symbol level. */
1189 static int current_level
;
1191 int __pthread_setconcurrency(int level
)
1193 /* We don't do anything unless we have found a useful interpretation. */
1194 current_level
= level
;
1197 weak_alias (__pthread_setconcurrency
, pthread_setconcurrency
)
1199 int __pthread_getconcurrency(void)
1201 return current_level
;
1203 weak_alias (__pthread_getconcurrency
, pthread_getconcurrency
)
1205 /* Primitives for controlling thread execution */
1207 void __pthread_wait_for_restart_signal(pthread_descr self
)
1211 sigprocmask(SIG_SETMASK
, NULL
, &mask
); /* Get current signal mask */
1212 sigdelset(&mask
, __pthread_sig_restart
); /* Unblock the restart signal */
1213 THREAD_SETMEM(self
, p_signal
, 0);
1215 __pthread_sigsuspend(&mask
); /* Wait for signal. Must not be a
1216 cancellation point. */
1217 } while (THREAD_GETMEM(self
, p_signal
) !=__pthread_sig_restart
);
1219 READ_MEMORY_BARRIER(); /* See comment in __pthread_restart_new */
1222 #if !__ASSUME_REALTIME_SIGNALS
1223 /* The _old variants are for 2.0 and early 2.1 kernels which don't have RT
1225 On these kernels, we use SIGUSR1 and SIGUSR2 for restart and cancellation.
1226 Since the restart signal does not queue, we use an atomic counter to create
1227 queuing semantics. This is needed to resolve a rare race condition in
1228 pthread_cond_timedwait_relative. */
1230 void __pthread_restart_old(pthread_descr th
)
1232 if (atomic_increment(&th
->p_resume_count
) == -1)
1233 kill(th
->p_pid
, __pthread_sig_restart
);
1236 void __pthread_suspend_old(pthread_descr self
)
1238 if (atomic_decrement(&self
->p_resume_count
) <= 0)
1239 __pthread_wait_for_restart_signal(self
);
1243 __pthread_timedsuspend_old(pthread_descr self
, const struct timespec
*abstime
)
1245 sigset_t unblock
, initial_mask
;
1246 int was_signalled
= 0;
1249 if (atomic_decrement(&self
->p_resume_count
) == 0) {
1250 /* Set up a longjmp handler for the restart signal, unblock
1251 the signal and sleep. */
1253 if (sigsetjmp(jmpbuf
, 1) == 0) {
1254 THREAD_SETMEM(self
, p_signal_jmp
, &jmpbuf
);
1255 THREAD_SETMEM(self
, p_signal
, 0);
1256 /* Unblock the restart signal */
1257 sigemptyset(&unblock
);
1258 sigaddset(&unblock
, __pthread_sig_restart
);
1259 sigprocmask(SIG_UNBLOCK
, &unblock
, &initial_mask
);
1263 struct timespec reltime
;
1265 /* Compute a time offset relative to now. */
1266 __gettimeofday (&now
, NULL
);
1267 reltime
.tv_nsec
= abstime
->tv_nsec
- now
.tv_usec
* 1000;
1268 reltime
.tv_sec
= abstime
->tv_sec
- now
.tv_sec
;
1269 if (reltime
.tv_nsec
< 0) {
1270 reltime
.tv_nsec
+= 1000000000;
1271 reltime
.tv_sec
-= 1;
1274 /* Sleep for the required duration. If woken by a signal,
1275 resume waiting as required by Single Unix Specification. */
1276 if (reltime
.tv_sec
< 0 || __libc_nanosleep(&reltime
, NULL
) == 0)
1280 /* Block the restart signal again */
1281 sigprocmask(SIG_SETMASK
, &initial_mask
, NULL
);
1286 THREAD_SETMEM(self
, p_signal_jmp
, NULL
);
1289 /* Now was_signalled is true if we exited the above code
1290 due to the delivery of a restart signal. In that case,
1291 we know we have been dequeued and resumed and that the
1292 resume count is balanced. Otherwise, there are some
1293 cases to consider. First, try to bump up the resume count
1294 back to zero. If it goes to 1, it means restart() was
1295 invoked on this thread. The signal must be consumed
1296 and the count bumped down and everything is cool. We
1297 can return a 1 to the caller.
1298 Otherwise, no restart was delivered yet, so a potential
1299 race exists; we return a 0 to the caller which must deal
1300 with this race in an appropriate way; for example by
1301 atomically removing the thread from consideration for a
1302 wakeup---if such a thing fails, it means a restart is
1305 if (!was_signalled
) {
1306 if (atomic_increment(&self
->p_resume_count
) != -1) {
1307 __pthread_wait_for_restart_signal(self
);
1308 atomic_decrement(&self
->p_resume_count
); /* should be zero now! */
1309 /* woke spontaneously and consumed restart signal */
1312 /* woke spontaneously but did not consume restart---caller must resolve */
1315 /* woken due to restart signal */
1318 #endif /* __ASSUME_REALTIME_SIGNALS */
1320 void __pthread_restart_new(pthread_descr th
)
1322 /* The barrier is proabably not needed, in which case it still documents
1323 our assumptions. The intent is to commit previous writes to shared
1324 memory so the woken thread will have a consistent view. Complementary
1325 read barriers are present to the suspend functions. */
1326 WRITE_MEMORY_BARRIER();
1327 kill(th
->p_pid
, __pthread_sig_restart
);
1330 /* There is no __pthread_suspend_new because it would just
1331 be a wasteful wrapper for __pthread_wait_for_restart_signal */
1334 __pthread_timedsuspend_new(pthread_descr self
, const struct timespec
*abstime
)
1336 sigset_t unblock
, initial_mask
;
1337 int was_signalled
= 0;
1340 if (sigsetjmp(jmpbuf
, 1) == 0) {
1341 THREAD_SETMEM(self
, p_signal_jmp
, &jmpbuf
);
1342 THREAD_SETMEM(self
, p_signal
, 0);
1343 /* Unblock the restart signal */
1344 sigemptyset(&unblock
);
1345 sigaddset(&unblock
, __pthread_sig_restart
);
1346 sigprocmask(SIG_UNBLOCK
, &unblock
, &initial_mask
);
1350 struct timespec reltime
;
1352 /* Compute a time offset relative to now. */
1353 __gettimeofday (&now
, NULL
);
1354 reltime
.tv_nsec
= abstime
->tv_nsec
- now
.tv_usec
* 1000;
1355 reltime
.tv_sec
= abstime
->tv_sec
- now
.tv_sec
;
1356 if (reltime
.tv_nsec
< 0) {
1357 reltime
.tv_nsec
+= 1000000000;
1358 reltime
.tv_sec
-= 1;
1361 /* Sleep for the required duration. If woken by a signal,
1362 resume waiting as required by Single Unix Specification. */
1363 if (reltime
.tv_sec
< 0 || __libc_nanosleep(&reltime
, NULL
) == 0)
1367 /* Block the restart signal again */
1368 sigprocmask(SIG_SETMASK
, &initial_mask
, NULL
);
1373 THREAD_SETMEM(self
, p_signal_jmp
, NULL
);
1375 /* Now was_signalled is true if we exited the above code
1376 due to the delivery of a restart signal. In that case,
1377 everything is cool. We have been removed from whatever
1378 we were waiting on by the other thread, and consumed its signal.
1380 Otherwise we this thread woke up spontaneously, or due to a signal other
1381 than restart. This is an ambiguous case that must be resolved by
1382 the caller; the thread is still eligible for a restart wakeup
1383 so there is a race. */
1385 READ_MEMORY_BARRIER(); /* See comment in __pthread_restart_new */
1386 return was_signalled
;
1395 void __pthread_message(const char * fmt
, ...)
1399 sprintf(buffer
, "%05d : ", __getpid());
1400 va_start(args
, fmt
);
1401 vsnprintf(buffer
+ 8, sizeof(buffer
) - 8, fmt
, args
);
1403 TEMP_FAILURE_RETRY(write_not_cancel(2, buffer
, strlen(buffer
)));