* sysdeps/unix/sysv/linux/m68k/sysdep.h (INLINE_SYSCALL): Don't
[glibc.git] / linuxthreads / manager.c
blob6016219b5ebd22d765fda488c7eb4cabed1e7412
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 #ifdef NEED_SEPARATE_REGISTER_STACK
357 void *res_addr;
359 if (attr != NULL)
361 guardsize = page_roundup (attr->__guardsize, granularity);
362 stacksize = STACK_SIZE - guardsize;
363 stacksize = MIN (stacksize,
364 page_roundup (attr->__stacksize, granularity));
366 else
368 guardsize = granularity;
369 stacksize = STACK_SIZE - granularity;
372 new_thread = default_new_thread;
373 new_thread_bottom = (char *) (new_thread + 1) - stacksize - guardsize;
374 /* Includes guard area, unlike the normal case. Use the bottom
375 end of the segment as backing store for the register stack.
376 Needed on IA64. In this case, we also map the entire stack at
377 once. According to David Mosberger, that's cheaper. It also
378 avoids the risk of intermittent failures due to other mappings
379 in the same region. The cost is that we might be able to map
380 slightly fewer stacks. */
382 /* XXX Fix for floating stacks with variable sizes. */
384 /* First the main stack: */
385 map_addr = (caddr_t)((char *)(new_thread + 1) - stacksize / 2);
386 res_addr = mmap(map_addr, stacksize / 2,
387 PROT_READ | PROT_WRITE | PROT_EXEC,
388 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
389 if (res_addr != map_addr)
391 /* Bad luck, this segment is already mapped. */
392 if (res_addr != MAP_FAILED)
393 munmap(res_addr, stacksize / 2);
394 return -1;
396 /* Then the register stack: */
397 map_addr = (caddr_t)new_thread_bottom;
398 res_addr = mmap(map_addr, stacksize/2,
399 PROT_READ | PROT_WRITE | PROT_EXEC,
400 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
401 if (res_addr != map_addr)
403 if (res_addr != MAP_FAILED)
404 munmap(res_addr, stacksize / 2);
405 munmap((caddr_t)((char *)(new_thread + 1) - stacksize/2),
406 stacksize/2);
407 return -1;
410 guardaddr = new_thread_bottom + stacksize/2;
411 /* We leave the guard area in the middle unmapped. */
412 #else /* !NEED_SEPARATE_REGISTER_STACK */
413 # if FLOATING_STACKS
414 if (attr != NULL)
416 guardsize = page_roundup (attr->__guardsize, granularity);
417 stacksize = __pthread_max_stacksize - guardsize;
418 stacksize = MIN (stacksize,
419 page_roundup (attr->__stacksize, granularity));
421 else
423 guardsize = granularity;
424 stacksize = __pthread_max_stacksize - guardsize;
427 map_addr = mmap(NULL, stacksize + guardsize,
428 PROT_READ | PROT_WRITE | PROT_EXEC,
429 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
430 if (map_addr == MAP_FAILED)
431 /* No more memory available. */
432 return -1;
434 # ifdef _STACK_GROWS_DOWN
435 guardaddr = map_addr;
436 if (guardsize > 0)
437 mprotect (guardaddr, guardsize, PROT_NONE);
439 new_thread_bottom = (char *) map_addr + guardsize;
440 new_thread = ((pthread_descr) (new_thread_bottom + stacksize)) - 1;
441 # elif _STACK_GROWS_UP
442 guardaddr = map_addr + stacksize;
443 if (guardsize > 0)
444 mprotect (guardaddr, guardsize, PROT_NONE);
446 new_thread = (pthread_descr) map_addr;
447 new_thread_bottom = (char *) (new_thread + 1);
448 # else
449 # error You must define a stack direction
450 # endif /* Stack direction */
451 # else /* !FLOATING_STACKS */
452 void *res_addr;
454 if (attr != NULL)
456 guardsize = page_roundup (attr->__guardsize, granularity);
457 stacksize = STACK_SIZE - guardsize;
458 stacksize = MIN (stacksize,
459 page_roundup (attr->__stacksize, granularity));
461 else
463 guardsize = granularity;
464 stacksize = STACK_SIZE - granularity;
467 # ifdef _STACK_GROWS_DOWN
468 new_thread = default_new_thread;
469 new_thread_bottom = (char *) (new_thread + 1) - stacksize;
470 map_addr = new_thread_bottom - guardsize;
471 res_addr = mmap(map_addr, stacksize + guardsize,
472 PROT_READ | PROT_WRITE | PROT_EXEC,
473 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
474 if (res_addr != map_addr)
476 /* Bad luck, this segment is already mapped. */
477 if (res_addr != MAP_FAILED)
478 munmap (res_addr, stacksize + guardsize);
479 return -1;
482 /* We manage to get a stack. Protect the guard area pages if
483 necessary. */
484 guardaddr = map_addr;
485 if (guardsize > 0)
486 mprotect (guardaddr, guardsize, PROT_NONE);
487 # else
488 /* The thread description goes at the bottom of this area, and
489 * the stack starts directly above it.
491 new_thread = (pthread_descr)((unsigned long)default_new_thread &~ (STACK_SIZE - 1));
492 map_addr = mmap(new_thread, stacksize + guardsize,
493 PROT_READ | PROT_WRITE | PROT_EXEC,
494 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
495 if (map_addr == MAP_FAILED)
496 return -1;
498 new_thread_bottom = map_addr + sizeof(*new_thread);
499 guardaddr = map_addr + stacksize;
500 if (guardsize > 0)
501 mprotect (guardaddr, guardsize, PROT_NONE);
503 # endif /* stack direction */
504 # endif
505 #endif /* !NEED_SEPARATE_REGISTER_STACK */
507 *out_new_thread = new_thread;
508 *out_new_thread_bottom = new_thread_bottom;
509 *out_guardaddr = guardaddr;
510 *out_guardsize = guardsize;
511 return 0;
514 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
515 void * (*start_routine)(void *), void *arg,
516 sigset_t * mask, int father_pid,
517 int report_events,
518 td_thr_events_t *event_maskp)
520 size_t sseg;
521 int pid;
522 pthread_descr new_thread;
523 char * new_thread_bottom;
524 pthread_t new_thread_id;
525 char *guardaddr = NULL;
526 size_t guardsize = 0;
527 int pagesize = __getpagesize();
529 /* First check whether we have to change the policy and if yes, whether
530 we can do this. Normally this should be done by examining the
531 return value of the __sched_setscheduler call in pthread_start_thread
532 but this is hard to implement. FIXME */
533 if (attr != NULL && attr->__schedpolicy != SCHED_OTHER && geteuid () != 0)
534 return EPERM;
535 /* Find a free segment for the thread, and allocate a stack if needed */
536 for (sseg = 2; ; sseg++)
538 if (sseg >= PTHREAD_THREADS_MAX)
539 return EAGAIN;
540 if (__pthread_handles[sseg].h_descr != NULL)
541 continue;
542 if (pthread_allocate_stack(attr, thread_segment(sseg),
543 pagesize,
544 &new_thread, &new_thread_bottom,
545 &guardaddr, &guardsize) == 0)
546 break;
548 __pthread_handles_num++;
549 /* Allocate new thread identifier */
550 pthread_threads_counter += PTHREAD_THREADS_MAX;
551 new_thread_id = sseg + pthread_threads_counter;
552 /* Initialize the thread descriptor. Elements which have to be
553 initialized to zero already have this value. */
554 new_thread->p_tid = new_thread_id;
555 new_thread->p_lock = &(__pthread_handles[sseg].h_lock);
556 new_thread->p_cancelstate = PTHREAD_CANCEL_ENABLE;
557 new_thread->p_canceltype = PTHREAD_CANCEL_DEFERRED;
558 new_thread->p_errnop = &new_thread->p_errno;
559 new_thread->p_h_errnop = &new_thread->p_h_errno;
560 new_thread->p_resp = &new_thread->p_res;
561 new_thread->p_guardaddr = guardaddr;
562 new_thread->p_guardsize = guardsize;
563 new_thread->p_header.data.self = new_thread;
564 new_thread->p_nr = sseg;
565 new_thread->p_inheritsched = attr ? attr->__inheritsched : 0;
566 /* Initialize the thread handle */
567 __pthread_init_lock(&__pthread_handles[sseg].h_lock);
568 __pthread_handles[sseg].h_descr = new_thread;
569 __pthread_handles[sseg].h_bottom = new_thread_bottom;
570 /* Determine scheduling parameters for the thread */
571 new_thread->p_start_args.schedpolicy = -1;
572 if (attr != NULL) {
573 new_thread->p_detached = attr->__detachstate;
574 new_thread->p_userstack = attr->__stackaddr_set;
576 switch(attr->__inheritsched) {
577 case PTHREAD_EXPLICIT_SCHED:
578 new_thread->p_start_args.schedpolicy = attr->__schedpolicy;
579 memcpy (&new_thread->p_start_args.schedparam, &attr->__schedparam,
580 sizeof (struct sched_param));
581 break;
582 case PTHREAD_INHERIT_SCHED:
583 new_thread->p_start_args.schedpolicy = __sched_getscheduler(father_pid);
584 __sched_getparam(father_pid, &new_thread->p_start_args.schedparam);
585 break;
587 new_thread->p_priority =
588 new_thread->p_start_args.schedparam.sched_priority;
590 /* Finish setting up arguments to pthread_start_thread */
591 new_thread->p_start_args.start_routine = start_routine;
592 new_thread->p_start_args.arg = arg;
593 new_thread->p_start_args.mask = *mask;
594 /* Make the new thread ID available already now. If any of the later
595 functions fail we return an error value and the caller must not use
596 the stored thread ID. */
597 *thread = new_thread_id;
598 /* Raise priority of thread manager if needed */
599 __pthread_manager_adjust_prio(new_thread->p_priority);
600 /* Do the cloning. We have to use two different functions depending
601 on whether we are debugging or not. */
602 pid = 0; /* Note that the thread never can have PID zero. */
603 if (report_events)
605 /* See whether the TD_CREATE event bit is set in any of the
606 masks. */
607 int idx = __td_eventword (TD_CREATE);
608 uint32_t mask = __td_eventmask (TD_CREATE);
610 if ((mask & (__pthread_threads_events.event_bits[idx]
611 | event_maskp->event_bits[idx])) != 0)
613 /* Lock the mutex the child will use now so that it will stop. */
614 __pthread_lock(new_thread->p_lock, NULL);
616 /* We have to report this event. */
617 #ifdef NEED_SEPARATE_REGISTER_STACK
618 /* Perhaps this version should be used on all platforms. But
619 this requires that __clone2 be uniformly supported
620 everywhere.
622 And there is some argument for changing the __clone2
623 interface to pass sp and bsp instead, making it more IA64
624 specific, but allowing stacks to grow outward from each
625 other, to get less paging and fewer mmaps. */
626 pid = __clone2(pthread_start_thread_event,
627 (void **)new_thread_bottom,
628 (char *)new_thread - new_thread_bottom,
629 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
630 __pthread_sig_cancel, new_thread);
631 #elif _STACK_GROWS_UP
632 pid = __clone(pthread_start_thread_event, (void **) new_thread_bottom,
633 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
634 __pthread_sig_cancel, new_thread);
635 #else
636 pid = __clone(pthread_start_thread_event, (void **) new_thread,
637 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
638 __pthread_sig_cancel, new_thread);
639 #endif
640 if (pid != -1)
642 /* Now fill in the information about the new thread in
643 the newly created thread's data structure. We cannot let
644 the new thread do this since we don't know whether it was
645 already scheduled when we send the event. */
646 new_thread->p_eventbuf.eventdata = new_thread;
647 new_thread->p_eventbuf.eventnum = TD_CREATE;
648 __pthread_last_event = new_thread;
650 /* We have to set the PID here since the callback function
651 in the debug library will need it and we cannot guarantee
652 the child got scheduled before the debugger. */
653 new_thread->p_pid = pid;
655 /* Now call the function which signals the event. */
656 __linuxthreads_create_event ();
658 /* Now restart the thread. */
659 __pthread_unlock(new_thread->p_lock);
663 if (pid == 0)
665 #ifdef NEED_SEPARATE_REGISTER_STACK
666 pid = __clone2(pthread_start_thread,
667 (void **)new_thread_bottom,
668 (char *)new_thread - new_thread_bottom,
669 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
670 __pthread_sig_cancel, new_thread);
671 #elif _STACK_GROWS_UP
672 pid = __clone(pthread_start_thread, (void **) new_thread_bottom,
673 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
674 __pthread_sig_cancel, new_thread);
675 #else
676 pid = __clone(pthread_start_thread, (void **) new_thread,
677 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
678 __pthread_sig_cancel, new_thread);
679 #endif /* !NEED_SEPARATE_REGISTER_STACK */
681 /* Check if cloning succeeded */
682 if (pid == -1) {
683 /* Free the stack if we allocated it */
684 if (attr == NULL || !attr->__stackaddr_set)
686 #ifdef NEED_SEPARATE_REGISTER_STACK
687 size_t stacksize = ((char *)(new_thread->p_guardaddr)
688 - new_thread_bottom);
689 munmap((caddr_t)new_thread_bottom,
690 2 * stacksize + new_thread->p_guardsize);
691 #elif _STACK_GROWS_UP
692 size_t stacksize = guardaddr - (char *)new_thread;
693 munmap(new_thread, stacksize + guardsize);
694 #else
695 size_t stacksize = (char *)(new_thread+1) - new_thread_bottom;
696 munmap(new_thread_bottom - guardsize, guardsize + stacksize);
697 #endif
699 __pthread_handles[sseg].h_descr = NULL;
700 __pthread_handles[sseg].h_bottom = NULL;
701 __pthread_handles_num--;
702 return errno;
704 /* Insert new thread in doubly linked list of active threads */
705 new_thread->p_prevlive = __pthread_main_thread;
706 new_thread->p_nextlive = __pthread_main_thread->p_nextlive;
707 __pthread_main_thread->p_nextlive->p_prevlive = new_thread;
708 __pthread_main_thread->p_nextlive = new_thread;
709 /* Set pid field of the new thread, in case we get there before the
710 child starts. */
711 new_thread->p_pid = pid;
712 return 0;
716 /* Try to free the resources of a thread when requested by pthread_join
717 or pthread_detach on a terminated thread. */
719 static void pthread_free(pthread_descr th)
721 pthread_handle handle;
722 pthread_readlock_info *iter, *next;
724 ASSERT(th->p_exited);
725 /* Make the handle invalid */
726 handle = thread_handle(th->p_tid);
727 __pthread_lock(&handle->h_lock, NULL);
728 handle->h_descr = NULL;
729 handle->h_bottom = (char *)(-1L);
730 __pthread_unlock(&handle->h_lock);
731 #ifdef FREE_THREAD
732 FREE_THREAD(th, th->p_nr);
733 #endif
734 /* One fewer threads in __pthread_handles */
735 __pthread_handles_num--;
737 /* Destroy read lock list, and list of free read lock structures.
738 If the former is not empty, it means the thread exited while
739 holding read locks! */
741 for (iter = th->p_readlock_list; iter != NULL; iter = next)
743 next = iter->pr_next;
744 free(iter);
747 for (iter = th->p_readlock_free; iter != NULL; iter = next)
749 next = iter->pr_next;
750 free(iter);
753 /* If initial thread, nothing to free */
754 if (!th->p_userstack)
756 size_t guardsize = th->p_guardsize;
757 /* Free the stack and thread descriptor area */
758 char *guardaddr = th->p_guardaddr;
759 #ifdef _STACK_GROWS_UP
760 size_t stacksize = guardaddr - (char *)th;
761 guardaddr = (char *)th;
762 #else
763 /* Guardaddr is always set, even if guardsize is 0. This allows
764 us to compute everything else. */
765 size_t stacksize = (char *)(th+1) - guardaddr - guardsize;
766 #ifdef NEED_SEPARATE_REGISTER_STACK
767 /* Take account of the register stack, which is below guardaddr. */
768 guardaddr -= stacksize;
769 stacksize *= 2;
770 #endif
771 #endif
772 /* Unmap the stack. */
773 munmap(guardaddr, stacksize + guardsize);
777 /* Handle threads that have exited */
779 static void pthread_exited(pid_t pid)
781 pthread_descr th;
782 int detached;
783 /* Find thread with that pid */
784 for (th = __pthread_main_thread->p_nextlive;
785 th != __pthread_main_thread;
786 th = th->p_nextlive) {
787 if (th->p_pid == pid) {
788 /* Remove thread from list of active threads */
789 th->p_nextlive->p_prevlive = th->p_prevlive;
790 th->p_prevlive->p_nextlive = th->p_nextlive;
791 /* Mark thread as exited, and if detached, free its resources */
792 __pthread_lock(th->p_lock, NULL);
793 th->p_exited = 1;
794 /* If we have to signal this event do it now. */
795 if (th->p_report_events)
797 /* See whether TD_REAP is in any of the mask. */
798 int idx = __td_eventword (TD_REAP);
799 uint32_t mask = __td_eventmask (TD_REAP);
801 if ((mask & (__pthread_threads_events.event_bits[idx]
802 | th->p_eventbuf.eventmask.event_bits[idx])) != 0)
804 /* Yep, we have to signal the reapage. */
805 th->p_eventbuf.eventnum = TD_REAP;
806 th->p_eventbuf.eventdata = th;
807 __pthread_last_event = th;
809 /* Now call the function to signal the event. */
810 __linuxthreads_reap_event();
813 detached = th->p_detached;
814 __pthread_unlock(th->p_lock);
815 if (detached)
816 pthread_free(th);
817 break;
820 /* If all threads have exited and the main thread is pending on a
821 pthread_exit, wake up the main thread and terminate ourselves. */
822 if (main_thread_exiting &&
823 __pthread_main_thread->p_nextlive == __pthread_main_thread) {
824 restart(__pthread_main_thread);
825 /* Same logic as REQ_MAIN_THREAD_EXIT. */
829 static void pthread_reap_children(void)
831 pid_t pid;
832 int status;
834 while ((pid = __libc_waitpid(-1, &status, WNOHANG | __WCLONE)) > 0) {
835 pthread_exited(pid);
836 if (WIFSIGNALED(status)) {
837 /* If a thread died due to a signal, send the same signal to
838 all other threads, including the main thread. */
839 pthread_kill_all_threads(WTERMSIG(status), 1);
840 _exit(0);
845 /* Try to free the resources of a thread when requested by pthread_join
846 or pthread_detach on a terminated thread. */
848 static void pthread_handle_free(pthread_t th_id)
850 pthread_handle handle = thread_handle(th_id);
851 pthread_descr th;
853 __pthread_lock(&handle->h_lock, NULL);
854 if (nonexisting_handle(handle, th_id)) {
855 /* pthread_reap_children has deallocated the thread already,
856 nothing needs to be done */
857 __pthread_unlock(&handle->h_lock);
858 return;
860 th = handle->h_descr;
861 if (th->p_exited) {
862 __pthread_unlock(&handle->h_lock);
863 pthread_free(th);
864 } else {
865 /* The Unix process of the thread is still running.
866 Mark the thread as detached so that the thread manager will
867 deallocate its resources when the Unix process exits. */
868 th->p_detached = 1;
869 __pthread_unlock(&handle->h_lock);
873 /* Send a signal to all running threads */
875 static void pthread_kill_all_threads(int sig, int main_thread_also)
877 pthread_descr th;
878 for (th = __pthread_main_thread->p_nextlive;
879 th != __pthread_main_thread;
880 th = th->p_nextlive) {
881 kill(th->p_pid, sig);
883 if (main_thread_also) {
884 kill(__pthread_main_thread->p_pid, sig);
888 /* Process-wide exit() */
890 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
892 pthread_descr th;
893 __pthread_exit_requested = 1;
894 __pthread_exit_code = exitcode;
895 /* Send the CANCEL signal to all running threads, including the main
896 thread, but excluding the thread from which the exit request originated
897 (that thread must complete the exit, e.g. calling atexit functions
898 and flushing stdio buffers). */
899 for (th = issuing_thread->p_nextlive;
900 th != issuing_thread;
901 th = th->p_nextlive) {
902 kill(th->p_pid, __pthread_sig_cancel);
904 /* Now, wait for all these threads, so that they don't become zombies
905 and their times are properly added to the thread manager's times. */
906 for (th = issuing_thread->p_nextlive;
907 th != issuing_thread;
908 th = th->p_nextlive) {
909 waitpid(th->p_pid, NULL, __WCLONE);
911 restart(issuing_thread);
912 _exit(0);
915 /* Handler for __pthread_sig_cancel in thread manager thread */
917 void __pthread_manager_sighandler(int sig)
919 int kick_manager = terminated_children == 0 && main_thread_exiting;
920 terminated_children = 1;
922 /* If the main thread is terminating, kick the thread manager loop
923 each time some threads terminate. This eliminates a two second
924 shutdown delay caused by the thread manager sleeping in the
925 call to __poll(). Instead, the thread manager is kicked into
926 action, reaps the outstanding threads and resumes the main thread
927 so that it can complete the shutdown. */
929 if (kick_manager) {
930 struct pthread_request request;
931 request.req_thread = 0;
932 request.req_kind = REQ_KICK;
933 __libc_write(__pthread_manager_request, (char *) &request, sizeof(request));
937 /* Adjust priority of thread manager so that it always run at a priority
938 higher than all threads */
940 void __pthread_manager_adjust_prio(int thread_prio)
942 struct sched_param param;
944 if (thread_prio <= __pthread_manager_thread.p_priority) return;
945 param.sched_priority =
946 thread_prio < __sched_get_priority_max(SCHED_FIFO)
947 ? thread_prio + 1 : thread_prio;
948 __sched_setscheduler(__pthread_manager_thread.p_pid, SCHED_FIFO, &param);
949 __pthread_manager_thread.p_priority = thread_prio;