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 */
24 #include <sys/poll.h> /* for poll */
25 #include <sys/mman.h> /* for mmap */
26 #include <sys/param.h>
28 #include <sys/wait.h> /* for waitpid macros */
31 #include "internals.h"
34 #include "semaphore.h"
36 /* Array of active threads. Entry 0 is reserved for the initial thread. */
37 struct pthread_handle_struct __pthread_handles
[PTHREAD_THREADS_MAX
] =
38 { { __LOCK_INITIALIZER
, &__pthread_initial_thread
, 0},
39 { __LOCK_INITIALIZER
, &__pthread_manager_thread
, 0}, /* All NULLs */ };
41 /* For debugging purposes put the maximum number of threads in a variable. */
42 const int __linuxthreads_pthread_threads_max
= PTHREAD_THREADS_MAX
;
45 /* Indicate whether at least one thread has a user-defined stack (if 1),
46 or if all threads have stacks supplied by LinuxThreads (if 0). */
47 int __pthread_nonstandard_stacks
;
50 /* Number of active entries in __pthread_handles (used by gdb) */
51 volatile int __pthread_handles_num
= 2;
53 /* Whether to use debugger additional actions for thread creation
55 volatile int __pthread_threads_debug
;
57 /* Globally enabled events. */
58 volatile td_thr_events_t __pthread_threads_events
;
60 /* Pointer to thread descriptor with last event. */
61 volatile pthread_descr __pthread_last_event
;
63 /* Mapping from stack segment to thread descriptor. */
64 /* Stack segment numbers are also indices into the __pthread_handles array. */
65 /* Stack segment number 0 is reserved for the initial thread. */
68 # define thread_segment(seq) NULL
70 static inline pthread_descr
thread_segment(int seg
)
72 return (pthread_descr
)(THREAD_STACK_START_ADDRESS
- (seg
- 1) * STACK_SIZE
)
77 /* Flag set in signal handler to record child termination */
79 static volatile int terminated_children
;
81 /* Flag set when the initial thread is blocked on pthread_exit waiting
82 for all other threads to terminate */
84 static int main_thread_exiting
;
86 /* Counter used to generate unique thread identifier.
87 Thread identifier is pthread_threads_counter + segment. */
89 static pthread_t pthread_threads_counter
;
91 /* Forward declarations */
93 static int pthread_handle_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
94 void * (*start_routine
)(void *), void *arg
,
95 sigset_t
*mask
, int father_pid
,
97 td_thr_events_t
*event_maskp
);
98 static void pthread_handle_free(pthread_t th_id
);
99 static void pthread_handle_exit(pthread_descr issuing_thread
, int exitcode
)
100 __attribute__ ((noreturn
));
101 static void pthread_reap_children(void);
102 static void pthread_kill_all_threads(int sig
, int main_thread_also
);
103 static void pthread_for_each_thread(void *arg
,
104 void (*fn
)(void *, pthread_descr
));
106 /* The server thread managing requests for thread creation and termination */
109 __attribute__ ((noreturn
))
110 __pthread_manager(void *arg
)
112 int reqfd
= (int) (long int) arg
;
114 sigset_t manager_mask
;
116 struct pthread_request request
;
118 /* If we have special thread_self processing, initialize it. */
119 #ifdef INIT_THREAD_SELF
120 INIT_THREAD_SELF(&__pthread_manager_thread
, 1);
122 /* Set the error variable. */
123 __pthread_manager_thread
.p_errnop
= &__pthread_manager_thread
.p_errno
;
124 __pthread_manager_thread
.p_h_errnop
= &__pthread_manager_thread
.p_h_errno
;
125 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
126 sigfillset(&manager_mask
);
127 sigdelset(&manager_mask
, __pthread_sig_cancel
); /* for thread termination */
128 sigdelset(&manager_mask
, SIGTRAP
); /* for debugging purposes */
129 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0)
130 sigdelset(&manager_mask
, __pthread_sig_debug
);
131 sigprocmask(SIG_SETMASK
, &manager_mask
, NULL
);
132 /* Raise our priority to match that of main thread */
133 __pthread_manager_adjust_prio(__pthread_main_thread
->p_priority
);
134 /* Synchronize debugging of the thread manager */
135 n
= TEMP_FAILURE_RETRY(__libc_read(reqfd
, (char *)&request
,
137 ASSERT(n
== sizeof(request
) && request
.req_kind
== REQ_DEBUG
);
140 /* Enter server loop */
142 n
= __poll(&ufd
, 1, 2000);
144 /* Check for termination of the main thread */
145 if (getppid() == 1) {
146 pthread_kill_all_threads(SIGKILL
, 0);
149 /* Check for dead children */
150 if (terminated_children
) {
151 terminated_children
= 0;
152 pthread_reap_children();
154 /* Read and execute request */
155 if (n
== 1 && (ufd
.revents
& POLLIN
)) {
156 n
= TEMP_FAILURE_RETRY(__libc_read(reqfd
, (char *)&request
,
161 write(STDERR_FILENO
, d
, snprintf(d
, sizeof(d
), "*** read err %m\n"));
162 } else if (n
!= sizeof(request
)) {
163 write(STDERR_FILENO
, "*** short read in manager\n", 26);
167 switch(request
.req_kind
) {
169 request
.req_thread
->p_retcode
=
170 pthread_handle_create((pthread_t
*) &request
.req_thread
->p_retval
,
171 request
.req_args
.create
.attr
,
172 request
.req_args
.create
.fn
,
173 request
.req_args
.create
.arg
,
174 &request
.req_args
.create
.mask
,
175 request
.req_thread
->p_pid
,
176 request
.req_thread
->p_report_events
,
177 &request
.req_thread
->p_eventbuf
.eventmask
);
178 restart(request
.req_thread
);
181 pthread_handle_free(request
.req_args
.free
.thread_id
);
183 case REQ_PROCESS_EXIT
:
184 pthread_handle_exit(request
.req_thread
,
185 request
.req_args
.exit
.code
);
188 case REQ_MAIN_THREAD_EXIT
:
189 main_thread_exiting
= 1;
190 /* Reap children in case all other threads died and the signal handler
191 went off before we set main_thread_exiting to 1, and therefore did
193 pthread_reap_children();
195 if (__pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
196 restart(__pthread_main_thread
);
197 /* The main thread will now call exit() which will trigger an
198 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
199 to the thread manager. In case you are wondering how the
200 manager terminates from its loop here. */
204 __new_sem_post(request
.req_args
.post
);
207 /* Make gdb aware of new thread and gdb will restart the
208 new thread when it is ready to handle the new thread. */
209 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0)
210 raise(__pthread_sig_debug
);
213 /* This is just a prod to get the manager to reap some
214 threads right away, avoiding a potential delay at shutdown. */
216 case REQ_FOR_EACH_THREAD
:
217 pthread_for_each_thread(request
.req_args
.for_each
.arg
,
218 request
.req_args
.for_each
.fn
);
219 restart(request
.req_thread
);
226 int __pthread_manager_event(void *arg
)
228 /* If we have special thread_self processing, initialize it. */
229 #ifdef INIT_THREAD_SELF
230 INIT_THREAD_SELF(&__pthread_manager_thread
, 1);
233 /* Get the lock the manager will free once all is correctly set up. */
234 __pthread_lock (THREAD_GETMEM((&__pthread_manager_thread
), p_lock
), NULL
);
235 /* Free it immediately. */
236 __pthread_unlock (THREAD_GETMEM((&__pthread_manager_thread
), p_lock
));
238 return __pthread_manager(arg
);
241 /* Process creation */
244 __attribute__ ((noreturn
))
245 pthread_start_thread(void *arg
)
247 pthread_descr self
= (pthread_descr
) arg
;
248 struct pthread_request request
;
251 hp_timing_t tmpclock
;
253 /* Initialize special thread_self processing, if any. */
254 #ifdef INIT_THREAD_SELF
255 INIT_THREAD_SELF(self
, self
->p_nr
);
258 HP_TIMING_NOW (tmpclock
);
259 THREAD_SETMEM (self
, p_cpuclock_offset
, tmpclock
);
261 /* Make sure our pid field is initialized, just in case we get there
262 before our father has initialized it. */
263 THREAD_SETMEM(self
, p_pid
, __getpid());
264 /* Initial signal mask is that of the creating thread. (Otherwise,
265 we'd just inherit the mask of the thread manager.) */
266 sigprocmask(SIG_SETMASK
, &self
->p_start_args
.mask
, NULL
);
267 /* Set the scheduling policy and priority for the new thread, if needed */
268 if (THREAD_GETMEM(self
, p_start_args
.schedpolicy
) >= 0)
269 /* Explicit scheduling attributes were provided: apply them */
270 __sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
271 THREAD_GETMEM(self
, p_start_args
.schedpolicy
),
272 &self
->p_start_args
.schedparam
);
273 else if (__pthread_manager_thread
.p_priority
> 0)
274 /* Default scheduling required, but thread manager runs in realtime
275 scheduling: switch new thread to SCHED_OTHER policy */
277 struct sched_param default_params
;
278 default_params
.sched_priority
= 0;
279 __sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
280 SCHED_OTHER
, &default_params
);
282 /* Make gdb aware of new thread */
283 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0) {
284 request
.req_thread
= self
;
285 request
.req_kind
= REQ_DEBUG
;
286 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
287 (char *) &request
, sizeof(request
)));
290 /* Run the thread code */
291 outcome
= self
->p_start_args
.start_routine(THREAD_GETMEM(self
,
293 /* Exit with the given return value */
294 __pthread_do_exit(outcome
, CURRENT_STACK_FRAME
);
298 __attribute__ ((noreturn
))
299 pthread_start_thread_event(void *arg
)
301 pthread_descr self
= (pthread_descr
) arg
;
303 #ifdef INIT_THREAD_SELF
304 INIT_THREAD_SELF(self
, self
->p_nr
);
306 /* Make sure our pid field is initialized, just in case we get there
307 before our father has initialized it. */
308 THREAD_SETMEM(self
, p_pid
, __getpid());
309 /* Get the lock the manager will free once all is correctly set up. */
310 __pthread_lock (THREAD_GETMEM(self
, p_lock
), NULL
);
311 /* Free it immediately. */
312 __pthread_unlock (THREAD_GETMEM(self
, p_lock
));
314 /* Continue with the real function. */
315 pthread_start_thread (arg
);
318 static int pthread_allocate_stack(const pthread_attr_t
*attr
,
319 pthread_descr default_new_thread
,
321 pthread_descr
* out_new_thread
,
322 char ** out_new_thread_bottom
,
323 char ** out_guardaddr
,
324 size_t * out_guardsize
)
326 pthread_descr new_thread
;
327 char * new_thread_bottom
;
329 size_t stacksize
, guardsize
;
331 if (attr
!= NULL
&& attr
->__stackaddr_set
)
333 #ifdef _STACK_GROWS_UP
334 /* The user provided a stack. */
335 new_thread
= (pthread_descr
) attr
->__stackaddr
;
336 new_thread_bottom
= (char *) (new_thread
+ 1);
337 guardaddr
= attr
->__stackaddr
+ attr
->__stacksize
;
340 /* The user provided a stack. For now we interpret the supplied
341 address as 1 + the highest addr. in the stack segment. If a
342 separate register stack is needed, we place it at the low end
343 of the segment, relying on the associated stacksize to
344 determine the low end of the segment. This differs from many
345 (but not all) other pthreads implementations. The intent is
346 that on machines with a single stack growing toward higher
347 addresses, stackaddr would be the lowest address in the stack
348 segment, so that it is consistently close to the initial sp
351 (pthread_descr
) ((long)(attr
->__stackaddr
) & -sizeof(void *)) - 1;
352 new_thread_bottom
= (char *) attr
->__stackaddr
- attr
->__stacksize
;
353 guardaddr
= new_thread_bottom
;
357 __pthread_nonstandard_stacks
= 1;
359 /* Clear the thread data structure. */
360 memset (new_thread
, '\0', sizeof (*new_thread
));
364 #ifdef NEED_SEPARATE_REGISTER_STACK
365 size_t granularity
= 2 * pagesize
;
366 /* Try to make stacksize/2 a multiple of pagesize */
368 size_t granularity
= pagesize
;
372 /* Allocate space for stack and thread descriptor at default address */
376 guardsize
= page_roundup (attr
->__guardsize
, granularity
);
377 stacksize
= __pthread_max_stacksize
- guardsize
;
378 stacksize
= MIN (stacksize
,
379 page_roundup (attr
->__stacksize
, granularity
));
383 guardsize
= granularity
;
384 stacksize
= __pthread_max_stacksize
- guardsize
;
387 map_addr
= mmap(NULL
, stacksize
+ guardsize
,
388 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
389 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
390 if (map_addr
== MAP_FAILED
)
391 /* No more memory available. */
394 # ifdef NEED_SEPARATE_REGISTER_STACK
395 guardaddr
= map_addr
+ stacksize
/ 2;
397 mprotect (guardaddr
, guardsize
, PROT_NONE
);
399 new_thread_bottom
= (char *) map_addr
;
400 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
402 # elif _STACK_GROWS_DOWN
403 guardaddr
= map_addr
;
405 mprotect (guardaddr
, guardsize
, PROT_NONE
);
407 new_thread_bottom
= (char *) map_addr
+ guardsize
;
408 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
)) - 1;
409 # elif _STACK_GROWS_UP
410 guardaddr
= map_addr
+ stacksize
;
412 mprotect (guardaddr
, guardsize
, PROT_NONE
);
414 new_thread
= (pthread_descr
) map_addr
;
415 new_thread_bottom
= (char *) (new_thread
+ 1);
417 # error You must define a stack direction
418 # endif /* Stack direction */
419 #else /* !FLOATING_STACKS */
424 guardsize
= page_roundup (attr
->__guardsize
, granularity
);
425 stacksize
= STACK_SIZE
- guardsize
;
426 stacksize
= MIN (stacksize
,
427 page_roundup (attr
->__stacksize
, granularity
));
431 guardsize
= granularity
;
432 stacksize
= STACK_SIZE
- granularity
;
435 # ifdef NEED_SEPARATE_REGISTER_STACK
436 new_thread
= default_new_thread
;
437 new_thread_bottom
= (char *) (new_thread
+ 1) - stacksize
- guardsize
;
438 /* Includes guard area, unlike the normal case. Use the bottom
439 end of the segment as backing store for the register stack.
440 Needed on IA64. In this case, we also map the entire stack at
441 once. According to David Mosberger, that's cheaper. It also
442 avoids the risk of intermittent failures due to other mappings
443 in the same region. The cost is that we might be able to map
444 slightly fewer stacks. */
446 /* First the main stack: */
447 map_addr
= (caddr_t
)((char *)(new_thread
+ 1) - stacksize
/ 2);
448 res_addr
= mmap(map_addr
, stacksize
/ 2,
449 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
450 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
451 if (res_addr
!= map_addr
)
453 /* Bad luck, this segment is already mapped. */
454 if (res_addr
!= MAP_FAILED
)
455 munmap(res_addr
, stacksize
/ 2);
458 /* Then the register stack: */
459 map_addr
= (caddr_t
)new_thread_bottom
;
460 res_addr
= mmap(map_addr
, stacksize
/2,
461 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
462 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
463 if (res_addr
!= map_addr
)
465 if (res_addr
!= MAP_FAILED
)
466 munmap(res_addr
, stacksize
/ 2);
467 munmap((caddr_t
)((char *)(new_thread
+ 1) - stacksize
/2),
472 guardaddr
= new_thread_bottom
+ stacksize
/2;
473 /* We leave the guard area in the middle unmapped. */
474 # else /* !NEED_SEPARATE_REGISTER_STACK */
475 # ifdef _STACK_GROWS_DOWN
476 new_thread
= default_new_thread
;
477 new_thread_bottom
= (char *) (new_thread
+ 1) - stacksize
;
478 map_addr
= new_thread_bottom
- guardsize
;
479 res_addr
= mmap(map_addr
, stacksize
+ guardsize
,
480 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
481 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
482 if (res_addr
!= map_addr
)
484 /* Bad luck, this segment is already mapped. */
485 if (res_addr
!= MAP_FAILED
)
486 munmap (res_addr
, stacksize
+ guardsize
);
490 /* We manage to get a stack. Protect the guard area pages if
492 guardaddr
= map_addr
;
494 mprotect (guardaddr
, guardsize
, PROT_NONE
);
496 /* The thread description goes at the bottom of this area, and
497 * the stack starts directly above it.
499 new_thread
= (pthread_descr
)((unsigned long)default_new_thread
&~ (STACK_SIZE
- 1));
500 map_addr
= mmap(new_thread
, stacksize
+ guardsize
,
501 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
502 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
503 if (map_addr
== MAP_FAILED
)
506 new_thread_bottom
= map_addr
+ sizeof(*new_thread
);
507 guardaddr
= map_addr
+ stacksize
;
509 mprotect (guardaddr
, guardsize
, PROT_NONE
);
511 # endif /* stack direction */
512 # endif /* !NEED_SEPARATE_REGISTER_STACK */
513 #endif /* !FLOATING_STACKS */
515 *out_new_thread
= new_thread
;
516 *out_new_thread_bottom
= new_thread_bottom
;
517 *out_guardaddr
= guardaddr
;
518 *out_guardsize
= guardsize
;
522 static int pthread_handle_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
523 void * (*start_routine
)(void *), void *arg
,
524 sigset_t
* mask
, int father_pid
,
526 td_thr_events_t
*event_maskp
)
530 pthread_descr new_thread
;
531 char * new_thread_bottom
;
532 pthread_t new_thread_id
;
533 char *guardaddr
= NULL
;
534 size_t guardsize
= 0;
535 int pagesize
= __getpagesize();
538 /* First check whether we have to change the policy and if yes, whether
539 we can do this. Normally this should be done by examining the
540 return value of the __sched_setscheduler call in pthread_start_thread
541 but this is hard to implement. FIXME */
542 if (attr
!= NULL
&& attr
->__schedpolicy
!= SCHED_OTHER
&& geteuid () != 0)
544 /* Find a free segment for the thread, and allocate a stack if needed */
545 for (sseg
= 2; ; sseg
++)
547 if (sseg
>= PTHREAD_THREADS_MAX
)
549 if (__pthread_handles
[sseg
].h_descr
!= NULL
)
551 if (pthread_allocate_stack(attr
, thread_segment(sseg
),
553 &new_thread
, &new_thread_bottom
,
554 &guardaddr
, &guardsize
) == 0)
557 __pthread_handles_num
++;
558 /* Allocate new thread identifier */
559 pthread_threads_counter
+= PTHREAD_THREADS_MAX
;
560 new_thread_id
= sseg
+ pthread_threads_counter
;
561 /* Initialize the thread descriptor. Elements which have to be
562 initialized to zero already have this value. */
563 new_thread
->p_tid
= new_thread_id
;
564 new_thread
->p_lock
= &(__pthread_handles
[sseg
].h_lock
);
565 new_thread
->p_cancelstate
= PTHREAD_CANCEL_ENABLE
;
566 new_thread
->p_canceltype
= PTHREAD_CANCEL_DEFERRED
;
567 new_thread
->p_errnop
= &new_thread
->p_errno
;
568 new_thread
->p_h_errnop
= &new_thread
->p_h_errno
;
569 new_thread
->p_resp
= &new_thread
->p_res
;
570 new_thread
->p_guardaddr
= guardaddr
;
571 new_thread
->p_guardsize
= guardsize
;
572 new_thread
->p_header
.data
.self
= new_thread
;
573 new_thread
->p_nr
= sseg
;
574 new_thread
->p_inheritsched
= attr
? attr
->__inheritsched
: 0;
575 /* Initialize the thread handle */
576 __pthread_init_lock(&__pthread_handles
[sseg
].h_lock
);
577 __pthread_handles
[sseg
].h_descr
= new_thread
;
578 __pthread_handles
[sseg
].h_bottom
= new_thread_bottom
;
579 /* Determine scheduling parameters for the thread */
580 new_thread
->p_start_args
.schedpolicy
= -1;
582 new_thread
->p_detached
= attr
->__detachstate
;
583 new_thread
->p_userstack
= attr
->__stackaddr_set
;
585 switch(attr
->__inheritsched
) {
586 case PTHREAD_EXPLICIT_SCHED
:
587 new_thread
->p_start_args
.schedpolicy
= attr
->__schedpolicy
;
588 memcpy (&new_thread
->p_start_args
.schedparam
, &attr
->__schedparam
,
589 sizeof (struct sched_param
));
591 case PTHREAD_INHERIT_SCHED
:
592 new_thread
->p_start_args
.schedpolicy
= __sched_getscheduler(father_pid
);
593 __sched_getparam(father_pid
, &new_thread
->p_start_args
.schedparam
);
596 new_thread
->p_priority
=
597 new_thread
->p_start_args
.schedparam
.sched_priority
;
599 /* Finish setting up arguments to pthread_start_thread */
600 new_thread
->p_start_args
.start_routine
= start_routine
;
601 new_thread
->p_start_args
.arg
= arg
;
602 new_thread
->p_start_args
.mask
= *mask
;
603 /* Make the new thread ID available already now. If any of the later
604 functions fail we return an error value and the caller must not use
605 the stored thread ID. */
606 *thread
= new_thread_id
;
607 /* Raise priority of thread manager if needed */
608 __pthread_manager_adjust_prio(new_thread
->p_priority
);
609 /* Do the cloning. We have to use two different functions depending
610 on whether we are debugging or not. */
611 pid
= 0; /* Note that the thread never can have PID zero. */
614 /* See whether the TD_CREATE event bit is set in any of the
616 int idx
= __td_eventword (TD_CREATE
);
617 uint32_t mask
= __td_eventmask (TD_CREATE
);
619 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
620 | event_maskp
->event_bits
[idx
])) != 0)
622 /* Lock the mutex the child will use now so that it will stop. */
623 __pthread_lock(new_thread
->p_lock
, NULL
);
625 /* We have to report this event. */
626 #ifdef NEED_SEPARATE_REGISTER_STACK
627 /* Perhaps this version should be used on all platforms. But
628 this requires that __clone2 be uniformly supported
631 And there is some argument for changing the __clone2
632 interface to pass sp and bsp instead, making it more IA64
633 specific, but allowing stacks to grow outward from each
634 other, to get less paging and fewer mmaps. */
635 pid
= __clone2(pthread_start_thread_event
,
636 (void **)new_thread_bottom
,
637 (char *)new_thread
- new_thread_bottom
,
638 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
639 __pthread_sig_cancel
, new_thread
);
640 #elif _STACK_GROWS_UP
641 pid
= __clone(pthread_start_thread_event
, (void **) new_thread_bottom
,
642 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
643 __pthread_sig_cancel
, new_thread
);
645 pid
= __clone(pthread_start_thread_event
, (void **) new_thread
,
646 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
647 __pthread_sig_cancel
, new_thread
);
652 /* Now fill in the information about the new thread in
653 the newly created thread's data structure. We cannot let
654 the new thread do this since we don't know whether it was
655 already scheduled when we send the event. */
656 new_thread
->p_eventbuf
.eventdata
= new_thread
;
657 new_thread
->p_eventbuf
.eventnum
= TD_CREATE
;
658 __pthread_last_event
= new_thread
;
660 /* We have to set the PID here since the callback function
661 in the debug library will need it and we cannot guarantee
662 the child got scheduled before the debugger. */
663 new_thread
->p_pid
= pid
;
665 /* Now call the function which signals the event. */
666 __linuxthreads_create_event ();
668 /* Now restart the thread. */
669 __pthread_unlock(new_thread
->p_lock
);
675 #ifdef NEED_SEPARATE_REGISTER_STACK
676 pid
= __clone2(pthread_start_thread
,
677 (void **)new_thread_bottom
,
678 (char *)new_thread
- new_thread_bottom
,
679 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
680 __pthread_sig_cancel
, new_thread
);
681 #elif _STACK_GROWS_UP
682 pid
= __clone(pthread_start_thread
, (void **) new_thread_bottom
,
683 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
684 __pthread_sig_cancel
, new_thread
);
686 pid
= __clone(pthread_start_thread
, (void **) new_thread
,
687 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
688 __pthread_sig_cancel
, new_thread
);
689 #endif /* !NEED_SEPARATE_REGISTER_STACK */
692 /* Check if cloning succeeded */
694 /* Free the stack if we allocated it */
695 if (attr
== NULL
|| !attr
->__stackaddr_set
)
697 #ifdef NEED_SEPARATE_REGISTER_STACK
698 size_t stacksize
= ((char *)(new_thread
->p_guardaddr
)
699 - new_thread_bottom
);
700 munmap((caddr_t
)new_thread_bottom
,
701 2 * stacksize
+ new_thread
->p_guardsize
);
702 #elif _STACK_GROWS_UP
703 size_t stacksize
= guardaddr
- (char *)new_thread
;
704 munmap(new_thread
, stacksize
+ guardsize
);
706 size_t stacksize
= (char *)(new_thread
+1) - new_thread_bottom
;
707 munmap(new_thread_bottom
- guardsize
, guardsize
+ stacksize
);
710 __pthread_handles
[sseg
].h_descr
= NULL
;
711 __pthread_handles
[sseg
].h_bottom
= NULL
;
712 __pthread_handles_num
--;
715 /* Insert new thread in doubly linked list of active threads */
716 new_thread
->p_prevlive
= __pthread_main_thread
;
717 new_thread
->p_nextlive
= __pthread_main_thread
->p_nextlive
;
718 __pthread_main_thread
->p_nextlive
->p_prevlive
= new_thread
;
719 __pthread_main_thread
->p_nextlive
= new_thread
;
720 /* Set pid field of the new thread, in case we get there before the
722 new_thread
->p_pid
= pid
;
727 /* Try to free the resources of a thread when requested by pthread_join
728 or pthread_detach on a terminated thread. */
730 static void pthread_free(pthread_descr th
)
732 pthread_handle handle
;
733 pthread_readlock_info
*iter
, *next
;
735 ASSERT(th
->p_exited
);
736 /* Make the handle invalid */
737 handle
= thread_handle(th
->p_tid
);
738 __pthread_lock(&handle
->h_lock
, NULL
);
739 handle
->h_descr
= NULL
;
740 handle
->h_bottom
= (char *)(-1L);
741 __pthread_unlock(&handle
->h_lock
);
743 FREE_THREAD(th
, th
->p_nr
);
745 /* One fewer threads in __pthread_handles */
746 __pthread_handles_num
--;
748 /* Destroy read lock list, and list of free read lock structures.
749 If the former is not empty, it means the thread exited while
750 holding read locks! */
752 for (iter
= th
->p_readlock_list
; iter
!= NULL
; iter
= next
)
754 next
= iter
->pr_next
;
758 for (iter
= th
->p_readlock_free
; iter
!= NULL
; iter
= next
)
760 next
= iter
->pr_next
;
764 /* If initial thread, nothing to free */
765 if (!th
->p_userstack
)
767 size_t guardsize
= th
->p_guardsize
;
768 /* Free the stack and thread descriptor area */
769 char *guardaddr
= th
->p_guardaddr
;
770 #ifdef _STACK_GROWS_UP
771 size_t stacksize
= guardaddr
- (char *)th
;
772 guardaddr
= (char *)th
;
774 /* Guardaddr is always set, even if guardsize is 0. This allows
775 us to compute everything else. */
776 size_t stacksize
= (char *)(th
+1) - guardaddr
- guardsize
;
777 #ifdef NEED_SEPARATE_REGISTER_STACK
778 /* Take account of the register stack, which is below guardaddr. */
779 guardaddr
-= stacksize
;
783 /* Unmap the stack. */
784 munmap(guardaddr
, stacksize
+ guardsize
);
788 /* Handle threads that have exited */
790 static void pthread_exited(pid_t pid
)
794 /* Find thread with that pid */
795 for (th
= __pthread_main_thread
->p_nextlive
;
796 th
!= __pthread_main_thread
;
797 th
= th
->p_nextlive
) {
798 if (th
->p_pid
== pid
) {
799 /* Remove thread from list of active threads */
800 th
->p_nextlive
->p_prevlive
= th
->p_prevlive
;
801 th
->p_prevlive
->p_nextlive
= th
->p_nextlive
;
802 /* Mark thread as exited, and if detached, free its resources */
803 __pthread_lock(th
->p_lock
, NULL
);
805 /* If we have to signal this event do it now. */
806 if (th
->p_report_events
)
808 /* See whether TD_REAP is in any of the mask. */
809 int idx
= __td_eventword (TD_REAP
);
810 uint32_t mask
= __td_eventmask (TD_REAP
);
812 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
813 | th
->p_eventbuf
.eventmask
.event_bits
[idx
])) != 0)
815 /* Yep, we have to signal the reapage. */
816 th
->p_eventbuf
.eventnum
= TD_REAP
;
817 th
->p_eventbuf
.eventdata
= th
;
818 __pthread_last_event
= th
;
820 /* Now call the function to signal the event. */
821 __linuxthreads_reap_event();
824 detached
= th
->p_detached
;
825 __pthread_unlock(th
->p_lock
);
831 /* If all threads have exited and the main thread is pending on a
832 pthread_exit, wake up the main thread and terminate ourselves. */
833 if (main_thread_exiting
&&
834 __pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
835 restart(__pthread_main_thread
);
836 /* Same logic as REQ_MAIN_THREAD_EXIT. */
840 static void pthread_reap_children(void)
845 while ((pid
= __libc_waitpid(-1, &status
, WNOHANG
| __WCLONE
)) > 0) {
847 if (WIFSIGNALED(status
)) {
848 /* If a thread died due to a signal, send the same signal to
849 all other threads, including the main thread. */
850 pthread_kill_all_threads(WTERMSIG(status
), 1);
856 /* Try to free the resources of a thread when requested by pthread_join
857 or pthread_detach on a terminated thread. */
859 static void pthread_handle_free(pthread_t th_id
)
861 pthread_handle handle
= thread_handle(th_id
);
864 __pthread_lock(&handle
->h_lock
, NULL
);
865 if (nonexisting_handle(handle
, th_id
)) {
866 /* pthread_reap_children has deallocated the thread already,
867 nothing needs to be done */
868 __pthread_unlock(&handle
->h_lock
);
871 th
= handle
->h_descr
;
873 __pthread_unlock(&handle
->h_lock
);
876 /* The Unix process of the thread is still running.
877 Mark the thread as detached so that the thread manager will
878 deallocate its resources when the Unix process exits. */
880 __pthread_unlock(&handle
->h_lock
);
884 /* Send a signal to all running threads */
886 static void pthread_kill_all_threads(int sig
, int main_thread_also
)
889 for (th
= __pthread_main_thread
->p_nextlive
;
890 th
!= __pthread_main_thread
;
891 th
= th
->p_nextlive
) {
892 kill(th
->p_pid
, sig
);
894 if (main_thread_also
) {
895 kill(__pthread_main_thread
->p_pid
, sig
);
899 static void pthread_for_each_thread(void *arg
,
900 void (*fn
)(void *, pthread_descr
))
904 for (th
= __pthread_main_thread
->p_nextlive
;
905 th
!= __pthread_main_thread
;
906 th
= th
->p_nextlive
) {
910 fn(arg
, __pthread_main_thread
);
913 /* Process-wide exit() */
915 static void pthread_handle_exit(pthread_descr issuing_thread
, int exitcode
)
918 __pthread_exit_requested
= 1;
919 __pthread_exit_code
= exitcode
;
920 /* A forced asynchronous cancellation follows. Make sure we won't
921 get stuck later in the main thread with a system lock being held
922 by one of the cancelled threads. Ideally one would use the same
923 code as in pthread_atfork(), but we can't distinguish system and
924 user handlers there. */
926 /* Send the CANCEL signal to all running threads, including the main
927 thread, but excluding the thread from which the exit request originated
928 (that thread must complete the exit, e.g. calling atexit functions
929 and flushing stdio buffers). */
930 for (th
= issuing_thread
->p_nextlive
;
931 th
!= issuing_thread
;
932 th
= th
->p_nextlive
) {
933 kill(th
->p_pid
, __pthread_sig_cancel
);
935 /* Now, wait for all these threads, so that they don't become zombies
936 and their times are properly added to the thread manager's times. */
937 for (th
= issuing_thread
->p_nextlive
;
938 th
!= issuing_thread
;
939 th
= th
->p_nextlive
) {
940 waitpid(th
->p_pid
, NULL
, __WCLONE
);
943 restart(issuing_thread
);
947 /* Handler for __pthread_sig_cancel in thread manager thread */
949 void __pthread_manager_sighandler(int sig
)
951 int kick_manager
= terminated_children
== 0 && main_thread_exiting
;
952 terminated_children
= 1;
954 /* If the main thread is terminating, kick the thread manager loop
955 each time some threads terminate. This eliminates a two second
956 shutdown delay caused by the thread manager sleeping in the
957 call to __poll(). Instead, the thread manager is kicked into
958 action, reaps the outstanding threads and resumes the main thread
959 so that it can complete the shutdown. */
962 struct pthread_request request
;
963 request
.req_thread
= 0;
964 request
.req_kind
= REQ_KICK
;
965 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
966 (char *) &request
, sizeof(request
)));
970 /* Adjust priority of thread manager so that it always run at a priority
971 higher than all threads */
973 void __pthread_manager_adjust_prio(int thread_prio
)
975 struct sched_param param
;
977 if (thread_prio
<= __pthread_manager_thread
.p_priority
) return;
978 param
.sched_priority
=
979 thread_prio
< __sched_get_priority_max(SCHED_FIFO
)
980 ? thread_prio
+ 1 : thread_prio
;
981 __sched_setscheduler(__pthread_manager_thread
.p_pid
, SCHED_FIFO
, ¶m
);
982 __pthread_manager_thread
.p_priority
= thread_prio
;