Update.
[glibc.git] / linuxthreads / pthread.c
blobb7cf573fe9a2cf03b10b78c0eb5398f830989841
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 #include <sys/wait.h>
25 #include <sys/resource.h>
26 #include "pthread.h"
27 #include "internals.h"
28 #include "spinlock.h"
29 #include "restart.h"
31 /* Descriptor of the initial thread */
33 struct _pthread_descr_struct __pthread_initial_thread = {
34 &__pthread_initial_thread, /* pthread_descr p_nextlive */
35 &__pthread_initial_thread, /* pthread_descr p_prevlive */
36 NULL, /* pthread_descr p_nextwaiting */
37 PTHREAD_THREADS_MAX, /* pthread_t p_tid */
38 0, /* int p_pid */
39 0, /* int p_priority */
40 &__pthread_handles[0].h_lock, /* struct _pthread_fastlock * p_lock */
41 0, /* int p_signal */
42 NULL, /* sigjmp_buf * p_signal_buf */
43 NULL, /* sigjmp_buf * p_cancel_buf */
44 0, /* char p_terminated */
45 0, /* char p_detached */
46 0, /* char p_exited */
47 NULL, /* void * p_retval */
48 0, /* int p_retval */
49 NULL, /* pthread_descr p_joining */
50 NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
51 0, /* char p_cancelstate */
52 0, /* char p_canceltype */
53 0, /* char p_canceled */
54 NULL, /* int *p_errnop */
55 0, /* int p_errno */
56 NULL, /* int *p_h_errnop */
57 0, /* int p_h_errno */
58 NULL, /* char * p_in_sighandler */
59 0, /* char p_sigwaiting */
60 PTHREAD_START_ARGS_INITIALIZER, /* struct pthread_start_args p_start_args */
61 {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
62 {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
63 0, /* int p_userstack */
64 NULL, /* void * p_guardaddr */
65 0, /* size_t p_guardsize */
66 &__pthread_initial_thread, /* pthread_descr p_self */
67 0 /* Always index 0 */
70 /* Descriptor of the manager thread; none of this is used but the error
71 variables, the p_pid and p_priority fields,
72 and the address for identification. */
74 struct _pthread_descr_struct __pthread_manager_thread = {
75 NULL, /* pthread_descr p_nextlive */
76 NULL, /* pthread_descr p_prevlive */
77 NULL, /* pthread_descr p_nextwaiting */
78 0, /* int p_tid */
79 0, /* int p_pid */
80 0, /* int p_priority */
81 NULL, /* struct _pthread_fastlock * p_lock */
82 0, /* int p_signal */
83 NULL, /* sigjmp_buf * p_signal_buf */
84 NULL, /* sigjmp_buf * p_cancel_buf */
85 0, /* char p_terminated */
86 0, /* char p_detached */
87 0, /* char p_exited */
88 NULL, /* void * p_retval */
89 0, /* int p_retval */
90 NULL, /* pthread_descr p_joining */
91 NULL, /* struct _pthread_cleanup_buffer * p_cleanup */
92 0, /* char p_cancelstate */
93 0, /* char p_canceltype */
94 0, /* char p_canceled */
95 &__pthread_manager_thread.p_errno, /* int *p_errnop */
96 0, /* int p_errno */
97 NULL, /* int *p_h_errnop */
98 0, /* int p_h_errno */
99 NULL, /* char * p_in_sighandler */
100 0, /* char p_sigwaiting */
101 PTHREAD_START_ARGS_INITIALIZER, /* struct pthread_start_args p_start_args */
102 {NULL}, /* void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE] */
103 {NULL}, /* void * p_libc_specific[_LIBC_TSD_KEY_N] */
104 0, /* int p_userstack */
105 NULL, /* void * p_guardaddr */
106 0, /* size_t p_guardsize */
107 &__pthread_manager_thread, /* pthread_descr p_self */
108 1 /* Always index 1 */
111 /* Pointer to the main thread (the father of the thread manager thread) */
112 /* Originally, this is the initial thread, but this changes after fork() */
114 pthread_descr __pthread_main_thread = &__pthread_initial_thread;
116 /* Limit between the stack of the initial thread (above) and the
117 stacks of other threads (below). Aligned on a STACK_SIZE boundary. */
119 char *__pthread_initial_thread_bos = NULL;
121 /* File descriptor for sending requests to the thread manager. */
122 /* Initially -1, meaning that the thread manager is not running. */
124 int __pthread_manager_request = -1;
126 /* Other end of the pipe for sending requests to the thread manager. */
128 int __pthread_manager_reader;
130 /* Limits of the thread manager stack */
132 char *__pthread_manager_thread_bos = NULL;
133 char *__pthread_manager_thread_tos = NULL;
135 /* For process-wide exit() */
137 int __pthread_exit_requested = 0;
138 int __pthread_exit_code = 0;
140 /* Communicate relevant LinuxThreads constants to gdb */
142 const int __pthread_threads_max = PTHREAD_THREADS_MAX;
143 const int __pthread_sizeof_handle = sizeof(struct pthread_handle_struct);
144 const int __pthread_offsetof_descr = offsetof(struct pthread_handle_struct,
145 h_descr);
146 const int __pthread_offsetof_pid = offsetof(struct _pthread_descr_struct,
147 p_pid);
149 /* These variables are used by the setup code. */
150 extern int _errno;
151 extern int _h_errno;
153 /* Forward declarations */
155 static void pthread_exit_process(int retcode, void *arg);
156 #ifndef __i386__
157 static void pthread_handle_sigcancel(int sig);
158 static void pthread_handle_sigrestart(int sig);
159 #else
160 static void pthread_handle_sigcancel(int sig, struct sigcontext ctx);
161 static void pthread_handle_sigrestart(int sig, struct sigcontext ctx);
162 #endif
163 static void pthread_handle_sigdebug(int sig);
165 /* Signal numbers used for the communication.
166 In these variables we keep track of the used variables. If the
167 platform does not support any real-time signals we will define the
168 values to some unreasonable value which will signal failing of all
169 the functions below. */
170 #ifndef __SIGRTMIN
171 static int current_rtmin = -1;
172 static int current_rtmax = -1;
173 int __pthread_sig_restart = SIGUSR1;
174 int __pthread_sig_cancel = SIGUSR2;
175 int __pthread_sig_debug = 0;
176 #else
177 static int current_rtmin;
178 static int current_rtmax;
180 #if __SIGRTMAX - __SIGRTMIN >= 3
181 int __pthread_sig_restart = __SIGRTMIN;
182 int __pthread_sig_cancel = __SIGRTMIN + 1;
183 int __pthread_sig_debug = __SIGRTMIN + 2;
184 #else
185 int __pthread_sig_restart = SIGUSR1;
186 int __pthread_sig_cancel = SIGUSR2;
187 int __pthread_sig_debug = 0;
188 #endif
190 static int rtsigs_initialized;
192 #include "testrtsig.h"
194 static void
195 init_rtsigs (void)
197 if (!kernel_has_rtsig ())
199 current_rtmin = -1;
200 current_rtmax = -1;
201 #if __SIGRTMAX - __SIGRTMIN >= 3
202 __pthread_sig_restart = SIGUSR1;
203 __pthread_sig_cancel = SIGUSR2;
204 __pthread_sig_debug = 0;
205 #endif
207 else
209 #if __SIGRTMAX - __SIGRTMIN >= 3
210 current_rtmin = __SIGRTMIN + 3;
211 #else
212 current_rtmin = __SIGRTMIN;
213 #endif
215 current_rtmax = __SIGRTMAX;
218 rtsigs_initialized = 1;
220 #endif
222 /* Return number of available real-time signal with highest priority. */
224 __libc_current_sigrtmin (void)
226 #ifdef __SIGRTMIN
227 if (!rtsigs_initialized)
228 init_rtsigs ();
229 #endif
230 return current_rtmin;
233 /* Return number of available real-time signal with lowest priority. */
235 __libc_current_sigrtmax (void)
237 #ifdef __SIGRTMIN
238 if (!rtsigs_initialized)
239 init_rtsigs ();
240 #endif
241 return current_rtmax;
244 /* Allocate real-time signal with highest/lowest available
245 priority. Please note that we don't use a lock since we assume
246 this function to be called at program start. */
248 __libc_allocate_rtsig (int high)
250 #ifndef __SIGRTMIN
251 return -1;
252 #else
253 if (!rtsigs_initialized)
254 init_rtsigs ();
255 if (current_rtmin == -1 || current_rtmin > current_rtmax)
256 /* We don't have anymore signal available. */
257 return -1;
259 return high ? current_rtmin++ : current_rtmax--;
260 #endif
263 /* Initialize the pthread library.
264 Initialization is split in two functions:
265 - a constructor function that blocks the __pthread_sig_restart signal
266 (must do this very early, since the program could capture the signal
267 mask with e.g. sigsetjmp before creating the first thread);
268 - a regular function called from pthread_create when needed. */
270 static void pthread_initialize(void) __attribute__((constructor));
272 static void pthread_initialize(void)
274 struct sigaction sa;
275 sigset_t mask;
276 struct rlimit limit;
277 int max_stack;
279 /* If already done (e.g. by a constructor called earlier!), bail out */
280 if (__pthread_initial_thread_bos != NULL) return;
281 #ifdef TEST_FOR_COMPARE_AND_SWAP
282 /* Test if compare-and-swap is available */
283 __pthread_has_cas = compare_and_swap_is_available();
284 #endif
285 /* For the initial stack, reserve at least STACK_SIZE bytes of stack
286 below the current stack address, and align that on a
287 STACK_SIZE boundary. */
288 __pthread_initial_thread_bos =
289 (char *)(((long)CURRENT_STACK_FRAME - 2 * STACK_SIZE) & ~(STACK_SIZE - 1));
290 /* Play with the stack size limit to make sure that no stack ever grows
291 beyond STACK_SIZE minus two pages (one page for the thread descriptor
292 immediately beyond, and one page to act as a guard page). */
293 getrlimit(RLIMIT_STACK, &limit);
294 max_stack = STACK_SIZE - 2 * __getpagesize();
295 if (limit.rlim_cur > max_stack) {
296 limit.rlim_cur = max_stack;
297 setrlimit(RLIMIT_STACK, &limit);
299 /* Update the descriptor for the initial thread. */
300 __pthread_initial_thread.p_pid = __getpid();
301 /* If we have special thread_self processing, initialize that for the
302 main thread now. */
303 #ifdef INIT_THREAD_SELF
304 INIT_THREAD_SELF(&__pthread_initial_thread, 0);
305 #endif
306 /* The errno/h_errno variable of the main thread are the global ones. */
307 __pthread_initial_thread.p_errnop = &_errno;
308 __pthread_initial_thread.p_h_errnop = &_h_errno;
309 #ifdef __SIGRTMIN
310 /* Initialize real-time signals. */
311 init_rtsigs ();
312 #endif
313 /* Setup signal handlers for the initial thread.
314 Since signal handlers are shared between threads, these settings
315 will be inherited by all other threads. */
316 #ifndef __i386__
317 sa.sa_handler = pthread_handle_sigrestart;
318 #else
319 sa.sa_handler = (__sighandler_t) pthread_handle_sigrestart;
320 #endif
321 sigemptyset(&sa.sa_mask);
322 sa.sa_flags = 0;
323 __sigaction(__pthread_sig_restart, &sa, NULL);
324 #ifndef __i386__
325 sa.sa_handler = pthread_handle_sigcancel;
326 #else
327 sa.sa_handler = (__sighandler_t) pthread_handle_sigcancel;
328 #endif
329 sa.sa_flags = 0;
330 __sigaction(__pthread_sig_cancel, &sa, NULL);
331 if (__pthread_sig_debug > 0) {
332 sa.sa_handler = pthread_handle_sigdebug;
333 sigemptyset(&sa.sa_mask);
334 sa.sa_flags = 0;
335 __sigaction(__pthread_sig_debug, &sa, NULL);
337 /* Initially, block __pthread_sig_restart. Will be unblocked on demand. */
338 sigemptyset(&mask);
339 sigaddset(&mask, __pthread_sig_restart);
340 sigprocmask(SIG_BLOCK, &mask, NULL);
341 /* Register an exit function to kill all other threads. */
342 /* Do it early so that user-registered atexit functions are called
343 before pthread_exit_process. */
344 __on_exit(pthread_exit_process, NULL);
347 void __pthread_initialize(void)
349 pthread_initialize();
352 int __pthread_initialize_manager(void)
354 int manager_pipe[2];
355 int pid;
356 struct pthread_request request;
358 /* If basic initialization not done yet (e.g. we're called from a
359 constructor run before our constructor), do it now */
360 if (__pthread_initial_thread_bos == NULL) pthread_initialize();
361 /* Setup stack for thread manager */
362 __pthread_manager_thread_bos = malloc(THREAD_MANAGER_STACK_SIZE);
363 if (__pthread_manager_thread_bos == NULL) return -1;
364 __pthread_manager_thread_tos =
365 __pthread_manager_thread_bos + THREAD_MANAGER_STACK_SIZE;
366 /* Setup pipe to communicate with thread manager */
367 if (pipe(manager_pipe) == -1) {
368 free(__pthread_manager_thread_bos);
369 return -1;
371 /* Start the thread manager */
372 pid = __clone(__pthread_manager, (void **) __pthread_manager_thread_tos,
373 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND
374 , (void *)(long)manager_pipe[0]);
375 if (pid == -1) {
376 free(__pthread_manager_thread_bos);
377 __libc_close(manager_pipe[0]);
378 __libc_close(manager_pipe[1]);
379 return -1;
381 __pthread_manager_request = manager_pipe[1]; /* writing end */
382 __pthread_manager_reader = manager_pipe[0]; /* reading end */
383 __pthread_manager_thread.p_pid = pid;
384 /* Make gdb aware of new thread manager */
385 if (__pthread_threads_debug && __pthread_sig_debug > 0)
387 raise(__pthread_sig_debug);
388 /* We suspend ourself and gdb will wake us up when it is
389 ready to handle us. */
390 suspend(thread_self());
392 /* Synchronize debugging of the thread manager */
393 request.req_kind = REQ_DEBUG;
394 __libc_write(__pthread_manager_request, (char *) &request, sizeof(request));
395 return 0;
398 /* Thread creation */
400 int __pthread_create_2_1(pthread_t *thread, const pthread_attr_t *attr,
401 void * (*start_routine)(void *), void *arg)
403 pthread_descr self = thread_self();
404 struct pthread_request request;
405 if (__pthread_manager_request < 0) {
406 if (__pthread_initialize_manager() < 0) return EAGAIN;
408 request.req_thread = self;
409 request.req_kind = REQ_CREATE;
410 request.req_args.create.attr = attr;
411 request.req_args.create.fn = start_routine;
412 request.req_args.create.arg = arg;
413 sigprocmask(SIG_SETMASK, (const sigset_t *) NULL,
414 &request.req_args.create.mask);
415 __libc_write(__pthread_manager_request, (char *) &request, sizeof(request));
416 suspend(self);
417 if (THREAD_GETMEM(self, p_retcode) == 0)
418 *thread = (pthread_t) THREAD_GETMEM(self, p_retval);
419 return THREAD_GETMEM(self, p_retcode);
422 #if defined HAVE_ELF && defined PIC && defined DO_VERSIONING
423 default_symbol_version (__pthread_create_2_1, pthread_create, GLIBC_2.1);
425 int __pthread_create_2_0(pthread_t *thread, const pthread_attr_t *attr,
426 void * (*start_routine)(void *), void *arg)
428 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
429 the old size and access to the new members might crash the program.
430 We convert the struct now. */
431 pthread_attr_t new_attr;
433 if (attr != NULL)
435 size_t ps = __getpagesize ();
437 memcpy (&new_attr, attr,
438 (size_t) &(((pthread_attr_t*)NULL)->__guardsize));
439 new_attr.__guardsize = ps;
440 new_attr.__stackaddr_set = 0;
441 new_attr.__stackaddr = NULL;
442 new_attr.__stacksize = STACK_SIZE - ps;
443 attr = &new_attr;
445 return __pthread_create_2_1 (thread, attr, start_routine, arg);
447 symbol_version (__pthread_create_2_0, pthread_create, GLIBC_2.0);
448 #else
449 strong_alias (__pthread_create_2_1, pthread_create)
450 #endif
452 /* Simple operations on thread identifiers */
454 pthread_t pthread_self(void)
456 pthread_descr self = thread_self();
457 return THREAD_GETMEM(self, p_tid);
460 int pthread_equal(pthread_t thread1, pthread_t thread2)
462 return thread1 == thread2;
465 /* Helper function for thread_self in the case of user-provided stacks */
467 #ifndef THREAD_SELF
469 pthread_descr __pthread_find_self()
471 char * sp = CURRENT_STACK_FRAME;
472 pthread_handle h;
474 /* __pthread_handles[0] is the initial thread, __pthread_handles[1] is
475 the manager threads handled specially in thread_self(), so start at 2 */
476 h = __pthread_handles + 2;
477 while (! (sp <= (char *) h->h_descr && sp >= h->h_bottom)) h++;
478 return h->h_descr;
481 #endif
483 /* Thread scheduling */
485 int pthread_setschedparam(pthread_t thread, int policy,
486 const struct sched_param *param)
488 pthread_handle handle = thread_handle(thread);
489 pthread_descr th;
491 __pthread_lock(&handle->h_lock, NULL);
492 if (invalid_handle(handle, thread)) {
493 __pthread_unlock(&handle->h_lock);
494 return ESRCH;
496 th = handle->h_descr;
497 if (__sched_setscheduler(th->p_pid, policy, param) == -1) {
498 __pthread_unlock(&handle->h_lock);
499 return errno;
501 th->p_priority = policy == SCHED_OTHER ? 0 : param->sched_priority;
502 __pthread_unlock(&handle->h_lock);
503 if (__pthread_manager_request >= 0)
504 __pthread_manager_adjust_prio(th->p_priority);
505 return 0;
508 int pthread_getschedparam(pthread_t thread, int *policy,
509 struct sched_param *param)
511 pthread_handle handle = thread_handle(thread);
512 int pid, pol;
514 __pthread_lock(&handle->h_lock, NULL);
515 if (invalid_handle(handle, thread)) {
516 __pthread_unlock(&handle->h_lock);
517 return ESRCH;
519 pid = handle->h_descr->p_pid;
520 __pthread_unlock(&handle->h_lock);
521 pol = __sched_getscheduler(pid);
522 if (pol == -1) return errno;
523 if (__sched_getparam(pid, param) == -1) return errno;
524 *policy = pol;
525 return 0;
528 /* Process-wide exit() request */
530 static void pthread_exit_process(int retcode, void *arg)
532 struct pthread_request request;
533 pthread_descr self = thread_self();
535 if (__pthread_manager_request >= 0) {
536 request.req_thread = self;
537 request.req_kind = REQ_PROCESS_EXIT;
538 request.req_args.exit.code = retcode;
539 __libc_write(__pthread_manager_request,
540 (char *) &request, sizeof(request));
541 suspend(self);
542 /* Main thread should accumulate times for thread manager and its
543 children, so that timings for main thread account for all threads. */
544 if (self == __pthread_main_thread)
545 waitpid(__pthread_manager_thread.p_pid, NULL, __WCLONE);
549 /* The handler for the RESTART signal just records the signal received
550 in the thread descriptor, and optionally performs a siglongjmp
551 (for pthread_cond_timedwait). */
553 #ifndef __i386__
554 static void pthread_handle_sigrestart(int sig)
556 pthread_descr self = thread_self();
557 #else
558 static void pthread_handle_sigrestart(int sig, struct sigcontext ctx)
560 pthread_descr self;
561 asm volatile ("movw %w0,%%gs" : : "r" (ctx.gs));
562 self = thread_self();
563 #endif
564 THREAD_SETMEM(self, p_signal, sig);
565 if (THREAD_GETMEM(self, p_signal_jmp) != NULL)
566 siglongjmp(*THREAD_GETMEM(self, p_signal_jmp), 1);
569 /* The handler for the CANCEL signal checks for cancellation
570 (in asynchronous mode), for process-wide exit and exec requests.
571 For the thread manager thread, redirect the signal to
572 __pthread_manager_sighandler. */
574 #ifndef __i386__
575 static void pthread_handle_sigcancel(int sig)
577 pthread_descr self = thread_self();
578 sigjmp_buf * jmpbuf;
579 #else
580 static void pthread_handle_sigcancel(int sig, struct sigcontext ctx)
582 pthread_descr self;
583 sigjmp_buf * jmpbuf;
584 asm volatile ("movw %w0,%%gs" : : "r" (ctx.gs));
585 self = thread_self();
586 #endif
588 if (self == &__pthread_manager_thread)
590 __pthread_manager_sighandler(sig);
591 return;
593 if (__pthread_exit_requested) {
594 /* Main thread should accumulate times for thread manager and its
595 children, so that timings for main thread account for all threads. */
596 if (self == __pthread_main_thread)
597 waitpid(__pthread_manager_thread.p_pid, NULL, __WCLONE);
598 _exit(__pthread_exit_code);
600 if (THREAD_GETMEM(self, p_canceled)
601 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
602 if (THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
603 pthread_exit(PTHREAD_CANCELED);
604 jmpbuf = THREAD_GETMEM(self, p_cancel_jmp);
605 if (jmpbuf != NULL) {
606 THREAD_SETMEM(self, p_cancel_jmp, NULL);
607 siglongjmp(*jmpbuf, 1);
612 /* Handler for the DEBUG signal.
613 The debugging strategy is as follows:
614 On reception of a REQ_DEBUG request (sent by new threads created to
615 the thread manager under debugging mode), the thread manager throws
616 __pthread_sig_debug to itself. The debugger (if active) intercepts
617 this signal, takes into account new threads and continue execution
618 of the thread manager by propagating the signal because it doesn't
619 know what it is specifically done for. In the current implementation,
620 the thread manager simply discards it. */
622 static void pthread_handle_sigdebug(int sig)
624 /* Nothing */
627 /* Reset the state of the thread machinery after a fork().
628 Close the pipe used for requests and set the main thread to the forked
629 thread.
630 Notice that we can't free the stack segments, as the forked thread
631 may hold pointers into them. */
633 void __pthread_reset_main_thread()
635 pthread_descr self = thread_self();
637 if (__pthread_manager_request != -1) {
638 /* Free the thread manager stack */
639 free(__pthread_manager_thread_bos);
640 __pthread_manager_thread_bos = __pthread_manager_thread_tos = NULL;
641 /* Close the two ends of the pipe */
642 __libc_close(__pthread_manager_request);
643 __libc_close(__pthread_manager_reader);
644 __pthread_manager_request = __pthread_manager_reader = -1;
646 /* Update the pid of the main thread */
647 THREAD_SETMEM(self, p_pid, __getpid());
648 /* Make the forked thread the main thread */
649 __pthread_main_thread = self;
650 THREAD_SETMEM(self, p_nextlive, self);
651 THREAD_SETMEM(self, p_prevlive, self);
652 /* Now this thread modifies the global variables. */
653 THREAD_SETMEM(self, p_errnop, &_errno);
654 THREAD_SETMEM(self, p_h_errnop, &_h_errno);
657 /* Process-wide exec() request */
659 void __pthread_kill_other_threads_np(void)
661 /* Terminate all other threads and thread manager */
662 pthread_exit_process(0, NULL);
663 /* Make current thread the main thread in case the calling thread
664 changes its mind, does not exec(), and creates new threads instead. */
665 __pthread_reset_main_thread();
667 weak_alias (__pthread_kill_other_threads_np, pthread_kill_other_threads_np)
669 /* Concurrency symbol level. */
670 static int current_level;
672 int __pthread_setconcurrency(int level)
674 /* We don't do anything unless we have found a useful interpretation. */
675 current_level = level;
676 return 0;
678 weak_alias (__pthread_setconcurrency, pthread_setconcurrency)
680 int __pthread_getconcurrency(void)
682 return current_level;
684 weak_alias (__pthread_getconcurrency, pthread_getconcurrency)
686 /* Debugging aid */
688 #ifdef DEBUG
689 #include <stdarg.h>
691 void __pthread_message(char * fmt, ...)
693 char buffer[1024];
694 va_list args;
695 sprintf(buffer, "%05d : ", __getpid());
696 va_start(args, fmt);
697 vsnprintf(buffer + 8, sizeof(buffer) - 8, fmt, args);
698 va_end(args);
699 __libc_write(2, buffer, strlen(buffer));
702 #endif
705 #ifndef PIC
706 /* We need a hook to force the cancelation wrappers to be linked in when
707 static libpthread is used. */
708 extern const int __pthread_provide_wrappers;
709 static const int *const __pthread_require_wrappers =
710 &__pthread_provide_wrappers;
711 #endif