Revert "fcntl.h: fix a missing `struct timespec` definition"
[uclibc-ng.git] / libpthread / linuxthreads / pthread.c
blobd02da6b6c2125e53a8945d9a613879239ae944f4
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. */
15 /* Thread creation, initialization, and basic low-level routines */
17 #include <errno.h>
18 #include <netdb.h> /* for h_errno */
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <sys/wait.h>
26 #include <sys/resource.h>
27 #include "pthread.h"
28 #include "internals.h"
29 #include "spinlock.h"
30 #include "restart.h"
31 #include "debug.h" /* added to linuxthreads -StS */
34 /* Mods for uClibc: Some includes */
35 #include <signal.h>
36 #include <sys/types.h>
37 #include <sys/syscall.h>
39 /* These variables are used by the setup code. */
40 extern int _errno;
41 extern int _h_errno;
44 /* Descriptor of the initial thread */
46 struct _pthread_descr_struct __pthread_initial_thread = {
47 &__pthread_initial_thread, /* pthread_descr p_nextlive */
48 &__pthread_initial_thread, /* pthread_descr p_prevlive */
49 NULL, /* pthread_descr p_nextwaiting */
50 NULL, /* pthread_descr p_nextlock */
51 PTHREAD_THREADS_MAX, /* pthread_t p_tid */
52 0, /* int p_pid */
53 0, /* int p_priority */
54 &__pthread_handles[0].h_lock, /* struct _pthread_fastlock * p_lock */
55 0, /* int p_signal */
56 NULL, /* sigjmp_buf * p_signal_buf */
57 NULL, /* sigjmp_buf * p_cancel_buf */
58 0, /* char p_terminated */
59 0, /* char p_detached */
60 0, /* char p_exited */
61 NULL, /* void * p_retval */
62 0, /* int p_retval */
63 NULL, /* pthread_descr p_joining */
64 NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
65 0, /* char p_cancelstate */
66 0, /* char p_canceltype */
67 0, /* char p_canceled */
68 &_errno, /* int *p_errnop */
69 0, /* int p_errno */
70 &_h_errno, /* int *p_h_errnop */
71 0, /* int p_h_errno */
72 NULL, /* char * p_in_sighandler */
73 0, /* char p_sigwaiting */
74 PTHREAD_START_ARGS_INITIALIZER, /* struct pthread_start_args p_start_args */
75 {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
76 {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
77 0, /* int p_userstack */
78 NULL, /* void * p_guardaddr */
79 0, /* size_t p_guardsize */
80 &__pthread_initial_thread, /* pthread_descr p_self */
81 0, /* Always index 0 */
82 0, /* int p_report_events */
83 {{{0, }}, 0, NULL}, /* td_eventbuf_t p_eventbuf */
84 __ATOMIC_INITIALIZER, /* struct pthread_atomic p_resume_count */
85 0, /* char p_woken_by_cancel */
86 0, /* char p_condvar_avail */
87 0, /* char p_sem_avail */
88 NULL, /* struct pthread_extricate_if *p_extricate */
89 NULL, /* pthread_readlock_info *p_readlock_list; */
90 NULL, /* pthread_readlock_info *p_readlock_free; */
91 0 /* int p_untracked_readlock_count; */
92 #ifdef __UCLIBC_HAS_XLOCALE__
94 &__global_locale_data, /* __locale_t locale; */
95 #endif /* __UCLIBC_HAS_XLOCALE__ */
98 /* Descriptor of the manager thread; none of this is used but the error
99 variables, the p_pid and p_priority fields,
100 and the address for identification. */
101 #define manager_thread (&__pthread_manager_thread)
102 struct _pthread_descr_struct __pthread_manager_thread = {
103 NULL, /* pthread_descr p_nextlive */
104 NULL, /* pthread_descr p_prevlive */
105 NULL, /* pthread_descr p_nextwaiting */
106 NULL, /* pthread_descr p_nextlock */
107 0, /* int p_tid */
108 0, /* int p_pid */
109 0, /* int p_priority */
110 &__pthread_handles[1].h_lock, /* struct _pthread_fastlock * p_lock */
111 0, /* int p_signal */
112 NULL, /* sigjmp_buf * p_signal_buf */
113 NULL, /* sigjmp_buf * p_cancel_buf */
114 0, /* char p_terminated */
115 0, /* char p_detached */
116 0, /* char p_exited */
117 NULL, /* void * p_retval */
118 0, /* int p_retval */
119 NULL, /* pthread_descr p_joining */
120 NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
121 0, /* char p_cancelstate */
122 0, /* char p_canceltype */
123 0, /* char p_canceled */
124 &__pthread_manager_thread.p_errno, /* int *p_errnop */
125 0, /* int p_errno */
126 NULL, /* int *p_h_errnop */
127 0, /* int p_h_errno */
128 NULL, /* char * p_in_sighandler */
129 0, /* char p_sigwaiting */
130 PTHREAD_START_ARGS_INITIALIZER, /* struct pthread_start_args p_start_args */
131 {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
132 {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
133 0, /* int p_userstack */
134 NULL, /* void * p_guardaddr */
135 0, /* size_t p_guardsize */
136 &__pthread_manager_thread, /* pthread_descr p_self */
137 1, /* Always index 1 */
138 0, /* int p_report_events */
139 {{{0, }}, 0, NULL}, /* td_eventbuf_t p_eventbuf */
140 __ATOMIC_INITIALIZER, /* struct pthread_atomic p_resume_count */
141 0, /* char p_woken_by_cancel */
142 0, /* char p_condvar_avail */
143 0, /* char p_sem_avail */
144 NULL, /* struct pthread_extricate_if *p_extricate */
145 NULL, /* pthread_readlock_info *p_readlock_list; */
146 NULL, /* pthread_readlock_info *p_readlock_free; */
147 0 /* int p_untracked_readlock_count; */
148 #ifdef __UCLIBC_HAS_XLOCALE__
150 &__global_locale_data, /* __locale_t locale; */
151 #endif /* __UCLIBC_HAS_XLOCALE__ */
154 /* Pointer to the main thread (the father of the thread manager thread) */
155 /* Originally, this is the initial thread, but this changes after fork() */
157 pthread_descr __pthread_main_thread = &__pthread_initial_thread;
159 /* Limit between the stack of the initial thread (above) and the
160 stacks of other threads (below). Aligned on a STACK_SIZE boundary. */
162 char *__pthread_initial_thread_bos = NULL;
164 #ifndef __ARCH_USE_MMU__
165 /* See nommu notes in internals.h and pthread_initialize() below. */
166 char *__pthread_initial_thread_tos = NULL;
167 char *__pthread_initial_thread_mid = NULL;
168 #endif /* __ARCH_USE_MMU__ */
170 /* File descriptor for sending requests to the thread manager. */
171 /* Initially -1, meaning that the thread manager is not running. */
173 int __pthread_manager_request = -1;
175 /* Other end of the pipe for sending requests to the thread manager. */
177 int __pthread_manager_reader;
179 /* Limits of the thread manager stack */
181 char *__pthread_manager_thread_bos = NULL;
182 char *__pthread_manager_thread_tos = NULL;
184 /* For process-wide exit() */
186 int __pthread_exit_requested = 0;
187 int __pthread_exit_code = 0;
189 /* Communicate relevant LinuxThreads constants to gdb */
191 const int __pthread_threads_max = PTHREAD_THREADS_MAX;
192 const int __pthread_sizeof_handle = sizeof(struct pthread_handle_struct);
193 const int __pthread_offsetof_descr = offsetof(struct pthread_handle_struct, h_descr);
194 const int __pthread_offsetof_pid = offsetof(struct _pthread_descr_struct,
195 p_pid);
196 const int __linuxthreads_pthread_sizeof_descr
197 = sizeof(struct _pthread_descr_struct);
199 const int __linuxthreads_initial_report_events;
201 const char __linuxthreads_version[] = VERSION;
203 /* Forward declarations */
204 static void pthread_onexit_process(int retcode, void *arg);
205 static void pthread_handle_sigcancel(int sig);
206 static void pthread_handle_sigrestart(int sig);
207 static void pthread_handle_sigdebug(int sig);
208 int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime);
210 /* Signal numbers used for the communication.
211 In these variables we keep track of the used variables. If the
212 platform does not support any real-time signals we will define the
213 values to some unreasonable value which will signal failing of all
214 the functions below. */
215 #ifndef __NR_rt_sigaction
216 int __pthread_sig_restart = SIGUSR1;
217 int __pthread_sig_cancel = SIGUSR2;
218 int __pthread_sig_debug;
219 #else
221 #if __SIGRTMAX - __SIGRTMIN >= 3
222 int __pthread_sig_restart = __SIGRTMIN;
223 int __pthread_sig_cancel = __SIGRTMIN + 1;
224 int __pthread_sig_debug = __SIGRTMIN + 2;
225 void (*__pthread_restart)(pthread_descr) = __pthread_restart_new;
226 void (*__pthread_suspend)(pthread_descr) = __pthread_wait_for_restart_signal;
227 int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *) = __pthread_timedsuspend_new;
228 #else
229 int __pthread_sig_restart = SIGUSR1;
230 int __pthread_sig_cancel = SIGUSR2;
231 int __pthread_sig_debug;
232 void (*__pthread_restart)(pthread_descr) = __pthread_restart_old;
233 void (*__pthread_suspend)(pthread_descr) = __pthread_suspend_old;
234 int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *) = __pthread_timedsuspend_old;
236 #endif
237 #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. */
246 static void pthread_initialize(void) __attribute__((constructor));
248 libpthread_hidden_proto(pthread_attr_destroy)
249 libpthread_hidden_proto(pthread_attr_init)
250 libpthread_hidden_proto(pthread_attr_getdetachstate)
251 libpthread_hidden_proto(pthread_attr_setdetachstate)
252 libpthread_hidden_proto(pthread_attr_getinheritsched)
253 libpthread_hidden_proto(pthread_attr_setinheritsched)
254 libpthread_hidden_proto(pthread_attr_setschedparam)
255 libpthread_hidden_proto(pthread_attr_getschedparam)
256 libpthread_hidden_proto(pthread_attr_getschedpolicy)
257 libpthread_hidden_proto(pthread_attr_setschedpolicy)
258 libpthread_hidden_proto(pthread_attr_getscope)
259 libpthread_hidden_proto(pthread_attr_setscope)
261 libpthread_hidden_proto(pthread_exit)
263 libpthread_hidden_proto(pthread_equal)
264 libpthread_hidden_proto(pthread_self)
265 libpthread_hidden_proto(pthread_getschedparam)
266 libpthread_hidden_proto(pthread_setschedparam)
268 libpthread_hidden_proto(pthread_setcancelstate)
269 libpthread_hidden_proto(pthread_setcanceltype)
270 libpthread_hidden_proto(_pthread_cleanup_push_defer)
271 libpthread_hidden_proto(_pthread_cleanup_pop_restore)
273 libpthread_hidden_proto(pthread_cond_broadcast)
274 libpthread_hidden_proto(pthread_cond_destroy)
275 libpthread_hidden_proto(pthread_cond_init)
276 libpthread_hidden_proto(pthread_cond_signal)
277 libpthread_hidden_proto(pthread_cond_wait)
278 libpthread_hidden_proto(pthread_cond_timedwait)
280 libpthread_hidden_proto(pthread_condattr_destroy)
281 libpthread_hidden_proto(pthread_condattr_init)
283 static int *__libc_multiple_threads_ptr;
285 /* Do some minimal initialization which has to be done during the
286 startup of the C library. */
287 void __pthread_initialize_minimal(void)
289 /* If we have special thread_self processing, initialize
290 * that for the main thread now. */
291 #ifdef INIT_THREAD_SELF
292 INIT_THREAD_SELF(&__pthread_initial_thread, 0);
293 #endif
295 __libc_multiple_threads_ptr = __libc_pthread_init ();
299 static void pthread_initialize(void)
301 struct sigaction sa;
302 sigset_t mask;
303 #ifdef __ARCH_USE_MMU__
304 struct rlimit limit;
305 rlim_t max_stack;
306 #endif
308 /* If already done (e.g. by a constructor called earlier!), bail out */
309 if (__pthread_initial_thread_bos != NULL) return;
310 #ifdef TEST_FOR_COMPARE_AND_SWAP
311 /* Test if compare-and-swap is available */
312 __pthread_has_cas = compare_and_swap_is_available();
313 #endif
314 /* For the initial stack, reserve at least STACK_SIZE bytes of stack
315 below the current stack address, and align that on a
316 STACK_SIZE boundary. */
317 __pthread_initial_thread_bos =
318 (char *)(((long)CURRENT_STACK_FRAME - 2 * STACK_SIZE) & ~(STACK_SIZE - 1));
319 /* Update the descriptor for the initial thread. */
320 __pthread_initial_thread.p_pid = getpid();
321 /* If we have special thread_self processing, initialize that for the
322 main thread now. */
323 #ifdef INIT_THREAD_SELF
324 INIT_THREAD_SELF(&__pthread_initial_thread, 0);
325 #endif
326 /* The errno/h_errno variable of the main thread are the global ones. */
327 __pthread_initial_thread.p_errnop = &_errno;
328 __pthread_initial_thread.p_h_errnop = &_h_errno;
330 #ifdef __UCLIBC_HAS_XLOCALE__
331 /* The locale of the main thread is the current locale in use. */
332 __pthread_initial_thread.locale = __curlocale_var;
333 #endif /* __UCLIBC_HAS_XLOCALE__ */
335 { /* uClibc-specific stdio initialization for threads. */
336 FILE *fp;
338 _stdio_user_locking = 0; /* 2 if threading not initialized */
339 for (fp = _stdio_openlist; fp != NULL; fp = fp->__nextopen) {
340 if (fp->__user_locking != 1) {
341 fp->__user_locking = 0;
346 /* Play with the stack size limit to make sure that no stack ever grows
347 beyond STACK_SIZE minus two pages (one page for the thread descriptor
348 immediately beyond, and one page to act as a guard page). */
350 #ifdef __ARCH_USE_MMU__
351 /* We cannot allocate a huge chunk of memory to mmap all thread stacks later
352 * on a non-MMU system. Thus, we don't need the rlimit either. -StS */
353 getrlimit(RLIMIT_STACK, &limit);
354 max_stack = STACK_SIZE - 2 * getpagesize();
355 if (limit.rlim_cur > max_stack) {
356 limit.rlim_cur = max_stack;
357 setrlimit(RLIMIT_STACK, &limit);
359 #else
360 /* For non-MMU, the initial thread stack can reside anywhere in memory.
361 * We don't have a way of knowing where the kernel started things -- top
362 * or bottom (well, that isn't exactly true, but the solution is fairly
363 * complex and error prone). All we can determine here is an address
364 * that lies within that stack. Save that address as a reference so that
365 * as other thread stacks are created, we can adjust the estimated bounds
366 * of the initial thread's stack appropriately.
368 * This checking is handled in NOMMU_INITIAL_THREAD_BOUNDS(), so see that
369 * for a few more details.
371 __pthread_initial_thread_mid = CURRENT_STACK_FRAME;
372 __pthread_initial_thread_tos = (char *) -1;
373 __pthread_initial_thread_bos = (char *) 1; /* set it non-zero so we know we have been here */
374 PDEBUG("initial thread stack bounds: bos=%p, tos=%p\n",
375 __pthread_initial_thread_bos, __pthread_initial_thread_tos);
376 #endif /* __ARCH_USE_MMU__ */
378 /* Setup signal handlers for the initial thread.
379 Since signal handlers are shared between threads, these settings
380 will be inherited by all other threads. */
381 memset(&sa, 0, sizeof(sa));
382 sa.sa_handler = pthread_handle_sigrestart;
383 __libc_sigaction(__pthread_sig_restart, &sa, NULL);
384 sa.sa_handler = pthread_handle_sigcancel;
385 sigaddset(&sa.sa_mask, __pthread_sig_restart);
386 __libc_sigaction(__pthread_sig_cancel, &sa, NULL);
387 if (__pthread_sig_debug > 0) {
388 sa.sa_handler = pthread_handle_sigdebug;
389 __sigemptyset(&sa.sa_mask);
390 __libc_sigaction(__pthread_sig_debug, &sa, NULL);
392 /* Initially, block __pthread_sig_restart. Will be unblocked on demand. */
393 __sigemptyset(&mask);
394 sigaddset(&mask, __pthread_sig_restart);
395 sigprocmask(SIG_BLOCK, &mask, NULL);
396 /* And unblock __pthread_sig_cancel if it has been blocked. */
397 sigdelset(&mask, __pthread_sig_restart);
398 sigaddset(&mask, __pthread_sig_cancel);
399 sigprocmask(SIG_UNBLOCK, &mask, NULL);
400 /* Register an exit function to kill all other threads. */
401 /* Do it early so that user-registered atexit functions are called
402 before pthread_onexit_process. */
403 on_exit(pthread_onexit_process, NULL);
406 void __pthread_initialize(void);
407 void __pthread_initialize(void)
409 pthread_initialize();
412 int __pthread_initialize_manager(void)
414 int manager_pipe[2];
415 int pid;
416 int report_events;
417 struct pthread_request request;
419 *__libc_multiple_threads_ptr = 1;
421 /* If basic initialization not done yet (e.g. we're called from a
422 constructor run before our constructor), do it now */
423 if (__pthread_initial_thread_bos == NULL) pthread_initialize();
424 /* Setup stack for thread manager */
425 __pthread_manager_thread_bos = malloc(THREAD_MANAGER_STACK_SIZE);
426 if (__pthread_manager_thread_bos == NULL) return -1;
427 __pthread_manager_thread_tos =
428 __pthread_manager_thread_bos + THREAD_MANAGER_STACK_SIZE;
430 /* On non-MMU systems we make sure that the initial thread bounds don't overlap
431 * with the manager stack frame */
432 NOMMU_INITIAL_THREAD_BOUNDS(__pthread_manager_thread_tos,__pthread_manager_thread_bos);
433 PDEBUG("manager stack: size=%ld, bos=%p, tos=%p\n", THREAD_MANAGER_STACK_SIZE,
434 __pthread_manager_thread_bos, __pthread_manager_thread_tos);
435 #if 0
436 PDEBUG("initial stack: estimate bos=%p, tos=%p\n",
437 __pthread_initial_thread_bos, __pthread_initial_thread_tos);
438 #endif
440 /* Setup pipe to communicate with thread manager */
441 if (pipe(manager_pipe) == -1) {
442 free(__pthread_manager_thread_bos);
443 return -1;
445 /* Start the thread manager */
446 pid = 0;
447 #if defined(USE_TLS) && USE_TLS
448 if (__linuxthreads_initial_report_events != 0)
449 THREAD_SETMEM (((pthread_descr) NULL), p_report_events,
450 __linuxthreads_initial_report_events);
451 report_events = THREAD_GETMEM (((pthread_descr) NULL), p_report_events);
452 #else
453 if (__linuxthreads_initial_report_events != 0)
454 __pthread_initial_thread.p_report_events
455 = __linuxthreads_initial_report_events;
456 report_events = __pthread_initial_thread.p_report_events;
457 #endif
458 if (__builtin_expect (report_events, 0))
460 /* It's a bit more complicated. We have to report the creation of
461 the manager thread. */
462 int idx = __td_eventword (TD_CREATE);
463 uint32_t mask = __td_eventmask (TD_CREATE);
465 if ((mask & (__pthread_threads_events.event_bits[idx]
466 | __pthread_initial_thread.p_eventbuf.eventmask.event_bits[idx]))
467 != 0)
470 __pthread_lock(__pthread_manager_thread.p_lock, NULL);
472 #ifdef __ia64__
473 pid = __clone2(__pthread_manager_event,
474 (void **) __pthread_manager_thread_tos,
475 THREAD_MANAGER_STACK_SIZE,
476 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
477 (void *)(long)manager_pipe[0]);
478 #else
479 pid = clone(__pthread_manager_event,
480 (void **) __pthread_manager_thread_tos,
481 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
482 (void *)(long)manager_pipe[0]);
483 #endif
485 if (pid != -1)
487 /* Now fill in the information about the new thread in
488 the newly created thread's data structure. We cannot let
489 the new thread do this since we don't know whether it was
490 already scheduled when we send the event. */
491 __pthread_manager_thread.p_eventbuf.eventdata =
492 &__pthread_manager_thread;
493 __pthread_manager_thread.p_eventbuf.eventnum = TD_CREATE;
494 __pthread_last_event = &__pthread_manager_thread;
495 __pthread_manager_thread.p_tid = 2* PTHREAD_THREADS_MAX + 1;
496 __pthread_manager_thread.p_pid = pid;
498 /* Now call the function which signals the event. */
499 __linuxthreads_create_event ();
501 /* Now restart the thread. */
502 __pthread_unlock(__pthread_manager_thread.p_lock);
506 if (pid == 0) {
507 #ifdef __ia64__
508 pid = __clone2(__pthread_manager, (void **) __pthread_manager_thread_tos,
509 THREAD_MANAGER_STACK_SIZE,
510 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
511 (void *)(long)manager_pipe[0]);
512 #else
513 pid = clone(__pthread_manager, (void **) __pthread_manager_thread_tos,
514 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
515 (void *)(long)manager_pipe[0]);
516 #endif
518 if (pid == -1) {
519 free(__pthread_manager_thread_bos);
520 close(manager_pipe[0]);
521 close(manager_pipe[1]);
522 return -1;
524 __pthread_manager_request = manager_pipe[1]; /* writing end */
525 __pthread_manager_reader = manager_pipe[0]; /* reading end */
526 __pthread_manager_thread.p_tid = 2* PTHREAD_THREADS_MAX + 1;
527 __pthread_manager_thread.p_pid = pid;
529 /* Make gdb aware of new thread manager */
530 if (__pthread_threads_debug && __pthread_sig_debug > 0)
532 raise(__pthread_sig_debug);
533 /* We suspend ourself and gdb will wake us up when it is
534 ready to handle us. */
535 __pthread_wait_for_restart_signal(thread_self());
537 /* Synchronize debugging of the thread manager */
538 PDEBUG("send REQ_DEBUG to manager thread\n");
539 request.req_kind = REQ_DEBUG;
540 TEMP_FAILURE_RETRY(write(__pthread_manager_request,
541 (char *) &request, sizeof(request)));
542 return 0;
545 /* Thread creation */
547 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
548 void * (*start_routine)(void *), void *arg)
550 pthread_descr self = thread_self();
551 struct pthread_request request;
552 if (__pthread_manager_request < 0) {
553 if (__pthread_initialize_manager() < 0) return EAGAIN;
555 request.req_thread = self;
556 request.req_kind = REQ_CREATE;
557 request.req_args.create.attr = attr;
558 request.req_args.create.fn = start_routine;
559 request.req_args.create.arg = arg;
560 sigprocmask(SIG_SETMASK, NULL, &request.req_args.create.mask);
561 PDEBUG("write REQ_CREATE to manager thread\n");
562 TEMP_FAILURE_RETRY(write(__pthread_manager_request,
563 (char *) &request, sizeof(request)));
564 PDEBUG("before suspend(self)\n");
565 suspend(self);
566 PDEBUG("after suspend(self)\n");
567 if (THREAD_GETMEM(self, p_retcode) == 0)
568 *thread = (pthread_t) THREAD_GETMEM(self, p_retval);
569 return THREAD_GETMEM(self, p_retcode);
572 /* Simple operations on thread identifiers */
574 pthread_t pthread_self(void)
576 pthread_descr self = thread_self();
577 return THREAD_GETMEM(self, p_tid);
579 libpthread_hidden_def (pthread_self)
581 int pthread_equal(pthread_t thread1, pthread_t thread2)
583 return thread1 == thread2;
585 libpthread_hidden_def (pthread_equal)
587 /* Helper function for thread_self in the case of user-provided stacks */
589 #ifndef THREAD_SELF
591 pthread_descr __pthread_find_self(void)
593 char * sp = CURRENT_STACK_FRAME;
594 pthread_handle h;
596 /* __pthread_handles[0] is the initial thread, __pthread_handles[1] is
597 the manager threads handled specially in thread_self(), so start at 2 */
598 h = __pthread_handles + 2;
599 while (! (sp <= (char *) h->h_descr && sp >= h->h_bottom)) h++;
601 #ifdef DEBUG_PT
602 if (h->h_descr == NULL) {
603 printf("*** %s ERROR descriptor is NULL!!!!! ***\n\n", __FUNCTION__);
604 _exit(1);
606 #endif
608 return h->h_descr;
610 #else
612 static pthread_descr thread_self_stack(void)
614 char *sp = CURRENT_STACK_FRAME;
615 pthread_handle h;
617 if (sp >= __pthread_manager_thread_bos && sp < __pthread_manager_thread_tos)
618 return manager_thread;
619 h = __pthread_handles + 2;
620 # if defined(USE_TLS) && USE_TLS
621 while (h->h_descr == NULL
622 || ! (sp <= (char *) h->h_descr->p_stackaddr && sp >= h->h_bottom))
623 h++;
624 # else
625 while (! (sp <= (char *) h->h_descr && sp >= h->h_bottom))
626 h++;
627 # endif
628 return h->h_descr;
631 #endif
633 /* Thread scheduling */
635 int pthread_setschedparam(pthread_t thread, int policy,
636 const struct sched_param *param)
638 pthread_handle handle = thread_handle(thread);
639 pthread_descr th;
641 __pthread_lock(&handle->h_lock, NULL);
642 if (invalid_handle(handle, thread)) {
643 __pthread_unlock(&handle->h_lock);
644 return ESRCH;
646 th = handle->h_descr;
647 if (sched_setscheduler(th->p_pid, policy, param) == -1) {
648 __pthread_unlock(&handle->h_lock);
649 return errno;
651 th->p_priority = policy == SCHED_OTHER ? 0 : param->sched_priority;
652 __pthread_unlock(&handle->h_lock);
653 if (__pthread_manager_request >= 0)
654 __pthread_manager_adjust_prio(th->p_priority);
655 return 0;
657 libpthread_hidden_def(pthread_setschedparam)
659 int pthread_getschedparam(pthread_t thread, int *policy,
660 struct sched_param *param)
662 pthread_handle handle = thread_handle(thread);
663 int pid, pol;
665 __pthread_lock(&handle->h_lock, NULL);
666 if (invalid_handle(handle, thread)) {
667 __pthread_unlock(&handle->h_lock);
668 return ESRCH;
670 pid = handle->h_descr->p_pid;
671 __pthread_unlock(&handle->h_lock);
672 pol = sched_getscheduler(pid);
673 if (pol == -1) return errno;
674 if (sched_getparam(pid, param) == -1) return errno;
675 *policy = pol;
676 return 0;
678 libpthread_hidden_def(pthread_getschedparam)
680 /* Process-wide exit() request */
682 static void pthread_onexit_process(int retcode, void *arg attribute_unused)
684 struct pthread_request request;
685 pthread_descr self = thread_self();
687 if (__pthread_manager_request >= 0) {
688 request.req_thread = self;
689 request.req_kind = REQ_PROCESS_EXIT;
690 request.req_args.exit.code = retcode;
691 TEMP_FAILURE_RETRY(write(__pthread_manager_request,
692 (char *) &request, sizeof(request)));
693 suspend(self);
694 /* Main thread should accumulate times for thread manager and its
695 children, so that timings for main thread account for all threads. */
696 if (self == __pthread_main_thread) {
697 waitpid(__pthread_manager_thread.p_pid, NULL, __WCLONE);
698 /* Since all threads have been asynchronously terminated
699 * (possibly holding locks), free cannot be used any more. */
700 __pthread_manager_thread_bos = __pthread_manager_thread_tos = NULL;
705 /* The handler for the RESTART signal just records the signal received
706 in the thread descriptor, and optionally performs a siglongjmp
707 (for pthread_cond_timedwait). */
709 static void pthread_handle_sigrestart(int sig)
711 pthread_descr self = thread_self();
712 THREAD_SETMEM(self, p_signal, sig);
713 if (THREAD_GETMEM(self, p_signal_jmp) != NULL)
714 siglongjmp(*THREAD_GETMEM(self, p_signal_jmp), 1);
717 /* The handler for the CANCEL signal checks for cancellation
718 (in asynchronous mode), for process-wide exit and exec requests.
719 For the thread manager thread, redirect the signal to
720 __pthread_manager_sighandler. */
722 static void pthread_handle_sigcancel(int sig)
724 pthread_descr self = thread_self();
725 sigjmp_buf * jmpbuf;
728 if (self == &__pthread_manager_thread)
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)
738 __pthread_manager_sighandler(sig);
739 return;
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
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
761 _exit(__pthread_exit_code);
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);
775 /* Handler for the DEBUG signal.
776 The debugging strategy is as follows:
777 On reception of a REQ_DEBUG request (sent by new threads created to
778 the thread manager under debugging mode), the thread manager throws
779 __pthread_sig_debug to itself. The debugger (if active) intercepts
780 this signal, takes into account new threads and continue execution
781 of the thread manager by propagating the signal because it doesn't
782 know what it is specifically done for. In the current implementation,
783 the thread manager simply discards it. */
785 static void pthread_handle_sigdebug(int sig attribute_unused)
787 /* Nothing */
790 /* Reset the state of the thread machinery after a fork().
791 Close the pipe used for requests and set the main thread to the forked
792 thread.
793 Notice that we can't free the stack segments, as the forked thread
794 may hold pointers into them. */
796 void __pthread_reset_main_thread(void)
798 pthread_descr self = thread_self();
800 if (__pthread_manager_request != -1) {
801 /* Free the thread manager stack */
802 free(__pthread_manager_thread_bos);
803 __pthread_manager_thread_bos = __pthread_manager_thread_tos = NULL;
804 /* Close the two ends of the pipe */
805 close(__pthread_manager_request);
806 close(__pthread_manager_reader);
807 __pthread_manager_request = __pthread_manager_reader = -1;
810 /* Update the pid of the main thread */
811 THREAD_SETMEM(self, p_pid, getpid());
812 /* Make the forked thread the main thread */
813 __pthread_main_thread = self;
814 THREAD_SETMEM(self, p_nextlive, self);
815 THREAD_SETMEM(self, p_prevlive, self);
816 /* Now this thread modifies the global variables. */
817 THREAD_SETMEM(self, p_errnop, &_errno);
818 THREAD_SETMEM(self, p_h_errnop, &_h_errno);
821 /* Process-wide exec() request */
823 void __pthread_kill_other_threads_np(void)
825 struct sigaction sa;
826 /* Terminate all other threads and thread manager */
827 pthread_onexit_process(0, NULL);
828 /* Make current thread the main thread in case the calling thread
829 changes its mind, does not exec(), and creates new threads instead. */
830 __pthread_reset_main_thread();
831 /* Reset the signal handlers behaviour for the signals the
832 implementation uses since this would be passed to the new
833 process. */
834 memset(&sa, 0, sizeof(sa));
835 if (SIG_DFL) /* if it's constant zero, it's already done */
836 sa.sa_handler = SIG_DFL;
837 __libc_sigaction(__pthread_sig_restart, &sa, NULL);
838 __libc_sigaction(__pthread_sig_cancel, &sa, NULL);
839 if (__pthread_sig_debug > 0)
840 __libc_sigaction(__pthread_sig_debug, &sa, NULL);
842 weak_alias (__pthread_kill_other_threads_np, pthread_kill_other_threads_np)
844 /* Concurrency symbol level. */
845 static int current_level;
847 int __pthread_setconcurrency(int level)
849 /* We don't do anything unless we have found a useful interpretation. */
850 current_level = level;
851 return 0;
853 weak_alias (__pthread_setconcurrency, pthread_setconcurrency)
855 int __pthread_getconcurrency(void)
857 return current_level;
859 weak_alias (__pthread_getconcurrency, pthread_getconcurrency)
862 /* Primitives for controlling thread execution */
864 void __pthread_wait_for_restart_signal(pthread_descr self)
866 sigset_t mask;
868 sigprocmask(SIG_SETMASK, NULL, &mask); /* Get current signal mask */
869 sigdelset(&mask, __pthread_sig_restart); /* Unblock the restart signal */
870 THREAD_SETMEM(self, p_signal, 0);
871 do {
872 sigsuspend(&mask); /* Wait for signal */
873 } while (THREAD_GETMEM(self, p_signal) !=__pthread_sig_restart);
875 READ_MEMORY_BARRIER(); /* See comment in __pthread_restart_new */
878 #ifndef __NR_rt_sigaction
879 /* The _old variants are for 2.0 and early 2.1 kernels which don't have RT
880 signals.
881 On these kernels, we use SIGUSR1 and SIGUSR2 for restart and cancellation.
882 Since the restart signal does not queue, we use an atomic counter to create
883 queuing semantics. This is needed to resolve a rare race condition in
884 pthread_cond_timedwait_relative. */
886 void __pthread_restart_old(pthread_descr th)
888 if (atomic_increment(&th->p_resume_count) == -1)
889 kill(th->p_pid, __pthread_sig_restart);
892 void __pthread_suspend_old(pthread_descr self)
894 if (atomic_decrement(&self->p_resume_count) <= 0)
895 __pthread_wait_for_restart_signal(self);
899 __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abstime)
901 sigset_t unblock, initial_mask;
902 int was_signalled = 0;
903 sigjmp_buf jmpbuf;
905 if (atomic_decrement(&self->p_resume_count) == 0) {
906 /* Set up a longjmp handler for the restart signal, unblock
907 the signal and sleep. */
909 if (sigsetjmp(jmpbuf, 1) == 0) {
910 THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
911 THREAD_SETMEM(self, p_signal, 0);
912 /* Unblock the restart signal */
913 __sigemptyset(&unblock);
914 sigaddset(&unblock, __pthread_sig_restart);
915 sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
917 while (1) {
918 struct timeval now;
919 struct timespec reltime;
921 /* Compute a time offset relative to now. */
922 gettimeofday (&now, NULL);
923 reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
924 reltime.tv_sec = abstime->tv_sec - now.tv_sec;
925 if (reltime.tv_nsec < 0) {
926 reltime.tv_nsec += 1000000000;
927 reltime.tv_sec -= 1;
930 /* Sleep for the required duration. If woken by a signal,
931 resume waiting as required by Single Unix Specification. */
932 if (reltime.tv_sec < 0 || nanosleep(&reltime, NULL) == 0)
933 break;
936 /* Block the restart signal again */
937 sigprocmask(SIG_SETMASK, &initial_mask, NULL);
938 was_signalled = 0;
939 } else {
940 was_signalled = 1;
942 THREAD_SETMEM(self, p_signal_jmp, NULL);
945 /* Now was_signalled is true if we exited the above code
946 due to the delivery of a restart signal. In that case,
947 we know we have been dequeued and resumed and that the
948 resume count is balanced. Otherwise, there are some
949 cases to consider. First, try to bump up the resume count
950 back to zero. If it goes to 1, it means restart() was
951 invoked on this thread. The signal must be consumed
952 and the count bumped down and everything is cool. We
953 can return a 1 to the caller.
954 Otherwise, no restart was delivered yet, so a potential
955 race exists; we return a 0 to the caller which must deal
956 with this race in an appropriate way; for example by
957 atomically removing the thread from consideration for a
958 wakeup---if such a thing fails, it means a restart is
959 being delivered. */
961 if (!was_signalled) {
962 if (atomic_increment(&self->p_resume_count) != -1) {
963 __pthread_wait_for_restart_signal(self);
964 atomic_decrement(&self->p_resume_count); /* should be zero now! */
965 /* woke spontaneously and consumed restart signal */
966 return 1;
968 /* woke spontaneously but did not consume restart---caller must resolve */
969 return 0;
971 /* woken due to restart signal */
972 return 1;
974 #endif /* __NR_rt_sigaction */
977 #ifdef __NR_rt_sigaction
978 void __pthread_restart_new(pthread_descr th)
980 /* The barrier is proabably not needed, in which case it still documents
981 our assumptions. The intent is to commit previous writes to shared
982 memory so the woken thread will have a consistent view. Complementary
983 read barriers are present to the suspend functions. */
984 WRITE_MEMORY_BARRIER();
985 kill(th->p_pid, __pthread_sig_restart);
988 int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abstime)
990 sigset_t unblock, initial_mask;
991 int was_signalled = 0;
992 sigjmp_buf jmpbuf;
994 if (sigsetjmp(jmpbuf, 1) == 0) {
995 THREAD_SETMEM(self, p_signal_jmp, &jmpbuf);
996 THREAD_SETMEM(self, p_signal, 0);
997 /* Unblock the restart signal */
998 __sigemptyset(&unblock);
999 sigaddset(&unblock, __pthread_sig_restart);
1000 sigprocmask(SIG_UNBLOCK, &unblock, &initial_mask);
1002 while (1) {
1003 struct timeval now;
1004 struct timespec reltime;
1006 /* Compute a time offset relative to now. */
1007 gettimeofday (&now, NULL);
1008 reltime.tv_nsec = abstime->tv_nsec - now.tv_usec * 1000;
1009 reltime.tv_sec = abstime->tv_sec - now.tv_sec;
1010 if (reltime.tv_nsec < 0) {
1011 reltime.tv_nsec += 1000000000;
1012 reltime.tv_sec -= 1;
1015 /* Sleep for the required duration. If woken by a signal,
1016 resume waiting as required by Single Unix Specification. */
1017 if (reltime.tv_sec < 0 || nanosleep(&reltime, NULL) == 0)
1018 break;
1021 /* Block the restart signal again */
1022 sigprocmask(SIG_SETMASK, &initial_mask, NULL);
1023 was_signalled = 0;
1024 } else {
1025 was_signalled = 1;
1027 THREAD_SETMEM(self, p_signal_jmp, NULL);
1029 /* Now was_signalled is true if we exited the above code
1030 due to the delivery of a restart signal. In that case,
1031 everything is cool. We have been removed from whatever
1032 we were waiting on by the other thread, and consumed its signal.
1034 Otherwise we this thread woke up spontaneously, or due to a signal other
1035 than restart. This is an ambiguous case that must be resolved by
1036 the caller; the thread is still eligible for a restart wakeup
1037 so there is a race. */
1039 READ_MEMORY_BARRIER(); /* See comment in __pthread_restart_new */
1040 return was_signalled;
1042 #endif
1044 /* Debugging aid */
1046 #ifdef DEBUG_PT
1047 #include <stdarg.h>
1049 void __pthread_message(char * fmt, ...)
1051 char buffer[1024];
1052 va_list args;
1053 sprintf(buffer, "%05d : ", getpid());
1054 va_start(args, fmt);
1055 vsnprintf(buffer + 8, sizeof(buffer) - 8, fmt, args);
1056 va_end(args);
1057 TEMP_FAILURE_RETRY(write(2, buffer, strlen(buffer)));
1060 #endif
1063 #ifndef __PIC__
1064 /* We need a hook to force the cancellation wrappers to be linked in when
1065 static libpthread is used. */
1066 extern const char __pthread_provide_wrappers;
1067 static const char *const __pthread_require_wrappers =
1068 &__pthread_provide_wrappers;
1069 #endif