(pthread_handle_create): Start the child thread with the cancel signal blocked, so...
[glibc.git] / linuxthreads / manager.c
blobc8808197c15dd8785ef91624c912293903e616be
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 /* The "thread manager" thread: manages creation and termination of threads */
17 #include <errno.h>
18 #include <sched.h>
19 #include <stddef.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/poll.h> /* for poll */
25 #include <sys/mman.h> /* for mmap */
26 #include <sys/param.h>
27 #include <sys/time.h>
28 #include <sys/wait.h> /* for waitpid macros */
30 #include "pthread.h"
31 #include "internals.h"
32 #include "spinlock.h"
33 #include "restart.h"
34 #include "semaphore.h"
36 /* Array of active threads. Entry 0 is reserved for the initial thread. */
37 struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX] =
38 { { __LOCK_INITIALIZER, &__pthread_initial_thread, 0},
39 { __LOCK_INITIALIZER, &__pthread_manager_thread, 0}, /* All NULLs */ };
41 /* For debugging purposes put the maximum number of threads in a variable. */
42 const int __linuxthreads_pthread_threads_max = PTHREAD_THREADS_MAX;
44 #ifndef THREAD_SELF
45 /* Indicate whether at least one thread has a user-defined stack (if 1),
46 or if all threads have stacks supplied by LinuxThreads (if 0). */
47 int __pthread_nonstandard_stacks;
48 #endif
50 /* Number of active entries in __pthread_handles (used by gdb) */
51 volatile int __pthread_handles_num = 2;
53 /* Whether to use debugger additional actions for thread creation
54 (set to 1 by gdb) */
55 volatile int __pthread_threads_debug;
57 /* Globally enabled events. */
58 volatile td_thr_events_t __pthread_threads_events;
60 /* Pointer to thread descriptor with last event. */
61 volatile pthread_descr __pthread_last_event;
63 /* Mapping from stack segment to thread descriptor. */
64 /* Stack segment numbers are also indices into the __pthread_handles array. */
65 /* Stack segment number 0 is reserved for the initial thread. */
67 #if FLOATING_STACKS
68 # define thread_segment(seq) NULL
69 #else
70 static inline pthread_descr thread_segment(int seg)
72 return (pthread_descr)(THREAD_STACK_START_ADDRESS - (seg - 1) * STACK_SIZE)
73 - 1;
75 #endif
77 /* Flag set in signal handler to record child termination */
79 static volatile int terminated_children;
81 /* Flag set when the initial thread is blocked on pthread_exit waiting
82 for all other threads to terminate */
84 static int main_thread_exiting;
86 /* Counter used to generate unique thread identifier.
87 Thread identifier is pthread_threads_counter + segment. */
89 static pthread_t pthread_threads_counter;
91 /* Forward declarations */
93 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
94 void * (*start_routine)(void *), void *arg,
95 sigset_t *mask, int father_pid,
96 int report_events,
97 td_thr_events_t *event_maskp);
98 static void pthread_handle_free(pthread_t th_id);
99 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
100 __attribute__ ((noreturn));
101 static void pthread_reap_children(void);
102 static void pthread_kill_all_threads(int sig, int main_thread_also);
104 /* The server thread managing requests for thread creation and termination */
107 __attribute__ ((noreturn))
108 __pthread_manager(void *arg)
110 int reqfd = (int) (long int) arg;
111 struct pollfd ufd;
112 sigset_t manager_mask;
113 int n;
114 struct pthread_request request;
116 /* If we have special thread_self processing, initialize it. */
117 #ifdef INIT_THREAD_SELF
118 INIT_THREAD_SELF(&__pthread_manager_thread, 1);
119 #endif
120 /* Set the error variable. */
121 __pthread_manager_thread.p_errnop = &__pthread_manager_thread.p_errno;
122 __pthread_manager_thread.p_h_errnop = &__pthread_manager_thread.p_h_errno;
123 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
124 sigfillset(&manager_mask);
125 sigdelset(&manager_mask, __pthread_sig_cancel); /* for thread termination */
126 sigdelset(&manager_mask, SIGTRAP); /* for debugging purposes */
127 if (__pthread_threads_debug && __pthread_sig_debug > 0)
128 sigdelset(&manager_mask, __pthread_sig_debug);
129 sigprocmask(SIG_SETMASK, &manager_mask, NULL);
130 /* Raise our priority to match that of main thread */
131 __pthread_manager_adjust_prio(__pthread_main_thread->p_priority);
132 /* Synchronize debugging of the thread manager */
133 n = TEMP_FAILURE_RETRY(__libc_read(reqfd, (char *)&request,
134 sizeof(request)));
135 ASSERT(n == sizeof(request) && request.req_kind == REQ_DEBUG);
136 ufd.fd = reqfd;
137 ufd.events = POLLIN;
138 /* Enter server loop */
139 while(1) {
140 n = __poll(&ufd, 1, 2000);
142 /* Check for termination of the main thread */
143 if (getppid() == 1) {
144 pthread_kill_all_threads(SIGKILL, 0);
145 _exit(0);
147 /* Check for dead children */
148 if (terminated_children) {
149 terminated_children = 0;
150 pthread_reap_children();
152 /* Read and execute request */
153 if (n == 1 && (ufd.revents & POLLIN)) {
154 n = TEMP_FAILURE_RETRY(__libc_read(reqfd, (char *)&request,
155 sizeof(request)));
156 #ifdef DEBUG
157 if (n < 0) {
158 char d[64];
159 write(STDERR_FILENO, d, snprintf(d, sizeof(d), "*** read err %m\n"));
160 } else if (n != sizeof(request)) {
161 write(STDERR_FILENO, "*** short read in manager\n", 26);
163 #endif
165 switch(request.req_kind) {
166 case REQ_CREATE:
167 request.req_thread->p_retcode =
168 pthread_handle_create((pthread_t *) &request.req_thread->p_retval,
169 request.req_args.create.attr,
170 request.req_args.create.fn,
171 request.req_args.create.arg,
172 &request.req_args.create.mask,
173 request.req_thread->p_pid,
174 request.req_thread->p_report_events,
175 &request.req_thread->p_eventbuf.eventmask);
176 restart(request.req_thread);
177 break;
178 case REQ_FREE:
179 pthread_handle_free(request.req_args.free.thread_id);
180 break;
181 case REQ_PROCESS_EXIT:
182 pthread_handle_exit(request.req_thread,
183 request.req_args.exit.code);
184 /* NOTREACHED */
185 break;
186 case REQ_MAIN_THREAD_EXIT:
187 main_thread_exiting = 1;
188 /* Reap children in case all other threads died and the signal handler
189 went off before we set main_thread_exiting to 1, and therefore did
190 not do REQ_KICK. */
191 pthread_reap_children();
193 if (__pthread_main_thread->p_nextlive == __pthread_main_thread) {
194 restart(__pthread_main_thread);
195 /* The main thread will now call exit() which will trigger an
196 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
197 to the thread manager. In case you are wondering how the
198 manager terminates from its loop here. */
200 break;
201 case REQ_POST:
202 __new_sem_post(request.req_args.post);
203 break;
204 case REQ_DEBUG:
205 /* Make gdb aware of new thread and gdb will restart the
206 new thread when it is ready to handle the new thread. */
207 if (__pthread_threads_debug && __pthread_sig_debug > 0)
208 raise(__pthread_sig_debug);
209 break;
210 case REQ_KICK:
211 /* This is just a prod to get the manager to reap some
212 threads right away, avoiding a potential delay at shutdown. */
213 break;
219 int __pthread_manager_event(void *arg)
221 /* If we have special thread_self processing, initialize it. */
222 #ifdef INIT_THREAD_SELF
223 INIT_THREAD_SELF(&__pthread_manager_thread, 1);
224 #endif
226 /* Get the lock the manager will free once all is correctly set up. */
227 __pthread_lock (THREAD_GETMEM((&__pthread_manager_thread), p_lock), NULL);
228 /* Free it immediately. */
229 __pthread_unlock (THREAD_GETMEM((&__pthread_manager_thread), p_lock));
231 return __pthread_manager(arg);
234 /* Process creation */
236 static int
237 __attribute__ ((noreturn))
238 pthread_start_thread(void *arg)
240 pthread_descr self = (pthread_descr) arg;
241 struct pthread_request request;
242 void * outcome;
243 #if HP_TIMING_AVAIL
244 hp_timing_t tmpclock;
245 #endif
246 /* Initialize special thread_self processing, if any. */
247 #ifdef INIT_THREAD_SELF
248 INIT_THREAD_SELF(self, self->p_nr);
249 #endif
250 #if HP_TIMING_AVAIL
251 HP_TIMING_NOW (tmpclock);
252 THREAD_SETMEM (self, p_cpuclock_offset, tmpclock);
253 #endif
254 /* Make sure our pid field is initialized, just in case we get there
255 before our father has initialized it. */
256 THREAD_SETMEM(self, p_pid, __getpid());
257 /* Initial signal mask is that of the creating thread. (Otherwise,
258 we'd just inherit the mask of the thread manager.) */
259 sigprocmask(SIG_SETMASK, &self->p_start_args.mask, NULL);
260 /* Set the scheduling policy and priority for the new thread, if needed */
261 if (THREAD_GETMEM(self, p_start_args.schedpolicy) >= 0)
262 /* Explicit scheduling attributes were provided: apply them */
263 __sched_setscheduler(THREAD_GETMEM(self, p_pid),
264 THREAD_GETMEM(self, p_start_args.schedpolicy),
265 &self->p_start_args.schedparam);
266 else if (__pthread_manager_thread.p_priority > 0)
267 /* Default scheduling required, but thread manager runs in realtime
268 scheduling: switch new thread to SCHED_OTHER policy */
270 struct sched_param default_params;
271 default_params.sched_priority = 0;
272 __sched_setscheduler(THREAD_GETMEM(self, p_pid),
273 SCHED_OTHER, &default_params);
275 /* Make gdb aware of new thread */
276 if (__pthread_threads_debug && __pthread_sig_debug > 0) {
277 request.req_thread = self;
278 request.req_kind = REQ_DEBUG;
279 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
280 (char *) &request, sizeof(request)));
281 suspend(self);
283 /* Run the thread code */
284 outcome = self->p_start_args.start_routine(THREAD_GETMEM(self,
285 p_start_args.arg));
286 /* Exit with the given return value */
287 __pthread_do_exit(outcome, CURRENT_STACK_FRAME);
290 static int
291 __attribute__ ((noreturn))
292 pthread_start_thread_event(void *arg)
294 pthread_descr self = (pthread_descr) arg;
296 #ifdef INIT_THREAD_SELF
297 INIT_THREAD_SELF(self, self->p_nr);
298 #endif
299 /* Make sure our pid field is initialized, just in case we get there
300 before our father has initialized it. */
301 THREAD_SETMEM(self, p_pid, __getpid());
302 /* Get the lock the manager will free once all is correctly set up. */
303 __pthread_lock (THREAD_GETMEM(self, p_lock), NULL);
304 /* Free it immediately. */
305 __pthread_unlock (THREAD_GETMEM(self, p_lock));
307 /* Continue with the real function. */
308 pthread_start_thread (arg);
311 static int pthread_allocate_stack(const pthread_attr_t *attr,
312 pthread_descr default_new_thread,
313 int pagesize,
314 pthread_descr * out_new_thread,
315 char ** out_new_thread_bottom,
316 char ** out_guardaddr,
317 size_t * out_guardsize)
319 pthread_descr new_thread;
320 char * new_thread_bottom;
321 char * guardaddr;
322 size_t stacksize, guardsize;
324 if (attr != NULL && attr->__stackaddr_set)
326 #ifdef _STACK_GROWS_UP
327 /* The user provided a stack. */
328 new_thread = (pthread_descr) attr->__stackaddr;
329 new_thread_bottom = (char *) (new_thread + 1);
330 guardaddr = attr->__stackaddr + attr->__stacksize;
331 guardsize = 0;
332 #else
333 /* The user provided a stack. For now we interpret the supplied
334 address as 1 + the highest addr. in the stack segment. If a
335 separate register stack is needed, we place it at the low end
336 of the segment, relying on the associated stacksize to
337 determine the low end of the segment. This differs from many
338 (but not all) other pthreads implementations. The intent is
339 that on machines with a single stack growing toward higher
340 addresses, stackaddr would be the lowest address in the stack
341 segment, so that it is consistently close to the initial sp
342 value. */
343 new_thread =
344 (pthread_descr) ((long)(attr->__stackaddr) & -sizeof(void *)) - 1;
345 new_thread_bottom = (char *) attr->__stackaddr - attr->__stacksize;
346 guardaddr = new_thread_bottom;
347 guardsize = 0;
348 #endif
349 #ifndef THREAD_SELF
350 __pthread_nonstandard_stacks = 1;
351 #endif
352 /* Clear the thread data structure. */
353 memset (new_thread, '\0', sizeof (*new_thread));
355 else
357 #ifdef NEED_SEPARATE_REGISTER_STACK
358 size_t granularity = 2 * pagesize;
359 /* Try to make stacksize/2 a multiple of pagesize */
360 #else
361 size_t granularity = pagesize;
362 #endif
363 void *map_addr;
365 /* Allocate space for stack and thread descriptor at default address */
366 #if FLOATING_STACKS
367 if (attr != NULL)
369 guardsize = page_roundup (attr->__guardsize, granularity);
370 stacksize = __pthread_max_stacksize - guardsize;
371 stacksize = MIN (stacksize,
372 page_roundup (attr->__stacksize, granularity));
374 else
376 guardsize = granularity;
377 stacksize = __pthread_max_stacksize - guardsize;
380 map_addr = mmap(NULL, stacksize + guardsize,
381 PROT_READ | PROT_WRITE | PROT_EXEC,
382 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
383 if (map_addr == MAP_FAILED)
384 /* No more memory available. */
385 return -1;
387 # ifdef NEED_SEPARATE_REGISTER_STACK
388 guardaddr = map_addr + stacksize / 2;
389 if (guardsize > 0)
390 mprotect (guardaddr, guardsize, PROT_NONE);
392 new_thread_bottom = (char *) map_addr;
393 new_thread = ((pthread_descr) (new_thread_bottom + stacksize
394 + guardsize)) - 1;
395 # elif _STACK_GROWS_DOWN
396 guardaddr = map_addr;
397 if (guardsize > 0)
398 mprotect (guardaddr, guardsize, PROT_NONE);
400 new_thread_bottom = (char *) map_addr + guardsize;
401 new_thread = ((pthread_descr) (new_thread_bottom + stacksize)) - 1;
402 # elif _STACK_GROWS_UP
403 guardaddr = map_addr + stacksize;
404 if (guardsize > 0)
405 mprotect (guardaddr, guardsize, PROT_NONE);
407 new_thread = (pthread_descr) map_addr;
408 new_thread_bottom = (char *) (new_thread + 1);
409 # else
410 # error You must define a stack direction
411 # endif /* Stack direction */
412 #else /* !FLOATING_STACKS */
413 void *res_addr;
415 if (attr != NULL)
417 guardsize = page_roundup (attr->__guardsize, granularity);
418 stacksize = STACK_SIZE - guardsize;
419 stacksize = MIN (stacksize,
420 page_roundup (attr->__stacksize, granularity));
422 else
424 guardsize = granularity;
425 stacksize = STACK_SIZE - granularity;
428 # ifdef NEED_SEPARATE_REGISTER_STACK
429 new_thread = default_new_thread;
430 new_thread_bottom = (char *) (new_thread + 1) - stacksize - guardsize;
431 /* Includes guard area, unlike the normal case. Use the bottom
432 end of the segment as backing store for the register stack.
433 Needed on IA64. In this case, we also map the entire stack at
434 once. According to David Mosberger, that's cheaper. It also
435 avoids the risk of intermittent failures due to other mappings
436 in the same region. The cost is that we might be able to map
437 slightly fewer stacks. */
439 /* First the main stack: */
440 map_addr = (caddr_t)((char *)(new_thread + 1) - stacksize / 2);
441 res_addr = mmap(map_addr, stacksize / 2,
442 PROT_READ | PROT_WRITE | PROT_EXEC,
443 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
444 if (res_addr != map_addr)
446 /* Bad luck, this segment is already mapped. */
447 if (res_addr != MAP_FAILED)
448 munmap(res_addr, stacksize / 2);
449 return -1;
451 /* Then the register stack: */
452 map_addr = (caddr_t)new_thread_bottom;
453 res_addr = mmap(map_addr, stacksize/2,
454 PROT_READ | PROT_WRITE | PROT_EXEC,
455 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
456 if (res_addr != map_addr)
458 if (res_addr != MAP_FAILED)
459 munmap(res_addr, stacksize / 2);
460 munmap((caddr_t)((char *)(new_thread + 1) - stacksize/2),
461 stacksize/2);
462 return -1;
465 guardaddr = new_thread_bottom + stacksize/2;
466 /* We leave the guard area in the middle unmapped. */
467 # else /* !NEED_SEPARATE_REGISTER_STACK */
468 # ifdef _STACK_GROWS_DOWN
469 new_thread = default_new_thread;
470 new_thread_bottom = (char *) (new_thread + 1) - stacksize;
471 map_addr = new_thread_bottom - guardsize;
472 res_addr = mmap(map_addr, stacksize + guardsize,
473 PROT_READ | PROT_WRITE | PROT_EXEC,
474 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
475 if (res_addr != map_addr)
477 /* Bad luck, this segment is already mapped. */
478 if (res_addr != MAP_FAILED)
479 munmap (res_addr, stacksize + guardsize);
480 return -1;
483 /* We manage to get a stack. Protect the guard area pages if
484 necessary. */
485 guardaddr = map_addr;
486 if (guardsize > 0)
487 mprotect (guardaddr, guardsize, PROT_NONE);
488 # else
489 /* The thread description goes at the bottom of this area, and
490 * the stack starts directly above it.
492 new_thread = (pthread_descr)((unsigned long)default_new_thread &~ (STACK_SIZE - 1));
493 map_addr = mmap(new_thread, stacksize + guardsize,
494 PROT_READ | PROT_WRITE | PROT_EXEC,
495 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
496 if (map_addr == MAP_FAILED)
497 return -1;
499 new_thread_bottom = map_addr + sizeof(*new_thread);
500 guardaddr = map_addr + stacksize;
501 if (guardsize > 0)
502 mprotect (guardaddr, guardsize, PROT_NONE);
504 # endif /* stack direction */
505 # endif /* !NEED_SEPARATE_REGISTER_STACK */
506 #endif /* !FLOATING_STACKS */
508 *out_new_thread = new_thread;
509 *out_new_thread_bottom = new_thread_bottom;
510 *out_guardaddr = guardaddr;
511 *out_guardsize = guardsize;
512 return 0;
515 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
516 void * (*start_routine)(void *), void *arg,
517 sigset_t * mask, int father_pid,
518 int report_events,
519 td_thr_events_t *event_maskp)
521 size_t sseg;
522 int pid;
523 pthread_descr new_thread;
524 char * new_thread_bottom;
525 pthread_t new_thread_id;
526 char *guardaddr = NULL;
527 size_t guardsize = 0;
528 int pagesize = __getpagesize();
529 int saved_errno;
530 sigset_t newmask, oldmask;
532 /* First check whether we have to change the policy and if yes, whether
533 we can do this. Normally this should be done by examining the
534 return value of the __sched_setscheduler call in pthread_start_thread
535 but this is hard to implement. FIXME */
536 if (attr != NULL && attr->__schedpolicy != SCHED_OTHER && geteuid () != 0)
537 return EPERM;
538 /* Find a free segment for the thread, and allocate a stack if needed */
539 for (sseg = 2; ; sseg++)
541 if (sseg >= PTHREAD_THREADS_MAX)
542 return EAGAIN;
543 if (__pthread_handles[sseg].h_descr != NULL)
544 continue;
545 if (pthread_allocate_stack(attr, thread_segment(sseg),
546 pagesize,
547 &new_thread, &new_thread_bottom,
548 &guardaddr, &guardsize) == 0)
549 break;
551 __pthread_handles_num++;
552 /* Allocate new thread identifier */
553 pthread_threads_counter += PTHREAD_THREADS_MAX;
554 new_thread_id = sseg + pthread_threads_counter;
555 /* Initialize the thread descriptor. Elements which have to be
556 initialized to zero already have this value. */
557 new_thread->p_tid = new_thread_id;
558 new_thread->p_lock = &(__pthread_handles[sseg].h_lock);
559 new_thread->p_cancelstate = PTHREAD_CANCEL_ENABLE;
560 new_thread->p_canceltype = PTHREAD_CANCEL_DEFERRED;
561 new_thread->p_errnop = &new_thread->p_errno;
562 new_thread->p_h_errnop = &new_thread->p_h_errno;
563 new_thread->p_resp = &new_thread->p_res;
564 new_thread->p_guardaddr = guardaddr;
565 new_thread->p_guardsize = guardsize;
566 new_thread->p_header.data.self = new_thread;
567 new_thread->p_nr = sseg;
568 new_thread->p_inheritsched = attr ? attr->__inheritsched : 0;
569 /* Initialize the thread handle */
570 __pthread_init_lock(&__pthread_handles[sseg].h_lock);
571 __pthread_handles[sseg].h_descr = new_thread;
572 __pthread_handles[sseg].h_bottom = new_thread_bottom;
573 /* Determine scheduling parameters for the thread */
574 new_thread->p_start_args.schedpolicy = -1;
575 if (attr != NULL) {
576 new_thread->p_detached = attr->__detachstate;
577 new_thread->p_userstack = attr->__stackaddr_set;
579 switch(attr->__inheritsched) {
580 case PTHREAD_EXPLICIT_SCHED:
581 new_thread->p_start_args.schedpolicy = attr->__schedpolicy;
582 memcpy (&new_thread->p_start_args.schedparam, &attr->__schedparam,
583 sizeof (struct sched_param));
584 break;
585 case PTHREAD_INHERIT_SCHED:
586 new_thread->p_start_args.schedpolicy = __sched_getscheduler(father_pid);
587 __sched_getparam(father_pid, &new_thread->p_start_args.schedparam);
588 break;
590 new_thread->p_priority =
591 new_thread->p_start_args.schedparam.sched_priority;
593 /* Finish setting up arguments to pthread_start_thread */
594 new_thread->p_start_args.start_routine = start_routine;
595 new_thread->p_start_args.arg = arg;
596 new_thread->p_start_args.mask = *mask;
597 /* Make the new thread ID available already now. If any of the later
598 functions fail we return an error value and the caller must not use
599 the stored thread ID. */
600 *thread = new_thread_id;
601 /* Raise priority of thread manager if needed */
602 __pthread_manager_adjust_prio(new_thread->p_priority);
603 /* Do the cloning. We have to use two different functions depending
604 on whether we are debugging or not. */
605 pid = 0; /* Note that the thread never can have PID zero. */
606 if (report_events)
608 /* See whether the TD_CREATE event bit is set in any of the
609 masks. */
610 int idx = __td_eventword (TD_CREATE);
611 uint32_t mask = __td_eventmask (TD_CREATE);
613 if ((mask & (__pthread_threads_events.event_bits[idx]
614 | event_maskp->event_bits[idx])) != 0)
616 /* Block cancel signal in the child until it is fully
617 initialized. */
618 sigemptyset(&newmask);
619 sigaddset(&newmask, __pthread_sig_cancel);
620 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
621 /* Lock the mutex the child will use now so that it will stop. */
622 __pthread_lock(new_thread->p_lock, NULL);
624 /* We have to report this event. */
625 #ifdef NEED_SEPARATE_REGISTER_STACK
626 /* Perhaps this version should be used on all platforms. But
627 this requires that __clone2 be uniformly supported
628 everywhere.
630 And there is some argument for changing the __clone2
631 interface to pass sp and bsp instead, making it more IA64
632 specific, but allowing stacks to grow outward from each
633 other, to get less paging and fewer mmaps. */
634 pid = __clone2(pthread_start_thread_event,
635 (void **)new_thread_bottom,
636 (char *)new_thread - new_thread_bottom,
637 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
638 __pthread_sig_cancel, new_thread);
639 #elif _STACK_GROWS_UP
640 pid = __clone(pthread_start_thread_event, (void **) new_thread_bottom,
641 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
642 __pthread_sig_cancel, new_thread);
643 #else
644 pid = __clone(pthread_start_thread_event, (void **) new_thread,
645 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
646 __pthread_sig_cancel, new_thread);
647 #endif
648 saved_errno = errno;
649 sigprocmask(SIG_SETMASK, &oldmask, NULL);
650 if (pid != -1)
652 /* Now fill in the information about the new thread in
653 the newly created thread's data structure. We cannot let
654 the new thread do this since we don't know whether it was
655 already scheduled when we send the event. */
656 new_thread->p_eventbuf.eventdata = new_thread;
657 new_thread->p_eventbuf.eventnum = TD_CREATE;
658 __pthread_last_event = new_thread;
660 /* We have to set the PID here since the callback function
661 in the debug library will need it and we cannot guarantee
662 the child got scheduled before the debugger. */
663 new_thread->p_pid = pid;
665 /* Now call the function which signals the event. */
666 __linuxthreads_create_event ();
668 /* Now restart the thread. */
669 __pthread_unlock(new_thread->p_lock);
673 if (pid == 0)
675 /* Block cancel signal in the child until it is fully
676 initialized. */
677 sigemptyset(&newmask);
678 sigaddset(&newmask, __pthread_sig_cancel);
679 sigprocmask(SIG_BLOCK, &newmask, &oldmask);
680 #ifdef NEED_SEPARATE_REGISTER_STACK
681 pid = __clone2(pthread_start_thread,
682 (void **)new_thread_bottom,
683 (char *)new_thread - new_thread_bottom,
684 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
685 __pthread_sig_cancel, new_thread);
686 #elif _STACK_GROWS_UP
687 pid = __clone(pthread_start_thread, (void **) new_thread_bottom,
688 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
689 __pthread_sig_cancel, new_thread);
690 #else
691 pid = __clone(pthread_start_thread, (void **) new_thread,
692 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
693 __pthread_sig_cancel, new_thread);
694 #endif /* !NEED_SEPARATE_REGISTER_STACK */
695 saved_errno = errno;
696 sigprocmask(SIG_SETMASK, &oldmask, NULL);
698 /* Check if cloning succeeded */
699 if (pid == -1) {
700 /* Free the stack if we allocated it */
701 if (attr == NULL || !attr->__stackaddr_set)
703 #ifdef NEED_SEPARATE_REGISTER_STACK
704 size_t stacksize = ((char *)(new_thread->p_guardaddr)
705 - new_thread_bottom);
706 munmap((caddr_t)new_thread_bottom,
707 2 * stacksize + new_thread->p_guardsize);
708 #elif _STACK_GROWS_UP
709 size_t stacksize = guardaddr - (char *)new_thread;
710 munmap(new_thread, stacksize + guardsize);
711 #else
712 size_t stacksize = (char *)(new_thread+1) - new_thread_bottom;
713 munmap(new_thread_bottom - guardsize, guardsize + stacksize);
714 #endif
716 __pthread_handles[sseg].h_descr = NULL;
717 __pthread_handles[sseg].h_bottom = NULL;
718 __pthread_handles_num--;
719 return saved_errno;
721 /* Insert new thread in doubly linked list of active threads */
722 new_thread->p_prevlive = __pthread_main_thread;
723 new_thread->p_nextlive = __pthread_main_thread->p_nextlive;
724 __pthread_main_thread->p_nextlive->p_prevlive = new_thread;
725 __pthread_main_thread->p_nextlive = new_thread;
726 /* Set pid field of the new thread, in case we get there before the
727 child starts. */
728 new_thread->p_pid = pid;
729 return 0;
733 /* Try to free the resources of a thread when requested by pthread_join
734 or pthread_detach on a terminated thread. */
736 static void pthread_free(pthread_descr th)
738 pthread_handle handle;
739 pthread_readlock_info *iter, *next;
741 ASSERT(th->p_exited);
742 /* Make the handle invalid */
743 handle = thread_handle(th->p_tid);
744 __pthread_lock(&handle->h_lock, NULL);
745 handle->h_descr = NULL;
746 handle->h_bottom = (char *)(-1L);
747 __pthread_unlock(&handle->h_lock);
748 #ifdef FREE_THREAD
749 FREE_THREAD(th, th->p_nr);
750 #endif
751 /* One fewer threads in __pthread_handles */
752 __pthread_handles_num--;
754 /* Destroy read lock list, and list of free read lock structures.
755 If the former is not empty, it means the thread exited while
756 holding read locks! */
758 for (iter = th->p_readlock_list; iter != NULL; iter = next)
760 next = iter->pr_next;
761 free(iter);
764 for (iter = th->p_readlock_free; iter != NULL; iter = next)
766 next = iter->pr_next;
767 free(iter);
770 /* If initial thread, nothing to free */
771 if (!th->p_userstack)
773 size_t guardsize = th->p_guardsize;
774 /* Free the stack and thread descriptor area */
775 char *guardaddr = th->p_guardaddr;
776 #ifdef _STACK_GROWS_UP
777 size_t stacksize = guardaddr - (char *)th;
778 guardaddr = (char *)th;
779 #else
780 /* Guardaddr is always set, even if guardsize is 0. This allows
781 us to compute everything else. */
782 size_t stacksize = (char *)(th+1) - guardaddr - guardsize;
783 #ifdef NEED_SEPARATE_REGISTER_STACK
784 /* Take account of the register stack, which is below guardaddr. */
785 guardaddr -= stacksize;
786 stacksize *= 2;
787 #endif
788 #endif
789 /* Unmap the stack. */
790 munmap(guardaddr, stacksize + guardsize);
794 /* Handle threads that have exited */
796 static void pthread_exited(pid_t pid)
798 pthread_descr th;
799 int detached;
800 /* Find thread with that pid */
801 for (th = __pthread_main_thread->p_nextlive;
802 th != __pthread_main_thread;
803 th = th->p_nextlive) {
804 if (th->p_pid == pid) {
805 /* Remove thread from list of active threads */
806 th->p_nextlive->p_prevlive = th->p_prevlive;
807 th->p_prevlive->p_nextlive = th->p_nextlive;
808 /* Mark thread as exited, and if detached, free its resources */
809 __pthread_lock(th->p_lock, NULL);
810 th->p_exited = 1;
811 /* If we have to signal this event do it now. */
812 if (th->p_report_events)
814 /* See whether TD_REAP is in any of the mask. */
815 int idx = __td_eventword (TD_REAP);
816 uint32_t mask = __td_eventmask (TD_REAP);
818 if ((mask & (__pthread_threads_events.event_bits[idx]
819 | th->p_eventbuf.eventmask.event_bits[idx])) != 0)
821 /* Yep, we have to signal the reapage. */
822 th->p_eventbuf.eventnum = TD_REAP;
823 th->p_eventbuf.eventdata = th;
824 __pthread_last_event = th;
826 /* Now call the function to signal the event. */
827 __linuxthreads_reap_event();
830 detached = th->p_detached;
831 __pthread_unlock(th->p_lock);
832 if (detached)
833 pthread_free(th);
834 break;
837 /* If all threads have exited and the main thread is pending on a
838 pthread_exit, wake up the main thread and terminate ourselves. */
839 if (main_thread_exiting &&
840 __pthread_main_thread->p_nextlive == __pthread_main_thread) {
841 restart(__pthread_main_thread);
842 /* Same logic as REQ_MAIN_THREAD_EXIT. */
846 static void pthread_reap_children(void)
848 pid_t pid;
849 int status;
851 while ((pid = __libc_waitpid(-1, &status, WNOHANG | __WCLONE)) > 0) {
852 pthread_exited(pid);
853 if (WIFSIGNALED(status)) {
854 /* If a thread died due to a signal, send the same signal to
855 all other threads, including the main thread. */
856 pthread_kill_all_threads(WTERMSIG(status), 1);
857 _exit(0);
862 /* Try to free the resources of a thread when requested by pthread_join
863 or pthread_detach on a terminated thread. */
865 static void pthread_handle_free(pthread_t th_id)
867 pthread_handle handle = thread_handle(th_id);
868 pthread_descr th;
870 __pthread_lock(&handle->h_lock, NULL);
871 if (nonexisting_handle(handle, th_id)) {
872 /* pthread_reap_children has deallocated the thread already,
873 nothing needs to be done */
874 __pthread_unlock(&handle->h_lock);
875 return;
877 th = handle->h_descr;
878 if (th->p_exited) {
879 __pthread_unlock(&handle->h_lock);
880 pthread_free(th);
881 } else {
882 /* The Unix process of the thread is still running.
883 Mark the thread as detached so that the thread manager will
884 deallocate its resources when the Unix process exits. */
885 th->p_detached = 1;
886 __pthread_unlock(&handle->h_lock);
890 /* Send a signal to all running threads */
892 static void pthread_kill_all_threads(int sig, int main_thread_also)
894 pthread_descr th;
895 for (th = __pthread_main_thread->p_nextlive;
896 th != __pthread_main_thread;
897 th = th->p_nextlive) {
898 kill(th->p_pid, sig);
900 if (main_thread_also) {
901 kill(__pthread_main_thread->p_pid, sig);
905 /* Process-wide exit() */
907 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
909 pthread_descr th;
910 __pthread_exit_requested = 1;
911 __pthread_exit_code = exitcode;
912 /* Send the CANCEL signal to all running threads, including the main
913 thread, but excluding the thread from which the exit request originated
914 (that thread must complete the exit, e.g. calling atexit functions
915 and flushing stdio buffers). */
916 for (th = issuing_thread->p_nextlive;
917 th != issuing_thread;
918 th = th->p_nextlive) {
919 kill(th->p_pid, __pthread_sig_cancel);
921 /* Now, wait for all these threads, so that they don't become zombies
922 and their times are properly added to the thread manager's times. */
923 for (th = issuing_thread->p_nextlive;
924 th != issuing_thread;
925 th = th->p_nextlive) {
926 waitpid(th->p_pid, NULL, __WCLONE);
928 restart(issuing_thread);
929 _exit(0);
932 /* Handler for __pthread_sig_cancel in thread manager thread */
934 void __pthread_manager_sighandler(int sig)
936 int kick_manager = terminated_children == 0 && main_thread_exiting;
937 terminated_children = 1;
939 /* If the main thread is terminating, kick the thread manager loop
940 each time some threads terminate. This eliminates a two second
941 shutdown delay caused by the thread manager sleeping in the
942 call to __poll(). Instead, the thread manager is kicked into
943 action, reaps the outstanding threads and resumes the main thread
944 so that it can complete the shutdown. */
946 if (kick_manager) {
947 struct pthread_request request;
948 request.req_thread = 0;
949 request.req_kind = REQ_KICK;
950 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
951 (char *) &request, sizeof(request)));
955 /* Adjust priority of thread manager so that it always run at a priority
956 higher than all threads */
958 void __pthread_manager_adjust_prio(int thread_prio)
960 struct sched_param param;
962 if (thread_prio <= __pthread_manager_thread.p_priority) return;
963 param.sched_priority =
964 thread_prio < __sched_get_priority_max(SCHED_FIFO)
965 ? thread_prio + 1 : thread_prio;
966 __sched_setscheduler(__pthread_manager_thread.p_pid, SCHED_FIFO, &param);
967 __pthread_manager_thread.p_priority = thread_prio;