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 */
31 #include <resolv.h> /* for __resp */
35 #include "internals.h"
38 #include "semaphore.h"
39 #include <not-cancel.h>
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 static pthread_descr manager_thread
;
65 /* Mapping from stack segment to thread descriptor. */
66 /* Stack segment numbers are also indices into the __pthread_handles array. */
67 /* Stack segment number 0 is reserved for the initial thread. */
70 # define thread_segment(seq) NULL
72 static inline pthread_descr
thread_segment(int seg
)
74 # ifdef _STACK_GROWS_UP
75 return (pthread_descr
)(THREAD_STACK_START_ADDRESS
+ (seg
- 1) * STACK_SIZE
)
78 return (pthread_descr
)(THREAD_STACK_START_ADDRESS
- (seg
- 1) * STACK_SIZE
)
84 /* Flag set in signal handler to record child termination */
86 static volatile int terminated_children
;
88 /* Flag set when the initial thread is blocked on pthread_exit waiting
89 for all other threads to terminate */
91 static int main_thread_exiting
;
93 /* Counter used to generate unique thread identifier.
94 Thread identifier is pthread_threads_counter + segment. */
96 static pthread_t pthread_threads_counter
;
98 /* Forward declarations */
100 static int pthread_handle_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
101 void * (*start_routine
)(void *), void *arg
,
102 sigset_t
*mask
, int father_pid
,
104 td_thr_events_t
*event_maskp
);
105 static void pthread_handle_free(pthread_t th_id
);
106 static void pthread_handle_exit(pthread_descr issuing_thread
, int exitcode
)
107 __attribute__ ((noreturn
));
108 static void pthread_reap_children(void);
109 static void pthread_kill_all_threads(int sig
, int main_thread_also
);
110 static void pthread_for_each_thread(void *arg
,
111 void (*fn
)(void *, pthread_descr
));
113 /* The server thread managing requests for thread creation and termination */
116 __attribute__ ((noreturn
))
117 __pthread_manager(void *arg
)
119 pthread_descr self
= manager_thread
= arg
;
120 int reqfd
= __pthread_manager_reader
;
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(self
, 1);
130 #if !(USE_TLS && HAVE___THREAD)
131 /* Set the error variable. */
132 self
->p_errnop
= &self
->p_errno
;
133 self
->p_h_errnop
= &self
->p_h_errno
;
135 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
136 sigfillset(&manager_mask
);
137 sigdelset(&manager_mask
, __pthread_sig_cancel
); /* for thread termination */
138 sigdelset(&manager_mask
, SIGTRAP
); /* for debugging purposes */
139 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0)
140 sigdelset(&manager_mask
, __pthread_sig_debug
);
141 sigprocmask(SIG_SETMASK
, &manager_mask
, NULL
);
142 /* Raise our priority to match that of main thread */
143 __pthread_manager_adjust_prio(__pthread_main_thread
->p_priority
);
144 /* Synchronize debugging of the thread manager */
145 n
= TEMP_FAILURE_RETRY(read_not_cancel(reqfd
, (char *)&request
,
147 ASSERT(n
== sizeof(request
) && request
.req_kind
== REQ_DEBUG
);
150 /* Enter server loop */
152 n
= __poll(&ufd
, 1, 2000);
154 /* Check for termination of the main thread */
155 if (getppid() == 1) {
156 pthread_kill_all_threads(SIGKILL
, 0);
159 /* Check for dead children */
160 if (terminated_children
) {
161 terminated_children
= 0;
162 pthread_reap_children();
164 /* Read and execute request */
165 if (n
== 1 && (ufd
.revents
& POLLIN
)) {
166 n
= TEMP_FAILURE_RETRY(read_not_cancel(reqfd
, (char *)&request
,
171 write(STDERR_FILENO
, d
, snprintf(d
, sizeof(d
), "*** read err %m\n"));
172 } else if (n
!= sizeof(request
)) {
173 write(STDERR_FILENO
, "*** short read in manager\n", 26);
177 switch(request
.req_kind
) {
179 request
.req_thread
->p_retcode
=
180 pthread_handle_create((pthread_t
*) &request
.req_thread
->p_retval
,
181 request
.req_args
.create
.attr
,
182 request
.req_args
.create
.fn
,
183 request
.req_args
.create
.arg
,
184 &request
.req_args
.create
.mask
,
185 request
.req_thread
->p_pid
,
186 request
.req_thread
->p_report_events
,
187 &request
.req_thread
->p_eventbuf
.eventmask
);
188 restart(request
.req_thread
);
191 pthread_handle_free(request
.req_args
.free
.thread_id
);
193 case REQ_PROCESS_EXIT
:
194 pthread_handle_exit(request
.req_thread
,
195 request
.req_args
.exit
.code
);
198 case REQ_MAIN_THREAD_EXIT
:
199 main_thread_exiting
= 1;
200 /* Reap children in case all other threads died and the signal handler
201 went off before we set main_thread_exiting to 1, and therefore did
203 pthread_reap_children();
205 if (__pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
206 restart(__pthread_main_thread
);
207 /* The main thread will now call exit() which will trigger an
208 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
209 to the thread manager. In case you are wondering how the
210 manager terminates from its loop here. */
214 __new_sem_post(request
.req_args
.post
);
217 /* Make gdb aware of new thread and gdb will restart the
218 new thread when it is ready to handle the new thread. */
219 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0)
220 raise(__pthread_sig_debug
);
223 /* This is just a prod to get the manager to reap some
224 threads right away, avoiding a potential delay at shutdown. */
226 case REQ_FOR_EACH_THREAD
:
227 pthread_for_each_thread(request
.req_args
.for_each
.arg
,
228 request
.req_args
.for_each
.fn
);
229 restart(request
.req_thread
);
236 int __pthread_manager_event(void *arg
)
238 pthread_descr self
= arg
;
239 /* If we have special thread_self processing, initialize it. */
240 #ifdef INIT_THREAD_SELF
241 INIT_THREAD_SELF(self
, 1);
244 /* Get the lock the manager will free once all is correctly set up. */
245 __pthread_lock (THREAD_GETMEM(self
, p_lock
), NULL
);
246 /* Free it immediately. */
247 __pthread_unlock (THREAD_GETMEM(self
, p_lock
));
249 return __pthread_manager(arg
);
252 /* Process creation */
255 __attribute__ ((noreturn
))
256 pthread_start_thread(void *arg
)
258 pthread_descr self
= (pthread_descr
) arg
;
259 struct pthread_request request
;
262 hp_timing_t tmpclock
;
264 /* Initialize special thread_self processing, if any. */
265 #ifdef INIT_THREAD_SELF
266 INIT_THREAD_SELF(self
, self
->p_nr
);
269 HP_TIMING_NOW (tmpclock
);
270 THREAD_SETMEM (self
, p_cpuclock_offset
, tmpclock
);
272 /* Make sure our pid field is initialized, just in case we get there
273 before our father has initialized it. */
274 THREAD_SETMEM(self
, p_pid
, __getpid());
275 /* Initial signal mask is that of the creating thread. (Otherwise,
276 we'd just inherit the mask of the thread manager.) */
277 sigprocmask(SIG_SETMASK
, &self
->p_start_args
.mask
, NULL
);
278 /* Set the scheduling policy and priority for the new thread, if needed */
279 if (THREAD_GETMEM(self
, p_start_args
.schedpolicy
) >= 0)
280 /* Explicit scheduling attributes were provided: apply them */
281 __sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
282 THREAD_GETMEM(self
, p_start_args
.schedpolicy
),
283 &self
->p_start_args
.schedparam
);
284 else if (manager_thread
->p_priority
> 0)
285 /* Default scheduling required, but thread manager runs in realtime
286 scheduling: switch new thread to SCHED_OTHER policy */
288 struct sched_param default_params
;
289 default_params
.sched_priority
= 0;
290 __sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
291 SCHED_OTHER
, &default_params
);
293 #if !(USE_TLS && HAVE___THREAD)
294 /* Initialize thread-locale current locale to point to the global one.
295 With __thread support, the variable's initializer takes care of this. */
296 __uselocale (LC_GLOBAL_LOCALE
);
298 /* Initialize __resp. */
299 __resp
= &self
->p_res
;
301 /* Make gdb aware of new thread */
302 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0) {
303 request
.req_thread
= self
;
304 request
.req_kind
= REQ_DEBUG
;
305 TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request
,
306 (char *) &request
, sizeof(request
)));
309 /* Run the thread code */
310 outcome
= self
->p_start_args
.start_routine(THREAD_GETMEM(self
,
312 /* Exit with the given return value */
313 __pthread_do_exit(outcome
, CURRENT_STACK_FRAME
);
317 __attribute__ ((noreturn
))
318 pthread_start_thread_event(void *arg
)
320 pthread_descr self
= (pthread_descr
) arg
;
322 #ifdef INIT_THREAD_SELF
323 INIT_THREAD_SELF(self
, self
->p_nr
);
325 /* Make sure our pid field is initialized, just in case we get there
326 before our father has initialized it. */
327 THREAD_SETMEM(self
, p_pid
, __getpid());
328 /* Get the lock the manager will free once all is correctly set up. */
329 __pthread_lock (THREAD_GETMEM(self
, p_lock
), NULL
);
330 /* Free it immediately. */
331 __pthread_unlock (THREAD_GETMEM(self
, p_lock
));
333 /* Continue with the real function. */
334 pthread_start_thread (arg
);
337 #if defined USE_TLS && !FLOATING_STACKS
338 # error "TLS can only work with floating stacks"
341 static int pthread_allocate_stack(const pthread_attr_t
*attr
,
342 pthread_descr default_new_thread
,
344 char ** out_new_thread
,
345 char ** out_new_thread_bottom
,
346 char ** out_guardaddr
,
347 size_t * out_guardsize
,
348 size_t * out_stacksize
)
350 pthread_descr new_thread
;
351 char * new_thread_bottom
;
353 size_t stacksize
, guardsize
;
356 /* TLS cannot work with fixed thread descriptor addresses. */
357 assert (default_new_thread
== NULL
);
360 if (attr
!= NULL
&& attr
->__stackaddr_set
)
362 #ifdef _STACK_GROWS_UP
363 /* The user provided a stack. */
365 /* This value is not needed. */
366 new_thread
= (pthread_descr
) attr
->__stackaddr
;
367 new_thread_bottom
= (char *) new_thread
;
369 new_thread
= (pthread_descr
) attr
->__stackaddr
;
370 new_thread_bottom
= (char *) (new_thread
+ 1);
372 guardaddr
= attr
->__stackaddr
+ attr
->__stacksize
;
375 /* The user provided a stack. For now we interpret the supplied
376 address as 1 + the highest addr. in the stack segment. If a
377 separate register stack is needed, we place it at the low end
378 of the segment, relying on the associated stacksize to
379 determine the low end of the segment. This differs from many
380 (but not all) other pthreads implementations. The intent is
381 that on machines with a single stack growing toward higher
382 addresses, stackaddr would be the lowest address in the stack
383 segment, so that it is consistently close to the initial sp
386 new_thread
= (pthread_descr
) attr
->__stackaddr
;
389 (pthread_descr
) ((long)(attr
->__stackaddr
) & -sizeof(void *)) - 1;
391 new_thread_bottom
= (char *) attr
->__stackaddr
- attr
->__stacksize
;
392 guardaddr
= new_thread_bottom
;
396 __pthread_nonstandard_stacks
= 1;
399 /* Clear the thread data structure. */
400 memset (new_thread
, '\0', sizeof (*new_thread
));
402 stacksize
= attr
->__stacksize
;
406 #ifdef NEED_SEPARATE_REGISTER_STACK
407 const size_t granularity
= 2 * pagesize
;
408 /* Try to make stacksize/2 a multiple of pagesize */
410 const size_t granularity
= pagesize
;
414 /* Allocate space for stack and thread descriptor at default address */
418 guardsize
= page_roundup (attr
->__guardsize
, granularity
);
419 stacksize
= __pthread_max_stacksize
- guardsize
;
420 stacksize
= MIN (stacksize
,
421 page_roundup (attr
->__stacksize
, granularity
));
425 guardsize
= granularity
;
426 stacksize
= __pthread_max_stacksize
- guardsize
;
429 map_addr
= mmap(NULL
, stacksize
+ guardsize
,
430 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
431 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
432 if (map_addr
== MAP_FAILED
)
433 /* No more memory available. */
436 # ifdef NEED_SEPARATE_REGISTER_STACK
437 guardaddr
= map_addr
+ stacksize
/ 2;
439 mprotect (guardaddr
, guardsize
, PROT_NONE
);
441 new_thread_bottom
= (char *) map_addr
;
443 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
446 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
449 # elif _STACK_GROWS_DOWN
450 guardaddr
= map_addr
;
452 mprotect (guardaddr
, guardsize
, PROT_NONE
);
454 new_thread_bottom
= (char *) map_addr
+ guardsize
;
456 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
));
458 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
)) - 1;
460 # elif _STACK_GROWS_UP
461 guardaddr
= map_addr
+ stacksize
;
463 mprotect (guardaddr
, guardsize
, PROT_NONE
);
465 new_thread
= (pthread_descr
) map_addr
;
467 new_thread_bottom
= (char *) new_thread
;
469 new_thread_bottom
= (char *) (new_thread
+ 1);
472 # error You must define a stack direction
473 # endif /* Stack direction */
474 #else /* !FLOATING_STACKS */
479 guardsize
= page_roundup (attr
->__guardsize
, granularity
);
480 stacksize
= STACK_SIZE
- guardsize
;
481 stacksize
= MIN (stacksize
,
482 page_roundup (attr
->__stacksize
, granularity
));
486 guardsize
= granularity
;
487 stacksize
= STACK_SIZE
- granularity
;
490 # ifdef NEED_SEPARATE_REGISTER_STACK
491 new_thread
= default_new_thread
;
492 new_thread_bottom
= (char *) (new_thread
+ 1) - stacksize
- guardsize
;
493 /* Includes guard area, unlike the normal case. Use the bottom
494 end of the segment as backing store for the register stack.
495 Needed on IA64. In this case, we also map the entire stack at
496 once. According to David Mosberger, that's cheaper. It also
497 avoids the risk of intermittent failures due to other mappings
498 in the same region. The cost is that we might be able to map
499 slightly fewer stacks. */
501 /* First the main stack: */
502 map_addr
= (caddr_t
)((char *)(new_thread
+ 1) - stacksize
/ 2);
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 /* Bad luck, this segment is already mapped. */
509 if (res_addr
!= MAP_FAILED
)
510 munmap(res_addr
, stacksize
/ 2);
513 /* Then the register stack: */
514 map_addr
= (caddr_t
)new_thread_bottom
;
515 res_addr
= mmap(map_addr
, stacksize
/2,
516 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
517 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
518 if (res_addr
!= map_addr
)
520 if (res_addr
!= MAP_FAILED
)
521 munmap(res_addr
, stacksize
/ 2);
522 munmap((caddr_t
)((char *)(new_thread
+ 1) - stacksize
/2),
527 guardaddr
= new_thread_bottom
+ stacksize
/2;
528 /* We leave the guard area in the middle unmapped. */
529 # else /* !NEED_SEPARATE_REGISTER_STACK */
530 # ifdef _STACK_GROWS_DOWN
531 new_thread
= default_new_thread
;
532 new_thread_bottom
= (char *) (new_thread
+ 1) - stacksize
;
533 map_addr
= new_thread_bottom
- guardsize
;
534 res_addr
= mmap(map_addr
, stacksize
+ guardsize
,
535 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
536 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
537 if (res_addr
!= map_addr
)
539 /* Bad luck, this segment is already mapped. */
540 if (res_addr
!= MAP_FAILED
)
541 munmap (res_addr
, stacksize
+ guardsize
);
545 /* We manage to get a stack. Protect the guard area pages if
547 guardaddr
= map_addr
;
549 mprotect (guardaddr
, guardsize
, PROT_NONE
);
551 /* The thread description goes at the bottom of this area, and
552 * the stack starts directly above it.
554 new_thread
= (pthread_descr
)((unsigned long)default_new_thread
&~ (STACK_SIZE
- 1));
555 map_addr
= mmap(new_thread
, stacksize
+ guardsize
,
556 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
557 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
558 if (map_addr
== MAP_FAILED
)
561 new_thread_bottom
= map_addr
+ sizeof(*new_thread
);
562 guardaddr
= map_addr
+ stacksize
;
564 mprotect (guardaddr
, guardsize
, PROT_NONE
);
566 # endif /* stack direction */
567 # endif /* !NEED_SEPARATE_REGISTER_STACK */
568 #endif /* !FLOATING_STACKS */
570 *out_new_thread
= (char *) new_thread
;
571 *out_new_thread_bottom
= new_thread_bottom
;
572 *out_guardaddr
= guardaddr
;
573 *out_guardsize
= guardsize
;
574 #ifdef NEED_SEPARATE_REGISTER_STACK
575 *out_stacksize
= stacksize
/ 2;
577 *out_stacksize
= stacksize
;
582 static int pthread_handle_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
583 void * (*start_routine
)(void *), void *arg
,
584 sigset_t
* mask
, int father_pid
,
586 td_thr_events_t
*event_maskp
)
590 pthread_descr new_thread
;
592 char * new_thread_bottom
;
593 pthread_t new_thread_id
;
594 char *guardaddr
= NULL
;
595 size_t guardsize
= 0, stksize
= 0;
596 int pagesize
= __getpagesize();
600 new_thread
= _dl_allocate_tls (NULL
);
601 if (new_thread
== NULL
)
604 /* pthread_descr is below TP. */
605 new_thread
= (pthread_descr
) ((char *) new_thread
- TLS_PRE_TCB_SIZE
);
608 /* Prevent warnings. */
612 /* First check whether we have to change the policy and if yes, whether
613 we can do this. Normally this should be done by examining the
614 return value of the __sched_setscheduler call in pthread_start_thread
615 but this is hard to implement. FIXME */
616 if (attr
!= NULL
&& attr
->__schedpolicy
!= SCHED_OTHER
&& geteuid () != 0)
618 /* Find a free segment for the thread, and allocate a stack if needed */
619 for (sseg
= 2; ; sseg
++)
621 if (sseg
>= PTHREAD_THREADS_MAX
)
625 new_thread
= (pthread_descr
) ((char *) new_thread
+ TLS_PRE_TCB_SIZE
);
627 _dl_deallocate_tls (new_thread
, true);
631 if (__pthread_handles
[sseg
].h_descr
!= NULL
)
633 if (pthread_allocate_stack(attr
, thread_segment(sseg
),
634 pagesize
, &stack_addr
, &new_thread_bottom
,
635 &guardaddr
, &guardsize
, &stksize
) == 0)
638 new_thread
->p_stackaddr
= stack_addr
;
640 new_thread
= (pthread_descr
) stack_addr
;
645 __pthread_handles_num
++;
646 /* Allocate new thread identifier */
647 pthread_threads_counter
+= PTHREAD_THREADS_MAX
;
648 new_thread_id
= sseg
+ pthread_threads_counter
;
649 /* Initialize the thread descriptor. Elements which have to be
650 initialized to zero already have this value. */
651 #if !defined USE_TLS || !TLS_DTV_AT_TP
652 new_thread
->p_header
.data
.tcb
= new_thread
;
653 new_thread
->p_header
.data
.self
= new_thread
;
655 #if TLS_MULTIPLE_THREADS_IN_TCB || !defined USE_TLS || !TLS_DTV_AT_TP
656 new_thread
->p_multiple_threads
= 1;
658 new_thread
->p_tid
= new_thread_id
;
659 new_thread
->p_lock
= &(__pthread_handles
[sseg
].h_lock
);
660 new_thread
->p_cancelstate
= PTHREAD_CANCEL_ENABLE
;
661 new_thread
->p_canceltype
= PTHREAD_CANCEL_DEFERRED
;
662 #if !(USE_TLS && HAVE___THREAD)
663 new_thread
->p_errnop
= &new_thread
->p_errno
;
664 new_thread
->p_h_errnop
= &new_thread
->p_h_errno
;
665 new_thread
->p_resp
= &new_thread
->p_res
;
667 new_thread
->p_guardaddr
= guardaddr
;
668 new_thread
->p_guardsize
= guardsize
;
669 new_thread
->p_nr
= sseg
;
670 new_thread
->p_inheritsched
= attr
? attr
->__inheritsched
: 0;
671 new_thread
->p_alloca_cutoff
= stksize
/ 4 > __MAX_ALLOCA_CUTOFF
672 ? __MAX_ALLOCA_CUTOFF
: stksize
/ 4;
673 /* Initialize the thread handle */
674 __pthread_init_lock(&__pthread_handles
[sseg
].h_lock
);
675 __pthread_handles
[sseg
].h_descr
= new_thread
;
676 __pthread_handles
[sseg
].h_bottom
= new_thread_bottom
;
677 /* Determine scheduling parameters for the thread */
678 new_thread
->p_start_args
.schedpolicy
= -1;
680 new_thread
->p_detached
= attr
->__detachstate
;
681 new_thread
->p_userstack
= attr
->__stackaddr_set
;
683 switch(attr
->__inheritsched
) {
684 case PTHREAD_EXPLICIT_SCHED
:
685 new_thread
->p_start_args
.schedpolicy
= attr
->__schedpolicy
;
686 memcpy (&new_thread
->p_start_args
.schedparam
, &attr
->__schedparam
,
687 sizeof (struct sched_param
));
689 case PTHREAD_INHERIT_SCHED
:
690 new_thread
->p_start_args
.schedpolicy
= __sched_getscheduler(father_pid
);
691 __sched_getparam(father_pid
, &new_thread
->p_start_args
.schedparam
);
694 new_thread
->p_priority
=
695 new_thread
->p_start_args
.schedparam
.sched_priority
;
697 /* Finish setting up arguments to pthread_start_thread */
698 new_thread
->p_start_args
.start_routine
= start_routine
;
699 new_thread
->p_start_args
.arg
= arg
;
700 new_thread
->p_start_args
.mask
= *mask
;
701 /* Make the new thread ID available already now. If any of the later
702 functions fail we return an error value and the caller must not use
703 the stored thread ID. */
704 *thread
= new_thread_id
;
705 /* Raise priority of thread manager if needed */
706 __pthread_manager_adjust_prio(new_thread
->p_priority
);
707 /* Do the cloning. We have to use two different functions depending
708 on whether we are debugging or not. */
709 pid
= 0; /* Note that the thread never can have PID zero. */
712 /* See whether the TD_CREATE event bit is set in any of the
714 int idx
= __td_eventword (TD_CREATE
);
715 uint32_t mask
= __td_eventmask (TD_CREATE
);
717 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
718 | event_maskp
->event_bits
[idx
])) != 0)
720 /* Lock the mutex the child will use now so that it will stop. */
721 __pthread_lock(new_thread
->p_lock
, NULL
);
723 /* We have to report this event. */
724 #ifdef NEED_SEPARATE_REGISTER_STACK
725 /* Perhaps this version should be used on all platforms. But
726 this requires that __clone2 be uniformly supported
729 And there is some argument for changing the __clone2
730 interface to pass sp and bsp instead, making it more IA64
731 specific, but allowing stacks to grow outward from each
732 other, to get less paging and fewer mmaps. */
733 pid
= __clone2(pthread_start_thread_event
,
734 (void **)new_thread_bottom
,
735 (char *)stack_addr
- new_thread_bottom
,
736 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
737 __pthread_sig_cancel
, new_thread
);
738 #elif _STACK_GROWS_UP
739 pid
= __clone(pthread_start_thread_event
, (void *) new_thread_bottom
,
740 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
741 __pthread_sig_cancel
, new_thread
);
743 pid
= __clone(pthread_start_thread_event
, stack_addr
,
744 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
745 __pthread_sig_cancel
, new_thread
);
750 /* Now fill in the information about the new thread in
751 the newly created thread's data structure. We cannot let
752 the new thread do this since we don't know whether it was
753 already scheduled when we send the event. */
754 new_thread
->p_eventbuf
.eventdata
= new_thread
;
755 new_thread
->p_eventbuf
.eventnum
= TD_CREATE
;
756 __pthread_last_event
= new_thread
;
758 /* We have to set the PID here since the callback function
759 in the debug library will need it and we cannot guarantee
760 the child got scheduled before the debugger. */
761 new_thread
->p_pid
= pid
;
763 /* Now call the function which signals the event. */
764 __linuxthreads_create_event ();
766 /* Now restart the thread. */
767 __pthread_unlock(new_thread
->p_lock
);
773 #ifdef NEED_SEPARATE_REGISTER_STACK
774 pid
= __clone2(pthread_start_thread
,
775 (void **)new_thread_bottom
,
776 (char *)stack_addr
- new_thread_bottom
,
777 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
778 __pthread_sig_cancel
, new_thread
);
779 #elif _STACK_GROWS_UP
780 pid
= __clone(pthread_start_thread
, (void *) new_thread_bottom
,
781 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
782 __pthread_sig_cancel
, new_thread
);
784 pid
= __clone(pthread_start_thread
, stack_addr
,
785 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
786 __pthread_sig_cancel
, new_thread
);
787 #endif /* !NEED_SEPARATE_REGISTER_STACK */
790 /* Check if cloning succeeded */
792 /* Free the stack if we allocated it */
793 if (attr
== NULL
|| !attr
->__stackaddr_set
)
795 #ifdef NEED_SEPARATE_REGISTER_STACK
796 size_t stacksize
= ((char *)(new_thread
->p_guardaddr
)
797 - new_thread_bottom
);
798 munmap((caddr_t
)new_thread_bottom
,
799 2 * stacksize
+ new_thread
->p_guardsize
);
800 #elif _STACK_GROWS_UP
802 size_t stacksize
= guardaddr
- stack_addr
;
803 munmap(stack_addr
, stacksize
+ guardsize
);
805 size_t stacksize
= guardaddr
- (char *)new_thread
;
806 munmap(new_thread
, stacksize
+ guardsize
);
810 size_t stacksize
= stack_addr
- new_thread_bottom
;
812 size_t stacksize
= (char *)(new_thread
+1) - new_thread_bottom
;
814 munmap(new_thread_bottom
- guardsize
, guardsize
+ stacksize
);
819 new_thread
= (pthread_descr
) ((char *) new_thread
+ TLS_PRE_TCB_SIZE
);
821 _dl_deallocate_tls (new_thread
, true);
823 __pthread_handles
[sseg
].h_descr
= NULL
;
824 __pthread_handles
[sseg
].h_bottom
= NULL
;
825 __pthread_handles_num
--;
828 /* Insert new thread in doubly linked list of active threads */
829 new_thread
->p_prevlive
= __pthread_main_thread
;
830 new_thread
->p_nextlive
= __pthread_main_thread
->p_nextlive
;
831 __pthread_main_thread
->p_nextlive
->p_prevlive
= new_thread
;
832 __pthread_main_thread
->p_nextlive
= new_thread
;
833 /* Set pid field of the new thread, in case we get there before the
835 new_thread
->p_pid
= pid
;
840 /* Try to free the resources of a thread when requested by pthread_join
841 or pthread_detach on a terminated thread. */
843 static void pthread_free(pthread_descr th
)
845 pthread_handle handle
;
846 pthread_readlock_info
*iter
, *next
;
848 ASSERT(th
->p_exited
);
849 /* Make the handle invalid */
850 handle
= thread_handle(th
->p_tid
);
851 __pthread_lock(&handle
->h_lock
, NULL
);
852 handle
->h_descr
= NULL
;
853 handle
->h_bottom
= (char *)(-1L);
854 __pthread_unlock(&handle
->h_lock
);
856 FREE_THREAD(th
, th
->p_nr
);
858 /* One fewer threads in __pthread_handles */
859 __pthread_handles_num
--;
861 /* Destroy read lock list, and list of free read lock structures.
862 If the former is not empty, it means the thread exited while
863 holding read locks! */
865 for (iter
= th
->p_readlock_list
; iter
!= NULL
; iter
= next
)
867 next
= iter
->pr_next
;
871 for (iter
= th
->p_readlock_free
; iter
!= NULL
; iter
= next
)
873 next
= iter
->pr_next
;
877 /* If initial thread, nothing to free */
878 if (!th
->p_userstack
)
880 size_t guardsize
= th
->p_guardsize
;
881 /* Free the stack and thread descriptor area */
882 char *guardaddr
= th
->p_guardaddr
;
883 #ifdef _STACK_GROWS_UP
885 size_t stacksize
= guardaddr
- th
->p_stackaddr
;
887 size_t stacksize
= guardaddr
- (char *)th
;
889 guardaddr
= (char *)th
;
891 /* Guardaddr is always set, even if guardsize is 0. This allows
892 us to compute everything else. */
894 size_t stacksize
= th
->p_stackaddr
- guardaddr
- guardsize
;
896 size_t stacksize
= (char *)(th
+1) - guardaddr
- guardsize
;
898 # ifdef NEED_SEPARATE_REGISTER_STACK
899 /* Take account of the register stack, which is below guardaddr. */
900 guardaddr
-= stacksize
;
904 /* Unmap the stack. */
905 munmap(guardaddr
, stacksize
+ guardsize
);
911 th
= (pthread_descr
) ((char *) th
+ TLS_PRE_TCB_SIZE
);
913 _dl_deallocate_tls (th
, true);
917 /* Handle threads that have exited */
919 static void pthread_exited(pid_t pid
)
923 /* Find thread with that pid */
924 for (th
= __pthread_main_thread
->p_nextlive
;
925 th
!= __pthread_main_thread
;
926 th
= th
->p_nextlive
) {
927 if (th
->p_pid
== pid
) {
928 /* Remove thread from list of active threads */
929 th
->p_nextlive
->p_prevlive
= th
->p_prevlive
;
930 th
->p_prevlive
->p_nextlive
= th
->p_nextlive
;
931 /* Mark thread as exited, and if detached, free its resources */
932 __pthread_lock(th
->p_lock
, NULL
);
934 /* If we have to signal this event do it now. */
935 if (th
->p_report_events
)
937 /* See whether TD_REAP is in any of the mask. */
938 int idx
= __td_eventword (TD_REAP
);
939 uint32_t mask
= __td_eventmask (TD_REAP
);
941 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
942 | th
->p_eventbuf
.eventmask
.event_bits
[idx
])) != 0)
944 /* Yep, we have to signal the reapage. */
945 th
->p_eventbuf
.eventnum
= TD_REAP
;
946 th
->p_eventbuf
.eventdata
= th
;
947 __pthread_last_event
= th
;
949 /* Now call the function to signal the event. */
950 __linuxthreads_reap_event();
953 detached
= th
->p_detached
;
954 __pthread_unlock(th
->p_lock
);
960 /* If all threads have exited and the main thread is pending on a
961 pthread_exit, wake up the main thread and terminate ourselves. */
962 if (main_thread_exiting
&&
963 __pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
964 restart(__pthread_main_thread
);
965 /* Same logic as REQ_MAIN_THREAD_EXIT. */
969 static void pthread_reap_children(void)
974 while ((pid
= waitpid_not_cancel(-1, &status
, WNOHANG
| __WCLONE
)) > 0) {
976 if (WIFSIGNALED(status
)) {
977 /* If a thread died due to a signal, send the same signal to
978 all other threads, including the main thread. */
979 pthread_kill_all_threads(WTERMSIG(status
), 1);
985 /* Try to free the resources of a thread when requested by pthread_join
986 or pthread_detach on a terminated thread. */
988 static void pthread_handle_free(pthread_t th_id
)
990 pthread_handle handle
= thread_handle(th_id
);
993 __pthread_lock(&handle
->h_lock
, NULL
);
994 if (nonexisting_handle(handle
, th_id
)) {
995 /* pthread_reap_children has deallocated the thread already,
996 nothing needs to be done */
997 __pthread_unlock(&handle
->h_lock
);
1000 th
= handle
->h_descr
;
1002 __pthread_unlock(&handle
->h_lock
);
1005 /* The Unix process of the thread is still running.
1006 Mark the thread as detached so that the thread manager will
1007 deallocate its resources when the Unix process exits. */
1009 __pthread_unlock(&handle
->h_lock
);
1013 /* Send a signal to all running threads */
1015 static void pthread_kill_all_threads(int sig
, int main_thread_also
)
1018 for (th
= __pthread_main_thread
->p_nextlive
;
1019 th
!= __pthread_main_thread
;
1020 th
= th
->p_nextlive
) {
1021 kill(th
->p_pid
, sig
);
1023 if (main_thread_also
) {
1024 kill(__pthread_main_thread
->p_pid
, sig
);
1028 static void pthread_for_each_thread(void *arg
,
1029 void (*fn
)(void *, pthread_descr
))
1033 for (th
= __pthread_main_thread
->p_nextlive
;
1034 th
!= __pthread_main_thread
;
1035 th
= th
->p_nextlive
) {
1039 fn(arg
, __pthread_main_thread
);
1042 /* Process-wide exit() */
1044 static void pthread_handle_exit(pthread_descr issuing_thread
, int exitcode
)
1047 __pthread_exit_requested
= 1;
1048 __pthread_exit_code
= exitcode
;
1049 /* A forced asynchronous cancellation follows. Make sure we won't
1050 get stuck later in the main thread with a system lock being held
1051 by one of the cancelled threads. Ideally one would use the same
1052 code as in pthread_atfork(), but we can't distinguish system and
1053 user handlers there. */
1055 /* Send the CANCEL signal to all running threads, including the main
1056 thread, but excluding the thread from which the exit request originated
1057 (that thread must complete the exit, e.g. calling atexit functions
1058 and flushing stdio buffers). */
1059 for (th
= issuing_thread
->p_nextlive
;
1060 th
!= issuing_thread
;
1061 th
= th
->p_nextlive
) {
1062 kill(th
->p_pid
, __pthread_sig_cancel
);
1064 /* Now, wait for all these threads, so that they don't become zombies
1065 and their times are properly added to the thread manager's times. */
1066 for (th
= issuing_thread
->p_nextlive
;
1067 th
!= issuing_thread
;
1068 th
= th
->p_nextlive
) {
1069 waitpid(th
->p_pid
, NULL
, __WCLONE
);
1071 __fresetlockfiles();
1072 restart(issuing_thread
);
1076 /* Handler for __pthread_sig_cancel in thread manager thread */
1078 void __pthread_manager_sighandler(int sig
)
1080 int kick_manager
= terminated_children
== 0 && main_thread_exiting
;
1081 terminated_children
= 1;
1083 /* If the main thread is terminating, kick the thread manager loop
1084 each time some threads terminate. This eliminates a two second
1085 shutdown delay caused by the thread manager sleeping in the
1086 call to __poll(). Instead, the thread manager is kicked into
1087 action, reaps the outstanding threads and resumes the main thread
1088 so that it can complete the shutdown. */
1091 struct pthread_request request
;
1092 request
.req_thread
= 0;
1093 request
.req_kind
= REQ_KICK
;
1094 TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request
,
1095 (char *) &request
, sizeof(request
)));
1099 /* Adjust priority of thread manager so that it always run at a priority
1100 higher than all threads */
1102 void __pthread_manager_adjust_prio(int thread_prio
)
1104 struct sched_param param
;
1106 if (thread_prio
<= manager_thread
->p_priority
) return;
1107 param
.sched_priority
=
1108 thread_prio
< __sched_get_priority_max(SCHED_FIFO
)
1109 ? thread_prio
+ 1 : thread_prio
;
1110 __sched_setscheduler(manager_thread
->p_pid
, SCHED_FIFO
, ¶m
);
1111 manager_thread
->p_priority
= thread_prio
;