1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
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. */
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 */
25 #include <sys/poll.h> /* for poll */
26 #include <sys/mman.h> /* for mmap */
27 #include <sys/param.h>
29 #include <sys/wait.h> /* for waitpid macros */
32 #include "internals.h"
35 #include "semaphore.h"
36 #include "debug.h" /* PDEBUG, added by StS */
38 #ifndef THREAD_STACK_OFFSET
39 #define THREAD_STACK_OFFSET 0
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>
50 /* Array of active threads. Entry 0 is reserved for the initial thread. */
51 struct pthread_handle_struct __pthread_handles
[PTHREAD_THREADS_MAX
] =
52 { { __LOCK_INITIALIZER
, &__pthread_initial_thread
, 0},
53 { __LOCK_INITIALIZER
, &__pthread_manager_thread
, 0}, /* All NULLs */ };
55 /* For debugging purposes put the maximum number of threads in a variable. */
56 const int __linuxthreads_pthread_threads_max
= PTHREAD_THREADS_MAX
;
58 /* Indicate whether at least one thread has a user-defined stack (if 1),
59 or if all threads have stacks supplied by LinuxThreads (if 0). */
60 int __pthread_nonstandard_stacks
;
62 /* Number of active entries in __pthread_handles (used by gdb) */
63 volatile int __pthread_handles_num
= 2;
65 /* Whether to use debugger additional actions for thread creation
67 volatile int __pthread_threads_debug
;
69 /* Globally enabled events. */
70 volatile td_thr_events_t __pthread_threads_events
;
72 /* Pointer to thread descriptor with last event. */
73 volatile pthread_descr __pthread_last_event
;
75 /* Mapping from stack segment to thread descriptor. */
76 /* Stack segment numbers are also indices into the __pthread_handles array. */
77 /* Stack segment number 0 is reserved for the initial thread. */
79 static __inline__ pthread_descr
thread_segment(int seg
)
81 return (pthread_descr
)(THREAD_STACK_START_ADDRESS
- (seg
- 1) * STACK_SIZE
)
85 /* Flag set in signal handler to record child termination */
87 static volatile int terminated_children
= 0;
89 /* Flag set when the initial thread is blocked on pthread_exit waiting
90 for all other threads to terminate */
92 static int main_thread_exiting
= 0;
94 /* Counter used to generate unique thread identifier.
95 Thread identifier is pthread_threads_counter + segment. */
97 static pthread_t pthread_threads_counter
= 0;
99 /* Forward declarations */
101 static int pthread_handle_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
102 void * (*start_routine
)(void *), void *arg
,
103 sigset_t
*mask
, int father_pid
,
105 td_thr_events_t
*event_maskp
);
106 static void pthread_handle_free(pthread_t th_id
);
107 static void pthread_handle_exit(pthread_descr issuing_thread
, int exitcode
) attribute_noreturn
;
108 static void pthread_reap_children(void);
109 static void pthread_kill_all_threads(int sig
, int main_thread_also
);
111 /* The server thread managing requests for thread creation and termination */
113 int attribute_noreturn
__pthread_manager(void *arg
)
115 int reqfd
= (int) (long int) arg
;
122 sigset_t manager_mask
;
124 struct pthread_request request
;
126 /* If we have special thread_self processing, initialize it. */
127 #ifdef INIT_THREAD_SELF
128 INIT_THREAD_SELF(&__pthread_manager_thread
, 1);
130 /* Set the error variable. */
131 __pthread_manager_thread
.p_errnop
= &__pthread_manager_thread
.p_errno
;
132 __pthread_manager_thread
.p_h_errnop
= &__pthread_manager_thread
.p_h_errno
;
134 #ifdef __UCLIBC_HAS_XLOCALE__
135 /* Initialize thread's locale to the global locale. */
136 __pthread_manager_thread
.locale
= __global_locale
;
137 #endif /* __UCLIBC_HAS_XLOCALE__ */
139 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
140 __sigfillset(&manager_mask
);
141 sigdelset(&manager_mask
, __pthread_sig_cancel
); /* for thread termination */
142 sigdelset(&manager_mask
, SIGTRAP
); /* for debugging purposes */
143 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0)
144 sigdelset(&manager_mask
, __pthread_sig_debug
);
145 sigprocmask(SIG_SETMASK
, &manager_mask
, NULL
);
146 /* Raise our priority to match that of main thread */
147 __pthread_manager_adjust_prio(__pthread_main_thread
->p_priority
);
148 /* Synchronize debugging of the thread manager */
149 n
= TEMP_FAILURE_RETRY(read(reqfd
, (char *)&request
,
155 /* Enter server loop */
162 n
= select (reqfd
+ 1, &fd
, NULL
, NULL
, &tv
);
164 PDEBUG("before poll\n");
165 n
= poll(&ufd
, 1, 2000);
166 PDEBUG("after poll\n");
168 /* Check for termination of the main thread */
169 if (getppid() == 1) {
170 pthread_kill_all_threads(SIGKILL
, 0);
173 /* Check for dead children */
174 if (terminated_children
) {
175 terminated_children
= 0;
176 pthread_reap_children();
178 /* Read and execute request */
182 if (n
== 1 && (ufd
.revents
& POLLIN
))
186 PDEBUG("before read\n");
187 n
= read(reqfd
, (char *)&request
, sizeof(request
));
188 PDEBUG("after read, n=%d\n", n
);
189 switch(request
.req_kind
) {
191 PDEBUG("got REQ_CREATE\n");
192 request
.req_thread
->p_retcode
=
193 pthread_handle_create((pthread_t
*) &request
.req_thread
->p_retval
,
194 request
.req_args
.create
.attr
,
195 request
.req_args
.create
.fn
,
196 request
.req_args
.create
.arg
,
197 &request
.req_args
.create
.mask
,
198 request
.req_thread
->p_pid
,
199 request
.req_thread
->p_report_events
,
200 &request
.req_thread
->p_eventbuf
.eventmask
);
201 PDEBUG("restarting %p\n", request
.req_thread
);
202 restart(request
.req_thread
);
205 PDEBUG("got REQ_FREE\n");
206 pthread_handle_free(request
.req_args
.free
.thread_id
);
208 case REQ_PROCESS_EXIT
:
209 PDEBUG("got REQ_PROCESS_EXIT from %p, exit code = %d\n",
210 request
.req_thread
, request
.req_args
.exit
.code
);
211 pthread_handle_exit(request
.req_thread
,
212 request
.req_args
.exit
.code
);
214 case REQ_MAIN_THREAD_EXIT
:
215 PDEBUG("got REQ_MAIN_THREAD_EXIT\n");
216 main_thread_exiting
= 1;
217 /* Reap children in case all other threads died and the signal handler
218 went off before we set main_thread_exiting to 1, and therefore did
220 pthread_reap_children();
222 if (__pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
223 restart(__pthread_main_thread
);
224 /* The main thread will now call exit() which will trigger an
225 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
226 to the thread manager. In case you are wondering how the
227 manager terminates from its loop here. */
231 PDEBUG("got REQ_POST\n");
232 sem_post(request
.req_args
.post
);
235 PDEBUG("got REQ_DEBUG\n");
236 /* Make gdb aware of new thread and gdb will restart the
237 new thread when it is ready to handle the new thread. */
238 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0) {
239 PDEBUG("about to call raise(__pthread_sig_debug)\n");
240 raise(__pthread_sig_debug
);
243 /* This is just a prod to get the manager to reap some
244 threads right away, avoiding a potential delay at shutdown. */
251 int attribute_noreturn
__pthread_manager_event(void *arg
)
253 /* If we have special thread_self processing, initialize it. */
254 #ifdef INIT_THREAD_SELF
255 INIT_THREAD_SELF(&__pthread_manager_thread
, 1);
258 /* Get the lock the manager will free once all is correctly set up. */
259 __pthread_lock (THREAD_GETMEM((&__pthread_manager_thread
), p_lock
), NULL
);
260 /* Free it immediately. */
261 __pthread_unlock (THREAD_GETMEM((&__pthread_manager_thread
), p_lock
));
263 __pthread_manager(arg
);
266 /* Process creation */
269 pthread_start_thread(void *arg
)
271 pthread_descr self
= (pthread_descr
) arg
;
272 struct pthread_request request
;
274 /* Initialize special thread_self processing, if any. */
275 #ifdef INIT_THREAD_SELF
276 INIT_THREAD_SELF(self
, self
->p_nr
);
279 /* Make sure our pid field is initialized, just in case we get there
280 before our father has initialized it. */
281 THREAD_SETMEM(self
, p_pid
, getpid());
282 /* Initial signal mask is that of the creating thread. (Otherwise,
283 we'd just inherit the mask of the thread manager.) */
284 sigprocmask(SIG_SETMASK
, &self
->p_start_args
.mask
, NULL
);
285 /* Set the scheduling policy and priority for the new thread, if needed */
286 if (THREAD_GETMEM(self
, p_start_args
.schedpolicy
) >= 0)
287 /* Explicit scheduling attributes were provided: apply them */
288 sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
289 THREAD_GETMEM(self
, p_start_args
.schedpolicy
),
290 &self
->p_start_args
.schedparam
);
291 else if (__pthread_manager_thread
.p_priority
> 0)
292 /* Default scheduling required, but thread manager runs in realtime
293 scheduling: switch new thread to SCHED_OTHER policy */
295 struct sched_param default_params
;
296 default_params
.sched_priority
= 0;
297 sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
298 SCHED_OTHER
, &default_params
);
300 /* Make gdb aware of new thread */
301 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0) {
302 request
.req_thread
= self
;
303 request
.req_kind
= REQ_DEBUG
;
304 TEMP_FAILURE_RETRY(write(__pthread_manager_request
,
305 (char *) &request
, sizeof(request
)));
308 /* Run the thread code */
309 outcome
= self
->p_start_args
.start_routine(THREAD_GETMEM(self
,
311 /* Exit with the given return value */
312 __pthread_do_exit(outcome
, CURRENT_STACK_FRAME
);
317 pthread_start_thread_event(void *arg
)
319 pthread_descr self
= (pthread_descr
) arg
;
321 #ifdef INIT_THREAD_SELF
322 INIT_THREAD_SELF(self
, self
->p_nr
);
324 /* Make sure our pid field is initialized, just in case we get there
325 before our father has initialized it. */
326 THREAD_SETMEM(self
, p_pid
, getpid());
327 /* Get the lock the manager will free once all is correctly set up. */
328 __pthread_lock (THREAD_GETMEM(self
, p_lock
), NULL
);
329 /* Free it immediately. */
330 __pthread_unlock (THREAD_GETMEM(self
, p_lock
));
332 /* Continue with the real function. */
333 pthread_start_thread (arg
);
336 static int pthread_allocate_stack(const pthread_attr_t
*attr
,
337 pthread_descr default_new_thread
,
339 pthread_descr
* out_new_thread
,
340 char ** out_new_thread_bottom
,
341 char ** out_guardaddr
,
342 size_t * out_guardsize
)
344 pthread_descr new_thread
;
345 char * new_thread_bottom
;
347 size_t stacksize
, guardsize
;
349 if (attr
!= NULL
&& attr
->__stackaddr_set
)
351 /* The user provided a stack. */
352 new_thread
= (pthread_descr
) ((long)(attr
->__stackaddr
) & -sizeof(void *)) - 1;
353 new_thread_bottom
= (char *) attr
->__stackaddr
- attr
->__stacksize
;
356 __pthread_nonstandard_stacks
= 1;
357 #ifndef __ARCH_USE_MMU__
358 /* check the initial thread stack boundaries so they don't overlap */
359 NOMMU_INITIAL_THREAD_BOUNDS((char *) new_thread
, (char *) new_thread_bottom
);
361 PDEBUG("initial stack: bos=%p, tos=%p\n", __pthread_initial_thread_bos
,
362 __pthread_initial_thread_tos
);
367 #ifdef __ARCH_USE_MMU__
368 stacksize
= STACK_SIZE
- pagesize
;
370 stacksize
= MIN(stacksize
, roundup(attr
->__stacksize
, pagesize
));
371 /* Allocate space for stack and thread descriptor at default address */
372 new_thread
= default_new_thread
;
373 new_thread_bottom
= (char *) (new_thread
+ 1) - stacksize
;
374 if (mmap((caddr_t
)((char *)(new_thread
+ 1) - INITIAL_STACK_SIZE
),
375 INITIAL_STACK_SIZE
, PROT_READ
| PROT_WRITE
| PROT_EXEC
,
376 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
| MAP_GROWSDOWN
,
377 -1, 0) == MAP_FAILED
)
378 /* Bad luck, this segment is already mapped. */
380 /* We manage to get a stack. Now see whether we need a guard
381 and allocate it if necessary. Notice that the default
382 attributes (stack_size = STACK_SIZE - pagesize) do not need
383 a guard page, since the RLIMIT_STACK soft limit prevents stacks
384 from running into one another. */
385 if (stacksize
== (size_t) (STACK_SIZE
- pagesize
))
387 /* We don't need a guard page. */
393 /* Put a bad page at the bottom of the stack */
394 guardsize
= attr
->__guardsize
;
395 guardaddr
= (void *)new_thread_bottom
- guardsize
;
396 if (mmap((caddr_t
) guardaddr
, guardsize
, 0, MAP_FIXED
, -1, 0)
399 /* We don't make this an error. */
405 /* We cannot mmap to this huge chunk of stack space when we don't have
406 * an MMU. Pretend we are using a user provided stack even if there was
407 * none provided by the user. Thus, we get around the mmap and reservation
408 * of a huge stack segment. -StS */
410 stacksize
= INITIAL_STACK_SIZE
;
411 /* The user may want to use a non-default stacksize */
414 stacksize
= attr
->__stacksize
;
417 /* malloc a stack - memory from the bottom up */
418 if ((new_thread_bottom
= malloc(stacksize
)) == NULL
)
420 /* bad luck, we cannot malloc any more */
423 PDEBUG("malloced chunk: base=%p, size=0x%04x\n", new_thread_bottom
, stacksize
);
425 /* Set up the pointers. new_thread marks the TOP of the stack frame and
426 * the address of the pthread_descr struct at the same time. Therefore we
427 * must account for its size and fit it in the malloc()'ed block. The
428 * value of `new_thread' is then passed to clone() as the stack argument.
430 * ^ +------------------------+
431 * | | pthread_descr struct |
432 * | +------------------------+ <- new_thread
436 * v +------------------------+ <- new_thread_bottom
438 * Note: The calculated value of new_thread must be word aligned otherwise
439 * the kernel chokes on a non-aligned stack frame. Choose the lower
440 * available word boundary.
442 new_thread
= ((pthread_descr
) ((int)(new_thread_bottom
+ stacksize
) & -sizeof(void*))) - 1;
446 PDEBUG("thread stack: bos=%p, tos=%p\n", new_thread_bottom
, new_thread
);
448 /* check the initial thread stack boundaries so they don't overlap */
449 NOMMU_INITIAL_THREAD_BOUNDS((char *) new_thread
, (char *) new_thread_bottom
);
451 PDEBUG("initial stack: bos=%p, tos=%p\n", __pthread_initial_thread_bos
,
452 __pthread_initial_thread_tos
);
454 /* on non-MMU systems we always have non-standard stack frames */
455 __pthread_nonstandard_stacks
= 1;
457 #endif /* __ARCH_USE_MMU__ */
460 /* Clear the thread data structure. */
461 memset (new_thread
, '\0', sizeof (*new_thread
));
462 *out_new_thread
= new_thread
;
463 *out_new_thread_bottom
= new_thread_bottom
;
464 *out_guardaddr
= guardaddr
;
465 *out_guardsize
= guardsize
;
469 static int pthread_handle_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
470 void * (*start_routine
)(void *), void *arg
,
471 sigset_t
* mask
, int father_pid
,
473 td_thr_events_t
*event_maskp
)
477 pthread_descr new_thread
;
478 char * new_thread_bottom
;
479 char * new_thread_top
;
480 pthread_t new_thread_id
;
481 char *guardaddr
= NULL
;
482 size_t guardsize
= 0;
483 int pagesize
= getpagesize();
486 /* First check whether we have to change the policy and if yes, whether
487 we can do this. Normally this should be done by examining the
488 return value of the sched_setscheduler call in pthread_start_thread
489 but this is hard to implement. FIXME */
490 if (attr
!= NULL
&& attr
->__schedpolicy
!= SCHED_OTHER
&& geteuid () != 0)
492 /* Find a free segment for the thread, and allocate a stack if needed */
493 for (sseg
= 2; ; sseg
++)
495 if (sseg
>= PTHREAD_THREADS_MAX
)
497 if (__pthread_handles
[sseg
].h_descr
!= NULL
)
499 if (pthread_allocate_stack(attr
, thread_segment(sseg
), pagesize
,
500 &new_thread
, &new_thread_bottom
,
501 &guardaddr
, &guardsize
) == 0)
503 #ifndef __ARCH_USE_MMU__
505 /* When there is MMU, mmap () is used to allocate the stack. If one
506 * segment is already mapped, we should continue to see if we can
507 * use the next one. However, when there is no MMU, malloc () is used.
508 * It's waste of CPU cycles to continue to try if it fails. */
512 __pthread_handles_num
++;
513 /* Allocate new thread identifier */
514 pthread_threads_counter
+= PTHREAD_THREADS_MAX
;
515 new_thread_id
= sseg
+ pthread_threads_counter
;
516 /* Initialize the thread descriptor. Elements which have to be
517 initialized to zero already have this value. */
518 new_thread
->p_tid
= new_thread_id
;
519 new_thread
->p_lock
= &(__pthread_handles
[sseg
].h_lock
);
520 new_thread
->p_cancelstate
= PTHREAD_CANCEL_ENABLE
;
521 new_thread
->p_canceltype
= PTHREAD_CANCEL_DEFERRED
;
522 new_thread
->p_errnop
= &new_thread
->p_errno
;
523 new_thread
->p_h_errnop
= &new_thread
->p_h_errno
;
524 #ifdef __UCLIBC_HAS_XLOCALE__
525 /* Initialize thread's locale to the global locale. */
526 new_thread
->locale
= __global_locale
;
527 #endif /* __UCLIBC_HAS_XLOCALE__ */
528 new_thread
->p_guardaddr
= guardaddr
;
529 new_thread
->p_guardsize
= guardsize
;
530 new_thread
->p_self
= new_thread
;
531 new_thread
->p_nr
= sseg
;
532 /* Initialize the thread handle */
533 __pthread_init_lock(&__pthread_handles
[sseg
].h_lock
);
534 __pthread_handles
[sseg
].h_descr
= new_thread
;
535 __pthread_handles
[sseg
].h_bottom
= new_thread_bottom
;
536 /* Determine scheduling parameters for the thread */
537 new_thread
->p_start_args
.schedpolicy
= -1;
539 new_thread
->p_detached
= attr
->__detachstate
;
540 new_thread
->p_userstack
= attr
->__stackaddr_set
;
542 switch(attr
->__inheritsched
) {
543 case PTHREAD_EXPLICIT_SCHED
:
544 new_thread
->p_start_args
.schedpolicy
= attr
->__schedpolicy
;
545 memcpy (&new_thread
->p_start_args
.schedparam
, &attr
->__schedparam
,
546 sizeof (struct sched_param
));
548 case PTHREAD_INHERIT_SCHED
:
549 new_thread
->p_start_args
.schedpolicy
= sched_getscheduler(father_pid
);
550 sched_getparam(father_pid
, &new_thread
->p_start_args
.schedparam
);
553 new_thread
->p_priority
=
554 new_thread
->p_start_args
.schedparam
.sched_priority
;
556 /* Finish setting up arguments to pthread_start_thread */
557 new_thread
->p_start_args
.start_routine
= start_routine
;
558 new_thread
->p_start_args
.arg
= arg
;
559 new_thread
->p_start_args
.mask
= *mask
;
560 /* Raise priority of thread manager if needed */
561 __pthread_manager_adjust_prio(new_thread
->p_priority
);
562 /* Do the cloning. We have to use two different functions depending
563 on whether we are debugging or not. */
564 pid
= 0; /* Note that the thread never can have PID zero. */
565 new_thread_top
= ((char *)new_thread
- THREAD_STACK_OFFSET
);
567 /* ******************************************************** */
568 /* This code was moved from below to cope with running threads
569 * on uClinux systems. See comment below...
570 * Insert new thread in doubly linked list of active threads */
571 new_thread
->p_prevlive
= __pthread_main_thread
;
572 new_thread
->p_nextlive
= __pthread_main_thread
->p_nextlive
;
573 __pthread_main_thread
->p_nextlive
->p_prevlive
= new_thread
;
574 __pthread_main_thread
->p_nextlive
= new_thread
;
575 /* ********************************************************* */
579 /* See whether the TD_CREATE event bit is set in any of the
581 int idx
= __td_eventword (TD_CREATE
);
582 uint32_t m
= __td_eventmask (TD_CREATE
);
584 if ((m
& (__pthread_threads_events
.event_bits
[idx
]
585 | event_maskp
->event_bits
[idx
])) != 0)
587 /* Lock the mutex the child will use now so that it will stop. */
588 __pthread_lock(new_thread
->p_lock
, NULL
);
590 /* We have to report this event. */
592 pid
= __clone2(pthread_start_thread_event
, new_thread_top
,
593 new_thread_top
- new_thread_bottom
,
594 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
595 __pthread_sig_cancel
, new_thread
);
597 pid
= clone(pthread_start_thread_event
, new_thread_top
,
598 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
599 __pthread_sig_cancel
, new_thread
);
605 /* Now fill in the information about the new thread in
606 the newly created thread's data structure. We cannot let
607 the new thread do this since we don't know whether it was
608 already scheduled when we send the event. */
609 new_thread
->p_eventbuf
.eventdata
= new_thread
;
610 new_thread
->p_eventbuf
.eventnum
= TD_CREATE
;
611 __pthread_last_event
= new_thread
;
613 /* We have to set the PID here since the callback function
614 in the debug library will need it and we cannot guarantee
615 the child got scheduled before the debugger. */
616 new_thread
->p_pid
= pid
;
618 /* Now call the function which signals the event. */
619 __linuxthreads_create_event ();
621 /* Now restart the thread. */
622 __pthread_unlock(new_thread
->p_lock
);
628 PDEBUG("cloning new_thread = %p\n", new_thread
);
630 pid
= __clone2(pthread_start_thread
, new_thread_top
,
631 new_thread_top
- new_thread_bottom
,
632 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
633 __pthread_sig_cancel
, new_thread
);
635 pid
= clone(pthread_start_thread
, new_thread_top
,
636 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
637 __pthread_sig_cancel
, new_thread
);
641 /* Check if cloning succeeded */
643 /********************************************************
644 * Code inserted to remove the thread from our list of active
645 * threads in case of failure (needed to cope with uClinux),
646 * See comment below. */
647 new_thread
->p_nextlive
->p_prevlive
= new_thread
->p_prevlive
;
648 new_thread
->p_prevlive
->p_nextlive
= new_thread
->p_nextlive
;
649 /********************************************************/
651 /* Free the stack if we allocated it */
652 if (attr
== NULL
|| !attr
->__stackaddr_set
)
654 #ifdef __ARCH_USE_MMU__
655 if (new_thread
->p_guardsize
!= 0)
656 munmap(new_thread
->p_guardaddr
, new_thread
->p_guardsize
);
657 munmap((caddr_t
)((char *)(new_thread
+1) - INITIAL_STACK_SIZE
),
660 free(new_thread_bottom
);
661 #endif /* __ARCH_USE_MMU__ */
663 __pthread_handles
[sseg
].h_descr
= NULL
;
664 __pthread_handles
[sseg
].h_bottom
= NULL
;
665 __pthread_handles_num
--;
668 PDEBUG("new thread pid = %d\n", pid
);
671 /* ***********************************************************
672 This code has been moved before the call to clone(). In uClinux,
673 the use of wait on a semaphore is dependant upon that the child so
674 the child must be in the active threads list. This list is used in
675 pthread_find_self() to get the pthread_descr of self. So, if the
676 child calls sem_wait before this code is executed , it will hang
677 forever and initial_thread will instead be posted by a sem_post
680 /* Insert new thread in doubly linked list of active threads */
681 new_thread
->p_prevlive
= __pthread_main_thread
;
682 new_thread
->p_nextlive
= __pthread_main_thread
->p_nextlive
;
683 __pthread_main_thread
->p_nextlive
->p_prevlive
= new_thread
;
684 __pthread_main_thread
->p_nextlive
= new_thread
;
685 /************************************************************/
688 /* Set pid field of the new thread, in case we get there before the
690 new_thread
->p_pid
= pid
;
692 *thread
= new_thread_id
;
697 /* Try to free the resources of a thread when requested by pthread_join
698 or pthread_detach on a terminated thread. */
700 static void pthread_free(pthread_descr th
)
702 pthread_handle handle
;
703 pthread_readlock_info
*iter
, *next
;
704 #ifndef __ARCH_USE_MMU__
708 /* Make the handle invalid */
709 handle
= thread_handle(th
->p_tid
);
710 __pthread_lock(&handle
->h_lock
, NULL
);
711 #ifndef __ARCH_USE_MMU__
712 h_bottom_save
= handle
->h_bottom
;
714 handle
->h_descr
= NULL
;
715 handle
->h_bottom
= (char *)(-1L);
716 __pthread_unlock(&handle
->h_lock
);
717 #ifdef FREE_THREAD_SELF
718 FREE_THREAD_SELF(th
, th
->p_nr
);
720 /* One fewer threads in __pthread_handles */
721 __pthread_handles_num
--;
723 /* Destroy read lock list, and list of free read lock structures.
724 If the former is not empty, it means the thread exited while
725 holding read locks! */
727 for (iter
= th
->p_readlock_list
; iter
!= NULL
; iter
= next
)
729 next
= iter
->pr_next
;
733 for (iter
= th
->p_readlock_free
; iter
!= NULL
; iter
= next
)
735 next
= iter
->pr_next
;
739 /* If initial thread, nothing to free */
740 if (th
== &__pthread_initial_thread
) return;
741 if (!th
->p_userstack
)
743 #ifdef __ARCH_USE_MMU__
744 /* Free the stack and thread descriptor area */
745 if (th
->p_guardsize
!= 0)
746 munmap(th
->p_guardaddr
, th
->p_guardsize
);
747 munmap((caddr_t
) ((char *)(th
+1) - STACK_SIZE
), STACK_SIZE
);
749 /* For non-MMU systems we always malloc the stack, so free it here. -StS */
751 #endif /* __ARCH_USE_MMU__ */
755 /* Handle threads that have exited */
757 static void pthread_exited(pid_t pid
)
761 /* Find thread with that pid */
762 for (th
= __pthread_main_thread
->p_nextlive
;
763 th
!= __pthread_main_thread
;
764 th
= th
->p_nextlive
) {
765 if (th
->p_pid
== pid
) {
766 /* Remove thread from list of active threads */
767 th
->p_nextlive
->p_prevlive
= th
->p_prevlive
;
768 th
->p_prevlive
->p_nextlive
= th
->p_nextlive
;
769 /* Mark thread as exited, and if detached, free its resources */
770 __pthread_lock(th
->p_lock
, NULL
);
772 /* If we have to signal this event do it now. */
773 if (th
->p_report_events
)
775 /* See whether TD_REAP is in any of the mask. */
776 int idx
= __td_eventword (TD_REAP
);
777 uint32_t mask
= __td_eventmask (TD_REAP
);
779 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
780 | th
->p_eventbuf
.eventmask
.event_bits
[idx
])) != 0)
782 /* Yep, we have to signal the reapage. */
783 th
->p_eventbuf
.eventnum
= TD_REAP
;
784 th
->p_eventbuf
.eventdata
= th
;
785 __pthread_last_event
= th
;
787 /* Now call the function to signal the event. */
788 __linuxthreads_reap_event();
791 detached
= th
->p_detached
;
792 __pthread_unlock(th
->p_lock
);
798 /* If all threads have exited and the main thread is pending on a
799 pthread_exit, wake up the main thread and terminate ourselves. */
800 if (main_thread_exiting
&&
801 __pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
802 restart(__pthread_main_thread
);
803 /* Same logic as REQ_MAIN_THREAD_EXIT. */
807 static void pthread_reap_children(void)
813 while ((pid
= waitpid(-1, &status
, WNOHANG
| __WCLONE
)) > 0) {
815 if (WIFSIGNALED(status
)) {
816 /* If a thread died due to a signal, send the same signal to
817 all other threads, including the main thread. */
818 pthread_kill_all_threads(WTERMSIG(status
), 1);
824 /* Try to free the resources of a thread when requested by pthread_join
825 or pthread_detach on a terminated thread. */
827 static void pthread_handle_free(pthread_t th_id
)
829 pthread_handle handle
= thread_handle(th_id
);
832 __pthread_lock(&handle
->h_lock
, NULL
);
833 if (invalid_handle(handle
, th_id
)) {
834 /* pthread_reap_children has deallocated the thread already,
835 nothing needs to be done */
836 __pthread_unlock(&handle
->h_lock
);
839 th
= handle
->h_descr
;
841 __pthread_unlock(&handle
->h_lock
);
844 /* The Unix process of the thread is still running.
845 Mark the thread as detached so that the thread manager will
846 deallocate its resources when the Unix process exits. */
848 __pthread_unlock(&handle
->h_lock
);
852 /* Send a signal to all running threads */
854 static void pthread_kill_all_threads(int sig
, int main_thread_also
)
857 for (th
= __pthread_main_thread
->p_nextlive
;
858 th
!= __pthread_main_thread
;
859 th
= th
->p_nextlive
) {
860 kill(th
->p_pid
, sig
);
862 if (main_thread_also
) {
863 kill(__pthread_main_thread
->p_pid
, sig
);
867 /* Process-wide exit() */
869 static void pthread_handle_exit(pthread_descr issuing_thread
, int exitcode
)
872 __pthread_exit_requested
= 1;
873 __pthread_exit_code
= exitcode
;
874 /* Send the CANCEL signal to all running threads, including the main
875 thread, but excluding the thread from which the exit request originated
876 (that thread must complete the exit, e.g. calling atexit functions
877 and flushing stdio buffers). */
878 for (th
= issuing_thread
->p_nextlive
;
879 th
!= issuing_thread
;
880 th
= th
->p_nextlive
) {
881 kill(th
->p_pid
, __pthread_sig_cancel
);
883 /* Now, wait for all these threads, so that they don't become zombies
884 and their times are properly added to the thread manager's times. */
885 for (th
= issuing_thread
->p_nextlive
;
886 th
!= issuing_thread
;
887 th
= th
->p_nextlive
) {
888 waitpid(th
->p_pid
, NULL
, __WCLONE
);
890 restart(issuing_thread
);
894 /* Handler for __pthread_sig_cancel in thread manager thread */
896 void __pthread_manager_sighandler(int sig attribute_unused
)
898 int kick_manager
= terminated_children
== 0 && main_thread_exiting
;
899 terminated_children
= 1;
901 /* If the main thread is terminating, kick the thread manager loop
902 each time some threads terminate. This eliminates a two second
903 shutdown delay caused by the thread manager sleeping in the
904 call to __poll(). Instead, the thread manager is kicked into
905 action, reaps the outstanding threads and resumes the main thread
906 so that it can complete the shutdown. */
909 struct pthread_request request
;
910 request
.req_thread
= 0;
911 request
.req_kind
= REQ_KICK
;
912 TEMP_FAILURE_RETRY(write(__pthread_manager_request
,
913 (char *) &request
, sizeof(request
)));
917 /* Adjust priority of thread manager so that it always run at a priority
918 higher than all threads */
920 void __pthread_manager_adjust_prio(int thread_prio
)
922 struct sched_param param
;
924 if (thread_prio
<= __pthread_manager_thread
.p_priority
) return;
925 param
.sched_priority
=
926 thread_prio
< sched_get_priority_max(SCHED_FIFO
)
927 ? thread_prio
+ 1 : thread_prio
;
928 sched_setscheduler(__pthread_manager_thread
.p_pid
, SCHED_FIFO
, ¶m
);
929 __pthread_manager_thread
.p_priority
= thread_prio
;