Update.
[glibc.git] / linuxthreads / manager.c
blobc7fd9653f2d54bae7417204713fef52b6426ede0
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 = __libc_read(reqfd, (char *)&request, sizeof(request));
134 ASSERT(n == sizeof(request) && request.req_kind == REQ_DEBUG);
135 ufd.fd = reqfd;
136 ufd.events = POLLIN;
137 /* Enter server loop */
138 while(1) {
139 n = __poll(&ufd, 1, 2000);
141 /* Check for termination of the main thread */
142 if (getppid() == 1) {
143 pthread_kill_all_threads(SIGKILL, 0);
144 _exit(0);
146 /* Check for dead children */
147 if (terminated_children) {
148 terminated_children = 0;
149 pthread_reap_children();
151 /* Read and execute request */
152 if (n == 1 && (ufd.revents & POLLIN)) {
153 n = __libc_read(reqfd, (char *)&request, sizeof(request));
154 ASSERT(n == sizeof(request));
155 switch(request.req_kind) {
156 case REQ_CREATE:
157 request.req_thread->p_retcode =
158 pthread_handle_create((pthread_t *) &request.req_thread->p_retval,
159 request.req_args.create.attr,
160 request.req_args.create.fn,
161 request.req_args.create.arg,
162 &request.req_args.create.mask,
163 request.req_thread->p_pid,
164 request.req_thread->p_report_events,
165 &request.req_thread->p_eventbuf.eventmask);
166 restart(request.req_thread);
167 break;
168 case REQ_FREE:
169 pthread_handle_free(request.req_args.free.thread_id);
170 break;
171 case REQ_PROCESS_EXIT:
172 pthread_handle_exit(request.req_thread,
173 request.req_args.exit.code);
174 /* NOTREACHED */
175 break;
176 case REQ_MAIN_THREAD_EXIT:
177 main_thread_exiting = 1;
178 /* Reap children in case all other threads died and the signal handler
179 went off before we set main_thread_exiting to 1, and therefore did
180 not do REQ_KICK. */
181 pthread_reap_children();
183 if (__pthread_main_thread->p_nextlive == __pthread_main_thread) {
184 restart(__pthread_main_thread);
185 /* The main thread will now call exit() which will trigger an
186 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
187 to the thread manager. In case you are wondering how the
188 manager terminates from its loop here. */
190 break;
191 case REQ_POST:
192 __new_sem_post(request.req_args.post);
193 break;
194 case REQ_DEBUG:
195 /* Make gdb aware of new thread and gdb will restart the
196 new thread when it is ready to handle the new thread. */
197 if (__pthread_threads_debug && __pthread_sig_debug > 0)
198 raise(__pthread_sig_debug);
199 break;
200 case REQ_KICK:
201 /* This is just a prod to get the manager to reap some
202 threads right away, avoiding a potential delay at shutdown. */
203 break;
209 int __pthread_manager_event(void *arg)
211 /* If we have special thread_self processing, initialize it. */
212 #ifdef INIT_THREAD_SELF
213 INIT_THREAD_SELF(&__pthread_manager_thread, 1);
214 #endif
216 /* Get the lock the manager will free once all is correctly set up. */
217 __pthread_lock (THREAD_GETMEM((&__pthread_manager_thread), p_lock), NULL);
218 /* Free it immediately. */
219 __pthread_unlock (THREAD_GETMEM((&__pthread_manager_thread), p_lock));
221 return __pthread_manager(arg);
224 /* Process creation */
226 static int
227 __attribute__ ((noreturn))
228 pthread_start_thread(void *arg)
230 pthread_descr self = (pthread_descr) arg;
231 struct pthread_request request;
232 void * outcome;
233 #if HP_TIMING_AVAIL
234 hp_timing_t tmpclock;
235 #endif
236 /* Initialize special thread_self processing, if any. */
237 #ifdef INIT_THREAD_SELF
238 INIT_THREAD_SELF(self, self->p_nr);
239 #endif
240 #if HP_TIMING_AVAIL
241 HP_TIMING_NOW (tmpclock);
242 THREAD_SETMEM (self, p_cpuclock_offset, tmpclock);
243 #endif
244 /* Make sure our pid field is initialized, just in case we get there
245 before our father has initialized it. */
246 THREAD_SETMEM(self, p_pid, __getpid());
247 /* Initial signal mask is that of the creating thread. (Otherwise,
248 we'd just inherit the mask of the thread manager.) */
249 sigprocmask(SIG_SETMASK, &self->p_start_args.mask, NULL);
250 /* Set the scheduling policy and priority for the new thread, if needed */
251 if (THREAD_GETMEM(self, p_start_args.schedpolicy) >= 0)
252 /* Explicit scheduling attributes were provided: apply them */
253 __sched_setscheduler(THREAD_GETMEM(self, p_pid),
254 THREAD_GETMEM(self, p_start_args.schedpolicy),
255 &self->p_start_args.schedparam);
256 else if (__pthread_manager_thread.p_priority > 0)
257 /* Default scheduling required, but thread manager runs in realtime
258 scheduling: switch new thread to SCHED_OTHER policy */
260 struct sched_param default_params;
261 default_params.sched_priority = 0;
262 __sched_setscheduler(THREAD_GETMEM(self, p_pid),
263 SCHED_OTHER, &default_params);
265 /* Make gdb aware of new thread */
266 if (__pthread_threads_debug && __pthread_sig_debug > 0) {
267 request.req_thread = self;
268 request.req_kind = REQ_DEBUG;
269 __libc_write(__pthread_manager_request,
270 (char *) &request, sizeof(request));
271 suspend(self);
273 /* Run the thread code */
274 outcome = self->p_start_args.start_routine(THREAD_GETMEM(self,
275 p_start_args.arg));
276 /* Exit with the given return value */
277 __pthread_do_exit(outcome, CURRENT_STACK_FRAME);
280 static int
281 __attribute__ ((noreturn))
282 pthread_start_thread_event(void *arg)
284 pthread_descr self = (pthread_descr) arg;
286 #ifdef INIT_THREAD_SELF
287 INIT_THREAD_SELF(self, self->p_nr);
288 #endif
289 /* Make sure our pid field is initialized, just in case we get there
290 before our father has initialized it. */
291 THREAD_SETMEM(self, p_pid, __getpid());
292 /* Get the lock the manager will free once all is correctly set up. */
293 __pthread_lock (THREAD_GETMEM(self, p_lock), NULL);
294 /* Free it immediately. */
295 __pthread_unlock (THREAD_GETMEM(self, p_lock));
297 /* Continue with the real function. */
298 pthread_start_thread (arg);
301 static int pthread_allocate_stack(const pthread_attr_t *attr,
302 pthread_descr default_new_thread,
303 int pagesize,
304 pthread_descr * out_new_thread,
305 char ** out_new_thread_bottom,
306 char ** out_guardaddr,
307 size_t * out_guardsize)
309 pthread_descr new_thread;
310 char * new_thread_bottom;
311 char * guardaddr;
312 size_t stacksize, guardsize;
314 if (attr != NULL && attr->__stackaddr_set)
316 #ifdef _STACK_GROWS_UP
317 /* The user provided a stack. */
318 new_thread = (pthread_descr) attr->__stackaddr;
319 new_thread_bottom = (char *) (new_thread + 1);
320 guardaddr = attr->__stackaddr + attr->__stacksize;
321 guardsize = 0;
322 #else
323 /* The user provided a stack. For now we interpret the supplied
324 address as 1 + the highest addr. in the stack segment. If a
325 separate register stack is needed, we place it at the low end
326 of the segment, relying on the associated stacksize to
327 determine the low end of the segment. This differs from many
328 (but not all) other pthreads implementations. The intent is
329 that on machines with a single stack growing toward higher
330 addresses, stackaddr would be the lowest address in the stack
331 segment, so that it is consistently close to the initial sp
332 value. */
333 new_thread =
334 (pthread_descr) ((long)(attr->__stackaddr) & -sizeof(void *)) - 1;
335 new_thread_bottom = (char *) attr->__stackaddr - attr->__stacksize;
336 guardaddr = new_thread_bottom;
337 guardsize = 0;
338 #endif
339 #ifndef THREAD_SELF
340 __pthread_nonstandard_stacks = 1;
341 #endif
342 /* Clear the thread data structure. */
343 memset (new_thread, '\0', sizeof (*new_thread));
345 else
347 #ifdef NEED_SEPARATE_REGISTER_STACK
348 size_t granularity = 2 * pagesize;
349 /* Try to make stacksize/2 a multiple of pagesize */
350 #else
351 size_t granularity = pagesize;
352 #endif
353 void *map_addr;
355 /* Allocate space for stack and thread descriptor at default address */
356 #if FLOATING_STACKS
357 if (attr != NULL)
359 guardsize = page_roundup (attr->__guardsize, granularity);
360 stacksize = __pthread_max_stacksize - guardsize;
361 stacksize = MIN (stacksize,
362 page_roundup (attr->__stacksize, granularity));
364 else
366 guardsize = granularity;
367 stacksize = __pthread_max_stacksize - guardsize;
370 map_addr = mmap(NULL, stacksize + guardsize,
371 PROT_READ | PROT_WRITE | PROT_EXEC,
372 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
373 if (map_addr == MAP_FAILED)
374 /* No more memory available. */
375 return -1;
377 # ifdef NEED_SEPARATE_REGISTER_STACK
378 guardaddr = map_addr + stacksize / 2;
379 if (guardsize > 0)
380 mprotect (guardaddr, guardsize, PROT_NONE);
382 new_thread_bottom = (char *) map_addr;
383 new_thread = ((pthread_descr) (new_thread_bottom + stacksize
384 + guardsize)) - 1;
385 # elif _STACK_GROWS_DOWN
386 guardaddr = map_addr;
387 if (guardsize > 0)
388 mprotect (guardaddr, guardsize, PROT_NONE);
390 new_thread_bottom = (char *) map_addr + guardsize;
391 new_thread = ((pthread_descr) (new_thread_bottom + stacksize)) - 1;
392 # elif _STACK_GROWS_UP
393 guardaddr = map_addr + stacksize;
394 if (guardsize > 0)
395 mprotect (guardaddr, guardsize, PROT_NONE);
397 new_thread = (pthread_descr) map_addr;
398 new_thread_bottom = (char *) (new_thread + 1);
399 # else
400 # error You must define a stack direction
401 # endif /* Stack direction */
402 #else /* !FLOATING_STACKS */
403 void *res_addr;
405 if (attr != NULL)
407 guardsize = page_roundup (attr->__guardsize, granularity);
408 stacksize = STACK_SIZE - guardsize;
409 stacksize = MIN (stacksize,
410 page_roundup (attr->__stacksize, granularity));
412 else
414 guardsize = granularity;
415 stacksize = STACK_SIZE - granularity;
418 # ifdef NEED_SEPARATE_REGISTER_STACK
419 new_thread = default_new_thread;
420 new_thread_bottom = (char *) (new_thread + 1) - stacksize - guardsize;
421 /* Includes guard area, unlike the normal case. Use the bottom
422 end of the segment as backing store for the register stack.
423 Needed on IA64. In this case, we also map the entire stack at
424 once. According to David Mosberger, that's cheaper. It also
425 avoids the risk of intermittent failures due to other mappings
426 in the same region. The cost is that we might be able to map
427 slightly fewer stacks. */
429 /* First the main stack: */
430 map_addr = (caddr_t)((char *)(new_thread + 1) - stacksize / 2);
431 res_addr = mmap(map_addr, stacksize / 2,
432 PROT_READ | PROT_WRITE | PROT_EXEC,
433 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
434 if (res_addr != map_addr)
436 /* Bad luck, this segment is already mapped. */
437 if (res_addr != MAP_FAILED)
438 munmap(res_addr, stacksize / 2);
439 return -1;
441 /* Then the register stack: */
442 map_addr = (caddr_t)new_thread_bottom;
443 res_addr = mmap(map_addr, stacksize/2,
444 PROT_READ | PROT_WRITE | PROT_EXEC,
445 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
446 if (res_addr != map_addr)
448 if (res_addr != MAP_FAILED)
449 munmap(res_addr, stacksize / 2);
450 munmap((caddr_t)((char *)(new_thread + 1) - stacksize/2),
451 stacksize/2);
452 return -1;
455 guardaddr = new_thread_bottom + stacksize/2;
456 /* We leave the guard area in the middle unmapped. */
457 # else /* !NEED_SEPARATE_REGISTER_STACK */
458 # ifdef _STACK_GROWS_DOWN
459 new_thread = default_new_thread;
460 new_thread_bottom = (char *) (new_thread + 1) - stacksize;
461 map_addr = new_thread_bottom - guardsize;
462 res_addr = mmap(map_addr, stacksize + guardsize,
463 PROT_READ | PROT_WRITE | PROT_EXEC,
464 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
465 if (res_addr != map_addr)
467 /* Bad luck, this segment is already mapped. */
468 if (res_addr != MAP_FAILED)
469 munmap (res_addr, stacksize + guardsize);
470 return -1;
473 /* We manage to get a stack. Protect the guard area pages if
474 necessary. */
475 guardaddr = map_addr;
476 if (guardsize > 0)
477 mprotect (guardaddr, guardsize, PROT_NONE);
478 # else
479 /* The thread description goes at the bottom of this area, and
480 * the stack starts directly above it.
482 new_thread = (pthread_descr)((unsigned long)default_new_thread &~ (STACK_SIZE - 1));
483 map_addr = mmap(new_thread, stacksize + guardsize,
484 PROT_READ | PROT_WRITE | PROT_EXEC,
485 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
486 if (map_addr == MAP_FAILED)
487 return -1;
489 new_thread_bottom = map_addr + sizeof(*new_thread);
490 guardaddr = map_addr + stacksize;
491 if (guardsize > 0)
492 mprotect (guardaddr, guardsize, PROT_NONE);
494 # endif /* stack direction */
495 # endif /* !NEED_SEPARATE_REGISTER_STACK */
496 #endif /* !FLOATING_STACKS */
498 *out_new_thread = new_thread;
499 *out_new_thread_bottom = new_thread_bottom;
500 *out_guardaddr = guardaddr;
501 *out_guardsize = guardsize;
502 return 0;
505 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
506 void * (*start_routine)(void *), void *arg,
507 sigset_t * mask, int father_pid,
508 int report_events,
509 td_thr_events_t *event_maskp)
511 size_t sseg;
512 int pid;
513 pthread_descr new_thread;
514 char * new_thread_bottom;
515 pthread_t new_thread_id;
516 char *guardaddr = NULL;
517 size_t guardsize = 0;
518 int pagesize = __getpagesize();
520 /* First check whether we have to change the policy and if yes, whether
521 we can do this. Normally this should be done by examining the
522 return value of the __sched_setscheduler call in pthread_start_thread
523 but this is hard to implement. FIXME */
524 if (attr != NULL && attr->__schedpolicy != SCHED_OTHER && geteuid () != 0)
525 return EPERM;
526 /* Find a free segment for the thread, and allocate a stack if needed */
527 for (sseg = 2; ; sseg++)
529 if (sseg >= PTHREAD_THREADS_MAX)
530 return EAGAIN;
531 if (__pthread_handles[sseg].h_descr != NULL)
532 continue;
533 if (pthread_allocate_stack(attr, thread_segment(sseg),
534 pagesize,
535 &new_thread, &new_thread_bottom,
536 &guardaddr, &guardsize) == 0)
537 break;
539 __pthread_handles_num++;
540 /* Allocate new thread identifier */
541 pthread_threads_counter += PTHREAD_THREADS_MAX;
542 new_thread_id = sseg + pthread_threads_counter;
543 /* Initialize the thread descriptor. Elements which have to be
544 initialized to zero already have this value. */
545 new_thread->p_tid = new_thread_id;
546 new_thread->p_lock = &(__pthread_handles[sseg].h_lock);
547 new_thread->p_cancelstate = PTHREAD_CANCEL_ENABLE;
548 new_thread->p_canceltype = PTHREAD_CANCEL_DEFERRED;
549 new_thread->p_errnop = &new_thread->p_errno;
550 new_thread->p_h_errnop = &new_thread->p_h_errno;
551 new_thread->p_resp = &new_thread->p_res;
552 new_thread->p_guardaddr = guardaddr;
553 new_thread->p_guardsize = guardsize;
554 new_thread->p_header.data.self = new_thread;
555 new_thread->p_nr = sseg;
556 new_thread->p_inheritsched = attr ? attr->__inheritsched : 0;
557 /* Initialize the thread handle */
558 __pthread_init_lock(&__pthread_handles[sseg].h_lock);
559 __pthread_handles[sseg].h_descr = new_thread;
560 __pthread_handles[sseg].h_bottom = new_thread_bottom;
561 /* Determine scheduling parameters for the thread */
562 new_thread->p_start_args.schedpolicy = -1;
563 if (attr != NULL) {
564 new_thread->p_detached = attr->__detachstate;
565 new_thread->p_userstack = attr->__stackaddr_set;
567 switch(attr->__inheritsched) {
568 case PTHREAD_EXPLICIT_SCHED:
569 new_thread->p_start_args.schedpolicy = attr->__schedpolicy;
570 memcpy (&new_thread->p_start_args.schedparam, &attr->__schedparam,
571 sizeof (struct sched_param));
572 break;
573 case PTHREAD_INHERIT_SCHED:
574 new_thread->p_start_args.schedpolicy = __sched_getscheduler(father_pid);
575 __sched_getparam(father_pid, &new_thread->p_start_args.schedparam);
576 break;
578 new_thread->p_priority =
579 new_thread->p_start_args.schedparam.sched_priority;
581 /* Finish setting up arguments to pthread_start_thread */
582 new_thread->p_start_args.start_routine = start_routine;
583 new_thread->p_start_args.arg = arg;
584 new_thread->p_start_args.mask = *mask;
585 /* Make the new thread ID available already now. If any of the later
586 functions fail we return an error value and the caller must not use
587 the stored thread ID. */
588 *thread = new_thread_id;
589 /* Raise priority of thread manager if needed */
590 __pthread_manager_adjust_prio(new_thread->p_priority);
591 /* Do the cloning. We have to use two different functions depending
592 on whether we are debugging or not. */
593 pid = 0; /* Note that the thread never can have PID zero. */
594 if (report_events)
596 /* See whether the TD_CREATE event bit is set in any of the
597 masks. */
598 int idx = __td_eventword (TD_CREATE);
599 uint32_t mask = __td_eventmask (TD_CREATE);
601 if ((mask & (__pthread_threads_events.event_bits[idx]
602 | event_maskp->event_bits[idx])) != 0)
604 /* Lock the mutex the child will use now so that it will stop. */
605 __pthread_lock(new_thread->p_lock, NULL);
607 /* We have to report this event. */
608 #ifdef NEED_SEPARATE_REGISTER_STACK
609 /* Perhaps this version should be used on all platforms. But
610 this requires that __clone2 be uniformly supported
611 everywhere.
613 And there is some argument for changing the __clone2
614 interface to pass sp and bsp instead, making it more IA64
615 specific, but allowing stacks to grow outward from each
616 other, to get less paging and fewer mmaps. */
617 pid = __clone2(pthread_start_thread_event,
618 (void **)new_thread_bottom,
619 (char *)new_thread - new_thread_bottom,
620 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
621 __pthread_sig_cancel, new_thread);
622 #elif _STACK_GROWS_UP
623 pid = __clone(pthread_start_thread_event, (void **) new_thread_bottom,
624 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
625 __pthread_sig_cancel, new_thread);
626 #else
627 pid = __clone(pthread_start_thread_event, (void **) new_thread,
628 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
629 __pthread_sig_cancel, new_thread);
630 #endif
631 if (pid != -1)
633 /* Now fill in the information about the new thread in
634 the newly created thread's data structure. We cannot let
635 the new thread do this since we don't know whether it was
636 already scheduled when we send the event. */
637 new_thread->p_eventbuf.eventdata = new_thread;
638 new_thread->p_eventbuf.eventnum = TD_CREATE;
639 __pthread_last_event = new_thread;
641 /* We have to set the PID here since the callback function
642 in the debug library will need it and we cannot guarantee
643 the child got scheduled before the debugger. */
644 new_thread->p_pid = pid;
646 /* Now call the function which signals the event. */
647 __linuxthreads_create_event ();
649 /* Now restart the thread. */
650 __pthread_unlock(new_thread->p_lock);
654 if (pid == 0)
656 #ifdef NEED_SEPARATE_REGISTER_STACK
657 pid = __clone2(pthread_start_thread,
658 (void **)new_thread_bottom,
659 (char *)new_thread - new_thread_bottom,
660 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
661 __pthread_sig_cancel, new_thread);
662 #elif _STACK_GROWS_UP
663 pid = __clone(pthread_start_thread, (void **) new_thread_bottom,
664 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
665 __pthread_sig_cancel, new_thread);
666 #else
667 pid = __clone(pthread_start_thread, (void **) new_thread,
668 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
669 __pthread_sig_cancel, new_thread);
670 #endif /* !NEED_SEPARATE_REGISTER_STACK */
672 /* Check if cloning succeeded */
673 if (pid == -1) {
674 /* Free the stack if we allocated it */
675 if (attr == NULL || !attr->__stackaddr_set)
677 #ifdef NEED_SEPARATE_REGISTER_STACK
678 size_t stacksize = ((char *)(new_thread->p_guardaddr)
679 - new_thread_bottom);
680 munmap((caddr_t)new_thread_bottom,
681 2 * stacksize + new_thread->p_guardsize);
682 #elif _STACK_GROWS_UP
683 size_t stacksize = guardaddr - (char *)new_thread;
684 munmap(new_thread, stacksize + guardsize);
685 #else
686 size_t stacksize = (char *)(new_thread+1) - new_thread_bottom;
687 munmap(new_thread_bottom - guardsize, guardsize + stacksize);
688 #endif
690 __pthread_handles[sseg].h_descr = NULL;
691 __pthread_handles[sseg].h_bottom = NULL;
692 __pthread_handles_num--;
693 return errno;
695 /* Insert new thread in doubly linked list of active threads */
696 new_thread->p_prevlive = __pthread_main_thread;
697 new_thread->p_nextlive = __pthread_main_thread->p_nextlive;
698 __pthread_main_thread->p_nextlive->p_prevlive = new_thread;
699 __pthread_main_thread->p_nextlive = new_thread;
700 /* Set pid field of the new thread, in case we get there before the
701 child starts. */
702 new_thread->p_pid = pid;
703 return 0;
707 /* Try to free the resources of a thread when requested by pthread_join
708 or pthread_detach on a terminated thread. */
710 static void pthread_free(pthread_descr th)
712 pthread_handle handle;
713 pthread_readlock_info *iter, *next;
715 ASSERT(th->p_exited);
716 /* Make the handle invalid */
717 handle = thread_handle(th->p_tid);
718 __pthread_lock(&handle->h_lock, NULL);
719 handle->h_descr = NULL;
720 handle->h_bottom = (char *)(-1L);
721 __pthread_unlock(&handle->h_lock);
722 #ifdef FREE_THREAD
723 FREE_THREAD(th, th->p_nr);
724 #endif
725 /* One fewer threads in __pthread_handles */
726 __pthread_handles_num--;
728 /* Destroy read lock list, and list of free read lock structures.
729 If the former is not empty, it means the thread exited while
730 holding read locks! */
732 for (iter = th->p_readlock_list; iter != NULL; iter = next)
734 next = iter->pr_next;
735 free(iter);
738 for (iter = th->p_readlock_free; iter != NULL; iter = next)
740 next = iter->pr_next;
741 free(iter);
744 /* If initial thread, nothing to free */
745 if (!th->p_userstack)
747 size_t guardsize = th->p_guardsize;
748 /* Free the stack and thread descriptor area */
749 char *guardaddr = th->p_guardaddr;
750 #ifdef _STACK_GROWS_UP
751 size_t stacksize = guardaddr - (char *)th;
752 guardaddr = (char *)th;
753 #else
754 /* Guardaddr is always set, even if guardsize is 0. This allows
755 us to compute everything else. */
756 size_t stacksize = (char *)(th+1) - guardaddr - guardsize;
757 #ifdef NEED_SEPARATE_REGISTER_STACK
758 /* Take account of the register stack, which is below guardaddr. */
759 guardaddr -= stacksize;
760 stacksize *= 2;
761 #endif
762 #endif
763 /* Unmap the stack. */
764 munmap(guardaddr, stacksize + guardsize);
768 /* Handle threads that have exited */
770 static void pthread_exited(pid_t pid)
772 pthread_descr th;
773 int detached;
774 /* Find thread with that pid */
775 for (th = __pthread_main_thread->p_nextlive;
776 th != __pthread_main_thread;
777 th = th->p_nextlive) {
778 if (th->p_pid == pid) {
779 /* Remove thread from list of active threads */
780 th->p_nextlive->p_prevlive = th->p_prevlive;
781 th->p_prevlive->p_nextlive = th->p_nextlive;
782 /* Mark thread as exited, and if detached, free its resources */
783 __pthread_lock(th->p_lock, NULL);
784 th->p_exited = 1;
785 /* If we have to signal this event do it now. */
786 if (th->p_report_events)
788 /* See whether TD_REAP is in any of the mask. */
789 int idx = __td_eventword (TD_REAP);
790 uint32_t mask = __td_eventmask (TD_REAP);
792 if ((mask & (__pthread_threads_events.event_bits[idx]
793 | th->p_eventbuf.eventmask.event_bits[idx])) != 0)
795 /* Yep, we have to signal the reapage. */
796 th->p_eventbuf.eventnum = TD_REAP;
797 th->p_eventbuf.eventdata = th;
798 __pthread_last_event = th;
800 /* Now call the function to signal the event. */
801 __linuxthreads_reap_event();
804 detached = th->p_detached;
805 __pthread_unlock(th->p_lock);
806 if (detached)
807 pthread_free(th);
808 break;
811 /* If all threads have exited and the main thread is pending on a
812 pthread_exit, wake up the main thread and terminate ourselves. */
813 if (main_thread_exiting &&
814 __pthread_main_thread->p_nextlive == __pthread_main_thread) {
815 restart(__pthread_main_thread);
816 /* Same logic as REQ_MAIN_THREAD_EXIT. */
820 static void pthread_reap_children(void)
822 pid_t pid;
823 int status;
825 while ((pid = __libc_waitpid(-1, &status, WNOHANG | __WCLONE)) > 0) {
826 pthread_exited(pid);
827 if (WIFSIGNALED(status)) {
828 /* If a thread died due to a signal, send the same signal to
829 all other threads, including the main thread. */
830 pthread_kill_all_threads(WTERMSIG(status), 1);
831 _exit(0);
836 /* Try to free the resources of a thread when requested by pthread_join
837 or pthread_detach on a terminated thread. */
839 static void pthread_handle_free(pthread_t th_id)
841 pthread_handle handle = thread_handle(th_id);
842 pthread_descr th;
844 __pthread_lock(&handle->h_lock, NULL);
845 if (nonexisting_handle(handle, th_id)) {
846 /* pthread_reap_children has deallocated the thread already,
847 nothing needs to be done */
848 __pthread_unlock(&handle->h_lock);
849 return;
851 th = handle->h_descr;
852 if (th->p_exited) {
853 __pthread_unlock(&handle->h_lock);
854 pthread_free(th);
855 } else {
856 /* The Unix process of the thread is still running.
857 Mark the thread as detached so that the thread manager will
858 deallocate its resources when the Unix process exits. */
859 th->p_detached = 1;
860 __pthread_unlock(&handle->h_lock);
864 /* Send a signal to all running threads */
866 static void pthread_kill_all_threads(int sig, int main_thread_also)
868 pthread_descr th;
869 for (th = __pthread_main_thread->p_nextlive;
870 th != __pthread_main_thread;
871 th = th->p_nextlive) {
872 kill(th->p_pid, sig);
874 if (main_thread_also) {
875 kill(__pthread_main_thread->p_pid, sig);
879 /* Process-wide exit() */
881 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
883 pthread_descr th;
884 __pthread_exit_requested = 1;
885 __pthread_exit_code = exitcode;
886 /* Send the CANCEL signal to all running threads, including the main
887 thread, but excluding the thread from which the exit request originated
888 (that thread must complete the exit, e.g. calling atexit functions
889 and flushing stdio buffers). */
890 for (th = issuing_thread->p_nextlive;
891 th != issuing_thread;
892 th = th->p_nextlive) {
893 kill(th->p_pid, __pthread_sig_cancel);
895 /* Now, wait for all these threads, so that they don't become zombies
896 and their times are properly added to the thread manager's times. */
897 for (th = issuing_thread->p_nextlive;
898 th != issuing_thread;
899 th = th->p_nextlive) {
900 waitpid(th->p_pid, NULL, __WCLONE);
902 restart(issuing_thread);
903 _exit(0);
906 /* Handler for __pthread_sig_cancel in thread manager thread */
908 void __pthread_manager_sighandler(int sig)
910 int kick_manager = terminated_children == 0 && main_thread_exiting;
911 terminated_children = 1;
913 /* If the main thread is terminating, kick the thread manager loop
914 each time some threads terminate. This eliminates a two second
915 shutdown delay caused by the thread manager sleeping in the
916 call to __poll(). Instead, the thread manager is kicked into
917 action, reaps the outstanding threads and resumes the main thread
918 so that it can complete the shutdown. */
920 if (kick_manager) {
921 struct pthread_request request;
922 request.req_thread = 0;
923 request.req_kind = REQ_KICK;
924 __libc_write(__pthread_manager_request, (char *) &request, sizeof(request));
928 /* Adjust priority of thread manager so that it always run at a priority
929 higher than all threads */
931 void __pthread_manager_adjust_prio(int thread_prio)
933 struct sched_param param;
935 if (thread_prio <= __pthread_manager_thread.p_priority) return;
936 param.sched_priority =
937 thread_prio < __sched_get_priority_max(SCHED_FIFO)
938 ? thread_prio + 1 : thread_prio;
939 __sched_setscheduler(__pthread_manager_thread.p_pid, SCHED_FIFO, &param);
940 __pthread_manager_thread.p_priority = thread_prio;