1 /* Copyright (C) 2002-2007,2008,2009,2010 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <hp-timing.h>
28 #include <libc-internal.h>
30 #include <kernel-features.h>
32 #include <shlib-compat.h>
35 /* Local function to start thread and handle cleanup. */
36 static int start_thread (void *arg
);
39 /* Nozero if debugging mode is enabled. */
42 /* Globally enabled events. */
43 static td_thr_events_t __nptl_threads_events __attribute_used__
;
45 /* Pointer to descriptor with the last event. */
46 static struct pthread
*__nptl_last_event __attribute_used__
;
48 /* Number of threads running. */
49 unsigned int __nptl_nthreads
= 1;
52 /* Code to allocate and deallocate a stack. */
53 #include "allocatestack.c"
55 /* Code to create the thread. */
56 #include <createthread.c>
61 __find_in_stack_list (pd
)
65 struct pthread
*result
= NULL
;
67 lll_lock (stack_cache_lock
, LLL_PRIVATE
);
69 list_for_each (entry
, &stack_used
)
73 curp
= list_entry (entry
, struct pthread
, list
);
82 list_for_each (entry
, &__stack_user
)
86 curp
= list_entry (entry
, struct pthread
, list
);
94 lll_unlock (stack_cache_lock
, LLL_PRIVATE
);
100 /* Deallocate POSIX thread-local-storage. */
103 __nptl_deallocate_tsd (void)
105 struct pthread
*self
= THREAD_SELF
;
107 /* Maybe no data was ever allocated. This happens often so we have
109 if (THREAD_GETMEM (self
, specific_used
))
119 /* So far no new nonzero data entry. */
120 THREAD_SETMEM (self
, specific_used
, false);
122 for (cnt
= idx
= 0; cnt
< PTHREAD_KEY_1STLEVEL_SIZE
; ++cnt
)
124 struct pthread_key_data
*level2
;
126 level2
= THREAD_GETMEM_NC (self
, specific
, cnt
);
132 for (inner
= 0; inner
< PTHREAD_KEY_2NDLEVEL_SIZE
;
135 void *data
= level2
[inner
].data
;
139 /* Always clear the data. */
140 level2
[inner
].data
= NULL
;
142 /* Make sure the data corresponds to a valid
143 key. This test fails if the key was
144 deallocated and also if it was
145 re-allocated. It is the user's
146 responsibility to free the memory in this
148 if (level2
[inner
].seq
149 == __pthread_keys
[idx
].seq
150 /* It is not necessary to register a destructor
152 && __pthread_keys
[idx
].destr
!= NULL
)
153 /* Call the user-provided destructor. */
154 __pthread_keys
[idx
].destr (data
);
159 idx
+= PTHREAD_KEY_1STLEVEL_SIZE
;
162 if (THREAD_GETMEM (self
, specific_used
) == 0)
163 /* No data has been modified. */
166 /* We only repeat the process a fixed number of times. */
167 while (__builtin_expect (++round
< PTHREAD_DESTRUCTOR_ITERATIONS
, 0));
169 /* Just clear the memory of the first block for reuse. */
170 memset (&THREAD_SELF
->specific_1stblock
, '\0',
171 sizeof (self
->specific_1stblock
));
174 /* Free the memory for the other blocks. */
175 for (cnt
= 1; cnt
< PTHREAD_KEY_1STLEVEL_SIZE
; ++cnt
)
177 struct pthread_key_data
*level2
;
179 level2
= THREAD_GETMEM_NC (self
, specific
, cnt
);
182 /* The first block is allocated as part of the thread
185 THREAD_SETMEM_NC (self
, specific
, cnt
, NULL
);
189 THREAD_SETMEM (self
, specific_used
, false);
194 /* Deallocate a thread's stack after optionally making sure the thread
195 descriptor is still valid. */
198 __free_tcb (struct pthread
*pd
)
200 /* The thread is exiting now. */
201 if (__builtin_expect (atomic_bit_test_set (&pd
->cancelhandling
,
202 TERMINATED_BIT
) == 0, 1))
204 /* Remove the descriptor from the list. */
205 if (DEBUGGING_P
&& __find_in_stack_list (pd
) == NULL
)
206 /* Something is really wrong. The descriptor for a still
207 running thread is gone. */
211 if (__builtin_expect (pd
->tpp
!= NULL
, 0))
213 struct priority_protection_data
*tpp
= pd
->tpp
;
219 /* Queue the stack memory block for reuse and exit the process. The
220 kernel will signal via writing to the address returned by
221 QUEUE-STACK when the stack is available. */
222 __deallocate_stack (pd
);
228 start_thread (void *arg
)
230 struct pthread
*pd
= (struct pthread
*) arg
;
233 /* Remember the time when the thread was started. */
236 THREAD_SETMEM (pd
, cpuclock_offset
, now
);
239 /* Initialize resolver state pointer. */
242 /* Allow setxid from now onwards. */
243 if (__builtin_expect (atomic_exchange_acq (&pd
->setxid_futex
, 0) == -2, 0))
244 lll_futex_wake (&pd
->setxid_futex
, 1, LLL_PRIVATE
);
246 #ifdef __NR_set_robust_list
247 # ifndef __ASSUME_SET_ROBUST_LIST
248 if (__set_robust_list_avail
>= 0)
251 INTERNAL_SYSCALL_DECL (err
);
252 /* This call should never fail because the initial call in init.c
254 INTERNAL_SYSCALL (set_robust_list
, err
, 2, &pd
->robust_head
,
255 sizeof (struct robust_list_head
));
259 /* If the parent was running cancellation handlers while creating
260 the thread the new thread inherited the signal mask. Reset the
261 cancellation signal mask. */
262 if (__builtin_expect (pd
->parent_cancelhandling
& CANCELING_BITMASK
, 0))
264 INTERNAL_SYSCALL_DECL (err
);
266 __sigemptyset (&mask
);
267 __sigaddset (&mask
, SIGCANCEL
);
268 (void) INTERNAL_SYSCALL (rt_sigprocmask
, err
, 4, SIG_UNBLOCK
, &mask
,
272 /* This is where the try/finally block should be created. For
273 compilers without that support we do use setjmp. */
274 struct pthread_unwind_buf unwind_buf
;
276 /* No previous handlers. */
277 unwind_buf
.priv
.data
.prev
= NULL
;
278 unwind_buf
.priv
.data
.cleanup
= NULL
;
281 not_first_call
= setjmp ((struct __jmp_buf_tag
*) unwind_buf
.cancel_jmp_buf
);
282 if (__builtin_expect (! not_first_call
, 1))
284 /* Store the new cleanup handler info. */
285 THREAD_SETMEM (pd
, cleanup_jmp_buf
, &unwind_buf
);
287 if (__builtin_expect (pd
->stopped_start
, 0))
289 int oldtype
= CANCEL_ASYNC ();
291 /* Get the lock the parent locked to force synchronization. */
292 lll_lock (pd
->lock
, LLL_PRIVATE
);
293 /* And give it up right away. */
294 lll_unlock (pd
->lock
, LLL_PRIVATE
);
296 CANCEL_RESET (oldtype
);
299 /* Run the code the user provided. */
300 #ifdef CALL_THREAD_FCT
301 THREAD_SETMEM (pd
, result
, CALL_THREAD_FCT (pd
));
303 THREAD_SETMEM (pd
, result
, pd
->start_routine (pd
->arg
));
307 /* Run the destructor for the thread-local data. */
308 __nptl_deallocate_tsd ();
310 /* Clean up any state libc stored in thread-local variables. */
311 __libc_thread_freeres ();
313 /* If this is the last thread we terminate the process now. We
314 do not notify the debugger, it might just irritate it if there
315 is no thread left. */
316 if (__builtin_expect (atomic_decrement_and_test (&__nptl_nthreads
), 0))
317 /* This was the last thread. */
320 /* Report the death of the thread if this is wanted. */
321 if (__builtin_expect (pd
->report_events
, 0))
323 /* See whether TD_DEATH is in any of the mask. */
324 const int idx
= __td_eventword (TD_DEATH
);
325 const uint32_t mask
= __td_eventmask (TD_DEATH
);
327 if ((mask
& (__nptl_threads_events
.event_bits
[idx
]
328 | pd
->eventbuf
.eventmask
.event_bits
[idx
])) != 0)
330 /* Yep, we have to signal the death. Add the descriptor to
331 the list but only if it is not already on it. */
332 if (pd
->nextevent
== NULL
)
334 pd
->eventbuf
.eventnum
= TD_DEATH
;
335 pd
->eventbuf
.eventdata
= pd
;
338 pd
->nextevent
= __nptl_last_event
;
339 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event
,
343 /* Now call the function to signal the event. */
344 __nptl_death_event ();
348 /* The thread is exiting now. Don't set this bit until after we've hit
349 the event-reporting breakpoint, so that td_thr_get_info on us while at
350 the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
351 atomic_bit_set (&pd
->cancelhandling
, EXITING_BIT
);
353 #ifndef __ASSUME_SET_ROBUST_LIST
354 /* If this thread has any robust mutexes locked, handle them now. */
355 # if __WORDSIZE == 64
356 void *robust
= pd
->robust_head
.list
;
358 __pthread_slist_t
*robust
= pd
->robust_list
.__next
;
360 /* We let the kernel do the notification if it is able to do so.
361 If we have to do it here there for sure are no PI mutexes involved
362 since the kernel support for them is even more recent. */
363 if (__set_robust_list_avail
< 0
364 && __builtin_expect (robust
!= (void *) &pd
->robust_head
, 0))
368 struct __pthread_mutex_s
*this = (struct __pthread_mutex_s
*)
369 ((char *) robust
- offsetof (struct __pthread_mutex_s
,
371 robust
= *((void **) robust
);
373 # ifdef __PTHREAD_MUTEX_HAVE_PREV
374 this->__list
.__prev
= NULL
;
376 this->__list
.__next
= NULL
;
378 lll_robust_dead (this->__lock
, /* XYZ */ LLL_SHARED
);
380 while (robust
!= (void *) &pd
->robust_head
);
384 /* Mark the memory of the stack as usable to the kernel. We free
385 everything except for the space used for the TCB itself. */
386 size_t pagesize_m1
= __getpagesize () - 1;
387 #ifdef _STACK_GROWS_DOWN
388 char *sp
= CURRENT_STACK_FRAME
;
389 size_t freesize
= (sp
- (char *) pd
->stackblock
) & ~pagesize_m1
;
393 assert (freesize
< pd
->stackblock_size
);
394 if (freesize
> PTHREAD_STACK_MIN
)
395 madvise (pd
->stackblock
, freesize
- PTHREAD_STACK_MIN
, MADV_DONTNEED
);
397 /* If the thread is detached free the TCB. */
398 if (IS_DETACHED (pd
))
401 else if (__builtin_expect (pd
->cancelhandling
& SETXID_BITMASK
, 0))
403 /* Some other thread might call any of the setXid functions and expect
404 us to reply. In this case wait until we did that. */
406 lll_futex_wait (&pd
->setxid_futex
, 0, LLL_PRIVATE
);
407 while (pd
->cancelhandling
& SETXID_BITMASK
);
409 /* Reset the value so that the stack can be reused. */
410 pd
->setxid_futex
= 0;
413 /* We cannot call '_exit' here. '_exit' will terminate the process.
415 The 'exit' implementation in the kernel will signal when the
416 process is really dead since 'clone' got passed the CLONE_CLEARTID
417 flag. The 'tid' field in the TCB will be set to zero.
419 The exit code is zero since in case all threads exit by calling
420 'pthread_exit' the exit status must be 0 (zero). */
421 __exit_thread_inline (0);
428 /* Default thread attributes for the case when the user does not
430 static const struct pthread_attr default_attr
=
432 /* Just some value > 0 which gets rounded to the nearest page size. */
438 __pthread_create_2_1 (newthread
, attr
, start_routine
, arg
)
439 pthread_t
*newthread
;
440 const pthread_attr_t
*attr
;
441 void *(*start_routine
) (void *);
446 const struct pthread_attr
*iattr
= (struct pthread_attr
*) attr
;
448 /* Is this the best idea? On NUMA machines this could mean
449 accessing far-away memory. */
450 iattr
= &default_attr
;
452 struct pthread
*pd
= NULL
;
453 int err
= ALLOCATE_STACK (iattr
, &pd
);
454 if (__builtin_expect (err
!= 0, 0))
455 /* Something went wrong. Maybe a parameter of the attributes is
456 invalid or we could not allocate memory. */
460 /* Initialize the TCB. All initializations with zero should be
461 performed in 'get_cached_stack'. This way we avoid doing this if
462 the stack freshly allocated with 'mmap'. */
465 /* Reference to the TCB itself. */
466 pd
->header
.self
= pd
;
468 /* Self-reference for TLS. */
472 /* Store the address of the start routine and the parameter. Since
473 we do not start the function directly the stillborn thread will
474 get the information from its thread descriptor. */
475 pd
->start_routine
= start_routine
;
478 /* Copy the thread attribute flags. */
479 struct pthread
*self
= THREAD_SELF
;
480 pd
->flags
= ((iattr
->flags
& ~(ATTR_FLAG_SCHED_SET
| ATTR_FLAG_POLICY_SET
))
481 | (self
->flags
& (ATTR_FLAG_SCHED_SET
| ATTR_FLAG_POLICY_SET
)));
483 /* Initialize the field for the ID of the thread which is waiting
484 for us. This is a self-reference in case the thread is created
486 pd
->joinid
= iattr
->flags
& ATTR_FLAG_DETACHSTATE
? pd
: NULL
;
488 /* The debug events are inherited from the parent. */
489 pd
->eventbuf
= self
->eventbuf
;
492 /* Copy the parent's scheduling parameters. The flags will say what
493 is valid and what is not. */
494 pd
->schedpolicy
= self
->schedpolicy
;
495 pd
->schedparam
= self
->schedparam
;
497 /* Copy the stack guard canary. */
498 #ifdef THREAD_COPY_STACK_GUARD
499 THREAD_COPY_STACK_GUARD (pd
);
502 /* Copy the pointer guard value. */
503 #ifdef THREAD_COPY_POINTER_GUARD
504 THREAD_COPY_POINTER_GUARD (pd
);
507 /* Determine scheduling parameters for the thread. */
509 && __builtin_expect ((iattr
->flags
& ATTR_FLAG_NOTINHERITSCHED
) != 0, 0)
510 && (iattr
->flags
& (ATTR_FLAG_SCHED_SET
| ATTR_FLAG_POLICY_SET
)) != 0)
512 INTERNAL_SYSCALL_DECL (scerr
);
514 /* Use the scheduling parameters the user provided. */
515 if (iattr
->flags
& ATTR_FLAG_POLICY_SET
)
516 pd
->schedpolicy
= iattr
->schedpolicy
;
517 else if ((pd
->flags
& ATTR_FLAG_POLICY_SET
) == 0)
519 pd
->schedpolicy
= INTERNAL_SYSCALL (sched_getscheduler
, scerr
, 1, 0);
520 pd
->flags
|= ATTR_FLAG_POLICY_SET
;
523 if (iattr
->flags
& ATTR_FLAG_SCHED_SET
)
524 memcpy (&pd
->schedparam
, &iattr
->schedparam
,
525 sizeof (struct sched_param
));
526 else if ((pd
->flags
& ATTR_FLAG_SCHED_SET
) == 0)
528 INTERNAL_SYSCALL (sched_getparam
, scerr
, 2, 0, &pd
->schedparam
);
529 pd
->flags
|= ATTR_FLAG_SCHED_SET
;
532 /* Check for valid priorities. */
533 int minprio
= INTERNAL_SYSCALL (sched_get_priority_min
, scerr
, 1,
535 int maxprio
= INTERNAL_SYSCALL (sched_get_priority_max
, scerr
, 1,
537 if (pd
->schedparam
.sched_priority
< minprio
538 || pd
->schedparam
.sched_priority
> maxprio
)
540 /* Perhaps a thread wants to change the IDs and if waiting
541 for this stillborn thread. */
542 if (__builtin_expect (atomic_exchange_acq (&pd
->setxid_futex
, 0)
544 lll_futex_wake (&pd
->setxid_futex
, 1, LLL_PRIVATE
);
546 __deallocate_stack (pd
);
552 /* Pass the descriptor to the caller. */
553 *newthread
= (pthread_t
) pd
;
555 /* Start the thread. */
556 return create_thread (pd
, iattr
, STACK_VARIABLES_ARGS
);
558 versioned_symbol (libpthread
, __pthread_create_2_1
, pthread_create
, GLIBC_2_1
);
561 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
563 __pthread_create_2_0 (newthread
, attr
, start_routine
, arg
)
564 pthread_t
*newthread
;
565 const pthread_attr_t
*attr
;
566 void *(*start_routine
) (void *);
569 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
570 the old size and access to the new members might crash the program.
571 We convert the struct now. */
572 struct pthread_attr new_attr
;
576 struct pthread_attr
*iattr
= (struct pthread_attr
*) attr
;
577 size_t ps
= __getpagesize ();
579 /* Copy values from the user-provided attributes. */
580 new_attr
.schedparam
= iattr
->schedparam
;
581 new_attr
.schedpolicy
= iattr
->schedpolicy
;
582 new_attr
.flags
= iattr
->flags
;
584 /* Fill in default values for the fields not present in the old
586 new_attr
.guardsize
= ps
;
587 new_attr
.stackaddr
= NULL
;
588 new_attr
.stacksize
= 0;
589 new_attr
.cpuset
= NULL
;
591 /* We will pass this value on to the real implementation. */
592 attr
= (pthread_attr_t
*) &new_attr
;
595 return __pthread_create_2_1 (newthread
, attr
, start_routine
, arg
);
597 compat_symbol (libpthread
, __pthread_create_2_0
, pthread_create
,
601 /* Information for libthread_db. */
603 #include "../nptl_db/db_info.c"
605 /* If pthread_create is present, libgcc_eh.a and libsupc++.a expects some other POSIX thread
606 functions to be present as well. */
607 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_lock
)
608 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_trylock
)
609 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_unlock
)
611 PTHREAD_STATIC_FN_REQUIRE (pthread_once
)
612 PTHREAD_STATIC_FN_REQUIRE (pthread_cancel
)
614 PTHREAD_STATIC_FN_REQUIRE (pthread_key_create
)
615 PTHREAD_STATIC_FN_REQUIRE (pthread_key_delete
)
616 PTHREAD_STATIC_FN_REQUIRE (pthread_setspecific
)
617 PTHREAD_STATIC_FN_REQUIRE (pthread_getspecific
)