ARC defconfigs: enable some items
[uclibc-ng.git] / libpthread / linuxthreads.old / manager.c
blobe4022f8eae370a449f918a085e8143a3258a16a7
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 <features.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 */
31 #include "pthread.h"
32 #include "internals.h"
33 #include "spinlock.h"
34 #include "restart.h"
35 #include "semaphore.h"
36 #include "debug.h" /* PDEBUG, added by StS */
38 #ifndef THREAD_STACK_OFFSET
39 #define THREAD_STACK_OFFSET 0
40 #endif
42 /* poll() is not supported in kernel <= 2.0, therefore is __NR_poll is
43 * not available, we assume an old Linux kernel is in use and we will
44 * use select() instead. */
45 #include <sys/syscall.h>
46 #ifndef __NR_poll
47 # define USE_SELECT
48 #endif
50 libpthread_hidden_proto(waitpid)
51 libpthread_hidden_proto(raise)
53 /* Array of active threads. Entry 0 is reserved for the initial thread. */
54 struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX] =
55 { { __LOCK_INITIALIZER, &__pthread_initial_thread, 0},
56 { __LOCK_INITIALIZER, &__pthread_manager_thread, 0}, /* All NULLs */ };
58 /* For debugging purposes put the maximum number of threads in a variable. */
59 const int __linuxthreads_pthread_threads_max = PTHREAD_THREADS_MAX;
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;
65 /* Number of active entries in __pthread_handles (used by gdb) */
66 volatile int __pthread_handles_num = 2;
68 /* Whether to use debugger additional actions for thread creation
69 (set to 1 by gdb) */
70 volatile int __pthread_threads_debug;
72 /* Globally enabled events. */
73 volatile td_thr_events_t __pthread_threads_events;
75 /* Pointer to thread descriptor with last event. */
76 volatile pthread_descr __pthread_last_event;
78 /* Mapping from stack segment to thread descriptor. */
79 /* Stack segment numbers are also indices into the __pthread_handles array. */
80 /* Stack segment number 0 is reserved for the initial thread. */
82 static __inline__ pthread_descr thread_segment(int seg)
84 return (pthread_descr)(THREAD_STACK_START_ADDRESS - (seg - 1) * STACK_SIZE)
85 - 1;
88 /* Flag set in signal handler to record child termination */
90 static volatile int terminated_children = 0;
92 /* Flag set when the initial thread is blocked on pthread_exit waiting
93 for all other threads to terminate */
95 static int main_thread_exiting = 0;
97 /* Counter used to generate unique thread identifier.
98 Thread identifier is pthread_threads_counter + segment. */
100 static pthread_t pthread_threads_counter = 0;
102 /* Forward declarations */
104 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
105 void * (*start_routine)(void *), void *arg,
106 sigset_t *mask, int father_pid,
107 int report_events,
108 td_thr_events_t *event_maskp);
109 static void pthread_handle_free(pthread_t th_id);
110 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode) attribute_noreturn;
111 static void pthread_reap_children(void);
112 static void pthread_kill_all_threads(int sig, int main_thread_also);
114 /* The server thread managing requests for thread creation and termination */
116 int attribute_noreturn __pthread_manager(void *arg)
118 int reqfd = (int) (long int) arg;
119 #ifdef USE_SELECT
120 struct timeval tv;
121 fd_set fd;
122 #else
123 struct pollfd ufd;
124 #endif
125 sigset_t manager_mask;
126 int n;
127 struct pthread_request request;
129 /* If we have special thread_self processing, initialize it. */
130 #ifdef INIT_THREAD_SELF
131 INIT_THREAD_SELF(&__pthread_manager_thread, 1);
132 #endif
133 /* Set the error variable. */
134 __pthread_manager_thread.p_errnop = &__pthread_manager_thread.p_errno;
135 __pthread_manager_thread.p_h_errnop = &__pthread_manager_thread.p_h_errno;
137 #ifdef __UCLIBC_HAS_XLOCALE__
138 /* Initialize thread's locale to the global locale. */
139 __pthread_manager_thread.locale = __global_locale;
140 #endif /* __UCLIBC_HAS_XLOCALE__ */
142 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
143 __sigfillset(&manager_mask);
144 sigdelset(&manager_mask, __pthread_sig_cancel); /* for thread termination */
145 sigdelset(&manager_mask, SIGTRAP); /* for debugging purposes */
146 if (__pthread_threads_debug && __pthread_sig_debug > 0)
147 sigdelset(&manager_mask, __pthread_sig_debug);
148 sigprocmask(SIG_SETMASK, &manager_mask, NULL);
149 /* Raise our priority to match that of main thread */
150 __pthread_manager_adjust_prio(__pthread_main_thread->p_priority);
151 /* Synchronize debugging of the thread manager */
152 n = TEMP_FAILURE_RETRY(read(reqfd, (char *)&request,
153 sizeof(request)));
154 #ifndef USE_SELECT
155 ufd.fd = reqfd;
156 ufd.events = POLLIN;
157 #endif
158 /* Enter server loop */
159 while(1) {
160 #ifdef USE_SELECT
161 tv.tv_sec = 2;
162 tv.tv_usec = 0;
163 FD_ZERO (&fd);
164 FD_SET (reqfd, &fd);
165 n = select (reqfd + 1, &fd, NULL, NULL, &tv);
166 #else
167 PDEBUG("before poll\n");
168 n = poll(&ufd, 1, 2000);
169 PDEBUG("after poll\n");
170 #endif
171 /* Check for termination of the main thread */
172 if (getppid() == 1) {
173 pthread_kill_all_threads(SIGKILL, 0);
174 _exit(0);
176 /* Check for dead children */
177 if (terminated_children) {
178 terminated_children = 0;
179 pthread_reap_children();
181 /* Read and execute request */
182 #ifdef USE_SELECT
183 if (n == 1)
184 #else
185 if (n == 1 && (ufd.revents & POLLIN))
186 #endif
189 PDEBUG("before read\n");
190 n = read(reqfd, (char *)&request, sizeof(request));
191 PDEBUG("after read, n=%d\n", n);
192 switch(request.req_kind) {
193 case REQ_CREATE:
194 PDEBUG("got REQ_CREATE\n");
195 request.req_thread->p_retcode =
196 pthread_handle_create((pthread_t *) &request.req_thread->p_retval,
197 request.req_args.create.attr,
198 request.req_args.create.fn,
199 request.req_args.create.arg,
200 &request.req_args.create.mask,
201 request.req_thread->p_pid,
202 request.req_thread->p_report_events,
203 &request.req_thread->p_eventbuf.eventmask);
204 PDEBUG("restarting %p\n", request.req_thread);
205 restart(request.req_thread);
206 break;
207 case REQ_FREE:
208 PDEBUG("got REQ_FREE\n");
209 pthread_handle_free(request.req_args.free.thread_id);
210 break;
211 case REQ_PROCESS_EXIT:
212 PDEBUG("got REQ_PROCESS_EXIT from %p, exit code = %d\n",
213 request.req_thread, request.req_args.exit.code);
214 pthread_handle_exit(request.req_thread,
215 request.req_args.exit.code);
216 break;
217 case REQ_MAIN_THREAD_EXIT:
218 PDEBUG("got REQ_MAIN_THREAD_EXIT\n");
219 main_thread_exiting = 1;
220 /* Reap children in case all other threads died and the signal handler
221 went off before we set main_thread_exiting to 1, and therefore did
222 not do REQ_KICK. */
223 pthread_reap_children();
225 if (__pthread_main_thread->p_nextlive == __pthread_main_thread) {
226 restart(__pthread_main_thread);
227 /* The main thread will now call exit() which will trigger an
228 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
229 to the thread manager. In case you are wondering how the
230 manager terminates from its loop here. */
232 break;
233 case REQ_POST:
234 PDEBUG("got REQ_POST\n");
235 sem_post(request.req_args.post);
236 break;
237 case REQ_DEBUG:
238 PDEBUG("got REQ_DEBUG\n");
239 /* Make gdb aware of new thread and gdb will restart the
240 new thread when it is ready to handle the new thread. */
241 if (__pthread_threads_debug && __pthread_sig_debug > 0) {
242 PDEBUG("about to call raise(__pthread_sig_debug)\n");
243 raise(__pthread_sig_debug);
245 case REQ_KICK:
246 /* This is just a prod to get the manager to reap some
247 threads right away, avoiding a potential delay at shutdown. */
248 break;
254 int attribute_noreturn __pthread_manager_event(void *arg)
256 /* If we have special thread_self processing, initialize it. */
257 #ifdef INIT_THREAD_SELF
258 INIT_THREAD_SELF(&__pthread_manager_thread, 1);
259 #endif
261 /* Get the lock the manager will free once all is correctly set up. */
262 __pthread_lock (THREAD_GETMEM((&__pthread_manager_thread), p_lock), NULL);
263 /* Free it immediately. */
264 __pthread_unlock (THREAD_GETMEM((&__pthread_manager_thread), p_lock));
266 __pthread_manager(arg);
269 /* Process creation */
270 static int
271 attribute_noreturn
272 pthread_start_thread(void *arg)
274 pthread_descr self = (pthread_descr) arg;
275 struct pthread_request request;
276 void * outcome;
277 /* Initialize special thread_self processing, if any. */
278 #ifdef INIT_THREAD_SELF
279 INIT_THREAD_SELF(self, self->p_nr);
280 #endif
281 PDEBUG("\n");
282 /* Make sure our pid field is initialized, just in case we get there
283 before our father has initialized it. */
284 THREAD_SETMEM(self, p_pid, getpid());
285 /* Initial signal mask is that of the creating thread. (Otherwise,
286 we'd just inherit the mask of the thread manager.) */
287 sigprocmask(SIG_SETMASK, &self->p_start_args.mask, NULL);
288 /* Set the scheduling policy and priority for the new thread, if needed */
289 if (THREAD_GETMEM(self, p_start_args.schedpolicy) >= 0)
290 /* Explicit scheduling attributes were provided: apply them */
291 sched_setscheduler(THREAD_GETMEM(self, p_pid),
292 THREAD_GETMEM(self, p_start_args.schedpolicy),
293 &self->p_start_args.schedparam);
294 else if (__pthread_manager_thread.p_priority > 0)
295 /* Default scheduling required, but thread manager runs in realtime
296 scheduling: switch new thread to SCHED_OTHER policy */
298 struct sched_param default_params;
299 default_params.sched_priority = 0;
300 sched_setscheduler(THREAD_GETMEM(self, p_pid),
301 SCHED_OTHER, &default_params);
303 /* Make gdb aware of new thread */
304 if (__pthread_threads_debug && __pthread_sig_debug > 0) {
305 request.req_thread = self;
306 request.req_kind = REQ_DEBUG;
307 TEMP_FAILURE_RETRY(write(__pthread_manager_request,
308 (char *) &request, sizeof(request)));
309 suspend(self);
311 /* Run the thread code */
312 outcome = self->p_start_args.start_routine(THREAD_GETMEM(self,
313 p_start_args.arg));
314 /* Exit with the given return value */
315 __pthread_do_exit(outcome, CURRENT_STACK_FRAME);
318 static int
319 attribute_noreturn
320 pthread_start_thread_event(void *arg)
322 pthread_descr self = (pthread_descr) arg;
324 #ifdef INIT_THREAD_SELF
325 INIT_THREAD_SELF(self, self->p_nr);
326 #endif
327 /* Make sure our pid field is initialized, just in case we get there
328 before our father has initialized it. */
329 THREAD_SETMEM(self, p_pid, getpid());
330 /* Get the lock the manager will free once all is correctly set up. */
331 __pthread_lock (THREAD_GETMEM(self, p_lock), NULL);
332 /* Free it immediately. */
333 __pthread_unlock (THREAD_GETMEM(self, p_lock));
335 /* Continue with the real function. */
336 pthread_start_thread (arg);
339 static int pthread_allocate_stack(const pthread_attr_t *attr,
340 pthread_descr default_new_thread,
341 int pagesize,
342 pthread_descr * out_new_thread,
343 char ** out_new_thread_bottom,
344 char ** out_guardaddr,
345 size_t * out_guardsize)
347 pthread_descr new_thread;
348 char * new_thread_bottom;
349 char * guardaddr;
350 size_t stacksize, guardsize;
352 if (attr != NULL && attr->__stackaddr_set)
354 /* The user provided a stack. */
355 new_thread = (pthread_descr) ((long)(attr->__stackaddr) & -sizeof(void *)) - 1;
356 new_thread_bottom = (char *) attr->__stackaddr - attr->__stacksize;
357 guardaddr = NULL;
358 guardsize = 0;
359 __pthread_nonstandard_stacks = 1;
360 #ifndef __ARCH_USE_MMU__
361 /* check the initial thread stack boundaries so they don't overlap */
362 NOMMU_INITIAL_THREAD_BOUNDS((char *) new_thread, (char *) new_thread_bottom);
364 PDEBUG("initial stack: bos=%p, tos=%p\n", __pthread_initial_thread_bos,
365 __pthread_initial_thread_tos);
366 #endif
368 else
370 #ifdef __ARCH_USE_MMU__
371 stacksize = STACK_SIZE - pagesize;
372 if (attr != NULL)
373 stacksize = MIN(stacksize, roundup(attr->__stacksize, pagesize));
374 /* Allocate space for stack and thread descriptor at default address */
375 new_thread = default_new_thread;
376 new_thread_bottom = (char *) (new_thread + 1) - stacksize;
377 if (mmap((caddr_t)((char *)(new_thread + 1) - INITIAL_STACK_SIZE),
378 INITIAL_STACK_SIZE, PROT_READ | PROT_WRITE | PROT_EXEC,
379 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_GROWSDOWN,
380 -1, 0) == MAP_FAILED)
381 /* Bad luck, this segment is already mapped. */
382 return -1;
383 /* We manage to get a stack. Now see whether we need a guard
384 and allocate it if necessary. Notice that the default
385 attributes (stack_size = STACK_SIZE - pagesize) do not need
386 a guard page, since the RLIMIT_STACK soft limit prevents stacks
387 from running into one another. */
388 if (stacksize == (size_t) (STACK_SIZE - pagesize))
390 /* We don't need a guard page. */
391 guardaddr = NULL;
392 guardsize = 0;
394 else
396 /* Put a bad page at the bottom of the stack */
397 guardsize = attr->__guardsize;
398 guardaddr = (void *)new_thread_bottom - guardsize;
399 if (mmap((caddr_t) guardaddr, guardsize, 0, MAP_FIXED, -1, 0)
400 == MAP_FAILED)
402 /* We don't make this an error. */
403 guardaddr = NULL;
404 guardsize = 0;
407 #else
408 /* We cannot mmap to this huge chunk of stack space when we don't have
409 * an MMU. Pretend we are using a user provided stack even if there was
410 * none provided by the user. Thus, we get around the mmap and reservation
411 * of a huge stack segment. -StS */
413 stacksize = INITIAL_STACK_SIZE;
414 /* The user may want to use a non-default stacksize */
415 if (attr != NULL)
417 stacksize = attr->__stacksize;
420 /* malloc a stack - memory from the bottom up */
421 if ((new_thread_bottom = malloc(stacksize)) == NULL)
423 /* bad luck, we cannot malloc any more */
424 return -1 ;
426 PDEBUG("malloced chunk: base=%p, size=0x%04x\n", new_thread_bottom, stacksize);
428 /* Set up the pointers. new_thread marks the TOP of the stack frame and
429 * the address of the pthread_descr struct at the same time. Therefore we
430 * must account for its size and fit it in the malloc()'ed block. The
431 * value of `new_thread' is then passed to clone() as the stack argument.
433 * ^ +------------------------+
434 * | | pthread_descr struct |
435 * | +------------------------+ <- new_thread
436 * malloc block | | |
437 * | | thread stack |
438 * | | |
439 * v +------------------------+ <- new_thread_bottom
441 * Note: The calculated value of new_thread must be word aligned otherwise
442 * the kernel chokes on a non-aligned stack frame. Choose the lower
443 * available word boundary.
445 new_thread = ((pthread_descr) ((int)(new_thread_bottom + stacksize) & -sizeof(void*))) - 1;
446 guardaddr = NULL;
447 guardsize = 0;
449 PDEBUG("thread stack: bos=%p, tos=%p\n", new_thread_bottom, new_thread);
451 /* check the initial thread stack boundaries so they don't overlap */
452 NOMMU_INITIAL_THREAD_BOUNDS((char *) new_thread, (char *) new_thread_bottom);
454 PDEBUG("initial stack: bos=%p, tos=%p\n", __pthread_initial_thread_bos,
455 __pthread_initial_thread_tos);
457 /* on non-MMU systems we always have non-standard stack frames */
458 __pthread_nonstandard_stacks = 1;
460 #endif /* __ARCH_USE_MMU__ */
463 /* Clear the thread data structure. */
464 memset (new_thread, '\0', sizeof (*new_thread));
465 *out_new_thread = new_thread;
466 *out_new_thread_bottom = new_thread_bottom;
467 *out_guardaddr = guardaddr;
468 *out_guardsize = guardsize;
469 return 0;
472 static int pthread_handle_create(pthread_t *thread, const pthread_attr_t *attr,
473 void * (*start_routine)(void *), void *arg,
474 sigset_t * mask, int father_pid,
475 int report_events,
476 td_thr_events_t *event_maskp)
478 size_t sseg;
479 int pid;
480 pthread_descr new_thread;
481 char * new_thread_bottom;
482 char * new_thread_top;
483 pthread_t new_thread_id;
484 char *guardaddr = NULL;
485 size_t guardsize = 0;
486 int pagesize = getpagesize();
487 int saved_errno = 0;
489 /* First check whether we have to change the policy and if yes, whether
490 we can do this. Normally this should be done by examining the
491 return value of the sched_setscheduler call in pthread_start_thread
492 but this is hard to implement. FIXME */
493 if (attr != NULL && attr->__schedpolicy != SCHED_OTHER && geteuid () != 0)
494 return EPERM;
495 /* Find a free segment for the thread, and allocate a stack if needed */
496 for (sseg = 2; ; sseg++)
498 if (sseg >= PTHREAD_THREADS_MAX)
499 return EAGAIN;
500 if (__pthread_handles[sseg].h_descr != NULL)
501 continue;
502 if (pthread_allocate_stack(attr, thread_segment(sseg), pagesize,
503 &new_thread, &new_thread_bottom,
504 &guardaddr, &guardsize) == 0)
505 break;
506 #ifndef __ARCH_USE_MMU__
507 else
508 /* When there is MMU, mmap () is used to allocate the stack. If one
509 * segment is already mapped, we should continue to see if we can
510 * use the next one. However, when there is no MMU, malloc () is used.
511 * It's waste of CPU cycles to continue to try if it fails. */
512 return EAGAIN;
513 #endif
515 __pthread_handles_num++;
516 /* Allocate new thread identifier */
517 pthread_threads_counter += PTHREAD_THREADS_MAX;
518 new_thread_id = sseg + pthread_threads_counter;
519 /* Initialize the thread descriptor. Elements which have to be
520 initialized to zero already have this value. */
521 new_thread->p_tid = new_thread_id;
522 new_thread->p_lock = &(__pthread_handles[sseg].h_lock);
523 new_thread->p_cancelstate = PTHREAD_CANCEL_ENABLE;
524 new_thread->p_canceltype = PTHREAD_CANCEL_DEFERRED;
525 new_thread->p_errnop = &new_thread->p_errno;
526 new_thread->p_h_errnop = &new_thread->p_h_errno;
527 #ifdef __UCLIBC_HAS_XLOCALE__
528 /* Initialize thread's locale to the global locale. */
529 new_thread->locale = __global_locale;
530 #endif /* __UCLIBC_HAS_XLOCALE__ */
531 new_thread->p_guardaddr = guardaddr;
532 new_thread->p_guardsize = guardsize;
533 new_thread->p_self = new_thread;
534 new_thread->p_nr = sseg;
535 /* Initialize the thread handle */
536 __pthread_init_lock(&__pthread_handles[sseg].h_lock);
537 __pthread_handles[sseg].h_descr = new_thread;
538 __pthread_handles[sseg].h_bottom = new_thread_bottom;
539 /* Determine scheduling parameters for the thread */
540 new_thread->p_start_args.schedpolicy = -1;
541 if (attr != NULL) {
542 new_thread->p_detached = attr->__detachstate;
543 new_thread->p_userstack = attr->__stackaddr_set;
545 switch(attr->__inheritsched) {
546 case PTHREAD_EXPLICIT_SCHED:
547 new_thread->p_start_args.schedpolicy = attr->__schedpolicy;
548 memcpy (&new_thread->p_start_args.schedparam, &attr->__schedparam,
549 sizeof (struct sched_param));
550 break;
551 case PTHREAD_INHERIT_SCHED:
552 new_thread->p_start_args.schedpolicy = sched_getscheduler(father_pid);
553 sched_getparam(father_pid, &new_thread->p_start_args.schedparam);
554 break;
556 new_thread->p_priority =
557 new_thread->p_start_args.schedparam.sched_priority;
559 /* Finish setting up arguments to pthread_start_thread */
560 new_thread->p_start_args.start_routine = start_routine;
561 new_thread->p_start_args.arg = arg;
562 new_thread->p_start_args.mask = *mask;
563 /* Raise priority of thread manager if needed */
564 __pthread_manager_adjust_prio(new_thread->p_priority);
565 /* Do the cloning. We have to use two different functions depending
566 on whether we are debugging or not. */
567 pid = 0; /* Note that the thread never can have PID zero. */
568 new_thread_top = ((char *)new_thread - THREAD_STACK_OFFSET);
570 /* ******************************************************** */
571 /* This code was moved from below to cope with running threads
572 * on uClinux systems. See comment below...
573 * Insert new thread in doubly linked list of active threads */
574 new_thread->p_prevlive = __pthread_main_thread;
575 new_thread->p_nextlive = __pthread_main_thread->p_nextlive;
576 __pthread_main_thread->p_nextlive->p_prevlive = new_thread;
577 __pthread_main_thread->p_nextlive = new_thread;
578 /* ********************************************************* */
580 if (report_events)
582 /* See whether the TD_CREATE event bit is set in any of the
583 masks. */
584 int idx = __td_eventword (TD_CREATE);
585 uint32_t m = __td_eventmask (TD_CREATE);
587 if ((m & (__pthread_threads_events.event_bits[idx]
588 | event_maskp->event_bits[idx])) != 0)
590 /* Lock the mutex the child will use now so that it will stop. */
591 __pthread_lock(new_thread->p_lock, NULL);
593 /* We have to report this event. */
594 #ifdef __ia64__
595 pid = __clone2(pthread_start_thread_event, new_thread_top,
596 new_thread_top - new_thread_bottom,
597 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
598 __pthread_sig_cancel, new_thread);
599 #else
600 pid = clone(pthread_start_thread_event, new_thread_top,
601 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
602 __pthread_sig_cancel, new_thread);
603 #endif
605 saved_errno = errno;
606 if (pid != -1)
608 /* Now fill in the information about the new thread in
609 the newly created thread's data structure. We cannot let
610 the new thread do this since we don't know whether it was
611 already scheduled when we send the event. */
612 new_thread->p_eventbuf.eventdata = new_thread;
613 new_thread->p_eventbuf.eventnum = TD_CREATE;
614 __pthread_last_event = new_thread;
616 /* We have to set the PID here since the callback function
617 in the debug library will need it and we cannot guarantee
618 the child got scheduled before the debugger. */
619 new_thread->p_pid = pid;
621 /* Now call the function which signals the event. */
622 __linuxthreads_create_event ();
624 /* Now restart the thread. */
625 __pthread_unlock(new_thread->p_lock);
629 if (pid == 0)
631 PDEBUG("cloning new_thread = %p\n", new_thread);
632 #ifdef __ia64__
633 pid = __clone2(pthread_start_thread, new_thread_top,
634 new_thread_top - new_thread_bottom,
635 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
636 __pthread_sig_cancel, new_thread);
637 #else
638 pid = clone(pthread_start_thread, new_thread_top,
639 CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND |
640 __pthread_sig_cancel, new_thread);
641 #endif
642 saved_errno = errno;
644 /* Check if cloning succeeded */
645 if (pid == -1) {
646 /********************************************************
647 * Code inserted to remove the thread from our list of active
648 * threads in case of failure (needed to cope with uClinux),
649 * See comment below. */
650 new_thread->p_nextlive->p_prevlive = new_thread->p_prevlive;
651 new_thread->p_prevlive->p_nextlive = new_thread->p_nextlive;
652 /********************************************************/
654 /* Free the stack if we allocated it */
655 if (attr == NULL || !attr->__stackaddr_set)
657 #ifdef __ARCH_USE_MMU__
658 if (new_thread->p_guardsize != 0)
659 munmap(new_thread->p_guardaddr, new_thread->p_guardsize);
660 munmap((caddr_t)((char *)(new_thread+1) - INITIAL_STACK_SIZE),
661 INITIAL_STACK_SIZE);
662 #else
663 free(new_thread_bottom);
664 #endif /* __ARCH_USE_MMU__ */
666 __pthread_handles[sseg].h_descr = NULL;
667 __pthread_handles[sseg].h_bottom = NULL;
668 __pthread_handles_num--;
669 return saved_errno;
671 PDEBUG("new thread pid = %d\n", pid);
673 #if 0
674 /* ***********************************************************
675 This code has been moved before the call to clone(). In uClinux,
676 the use of wait on a semaphore is dependant upon that the child so
677 the child must be in the active threads list. This list is used in
678 pthread_find_self() to get the pthread_descr of self. So, if the
679 child calls sem_wait before this code is executed , it will hang
680 forever and initial_thread will instead be posted by a sem_post
681 call. */
683 /* Insert new thread in doubly linked list of active threads */
684 new_thread->p_prevlive = __pthread_main_thread;
685 new_thread->p_nextlive = __pthread_main_thread->p_nextlive;
686 __pthread_main_thread->p_nextlive->p_prevlive = new_thread;
687 __pthread_main_thread->p_nextlive = new_thread;
688 /************************************************************/
689 #endif
691 /* Set pid field of the new thread, in case we get there before the
692 child starts. */
693 new_thread->p_pid = pid;
694 /* We're all set */
695 *thread = new_thread_id;
696 return 0;
700 /* Try to free the resources of a thread when requested by pthread_join
701 or pthread_detach on a terminated thread. */
703 static void pthread_free(pthread_descr th)
705 pthread_handle handle;
706 pthread_readlock_info *iter, *next;
707 #ifndef __ARCH_USE_MMU__
708 char *h_bottom_save;
709 #endif
711 /* Make the handle invalid */
712 handle = thread_handle(th->p_tid);
713 __pthread_lock(&handle->h_lock, NULL);
714 #ifndef __ARCH_USE_MMU__
715 h_bottom_save = handle->h_bottom;
716 #endif
717 handle->h_descr = NULL;
718 handle->h_bottom = (char *)(-1L);
719 __pthread_unlock(&handle->h_lock);
720 #ifdef FREE_THREAD_SELF
721 FREE_THREAD_SELF(th, th->p_nr);
722 #endif
723 /* One fewer threads in __pthread_handles */
724 __pthread_handles_num--;
726 /* Destroy read lock list, and list of free read lock structures.
727 If the former is not empty, it means the thread exited while
728 holding read locks! */
730 for (iter = th->p_readlock_list; iter != NULL; iter = next)
732 next = iter->pr_next;
733 free(iter);
736 for (iter = th->p_readlock_free; iter != NULL; iter = next)
738 next = iter->pr_next;
739 free(iter);
742 /* If initial thread, nothing to free */
743 if (th == &__pthread_initial_thread) return;
744 if (!th->p_userstack)
746 #ifdef __ARCH_USE_MMU__
747 /* Free the stack and thread descriptor area */
748 if (th->p_guardsize != 0)
749 munmap(th->p_guardaddr, th->p_guardsize);
750 munmap((caddr_t) ((char *)(th+1) - STACK_SIZE), STACK_SIZE);
751 #else
752 /* For non-MMU systems we always malloc the stack, so free it here. -StS */
753 free(h_bottom_save);
754 #endif /* __ARCH_USE_MMU__ */
758 /* Handle threads that have exited */
760 static void pthread_exited(pid_t pid)
762 pthread_descr th;
763 int detached;
764 /* Find thread with that pid */
765 for (th = __pthread_main_thread->p_nextlive;
766 th != __pthread_main_thread;
767 th = th->p_nextlive) {
768 if (th->p_pid == pid) {
769 /* Remove thread from list of active threads */
770 th->p_nextlive->p_prevlive = th->p_prevlive;
771 th->p_prevlive->p_nextlive = th->p_nextlive;
772 /* Mark thread as exited, and if detached, free its resources */
773 __pthread_lock(th->p_lock, NULL);
774 th->p_exited = 1;
775 /* If we have to signal this event do it now. */
776 if (th->p_report_events)
778 /* See whether TD_REAP is in any of the mask. */
779 int idx = __td_eventword (TD_REAP);
780 uint32_t mask = __td_eventmask (TD_REAP);
782 if ((mask & (__pthread_threads_events.event_bits[idx]
783 | th->p_eventbuf.eventmask.event_bits[idx])) != 0)
785 /* Yep, we have to signal the reapage. */
786 th->p_eventbuf.eventnum = TD_REAP;
787 th->p_eventbuf.eventdata = th;
788 __pthread_last_event = th;
790 /* Now call the function to signal the event. */
791 __linuxthreads_reap_event();
794 detached = th->p_detached;
795 __pthread_unlock(th->p_lock);
796 if (detached)
797 pthread_free(th);
798 break;
801 /* If all threads have exited and the main thread is pending on a
802 pthread_exit, wake up the main thread and terminate ourselves. */
803 if (main_thread_exiting &&
804 __pthread_main_thread->p_nextlive == __pthread_main_thread) {
805 restart(__pthread_main_thread);
806 /* Same logic as REQ_MAIN_THREAD_EXIT. */
810 static void pthread_reap_children(void)
812 pid_t pid;
813 int status;
814 PDEBUG("\n");
816 while ((pid = waitpid(-1, &status, WNOHANG | __WCLONE)) > 0) {
817 pthread_exited(pid);
818 if (WIFSIGNALED(status)) {
819 /* If a thread died due to a signal, send the same signal to
820 all other threads, including the main thread. */
821 pthread_kill_all_threads(WTERMSIG(status), 1);
822 _exit(0);
827 /* Try to free the resources of a thread when requested by pthread_join
828 or pthread_detach on a terminated thread. */
830 static void pthread_handle_free(pthread_t th_id)
832 pthread_handle handle = thread_handle(th_id);
833 pthread_descr th;
835 __pthread_lock(&handle->h_lock, NULL);
836 if (invalid_handle(handle, th_id)) {
837 /* pthread_reap_children has deallocated the thread already,
838 nothing needs to be done */
839 __pthread_unlock(&handle->h_lock);
840 return;
842 th = handle->h_descr;
843 if (th->p_exited) {
844 __pthread_unlock(&handle->h_lock);
845 pthread_free(th);
846 } else {
847 /* The Unix process of the thread is still running.
848 Mark the thread as detached so that the thread manager will
849 deallocate its resources when the Unix process exits. */
850 th->p_detached = 1;
851 __pthread_unlock(&handle->h_lock);
855 /* Send a signal to all running threads */
857 static void pthread_kill_all_threads(int sig, int main_thread_also)
859 pthread_descr th;
860 for (th = __pthread_main_thread->p_nextlive;
861 th != __pthread_main_thread;
862 th = th->p_nextlive) {
863 kill(th->p_pid, sig);
865 if (main_thread_also) {
866 kill(__pthread_main_thread->p_pid, sig);
870 /* Process-wide exit() */
872 static void pthread_handle_exit(pthread_descr issuing_thread, int exitcode)
874 pthread_descr th;
875 __pthread_exit_requested = 1;
876 __pthread_exit_code = exitcode;
877 /* Send the CANCEL signal to all running threads, including the main
878 thread, but excluding the thread from which the exit request originated
879 (that thread must complete the exit, e.g. calling atexit functions
880 and flushing stdio buffers). */
881 for (th = issuing_thread->p_nextlive;
882 th != issuing_thread;
883 th = th->p_nextlive) {
884 kill(th->p_pid, __pthread_sig_cancel);
886 /* Now, wait for all these threads, so that they don't become zombies
887 and their times are properly added to the thread manager's times. */
888 for (th = issuing_thread->p_nextlive;
889 th != issuing_thread;
890 th = th->p_nextlive) {
891 waitpid(th->p_pid, NULL, __WCLONE);
893 restart(issuing_thread);
894 _exit(0);
897 /* Handler for __pthread_sig_cancel in thread manager thread */
899 void __pthread_manager_sighandler(int sig attribute_unused)
901 int kick_manager = terminated_children == 0 && main_thread_exiting;
902 terminated_children = 1;
904 /* If the main thread is terminating, kick the thread manager loop
905 each time some threads terminate. This eliminates a two second
906 shutdown delay caused by the thread manager sleeping in the
907 call to __poll(). Instead, the thread manager is kicked into
908 action, reaps the outstanding threads and resumes the main thread
909 so that it can complete the shutdown. */
911 if (kick_manager) {
912 struct pthread_request request;
913 request.req_thread = 0;
914 request.req_kind = REQ_KICK;
915 TEMP_FAILURE_RETRY(write(__pthread_manager_request,
916 (char *) &request, sizeof(request)));
920 /* Adjust priority of thread manager so that it always run at a priority
921 higher than all threads */
923 void __pthread_manager_adjust_prio(int thread_prio)
925 struct sched_param param;
927 if (thread_prio <= __pthread_manager_thread.p_priority) return;
928 param.sched_priority =
929 thread_prio < sched_get_priority_max(SCHED_FIFO)
930 ? thread_prio + 1 : thread_prio;
931 sched_setscheduler(__pthread_manager_thread.p_pid, SCHED_FIFO, &param);
932 __pthread_manager_thread.p_priority = thread_prio;