* sysdeps/m68k/setjmp.c: Add hidden_def.
[glibc/pb-stable.git] / linuxthreads / manager.c
blobf8647b47ca3c07ddb8f395c614afa92860aef4b2
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 <assert.h>
18 #include <errno.h>
19 #include <sched.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/poll.h> /* for poll */
26 #include <sys/mman.h> /* for mmap */
27 #include <sys/param.h>
28 #include <sys/time.h>
29 #include <sys/wait.h> /* for waitpid macros */
30 #include <locale.h> /* for __uselocale */
31 #include <resolv.h> /* for __resp */
33 #include <ldsodefs.h>
34 #include "pthread.h"
35 #include "internals.h"
36 #include "spinlock.h"
37 #include "restart.h"
38 #include "semaphore.h"
40 /* For debugging purposes put the maximum number of threads in a variable. */
41 const int __linuxthreads_pthread_threads_max = PTHREAD_THREADS_MAX;
43 #ifndef THREAD_SELF
44 /* Indicate whether at least one thread has a user-defined stack (if 1),
45 or if all threads have stacks supplied by LinuxThreads (if 0). */
46 int __pthread_nonstandard_stacks;
47 #endif
49 /* Number of active entries in __pthread_handles (used by gdb) */
50 volatile int __pthread_handles_num = 2;
52 /* Whether to use debugger additional actions for thread creation
53 (set to 1 by gdb) */
54 volatile int __pthread_threads_debug;
56 /* Globally enabled events. */
57 volatile td_thr_events_t __pthread_threads_events;
59 /* Pointer to thread descriptor with last event. */
60 volatile pthread_descr __pthread_last_event;
62 static pthread_descr manager_thread;
64 /* Mapping from stack segment to thread descriptor. */
65 /* Stack segment numbers are also indices into the __pthread_handles array. */
66 /* Stack segment number 0 is reserved for the initial thread. */
68 #if FLOATING_STACKS
69 # define thread_segment(seq) NULL
70 #else
71 static inline pthread_descr thread_segment(int seg)
73 return (pthread_descr)(THREAD_STACK_START_ADDRESS - (seg - 1) * STACK_SIZE)
74 - 1;
76 #endif
78 /* Flag set in signal handler to record child termination */
80 static volatile int terminated_children;
82 /* Flag set when the initial thread is blocked on pthread_exit waiting
83 for all other threads to terminate */
85 static int main_thread_exiting;
87 /* Counter used to generate unique thread identifier.
88 Thread identifier is pthread_threads_counter + segment. */
90 static pthread_t pthread_threads_counter;
92 /* Forward declarations */
94 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
95 void * (*start_routine)(void *), void *arg,
96 sigset_t *mask, int father_pid,
97 int report_events,
98 td_thr_events_t *event_maskp);
99 static void pthread_handle_free(pthread_t th_id);
100 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
101 __attribute__ ((noreturn));
102 static void pthread_reap_children(void);
103 static void pthread_kill_all_threads(int sig, int main_thread_also);
104 static void pthread_for_each_thread(void *arg,
105 void (*fn)(void *, pthread_descr));
107 /* The server thread managing requests for thread creation and termination */
110 __attribute__ ((noreturn))
111 __pthread_manager(void *arg)
113 pthread_descr self = manager_thread = arg;
114 int reqfd = __pthread_manager_reader;
115 struct pollfd ufd;
116 sigset_t manager_mask;
117 int n;
118 struct pthread_request request;
120 /* If we have special thread_self processing, initialize it. */
121 #ifdef INIT_THREAD_SELF
122 INIT_THREAD_SELF(self, 1);
123 #endif
124 #if !(USE_TLS && HAVE___THREAD)
125 /* Set the error variable. */
126 self->p_errnop = &self->p_errno;
127 self->p_h_errnop = &self->p_h_errno;
128 #endif
129 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
130 sigfillset(&manager_mask);
131 sigdelset(&manager_mask, __pthread_sig_cancel); /* for thread termination */
132 sigdelset(&manager_mask, SIGTRAP); /* for debugging purposes */
133 if (__pthread_threads_debug && __pthread_sig_debug > 0)
134 sigdelset(&manager_mask, __pthread_sig_debug);
135 sigprocmask(SIG_SETMASK, &manager_mask, NULL);
136 /* Raise our priority to match that of main thread */
137 __pthread_manager_adjust_prio(__pthread_main_thread->p_priority);
138 /* Synchronize debugging of the thread manager */
139 n = TEMP_FAILURE_RETRY(__libc_read(reqfd, (char *)&request,
140 sizeof(request)));
141 ASSERT(n == sizeof(request) && request.req_kind == REQ_DEBUG);
142 ufd.fd = reqfd;
143 ufd.events = POLLIN;
144 /* Enter server loop */
145 while(1) {
146 n = __poll(&ufd, 1, 2000);
148 /* Check for termination of the main thread */
149 if (getppid() == 1) {
150 pthread_kill_all_threads(SIGKILL, 0);
151 _exit(0);
153 /* Check for dead children */
154 if (terminated_children) {
155 terminated_children = 0;
156 pthread_reap_children();
158 /* Read and execute request */
159 if (n == 1 && (ufd.revents & POLLIN)) {
160 n = TEMP_FAILURE_RETRY(__libc_read(reqfd, (char *)&request,
161 sizeof(request)));
162 #ifdef DEBUG
163 if (n < 0) {
164 char d[64];
165 write(STDERR_FILENO, d, snprintf(d, sizeof(d), "*** read err %m\n"));
166 } else if (n != sizeof(request)) {
167 write(STDERR_FILENO, "*** short read in manager\n", 26);
169 #endif
171 switch(request.req_kind) {
172 case REQ_CREATE:
173 request.req_thread->p_retcode =
174 pthread_handle_create((pthread_t *) &request.req_thread->p_retval,
175 request.req_args.create.attr,
176 request.req_args.create.fn,
177 request.req_args.create.arg,
178 &request.req_args.create.mask,
179 request.req_thread->p_pid,
180 request.req_thread->p_report_events,
181 &request.req_thread->p_eventbuf.eventmask);
182 restart(request.req_thread);
183 break;
184 case REQ_FREE:
185 pthread_handle_free(request.req_args.free.thread_id);
186 break;
187 case REQ_PROCESS_EXIT:
188 pthread_handle_exit(request.req_thread,
189 request.req_args.exit.code);
190 /* NOTREACHED */
191 break;
192 case REQ_MAIN_THREAD_EXIT:
193 main_thread_exiting = 1;
194 /* Reap children in case all other threads died and the signal handler
195 went off before we set main_thread_exiting to 1, and therefore did
196 not do REQ_KICK. */
197 pthread_reap_children();
199 if (__pthread_main_thread->p_nextlive == __pthread_main_thread) {
200 restart(__pthread_main_thread);
201 /* The main thread will now call exit() which will trigger an
202 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
203 to the thread manager. In case you are wondering how the
204 manager terminates from its loop here. */
206 break;
207 case REQ_POST:
208 __new_sem_post(request.req_args.post);
209 break;
210 case REQ_DEBUG:
211 /* Make gdb aware of new thread and gdb will restart the
212 new thread when it is ready to handle the new thread. */
213 if (__pthread_threads_debug && __pthread_sig_debug > 0)
214 raise(__pthread_sig_debug);
215 break;
216 case REQ_KICK:
217 /* This is just a prod to get the manager to reap some
218 threads right away, avoiding a potential delay at shutdown. */
219 break;
220 case REQ_FOR_EACH_THREAD:
221 pthread_for_each_thread(request.req_args.for_each.arg,
222 request.req_args.for_each.fn);
223 restart(request.req_thread);
224 break;
230 int __pthread_manager_event(void *arg)
232 pthread_descr self = arg;
233 /* If we have special thread_self processing, initialize it. */
234 #ifdef INIT_THREAD_SELF
235 INIT_THREAD_SELF(self, 1);
236 #endif
238 /* Get the lock the manager will free once all is correctly set up. */
239 __pthread_lock (THREAD_GETMEM(self, p_lock), NULL);
240 /* Free it immediately. */
241 __pthread_unlock (THREAD_GETMEM(self, p_lock));
243 return __pthread_manager(arg);
246 /* Process creation */
248 static int
249 __attribute__ ((noreturn))
250 pthread_start_thread(void *arg)
252 pthread_descr self = (pthread_descr) arg;
253 struct pthread_request request;
254 void * outcome;
255 #if HP_TIMING_AVAIL
256 hp_timing_t tmpclock;
257 #endif
258 /* Initialize special thread_self processing, if any. */
259 #ifdef INIT_THREAD_SELF
260 INIT_THREAD_SELF(self, self->p_nr);
261 #endif
262 #if HP_TIMING_AVAIL
263 HP_TIMING_NOW (tmpclock);
264 THREAD_SETMEM (self, p_cpuclock_offset, tmpclock);
265 #endif
266 /* Make sure our pid field is initialized, just in case we get there
267 before our father has initialized it. */
268 THREAD_SETMEM(self, p_pid, __getpid());
269 /* Initial signal mask is that of the creating thread. (Otherwise,
270 we'd just inherit the mask of the thread manager.) */
271 sigprocmask(SIG_SETMASK, &self->p_start_args.mask, NULL);
272 /* Set the scheduling policy and priority for the new thread, if needed */
273 if (THREAD_GETMEM(self, p_start_args.schedpolicy) >= 0)
274 /* Explicit scheduling attributes were provided: apply them */
275 __sched_setscheduler(THREAD_GETMEM(self, p_pid),
276 THREAD_GETMEM(self, p_start_args.schedpolicy),
277 &self->p_start_args.schedparam);
278 else if (manager_thread->p_priority > 0)
279 /* Default scheduling required, but thread manager runs in realtime
280 scheduling: switch new thread to SCHED_OTHER policy */
282 struct sched_param default_params;
283 default_params.sched_priority = 0;
284 __sched_setscheduler(THREAD_GETMEM(self, p_pid),
285 SCHED_OTHER, &default_params);
287 #if !(USE_TLS && HAVE___THREAD)
288 /* Initialize thread-locale current locale to point to the global one.
289 With __thread support, the variable's initializer takes care of this. */
290 __uselocale (LC_GLOBAL_LOCALE);
291 #else
292 /* Initialize __resp. */
293 __resp = &self->p_res;
294 #endif
295 /* Make gdb aware of new thread */
296 if (__pthread_threads_debug && __pthread_sig_debug > 0) {
297 request.req_thread = self;
298 request.req_kind = REQ_DEBUG;
299 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
300 (char *) &request, sizeof(request)));
301 suspend(self);
303 /* Run the thread code */
304 outcome = self->p_start_args.start_routine(THREAD_GETMEM(self,
305 p_start_args.arg));
306 /* Exit with the given return value */
307 __pthread_do_exit(outcome, CURRENT_STACK_FRAME);
310 static int
311 __attribute__ ((noreturn))
312 pthread_start_thread_event(void *arg)
314 pthread_descr self = (pthread_descr) arg;
316 #ifdef INIT_THREAD_SELF
317 INIT_THREAD_SELF(self, self->p_nr);
318 #endif
319 /* Make sure our pid field is initialized, just in case we get there
320 before our father has initialized it. */
321 THREAD_SETMEM(self, p_pid, __getpid());
322 /* Get the lock the manager will free once all is correctly set up. */
323 __pthread_lock (THREAD_GETMEM(self, p_lock), NULL);
324 /* Free it immediately. */
325 __pthread_unlock (THREAD_GETMEM(self, p_lock));
327 /* Continue with the real function. */
328 pthread_start_thread (arg);
331 #if defined USE_TLS && !FLOATING_STACKS
332 # error "TLS can only work with floating stacks"
333 #endif
335 static int pthread_allocate_stack(const pthread_attr_t *attr,
336 pthread_descr default_new_thread,
337 int pagesize,
338 char ** out_new_thread,
339 char ** out_new_thread_bottom,
340 char ** out_guardaddr,
341 size_t * out_guardsize,
342 size_t * out_stacksize)
344 pthread_descr new_thread;
345 char * new_thread_bottom;
346 char * guardaddr;
347 size_t stacksize, guardsize;
349 #ifdef USE_TLS
350 /* TLS cannot work with fixed thread descriptor addresses. */
351 assert (default_new_thread == NULL);
352 #endif
354 if (attr != NULL && attr->__stackaddr_set)
356 #ifdef _STACK_GROWS_UP
357 /* The user provided a stack. */
358 # ifdef USE_TLS
359 /* This value is not needed. */
360 new_thread = (pthread_descr) attr->__stackaddr;
361 new_thread_bottom = (char *) new_thread;
362 # else
363 new_thread = (pthread_descr) attr->__stackaddr;
364 new_thread_bottom = (char *) (new_thread + 1);
365 # endif
366 guardaddr = attr->__stackaddr + attr->__stacksize;
367 guardsize = 0;
368 #else
369 /* The user provided a stack. For now we interpret the supplied
370 address as 1 + the highest addr. in the stack segment. If a
371 separate register stack is needed, we place it at the low end
372 of the segment, relying on the associated stacksize to
373 determine the low end of the segment. This differs from many
374 (but not all) other pthreads implementations. The intent is
375 that on machines with a single stack growing toward higher
376 addresses, stackaddr would be the lowest address in the stack
377 segment, so that it is consistently close to the initial sp
378 value. */
379 # ifdef USE_TLS
380 new_thread = (pthread_descr) attr->__stackaddr;
381 # else
382 new_thread =
383 (pthread_descr) ((long)(attr->__stackaddr) & -sizeof(void *)) - 1;
384 # endif
385 new_thread_bottom = (char *) attr->__stackaddr - attr->__stacksize;
386 guardaddr = new_thread_bottom;
387 guardsize = 0;
388 #endif
389 #ifndef THREAD_SELF
390 __pthread_nonstandard_stacks = 1;
391 #endif
392 #ifndef USE_TLS
393 /* Clear the thread data structure. */
394 memset (new_thread, '\0', sizeof (*new_thread));
395 #endif
396 stacksize = attr->__stacksize;
398 else
400 #ifdef NEED_SEPARATE_REGISTER_STACK
401 const size_t granularity = 2 * pagesize;
402 /* Try to make stacksize/2 a multiple of pagesize */
403 #else
404 const size_t granularity = pagesize;
405 #endif
406 void *map_addr;
408 /* Allocate space for stack and thread descriptor at default address */
409 #if FLOATING_STACKS
410 if (attr != NULL)
412 guardsize = page_roundup (attr->__guardsize, granularity);
413 stacksize = __pthread_max_stacksize - guardsize;
414 stacksize = MIN (stacksize,
415 page_roundup (attr->__stacksize, granularity));
417 else
419 guardsize = granularity;
420 stacksize = __pthread_max_stacksize - guardsize;
423 map_addr = mmap(NULL, stacksize + guardsize,
424 PROT_READ | PROT_WRITE | PROT_EXEC,
425 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
426 if (map_addr == MAP_FAILED)
427 /* No more memory available. */
428 return -1;
430 # ifdef NEED_SEPARATE_REGISTER_STACK
431 guardaddr = map_addr + stacksize / 2;
432 if (guardsize > 0)
433 mprotect (guardaddr, guardsize, PROT_NONE);
435 new_thread_bottom = (char *) map_addr;
436 # ifdef USE_TLS
437 new_thread = ((pthread_descr) (new_thread_bottom + stacksize
438 + guardsize));
439 # else
440 new_thread = ((pthread_descr) (new_thread_bottom + stacksize
441 + guardsize)) - 1;
442 # endif
443 # elif _STACK_GROWS_DOWN
444 guardaddr = map_addr;
445 if (guardsize > 0)
446 mprotect (guardaddr, guardsize, PROT_NONE);
448 new_thread_bottom = (char *) map_addr + guardsize;
449 # ifdef USE_TLS
450 new_thread = ((pthread_descr) (new_thread_bottom + stacksize));
451 # else
452 new_thread = ((pthread_descr) (new_thread_bottom + stacksize)) - 1;
453 # endif
454 # elif _STACK_GROWS_UP
455 guardaddr = map_addr + stacksize;
456 if (guardsize > 0)
457 mprotect (guardaddr, guardsize, PROT_NONE);
459 new_thread = (pthread_descr) map_addr;
460 # ifdef USE_TLS
461 new_thread_bottom = (char *) new_thread;
462 # else
463 new_thread_bottom = (char *) (new_thread + 1);
464 # endif
465 # else
466 # error You must define a stack direction
467 # endif /* Stack direction */
468 #else /* !FLOATING_STACKS */
469 void *res_addr;
471 if (attr != NULL)
473 guardsize = page_roundup (attr->__guardsize, granularity);
474 stacksize = STACK_SIZE - guardsize;
475 stacksize = MIN (stacksize,
476 page_roundup (attr->__stacksize, granularity));
478 else
480 guardsize = granularity;
481 stacksize = STACK_SIZE - granularity;
484 # ifdef NEED_SEPARATE_REGISTER_STACK
485 new_thread = default_new_thread;
486 new_thread_bottom = (char *) (new_thread + 1) - stacksize - guardsize;
487 /* Includes guard area, unlike the normal case. Use the bottom
488 end of the segment as backing store for the register stack.
489 Needed on IA64. In this case, we also map the entire stack at
490 once. According to David Mosberger, that's cheaper. It also
491 avoids the risk of intermittent failures due to other mappings
492 in the same region. The cost is that we might be able to map
493 slightly fewer stacks. */
495 /* First the main stack: */
496 map_addr = (caddr_t)((char *)(new_thread + 1) - stacksize / 2);
497 res_addr = mmap(map_addr, stacksize / 2,
498 PROT_READ | PROT_WRITE | PROT_EXEC,
499 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
500 if (res_addr != map_addr)
502 /* Bad luck, this segment is already mapped. */
503 if (res_addr != MAP_FAILED)
504 munmap(res_addr, stacksize / 2);
505 return -1;
507 /* Then the register stack: */
508 map_addr = (caddr_t)new_thread_bottom;
509 res_addr = mmap(map_addr, stacksize/2,
510 PROT_READ | PROT_WRITE | PROT_EXEC,
511 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
512 if (res_addr != map_addr)
514 if (res_addr != MAP_FAILED)
515 munmap(res_addr, stacksize / 2);
516 munmap((caddr_t)((char *)(new_thread + 1) - stacksize/2),
517 stacksize/2);
518 return -1;
521 guardaddr = new_thread_bottom + stacksize/2;
522 /* We leave the guard area in the middle unmapped. */
523 # else /* !NEED_SEPARATE_REGISTER_STACK */
524 # ifdef _STACK_GROWS_DOWN
525 new_thread = default_new_thread;
526 new_thread_bottom = (char *) (new_thread + 1) - stacksize;
527 map_addr = new_thread_bottom - guardsize;
528 res_addr = mmap(map_addr, stacksize + guardsize,
529 PROT_READ | PROT_WRITE | PROT_EXEC,
530 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
531 if (res_addr != map_addr)
533 /* Bad luck, this segment is already mapped. */
534 if (res_addr != MAP_FAILED)
535 munmap (res_addr, stacksize + guardsize);
536 return -1;
539 /* We manage to get a stack. Protect the guard area pages if
540 necessary. */
541 guardaddr = map_addr;
542 if (guardsize > 0)
543 mprotect (guardaddr, guardsize, PROT_NONE);
544 # else
545 /* The thread description goes at the bottom of this area, and
546 * the stack starts directly above it.
548 new_thread = (pthread_descr)((unsigned long)default_new_thread &~ (STACK_SIZE - 1));
549 map_addr = mmap(new_thread, stacksize + guardsize,
550 PROT_READ | PROT_WRITE | PROT_EXEC,
551 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
552 if (map_addr == MAP_FAILED)
553 return -1;
555 new_thread_bottom = map_addr + sizeof(*new_thread);
556 guardaddr = map_addr + stacksize;
557 if (guardsize > 0)
558 mprotect (guardaddr, guardsize, PROT_NONE);
560 # endif /* stack direction */
561 # endif /* !NEED_SEPARATE_REGISTER_STACK */
562 #endif /* !FLOATING_STACKS */
564 *out_new_thread = (char *) new_thread;
565 *out_new_thread_bottom = new_thread_bottom;
566 *out_guardaddr = guardaddr;
567 *out_guardsize = guardsize;
568 #ifdef NEED_SEPARATE_REGISTER_STACK
569 *out_stacksize = stacksize / 2;
570 #else
571 *out_stacksize = stacksize;
572 #endif
573 return 0;
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,
579 int report_events,
580 td_thr_events_t *event_maskp)
582 size_t sseg;
583 int pid;
584 pthread_descr new_thread;
585 char *stack_addr;
586 char * new_thread_bottom;
587 pthread_t new_thread_id;
588 char *guardaddr = NULL;
589 size_t guardsize = 0, stksize = 0;
590 int pagesize = __getpagesize();
591 int saved_errno = 0;
593 #ifdef USE_TLS
594 new_thread = _dl_allocate_tls (NULL);
595 if (new_thread == NULL)
596 return EAGAIN;
597 # if TLS_DTV_AT_TP
598 /* pthread_descr is below TP. */
599 new_thread = (pthread_descr) ((char *) new_thread - TLS_PRE_TCB_SIZE);
600 # endif
601 #else
602 /* Prevent warnings. */
603 new_thread = NULL;
604 #endif
606 /* First check whether we have to change the policy and if yes, whether
607 we can do this. Normally this should be done by examining the
608 return value of the __sched_setscheduler call in pthread_start_thread
609 but this is hard to implement. FIXME */
610 if (attr != NULL && attr->__schedpolicy != SCHED_OTHER && geteuid () != 0)
611 return EPERM;
612 /* Find a free segment for the thread, and allocate a stack if needed */
613 for (sseg = 2; ; sseg++)
615 if (sseg >= PTHREAD_THREADS_MAX)
617 #ifdef USE_TLS
618 # if TLS_DTV_AT_TP
619 new_thread = (pthread_descr) ((char *) new_thread + TLS_PRE_TCB_SIZE);
620 # endif
621 _dl_deallocate_tls (new_thread, true);
622 #endif
623 return EAGAIN;
625 if (__pthread_handles[sseg].h_descr != NULL)
626 continue;
627 if (pthread_allocate_stack(attr, thread_segment(sseg),
628 pagesize, &stack_addr, &new_thread_bottom,
629 &guardaddr, &guardsize, &stksize) == 0)
631 #ifdef USE_TLS
632 new_thread->p_stackaddr = stack_addr;
633 #else
634 new_thread = (pthread_descr) stack_addr;
635 #endif
636 break;
639 __pthread_handles_num++;
640 /* Allocate new thread identifier */
641 pthread_threads_counter += PTHREAD_THREADS_MAX;
642 new_thread_id = sseg + pthread_threads_counter;
643 /* Initialize the thread descriptor. Elements which have to be
644 initialized to zero already have this value. */
645 #if !defined USE_TLS || !TLS_DTV_AT_TP
646 new_thread->p_header.data.tcb = new_thread;
647 new_thread->p_header.data.self = new_thread;
648 #endif
649 #if TLS_MULTIPLE_THREADS_IN_TCB || !defined USE_TLS || !TLS_DTV_AT_TP
650 new_thread->p_multiple_threads = 1;
651 #endif
652 new_thread->p_tid = new_thread_id;
653 new_thread->p_lock = &(__pthread_handles[sseg].h_lock);
654 new_thread->p_cancelstate = PTHREAD_CANCEL_ENABLE;
655 new_thread->p_canceltype = PTHREAD_CANCEL_DEFERRED;
656 #if !(USE_TLS && HAVE___THREAD)
657 new_thread->p_errnop = &new_thread->p_errno;
658 new_thread->p_h_errnop = &new_thread->p_h_errno;
659 new_thread->p_resp = &new_thread->p_res;
660 #endif
661 new_thread->p_guardaddr = guardaddr;
662 new_thread->p_guardsize = guardsize;
663 new_thread->p_nr = sseg;
664 new_thread->p_inheritsched = attr ? attr->__inheritsched : 0;
665 new_thread->p_alloca_cutoff = stksize / 4 > __MAX_ALLOCA_CUTOFF
666 ? __MAX_ALLOCA_CUTOFF : stksize / 4;
667 /* Initialize the thread handle */
668 __pthread_init_lock(&__pthread_handles[sseg].h_lock);
669 __pthread_handles[sseg].h_descr = new_thread;
670 __pthread_handles[sseg].h_bottom = new_thread_bottom;
671 /* Determine scheduling parameters for the thread */
672 new_thread->p_start_args.schedpolicy = -1;
673 if (attr != NULL) {
674 new_thread->p_detached = attr->__detachstate;
675 new_thread->p_userstack = attr->__stackaddr_set;
677 switch(attr->__inheritsched) {
678 case PTHREAD_EXPLICIT_SCHED:
679 new_thread->p_start_args.schedpolicy = attr->__schedpolicy;
680 memcpy (&new_thread->p_start_args.schedparam, &attr->__schedparam,
681 sizeof (struct sched_param));
682 break;
683 case PTHREAD_INHERIT_SCHED:
684 new_thread->p_start_args.schedpolicy = __sched_getscheduler(father_pid);
685 __sched_getparam(father_pid, &new_thread->p_start_args.schedparam);
686 break;
688 new_thread->p_priority =
689 new_thread->p_start_args.schedparam.sched_priority;
691 /* Finish setting up arguments to pthread_start_thread */
692 new_thread->p_start_args.start_routine = start_routine;
693 new_thread->p_start_args.arg = arg;
694 new_thread->p_start_args.mask = *mask;
695 /* Make the new thread ID available already now. If any of the later
696 functions fail we return an error value and the caller must not use
697 the stored thread ID. */
698 *thread = new_thread_id;
699 /* Raise priority of thread manager if needed */
700 __pthread_manager_adjust_prio(new_thread->p_priority);
701 /* Do the cloning. We have to use two different functions depending
702 on whether we are debugging or not. */
703 pid = 0; /* Note that the thread never can have PID zero. */
704 if (report_events)
706 /* See whether the TD_CREATE event bit is set in any of the
707 masks. */
708 int idx = __td_eventword (TD_CREATE);
709 uint32_t mask = __td_eventmask (TD_CREATE);
711 if ((mask & (__pthread_threads_events.event_bits[idx]
712 | event_maskp->event_bits[idx])) != 0)
714 /* Lock the mutex the child will use now so that it will stop. */
715 __pthread_lock(new_thread->p_lock, NULL);
717 /* We have to report this event. */
718 #ifdef NEED_SEPARATE_REGISTER_STACK
719 /* Perhaps this version should be used on all platforms. But
720 this requires that __clone2 be uniformly supported
721 everywhere.
723 And there is some argument for changing the __clone2
724 interface to pass sp and bsp instead, making it more IA64
725 specific, but allowing stacks to grow outward from each
726 other, to get less paging and fewer mmaps. */
727 pid = __clone2(pthread_start_thread_event,
728 (void **)new_thread_bottom,
729 (char *)stack_addr - new_thread_bottom,
730 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
731 __pthread_sig_cancel, new_thread);
732 #elif _STACK_GROWS_UP
733 pid = __clone(pthread_start_thread_event, (void *) new_thread_bottom,
734 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
735 __pthread_sig_cancel, new_thread);
736 #else
737 pid = __clone(pthread_start_thread_event, stack_addr,
738 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
739 __pthread_sig_cancel, new_thread);
740 #endif
741 saved_errno = errno;
742 if (pid != -1)
744 /* Now fill in the information about the new thread in
745 the newly created thread's data structure. We cannot let
746 the new thread do this since we don't know whether it was
747 already scheduled when we send the event. */
748 new_thread->p_eventbuf.eventdata = new_thread;
749 new_thread->p_eventbuf.eventnum = TD_CREATE;
750 __pthread_last_event = new_thread;
752 /* We have to set the PID here since the callback function
753 in the debug library will need it and we cannot guarantee
754 the child got scheduled before the debugger. */
755 new_thread->p_pid = pid;
757 /* Now call the function which signals the event. */
758 __linuxthreads_create_event ();
760 /* Now restart the thread. */
761 __pthread_unlock(new_thread->p_lock);
765 if (pid == 0)
767 #ifdef NEED_SEPARATE_REGISTER_STACK
768 pid = __clone2(pthread_start_thread,
769 (void **)new_thread_bottom,
770 (char *)stack_addr - new_thread_bottom,
771 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
772 __pthread_sig_cancel, new_thread);
773 #elif _STACK_GROWS_UP
774 pid = __clone(pthread_start_thread, (void *) new_thread_bottom,
775 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
776 __pthread_sig_cancel, new_thread);
777 #else
778 pid = __clone(pthread_start_thread, stack_addr,
779 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
780 __pthread_sig_cancel, new_thread);
781 #endif /* !NEED_SEPARATE_REGISTER_STACK */
782 saved_errno = errno;
784 /* Check if cloning succeeded */
785 if (pid == -1) {
786 /* Free the stack if we allocated it */
787 if (attr == NULL || !attr->__stackaddr_set)
789 #ifdef NEED_SEPARATE_REGISTER_STACK
790 size_t stacksize = ((char *)(new_thread->p_guardaddr)
791 - new_thread_bottom);
792 munmap((caddr_t)new_thread_bottom,
793 2 * stacksize + new_thread->p_guardsize);
794 #elif _STACK_GROWS_UP
795 # ifdef USE_TLS
796 size_t stacksize = guardaddr - stack_addr;
797 munmap(stack_addr, stacksize + guardsize);
798 # else
799 size_t stacksize = guardaddr - (char *)new_thread;
800 munmap(new_thread, stacksize + guardsize);
801 # endif
802 #else
803 # ifdef USE_TLS
804 size_t stacksize = stack_addr - new_thread_bottom;
805 # else
806 size_t stacksize = (char *)(new_thread+1) - new_thread_bottom;
807 # endif
808 munmap(new_thread_bottom - guardsize, guardsize + stacksize);
809 #endif
811 #ifdef USE_TLS
812 # if TLS_DTV_AT_TP
813 new_thread = (pthread_descr) ((char *) new_thread + TLS_PRE_TCB_SIZE);
814 # endif
815 _dl_deallocate_tls (new_thread, true);
816 #endif
817 __pthread_handles[sseg].h_descr = NULL;
818 __pthread_handles[sseg].h_bottom = NULL;
819 __pthread_handles_num--;
820 return saved_errno;
822 /* Insert new thread in doubly linked list of active threads */
823 new_thread->p_prevlive = __pthread_main_thread;
824 new_thread->p_nextlive = __pthread_main_thread->p_nextlive;
825 __pthread_main_thread->p_nextlive->p_prevlive = new_thread;
826 __pthread_main_thread->p_nextlive = new_thread;
827 /* Set pid field of the new thread, in case we get there before the
828 child starts. */
829 new_thread->p_pid = pid;
830 return 0;
834 /* Try to free the resources of a thread when requested by pthread_join
835 or pthread_detach on a terminated thread. */
837 static void pthread_free(pthread_descr th)
839 pthread_handle handle;
840 pthread_readlock_info *iter, *next;
842 ASSERT(th->p_exited);
843 /* Make the handle invalid */
844 handle = thread_handle(th->p_tid);
845 __pthread_lock(&handle->h_lock, NULL);
846 handle->h_descr = NULL;
847 handle->h_bottom = (char *)(-1L);
848 __pthread_unlock(&handle->h_lock);
849 #ifdef FREE_THREAD
850 FREE_THREAD(th, th->p_nr);
851 #endif
852 /* One fewer threads in __pthread_handles */
853 __pthread_handles_num--;
855 /* Destroy read lock list, and list of free read lock structures.
856 If the former is not empty, it means the thread exited while
857 holding read locks! */
859 for (iter = th->p_readlock_list; iter != NULL; iter = next)
861 next = iter->pr_next;
862 free(iter);
865 for (iter = th->p_readlock_free; iter != NULL; iter = next)
867 next = iter->pr_next;
868 free(iter);
871 /* If initial thread, nothing to free */
872 if (!th->p_userstack)
874 size_t guardsize = th->p_guardsize;
875 /* Free the stack and thread descriptor area */
876 char *guardaddr = th->p_guardaddr;
877 #ifdef _STACK_GROWS_UP
878 # ifdef USE_TLS
879 size_t stacksize = guardaddr - th->p_stackaddr;
880 # else
881 size_t stacksize = guardaddr - (char *)th;
882 # endif
883 guardaddr = (char *)th;
884 #else
885 /* Guardaddr is always set, even if guardsize is 0. This allows
886 us to compute everything else. */
887 # ifdef USE_TLS
888 size_t stacksize = th->p_stackaddr - guardaddr - guardsize;
889 # else
890 size_t stacksize = (char *)(th+1) - guardaddr - guardsize;
891 # endif
892 # ifdef NEED_SEPARATE_REGISTER_STACK
893 /* Take account of the register stack, which is below guardaddr. */
894 guardaddr -= stacksize;
895 stacksize *= 2;
896 # endif
897 #endif
898 /* Unmap the stack. */
899 munmap(guardaddr, stacksize + guardsize);
901 #ifdef USE_TLS
902 # if TLS_DTV_AT_TP
903 th = (pthread_descr) ((char *) th + TLS_PRE_TCB_SIZE);
904 # endif
905 _dl_deallocate_tls (th, true);
906 #endif
910 /* Handle threads that have exited */
912 static void pthread_exited(pid_t pid)
914 pthread_descr th;
915 int detached;
916 /* Find thread with that pid */
917 for (th = __pthread_main_thread->p_nextlive;
918 th != __pthread_main_thread;
919 th = th->p_nextlive) {
920 if (th->p_pid == pid) {
921 /* Remove thread from list of active threads */
922 th->p_nextlive->p_prevlive = th->p_prevlive;
923 th->p_prevlive->p_nextlive = th->p_nextlive;
924 /* Mark thread as exited, and if detached, free its resources */
925 __pthread_lock(th->p_lock, NULL);
926 th->p_exited = 1;
927 /* If we have to signal this event do it now. */
928 if (th->p_report_events)
930 /* See whether TD_REAP is in any of the mask. */
931 int idx = __td_eventword (TD_REAP);
932 uint32_t mask = __td_eventmask (TD_REAP);
934 if ((mask & (__pthread_threads_events.event_bits[idx]
935 | th->p_eventbuf.eventmask.event_bits[idx])) != 0)
937 /* Yep, we have to signal the reapage. */
938 th->p_eventbuf.eventnum = TD_REAP;
939 th->p_eventbuf.eventdata = th;
940 __pthread_last_event = th;
942 /* Now call the function to signal the event. */
943 __linuxthreads_reap_event();
946 detached = th->p_detached;
947 __pthread_unlock(th->p_lock);
948 if (detached)
949 pthread_free(th);
950 break;
953 /* If all threads have exited and the main thread is pending on a
954 pthread_exit, wake up the main thread and terminate ourselves. */
955 if (main_thread_exiting &&
956 __pthread_main_thread->p_nextlive == __pthread_main_thread) {
957 restart(__pthread_main_thread);
958 /* Same logic as REQ_MAIN_THREAD_EXIT. */
962 static void pthread_reap_children(void)
964 pid_t pid;
965 int status;
967 while ((pid = __libc_waitpid(-1, &status, WNOHANG | __WCLONE)) > 0) {
968 pthread_exited(pid);
969 if (WIFSIGNALED(status)) {
970 /* If a thread died due to a signal, send the same signal to
971 all other threads, including the main thread. */
972 pthread_kill_all_threads(WTERMSIG(status), 1);
973 _exit(0);
978 /* Try to free the resources of a thread when requested by pthread_join
979 or pthread_detach on a terminated thread. */
981 static void pthread_handle_free(pthread_t th_id)
983 pthread_handle handle = thread_handle(th_id);
984 pthread_descr th;
986 __pthread_lock(&handle->h_lock, NULL);
987 if (nonexisting_handle(handle, th_id)) {
988 /* pthread_reap_children has deallocated the thread already,
989 nothing needs to be done */
990 __pthread_unlock(&handle->h_lock);
991 return;
993 th = handle->h_descr;
994 if (th->p_exited) {
995 __pthread_unlock(&handle->h_lock);
996 pthread_free(th);
997 } else {
998 /* The Unix process of the thread is still running.
999 Mark the thread as detached so that the thread manager will
1000 deallocate its resources when the Unix process exits. */
1001 th->p_detached = 1;
1002 __pthread_unlock(&handle->h_lock);
1006 /* Send a signal to all running threads */
1008 static void pthread_kill_all_threads(int sig, int main_thread_also)
1010 pthread_descr th;
1011 for (th = __pthread_main_thread->p_nextlive;
1012 th != __pthread_main_thread;
1013 th = th->p_nextlive) {
1014 kill(th->p_pid, sig);
1016 if (main_thread_also) {
1017 kill(__pthread_main_thread->p_pid, sig);
1021 static void pthread_for_each_thread(void *arg,
1022 void (*fn)(void *, pthread_descr))
1024 pthread_descr th;
1026 for (th = __pthread_main_thread->p_nextlive;
1027 th != __pthread_main_thread;
1028 th = th->p_nextlive) {
1029 fn(arg, th);
1032 fn(arg, __pthread_main_thread);
1035 /* Process-wide exit() */
1037 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
1039 pthread_descr th;
1040 __pthread_exit_requested = 1;
1041 __pthread_exit_code = exitcode;
1042 /* A forced asynchronous cancellation follows. Make sure we won't
1043 get stuck later in the main thread with a system lock being held
1044 by one of the cancelled threads. Ideally one would use the same
1045 code as in pthread_atfork(), but we can't distinguish system and
1046 user handlers there. */
1047 __flockfilelist();
1048 /* Send the CANCEL signal to all running threads, including the main
1049 thread, but excluding the thread from which the exit request originated
1050 (that thread must complete the exit, e.g. calling atexit functions
1051 and flushing stdio buffers). */
1052 for (th = issuing_thread->p_nextlive;
1053 th != issuing_thread;
1054 th = th->p_nextlive) {
1055 kill(th->p_pid, __pthread_sig_cancel);
1057 /* Now, wait for all these threads, so that they don't become zombies
1058 and their times are properly added to the thread manager's times. */
1059 for (th = issuing_thread->p_nextlive;
1060 th != issuing_thread;
1061 th = th->p_nextlive) {
1062 waitpid(th->p_pid, NULL, __WCLONE);
1064 __fresetlockfiles();
1065 restart(issuing_thread);
1066 _exit(0);
1069 /* Handler for __pthread_sig_cancel in thread manager thread */
1071 void __pthread_manager_sighandler(int sig)
1073 int kick_manager = terminated_children == 0 && main_thread_exiting;
1074 terminated_children = 1;
1076 /* If the main thread is terminating, kick the thread manager loop
1077 each time some threads terminate. This eliminates a two second
1078 shutdown delay caused by the thread manager sleeping in the
1079 call to __poll(). Instead, the thread manager is kicked into
1080 action, reaps the outstanding threads and resumes the main thread
1081 so that it can complete the shutdown. */
1083 if (kick_manager) {
1084 struct pthread_request request;
1085 request.req_thread = 0;
1086 request.req_kind = REQ_KICK;
1087 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
1088 (char *) &request, sizeof(request)));
1092 /* Adjust priority of thread manager so that it always run at a priority
1093 higher than all threads */
1095 void __pthread_manager_adjust_prio(int thread_prio)
1097 struct sched_param param;
1099 if (thread_prio <= manager_thread->p_priority) return;
1100 param.sched_priority =
1101 thread_prio < __sched_get_priority_max(SCHED_FIFO)
1102 ? thread_prio + 1 : thread_prio;
1103 __sched_setscheduler(manager_thread->p_pid, SCHED_FIFO, &param);
1104 manager_thread->p_priority = thread_prio;