Support C11 __STDC_SOURCE__ and _ISOC11_SOURCE
[glibc.git] / nptl / pthread_create.c
blob6250d03ff6bab1ab66d9a0f32ebe7bcdde6ddf42
1 /* Copyright (C) 2002-2007,2008,2009,2010,2011 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
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <errno.h>
22 #include <stdbool.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include "pthreadP.h"
26 #include <hp-timing.h>
27 #include <ldsodefs.h>
28 #include <atomic.h>
29 #include <libc-internal.h>
30 #include <resolv.h>
31 #include <kernel-features.h>
33 #include <shlib-compat.h>
36 /* Local function to start thread and handle cleanup. */
37 static int start_thread (void *arg);
40 /* Nozero if debugging mode is enabled. */
41 int __pthread_debug;
43 /* Globally enabled events. */
44 static td_thr_events_t __nptl_threads_events __attribute_used__;
46 /* Pointer to descriptor with the last event. */
47 static struct pthread *__nptl_last_event __attribute_used__;
49 /* Number of threads running. */
50 unsigned int __nptl_nthreads = 1;
53 /* Code to allocate and deallocate a stack. */
54 #include "allocatestack.c"
56 /* Code to create the thread. */
57 #include <createthread.c>
60 struct pthread *
61 internal_function
62 __find_in_stack_list (pd)
63 struct pthread *pd;
65 list_t *entry;
66 struct pthread *result = NULL;
68 lll_lock (stack_cache_lock, LLL_PRIVATE);
70 list_for_each (entry, &stack_used)
72 struct pthread *curp;
74 curp = list_entry (entry, struct pthread, list);
75 if (curp == pd)
77 result = curp;
78 break;
82 if (result == NULL)
83 list_for_each (entry, &__stack_user)
85 struct pthread *curp;
87 curp = list_entry (entry, struct pthread, list);
88 if (curp == pd)
90 result = curp;
91 break;
95 lll_unlock (stack_cache_lock, LLL_PRIVATE);
97 return result;
101 /* Deallocate POSIX thread-local-storage. */
102 void
103 attribute_hidden
104 __nptl_deallocate_tsd (void)
106 struct pthread *self = THREAD_SELF;
108 /* Maybe no data was ever allocated. This happens often so we have
109 a flag for this. */
110 if (THREAD_GETMEM (self, specific_used))
112 size_t round;
113 size_t cnt;
115 round = 0;
118 size_t idx;
120 /* So far no new nonzero data entry. */
121 THREAD_SETMEM (self, specific_used, false);
123 for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
125 struct pthread_key_data *level2;
127 level2 = THREAD_GETMEM_NC (self, specific, cnt);
129 if (level2 != NULL)
131 size_t inner;
133 for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE;
134 ++inner, ++idx)
136 void *data = level2[inner].data;
138 if (data != NULL)
140 /* Always clear the data. */
141 level2[inner].data = NULL;
143 /* Make sure the data corresponds to a valid
144 key. This test fails if the key was
145 deallocated and also if it was
146 re-allocated. It is the user's
147 responsibility to free the memory in this
148 case. */
149 if (level2[inner].seq
150 == __pthread_keys[idx].seq
151 /* It is not necessary to register a destructor
152 function. */
153 && __pthread_keys[idx].destr != NULL)
154 /* Call the user-provided destructor. */
155 __pthread_keys[idx].destr (data);
159 else
160 idx += PTHREAD_KEY_1STLEVEL_SIZE;
163 if (THREAD_GETMEM (self, specific_used) == 0)
164 /* No data has been modified. */
165 goto just_free;
167 /* We only repeat the process a fixed number of times. */
168 while (__builtin_expect (++round < PTHREAD_DESTRUCTOR_ITERATIONS, 0));
170 /* Just clear the memory of the first block for reuse. */
171 memset (&THREAD_SELF->specific_1stblock, '\0',
172 sizeof (self->specific_1stblock));
174 just_free:
175 /* Free the memory for the other blocks. */
176 for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
178 struct pthread_key_data *level2;
180 level2 = THREAD_GETMEM_NC (self, specific, cnt);
181 if (level2 != NULL)
183 /* The first block is allocated as part of the thread
184 descriptor. */
185 free (level2);
186 THREAD_SETMEM_NC (self, specific, cnt, NULL);
190 THREAD_SETMEM (self, specific_used, false);
195 /* Deallocate a thread's stack after optionally making sure the thread
196 descriptor is still valid. */
197 void
198 internal_function
199 __free_tcb (struct pthread *pd)
201 /* The thread is exiting now. */
202 if (__builtin_expect (atomic_bit_test_set (&pd->cancelhandling,
203 TERMINATED_BIT) == 0, 1))
205 /* Remove the descriptor from the list. */
206 if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
207 /* Something is really wrong. The descriptor for a still
208 running thread is gone. */
209 abort ();
211 /* Free TPP data. */
212 if (__builtin_expect (pd->tpp != NULL, 0))
214 struct priority_protection_data *tpp = pd->tpp;
216 pd->tpp = NULL;
217 free (tpp);
220 /* Queue the stack memory block for reuse and exit the process. The
221 kernel will signal via writing to the address returned by
222 QUEUE-STACK when the stack is available. */
223 __deallocate_stack (pd);
228 static int
229 start_thread (void *arg)
231 struct pthread *pd = (struct pthread *) arg;
233 #if HP_TIMING_AVAIL
234 /* Remember the time when the thread was started. */
235 hp_timing_t now;
236 HP_TIMING_NOW (now);
237 THREAD_SETMEM (pd, cpuclock_offset, now);
238 #endif
240 /* Initialize resolver state pointer. */
241 __resp = &pd->res;
243 /* Initialize pointers to locale data. */
244 __ctype_init ();
246 /* Allow setxid from now onwards. */
247 if (__builtin_expect (atomic_exchange_acq (&pd->setxid_futex, 0) == -2, 0))
248 lll_futex_wake (&pd->setxid_futex, 1, LLL_PRIVATE);
250 #ifdef __NR_set_robust_list
251 # ifndef __ASSUME_SET_ROBUST_LIST
252 if (__set_robust_list_avail >= 0)
253 # endif
255 INTERNAL_SYSCALL_DECL (err);
256 /* This call should never fail because the initial call in init.c
257 succeeded. */
258 INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
259 sizeof (struct robust_list_head));
261 #endif
263 /* If the parent was running cancellation handlers while creating
264 the thread the new thread inherited the signal mask. Reset the
265 cancellation signal mask. */
266 if (__builtin_expect (pd->parent_cancelhandling & CANCELING_BITMASK, 0))
268 INTERNAL_SYSCALL_DECL (err);
269 sigset_t mask;
270 __sigemptyset (&mask);
271 __sigaddset (&mask, SIGCANCEL);
272 (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &mask,
273 NULL, _NSIG / 8);
276 /* This is where the try/finally block should be created. For
277 compilers without that support we do use setjmp. */
278 struct pthread_unwind_buf unwind_buf;
280 /* No previous handlers. */
281 unwind_buf.priv.data.prev = NULL;
282 unwind_buf.priv.data.cleanup = NULL;
284 int not_first_call;
285 not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
286 if (__builtin_expect (! not_first_call, 1))
288 /* Store the new cleanup handler info. */
289 THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf);
291 if (__builtin_expect (pd->stopped_start, 0))
293 int oldtype = CANCEL_ASYNC ();
295 /* Get the lock the parent locked to force synchronization. */
296 lll_lock (pd->lock, LLL_PRIVATE);
297 /* And give it up right away. */
298 lll_unlock (pd->lock, LLL_PRIVATE);
300 CANCEL_RESET (oldtype);
303 /* Run the code the user provided. */
304 #ifdef CALL_THREAD_FCT
305 THREAD_SETMEM (pd, result, CALL_THREAD_FCT (pd));
306 #else
307 THREAD_SETMEM (pd, result, pd->start_routine (pd->arg));
308 #endif
311 /* Run the destructor for the thread-local data. */
312 __nptl_deallocate_tsd ();
314 /* Clean up any state libc stored in thread-local variables. */
315 __libc_thread_freeres ();
317 /* If this is the last thread we terminate the process now. We
318 do not notify the debugger, it might just irritate it if there
319 is no thread left. */
320 if (__builtin_expect (atomic_decrement_and_test (&__nptl_nthreads), 0))
321 /* This was the last thread. */
322 exit (0);
324 /* Report the death of the thread if this is wanted. */
325 if (__builtin_expect (pd->report_events, 0))
327 /* See whether TD_DEATH is in any of the mask. */
328 const int idx = __td_eventword (TD_DEATH);
329 const uint32_t mask = __td_eventmask (TD_DEATH);
331 if ((mask & (__nptl_threads_events.event_bits[idx]
332 | pd->eventbuf.eventmask.event_bits[idx])) != 0)
334 /* Yep, we have to signal the death. Add the descriptor to
335 the list but only if it is not already on it. */
336 if (pd->nextevent == NULL)
338 pd->eventbuf.eventnum = TD_DEATH;
339 pd->eventbuf.eventdata = pd;
342 pd->nextevent = __nptl_last_event;
343 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
344 pd, pd->nextevent));
347 /* Now call the function to signal the event. */
348 __nptl_death_event ();
352 /* The thread is exiting now. Don't set this bit until after we've hit
353 the event-reporting breakpoint, so that td_thr_get_info on us while at
354 the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
355 atomic_bit_set (&pd->cancelhandling, EXITING_BIT);
357 #ifndef __ASSUME_SET_ROBUST_LIST
358 /* If this thread has any robust mutexes locked, handle them now. */
359 # if __WORDSIZE == 64
360 void *robust = pd->robust_head.list;
361 # else
362 __pthread_slist_t *robust = pd->robust_list.__next;
363 # endif
364 /* We let the kernel do the notification if it is able to do so.
365 If we have to do it here there for sure are no PI mutexes involved
366 since the kernel support for them is even more recent. */
367 if (__set_robust_list_avail < 0
368 && __builtin_expect (robust != (void *) &pd->robust_head, 0))
372 struct __pthread_mutex_s *this = (struct __pthread_mutex_s *)
373 ((char *) robust - offsetof (struct __pthread_mutex_s,
374 __list.__next));
375 robust = *((void **) robust);
377 # ifdef __PTHREAD_MUTEX_HAVE_PREV
378 this->__list.__prev = NULL;
379 # endif
380 this->__list.__next = NULL;
382 lll_robust_dead (this->__lock, /* XYZ */ LLL_SHARED);
384 while (robust != (void *) &pd->robust_head);
386 #endif
388 /* Mark the memory of the stack as usable to the kernel. We free
389 everything except for the space used for the TCB itself. */
390 size_t pagesize_m1 = __getpagesize () - 1;
391 #ifdef _STACK_GROWS_DOWN
392 char *sp = CURRENT_STACK_FRAME;
393 size_t freesize = (sp - (char *) pd->stackblock) & ~pagesize_m1;
394 #else
395 # error "to do"
396 #endif
397 assert (freesize < pd->stackblock_size);
398 if (freesize > PTHREAD_STACK_MIN)
399 madvise (pd->stackblock, freesize - PTHREAD_STACK_MIN, MADV_DONTNEED);
401 /* If the thread is detached free the TCB. */
402 if (IS_DETACHED (pd))
403 /* Free the TCB. */
404 __free_tcb (pd);
405 else if (__builtin_expect (pd->cancelhandling & SETXID_BITMASK, 0))
407 /* Some other thread might call any of the setXid functions and expect
408 us to reply. In this case wait until we did that. */
410 lll_futex_wait (&pd->setxid_futex, 0, LLL_PRIVATE);
411 while (pd->cancelhandling & SETXID_BITMASK);
413 /* Reset the value so that the stack can be reused. */
414 pd->setxid_futex = 0;
417 /* We cannot call '_exit' here. '_exit' will terminate the process.
419 The 'exit' implementation in the kernel will signal when the
420 process is really dead since 'clone' got passed the CLONE_CLEARTID
421 flag. The 'tid' field in the TCB will be set to zero.
423 The exit code is zero since in case all threads exit by calling
424 'pthread_exit' the exit status must be 0 (zero). */
425 __exit_thread_inline (0);
427 /* NOTREACHED */
428 return 0;
432 /* Default thread attributes for the case when the user does not
433 provide any. */
434 static const struct pthread_attr default_attr =
436 /* Just some value > 0 which gets rounded to the nearest page size. */
437 .guardsize = 1,
442 __pthread_create_2_1 (newthread, attr, start_routine, arg)
443 pthread_t *newthread;
444 const pthread_attr_t *attr;
445 void *(*start_routine) (void *);
446 void *arg;
448 STACK_VARIABLES;
450 const struct pthread_attr *iattr = (struct pthread_attr *) attr;
451 if (iattr == NULL)
452 /* Is this the best idea? On NUMA machines this could mean
453 accessing far-away memory. */
454 iattr = &default_attr;
456 struct pthread *pd = NULL;
457 int err = ALLOCATE_STACK (iattr, &pd);
458 if (__builtin_expect (err != 0, 0))
459 /* Something went wrong. Maybe a parameter of the attributes is
460 invalid or we could not allocate memory. Note we have to
461 translate error codes. */
462 return err == ENOMEM ? EAGAIN : err;
465 /* Initialize the TCB. All initializations with zero should be
466 performed in 'get_cached_stack'. This way we avoid doing this if
467 the stack freshly allocated with 'mmap'. */
469 #ifdef TLS_TCB_AT_TP
470 /* Reference to the TCB itself. */
471 pd->header.self = pd;
473 /* Self-reference for TLS. */
474 pd->header.tcb = pd;
475 #endif
477 /* Store the address of the start routine and the parameter. Since
478 we do not start the function directly the stillborn thread will
479 get the information from its thread descriptor. */
480 pd->start_routine = start_routine;
481 pd->arg = arg;
483 /* Copy the thread attribute flags. */
484 struct pthread *self = THREAD_SELF;
485 pd->flags = ((iattr->flags & ~(ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
486 | (self->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)));
488 /* Initialize the field for the ID of the thread which is waiting
489 for us. This is a self-reference in case the thread is created
490 detached. */
491 pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL;
493 /* The debug events are inherited from the parent. */
494 pd->eventbuf = self->eventbuf;
497 /* Copy the parent's scheduling parameters. The flags will say what
498 is valid and what is not. */
499 pd->schedpolicy = self->schedpolicy;
500 pd->schedparam = self->schedparam;
502 /* Copy the stack guard canary. */
503 #ifdef THREAD_COPY_STACK_GUARD
504 THREAD_COPY_STACK_GUARD (pd);
505 #endif
507 /* Copy the pointer guard value. */
508 #ifdef THREAD_COPY_POINTER_GUARD
509 THREAD_COPY_POINTER_GUARD (pd);
510 #endif
512 /* Determine scheduling parameters for the thread. */
513 if (attr != NULL
514 && __builtin_expect ((iattr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0, 0)
515 && (iattr->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) != 0)
517 INTERNAL_SYSCALL_DECL (scerr);
519 /* Use the scheduling parameters the user provided. */
520 if (iattr->flags & ATTR_FLAG_POLICY_SET)
521 pd->schedpolicy = iattr->schedpolicy;
522 else if ((pd->flags & ATTR_FLAG_POLICY_SET) == 0)
524 pd->schedpolicy = INTERNAL_SYSCALL (sched_getscheduler, scerr, 1, 0);
525 pd->flags |= ATTR_FLAG_POLICY_SET;
528 if (iattr->flags & ATTR_FLAG_SCHED_SET)
529 memcpy (&pd->schedparam, &iattr->schedparam,
530 sizeof (struct sched_param));
531 else if ((pd->flags & ATTR_FLAG_SCHED_SET) == 0)
533 INTERNAL_SYSCALL (sched_getparam, scerr, 2, 0, &pd->schedparam);
534 pd->flags |= ATTR_FLAG_SCHED_SET;
537 /* Check for valid priorities. */
538 int minprio = INTERNAL_SYSCALL (sched_get_priority_min, scerr, 1,
539 iattr->schedpolicy);
540 int maxprio = INTERNAL_SYSCALL (sched_get_priority_max, scerr, 1,
541 iattr->schedpolicy);
542 if (pd->schedparam.sched_priority < minprio
543 || pd->schedparam.sched_priority > maxprio)
545 /* Perhaps a thread wants to change the IDs and if waiting
546 for this stillborn thread. */
547 if (__builtin_expect (atomic_exchange_acq (&pd->setxid_futex, 0)
548 == -2, 0))
549 lll_futex_wake (&pd->setxid_futex, 1, LLL_PRIVATE);
551 __deallocate_stack (pd);
553 return EINVAL;
557 /* Pass the descriptor to the caller. */
558 *newthread = (pthread_t) pd;
560 /* Start the thread. */
561 return create_thread (pd, iattr, STACK_VARIABLES_ARGS);
563 versioned_symbol (libpthread, __pthread_create_2_1, pthread_create, GLIBC_2_1);
566 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
568 __pthread_create_2_0 (newthread, attr, start_routine, arg)
569 pthread_t *newthread;
570 const pthread_attr_t *attr;
571 void *(*start_routine) (void *);
572 void *arg;
574 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
575 the old size and access to the new members might crash the program.
576 We convert the struct now. */
577 struct pthread_attr new_attr;
579 if (attr != NULL)
581 struct pthread_attr *iattr = (struct pthread_attr *) attr;
582 size_t ps = __getpagesize ();
584 /* Copy values from the user-provided attributes. */
585 new_attr.schedparam = iattr->schedparam;
586 new_attr.schedpolicy = iattr->schedpolicy;
587 new_attr.flags = iattr->flags;
589 /* Fill in default values for the fields not present in the old
590 implementation. */
591 new_attr.guardsize = ps;
592 new_attr.stackaddr = NULL;
593 new_attr.stacksize = 0;
594 new_attr.cpuset = NULL;
596 /* We will pass this value on to the real implementation. */
597 attr = (pthread_attr_t *) &new_attr;
600 return __pthread_create_2_1 (newthread, attr, start_routine, arg);
602 compat_symbol (libpthread, __pthread_create_2_0, pthread_create,
603 GLIBC_2_0);
604 #endif
606 /* Information for libthread_db. */
608 #include "../nptl_db/db_info.c"
610 /* If pthread_create is present, libgcc_eh.a and libsupc++.a expects some other POSIX thread
611 functions to be present as well. */
612 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_lock)
613 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_trylock)
614 PTHREAD_STATIC_FN_REQUIRE (pthread_mutex_unlock)
616 PTHREAD_STATIC_FN_REQUIRE (pthread_once)
617 PTHREAD_STATIC_FN_REQUIRE (pthread_cancel)
619 PTHREAD_STATIC_FN_REQUIRE (pthread_key_create)
620 PTHREAD_STATIC_FN_REQUIRE (pthread_key_delete)
621 PTHREAD_STATIC_FN_REQUIRE (pthread_setspecific)
622 PTHREAD_STATIC_FN_REQUIRE (pthread_getspecific)