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