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