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 /* Array of active threads. Entry 0 is reserved for the initial thread. */
40 struct pthread_handle_struct __pthread_handles
[PTHREAD_THREADS_MAX
]
42 # if __LT_SPINLOCK_INIT != 0
44 { __LOCK_INITIALIZER
, NULL
, 0},
45 { __LOCK_INITIALIZER
, NULL
, 0},
51 { __LOCK_INITIALIZER
, &__pthread_initial_thread
, 0},
52 { __LOCK_INITIALIZER
, &__pthread_manager_thread
, 0},
58 /* For debugging purposes put the maximum number of threads in a variable. */
59 const int __linuxthreads_pthread_threads_max
= PTHREAD_THREADS_MAX
;
62 /* Indicate whether at least one thread has a user-defined stack (if 1),
63 or if all threads have stacks supplied by LinuxThreads (if 0). */
64 int __pthread_nonstandard_stacks
;
67 /* Number of active entries in __pthread_handles (used by gdb) */
68 volatile int __pthread_handles_num
= 2;
70 /* Whether to use debugger additional actions for thread creation
72 volatile int __pthread_threads_debug
;
74 /* Globally enabled events. */
75 volatile td_thr_events_t __pthread_threads_events
;
77 /* Pointer to thread descriptor with last event. */
78 volatile pthread_descr __pthread_last_event
;
80 static pthread_descr manager_thread
;
82 /* Mapping from stack segment to thread descriptor. */
83 /* Stack segment numbers are also indices into the __pthread_handles array. */
84 /* Stack segment number 0 is reserved for the initial thread. */
87 # define thread_segment(seq) NULL
89 static inline pthread_descr
thread_segment(int seg
)
91 return (pthread_descr
)(THREAD_STACK_START_ADDRESS
- (seg
- 1) * STACK_SIZE
)
96 /* Flag set in signal handler to record child termination */
98 static volatile int terminated_children
;
100 /* Flag set when the initial thread is blocked on pthread_exit waiting
101 for all other threads to terminate */
103 static int main_thread_exiting
;
105 /* Counter used to generate unique thread identifier.
106 Thread identifier is pthread_threads_counter + segment. */
108 static pthread_t pthread_threads_counter
;
110 /* Forward declarations */
112 static int pthread_handle_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
113 void * (*start_routine
)(void *), void *arg
,
114 sigset_t
*mask
, int father_pid
,
116 td_thr_events_t
*event_maskp
);
117 static void pthread_handle_free(pthread_t th_id
);
118 static void pthread_handle_exit(pthread_descr issuing_thread
, int exitcode
)
119 __attribute__ ((noreturn
));
120 static void pthread_reap_children(void);
121 static void pthread_kill_all_threads(int sig
, int main_thread_also
);
122 static void pthread_for_each_thread(void *arg
,
123 void (*fn
)(void *, pthread_descr
));
125 /* The server thread managing requests for thread creation and termination */
128 __attribute__ ((noreturn
))
129 __pthread_manager(void *arg
)
131 pthread_descr self
= manager_thread
= arg
;
132 int reqfd
= __pthread_manager_reader
;
134 sigset_t manager_mask
;
136 struct pthread_request request
;
138 /* If we have special thread_self processing, initialize it. */
139 #ifdef INIT_THREAD_SELF
140 INIT_THREAD_SELF(self
, 1);
142 #if !(USE_TLS && HAVE___THREAD)
143 /* Set the error variable. */
144 self
->p_errnop
= &self
->p_errno
;
145 self
->p_h_errnop
= &self
->p_h_errno
;
147 /* Block all signals except __pthread_sig_cancel and SIGTRAP */
148 sigfillset(&manager_mask
);
149 sigdelset(&manager_mask
, __pthread_sig_cancel
); /* for thread termination */
150 sigdelset(&manager_mask
, SIGTRAP
); /* for debugging purposes */
151 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0)
152 sigdelset(&manager_mask
, __pthread_sig_debug
);
153 sigprocmask(SIG_SETMASK
, &manager_mask
, NULL
);
154 /* Raise our priority to match that of main thread */
155 __pthread_manager_adjust_prio(__pthread_main_thread
->p_priority
);
156 /* Synchronize debugging of the thread manager */
157 n
= TEMP_FAILURE_RETRY(__libc_read(reqfd
, (char *)&request
,
159 ASSERT(n
== sizeof(request
) && request
.req_kind
== REQ_DEBUG
);
162 /* Enter server loop */
164 n
= __poll(&ufd
, 1, 2000);
166 /* Check for termination of the main thread */
167 if (getppid() == 1) {
168 pthread_kill_all_threads(SIGKILL
, 0);
171 /* Check for dead children */
172 if (terminated_children
) {
173 terminated_children
= 0;
174 pthread_reap_children();
176 /* Read and execute request */
177 if (n
== 1 && (ufd
.revents
& POLLIN
)) {
178 n
= TEMP_FAILURE_RETRY(__libc_read(reqfd
, (char *)&request
,
183 write(STDERR_FILENO
, d
, snprintf(d
, sizeof(d
), "*** read err %m\n"));
184 } else if (n
!= sizeof(request
)) {
185 write(STDERR_FILENO
, "*** short read in manager\n", 26);
189 switch(request
.req_kind
) {
191 request
.req_thread
->p_retcode
=
192 pthread_handle_create((pthread_t
*) &request
.req_thread
->p_retval
,
193 request
.req_args
.create
.attr
,
194 request
.req_args
.create
.fn
,
195 request
.req_args
.create
.arg
,
196 &request
.req_args
.create
.mask
,
197 request
.req_thread
->p_pid
,
198 request
.req_thread
->p_report_events
,
199 &request
.req_thread
->p_eventbuf
.eventmask
);
200 restart(request
.req_thread
);
203 pthread_handle_free(request
.req_args
.free
.thread_id
);
205 case REQ_PROCESS_EXIT
:
206 pthread_handle_exit(request
.req_thread
,
207 request
.req_args
.exit
.code
);
210 case REQ_MAIN_THREAD_EXIT
:
211 main_thread_exiting
= 1;
212 /* Reap children in case all other threads died and the signal handler
213 went off before we set main_thread_exiting to 1, and therefore did
215 pthread_reap_children();
217 if (__pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
218 restart(__pthread_main_thread
);
219 /* The main thread will now call exit() which will trigger an
220 __on_exit handler, which in turn will send REQ_PROCESS_EXIT
221 to the thread manager. In case you are wondering how the
222 manager terminates from its loop here. */
226 __new_sem_post(request
.req_args
.post
);
229 /* Make gdb aware of new thread and gdb will restart the
230 new thread when it is ready to handle the new thread. */
231 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0)
232 raise(__pthread_sig_debug
);
235 /* This is just a prod to get the manager to reap some
236 threads right away, avoiding a potential delay at shutdown. */
238 case REQ_FOR_EACH_THREAD
:
239 pthread_for_each_thread(request
.req_args
.for_each
.arg
,
240 request
.req_args
.for_each
.fn
);
241 restart(request
.req_thread
);
248 int __pthread_manager_event(void *arg
)
250 pthread_descr self
= arg
;
251 /* If we have special thread_self processing, initialize it. */
252 #ifdef INIT_THREAD_SELF
253 INIT_THREAD_SELF(self
, 1);
256 /* Get the lock the manager will free once all is correctly set up. */
257 __pthread_lock (THREAD_GETMEM(self
, p_lock
), NULL
);
258 /* Free it immediately. */
259 __pthread_unlock (THREAD_GETMEM(self
, p_lock
));
261 return __pthread_manager(arg
);
264 /* Process creation */
267 __attribute__ ((noreturn
))
268 pthread_start_thread(void *arg
)
270 pthread_descr self
= (pthread_descr
) arg
;
271 struct pthread_request request
;
274 hp_timing_t tmpclock
;
276 /* Initialize special thread_self processing, if any. */
277 #ifdef INIT_THREAD_SELF
278 INIT_THREAD_SELF(self
, self
->p_nr
);
281 HP_TIMING_NOW (tmpclock
);
282 THREAD_SETMEM (self
, p_cpuclock_offset
, tmpclock
);
284 /* Make sure our pid field is initialized, just in case we get there
285 before our father has initialized it. */
286 THREAD_SETMEM(self
, p_pid
, __getpid());
287 /* Initial signal mask is that of the creating thread. (Otherwise,
288 we'd just inherit the mask of the thread manager.) */
289 sigprocmask(SIG_SETMASK
, &self
->p_start_args
.mask
, NULL
);
290 /* Set the scheduling policy and priority for the new thread, if needed */
291 if (THREAD_GETMEM(self
, p_start_args
.schedpolicy
) >= 0)
292 /* Explicit scheduling attributes were provided: apply them */
293 __sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
294 THREAD_GETMEM(self
, p_start_args
.schedpolicy
),
295 &self
->p_start_args
.schedparam
);
296 else if (manager_thread
->p_priority
> 0)
297 /* Default scheduling required, but thread manager runs in realtime
298 scheduling: switch new thread to SCHED_OTHER policy */
300 struct sched_param default_params
;
301 default_params
.sched_priority
= 0;
302 __sched_setscheduler(THREAD_GETMEM(self
, p_pid
),
303 SCHED_OTHER
, &default_params
);
305 #if !(USE_TLS && HAVE___THREAD) && defined SHARED
306 /* Initialize thread-locale current locale to point to the global one.
307 With __thread support, the variable's initializer takes care of this. */
308 __uselocale (LC_GLOBAL_LOCALE
);
310 /* Make gdb aware of new thread */
311 if (__pthread_threads_debug
&& __pthread_sig_debug
> 0) {
312 request
.req_thread
= self
;
313 request
.req_kind
= REQ_DEBUG
;
314 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
315 (char *) &request
, sizeof(request
)));
318 /* Run the thread code */
319 outcome
= self
->p_start_args
.start_routine(THREAD_GETMEM(self
,
321 /* Exit with the given return value */
322 __pthread_do_exit(outcome
, CURRENT_STACK_FRAME
);
326 __attribute__ ((noreturn
))
327 pthread_start_thread_event(void *arg
)
329 pthread_descr self
= (pthread_descr
) arg
;
331 #ifdef INIT_THREAD_SELF
332 INIT_THREAD_SELF(self
, self
->p_nr
);
334 /* Make sure our pid field is initialized, just in case we get there
335 before our father has initialized it. */
336 THREAD_SETMEM(self
, p_pid
, __getpid());
337 /* Get the lock the manager will free once all is correctly set up. */
338 __pthread_lock (THREAD_GETMEM(self
, p_lock
), NULL
);
339 /* Free it immediately. */
340 __pthread_unlock (THREAD_GETMEM(self
, p_lock
));
342 /* Continue with the real function. */
343 pthread_start_thread (arg
);
346 #if defined USE_TLS && !FLOATING_STACKS
347 # error "TLS can only work with floating stacks"
350 static int pthread_allocate_stack(const pthread_attr_t
*attr
,
351 pthread_descr default_new_thread
,
353 char ** out_new_thread
,
354 char ** out_new_thread_bottom
,
355 char ** out_guardaddr
,
356 size_t * out_guardsize
)
358 pthread_descr new_thread
;
359 char * new_thread_bottom
;
361 size_t stacksize
, guardsize
;
364 /* TLS cannot work with fixed thread descriptor addresses. */
365 assert (default_new_thread
== NULL
);
368 if (attr
!= NULL
&& attr
->__stackaddr_set
)
370 #ifdef _STACK_GROWS_UP
371 /* The user provided a stack. */
373 /* This value is not needed. */
374 new_thread
= (pthread_descr
) attr
->__stackaddr
;
375 new_thread_bottom
= (char *) new_thread
;
377 new_thread
= (pthread_descr
) attr
->__stackaddr
;
378 new_thread_bottom
= (char *) (new_thread
+ 1);
380 guardaddr
= attr
->__stackaddr
+ attr
->__stacksize
;
383 /* The user provided a stack. For now we interpret the supplied
384 address as 1 + the highest addr. in the stack segment. If a
385 separate register stack is needed, we place it at the low end
386 of the segment, relying on the associated stacksize to
387 determine the low end of the segment. This differs from many
388 (but not all) other pthreads implementations. The intent is
389 that on machines with a single stack growing toward higher
390 addresses, stackaddr would be the lowest address in the stack
391 segment, so that it is consistently close to the initial sp
394 new_thread
= (pthread_descr
) attr
->__stackaddr
;
397 (pthread_descr
) ((long)(attr
->__stackaddr
) & -sizeof(void *)) - 1;
399 new_thread_bottom
= (char *) attr
->__stackaddr
- attr
->__stacksize
;
400 guardaddr
= new_thread_bottom
;
404 __pthread_nonstandard_stacks
= 1;
407 /* Clear the thread data structure. */
408 memset (new_thread
, '\0', sizeof (*new_thread
));
413 #ifdef NEED_SEPARATE_REGISTER_STACK
414 const size_t granularity
= 2 * pagesize
;
415 /* Try to make stacksize/2 a multiple of pagesize */
417 const size_t granularity
= pagesize
;
421 /* Allocate space for stack and thread descriptor at default address */
425 guardsize
= page_roundup (attr
->__guardsize
, granularity
);
426 stacksize
= __pthread_max_stacksize
- guardsize
;
427 stacksize
= MIN (stacksize
,
428 page_roundup (attr
->__stacksize
, granularity
));
432 guardsize
= granularity
;
433 stacksize
= __pthread_max_stacksize
- guardsize
;
436 map_addr
= mmap(NULL
, stacksize
+ guardsize
,
437 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
438 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
439 if (map_addr
== MAP_FAILED
)
440 /* No more memory available. */
443 # ifdef NEED_SEPARATE_REGISTER_STACK
444 guardaddr
= map_addr
+ stacksize
/ 2;
446 mprotect (guardaddr
, guardsize
, PROT_NONE
);
448 new_thread_bottom
= (char *) map_addr
;
450 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
453 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
456 # elif _STACK_GROWS_DOWN
457 guardaddr
= map_addr
;
459 mprotect (guardaddr
, guardsize
, PROT_NONE
);
461 new_thread_bottom
= (char *) map_addr
+ guardsize
;
463 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
));
465 new_thread
= ((pthread_descr
) (new_thread_bottom
+ stacksize
)) - 1;
467 # elif _STACK_GROWS_UP
468 guardaddr
= map_addr
+ stacksize
;
470 mprotect (guardaddr
, guardsize
, PROT_NONE
);
472 new_thread
= (pthread_descr
) map_addr
;
474 new_thread_bottom
= (char *) new_thread
;
476 new_thread_bottom
= (char *) (new_thread
+ 1);
479 # error You must define a stack direction
480 # endif /* Stack direction */
481 #else /* !FLOATING_STACKS */
486 guardsize
= page_roundup (attr
->__guardsize
, granularity
);
487 stacksize
= STACK_SIZE
- guardsize
;
488 stacksize
= MIN (stacksize
,
489 page_roundup (attr
->__stacksize
, granularity
));
493 guardsize
= granularity
;
494 stacksize
= STACK_SIZE
- granularity
;
497 # ifdef NEED_SEPARATE_REGISTER_STACK
498 new_thread
= default_new_thread
;
499 new_thread_bottom
= (char *) (new_thread
+ 1) - stacksize
- guardsize
;
500 /* Includes guard area, unlike the normal case. Use the bottom
501 end of the segment as backing store for the register stack.
502 Needed on IA64. In this case, we also map the entire stack at
503 once. According to David Mosberger, that's cheaper. It also
504 avoids the risk of intermittent failures due to other mappings
505 in the same region. The cost is that we might be able to map
506 slightly fewer stacks. */
508 /* First the main stack: */
509 map_addr
= (caddr_t
)((char *)(new_thread
+ 1) - stacksize
/ 2);
510 res_addr
= mmap(map_addr
, stacksize
/ 2,
511 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
512 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
513 if (res_addr
!= map_addr
)
515 /* Bad luck, this segment is already mapped. */
516 if (res_addr
!= MAP_FAILED
)
517 munmap(res_addr
, stacksize
/ 2);
520 /* Then the register stack: */
521 map_addr
= (caddr_t
)new_thread_bottom
;
522 res_addr
= mmap(map_addr
, stacksize
/2,
523 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
524 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
525 if (res_addr
!= map_addr
)
527 if (res_addr
!= MAP_FAILED
)
528 munmap(res_addr
, stacksize
/ 2);
529 munmap((caddr_t
)((char *)(new_thread
+ 1) - stacksize
/2),
534 guardaddr
= new_thread_bottom
+ stacksize
/2;
535 /* We leave the guard area in the middle unmapped. */
536 # else /* !NEED_SEPARATE_REGISTER_STACK */
537 # ifdef _STACK_GROWS_DOWN
538 new_thread
= default_new_thread
;
539 new_thread_bottom
= (char *) (new_thread
+ 1) - stacksize
;
540 map_addr
= new_thread_bottom
- guardsize
;
541 res_addr
= mmap(map_addr
, stacksize
+ guardsize
,
542 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
543 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
544 if (res_addr
!= map_addr
)
546 /* Bad luck, this segment is already mapped. */
547 if (res_addr
!= MAP_FAILED
)
548 munmap (res_addr
, stacksize
+ guardsize
);
552 /* We manage to get a stack. Protect the guard area pages if
554 guardaddr
= map_addr
;
556 mprotect (guardaddr
, guardsize
, PROT_NONE
);
558 /* The thread description goes at the bottom of this area, and
559 * the stack starts directly above it.
561 new_thread
= (pthread_descr
)((unsigned long)default_new_thread
&~ (STACK_SIZE
- 1));
562 map_addr
= mmap(new_thread
, stacksize
+ guardsize
,
563 PROT_READ
| PROT_WRITE
| PROT_EXEC
,
564 MAP_FIXED
| MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
565 if (map_addr
== MAP_FAILED
)
568 new_thread_bottom
= map_addr
+ sizeof(*new_thread
);
569 guardaddr
= map_addr
+ stacksize
;
571 mprotect (guardaddr
, guardsize
, PROT_NONE
);
573 # endif /* stack direction */
574 # endif /* !NEED_SEPARATE_REGISTER_STACK */
575 #endif /* !FLOATING_STACKS */
577 *out_new_thread
= (char *) new_thread
;
578 *out_new_thread_bottom
= new_thread_bottom
;
579 *out_guardaddr
= guardaddr
;
580 *out_guardsize
= guardsize
;
584 static int pthread_handle_create(pthread_t
*thread
, const pthread_attr_t
*attr
,
585 void * (*start_routine
)(void *), void *arg
,
586 sigset_t
* mask
, int father_pid
,
588 td_thr_events_t
*event_maskp
)
592 pthread_descr new_thread
;
594 char * new_thread_bottom
;
595 pthread_t new_thread_id
;
596 char *guardaddr
= NULL
;
597 size_t guardsize
= 0;
598 int pagesize
= __getpagesize();
602 new_thread
= _dl_allocate_tls (NULL
);
603 if (new_thread
== NULL
)
606 /* Prevent warnings. */
610 /* First check whether we have to change the policy and if yes, whether
611 we can do this. Normally this should be done by examining the
612 return value of the __sched_setscheduler call in pthread_start_thread
613 but this is hard to implement. FIXME */
614 if (attr
!= NULL
&& attr
->__schedpolicy
!= SCHED_OTHER
&& geteuid () != 0)
616 /* Find a free segment for the thread, and allocate a stack if needed */
617 for (sseg
= 2; ; sseg
++)
619 if (sseg
>= PTHREAD_THREADS_MAX
)
622 _dl_deallocate_tls (new_thread
, true);
626 if (__pthread_handles
[sseg
].h_descr
!= NULL
)
628 if (pthread_allocate_stack(attr
, thread_segment(sseg
),
629 pagesize
, &stack_addr
, &new_thread_bottom
,
630 &guardaddr
, &guardsize
) == 0)
633 new_thread
->p_stackaddr
= stack_addr
;
635 new_thread
= (pthread_descr
) stack_addr
;
640 __pthread_handles_num
++;
641 /* Allocate new thread identifier */
642 pthread_threads_counter
+= PTHREAD_THREADS_MAX
;
643 new_thread_id
= sseg
+ pthread_threads_counter
;
644 /* Initialize the thread descriptor. Elements which have to be
645 initialized to zero already have this value. */
646 new_thread
->p_header
.data
.tcb
= new_thread
;
647 new_thread
->p_header
.data
.self
= new_thread
;
648 new_thread
->p_tid
= new_thread_id
;
649 new_thread
->p_lock
= &(__pthread_handles
[sseg
].h_lock
);
650 new_thread
->p_cancelstate
= PTHREAD_CANCEL_ENABLE
;
651 new_thread
->p_canceltype
= PTHREAD_CANCEL_DEFERRED
;
652 #if !(USE_TLS && HAVE___THREAD)
653 new_thread
->p_errnop
= &new_thread
->p_errno
;
654 new_thread
->p_h_errnop
= &new_thread
->p_h_errno
;
655 new_thread
->p_resp
= &new_thread
->p_res
;
657 new_thread
->p_guardaddr
= guardaddr
;
658 new_thread
->p_guardsize
= guardsize
;
659 new_thread
->p_nr
= sseg
;
660 new_thread
->p_inheritsched
= attr
? attr
->__inheritsched
: 0;
661 /* Initialize the thread handle */
662 __pthread_init_lock(&__pthread_handles
[sseg
].h_lock
);
663 __pthread_handles
[sseg
].h_descr
= new_thread
;
664 __pthread_handles
[sseg
].h_bottom
= new_thread_bottom
;
665 /* Determine scheduling parameters for the thread */
666 new_thread
->p_start_args
.schedpolicy
= -1;
668 new_thread
->p_detached
= attr
->__detachstate
;
669 new_thread
->p_userstack
= attr
->__stackaddr_set
;
671 switch(attr
->__inheritsched
) {
672 case PTHREAD_EXPLICIT_SCHED
:
673 new_thread
->p_start_args
.schedpolicy
= attr
->__schedpolicy
;
674 memcpy (&new_thread
->p_start_args
.schedparam
, &attr
->__schedparam
,
675 sizeof (struct sched_param
));
677 case PTHREAD_INHERIT_SCHED
:
678 new_thread
->p_start_args
.schedpolicy
= __sched_getscheduler(father_pid
);
679 __sched_getparam(father_pid
, &new_thread
->p_start_args
.schedparam
);
682 new_thread
->p_priority
=
683 new_thread
->p_start_args
.schedparam
.sched_priority
;
685 /* Finish setting up arguments to pthread_start_thread */
686 new_thread
->p_start_args
.start_routine
= start_routine
;
687 new_thread
->p_start_args
.arg
= arg
;
688 new_thread
->p_start_args
.mask
= *mask
;
689 /* Make the new thread ID available already now. If any of the later
690 functions fail we return an error value and the caller must not use
691 the stored thread ID. */
692 *thread
= new_thread_id
;
693 /* Raise priority of thread manager if needed */
694 __pthread_manager_adjust_prio(new_thread
->p_priority
);
695 /* Do the cloning. We have to use two different functions depending
696 on whether we are debugging or not. */
697 pid
= 0; /* Note that the thread never can have PID zero. */
700 /* See whether the TD_CREATE event bit is set in any of the
702 int idx
= __td_eventword (TD_CREATE
);
703 uint32_t mask
= __td_eventmask (TD_CREATE
);
705 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
706 | event_maskp
->event_bits
[idx
])) != 0)
708 /* Lock the mutex the child will use now so that it will stop. */
709 __pthread_lock(new_thread
->p_lock
, NULL
);
711 /* We have to report this event. */
712 #ifdef NEED_SEPARATE_REGISTER_STACK
713 /* Perhaps this version should be used on all platforms. But
714 this requires that __clone2 be uniformly supported
717 And there is some argument for changing the __clone2
718 interface to pass sp and bsp instead, making it more IA64
719 specific, but allowing stacks to grow outward from each
720 other, to get less paging and fewer mmaps. */
721 pid
= __clone2(pthread_start_thread_event
,
722 (void **)new_thread_bottom
,
723 (char *)new_thread
- new_thread_bottom
,
724 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
725 __pthread_sig_cancel
, new_thread
);
726 #elif _STACK_GROWS_UP
727 pid
= __clone(pthread_start_thread_event
, (void **) new_thread_bottom
,
728 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
729 __pthread_sig_cancel
, new_thread
);
731 pid
= __clone(pthread_start_thread_event
, (void **) new_thread
,
732 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
733 __pthread_sig_cancel
, new_thread
);
738 /* Now fill in the information about the new thread in
739 the newly created thread's data structure. We cannot let
740 the new thread do this since we don't know whether it was
741 already scheduled when we send the event. */
742 new_thread
->p_eventbuf
.eventdata
= new_thread
;
743 new_thread
->p_eventbuf
.eventnum
= TD_CREATE
;
744 __pthread_last_event
= new_thread
;
746 /* We have to set the PID here since the callback function
747 in the debug library will need it and we cannot guarantee
748 the child got scheduled before the debugger. */
749 new_thread
->p_pid
= pid
;
751 /* Now call the function which signals the event. */
752 __linuxthreads_create_event ();
754 /* Now restart the thread. */
755 __pthread_unlock(new_thread
->p_lock
);
761 #ifdef NEED_SEPARATE_REGISTER_STACK
762 pid
= __clone2(pthread_start_thread
,
763 (void **)new_thread_bottom
,
764 (char *)stack_addr
- new_thread_bottom
,
765 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
766 __pthread_sig_cancel
, new_thread
);
767 #elif _STACK_GROWS_UP
768 pid
= __clone(pthread_start_thread
, (void *) new_thread_bottom
,
769 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
770 __pthread_sig_cancel
, new_thread
);
772 pid
= __clone(pthread_start_thread
, stack_addr
,
773 CLONE_VM
| CLONE_FS
| CLONE_FILES
| CLONE_SIGHAND
|
774 __pthread_sig_cancel
, new_thread
);
775 #endif /* !NEED_SEPARATE_REGISTER_STACK */
778 /* Check if cloning succeeded */
780 /* Free the stack if we allocated it */
781 if (attr
== NULL
|| !attr
->__stackaddr_set
)
783 #ifdef NEED_SEPARATE_REGISTER_STACK
784 size_t stacksize
= ((char *)(new_thread
->p_guardaddr
)
785 - new_thread_bottom
);
786 munmap((caddr_t
)new_thread_bottom
,
787 2 * stacksize
+ new_thread
->p_guardsize
);
788 #elif _STACK_GROWS_UP
790 size_t stacksize
= guardaddr
- stack_addr
;
791 munmap(stack_addr
, stacksize
+ guardsize
);
793 size_t stacksize
= guardaddr
- (char *)new_thread
;
794 munmap(new_thread
, stacksize
+ guardsize
);
798 size_t stacksize
= stack_addr
- new_thread_bottom
;
800 size_t stacksize
= (char *)(new_thread
+1) - new_thread_bottom
;
802 munmap(new_thread_bottom
- guardsize
, guardsize
+ stacksize
);
806 _dl_deallocate_tls (new_thread
, true);
808 __pthread_handles
[sseg
].h_descr
= NULL
;
809 __pthread_handles
[sseg
].h_bottom
= NULL
;
810 __pthread_handles_num
--;
813 /* Insert new thread in doubly linked list of active threads */
814 new_thread
->p_prevlive
= __pthread_main_thread
;
815 new_thread
->p_nextlive
= __pthread_main_thread
->p_nextlive
;
816 __pthread_main_thread
->p_nextlive
->p_prevlive
= new_thread
;
817 __pthread_main_thread
->p_nextlive
= new_thread
;
818 /* Set pid field of the new thread, in case we get there before the
820 new_thread
->p_pid
= pid
;
825 /* Try to free the resources of a thread when requested by pthread_join
826 or pthread_detach on a terminated thread. */
828 static void pthread_free(pthread_descr th
)
830 pthread_handle handle
;
831 pthread_readlock_info
*iter
, *next
;
833 ASSERT(th
->p_exited
);
834 /* Make the handle invalid */
835 handle
= thread_handle(th
->p_tid
);
836 __pthread_lock(&handle
->h_lock
, NULL
);
837 handle
->h_descr
= NULL
;
838 handle
->h_bottom
= (char *)(-1L);
839 __pthread_unlock(&handle
->h_lock
);
841 FREE_THREAD(th
, th
->p_nr
);
843 /* One fewer threads in __pthread_handles */
844 __pthread_handles_num
--;
846 /* Destroy read lock list, and list of free read lock structures.
847 If the former is not empty, it means the thread exited while
848 holding read locks! */
850 for (iter
= th
->p_readlock_list
; iter
!= NULL
; iter
= next
)
852 next
= iter
->pr_next
;
856 for (iter
= th
->p_readlock_free
; iter
!= NULL
; iter
= next
)
858 next
= iter
->pr_next
;
862 /* If initial thread, nothing to free */
863 if (!th
->p_userstack
)
865 size_t guardsize
= th
->p_guardsize
;
866 /* Free the stack and thread descriptor area */
867 char *guardaddr
= th
->p_guardaddr
;
868 #ifdef _STACK_GROWS_UP
870 size_t stacksize
= guardaddr
- th
->p_stackaddr
;
872 size_t stacksize
= guardaddr
- (char *)th
;
874 guardaddr
= (char *)th
;
876 /* Guardaddr is always set, even if guardsize is 0. This allows
877 us to compute everything else. */
879 size_t stacksize
= th
->p_stackaddr
- guardaddr
- guardsize
;
881 size_t stacksize
= (char *)(th
+1) - guardaddr
- guardsize
;
883 # ifdef NEED_SEPARATE_REGISTER_STACK
884 /* Take account of the register stack, which is below guardaddr. */
885 guardaddr
-= stacksize
;
889 /* Unmap the stack. */
890 munmap(guardaddr
, stacksize
+ guardsize
);
893 _dl_deallocate_tls (th
, true);
898 /* Handle threads that have exited */
900 static void pthread_exited(pid_t pid
)
904 /* Find thread with that pid */
905 for (th
= __pthread_main_thread
->p_nextlive
;
906 th
!= __pthread_main_thread
;
907 th
= th
->p_nextlive
) {
908 if (th
->p_pid
== pid
) {
909 /* Remove thread from list of active threads */
910 th
->p_nextlive
->p_prevlive
= th
->p_prevlive
;
911 th
->p_prevlive
->p_nextlive
= th
->p_nextlive
;
912 /* Mark thread as exited, and if detached, free its resources */
913 __pthread_lock(th
->p_lock
, NULL
);
915 /* If we have to signal this event do it now. */
916 if (th
->p_report_events
)
918 /* See whether TD_REAP is in any of the mask. */
919 int idx
= __td_eventword (TD_REAP
);
920 uint32_t mask
= __td_eventmask (TD_REAP
);
922 if ((mask
& (__pthread_threads_events
.event_bits
[idx
]
923 | th
->p_eventbuf
.eventmask
.event_bits
[idx
])) != 0)
925 /* Yep, we have to signal the reapage. */
926 th
->p_eventbuf
.eventnum
= TD_REAP
;
927 th
->p_eventbuf
.eventdata
= th
;
928 __pthread_last_event
= th
;
930 /* Now call the function to signal the event. */
931 __linuxthreads_reap_event();
934 detached
= th
->p_detached
;
935 __pthread_unlock(th
->p_lock
);
941 /* If all threads have exited and the main thread is pending on a
942 pthread_exit, wake up the main thread and terminate ourselves. */
943 if (main_thread_exiting
&&
944 __pthread_main_thread
->p_nextlive
== __pthread_main_thread
) {
945 restart(__pthread_main_thread
);
946 /* Same logic as REQ_MAIN_THREAD_EXIT. */
950 static void pthread_reap_children(void)
955 while ((pid
= __libc_waitpid(-1, &status
, WNOHANG
| __WCLONE
)) > 0) {
957 if (WIFSIGNALED(status
)) {
958 /* If a thread died due to a signal, send the same signal to
959 all other threads, including the main thread. */
960 pthread_kill_all_threads(WTERMSIG(status
), 1);
966 /* Try to free the resources of a thread when requested by pthread_join
967 or pthread_detach on a terminated thread. */
969 static void pthread_handle_free(pthread_t th_id
)
971 pthread_handle handle
= thread_handle(th_id
);
974 __pthread_lock(&handle
->h_lock
, NULL
);
975 if (nonexisting_handle(handle
, th_id
)) {
976 /* pthread_reap_children has deallocated the thread already,
977 nothing needs to be done */
978 __pthread_unlock(&handle
->h_lock
);
981 th
= handle
->h_descr
;
983 __pthread_unlock(&handle
->h_lock
);
986 /* The Unix process of the thread is still running.
987 Mark the thread as detached so that the thread manager will
988 deallocate its resources when the Unix process exits. */
990 __pthread_unlock(&handle
->h_lock
);
994 /* Send a signal to all running threads */
996 static void pthread_kill_all_threads(int sig
, int main_thread_also
)
999 for (th
= __pthread_main_thread
->p_nextlive
;
1000 th
!= __pthread_main_thread
;
1001 th
= th
->p_nextlive
) {
1002 kill(th
->p_pid
, sig
);
1004 if (main_thread_also
) {
1005 kill(__pthread_main_thread
->p_pid
, sig
);
1009 static void pthread_for_each_thread(void *arg
,
1010 void (*fn
)(void *, pthread_descr
))
1014 for (th
= __pthread_main_thread
->p_nextlive
;
1015 th
!= __pthread_main_thread
;
1016 th
= th
->p_nextlive
) {
1020 fn(arg
, __pthread_main_thread
);
1023 /* Process-wide exit() */
1025 static void pthread_handle_exit(pthread_descr issuing_thread
, int exitcode
)
1028 __pthread_exit_requested
= 1;
1029 __pthread_exit_code
= exitcode
;
1030 /* A forced asynchronous cancellation follows. Make sure we won't
1031 get stuck later in the main thread with a system lock being held
1032 by one of the cancelled threads. Ideally one would use the same
1033 code as in pthread_atfork(), but we can't distinguish system and
1034 user handlers there. */
1036 /* Send the CANCEL signal to all running threads, including the main
1037 thread, but excluding the thread from which the exit request originated
1038 (that thread must complete the exit, e.g. calling atexit functions
1039 and flushing stdio buffers). */
1040 for (th
= issuing_thread
->p_nextlive
;
1041 th
!= issuing_thread
;
1042 th
= th
->p_nextlive
) {
1043 kill(th
->p_pid
, __pthread_sig_cancel
);
1045 /* Now, wait for all these threads, so that they don't become zombies
1046 and their times are properly added to the thread manager's times. */
1047 for (th
= issuing_thread
->p_nextlive
;
1048 th
!= issuing_thread
;
1049 th
= th
->p_nextlive
) {
1050 waitpid(th
->p_pid
, NULL
, __WCLONE
);
1052 __fresetlockfiles();
1053 restart(issuing_thread
);
1057 /* Handler for __pthread_sig_cancel in thread manager thread */
1059 void __pthread_manager_sighandler(int sig
)
1061 int kick_manager
= terminated_children
== 0 && main_thread_exiting
;
1062 terminated_children
= 1;
1064 /* If the main thread is terminating, kick the thread manager loop
1065 each time some threads terminate. This eliminates a two second
1066 shutdown delay caused by the thread manager sleeping in the
1067 call to __poll(). Instead, the thread manager is kicked into
1068 action, reaps the outstanding threads and resumes the main thread
1069 so that it can complete the shutdown. */
1072 struct pthread_request request
;
1073 request
.req_thread
= 0;
1074 request
.req_kind
= REQ_KICK
;
1075 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request
,
1076 (char *) &request
, sizeof(request
)));
1080 /* Adjust priority of thread manager so that it always run at a priority
1081 higher than all threads */
1083 void __pthread_manager_adjust_prio(int thread_prio
)
1085 struct sched_param param
;
1087 if (thread_prio
<= manager_thread
->p_priority
) return;
1088 param
.sched_priority
=
1089 thread_prio
< __sched_get_priority_max(SCHED_FIFO
)
1090 ? thread_prio
+ 1 : thread_prio
;
1091 __sched_setscheduler(manager_thread
->p_pid
, SCHED_FIFO
, ¶m
);
1092 manager_thread
->p_priority
= thread_prio
;