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 */
32 #include "internals.h"
35 #include "semaphore.h"
36 #include "debug.h" /* PDEBUG, added by StS */
39 /* poll() is not supported in kernel <= 2.0, therefore is __NR_poll is
40 * not available, we assume an old Linux kernel is in use and we will
41 * use select() instead. */
42 #include <sys/syscall.h>
47 libpthread_hidden_proto(waitpid)
48 libpthread_hidden_proto(raise)
50 /* Array of active threads. Entry 0 is reserved for the initial thread. */
51 struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX] =
52 { { __LOCK_INITIALIZER, &__pthread_initial_thread, 0},
53 { __LOCK_INITIALIZER, &__pthread_manager_thread, 0}, /* All NULLs */ };
55 /* For debugging purposes put the maximum number of threads in a variable. */
56 const int __linuxthreads_pthread_threads_max = PTHREAD_THREADS_MAX;
58 /* Indicate whether at least one thread has a user-defined stack (if 1),
59 or if all threads have stacks supplied by LinuxThreads (if 0). */
60 int __pthread_nonstandard_stacks;
62 /* Number of active entries in __pthread_handles (used by gdb) */
63 volatile int __pthread_handles_num = 2;
65 /* Whether to use debugger additional actions for thread creation
67 volatile int __pthread_threads_debug;
69 /* Globally enabled events. */
70 volatile td_thr_events_t __pthread_threads_events;
72 /* Pointer to thread descriptor with last event. */
73 volatile pthread_descr __pthread_last_event;
75 /* Mapping from stack segment to thread descriptor. */
76 /* Stack segment numbers are also indices into the __pthread_handles array. */
77 /* Stack segment number 0 is reserved for the initial thread. */
79 static __inline__ pthread_descr thread_segment(int seg)
81 return (pthread_descr)(THREAD_STACK_START_ADDRESS - (seg - 1) * STACK_SIZE)
85 /* Flag set in signal handler to record child termination */
87 static volatile int terminated_children = 0;
89 /* Flag set when the initial thread is blocked on pthread_exit waiting
90 for all other threads to terminate */
92 static int main_thread_exiting = 0;
94 /* Counter used to generate unique thread identifier.
95 Thread identifier is pthread_threads_counter + segment. */
97 static pthread_t pthread_threads_counter = 0;
99 /* Forward declarations */
101 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
102 void * (*start_routine)(void *), void *arg,
103 sigset_t *mask, int father_pid,
105 td_thr_events_t *event_maskp);
106 static void pthread_handle_free(pthread_t th_id);
107 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode) attribute_noreturn;
108 static void pthread_reap_children(void);
109 static void pthread_kill_all_threads(int sig, int main_thread_also);
111 /* The server thread managing requests for thread creation and termination */
113 int attribute_noreturn __pthread_manager(void *arg)
115 int reqfd = (int) (long int) arg;
122 sigset_t manager_mask;
124 struct pthread_request request;
126 /* If we have special thread_self processing, initialize it. */
127 #ifdef INIT_THREAD_SELF
128 INIT_THREAD_SELF(&__pthread_manager_thread, 1);
130 /* Set the error variable. */
131 __pthread_manager_thread.p_errnop = &__pthread_manager_thread.p_errno;
132 __pthread_manager_thread.p_h_errnop = &__pthread_manager_thread.p_h_errno;
134 #ifdef __UCLIBC_HAS_XLOCALE__
135 /* Initialize thread's locale to the global locale. */
136 __pthread_manager_thread.locale = __global_locale;
137 #endif /* __UCLIBC_HAS_XLOCALE__ */
139 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
140 sigfillset(&manager_mask);
141 sigdelset(&manager_mask, __pthread_sig_cancel); /* for thread termination */
142 sigdelset(&manager_mask, SIGTRAP); /* for debugging purposes */
143 if (__pthread_threads_debug && __pthread_sig_debug > 0)
144 sigdelset(&manager_mask, __pthread_sig_debug);
145 sigprocmask(SIG_SETMASK, &manager_mask, NULL);
146 /* Raise our priority to match that of main thread */
147 __pthread_manager_adjust_prio(__pthread_main_thread->p_priority);
148 /* Synchronize debugging of the thread manager */
149 n = TEMP_FAILURE_RETRY(__libc_read(reqfd, (char *)&request,
155 /* Enter server loop */
162 n = select (reqfd + 1, &fd, NULL, NULL, &tv);
164 PDEBUG("before poll\n");
165 n = poll(&ufd, 1, 2000);
166 PDEBUG("after poll\n");
168 /* Check for termination of the main thread */
169 if (getppid() == 1) {
170 pthread_kill_all_threads(SIGKILL, 0);
173 /* Check for dead children */
174 if (terminated_children) {
175 terminated_children = 0;
176 pthread_reap_children();
178 /* Read and execute request */
182 if (n == 1 && (ufd.revents & POLLIN))
186 PDEBUG("before __libc_read\n");
187 n = __libc_read(reqfd, (char *)&request, sizeof(request));
188 PDEBUG("after __libc_read, n=%d\n", n);
189 switch(request.req_kind) {
191 PDEBUG("got REQ_CREATE\n");
192 request.req_thread->p_retcode =
193 pthread_handle_create((pthread_t *) &request.req_thread->p_retval,
194 request.req_args.create.attr,
195 request.req_args.create.fn,
196 request.req_args.create.arg,
197 &request.req_args.create.mask,
198 request.req_thread->p_pid,
199 request.req_thread->p_report_events,
200 &request.req_thread->p_eventbuf.eventmask);
201 PDEBUG("restarting %d\n", request.req_thread);
202 restart(request.req_thread);
205 PDEBUG("got REQ_FREE\n");
206 pthread_handle_free(request.req_args.free.thread_id);
208 case REQ_PROCESS_EXIT:
209 PDEBUG("got REQ_PROCESS_EXIT from %d, exit code = %d\n",
210 request.req_thread, request.req_args.exit.code);
211 pthread_handle_exit(request.req_thread,
212 request.req_args.exit.code);
214 case REQ_MAIN_THREAD_EXIT:
215 PDEBUG("got REQ_MAIN_THREAD_EXIT\n");
216 main_thread_exiting = 1;
217 /* Reap children in case all other threads died and the signal handler
218 went off before we set main_thread_exiting to 1, and therefore did
220 pthread_reap_children();
222 if (__pthread_main_thread->p_nextlive == __pthread_main_thread) {
223 restart(__pthread_main_thread);
224 /* The main thread will now call exit() which will trigger an
225 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
226 to the thread manager. In case you are wondering how the
227 manager terminates from its loop here. */
231 PDEBUG("got REQ_POST\n");
232 __new_sem_post(request.req_args.post);
235 PDEBUG("got REQ_DEBUG\n");
236 /* Make gdb aware of new thread and gdb will restart the
237 new thread when it is ready to handle the new thread. */
238 if (__pthread_threads_debug && __pthread_sig_debug > 0) {
239 PDEBUG("about to call raise(__pthread_sig_debug)\n");
240 raise(__pthread_sig_debug);
243 /* This is just a prod to get the manager to reap some
244 threads right away, avoiding a potential delay at shutdown. */
251 int __pthread_manager_event(void *arg)
253 /* If we have special thread_self processing, initialize it. */
254 #ifdef INIT_THREAD_SELF
255 INIT_THREAD_SELF(&__pthread_manager_thread, 1);
258 /* Get the lock the manager will free once all is correctly set up. */
259 __pthread_lock (THREAD_GETMEM((&__pthread_manager_thread), p_lock), NULL);
260 /* Free it immediately. */
261 __pthread_unlock (THREAD_GETMEM((&__pthread_manager_thread), p_lock));
263 return __pthread_manager(arg);
266 /* Process creation */
269 pthread_start_thread(void *arg)
271 pthread_descr self = (pthread_descr) arg;
272 struct pthread_request request;
274 /* Initialize special thread_self processing, if any. */
275 #ifdef INIT_THREAD_SELF
276 INIT_THREAD_SELF(self, self->p_nr);
279 /* Make sure our pid field is initialized, just in case we get there
280 before our father has initialized it. */
281 THREAD_SETMEM(self, p_pid, getpid());
282 /* Initial signal mask is that of the creating thread. (Otherwise,
283 we'd just inherit the mask of the thread manager.) */
284 sigprocmask(SIG_SETMASK, &self->p_start_args.mask, NULL);
285 /* Set the scheduling policy and priority for the new thread, if needed */
286 if (THREAD_GETMEM(self, p_start_args.schedpolicy) >= 0)
287 /* Explicit scheduling attributes were provided: apply them */
288 sched_setscheduler(THREAD_GETMEM(self, p_pid),
289 THREAD_GETMEM(self, p_start_args.schedpolicy),
290 &self->p_start_args.schedparam);
291 else if (__pthread_manager_thread.p_priority > 0)
292 /* Default scheduling required, but thread manager runs in realtime
293 scheduling: switch new thread to SCHED_OTHER policy */
295 struct sched_param default_params;
296 default_params.sched_priority = 0;
297 sched_setscheduler(THREAD_GETMEM(self, p_pid),
298 SCHED_OTHER, &default_params);
300 /* Make gdb aware of new thread */
301 if (__pthread_threads_debug && __pthread_sig_debug > 0) {
302 request.req_thread = self;
303 request.req_kind = REQ_DEBUG;
304 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
305 (char *) &request, sizeof(request)));
308 /* Run the thread code */
309 outcome = self->p_start_args.start_routine(THREAD_GETMEM(self,
311 /* Exit with the given return value */
312 __pthread_do_exit(outcome, CURRENT_STACK_FRAME);
317 pthread_start_thread_event(void *arg)
319 pthread_descr self = (pthread_descr) arg;
321 #ifdef INIT_THREAD_SELF
322 INIT_THREAD_SELF(self, self->p_nr);
324 /* Make sure our pid field is initialized, just in case we get there
325 before our father has initialized it. */
326 THREAD_SETMEM(self, p_pid, getpid());
327 /* Get the lock the manager will free once all is correctly set up. */
328 __pthread_lock (THREAD_GETMEM(self, p_lock), NULL);
329 /* Free it immediately. */
330 __pthread_unlock (THREAD_GETMEM(self, p_lock));
332 /* Continue with the real function. */
333 pthread_start_thread (arg);
336 static int pthread_allocate_stack(const pthread_attr_t *attr,
337 pthread_descr default_new_thread,
339 pthread_descr * out_new_thread,
340 char ** out_new_thread_bottom,
341 char ** out_guardaddr,
342 size_t * out_guardsize)
344 pthread_descr new_thread;
345 char * new_thread_bottom;
347 size_t stacksize, guardsize;
349 if (attr != NULL && attr->__stackaddr_set)
351 /* The user provided a stack. */
353 (pthread_descr) ((long)(attr->__stackaddr) & -sizeof(void *)) - 1;
354 new_thread_bottom = (char *) attr->__stackaddr - attr->__stacksize;
357 __pthread_nonstandard_stacks = 1;
358 #ifndef __ARCH_USE_MMU__
359 /* check the initial thread stack boundaries so they don't overlap */
360 NOMMU_INITIAL_THREAD_BOUNDS((char *) new_thread, (char *) new_thread_bottom);
362 PDEBUG("initial stack: bos=%p, tos=%p\n", __pthread_initial_thread_bos,
363 __pthread_initial_thread_tos);
368 #ifdef __ARCH_USE_MMU__
369 stacksize = STACK_SIZE - pagesize;
371 stacksize = MIN (stacksize, roundup(attr->__stacksize, pagesize));
372 /* Allocate space for stack and thread descriptor at default address */
373 new_thread = default_new_thread;
374 new_thread_bottom = (char *) (new_thread + 1) - stacksize;
375 if (mmap((caddr_t)((char *)(new_thread + 1) - INITIAL_STACK_SIZE),
376 INITIAL_STACK_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
377 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_GROWSDOWN,
378 -1, 0) == MAP_FAILED)
379 /* Bad luck, this segment is already mapped. */
381 /* We manage to get a stack. Now see whether we need a guard
382 and allocate it if necessary. Notice that the default
383 attributes (stack_size = STACK_SIZE - pagesize) do not need
384 a guard page, since the RLIMIT_STACK soft limit prevents stacks
385 from running into one another. */
386 if (stacksize == (size_t) (STACK_SIZE - pagesize))
388 /* We don't need a guard page. */
394 /* Put a bad page at the bottom of the stack */
395 guardsize = attr->__guardsize;
396 guardaddr = (void *)new_thread_bottom - guardsize;
397 if (mmap ((caddr_t) guardaddr, guardsize, 0, MAP_FIXED, -1, 0)
400 /* We don't make this an error. */
406 /* We cannot mmap to this huge chunk of stack space when we don't have
407 * an MMU. Pretend we are using a user provided stack even if there was
408 * none provided by the user. Thus, we get around the mmap and reservation
409 * of a huge stack segment. -StS */
411 stacksize = INITIAL_STACK_SIZE;
412 /* The user may want to use a non-default stacksize */
415 stacksize = attr->__stacksize;
418 /* malloc a stack - memory from the bottom up */
419 if ((new_thread_bottom = malloc(stacksize)) == NULL)
421 /* bad luck, we cannot malloc any more */
424 PDEBUG("malloced chunk: base=%p, size=0x%04x\n", new_thread_bottom, stacksize);
426 /* Set up the pointers. new_thread marks the TOP of the stack frame and
427 * the address of the pthread_descr struct at the same time. Therefore we
428 * must account for its size and fit it in the malloc()'ed block. The
429 * value of `new_thread' is then passed to clone() as the stack argument.
431 * ^ +------------------------+
432 * | | pthread_descr struct |
433 * | +------------------------+ <- new_thread
437 * v +------------------------+ <- new_thread_bottom
439 * Note: The calculated value of new_thread must be word aligned otherwise
440 * the kernel chokes on a non-aligned stack frame. Choose the lower
441 * available word boundary.
443 new_thread = ((pthread_descr) ((int)(new_thread_bottom + stacksize) & -sizeof(void*))) - 1;
447 PDEBUG("thread stack: bos=%p, tos=%p\n", new_thread_bottom, new_thread);
449 /* check the initial thread stack boundaries so they don't overlap */
450 NOMMU_INITIAL_THREAD_BOUNDS((char *) new_thread, (char *) new_thread_bottom);
452 PDEBUG("initial stack: bos=%p, tos=%p\n", __pthread_initial_thread_bos,
453 __pthread_initial_thread_tos);
455 /* on non-MMU systems we always have non-standard stack frames */
456 __pthread_nonstandard_stacks = 1;
458 #endif /* __ARCH_USE_MMU__ */
461 /* Clear the thread data structure. */
462 memset (new_thread, '\0', sizeof (*new_thread));
463 *out_new_thread = new_thread;
464 *out_new_thread_bottom = new_thread_bottom;
465 *out_guardaddr = guardaddr;
466 *out_guardsize = guardsize;
470 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
471 void * (*start_routine)(void *), void *arg,
472 sigset_t * mask, int father_pid,
474 td_thr_events_t *event_maskp)
478 pthread_descr new_thread;
479 char * new_thread_bottom;
480 pthread_t new_thread_id;
481 char *guardaddr = NULL;
482 size_t guardsize = 0;
483 int pagesize = getpagesize();
486 /* First check whether we have to change the policy and if yes, whether
487 we can do this. Normally this should be done by examining the
488 return value of the sched_setscheduler call in pthread_start_thread
489 but this is hard to implement. FIXME */
490 if (attr != NULL && attr->__schedpolicy != SCHED_OTHER && geteuid () != 0)
492 /* Find a free segment for the thread, and allocate a stack if needed */
493 for (sseg = 2; ; sseg++)
495 if (sseg >= PTHREAD_THREADS_MAX)
497 if (__pthread_handles[sseg].h_descr != NULL)
499 if (pthread_allocate_stack(attr, thread_segment(sseg), pagesize,
500 &new_thread, &new_thread_bottom,
501 &guardaddr, &guardsize) == 0)
503 #ifndef __ARCH_USE_MMU__
505 /* When there is MMU, mmap () is used to allocate the stack. If one
506 * segment is already mapped, we should continue to see if we can
507 * use the next one. However, when there is no MMU, malloc () is used.
508 * It's waste of CPU cycles to continue to try if it fails. */
512 __pthread_handles_num++;
513 /* Allocate new thread identifier */
514 pthread_threads_counter += PTHREAD_THREADS_MAX;
515 new_thread_id = sseg + pthread_threads_counter;
516 /* Initialize the thread descriptor. Elements which have to be
517 initialized to zero already have this value. */
518 new_thread->p_tid = new_thread_id;
519 new_thread->p_lock = &(__pthread_handles[sseg].h_lock);
520 new_thread->p_cancelstate = PTHREAD_CANCEL_ENABLE;
521 new_thread->p_canceltype = PTHREAD_CANCEL_DEFERRED;
522 new_thread->p_errnop = &new_thread->p_errno;
523 new_thread->p_h_errnop = &new_thread->p_h_errno;
524 #ifdef __UCLIBC_HAS_XLOCALE__
525 /* Initialize thread's locale to the global locale. */
526 new_thread->locale = __global_locale;
527 #endif /* __UCLIBC_HAS_XLOCALE__ */
528 new_thread->p_guardaddr = guardaddr;
529 new_thread->p_guardsize = guardsize;
530 new_thread->p_self = new_thread;
531 new_thread->p_nr = sseg;
532 /* Initialize the thread handle */
533 __pthread_init_lock(&__pthread_handles[sseg].h_lock);
534 __pthread_handles[sseg].h_descr = new_thread;
535 __pthread_handles[sseg].h_bottom = new_thread_bottom;
536 /* Determine scheduling parameters for the thread */
537 new_thread->p_start_args.schedpolicy = -1;
539 new_thread->p_detached = attr->__detachstate;
540 new_thread->p_userstack = attr->__stackaddr_set;
542 switch(attr->__inheritsched) {
543 case PTHREAD_EXPLICIT_SCHED:
544 new_thread->p_start_args.schedpolicy = attr->__schedpolicy;
545 memcpy (&new_thread->p_start_args.schedparam, &attr->__schedparam,
546 sizeof (struct sched_param));
548 case PTHREAD_INHERIT_SCHED:
549 new_thread->p_start_args.schedpolicy = sched_getscheduler(father_pid);
550 sched_getparam(father_pid, &new_thread->p_start_args.schedparam);
553 new_thread->p_priority =
554 new_thread->p_start_args.schedparam.sched_priority;
556 /* Finish setting up arguments to pthread_start_thread */
557 new_thread->p_start_args.start_routine = start_routine;
558 new_thread->p_start_args.arg = arg;
559 new_thread->p_start_args.mask = *mask;
560 /* Raise priority of thread manager if needed */
561 __pthread_manager_adjust_prio(new_thread->p_priority);
562 /* Do the cloning. We have to use two different functions depending
563 on whether we are debugging or not. */
564 pid = 0; /* Note that the thread never can have PID zero. */
567 /* ******************************************************** */
568 /* This code was moved from below to cope with running threads
569 * on uClinux systems. See comment below...
570 * Insert new thread in doubly linked list of active threads */
571 new_thread->p_prevlive = __pthread_main_thread;
572 new_thread->p_nextlive = __pthread_main_thread->p_nextlive;
573 __pthread_main_thread->p_nextlive->p_prevlive = new_thread;
574 __pthread_main_thread->p_nextlive = new_thread;
575 /* ********************************************************* */
579 /* See whether the TD_CREATE event bit is set in any of the
581 int idx = __td_eventword (TD_CREATE);
582 uint32_t mask = __td_eventmask (TD_CREATE);
584 if ((mask & (__pthread_threads_events.event_bits[idx]
585 | event_maskp->event_bits[idx])) != 0)
587 /* Lock the mutex the child will use now so that it will stop. */
588 __pthread_lock(new_thread->p_lock, NULL);
590 /* We have to report this event. */
592 pid = __clone2(pthread_start_thread_event, (void **) new_thread,
593 (char *)new_thread - new_thread_bottom,
594 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
595 __pthread_sig_cancel, new_thread);
597 pid = clone(pthread_start_thread_event, (void **) new_thread,
598 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
599 __pthread_sig_cancel, new_thread);
605 /* Now fill in the information about the new thread in
606 the newly created thread's data structure. We cannot let
607 the new thread do this since we don't know whether it was
608 already scheduled when we send the event. */
609 new_thread->p_eventbuf.eventdata = new_thread;
610 new_thread->p_eventbuf.eventnum = TD_CREATE;
611 __pthread_last_event = new_thread;
613 /* We have to set the PID here since the callback function
614 in the debug library will need it and we cannot guarantee
615 the child got scheduled before the debugger. */
616 new_thread->p_pid = pid;
618 /* Now call the function which signals the event. */
619 __linuxthreads_create_event ();
621 /* Now restart the thread. */
622 __pthread_unlock(new_thread->p_lock);
628 PDEBUG("cloning new_thread = %p\n", new_thread);
630 pid = __clone2(pthread_start_thread, (void **) new_thread,
631 (char *)new_thread - new_thread_bottom,
632 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
633 __pthread_sig_cancel, new_thread);
635 pid = clone(pthread_start_thread, (void **) new_thread,
636 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
637 __pthread_sig_cancel, new_thread);
641 /* Check if cloning succeeded */
643 /********************************************************
644 * Code inserted to remove the thread from our list of active
645 * threads in case of failure (needed to cope with uClinux),
646 * See comment below. */
647 new_thread->p_nextlive->p_prevlive = new_thread->p_prevlive;
648 new_thread->p_prevlive->p_nextlive = new_thread->p_nextlive;
649 /********************************************************/
651 /* Free the stack if we allocated it */
652 if (attr == NULL || !attr->__stackaddr_set)
654 #ifdef __ARCH_USE_MMU__
655 if (new_thread->p_guardsize != 0)
656 munmap(new_thread->p_guardaddr, new_thread->p_guardsize);
657 munmap((caddr_t)((char *)(new_thread+1) - INITIAL_STACK_SIZE),
660 free(new_thread_bottom);
661 #endif /* __ARCH_USE_MMU__ */
663 __pthread_handles[sseg].h_descr = NULL;
664 __pthread_handles[sseg].h_bottom = NULL;
665 __pthread_handles_num--;
668 PDEBUG("new thread pid = %d\n", pid);
671 /* ***********************************************************
672 This code has been moved before the call to clone(). In uClinux,
673 the use of wait on a semaphore is dependant upon that the child so
674 the child must be in the active threads list. This list is used in
675 pthread_find_self() to get the pthread_descr of self. So, if the
676 child calls sem_wait before this code is executed , it will hang
677 forever and initial_thread will instead be posted by a sem_post
680 /* Insert new thread in doubly linked list of active threads */
681 new_thread->p_prevlive = __pthread_main_thread;
682 new_thread->p_nextlive = __pthread_main_thread->p_nextlive;
683 __pthread_main_thread->p_nextlive->p_prevlive = new_thread;
684 __pthread_main_thread->p_nextlive = new_thread;
685 /************************************************************/
688 /* Set pid field of the new thread, in case we get there before the
690 new_thread->p_pid = pid;
692 *thread = new_thread_id;
697 /* Try to free the resources of a thread when requested by pthread_join
698 or pthread_detach on a terminated thread. */
700 static void pthread_free(pthread_descr th)
702 pthread_handle handle;
703 pthread_readlock_info *iter, *next;
706 /* Make the handle invalid */
707 handle = thread_handle(th->p_tid);
708 __pthread_lock(&handle->h_lock, NULL);
709 h_bottom_save = handle->h_bottom;
710 handle->h_descr = NULL;
711 handle->h_bottom = (char *)(-1L);
712 __pthread_unlock(&handle->h_lock);
713 #ifdef FREE_THREAD_SELF
714 FREE_THREAD_SELF(th, th->p_nr);
716 /* One fewer threads in __pthread_handles */
717 __pthread_handles_num--;
719 /* Destroy read lock list, and list of free read lock structures.
720 If the former is not empty, it means the thread exited while
721 holding read locks! */
723 for (iter = th->p_readlock_list; iter != NULL; iter = next)
725 next = iter->pr_next;
729 for (iter = th->p_readlock_free; iter != NULL; iter = next)
731 next = iter->pr_next;
735 /* If initial thread, nothing to free */
736 if (th == &__pthread_initial_thread) return;
737 #ifdef __ARCH_USE_MMU__
738 if (!th->p_userstack)
740 /* Free the stack and thread descriptor area */
741 if (th->p_guardsize != 0)
742 munmap(th->p_guardaddr, th->p_guardsize);
743 munmap((caddr_t) ((char *)(th+1) - STACK_SIZE), STACK_SIZE);
746 /* For non-MMU systems we always malloc the stack, so free it here. -StS */
747 if (!th->p_userstack) {
750 #endif /* __ARCH_USE_MMU__ */
753 /* Handle threads that have exited */
755 static void pthread_exited(pid_t pid)
759 /* Find thread with that pid */
760 for (th = __pthread_main_thread->p_nextlive;
761 th != __pthread_main_thread;
762 th = th->p_nextlive) {
763 if (th->p_pid == pid) {
764 /* Remove thread from list of active threads */
765 th->p_nextlive->p_prevlive = th->p_prevlive;
766 th->p_prevlive->p_nextlive = th->p_nextlive;
767 /* Mark thread as exited, and if detached, free its resources */
768 __pthread_lock(th->p_lock, NULL);
770 /* If we have to signal this event do it now. */
771 if (th->p_report_events)
773 /* See whether TD_REAP is in any of the mask. */
774 int idx = __td_eventword (TD_REAP);
775 uint32_t mask = __td_eventmask (TD_REAP);
777 if ((mask & (__pthread_threads_events.event_bits[idx]
778 | th->p_eventbuf.eventmask.event_bits[idx])) != 0)
780 /* Yep, we have to signal the reapage. */
781 th->p_eventbuf.eventnum = TD_REAP;
782 th->p_eventbuf.eventdata = th;
783 __pthread_last_event = th;
785 /* Now call the function to signal the event. */
786 __linuxthreads_reap_event();
789 detached = th->p_detached;
790 __pthread_unlock(th->p_lock);
796 /* If all threads have exited and the main thread is pending on a
797 pthread_exit, wake up the main thread and terminate ourselves. */
798 if (main_thread_exiting &&
799 __pthread_main_thread->p_nextlive == __pthread_main_thread) {
800 restart(__pthread_main_thread);
801 /* Same logic as REQ_MAIN_THREAD_EXIT. */
805 static void pthread_reap_children(void)
811 while ((pid = __libc_waitpid(-1, &status, WNOHANG | __WCLONE)) > 0) {
813 if (WIFSIGNALED(status)) {
814 /* If a thread died due to a signal, send the same signal to
815 all other threads, including the main thread. */
816 pthread_kill_all_threads(WTERMSIG(status), 1);
822 /* Try to free the resources of a thread when requested by pthread_join
823 or pthread_detach on a terminated thread. */
825 static void pthread_handle_free(pthread_t th_id)
827 pthread_handle handle = thread_handle(th_id);
830 __pthread_lock(&handle->h_lock, NULL);
831 if (invalid_handle(handle, th_id)) {
832 /* pthread_reap_children has deallocated the thread already,
833 nothing needs to be done */
834 __pthread_unlock(&handle->h_lock);
837 th = handle->h_descr;
839 __pthread_unlock(&handle->h_lock);
842 /* The Unix process of the thread is still running.
843 Mark the thread as detached so that the thread manager will
844 deallocate its resources when the Unix process exits. */
846 __pthread_unlock(&handle->h_lock);
850 /* Send a signal to all running threads */
852 static void pthread_kill_all_threads(int sig, int main_thread_also)
855 for (th = __pthread_main_thread->p_nextlive;
856 th != __pthread_main_thread;
857 th = th->p_nextlive) {
858 kill(th->p_pid, sig);
860 if (main_thread_also) {
861 kill(__pthread_main_thread->p_pid, sig);
865 /* Process-wide exit() */
867 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
870 __pthread_exit_requested = 1;
871 __pthread_exit_code = exitcode;
872 /* Send the CANCEL signal to all running threads, including the main
873 thread, but excluding the thread from which the exit request originated
874 (that thread must complete the exit, e.g. calling atexit functions
875 and flushing stdio buffers). */
876 for (th = issuing_thread->p_nextlive;
877 th != issuing_thread;
878 th = th->p_nextlive) {
879 kill(th->p_pid, __pthread_sig_cancel);
881 /* Now, wait for all these threads, so that they don't become zombies
882 and their times are properly added to the thread manager's times. */
883 for (th = issuing_thread->p_nextlive;
884 th != issuing_thread;
885 th = th->p_nextlive) {
886 waitpid(th->p_pid, NULL, __WCLONE);
888 restart(issuing_thread);
892 /* Handler for __pthread_sig_cancel in thread manager thread */
894 void __pthread_manager_sighandler(int sig attribute_unused)
896 int kick_manager = terminated_children == 0 && main_thread_exiting;
897 terminated_children = 1;
899 /* If the main thread is terminating, kick the thread manager loop
900 each time some threads terminate. This eliminates a two second
901 shutdown delay caused by the thread manager sleeping in the
902 call to __poll(). Instead, the thread manager is kicked into
903 action, reaps the outstanding threads and resumes the main thread
904 so that it can complete the shutdown. */
907 struct pthread_request request;
908 request.req_thread = 0;
909 request.req_kind = REQ_KICK;
910 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
911 (char *) &request, sizeof(request)));
915 /* Adjust priority of thread manager so that it always run at a priority
916 higher than all threads */
918 void __pthread_manager_adjust_prio(int thread_prio)
920 struct sched_param param;
922 if (thread_prio <= __pthread_manager_thread.p_priority) return;
923 param.sched_priority =
924 thread_prio < sched_get_priority_max(SCHED_FIFO)
925 ? thread_prio + 1 : thread_prio;
926 sched_setscheduler(__pthread_manager_thread.p_pid, SCHED_FIFO, ¶m);
927 __pthread_manager_thread.p_priority = thread_prio;