1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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 */
25 #include <sys/poll.h> /* for poll */
26 #include <sys/mman.h> /* for mmap */
27 #include <sys/param.h>
29 #include <sys/wait.h> /* for waitpid macros */
33 #include "internals.h"
36 #include "semaphore.h"
38 /* Array of active threads. Entry 0 is reserved for the initial thread. */
39 struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX]
41 # if __LT_SPINLOCK_INIT != 0
43 { __LOCK_INITIALIZER, NULL, 0},
44 { __LOCK_INITIALIZER, NULL, 0},
50 { __LOCK_INITIALIZER, &__pthread_initial_thread, 0},
51 { __LOCK_INITIALIZER, &__pthread_manager_thread, 0},
57 /* For debugging purposes put the maximum number of threads in a variable. */
58 const int __linuxthreads_pthread_threads_max = PTHREAD_THREADS_MAX;
61 /* Indicate whether at least one thread has a user-defined stack (if 1),
62 or if all threads have stacks supplied by LinuxThreads (if 0). */
63 int __pthread_nonstandard_stacks;
66 /* Number of active entries in __pthread_handles (used by gdb) */
67 volatile int __pthread_handles_num = 2;
69 /* Whether to use debugger additional actions for thread creation
71 volatile int __pthread_threads_debug;
73 /* Globally enabled events. */
74 volatile td_thr_events_t __pthread_threads_events;
76 /* Pointer to thread descriptor with last event. */
77 volatile pthread_descr __pthread_last_event;
79 static pthread_descr manager_thread;
81 /* Mapping from stack segment to thread descriptor. */
82 /* Stack segment numbers are also indices into the __pthread_handles array. */
83 /* Stack segment number 0 is reserved for the initial thread. */
86 # define thread_segment(seq) NULL
88 static inline pthread_descr thread_segment(int seg)
90 return (pthread_descr)(THREAD_STACK_START_ADDRESS - (seg - 1) * STACK_SIZE)
95 /* Flag set in signal handler to record child termination */
97 static volatile int terminated_children;
99 /* Flag set when the initial thread is blocked on pthread_exit waiting
100 for all other threads to terminate */
102 static int main_thread_exiting;
104 /* Counter used to generate unique thread identifier.
105 Thread identifier is pthread_threads_counter + segment. */
107 static pthread_t pthread_threads_counter;
109 /* Forward declarations */
111 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
112 void * (*start_routine)(void *), void *arg,
113 sigset_t *mask, int father_pid,
115 td_thr_events_t *event_maskp);
116 static void pthread_handle_free(pthread_t th_id);
117 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
118 __attribute__ ((noreturn));
119 static void pthread_reap_children(void);
120 static void pthread_kill_all_threads(int sig, int main_thread_also);
121 static void pthread_for_each_thread(void *arg,
122 void (*fn)(void *, pthread_descr));
124 /* The server thread managing requests for thread creation and termination */
127 __attribute__ ((noreturn))
128 __pthread_manager(void *arg)
130 pthread_descr self = manager_thread = arg;
131 int reqfd = __pthread_manager_reader;
133 sigset_t manager_mask;
135 struct pthread_request request;
137 /* If we have special thread_self processing, initialize it. */
138 #ifdef INIT_THREAD_SELF
139 INIT_THREAD_SELF(self, 1);
141 /* Set the error variable. */
142 self->p_errnop = &self->p_errno;
143 self->p_h_errnop = &self->p_h_errno;
144 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
145 sigfillset(&manager_mask);
146 sigdelset(&manager_mask, __pthread_sig_cancel); /* for thread termination */
147 sigdelset(&manager_mask, SIGTRAP); /* for debugging purposes */
148 if (__pthread_threads_debug && __pthread_sig_debug > 0)
149 sigdelset(&manager_mask, __pthread_sig_debug);
150 sigprocmask(SIG_SETMASK, &manager_mask, NULL);
151 /* Raise our priority to match that of main thread */
152 __pthread_manager_adjust_prio(__pthread_main_thread->p_priority);
153 /* Synchronize debugging of the thread manager */
154 n = TEMP_FAILURE_RETRY(__libc_read(reqfd, (char *)&request,
156 ASSERT(n == sizeof(request) && request.req_kind == REQ_DEBUG);
159 /* Enter server loop */
161 n = __poll(&ufd, 1, 2000);
163 /* Check for termination of the main thread */
164 if (getppid() == 1) {
165 pthread_kill_all_threads(SIGKILL, 0);
168 /* Check for dead children */
169 if (terminated_children) {
170 terminated_children = 0;
171 pthread_reap_children();
173 /* Read and execute request */
174 if (n == 1 && (ufd.revents & POLLIN)) {
175 n = TEMP_FAILURE_RETRY(__libc_read(reqfd, (char *)&request,
180 write(STDERR_FILENO, d, snprintf(d, sizeof(d), "*** read err %m\n"));
181 } else if (n != sizeof(request)) {
182 write(STDERR_FILENO, "*** short read in manager\n", 26);
186 switch(request.req_kind) {
188 request.req_thread->p_retcode =
189 pthread_handle_create((pthread_t *) &request.req_thread->p_retval,
190 request.req_args.create.attr,
191 request.req_args.create.fn,
192 request.req_args.create.arg,
193 &request.req_args.create.mask,
194 request.req_thread->p_pid,
195 request.req_thread->p_report_events,
196 &request.req_thread->p_eventbuf.eventmask);
197 restart(request.req_thread);
200 pthread_handle_free(request.req_args.free.thread_id);
202 case REQ_PROCESS_EXIT:
203 pthread_handle_exit(request.req_thread,
204 request.req_args.exit.code);
207 case REQ_MAIN_THREAD_EXIT:
208 main_thread_exiting = 1;
209 /* Reap children in case all other threads died and the signal handler
210 went off before we set main_thread_exiting to 1, and therefore did
212 pthread_reap_children();
214 if (__pthread_main_thread->p_nextlive == __pthread_main_thread) {
215 restart(__pthread_main_thread);
216 /* The main thread will now call exit() which will trigger an
217 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
218 to the thread manager. In case you are wondering how the
219 manager terminates from its loop here. */
223 __new_sem_post(request.req_args.post);
226 /* Make gdb aware of new thread and gdb will restart the
227 new thread when it is ready to handle the new thread. */
228 if (__pthread_threads_debug && __pthread_sig_debug > 0)
229 raise(__pthread_sig_debug);
232 /* This is just a prod to get the manager to reap some
233 threads right away, avoiding a potential delay at shutdown. */
235 case REQ_FOR_EACH_THREAD:
236 pthread_for_each_thread(request.req_args.for_each.arg,
237 request.req_args.for_each.fn);
238 restart(request.req_thread);
245 int __pthread_manager_event(void *arg)
247 pthread_descr self = arg;
248 /* If we have special thread_self processing, initialize it. */
249 #ifdef INIT_THREAD_SELF
250 INIT_THREAD_SELF(self, 1);
253 /* Get the lock the manager will free once all is correctly set up. */
254 __pthread_lock (THREAD_GETMEM(self, p_lock), NULL);
255 /* Free it immediately. */
256 __pthread_unlock (THREAD_GETMEM(self, p_lock));
258 return __pthread_manager(arg);
261 /* Process creation */
264 __attribute__ ((noreturn))
265 pthread_start_thread(void *arg)
267 pthread_descr self = (pthread_descr) arg;
268 struct pthread_request request;
271 hp_timing_t tmpclock;
273 /* Initialize special thread_self processing, if any. */
274 #ifdef INIT_THREAD_SELF
275 INIT_THREAD_SELF(self, self->p_nr);
278 HP_TIMING_NOW (tmpclock);
279 THREAD_SETMEM (self, p_cpuclock_offset, tmpclock);
281 /* Make sure our pid field is initialized, just in case we get there
282 before our father has initialized it. */
283 THREAD_SETMEM(self, p_pid, __getpid());
284 /* Initial signal mask is that of the creating thread. (Otherwise,
285 we'd just inherit the mask of the thread manager.) */
286 sigprocmask(SIG_SETMASK, &self->p_start_args.mask, NULL);
287 /* Set the scheduling policy and priority for the new thread, if needed */
288 if (THREAD_GETMEM(self, p_start_args.schedpolicy) >= 0)
289 /* Explicit scheduling attributes were provided: apply them */
290 __sched_setscheduler(THREAD_GETMEM(self, p_pid),
291 THREAD_GETMEM(self, p_start_args.schedpolicy),
292 &self->p_start_args.schedparam);
293 else if (manager_thread->p_priority > 0)
294 /* Default scheduling required, but thread manager runs in realtime
295 scheduling: switch new thread to SCHED_OTHER policy */
297 struct sched_param default_params;
298 default_params.sched_priority = 0;
299 __sched_setscheduler(THREAD_GETMEM(self, p_pid),
300 SCHED_OTHER, &default_params);
302 /* Make gdb aware of new thread */
303 if (__pthread_threads_debug && __pthread_sig_debug > 0) {
304 request.req_thread = self;
305 request.req_kind = REQ_DEBUG;
306 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
307 (char *) &request, sizeof(request)));
310 /* Run the thread code */
311 outcome = self->p_start_args.start_routine(THREAD_GETMEM(self,
313 /* Exit with the given return value */
314 __pthread_do_exit(outcome, CURRENT_STACK_FRAME);
318 __attribute__ ((noreturn))
319 pthread_start_thread_event(void *arg)
321 pthread_descr self = (pthread_descr) arg;
323 #ifdef INIT_THREAD_SELF
324 INIT_THREAD_SELF(self, self->p_nr);
326 /* Make sure our pid field is initialized, just in case we get there
327 before our father has initialized it. */
328 THREAD_SETMEM(self, p_pid, __getpid());
329 /* Get the lock the manager will free once all is correctly set up. */
330 __pthread_lock (THREAD_GETMEM(self, p_lock), NULL);
331 /* Free it immediately. */
332 __pthread_unlock (THREAD_GETMEM(self, p_lock));
334 /* Continue with the real function. */
335 pthread_start_thread (arg);
338 #if defined USE_TLS && !FLOATING_STACKS
339 # error "TLS can only work with floating stacks"
342 static int pthread_allocate_stack(const pthread_attr_t *attr,
343 pthread_descr default_new_thread,
345 char ** out_new_thread,
346 char ** out_new_thread_bottom,
347 char ** out_guardaddr,
348 size_t * out_guardsize)
350 pthread_descr new_thread;
351 char * new_thread_bottom;
353 size_t stacksize, guardsize;
356 /* TLS cannot work with fixed thread descriptor addresses. */
357 assert (default_new_thread == NULL);
360 if (attr != NULL && attr->__stackaddr_set)
362 #ifdef _STACK_GROWS_UP
363 /* The user provided a stack. */
365 /* This value is not needed. */
366 new_thread = (pthread_descr) attr->__stackaddr;
367 new_thread_bottom = (char *) new_thread;
369 new_thread = (pthread_descr) attr->__stackaddr;
370 new_thread_bottom = (char *) (new_thread + 1);
372 guardaddr = attr->__stackaddr + attr->__stacksize;
375 /* The user provided a stack. For now we interpret the supplied
376 address as 1 + the highest addr. in the stack segment. If a
377 separate register stack is needed, we place it at the low end
378 of the segment, relying on the associated stacksize to
379 determine the low end of the segment. This differs from many
380 (but not all) other pthreads implementations. The intent is
381 that on machines with a single stack growing toward higher
382 addresses, stackaddr would be the lowest address in the stack
383 segment, so that it is consistently close to the initial sp
386 new_thread = (pthread_descr) attr->__stackaddr;
389 (pthread_descr) ((long)(attr->__stackaddr) & -sizeof(void *)) - 1;
391 new_thread_bottom = (char *) attr->__stackaddr - attr->__stacksize;
392 guardaddr = new_thread_bottom;
396 __pthread_nonstandard_stacks = 1;
399 /* Clear the thread data structure. */
400 memset (new_thread, '\0', sizeof (*new_thread));
405 #ifdef NEED_SEPARATE_REGISTER_STACK
406 const size_t granularity = 2 * pagesize;
407 /* Try to make stacksize/2 a multiple of pagesize */
409 const size_t granularity = pagesize;
413 /* Allocate space for stack and thread descriptor at default address */
417 guardsize = page_roundup (attr->__guardsize, granularity);
418 stacksize = __pthread_max_stacksize - guardsize;
419 stacksize = MIN (stacksize,
420 page_roundup (attr->__stacksize, granularity));
424 guardsize = granularity;
425 stacksize = __pthread_max_stacksize - guardsize;
428 map_addr = mmap(NULL, stacksize + guardsize,
429 PROT_READ | PROT_WRITE | PROT_EXEC,
430 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
431 if (map_addr == MAP_FAILED)
432 /* No more memory available. */
435 # ifdef NEED_SEPARATE_REGISTER_STACK
436 guardaddr = map_addr + stacksize / 2;
438 mprotect (guardaddr, guardsize, PROT_NONE);
440 new_thread_bottom = (char *) map_addr;
442 new_thread = ((pthread_descr) (new_thread_bottom + stacksize
445 new_thread = ((pthread_descr) (new_thread_bottom + stacksize
448 # elif _STACK_GROWS_DOWN
449 guardaddr = map_addr;
451 mprotect (guardaddr, guardsize, PROT_NONE);
453 new_thread_bottom = (char *) map_addr + guardsize;
455 new_thread = ((pthread_descr) (new_thread_bottom + stacksize));
457 new_thread = ((pthread_descr) (new_thread_bottom + stacksize)) - 1;
459 # elif _STACK_GROWS_UP
460 guardaddr = map_addr + stacksize;
462 mprotect (guardaddr, guardsize, PROT_NONE);
464 new_thread = (pthread_descr) map_addr;
466 new_thread_bottom = (char *) new_thread;
468 new_thread_bottom = (char *) (new_thread + 1);
471 # error You must define a stack direction
472 # endif /* Stack direction */
473 #else /* !FLOATING_STACKS */
478 guardsize = page_roundup (attr->__guardsize, granularity);
479 stacksize = STACK_SIZE - guardsize;
480 stacksize = MIN (stacksize,
481 page_roundup (attr->__stacksize, granularity));
485 guardsize = granularity;
486 stacksize = STACK_SIZE - granularity;
489 # ifdef NEED_SEPARATE_REGISTER_STACK
490 new_thread = default_new_thread;
491 new_thread_bottom = (char *) (new_thread + 1) - stacksize - guardsize;
492 /* Includes guard area, unlike the normal case. Use the bottom
493 end of the segment as backing store for the register stack.
494 Needed on IA64. In this case, we also map the entire stack at
495 once. According to David Mosberger, that's cheaper. It also
496 avoids the risk of intermittent failures due to other mappings
497 in the same region. The cost is that we might be able to map
498 slightly fewer stacks. */
500 /* First the main stack: */
501 map_addr = (caddr_t)((char *)(new_thread + 1) - stacksize / 2);
502 res_addr = mmap(map_addr, stacksize / 2,
503 PROT_READ | PROT_WRITE | PROT_EXEC,
504 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
505 if (res_addr != map_addr)
507 /* Bad luck, this segment is already mapped. */
508 if (res_addr != MAP_FAILED)
509 munmap(res_addr, stacksize / 2);
512 /* Then the register stack: */
513 map_addr = (caddr_t)new_thread_bottom;
514 res_addr = mmap(map_addr, stacksize/2,
515 PROT_READ | PROT_WRITE | PROT_EXEC,
516 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
517 if (res_addr != map_addr)
519 if (res_addr != MAP_FAILED)
520 munmap(res_addr, stacksize / 2);
521 munmap((caddr_t)((char *)(new_thread + 1) - stacksize/2),
526 guardaddr = new_thread_bottom + stacksize/2;
527 /* We leave the guard area in the middle unmapped. */
528 # else /* !NEED_SEPARATE_REGISTER_STACK */
529 # ifdef _STACK_GROWS_DOWN
530 new_thread = default_new_thread;
531 new_thread_bottom = (char *) (new_thread + 1) - stacksize;
532 map_addr = new_thread_bottom - guardsize;
533 res_addr = mmap(map_addr, stacksize + guardsize,
534 PROT_READ | PROT_WRITE | PROT_EXEC,
535 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
536 if (res_addr != map_addr)
538 /* Bad luck, this segment is already mapped. */
539 if (res_addr != MAP_FAILED)
540 munmap (res_addr, stacksize + guardsize);
544 /* We manage to get a stack. Protect the guard area pages if
546 guardaddr = map_addr;
548 mprotect (guardaddr, guardsize, PROT_NONE);
550 /* The thread description goes at the bottom of this area, and
551 * the stack starts directly above it.
553 new_thread = (pthread_descr)((unsigned long)default_new_thread &~ (STACK_SIZE - 1));
554 map_addr = mmap(new_thread, stacksize + guardsize,
555 PROT_READ | PROT_WRITE | PROT_EXEC,
556 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
557 if (map_addr == MAP_FAILED)
560 new_thread_bottom = map_addr + sizeof(*new_thread);
561 guardaddr = map_addr + stacksize;
563 mprotect (guardaddr, guardsize, PROT_NONE);
565 # endif /* stack direction */
566 # endif /* !NEED_SEPARATE_REGISTER_STACK */
567 #endif /* !FLOATING_STACKS */
569 *out_new_thread = (char *) new_thread;
570 *out_new_thread_bottom = new_thread_bottom;
571 *out_guardaddr = guardaddr;
572 *out_guardsize = guardsize;
576 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
577 void * (*start_routine)(void *), void *arg,
578 sigset_t * mask, int father_pid,
580 td_thr_events_t *event_maskp)
584 pthread_descr new_thread;
586 char * new_thread_bottom;
587 pthread_t new_thread_id;
588 char *guardaddr = NULL;
589 size_t guardsize = 0;
590 int pagesize = __getpagesize();
594 new_thread = _dl_allocate_tls ();
595 if (new_thread == NULL)
598 /* Prevent warnings. */
602 /* First check whether we have to change the policy and if yes, whether
603 we can do this. Normally this should be done by examining the
604 return value of the __sched_setscheduler call in pthread_start_thread
605 but this is hard to implement. FIXME */
606 if (attr != NULL && attr->__schedpolicy != SCHED_OTHER && geteuid () != 0)
608 /* Find a free segment for the thread, and allocate a stack if needed */
609 for (sseg = 2; ; sseg++)
611 if (sseg >= PTHREAD_THREADS_MAX)
614 _dl_deallocate_tls (new_thread);
618 if (__pthread_handles[sseg].h_descr != NULL)
620 if (pthread_allocate_stack(attr, thread_segment(sseg),
621 pagesize, &stack_addr, &new_thread_bottom,
622 &guardaddr, &guardsize) == 0)
625 new_thread->p_stackaddr = stack_addr;
627 new_thread = (pthread_descr) stack_addr;
632 __pthread_handles_num++;
633 /* Allocate new thread identifier */
634 pthread_threads_counter += PTHREAD_THREADS_MAX;
635 new_thread_id = sseg + pthread_threads_counter;
636 /* Initialize the thread descriptor. Elements which have to be
637 initialized to zero already have this value. */
638 new_thread->p_tid = new_thread_id;
639 new_thread->p_lock = &(__pthread_handles[sseg].h_lock);
640 new_thread->p_cancelstate = PTHREAD_CANCEL_ENABLE;
641 new_thread->p_canceltype = PTHREAD_CANCEL_DEFERRED;
642 new_thread->p_errnop = &new_thread->p_errno;
643 new_thread->p_h_errnop = &new_thread->p_h_errno;
644 new_thread->p_resp = &new_thread->p_res;
645 new_thread->p_guardaddr = guardaddr;
646 new_thread->p_guardsize = guardsize;
647 new_thread->p_header.data.self = new_thread;
648 new_thread->p_nr = sseg;
649 new_thread->p_inheritsched = attr ? attr->__inheritsched : 0;
650 /* Initialize the thread handle */
651 __pthread_init_lock(&__pthread_handles[sseg].h_lock);
652 __pthread_handles[sseg].h_descr = new_thread;
653 __pthread_handles[sseg].h_bottom = new_thread_bottom;
654 /* Determine scheduling parameters for the thread */
655 new_thread->p_start_args.schedpolicy = -1;
657 new_thread->p_detached = attr->__detachstate;
658 new_thread->p_userstack = attr->__stackaddr_set;
660 switch(attr->__inheritsched) {
661 case PTHREAD_EXPLICIT_SCHED:
662 new_thread->p_start_args.schedpolicy = attr->__schedpolicy;
663 memcpy (&new_thread->p_start_args.schedparam, &attr->__schedparam,
664 sizeof (struct sched_param));
666 case PTHREAD_INHERIT_SCHED:
667 new_thread->p_start_args.schedpolicy = __sched_getscheduler(father_pid);
668 __sched_getparam(father_pid, &new_thread->p_start_args.schedparam);
671 new_thread->p_priority =
672 new_thread->p_start_args.schedparam.sched_priority;
674 /* Finish setting up arguments to pthread_start_thread */
675 new_thread->p_start_args.start_routine = start_routine;
676 new_thread->p_start_args.arg = arg;
677 new_thread->p_start_args.mask = *mask;
678 /* Make the new thread ID available already now. If any of the later
679 functions fail we return an error value and the caller must not use
680 the stored thread ID. */
681 *thread = new_thread_id;
682 /* Raise priority of thread manager if needed */
683 __pthread_manager_adjust_prio(new_thread->p_priority);
684 /* Do the cloning. We have to use two different functions depending
685 on whether we are debugging or not. */
686 pid = 0; /* Note that the thread never can have PID zero. */
689 /* See whether the TD_CREATE event bit is set in any of the
691 int idx = __td_eventword (TD_CREATE);
692 uint32_t mask = __td_eventmask (TD_CREATE);
694 if ((mask & (__pthread_threads_events.event_bits[idx]
695 | event_maskp->event_bits[idx])) != 0)
697 /* Lock the mutex the child will use now so that it will stop. */
698 __pthread_lock(new_thread->p_lock, NULL);
700 /* We have to report this event. */
701 #ifdef NEED_SEPARATE_REGISTER_STACK
702 /* Perhaps this version should be used on all platforms. But
703 this requires that __clone2 be uniformly supported
706 And there is some argument for changing the __clone2
707 interface to pass sp and bsp instead, making it more IA64
708 specific, but allowing stacks to grow outward from each
709 other, to get less paging and fewer mmaps. */
710 pid = __clone2(pthread_start_thread_event,
711 (void **)new_thread_bottom,
712 (char *)new_thread - new_thread_bottom,
713 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
714 __pthread_sig_cancel, new_thread);
715 #elif _STACK_GROWS_UP
716 pid = __clone(pthread_start_thread_event, (void **) new_thread_bottom,
717 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
718 __pthread_sig_cancel, new_thread);
720 pid = __clone(pthread_start_thread_event, (void **) new_thread,
721 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
722 __pthread_sig_cancel, new_thread);
727 /* Now fill in the information about the new thread in
728 the newly created thread's data structure. We cannot let
729 the new thread do this since we don't know whether it was
730 already scheduled when we send the event. */
731 new_thread->p_eventbuf.eventdata = new_thread;
732 new_thread->p_eventbuf.eventnum = TD_CREATE;
733 __pthread_last_event = new_thread;
735 /* We have to set the PID here since the callback function
736 in the debug library will need it and we cannot guarantee
737 the child got scheduled before the debugger. */
738 new_thread->p_pid = pid;
740 /* Now call the function which signals the event. */
741 __linuxthreads_create_event ();
743 /* Now restart the thread. */
744 __pthread_unlock(new_thread->p_lock);
750 #ifdef NEED_SEPARATE_REGISTER_STACK
751 pid = __clone2(pthread_start_thread,
752 (void **)new_thread_bottom,
753 (char *)stack_addr - new_thread_bottom,
754 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
755 __pthread_sig_cancel, new_thread);
756 #elif _STACK_GROWS_UP
757 pid = __clone(pthread_start_thread, (void *) new_thread_bottom,
758 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
759 __pthread_sig_cancel, new_thread);
761 pid = __clone(pthread_start_thread, stack_addr,
762 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
763 __pthread_sig_cancel, new_thread);
764 #endif /* !NEED_SEPARATE_REGISTER_STACK */
767 /* Check if cloning succeeded */
769 /* Free the stack if we allocated it */
770 if (attr == NULL || !attr->__stackaddr_set)
772 #ifdef NEED_SEPARATE_REGISTER_STACK
773 size_t stacksize = ((char *)(new_thread->p_guardaddr)
774 - new_thread_bottom);
775 munmap((caddr_t)new_thread_bottom,
776 2 * stacksize + new_thread->p_guardsize);
777 #elif _STACK_GROWS_UP
779 size_t stacksize = guardaddr - stack_addr;
780 munmap(stack_addr, stacksize + guardsize);
782 size_t stacksize = guardaddr - (char *)new_thread;
783 munmap(new_thread, stacksize + guardsize);
787 size_t stacksize = stack_addr - new_thread_bottom;
789 size_t stacksize = (char *)(new_thread+1) - new_thread_bottom;
791 munmap(new_thread_bottom - guardsize, guardsize + stacksize);
795 _dl_deallocate_tls (new_thread);
797 __pthread_handles[sseg].h_descr = NULL;
798 __pthread_handles[sseg].h_bottom = NULL;
799 __pthread_handles_num--;
802 /* Insert new thread in doubly linked list of active threads */
803 new_thread->p_prevlive = __pthread_main_thread;
804 new_thread->p_nextlive = __pthread_main_thread->p_nextlive;
805 __pthread_main_thread->p_nextlive->p_prevlive = new_thread;
806 __pthread_main_thread->p_nextlive = new_thread;
807 /* Set pid field of the new thread, in case we get there before the
809 new_thread->p_pid = pid;
814 /* Try to free the resources of a thread when requested by pthread_join
815 or pthread_detach on a terminated thread. */
817 static void pthread_free(pthread_descr th)
819 pthread_handle handle;
820 pthread_readlock_info *iter, *next;
822 ASSERT(th->p_exited);
823 /* Make the handle invalid */
824 handle = thread_handle(th->p_tid);
825 __pthread_lock(&handle->h_lock, NULL);
826 handle->h_descr = NULL;
827 handle->h_bottom = (char *)(-1L);
828 __pthread_unlock(&handle->h_lock);
830 FREE_THREAD(th, th->p_nr);
832 /* One fewer threads in __pthread_handles */
833 __pthread_handles_num--;
835 /* Destroy read lock list, and list of free read lock structures.
836 If the former is not empty, it means the thread exited while
837 holding read locks! */
839 for (iter = th->p_readlock_list; iter != NULL; iter = next)
841 next = iter->pr_next;
845 for (iter = th->p_readlock_free; iter != NULL; iter = next)
847 next = iter->pr_next;
851 /* If initial thread, nothing to free */
852 if (!th->p_userstack)
854 size_t guardsize = th->p_guardsize;
855 /* Free the stack and thread descriptor area */
856 char *guardaddr = th->p_guardaddr;
857 #ifdef _STACK_GROWS_UP
859 size_t stacksize = guardaddr - th->p_stackaddr;
861 size_t stacksize = guardaddr - (char *)th;
863 guardaddr = (char *)th;
865 /* Guardaddr is always set, even if guardsize is 0. This allows
866 us to compute everything else. */
868 size_t stacksize = th->p_stackaddr - guardaddr - guardsize;
870 size_t stacksize = (char *)(th+1) - guardaddr - guardsize;
872 # ifdef NEED_SEPARATE_REGISTER_STACK
873 /* Take account of the register stack, which is below guardaddr. */
874 guardaddr -= stacksize;
878 /* Unmap the stack. */
879 munmap(guardaddr, stacksize + guardsize);
882 _dl_deallocate_tls (th);
887 /* Handle threads that have exited */
889 static void pthread_exited(pid_t pid)
893 /* Find thread with that pid */
894 for (th = __pthread_main_thread->p_nextlive;
895 th != __pthread_main_thread;
896 th = th->p_nextlive) {
897 if (th->p_pid == pid) {
898 /* Remove thread from list of active threads */
899 th->p_nextlive->p_prevlive = th->p_prevlive;
900 th->p_prevlive->p_nextlive = th->p_nextlive;
901 /* Mark thread as exited, and if detached, free its resources */
902 __pthread_lock(th->p_lock, NULL);
904 /* If we have to signal this event do it now. */
905 if (th->p_report_events)
907 /* See whether TD_REAP is in any of the mask. */
908 int idx = __td_eventword (TD_REAP);
909 uint32_t mask = __td_eventmask (TD_REAP);
911 if ((mask & (__pthread_threads_events.event_bits[idx]
912 | th->p_eventbuf.eventmask.event_bits[idx])) != 0)
914 /* Yep, we have to signal the reapage. */
915 th->p_eventbuf.eventnum = TD_REAP;
916 th->p_eventbuf.eventdata = th;
917 __pthread_last_event = th;
919 /* Now call the function to signal the event. */
920 __linuxthreads_reap_event();
923 detached = th->p_detached;
924 __pthread_unlock(th->p_lock);
930 /* If all threads have exited and the main thread is pending on a
931 pthread_exit, wake up the main thread and terminate ourselves. */
932 if (main_thread_exiting &&
933 __pthread_main_thread->p_nextlive == __pthread_main_thread) {
934 restart(__pthread_main_thread);
935 /* Same logic as REQ_MAIN_THREAD_EXIT. */
939 static void pthread_reap_children(void)
944 while ((pid = __libc_waitpid(-1, &status, WNOHANG | __WCLONE)) > 0) {
946 if (WIFSIGNALED(status)) {
947 /* If a thread died due to a signal, send the same signal to
948 all other threads, including the main thread. */
949 pthread_kill_all_threads(WTERMSIG(status), 1);
955 /* Try to free the resources of a thread when requested by pthread_join
956 or pthread_detach on a terminated thread. */
958 static void pthread_handle_free(pthread_t th_id)
960 pthread_handle handle = thread_handle(th_id);
963 __pthread_lock(&handle->h_lock, NULL);
964 if (nonexisting_handle(handle, th_id)) {
965 /* pthread_reap_children has deallocated the thread already,
966 nothing needs to be done */
967 __pthread_unlock(&handle->h_lock);
970 th = handle->h_descr;
972 __pthread_unlock(&handle->h_lock);
975 /* The Unix process of the thread is still running.
976 Mark the thread as detached so that the thread manager will
977 deallocate its resources when the Unix process exits. */
979 __pthread_unlock(&handle->h_lock);
983 /* Send a signal to all running threads */
985 static void pthread_kill_all_threads(int sig, int main_thread_also)
988 for (th = __pthread_main_thread->p_nextlive;
989 th != __pthread_main_thread;
990 th = th->p_nextlive) {
991 kill(th->p_pid, sig);
993 if (main_thread_also) {
994 kill(__pthread_main_thread->p_pid, sig);
998 static void pthread_for_each_thread(void *arg,
999 void (*fn)(void *, pthread_descr))
1003 for (th = __pthread_main_thread->p_nextlive;
1004 th != __pthread_main_thread;
1005 th = th->p_nextlive) {
1009 fn(arg, __pthread_main_thread);
1012 /* Process-wide exit() */
1014 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
1017 __pthread_exit_requested = 1;
1018 __pthread_exit_code = exitcode;
1019 /* A forced asynchronous cancellation follows. Make sure we won't
1020 get stuck later in the main thread with a system lock being held
1021 by one of the cancelled threads. Ideally one would use the same
1022 code as in pthread_atfork(), but we can't distinguish system and
1023 user handlers there. */
1025 /* Send the CANCEL signal to all running threads, including the main
1026 thread, but excluding the thread from which the exit request originated
1027 (that thread must complete the exit, e.g. calling atexit functions
1028 and flushing stdio buffers). */
1029 for (th = issuing_thread->p_nextlive;
1030 th != issuing_thread;
1031 th = th->p_nextlive) {
1032 kill(th->p_pid, __pthread_sig_cancel);
1034 /* Now, wait for all these threads, so that they don't become zombies
1035 and their times are properly added to the thread manager's times. */
1036 for (th = issuing_thread->p_nextlive;
1037 th != issuing_thread;
1038 th = th->p_nextlive) {
1039 waitpid(th->p_pid, NULL, __WCLONE);
1041 __fresetlockfiles();
1042 restart(issuing_thread);
1046 /* Handler for __pthread_sig_cancel in thread manager thread */
1048 void __pthread_manager_sighandler(int sig)
1050 int kick_manager = terminated_children == 0 && main_thread_exiting;
1051 terminated_children = 1;
1053 /* If the main thread is terminating, kick the thread manager loop
1054 each time some threads terminate. This eliminates a two second
1055 shutdown delay caused by the thread manager sleeping in the
1056 call to __poll(). Instead, the thread manager is kicked into
1057 action, reaps the outstanding threads and resumes the main thread
1058 so that it can complete the shutdown. */
1061 struct pthread_request request;
1062 request.req_thread = 0;
1063 request.req_kind = REQ_KICK;
1064 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
1065 (char *) &request, sizeof(request)));
1069 /* Adjust priority of thread manager so that it always run at a priority
1070 higher than all threads */
1072 void __pthread_manager_adjust_prio(int thread_prio)
1074 struct sched_param param;
1076 if (thread_prio <= manager_thread->p_priority) return;
1077 param.sched_priority =
1078 thread_prio < __sched_get_priority_max(SCHED_FIFO)
1079 ? thread_prio + 1 : thread_prio;
1080 __sched_setscheduler(manager_thread->p_pid, SCHED_FIFO, ¶m);
1081 manager_thread->p_priority = thread_prio;