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 */
30 #include <locale.h> /* for __uselocale */
34 #include "internals.h"
37 #include "semaphore.h"
39 /* For debugging purposes put the maximum number of threads in a variable. */
40 const int __linuxthreads_pthread_threads_max
= PTHREAD_THREADS_MAX
;
43 /* Indicate whether at least one thread has a user-defined stack (if 1),
44 or if all threads have stacks supplied by LinuxThreads (if 0). */
45 int __pthread_nonstandard_stacks
;
48 /* Number of active entries in __pthread_handles (used by gdb) */
49 volatile int __pthread_handles_num
= 2;
51 /* Whether to use debugger additional actions for thread creation
53 volatile int __pthread_threads_debug
;
55 /* Globally enabled events. */
56 volatile td_thr_events_t __pthread_threads_events
;
58 /* Pointer to thread descriptor with last event. */
59 volatile pthread_descr __pthread_last_event
;
61 static pthread_descr manager_thread
;
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 pthread_descr self
= manager_thread
= arg
;
113 int reqfd
= __pthread_manager_reader
;
115 sigset_t manager_mask
;
117 struct pthread_request request
;
119 /* If we have special thread_self processing, initialize it. */
120 #ifdef INIT_THREAD_SELF
121 INIT_THREAD_SELF(self
, 1);
123 #if !(USE_TLS && HAVE___THREAD)
124 /* Set the error variable. */
125 self
->p_errnop
= &self
->p_errno
;
126 self
->p_h_errnop
= &self
->p_h_errno
;
128 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
129 sigfillset(&manager_mask
);
130 sigdelset(&manager_mask
, __pthread_sig_cancel
); /* for thread termination */
131 sigdelset(&manager_mask
, SIGTRAP
); /* for debugging purposes */
132 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0)
133 sigdelset(&manager_mask
, __pthread_sig_debug
);
134 sigprocmask(SIG_SETMASK
, &manager_mask
, NULL
);
135 /* Raise our priority to match that of main thread */
136 __pthread_manager_adjust_prio(__pthread_main_thread
->p_priority
);
137 /* Synchronize debugging of the thread manager */
138 n
= TEMP_FAILURE_RETRY(__libc_read(reqfd
, (char *)&request
,
140 ASSERT(n
== sizeof(request
) && request
.req_kind
== REQ_DEBUG
);
143 /* Enter server loop */
145 n
= __poll(&ufd
, 1, 2000);
147 /* Check for termination of the main thread */
148 if (getppid() == 1) {
149 pthread_kill_all_threads(SIGKILL
, 0);
152 /* Check for dead children */
153 if (terminated_children
) {
154 terminated_children
= 0;
155 pthread_reap_children();
157 /* Read and execute request */
158 if (n
== 1 && (ufd
.revents
& POLLIN
)) {
159 n
= TEMP_FAILURE_RETRY(__libc_read(reqfd
, (char *)&request
,
164 write(STDERR_FILENO
, d
, snprintf(d
, sizeof(d
), "*** read err %m\n"));
165 } else if (n
!= sizeof(request
)) {
166 write(STDERR_FILENO
, "*** short read in manager\n", 26);
170 switch(request
.req_kind
) {
172 request
.req_thread
->p_retcode
=
173 pthread_handle_create((pthread_t
*) &request
.req_thread
->p_retval
,
174 request
.req_args
.create
.attr
,
175 request
.req_args
.create
.fn
,
176 request
.req_args
.create
.arg
,
177 &request
.req_args
.create
.mask
,
178 request
.req_thread
->p_pid
,
179 request
.req_thread
->p_report_events
,
180 &request
.req_thread
->p_eventbuf
.eventmask
);
181 restart(request
.req_thread
);
184 pthread_handle_free(request
.req_args
.free
.thread_id
);
186 case REQ_PROCESS_EXIT
:
187 pthread_handle_exit(request
.req_thread
,
188 request
.req_args
.exit
.code
);
191 case REQ_MAIN_THREAD_EXIT
:
192 main_thread_exiting
= 1;
193 /* Reap children in case all other threads died and the signal handler
194 went off before we set main_thread_exiting to 1, and therefore did
196 pthread_reap_children();
198 if (__pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
199 restart(__pthread_main_thread
);
200 /* The main thread will now call exit() which will trigger an
201 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
202 to the thread manager. In case you are wondering how the
203 manager terminates from its loop here. */
207 __new_sem_post(request
.req_args
.post
);
210 /* Make gdb aware of new thread and gdb will restart the
211 new thread when it is ready to handle the new thread. */
212 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0)
213 raise(__pthread_sig_debug
);
216 /* This is just a prod to get the manager to reap some
217 threads right away, avoiding a potential delay at shutdown. */
219 case REQ_FOR_EACH_THREAD
:
220 pthread_for_each_thread(request
.req_args
.for_each
.arg
,
221 request
.req_args
.for_each
.fn
);
222 restart(request
.req_thread
);
229 int __pthread_manager_event(void *arg
)
231 pthread_descr self
= arg
;
232 /* If we have special thread_self processing, initialize it. */
233 #ifdef INIT_THREAD_SELF
234 INIT_THREAD_SELF(self
, 1);
237 /* Get the lock the manager will free once all is correctly set up. */
238 __pthread_lock (THREAD_GETMEM(self
, p_lock
), NULL
);
239 /* Free it immediately. */
240 __pthread_unlock (THREAD_GETMEM(self
, p_lock
));
242 return __pthread_manager(arg
);
245 /* Process creation */
248 __attribute__ ((noreturn
))
249 pthread_start_thread(void *arg
)
251 pthread_descr self
= (pthread_descr
) arg
;
252 struct pthread_request request
;
255 hp_timing_t tmpclock
;
257 /* Initialize special thread_self processing, if any. */
258 #ifdef INIT_THREAD_SELF
259 INIT_THREAD_SELF(self
, self
->p_nr
);
262 HP_TIMING_NOW (tmpclock
);
263 THREAD_SETMEM (self
, p_cpuclock_offset
, tmpclock
);
265 /* Make sure our pid field is initialized, just in case we get there
266 before our father has initialized it. */
267 THREAD_SETMEM(self
, p_pid
, __getpid());
268 /* Initial signal mask is that of the creating thread. (Otherwise,
269 we'd just inherit the mask of the thread manager.) */
270 sigprocmask(SIG_SETMASK
, &self
->p_start_args
.mask
, NULL
);
271 /* Set the scheduling policy and priority for the new thread, if needed */
272 if (THREAD_GETMEM(self
, p_start_args
.schedpolicy
) >= 0)
273 /* Explicit scheduling attributes were provided: apply them */
274 __sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
275 THREAD_GETMEM(self
, p_start_args
.schedpolicy
),
276 &self
->p_start_args
.schedparam
);
277 else if (manager_thread
->p_priority
> 0)
278 /* Default scheduling required, but thread manager runs in realtime
279 scheduling: switch new thread to SCHED_OTHER policy */
281 struct sched_param default_params
;
282 default_params
.sched_priority
= 0;
283 __sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
284 SCHED_OTHER
, &default_params
);
286 #if !(USE_TLS && HAVE___THREAD) && defined SHARED
287 /* Initialize thread-locale current locale to point to the global one.
288 With __thread support, the variable's initializer takes care of this. */
289 __uselocale (LC_GLOBAL_LOCALE
);
291 /* Make gdb aware of new thread */
292 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0) {
293 request
.req_thread
= self
;
294 request
.req_kind
= REQ_DEBUG
;
295 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
296 (char *) &request
, sizeof(request
)));
299 /* Run the thread code */
300 outcome
= self
->p_start_args
.start_routine(THREAD_GETMEM(self
,
302 /* Exit with the given return value */
303 __pthread_do_exit(outcome
, CURRENT_STACK_FRAME
);
307 __attribute__ ((noreturn
))
308 pthread_start_thread_event(void *arg
)
310 pthread_descr self
= (pthread_descr
) arg
;
312 #ifdef INIT_THREAD_SELF
313 INIT_THREAD_SELF(self
, self
->p_nr
);
315 /* Make sure our pid field is initialized, just in case we get there
316 before our father has initialized it. */
317 THREAD_SETMEM(self
, p_pid
, __getpid());
318 /* Get the lock the manager will free once all is correctly set up. */
319 __pthread_lock (THREAD_GETMEM(self
, p_lock
), NULL
);
320 /* Free it immediately. */
321 __pthread_unlock (THREAD_GETMEM(self
, p_lock
));
323 /* Continue with the real function. */
324 pthread_start_thread (arg
);
327 #if defined USE_TLS && !FLOATING_STACKS
328 # error "TLS can only work with floating stacks"
331 static int pthread_allocate_stack(const pthread_attr_t
*attr
,
332 pthread_descr default_new_thread
,
334 char ** out_new_thread
,
335 char ** out_new_thread_bottom
,
336 char ** out_guardaddr
,
337 size_t * out_guardsize
)
339 pthread_descr new_thread
;
340 char * new_thread_bottom
;
342 size_t stacksize
, guardsize
;
345 /* TLS cannot work with fixed thread descriptor addresses. */
346 assert (default_new_thread
== NULL
);
349 if (attr
!= NULL
&& attr
->__stackaddr_set
)
351 #ifdef _STACK_GROWS_UP
352 /* The user provided a stack. */
354 /* This value is not needed. */
355 new_thread
= (pthread_descr
) attr
->__stackaddr
;
356 new_thread_bottom
= (char *) new_thread
;
358 new_thread
= (pthread_descr
) attr
->__stackaddr
;
359 new_thread_bottom
= (char *) (new_thread
+ 1);
361 guardaddr
= attr
->__stackaddr
+ attr
->__stacksize
;
364 /* The user provided a stack. For now we interpret the supplied
365 address as 1 + the highest addr. in the stack segment. If a
366 separate register stack is needed, we place it at the low end
367 of the segment, relying on the associated stacksize to
368 determine the low end of the segment. This differs from many
369 (but not all) other pthreads implementations. The intent is
370 that on machines with a single stack growing toward higher
371 addresses, stackaddr would be the lowest address in the stack
372 segment, so that it is consistently close to the initial sp
375 new_thread
= (pthread_descr
) attr
->__stackaddr
;
378 (pthread_descr
) ((long)(attr
->__stackaddr
) & -sizeof(void *)) - 1;
380 new_thread_bottom
= (char *) attr
->__stackaddr
- attr
->__stacksize
;
381 guardaddr
= new_thread_bottom
;
385 __pthread_nonstandard_stacks
= 1;
388 /* Clear the thread data structure. */
389 memset (new_thread
, '\0', sizeof (*new_thread
));
394 #ifdef NEED_SEPARATE_REGISTER_STACK
395 const size_t granularity
= 2 * pagesize
;
396 /* Try to make stacksize/2 a multiple of pagesize */
398 const size_t granularity
= pagesize
;
402 /* Allocate space for stack and thread descriptor at default address */
406 guardsize
= page_roundup (attr
->__guardsize
, granularity
);
407 stacksize
= __pthread_max_stacksize
- guardsize
;
408 stacksize
= MIN (stacksize
,
409 page_roundup (attr
->__stacksize
, granularity
));
413 guardsize
= granularity
;
414 stacksize
= __pthread_max_stacksize
- guardsize
;
417 map_addr
= mmap(NULL
, stacksize
+ guardsize
,
418 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
419 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
420 if (map_addr
== MAP_FAILED
)
421 /* No more memory available. */
424 # ifdef NEED_SEPARATE_REGISTER_STACK
425 guardaddr
= map_addr
+ stacksize
/ 2;
427 mprotect (guardaddr
, guardsize
, PROT_NONE
);
429 new_thread_bottom
= (char *) map_addr
;
431 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
434 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
437 # elif _STACK_GROWS_DOWN
438 guardaddr
= map_addr
;
440 mprotect (guardaddr
, guardsize
, PROT_NONE
);
442 new_thread_bottom
= (char *) map_addr
+ guardsize
;
444 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
));
446 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
)) - 1;
448 # elif _STACK_GROWS_UP
449 guardaddr
= map_addr
+ stacksize
;
451 mprotect (guardaddr
, guardsize
, PROT_NONE
);
453 new_thread
= (pthread_descr
) map_addr
;
455 new_thread_bottom
= (char *) new_thread
;
457 new_thread_bottom
= (char *) (new_thread
+ 1);
460 # error You must define a stack direction
461 # endif /* Stack direction */
462 #else /* !FLOATING_STACKS */
467 guardsize
= page_roundup (attr
->__guardsize
, granularity
);
468 stacksize
= STACK_SIZE
- guardsize
;
469 stacksize
= MIN (stacksize
,
470 page_roundup (attr
->__stacksize
, granularity
));
474 guardsize
= granularity
;
475 stacksize
= STACK_SIZE
- granularity
;
478 # ifdef NEED_SEPARATE_REGISTER_STACK
479 new_thread
= default_new_thread
;
480 new_thread_bottom
= (char *) (new_thread
+ 1) - stacksize
- guardsize
;
481 /* Includes guard area, unlike the normal case. Use the bottom
482 end of the segment as backing store for the register stack.
483 Needed on IA64. In this case, we also map the entire stack at
484 once. According to David Mosberger, that's cheaper. It also
485 avoids the risk of intermittent failures due to other mappings
486 in the same region. The cost is that we might be able to map
487 slightly fewer stacks. */
489 /* First the main stack: */
490 map_addr
= (caddr_t
)((char *)(new_thread
+ 1) - stacksize
/ 2);
491 res_addr
= mmap(map_addr
, stacksize
/ 2,
492 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
493 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
494 if (res_addr
!= map_addr
)
496 /* Bad luck, this segment is already mapped. */
497 if (res_addr
!= MAP_FAILED
)
498 munmap(res_addr
, stacksize
/ 2);
501 /* Then the register stack: */
502 map_addr
= (caddr_t
)new_thread_bottom
;
503 res_addr
= mmap(map_addr
, stacksize
/2,
504 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
505 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
506 if (res_addr
!= map_addr
)
508 if (res_addr
!= MAP_FAILED
)
509 munmap(res_addr
, stacksize
/ 2);
510 munmap((caddr_t
)((char *)(new_thread
+ 1) - stacksize
/2),
515 guardaddr
= new_thread_bottom
+ stacksize
/2;
516 /* We leave the guard area in the middle unmapped. */
517 # else /* !NEED_SEPARATE_REGISTER_STACK */
518 # ifdef _STACK_GROWS_DOWN
519 new_thread
= default_new_thread
;
520 new_thread_bottom
= (char *) (new_thread
+ 1) - stacksize
;
521 map_addr
= new_thread_bottom
- guardsize
;
522 res_addr
= mmap(map_addr
, stacksize
+ guardsize
,
523 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
524 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
525 if (res_addr
!= map_addr
)
527 /* Bad luck, this segment is already mapped. */
528 if (res_addr
!= MAP_FAILED
)
529 munmap (res_addr
, stacksize
+ guardsize
);
533 /* We manage to get a stack. Protect the guard area pages if
535 guardaddr
= map_addr
;
537 mprotect (guardaddr
, guardsize
, PROT_NONE
);
539 /* The thread description goes at the bottom of this area, and
540 * the stack starts directly above it.
542 new_thread
= (pthread_descr
)((unsigned long)default_new_thread
&~ (STACK_SIZE
- 1));
543 map_addr
= mmap(new_thread
, stacksize
+ guardsize
,
544 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
545 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
546 if (map_addr
== MAP_FAILED
)
549 new_thread_bottom
= map_addr
+ sizeof(*new_thread
);
550 guardaddr
= map_addr
+ stacksize
;
552 mprotect (guardaddr
, guardsize
, PROT_NONE
);
554 # endif /* stack direction */
555 # endif /* !NEED_SEPARATE_REGISTER_STACK */
556 #endif /* !FLOATING_STACKS */
558 *out_new_thread
= (char *) new_thread
;
559 *out_new_thread_bottom
= new_thread_bottom
;
560 *out_guardaddr
= guardaddr
;
561 *out_guardsize
= guardsize
;
565 static int pthread_handle_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
566 void * (*start_routine
)(void *), void *arg
,
567 sigset_t
* mask
, int father_pid
,
569 td_thr_events_t
*event_maskp
)
573 pthread_descr new_thread
;
575 char * new_thread_bottom
;
576 pthread_t new_thread_id
;
577 char *guardaddr
= NULL
;
578 size_t guardsize
= 0;
579 int pagesize
= __getpagesize();
583 new_thread
= _dl_allocate_tls (NULL
);
584 if (new_thread
== NULL
)
587 /* Prevent warnings. */
591 /* First check whether we have to change the policy and if yes, whether
592 we can do this. Normally this should be done by examining the
593 return value of the __sched_setscheduler call in pthread_start_thread
594 but this is hard to implement. FIXME */
595 if (attr
!= NULL
&& attr
->__schedpolicy
!= SCHED_OTHER
&& geteuid () != 0)
597 /* Find a free segment for the thread, and allocate a stack if needed */
598 for (sseg
= 2; ; sseg
++)
600 if (sseg
>= PTHREAD_THREADS_MAX
)
603 _dl_deallocate_tls (new_thread
, true);
607 if (__pthread_handles
[sseg
].h_descr
!= NULL
)
609 if (pthread_allocate_stack(attr
, thread_segment(sseg
),
610 pagesize
, &stack_addr
, &new_thread_bottom
,
611 &guardaddr
, &guardsize
) == 0)
614 new_thread
->p_stackaddr
= stack_addr
;
616 new_thread
= (pthread_descr
) stack_addr
;
621 __pthread_handles_num
++;
622 /* Allocate new thread identifier */
623 pthread_threads_counter
+= PTHREAD_THREADS_MAX
;
624 new_thread_id
= sseg
+ pthread_threads_counter
;
625 /* Initialize the thread descriptor. Elements which have to be
626 initialized to zero already have this value. */
627 new_thread
->p_header
.data
.tcb
= new_thread
;
628 new_thread
->p_header
.data
.self
= new_thread
;
629 new_thread
->p_tid
= new_thread_id
;
630 new_thread
->p_lock
= &(__pthread_handles
[sseg
].h_lock
);
631 new_thread
->p_cancelstate
= PTHREAD_CANCEL_ENABLE
;
632 new_thread
->p_canceltype
= PTHREAD_CANCEL_DEFERRED
;
633 #if !(USE_TLS && HAVE___THREAD)
634 new_thread
->p_errnop
= &new_thread
->p_errno
;
635 new_thread
->p_h_errnop
= &new_thread
->p_h_errno
;
636 new_thread
->p_resp
= &new_thread
->p_res
;
638 new_thread
->p_guardaddr
= guardaddr
;
639 new_thread
->p_guardsize
= guardsize
;
640 new_thread
->p_nr
= sseg
;
641 new_thread
->p_inheritsched
= attr
? attr
->__inheritsched
: 0;
642 /* Initialize the thread handle */
643 __pthread_init_lock(&__pthread_handles
[sseg
].h_lock
);
644 __pthread_handles
[sseg
].h_descr
= new_thread
;
645 __pthread_handles
[sseg
].h_bottom
= new_thread_bottom
;
646 /* Determine scheduling parameters for the thread */
647 new_thread
->p_start_args
.schedpolicy
= -1;
649 new_thread
->p_detached
= attr
->__detachstate
;
650 new_thread
->p_userstack
= attr
->__stackaddr_set
;
652 switch(attr
->__inheritsched
) {
653 case PTHREAD_EXPLICIT_SCHED
:
654 new_thread
->p_start_args
.schedpolicy
= attr
->__schedpolicy
;
655 memcpy (&new_thread
->p_start_args
.schedparam
, &attr
->__schedparam
,
656 sizeof (struct sched_param
));
658 case PTHREAD_INHERIT_SCHED
:
659 new_thread
->p_start_args
.schedpolicy
= __sched_getscheduler(father_pid
);
660 __sched_getparam(father_pid
, &new_thread
->p_start_args
.schedparam
);
663 new_thread
->p_priority
=
664 new_thread
->p_start_args
.schedparam
.sched_priority
;
666 /* Finish setting up arguments to pthread_start_thread */
667 new_thread
->p_start_args
.start_routine
= start_routine
;
668 new_thread
->p_start_args
.arg
= arg
;
669 new_thread
->p_start_args
.mask
= *mask
;
670 /* Make the new thread ID available already now. If any of the later
671 functions fail we return an error value and the caller must not use
672 the stored thread ID. */
673 *thread
= new_thread_id
;
674 /* Raise priority of thread manager if needed */
675 __pthread_manager_adjust_prio(new_thread
->p_priority
);
676 /* Do the cloning. We have to use two different functions depending
677 on whether we are debugging or not. */
678 pid
= 0; /* Note that the thread never can have PID zero. */
681 /* See whether the TD_CREATE event bit is set in any of the
683 int idx
= __td_eventword (TD_CREATE
);
684 uint32_t mask
= __td_eventmask (TD_CREATE
);
686 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
687 | event_maskp
->event_bits
[idx
])) != 0)
689 /* Lock the mutex the child will use now so that it will stop. */
690 __pthread_lock(new_thread
->p_lock
, NULL
);
692 /* We have to report this event. */
693 #ifdef NEED_SEPARATE_REGISTER_STACK
694 /* Perhaps this version should be used on all platforms. But
695 this requires that __clone2 be uniformly supported
698 And there is some argument for changing the __clone2
699 interface to pass sp and bsp instead, making it more IA64
700 specific, but allowing stacks to grow outward from each
701 other, to get less paging and fewer mmaps. */
702 pid
= __clone2(pthread_start_thread_event
,
703 (void **)new_thread_bottom
,
704 (char *)new_thread
- new_thread_bottom
,
705 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
706 __pthread_sig_cancel
, new_thread
);
707 #elif _STACK_GROWS_UP
708 pid
= __clone(pthread_start_thread_event
, (void **) new_thread_bottom
,
709 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
710 __pthread_sig_cancel
, new_thread
);
712 pid
= __clone(pthread_start_thread_event
, (void **) new_thread
,
713 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
714 __pthread_sig_cancel
, new_thread
);
719 /* Now fill in the information about the new thread in
720 the newly created thread's data structure. We cannot let
721 the new thread do this since we don't know whether it was
722 already scheduled when we send the event. */
723 new_thread
->p_eventbuf
.eventdata
= new_thread
;
724 new_thread
->p_eventbuf
.eventnum
= TD_CREATE
;
725 __pthread_last_event
= new_thread
;
727 /* We have to set the PID here since the callback function
728 in the debug library will need it and we cannot guarantee
729 the child got scheduled before the debugger. */
730 new_thread
->p_pid
= pid
;
732 /* Now call the function which signals the event. */
733 __linuxthreads_create_event ();
735 /* Now restart the thread. */
736 __pthread_unlock(new_thread
->p_lock
);
742 #ifdef NEED_SEPARATE_REGISTER_STACK
743 pid
= __clone2(pthread_start_thread
,
744 (void **)new_thread_bottom
,
745 (char *)stack_addr
- new_thread_bottom
,
746 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
747 __pthread_sig_cancel
, new_thread
);
748 #elif _STACK_GROWS_UP
749 pid
= __clone(pthread_start_thread
, (void *) new_thread_bottom
,
750 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
751 __pthread_sig_cancel
, new_thread
);
753 pid
= __clone(pthread_start_thread
, stack_addr
,
754 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
755 __pthread_sig_cancel
, new_thread
);
756 #endif /* !NEED_SEPARATE_REGISTER_STACK */
759 /* Check if cloning succeeded */
761 /* Free the stack if we allocated it */
762 if (attr
== NULL
|| !attr
->__stackaddr_set
)
764 #ifdef NEED_SEPARATE_REGISTER_STACK
765 size_t stacksize
= ((char *)(new_thread
->p_guardaddr
)
766 - new_thread_bottom
);
767 munmap((caddr_t
)new_thread_bottom
,
768 2 * stacksize
+ new_thread
->p_guardsize
);
769 #elif _STACK_GROWS_UP
771 size_t stacksize
= guardaddr
- stack_addr
;
772 munmap(stack_addr
, stacksize
+ guardsize
);
774 size_t stacksize
= guardaddr
- (char *)new_thread
;
775 munmap(new_thread
, stacksize
+ guardsize
);
779 size_t stacksize
= stack_addr
- new_thread_bottom
;
781 size_t stacksize
= (char *)(new_thread
+1) - new_thread_bottom
;
783 munmap(new_thread_bottom
- guardsize
, guardsize
+ stacksize
);
787 _dl_deallocate_tls (new_thread
, true);
789 __pthread_handles
[sseg
].h_descr
= NULL
;
790 __pthread_handles
[sseg
].h_bottom
= NULL
;
791 __pthread_handles_num
--;
794 /* Insert new thread in doubly linked list of active threads */
795 new_thread
->p_prevlive
= __pthread_main_thread
;
796 new_thread
->p_nextlive
= __pthread_main_thread
->p_nextlive
;
797 __pthread_main_thread
->p_nextlive
->p_prevlive
= new_thread
;
798 __pthread_main_thread
->p_nextlive
= new_thread
;
799 /* Set pid field of the new thread, in case we get there before the
801 new_thread
->p_pid
= pid
;
806 /* Try to free the resources of a thread when requested by pthread_join
807 or pthread_detach on a terminated thread. */
809 static void pthread_free(pthread_descr th
)
811 pthread_handle handle
;
812 pthread_readlock_info
*iter
, *next
;
814 ASSERT(th
->p_exited
);
815 /* Make the handle invalid */
816 handle
= thread_handle(th
->p_tid
);
817 __pthread_lock(&handle
->h_lock
, NULL
);
818 handle
->h_descr
= NULL
;
819 handle
->h_bottom
= (char *)(-1L);
820 __pthread_unlock(&handle
->h_lock
);
822 FREE_THREAD(th
, th
->p_nr
);
824 /* One fewer threads in __pthread_handles */
825 __pthread_handles_num
--;
827 /* Destroy read lock list, and list of free read lock structures.
828 If the former is not empty, it means the thread exited while
829 holding read locks! */
831 for (iter
= th
->p_readlock_list
; iter
!= NULL
; iter
= next
)
833 next
= iter
->pr_next
;
837 for (iter
= th
->p_readlock_free
; iter
!= NULL
; iter
= next
)
839 next
= iter
->pr_next
;
843 /* If initial thread, nothing to free */
844 if (!th
->p_userstack
)
846 size_t guardsize
= th
->p_guardsize
;
847 /* Free the stack and thread descriptor area */
848 char *guardaddr
= th
->p_guardaddr
;
849 #ifdef _STACK_GROWS_UP
851 size_t stacksize
= guardaddr
- th
->p_stackaddr
;
853 size_t stacksize
= guardaddr
- (char *)th
;
855 guardaddr
= (char *)th
;
857 /* Guardaddr is always set, even if guardsize is 0. This allows
858 us to compute everything else. */
860 size_t stacksize
= th
->p_stackaddr
- guardaddr
- guardsize
;
862 size_t stacksize
= (char *)(th
+1) - guardaddr
- guardsize
;
864 # ifdef NEED_SEPARATE_REGISTER_STACK
865 /* Take account of the register stack, which is below guardaddr. */
866 guardaddr
-= stacksize
;
870 /* Unmap the stack. */
871 munmap(guardaddr
, stacksize
+ guardsize
);
874 _dl_deallocate_tls (th
, true);
879 /* Handle threads that have exited */
881 static void pthread_exited(pid_t pid
)
885 /* Find thread with that pid */
886 for (th
= __pthread_main_thread
->p_nextlive
;
887 th
!= __pthread_main_thread
;
888 th
= th
->p_nextlive
) {
889 if (th
->p_pid
== pid
) {
890 /* Remove thread from list of active threads */
891 th
->p_nextlive
->p_prevlive
= th
->p_prevlive
;
892 th
->p_prevlive
->p_nextlive
= th
->p_nextlive
;
893 /* Mark thread as exited, and if detached, free its resources */
894 __pthread_lock(th
->p_lock
, NULL
);
896 /* If we have to signal this event do it now. */
897 if (th
->p_report_events
)
899 /* See whether TD_REAP is in any of the mask. */
900 int idx
= __td_eventword (TD_REAP
);
901 uint32_t mask
= __td_eventmask (TD_REAP
);
903 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
904 | th
->p_eventbuf
.eventmask
.event_bits
[idx
])) != 0)
906 /* Yep, we have to signal the reapage. */
907 th
->p_eventbuf
.eventnum
= TD_REAP
;
908 th
->p_eventbuf
.eventdata
= th
;
909 __pthread_last_event
= th
;
911 /* Now call the function to signal the event. */
912 __linuxthreads_reap_event();
915 detached
= th
->p_detached
;
916 __pthread_unlock(th
->p_lock
);
922 /* If all threads have exited and the main thread is pending on a
923 pthread_exit, wake up the main thread and terminate ourselves. */
924 if (main_thread_exiting
&&
925 __pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
926 restart(__pthread_main_thread
);
927 /* Same logic as REQ_MAIN_THREAD_EXIT. */
931 static void pthread_reap_children(void)
936 while ((pid
= __libc_waitpid(-1, &status
, WNOHANG
| __WCLONE
)) > 0) {
938 if (WIFSIGNALED(status
)) {
939 /* If a thread died due to a signal, send the same signal to
940 all other threads, including the main thread. */
941 pthread_kill_all_threads(WTERMSIG(status
), 1);
947 /* Try to free the resources of a thread when requested by pthread_join
948 or pthread_detach on a terminated thread. */
950 static void pthread_handle_free(pthread_t th_id
)
952 pthread_handle handle
= thread_handle(th_id
);
955 __pthread_lock(&handle
->h_lock
, NULL
);
956 if (nonexisting_handle(handle
, th_id
)) {
957 /* pthread_reap_children has deallocated the thread already,
958 nothing needs to be done */
959 __pthread_unlock(&handle
->h_lock
);
962 th
= handle
->h_descr
;
964 __pthread_unlock(&handle
->h_lock
);
967 /* The Unix process of the thread is still running.
968 Mark the thread as detached so that the thread manager will
969 deallocate its resources when the Unix process exits. */
971 __pthread_unlock(&handle
->h_lock
);
975 /* Send a signal to all running threads */
977 static void pthread_kill_all_threads(int sig
, int main_thread_also
)
980 for (th
= __pthread_main_thread
->p_nextlive
;
981 th
!= __pthread_main_thread
;
982 th
= th
->p_nextlive
) {
983 kill(th
->p_pid
, sig
);
985 if (main_thread_also
) {
986 kill(__pthread_main_thread
->p_pid
, sig
);
990 static void pthread_for_each_thread(void *arg
,
991 void (*fn
)(void *, pthread_descr
))
995 for (th
= __pthread_main_thread
->p_nextlive
;
996 th
!= __pthread_main_thread
;
997 th
= th
->p_nextlive
) {
1001 fn(arg
, __pthread_main_thread
);
1004 /* Process-wide exit() */
1006 static void pthread_handle_exit(pthread_descr issuing_thread
, int exitcode
)
1009 __pthread_exit_requested
= 1;
1010 __pthread_exit_code
= exitcode
;
1011 /* A forced asynchronous cancellation follows. Make sure we won't
1012 get stuck later in the main thread with a system lock being held
1013 by one of the cancelled threads. Ideally one would use the same
1014 code as in pthread_atfork(), but we can't distinguish system and
1015 user handlers there. */
1017 /* Send the CANCEL signal to all running threads, including the main
1018 thread, but excluding the thread from which the exit request originated
1019 (that thread must complete the exit, e.g. calling atexit functions
1020 and flushing stdio buffers). */
1021 for (th
= issuing_thread
->p_nextlive
;
1022 th
!= issuing_thread
;
1023 th
= th
->p_nextlive
) {
1024 kill(th
->p_pid
, __pthread_sig_cancel
);
1026 /* Now, wait for all these threads, so that they don't become zombies
1027 and their times are properly added to the thread manager's times. */
1028 for (th
= issuing_thread
->p_nextlive
;
1029 th
!= issuing_thread
;
1030 th
= th
->p_nextlive
) {
1031 waitpid(th
->p_pid
, NULL
, __WCLONE
);
1033 __fresetlockfiles();
1034 restart(issuing_thread
);
1038 /* Handler for __pthread_sig_cancel in thread manager thread */
1040 void __pthread_manager_sighandler(int sig
)
1042 int kick_manager
= terminated_children
== 0 && main_thread_exiting
;
1043 terminated_children
= 1;
1045 /* If the main thread is terminating, kick the thread manager loop
1046 each time some threads terminate. This eliminates a two second
1047 shutdown delay caused by the thread manager sleeping in the
1048 call to __poll(). Instead, the thread manager is kicked into
1049 action, reaps the outstanding threads and resumes the main thread
1050 so that it can complete the shutdown. */
1053 struct pthread_request request
;
1054 request
.req_thread
= 0;
1055 request
.req_kind
= REQ_KICK
;
1056 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
1057 (char *) &request
, sizeof(request
)));
1061 /* Adjust priority of thread manager so that it always run at a priority
1062 higher than all threads */
1064 void __pthread_manager_adjust_prio(int thread_prio
)
1066 struct sched_param param
;
1068 if (thread_prio
<= manager_thread
->p_priority
) return;
1069 param
.sched_priority
=
1070 thread_prio
< __sched_get_priority_max(SCHED_FIFO
)
1071 ? thread_prio
+ 1 : thread_prio
;
1072 __sched_setscheduler(manager_thread
->p_pid
, SCHED_FIFO
, ¶m
);
1073 manager_thread
->p_priority
= thread_prio
;