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