Update.
[glibc.git] / linuxthreads / pthread.c
blobbddec8c757a9b246f035315d91c0ddc8c3b05089
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 <stddef.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #ifdef __i386__
25 # include <ucontext.h>
26 #endif
27 #include <sys/wait.h>
28 #include <sys/resource.h>
29 #include "pthread.h"
30 #include "internals.h"
31 #include "spinlock.h"
32 #include "restart.h"
34 /* Descriptor of the initial thread */
36 struct _pthread_descr_struct __pthread_initial_thread = {
37 &__pthread_initial_thread, /* pthread_descr p_nextlive */
38 &__pthread_initial_thread, /* pthread_descr p_prevlive */
39 NULL, /* pthread_descr p_nextwaiting */
40 NULL, /* pthread_descr p_nextlock */
41 PTHREAD_THREADS_MAX, /* pthread_t p_tid */
42 0, /* int p_pid */
43 0, /* int p_priority */
44 &__pthread_handles[0].h_lock, /* struct _pthread_fastlock * p_lock */
45 0, /* int p_signal */
46 NULL, /* sigjmp_buf * p_signal_buf */
47 NULL, /* sigjmp_buf * p_cancel_buf */
48 0, /* char p_terminated */
49 0, /* char p_detached */
50 0, /* char p_exited */
51 NULL, /* void * p_retval */
52 0, /* int p_retval */
53 NULL, /* pthread_descr p_joining */
54 NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
55 0, /* char p_cancelstate */
56 0, /* char p_canceltype */
57 0, /* char p_canceled */
58 NULL, /* int *p_errnop */
59 0, /* int p_errno */
60 NULL, /* int *p_h_errnop */
61 0, /* int p_h_errno */
62 NULL, /* char * p_in_sighandler */
63 0, /* char p_sigwaiting */
64 PTHREAD_START_ARGS_INITIALIZER(NULL),
65 /* struct pthread_start_args p_start_args */
66 {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
67 {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
68 0, /* int p_userstack */
69 NULL, /* void * p_guardaddr */
70 0, /* size_t p_guardsize */
71 &__pthread_initial_thread, /* pthread_descr p_self */
72 0 /* Always index 0 */
75 /* Descriptor of the manager thread; none of this is used but the error
76 variables, the p_pid and p_priority fields,
77 and the address for identification. */
79 struct _pthread_descr_struct __pthread_manager_thread = {
80 NULL, /* pthread_descr p_nextlive */
81 NULL, /* pthread_descr p_prevlive */
82 NULL, /* pthread_descr p_nextwaiting */
83 NULL, /* pthread_descr p_nextlock */
84 0, /* int p_tid */
85 0, /* int p_pid */
86 0, /* int p_priority */
87 &__pthread_handles[1].h_lock, /* struct _pthread_fastlock * p_lock */
88 0, /* int p_signal */
89 NULL, /* sigjmp_buf * p_signal_buf */
90 NULL, /* sigjmp_buf * p_cancel_buf */
91 0, /* char p_terminated */
92 0, /* char p_detached */
93 0, /* char p_exited */
94 NULL, /* void * p_retval */
95 0, /* int p_retval */
96 NULL, /* pthread_descr p_joining */
97 NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
98 0, /* char p_cancelstate */
99 0, /* char p_canceltype */
100 0, /* char p_canceled */
101 &__pthread_manager_thread.p_errno, /* int *p_errnop */
102 0, /* int p_errno */
103 NULL, /* int *p_h_errnop */
104 0, /* int p_h_errno */
105 NULL, /* char * p_in_sighandler */
106 0, /* char p_sigwaiting */
107 PTHREAD_START_ARGS_INITIALIZER(__pthread_manager),
108 /* struct pthread_start_args p_start_args */
109 {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
110 {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
111 0, /* int p_userstack */
112 NULL, /* void * p_guardaddr */
113 0, /* size_t p_guardsize */
114 &__pthread_manager_thread, /* pthread_descr p_self */
115 1 /* Always index 1 */
118 /* Pointer to the main thread (the father of the thread manager thread) */
119 /* Originally, this is the initial thread, but this changes after fork() */
121 pthread_descr __pthread_main_thread = &__pthread_initial_thread;
123 /* Limit between the stack of the initial thread (above) and the
124 stacks of other threads (below). Aligned on a STACK_SIZE boundary. */
126 char *__pthread_initial_thread_bos = NULL;
128 /* File descriptor for sending requests to the thread manager. */
129 /* Initially -1, meaning that the thread manager is not running. */
131 int __pthread_manager_request = -1;
133 /* Other end of the pipe for sending requests to the thread manager. */
135 int __pthread_manager_reader;
137 /* Limits of the thread manager stack */
139 char *__pthread_manager_thread_bos = NULL;
140 char *__pthread_manager_thread_tos = NULL;
142 /* For process-wide exit() */
144 int __pthread_exit_requested = 0;
145 int __pthread_exit_code = 0;
147 /* Communicate relevant LinuxThreads constants to gdb */
149 const int __pthread_threads_max = PTHREAD_THREADS_MAX;
150 const int __pthread_sizeof_handle = sizeof(struct pthread_handle_struct);
151 const int __pthread_offsetof_descr = offsetof(struct pthread_handle_struct,
152 h_descr);
153 const int __pthread_offsetof_pid = offsetof(struct _pthread_descr_struct,
154 p_pid);
155 const int __linuxthread_pthread_sizeof_descr
156 = sizeof(struct _pthread_descr_struct);
158 /* These variables are used by the setup code. */
159 extern int _errno;
160 extern int _h_errno;
162 /* Forward declarations */
164 static void pthread_exit_process(int retcode, void *arg);
165 static void pthread_handle_sigcancel(int sig);
166 static void pthread_handle_sigrestart(int sig);
167 #ifdef __i386__
168 static void pthread_handle_sigrestart_nonrt(int sig, struct sigcontext ctx);
169 static void pthread_handle_sigrestart_rt(int sig, struct siginfo *si,
170 struct ucontext *uc);
171 static void pthread_handle_sigcancel_nonrt(int sig, struct sigcontext ctx);
172 static void pthread_handle_sigcancel_rt(int sig, struct siginfo *si,
173 struct ucontext *uc);
174 #endif
175 static void pthread_handle_sigdebug(int sig);
177 /* Signal numbers used for the communication.
178 In these variables we keep track of the used variables. If the
179 platform does not support any real-time signals we will define the
180 values to some unreasonable value which will signal failing of all
181 the functions below. */
182 #ifndef __SIGRTMIN
183 static int current_rtmin = -1;
184 static int current_rtmax = -1;
185 int __pthread_sig_restart = SIGUSR1;
186 int __pthread_sig_cancel = SIGUSR2;
187 int __pthread_sig_debug = 0;
188 #else
189 static int current_rtmin;
190 static int current_rtmax;
192 #if __SIGRTMAX - __SIGRTMIN >= 3
193 int __pthread_sig_restart = __SIGRTMIN;
194 int __pthread_sig_cancel = __SIGRTMIN + 1;
195 int __pthread_sig_debug = __SIGRTMIN + 2;
196 #else
197 int __pthread_sig_restart = SIGUSR1;
198 int __pthread_sig_cancel = SIGUSR2;
199 int __pthread_sig_debug = 0;
200 #endif
202 static int rtsigs_initialized;
204 #include "testrtsig.h"
206 static void
207 init_rtsigs (void)
209 if (!kernel_has_rtsig ())
211 current_rtmin = -1;
212 current_rtmax = -1;
213 #if __SIGRTMAX - __SIGRTMIN >= 3
214 __pthread_sig_restart = SIGUSR1;
215 __pthread_sig_cancel = SIGUSR2;
216 __pthread_sig_debug = 0;
217 #endif
219 else
221 #if __SIGRTMAX - __SIGRTMIN >= 3
222 current_rtmin = __SIGRTMIN + 3;
223 #else
224 current_rtmin = __SIGRTMIN;
225 #endif
227 current_rtmax = __SIGRTMAX;
230 rtsigs_initialized = 1;
232 #endif
234 /* Return number of available real-time signal with highest priority. */
236 __libc_current_sigrtmin (void)
238 #ifdef __SIGRTMIN
239 if (!rtsigs_initialized)
240 init_rtsigs ();
241 #endif
242 return current_rtmin;
245 /* Return number of available real-time signal with lowest priority. */
247 __libc_current_sigrtmax (void)
249 #ifdef __SIGRTMIN
250 if (!rtsigs_initialized)
251 init_rtsigs ();
252 #endif
253 return current_rtmax;
256 /* Allocate real-time signal with highest/lowest available
257 priority. Please note that we don't use a lock since we assume
258 this function to be called at program start. */
260 __libc_allocate_rtsig (int high)
262 #ifndef __SIGRTMIN
263 return -1;
264 #else
265 if (!rtsigs_initialized)
266 init_rtsigs ();
267 if (current_rtmin == -1 || current_rtmin > current_rtmax)
268 /* We don't have anymore signal available. */
269 return -1;
271 return high ? current_rtmin++ : current_rtmax--;
272 #endif
275 /* Initialize the pthread library.
276 Initialization is split in two functions:
277 - a constructor function that blocks the __pthread_sig_restart signal
278 (must do this very early, since the program could capture the signal
279 mask with e.g. sigsetjmp before creating the first thread);
280 - a regular function called from pthread_create when needed. */
282 static void pthread_initialize(void) __attribute__((constructor));
284 static void pthread_initialize(void)
286 struct sigaction sa;
287 sigset_t mask;
288 struct rlimit limit;
289 int max_stack;
291 /* If already done (e.g. by a constructor called earlier!), bail out */
292 if (__pthread_initial_thread_bos != NULL) return;
293 #ifdef TEST_FOR_COMPARE_AND_SWAP
294 /* Test if compare-and-swap is available */
295 __pthread_has_cas = compare_and_swap_is_available();
296 #endif
297 /* For the initial stack, reserve at least STACK_SIZE bytes of stack
298 below the current stack address, and align that on a
299 STACK_SIZE boundary. */
300 __pthread_initial_thread_bos =
301 (char *)(((long)CURRENT_STACK_FRAME - 2 * STACK_SIZE) & ~(STACK_SIZE - 1));
302 /* Play with the stack size limit to make sure that no stack ever grows
303 beyond STACK_SIZE minus two pages (one page for the thread descriptor
304 immediately beyond, and one page to act as a guard page). */
305 getrlimit(RLIMIT_STACK, &limit);
306 max_stack = STACK_SIZE - 2 * __getpagesize();
307 if (limit.rlim_cur > max_stack) {
308 limit.rlim_cur = max_stack;
309 setrlimit(RLIMIT_STACK, &limit);
311 /* Update the descriptor for the initial thread. */
312 __pthread_initial_thread.p_pid = __getpid();
313 /* If we have special thread_self processing, initialize that for the
314 main thread now. */
315 #ifdef INIT_THREAD_SELF
316 INIT_THREAD_SELF(&__pthread_initial_thread, 0);
317 #endif
318 /* The errno/h_errno variable of the main thread are the global ones. */
319 __pthread_initial_thread.p_errnop = &_errno;
320 __pthread_initial_thread.p_h_errnop = &_h_errno;
321 #ifdef __SIGRTMIN
322 /* Initialize real-time signals. */
323 init_rtsigs ();
324 #endif
325 /* Setup signal handlers for the initial thread.
326 Since signal handlers are shared between threads, these settings
327 will be inherited by all other threads. */
328 #ifndef __i386__
329 sa.sa_handler = pthread_handle_sigrestart;
330 #else
331 if (__pthread_sig_restart >= SIGRTMIN)
332 sa.sa_handler = (__sighandler_t) pthread_handle_sigrestart_rt;
333 else
334 sa.sa_handler = (__sighandler_t) pthread_handle_sigrestart_nonrt;
335 #endif
336 sigemptyset(&sa.sa_mask);
337 sa.sa_flags = 0;
338 __sigaction(__pthread_sig_restart, &sa, NULL);
339 #ifndef __i386__
340 sa.sa_handler = pthread_handle_sigcancel;
341 #else
342 if (__pthread_sig_restart >= SIGRTMIN)
343 sa.sa_handler = (__sighandler_t) pthread_handle_sigcancel_rt;
344 else
345 sa.sa_handler = (__sighandler_t) pthread_handle_sigcancel_nonrt;
346 #endif
347 sa.sa_flags = 0;
348 __sigaction(__pthread_sig_cancel, &sa, NULL);
349 if (__pthread_sig_debug > 0) {
350 sa.sa_handler = pthread_handle_sigdebug;
351 sigemptyset(&sa.sa_mask);
352 sa.sa_flags = 0;
353 __sigaction(__pthread_sig_debug, &sa, NULL);
355 /* Initially, block __pthread_sig_restart. Will be unblocked on demand. */
356 sigemptyset(&mask);
357 sigaddset(&mask, __pthread_sig_restart);
358 sigprocmask(SIG_BLOCK, &mask, NULL);
359 /* Register an exit function to kill all other threads. */
360 /* Do it early so that user-registered atexit functions are called
361 before pthread_exit_process. */
362 __on_exit(pthread_exit_process, NULL);
365 void __pthread_initialize(void)
367 pthread_initialize();
370 int __pthread_initialize_manager(void)
372 int manager_pipe[2];
373 int pid;
374 struct pthread_request request;
376 /* If basic initialization not done yet (e.g. we're called from a
377 constructor run before our constructor), do it now */
378 if (__pthread_initial_thread_bos == NULL) pthread_initialize();
379 /* Setup stack for thread manager */
380 __pthread_manager_thread_bos = malloc(THREAD_MANAGER_STACK_SIZE);
381 if (__pthread_manager_thread_bos == NULL) return -1;
382 __pthread_manager_thread_tos =
383 __pthread_manager_thread_bos + THREAD_MANAGER_STACK_SIZE;
384 /* Setup pipe to communicate with thread manager */
385 if (pipe(manager_pipe) == -1) {
386 free(__pthread_manager_thread_bos);
387 return -1;
389 /* Start the thread manager */
390 pid = 0;
391 if (__pthread_initial_thread.p_report_events)
393 /* It's a bit more complicated. We have to report the creation of
394 the manager thread. */
395 int idx = __td_eventword (TD_CREATE);
396 uint32_t mask = __td_eventmask (TD_CREATE);
398 if ((mask & (__pthread_threads_events.event_bits[idx]
399 | __pthread_initial_thread.p_eventbuf.eventmask.event_bits[idx]))
400 != 0)
402 pid = __clone(__pthread_manager_event,
403 (void **) __pthread_manager_thread_tos,
404 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
405 (void *)(long)manager_pipe[0]);
407 if (pid != -1)
409 /* Now fill in the information about the new thread in
410 the newly created thread's data structure. We cannot let
411 the new thread do this since we don't know whether it was
412 already scheduled when we send the event. */
413 __pthread_manager_thread.p_eventbuf.eventdata =
414 &__pthread_manager_thread;
415 __pthread_manager_thread.p_eventbuf.eventnum = TD_CREATE;
416 __pthread_last_event = &__pthread_manager_thread;
417 __pthread_manager_thread.p_tid = 2* PTHREAD_THREADS_MAX + 1;
418 __pthread_manager_thread.p_pid = pid;
420 /* Now call the function which signals the event. */
421 __linuxthreads_create_event ();
423 /* Now restart the thread. */
424 __pthread_unlock(__pthread_manager_thread.p_lock);
429 if (pid == 0)
430 pid = __clone(__pthread_manager, (void **) __pthread_manager_thread_tos,
431 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND,
432 (void *)(long)manager_pipe[0]);
433 if (pid == -1) {
434 free(__pthread_manager_thread_bos);
435 __libc_close(manager_pipe[0]);
436 __libc_close(manager_pipe[1]);
437 return -1;
439 __pthread_manager_request = manager_pipe[1]; /* writing end */
440 __pthread_manager_reader = manager_pipe[0]; /* reading end */
441 __pthread_manager_thread.p_tid = 2* PTHREAD_THREADS_MAX + 1;
442 __pthread_manager_thread.p_pid = pid;
443 /* Make gdb aware of new thread manager */
444 if (__pthread_threads_debug && __pthread_sig_debug > 0)
446 raise(__pthread_sig_debug);
447 /* We suspend ourself and gdb will wake us up when it is
448 ready to handle us. */
449 suspend(thread_self());
451 /* Synchronize debugging of the thread manager */
452 request.req_kind = REQ_DEBUG;
453 __libc_write(__pthread_manager_request, (char *) &request, sizeof(request));
454 return 0;
457 /* Thread creation */
459 int __pthread_create_2_1(pthread_t *thread, const pthread_attr_t *attr,
460 void * (*start_routine)(void *), void *arg)
462 pthread_descr self = thread_self();
463 struct pthread_request request;
464 if (__pthread_manager_request < 0) {
465 if (__pthread_initialize_manager() < 0) return EAGAIN;
467 request.req_thread = self;
468 request.req_kind = REQ_CREATE;
469 request.req_args.create.attr = attr;
470 request.req_args.create.fn = start_routine;
471 request.req_args.create.arg = arg;
472 sigprocmask(SIG_SETMASK, (const sigset_t *) NULL,
473 &request.req_args.create.mask);
474 __libc_write(__pthread_manager_request, (char *) &request, sizeof(request));
475 suspend(self);
476 if (THREAD_GETMEM(self, p_retcode) == 0)
477 *thread = (pthread_t) THREAD_GETMEM(self, p_retval);
478 return THREAD_GETMEM(self, p_retcode);
481 #if defined HAVE_ELF && defined PIC && defined DO_VERSIONING
482 default_symbol_version (__pthread_create_2_1, pthread_create, GLIBC_2.1);
484 int __pthread_create_2_0(pthread_t *thread, const pthread_attr_t *attr,
485 void * (*start_routine)(void *), void *arg)
487 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
488 the old size and access to the new members might crash the program.
489 We convert the struct now. */
490 pthread_attr_t new_attr;
492 if (attr != NULL)
494 size_t ps = __getpagesize ();
496 memcpy (&new_attr, attr,
497 (size_t) &(((pthread_attr_t*)NULL)->__guardsize));
498 new_attr.__guardsize = ps;
499 new_attr.__stackaddr_set = 0;
500 new_attr.__stackaddr = NULL;
501 new_attr.__stacksize = STACK_SIZE - ps;
502 attr = &new_attr;
504 return __pthread_create_2_1 (thread, attr, start_routine, arg);
506 symbol_version (__pthread_create_2_0, pthread_create, GLIBC_2.0);
507 #else
508 strong_alias (__pthread_create_2_1, pthread_create)
509 #endif
511 /* Simple operations on thread identifiers */
513 pthread_t pthread_self(void)
515 pthread_descr self = thread_self();
516 return THREAD_GETMEM(self, p_tid);
519 int pthread_equal(pthread_t thread1, pthread_t thread2)
521 return thread1 == thread2;
524 /* Helper function for thread_self in the case of user-provided stacks */
526 #ifndef THREAD_SELF
528 pthread_descr __pthread_find_self()
530 char * sp = CURRENT_STACK_FRAME;
531 pthread_handle h;
533 /* __pthread_handles[0] is the initial thread, __pthread_handles[1] is
534 the manager threads handled specially in thread_self(), so start at 2 */
535 h = __pthread_handles + 2;
536 while (! (sp <= (char *) h->h_descr && sp >= h->h_bottom)) h++;
537 return h->h_descr;
540 #endif
542 /* Thread scheduling */
544 int pthread_setschedparam(pthread_t thread, int policy,
545 const struct sched_param *param)
547 pthread_handle handle = thread_handle(thread);
548 pthread_descr th;
550 __pthread_lock(&handle->h_lock, NULL);
551 if (invalid_handle(handle, thread)) {
552 __pthread_unlock(&handle->h_lock);
553 return ESRCH;
555 th = handle->h_descr;
556 if (__sched_setscheduler(th->p_pid, policy, param) == -1) {
557 __pthread_unlock(&handle->h_lock);
558 return errno;
560 th->p_priority = policy == SCHED_OTHER ? 0 : param->sched_priority;
561 __pthread_unlock(&handle->h_lock);
562 if (__pthread_manager_request >= 0)
563 __pthread_manager_adjust_prio(th->p_priority);
564 return 0;
567 int pthread_getschedparam(pthread_t thread, int *policy,
568 struct sched_param *param)
570 pthread_handle handle = thread_handle(thread);
571 int pid, pol;
573 __pthread_lock(&handle->h_lock, NULL);
574 if (invalid_handle(handle, thread)) {
575 __pthread_unlock(&handle->h_lock);
576 return ESRCH;
578 pid = handle->h_descr->p_pid;
579 __pthread_unlock(&handle->h_lock);
580 pol = __sched_getscheduler(pid);
581 if (pol == -1) return errno;
582 if (__sched_getparam(pid, param) == -1) return errno;
583 *policy = pol;
584 return 0;
587 /* Process-wide exit() request */
589 static void pthread_exit_process(int retcode, void *arg)
591 struct pthread_request request;
592 pthread_descr self = thread_self();
594 if (__pthread_manager_request >= 0) {
595 request.req_thread = self;
596 request.req_kind = REQ_PROCESS_EXIT;
597 request.req_args.exit.code = retcode;
598 __libc_write(__pthread_manager_request,
599 (char *) &request, sizeof(request));
600 suspend(self);
601 /* Main thread should accumulate times for thread manager and its
602 children, so that timings for main thread account for all threads. */
603 if (self == __pthread_main_thread)
604 waitpid(__pthread_manager_thread.p_pid, NULL, __WCLONE);
608 /* The handler for the RESTART signal just records the signal received
609 in the thread descriptor, and optionally performs a siglongjmp
610 (for pthread_cond_timedwait). */
612 static void pthread_handle_sigrestart(int sig)
614 pthread_descr self = thread_self();
615 THREAD_SETMEM(self, p_signal, sig);
616 if (THREAD_GETMEM(self, p_signal_jmp) != NULL)
617 siglongjmp(*THREAD_GETMEM(self, p_signal_jmp), 1);
620 #ifdef __i386__
621 static void pthread_handle_sigrestart_nonrt(int sig, struct sigcontext ctx)
623 asm volatile ("movw %w0,%%gs" : : "r" (ctx.gs));
624 pthread_handle_sigrestart(sig);
627 static void pthread_handle_sigrestart_rt(int sig, struct siginfo *si,
628 struct ucontext *uc)
630 asm volatile ("movw %w0,%%gs" : : "r" (uc->uc_mcontext.gregs[GS]));
631 pthread_handle_sigrestart(sig);
633 #endif
635 /* The handler for the CANCEL signal checks for cancellation
636 (in asynchronous mode), for process-wide exit and exec requests.
637 For the thread manager thread, redirect the signal to
638 __pthread_manager_sighandler. */
640 static void pthread_handle_sigcancel(int sig)
642 pthread_descr self = thread_self();
643 sigjmp_buf * jmpbuf;
645 if (self == &__pthread_manager_thread)
647 __pthread_manager_sighandler(sig);
648 return;
650 if (__pthread_exit_requested) {
651 /* Main thread should accumulate times for thread manager and its
652 children, so that timings for main thread account for all threads. */
653 if (self == __pthread_main_thread)
654 waitpid(__pthread_manager_thread.p_pid, NULL, __WCLONE);
655 _exit(__pthread_exit_code);
657 if (THREAD_GETMEM(self, p_canceled)
658 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
659 if (THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
660 pthread_exit(PTHREAD_CANCELED);
661 jmpbuf = THREAD_GETMEM(self, p_cancel_jmp);
662 if (jmpbuf != NULL) {
663 THREAD_SETMEM(self, p_cancel_jmp, NULL);
664 siglongjmp(*jmpbuf, 1);
669 #ifdef __i386__
670 static void pthread_handle_sigcancel_nonrt(int sig, struct sigcontext ctx)
672 asm volatile ("movw %w0,%%gs" : : "r" (ctx.gs));
673 pthread_handle_sigcancel(sig);
676 static void pthread_handle_sigcancel_rt(int sig, struct siginfo *si,
677 struct ucontext *uc)
679 asm volatile ("movw %w0,%%gs" : : "r" (uc->uc_mcontext.gregs[GS]));
680 pthread_handle_sigcancel(sig);
682 #endif
684 /* Handler for the DEBUG signal.
685 The debugging strategy is as follows:
686 On reception of a REQ_DEBUG request (sent by new threads created to
687 the thread manager under debugging mode), the thread manager throws
688 __pthread_sig_debug to itself. The debugger (if active) intercepts
689 this signal, takes into account new threads and continue execution
690 of the thread manager by propagating the signal because it doesn't
691 know what it is specifically done for. In the current implementation,
692 the thread manager simply discards it. */
694 static void pthread_handle_sigdebug(int sig)
696 /* Nothing */
699 /* Reset the state of the thread machinery after a fork().
700 Close the pipe used for requests and set the main thread to the forked
701 thread.
702 Notice that we can't free the stack segments, as the forked thread
703 may hold pointers into them. */
705 void __pthread_reset_main_thread()
707 pthread_descr self = thread_self();
709 if (__pthread_manager_request != -1) {
710 /* Free the thread manager stack */
711 free(__pthread_manager_thread_bos);
712 __pthread_manager_thread_bos = __pthread_manager_thread_tos = NULL;
713 /* Close the two ends of the pipe */
714 __libc_close(__pthread_manager_request);
715 __libc_close(__pthread_manager_reader);
716 __pthread_manager_request = __pthread_manager_reader = -1;
719 /* Update the pid of the main thread */
720 THREAD_SETMEM(self, p_pid, __getpid());
721 /* Make the forked thread the main thread */
722 __pthread_main_thread = self;
723 THREAD_SETMEM(self, p_nextlive, self);
724 THREAD_SETMEM(self, p_prevlive, self);
725 /* Now this thread modifies the global variables. */
726 THREAD_SETMEM(self, p_errnop, &_errno);
727 THREAD_SETMEM(self, p_h_errnop, &_h_errno);
730 /* Process-wide exec() request */
732 void __pthread_kill_other_threads_np(void)
734 struct sigaction sa;
735 /* Terminate all other threads and thread manager */
736 pthread_exit_process(0, NULL);
737 /* Make current thread the main thread in case the calling thread
738 changes its mind, does not exec(), and creates new threads instead. */
739 __pthread_reset_main_thread();
741 /* Reset the signal handlers behaviour for the signals the
742 implementation uses since this would be passed to the new
743 process. */
744 sigemptyset(&sa.sa_mask);
745 sa.sa_flags = 0;
746 sa.sa_handler = SIG_DFL;
747 __sigaction(__pthread_sig_restart, &sa, NULL);
748 __sigaction(__pthread_sig_cancel, &sa, NULL);
749 if (__pthread_sig_debug > 0)
750 __sigaction(__pthread_sig_debug, &sa, NULL);
752 weak_alias (__pthread_kill_other_threads_np, pthread_kill_other_threads_np)
754 /* Concurrency symbol level. */
755 static int current_level;
757 int __pthread_setconcurrency(int level)
759 /* We don't do anything unless we have found a useful interpretation. */
760 current_level = level;
761 return 0;
763 weak_alias (__pthread_setconcurrency, pthread_setconcurrency)
765 int __pthread_getconcurrency(void)
767 return current_level;
769 weak_alias (__pthread_getconcurrency, pthread_getconcurrency)
771 /* Debugging aid */
773 #ifdef DEBUG
774 #include <stdarg.h>
776 void __pthread_message(char * fmt, ...)
778 char buffer[1024];
779 va_list args;
780 sprintf(buffer, "%05d : ", __getpid());
781 va_start(args, fmt);
782 vsnprintf(buffer + 8, sizeof(buffer) - 8, fmt, args);
783 va_end(args);
784 __libc_write(2, buffer, strlen(buffer));
787 #endif
790 #ifndef PIC
791 /* We need a hook to force the cancelation wrappers to be linked in when
792 static libpthread is used. */
793 extern const int __pthread_provide_wrappers;
794 static const int *const __pthread_require_wrappers =
795 &__pthread_provide_wrappers;
796 #endif