]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/pthread_create.c
Add per-thread cache to malloc
[thirdparty/glibc.git] / nptl / pthread_create.c
CommitLineData
bfff8b1b 1/* Copyright (C) 2002-2017 Free Software Foundation, Inc.
76a50749
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
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.
9
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.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
76a50749 18
fd5bdc09 19#include <ctype.h>
76a50749
UD
20#include <errno.h>
21#include <stdbool.h>
22#include <stdlib.h>
23#include <string.h>
e054f494 24#include <stdint.h>
76a50749
UD
25#include "pthreadP.h"
26#include <hp-timing.h>
27#include <ldsodefs.h>
3e4fc359 28#include <atomic.h>
12d7ca07 29#include <libc-internal.h>
0e9d6240 30#include <resolv.h>
f8de5057 31#include <kernel-features.h>
e0db6517 32#include <exit-thread.h>
f214ff74 33#include <default-sched.h>
a2f0363f 34#include <futex-internal.h>
d2e04918 35#include "libioP.h"
76a50749
UD
36
37#include <shlib-compat.h>
38
3a097cc7
RM
39#include <stap-probe.h>
40
76a50749 41
76a50749
UD
42/* Nozero if debugging mode is enabled. */
43int __pthread_debug;
44
45/* Globally enabled events. */
e965d514 46static td_thr_events_t __nptl_threads_events __attribute_used__;
76a50749
UD
47
48/* Pointer to descriptor with the last event. */
e965d514 49static struct pthread *__nptl_last_event __attribute_used__;
76a50749 50
47202270
UD
51/* Number of threads running. */
52unsigned int __nptl_nthreads = 1;
53
76a50749
UD
54
55/* Code to allocate and deallocate a stack. */
76a50749
UD
56#include "allocatestack.c"
57
f8bf15fe
CD
58/* CONCURRENCY NOTES:
59
60 Understanding who is the owner of the 'struct pthread' or 'PD'
61 (refers to the value of the 'struct pthread *pd' function argument)
62 is critically important in determining exactly which operations are
63 allowed and which are not and when, particularly when it comes to the
64 implementation of pthread_create, pthread_join, pthread_detach, and
65 other functions which all operate on PD.
66
67 The owner of PD is responsible for freeing the final resources
68 associated with PD, and may examine the memory underlying PD at any
69 point in time until it frees it back to the OS or to reuse by the
70 runtime.
71
72 The thread which calls pthread_create is called the creating thread.
73 The creating thread begins as the owner of PD.
74
75 During startup the new thread may examine PD in coordination with the
76 owner thread (which may be itself).
77
78 The four cases of ownership transfer are:
79
80 (1) Ownership of PD is released to the process (all threads may use it)
81 after the new thread starts in a joinable state
82 i.e. pthread_create returns a usable pthread_t.
83
84 (2) Ownership of PD is released to the new thread starting in a detached
85 state.
86
87 (3) Ownership of PD is dynamically released to a running thread via
88 pthread_detach.
89
90 (4) Ownership of PD is acquired by the thread which calls pthread_join.
91
92 Implementation notes:
93
94 The PD->stopped_start and thread_ran variables are used to determine
95 exactly which of the four ownership states we are in and therefore
96 what actions can be taken. For example after (2) we cannot read or
97 write from PD anymore since the thread may no longer exist and the
fa17b9c7
CD
98 memory may be unmapped.
99
100 It is important to point out that PD->lock is being used both
101 similar to a one-shot semaphore and subsequently as a mutex. The
102 lock is taken in the parent to force the child to wait, and then the
103 child releases the lock. However, this semaphore-like effect is used
104 only for synchronizing the parent and child. After startup the lock
105 is used like a mutex to create a critical section during which a
106 single owner modifies the thread parameters.
107
108 The most complicated cases happen during thread startup:
f8bf15fe
CD
109
110 (a) If the created thread is in a detached (PTHREAD_CREATE_DETACHED),
111 or joinable (default PTHREAD_CREATE_JOINABLE) state and
112 STOPPED_START is true, then the creating thread has ownership of
113 PD until the PD->lock is released by pthread_create. If any
114 errors occur we are in states (c), (d), or (e) below.
115
116 (b) If the created thread is in a detached state
117 (PTHREAD_CREATED_DETACHED), and STOPPED_START is false, then the
118 creating thread has ownership of PD until it invokes the OS
119 kernel's thread creation routine. If this routine returns
120 without error, then the created thread owns PD; otherwise, see
121 (c) and (e) below.
122
123 (c) If the detached thread setup failed and THREAD_RAN is true, then
124 the creating thread releases ownership to the new thread by
125 sending a cancellation signal. All threads set THREAD_RAN to
126 true as quickly as possible after returning from the OS kernel's
127 thread creation routine.
128
129 (d) If the joinable thread setup failed and THREAD_RAN is true, then
130 then the creating thread retains ownership of PD and must cleanup
131 state. Ownership cannot be released to the process via the
132 return of pthread_create since a non-zero result entails PD is
133 undefined and therefore cannot be joined to free the resources.
134 We privately call pthread_join on the thread to finish handling
135 the resource shutdown (Or at least we should, see bug 19511).
136
137 (e) If the thread creation failed and THREAD_RAN is false, then the
138 creating thread retains ownership of PD and must cleanup state.
139 No waiting for the new thread is required because it never
140 started.
141
142 The nptl_db interface:
143
144 The interface with nptl_db requires that we enqueue PD into a linked
145 list and then call a function which the debugger will trap. The PD
146 will then be dequeued and control returned to the thread. The caller
147 at the time must have ownership of PD and such ownership remains
148 after control returns to thread. The enqueued PD is removed from the
149 linked list by the nptl_db callback td_thr_event_getmsg. The debugger
150 must ensure that the thread does not resume execution, otherwise
151 ownership of PD may be lost and examining PD will not be possible.
152
153 Note that the GNU Debugger as of (December 10th 2015) commit
154 c2c2a31fdb228d41ce3db62b268efea04bd39c18 no longer uses
155 td_thr_event_getmsg and several other related nptl_db interfaces. The
156 principal reason for this is that nptl_db does not support non-stop
157 mode where other threads can run concurrently and modify runtime
158 structures currently in use by the debugger and the nptl_db
159 interface.
160
161 Axioms:
162
163 * The create_thread function can never set stopped_start to false.
164 * The created thread can read stopped_start but never write to it.
165 * The variable thread_ran is set some time after the OS thread
166 creation routine returns, how much time after the thread is created
167 is unspecified, but it should be as quickly as possible.
168
169*/
170
171/* CREATE THREAD NOTES:
172
173 createthread.c defines the create_thread function, and two macros:
32fed10f
RM
174 START_THREAD_DEFN and START_THREAD_SELF (see below).
175
f8bf15fe
CD
176 create_thread must initialize PD->stopped_start. It should be true
177 if the STOPPED_START parameter is true, or if create_thread needs the
178 new thread to synchronize at startup for some other implementation
179 reason. If STOPPED_START will be true, then create_thread is obliged
180 to lock PD->lock before starting the thread. Then pthread_create
181 unlocks PD->lock which synchronizes-with START_THREAD_DEFN in the
182 child thread which does an acquire/release of PD->lock as the last
183 action before calling the user entry point. The goal of all of this
184 is to ensure that the required initial thread attributes are applied
185 (by the creating thread) before the new thread runs user code. Note
186 that the the functions pthread_getschedparam, pthread_setschedparam,
187 pthread_setschedprio, __pthread_tpp_change_priority, and
188 __pthread_current_priority reuse the same lock, PD->lock, for a
189 similar purpose e.g. synchronizing the setting of similar thread
190 attributes. These functions are never called before the thread is
191 created, so don't participate in startup syncronization, but given
192 that the lock is present already and in the unlocked state, reusing
193 it saves space.
32fed10f
RM
194
195 The return value is zero for success or an errno code for failure.
196 If the return value is ENOMEM, that will be translated to EAGAIN,
197 so create_thread need not do that. On failure, *THREAD_RAN should
198 be set to true iff the thread actually started up and then got
f8bf15fe 199 canceled before calling user code (*PD->start_routine). */
32fed10f 200static int create_thread (struct pthread *pd, const struct pthread_attr *attr,
f8bf15fe 201 bool *stopped_start, STACK_VARIABLES_PARMS,
32fed10f
RM
202 bool *thread_ran);
203
8dea90aa 204#include <createthread.c>
76a50749
UD
205
206
76a50749 207struct pthread *
90491dc4 208internal_function
9dd346ff 209__find_in_stack_list (struct pthread *pd)
76a50749
UD
210{
211 list_t *entry;
212 struct pthread *result = NULL;
213
e51deae7 214 lll_lock (stack_cache_lock, LLL_PRIVATE);
76a50749
UD
215
216 list_for_each (entry, &stack_used)
217 {
218 struct pthread *curp;
219
d4f64e1a 220 curp = list_entry (entry, struct pthread, list);
76a50749
UD
221 if (curp == pd)
222 {
223 result = curp;
224 break;
225 }
226 }
227
228 if (result == NULL)
229 list_for_each (entry, &__stack_user)
230 {
231 struct pthread *curp;
232
d4f64e1a 233 curp = list_entry (entry, struct pthread, list);
76a50749
UD
234 if (curp == pd)
235 {
236 result = curp;
237 break;
238 }
239 }
240
e51deae7 241 lll_unlock (stack_cache_lock, LLL_PRIVATE);
76a50749
UD
242
243 return result;
244}
245
246
247/* Deallocate POSIX thread-local-storage. */
3fa21fd8
UD
248void
249attribute_hidden
250__nptl_deallocate_tsd (void)
76a50749 251{
877e51b2
UD
252 struct pthread *self = THREAD_SELF;
253
76a50749
UD
254 /* Maybe no data was ever allocated. This happens often so we have
255 a flag for this. */
877e51b2 256 if (THREAD_GETMEM (self, specific_used))
76a50749
UD
257 {
258 size_t round;
6b4686a5 259 size_t cnt;
76a50749 260
6b4686a5
UD
261 round = 0;
262 do
76a50749 263 {
76a50749
UD
264 size_t idx;
265
c5acd3d7 266 /* So far no new nonzero data entry. */
877e51b2 267 THREAD_SETMEM (self, specific_used, false);
c5acd3d7 268
76a50749 269 for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
6b4686a5
UD
270 {
271 struct pthread_key_data *level2;
272
877e51b2 273 level2 = THREAD_GETMEM_NC (self, specific, cnt);
6b4686a5
UD
274
275 if (level2 != NULL)
276 {
277 size_t inner;
278
279 for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE;
280 ++inner, ++idx)
281 {
282 void *data = level2[inner].data;
283
877e51b2
UD
284 if (data != NULL)
285 {
286 /* Always clear the data. */
287 level2[inner].data = NULL;
288
6b4686a5
UD
289 /* Make sure the data corresponds to a valid
290 key. This test fails if the key was
291 deallocated and also if it was
292 re-allocated. It is the user's
293 responsibility to free the memory in this
294 case. */
877e51b2
UD
295 if (level2[inner].seq
296 == __pthread_keys[idx].seq
297 /* It is not necessary to register a destructor
298 function. */
299 && __pthread_keys[idx].destr != NULL)
300 /* Call the user-provided destructor. */
301 __pthread_keys[idx].destr (data);
6b4686a5
UD
302 }
303 }
304 }
305 else
306 idx += PTHREAD_KEY_1STLEVEL_SIZE;
307 }
877e51b2
UD
308
309 if (THREAD_GETMEM (self, specific_used) == 0)
310 /* No data has been modified. */
311 goto just_free;
76a50749 312 }
877e51b2
UD
313 /* We only repeat the process a fixed number of times. */
314 while (__builtin_expect (++round < PTHREAD_DESTRUCTOR_ITERATIONS, 0));
76a50749 315
877e51b2
UD
316 /* Just clear the memory of the first block for reuse. */
317 memset (&THREAD_SELF->specific_1stblock, '\0',
318 sizeof (self->specific_1stblock));
6b4686a5 319
877e51b2 320 just_free:
6b4686a5
UD
321 /* Free the memory for the other blocks. */
322 for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
323 {
324 struct pthread_key_data *level2;
325
877e51b2 326 level2 = THREAD_GETMEM_NC (self, specific, cnt);
6b4686a5
UD
327 if (level2 != NULL)
328 {
329 /* The first block is allocated as part of the thread
330 descriptor. */
331 free (level2);
877e51b2 332 THREAD_SETMEM_NC (self, specific, cnt, NULL);
6b4686a5
UD
333 }
334 }
335
877e51b2 336 THREAD_SETMEM (self, specific_used, false);
76a50749
UD
337 }
338}
339
340
341/* Deallocate a thread's stack after optionally making sure the thread
342 descriptor is still valid. */
343void
90491dc4 344internal_function
76a50749
UD
345__free_tcb (struct pthread *pd)
346{
347 /* The thread is exiting now. */
ba25bb0f
UD
348 if (__builtin_expect (atomic_bit_test_set (&pd->cancelhandling,
349 TERMINATED_BIT) == 0, 1))
76a50749
UD
350 {
351 /* Remove the descriptor from the list. */
352 if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
353 /* Something is really wrong. The descriptor for a still
354 running thread is gone. */
355 abort ();
356
f17efcb4 357 /* Free TPP data. */
a1ffb40e 358 if (__glibc_unlikely (pd->tpp != NULL))
f17efcb4
UD
359 {
360 struct priority_protection_data *tpp = pd->tpp;
361
362 pd->tpp = NULL;
363 free (tpp);
364 }
365
76a50749
UD
366 /* Queue the stack memory block for reuse and exit the process. The
367 kernel will signal via writing to the address returned by
368 QUEUE-STACK when the stack is available. */
369 __deallocate_stack (pd);
370 }
371}
372
373
32fed10f
RM
374/* Local function to start thread and handle cleanup.
375 createthread.c defines the macro START_THREAD_DEFN to the
376 declaration that its create_thread function will refer to, and
377 START_THREAD_SELF to the expression to optimally deliver the new
378 thread's THREAD_SELF value. */
379START_THREAD_DEFN
76a50749 380{
32fed10f 381 struct pthread *pd = START_THREAD_SELF;
76a50749
UD
382
383#if HP_TIMING_AVAIL
384 /* Remember the time when the thread was started. */
385 hp_timing_t now;
386 HP_TIMING_NOW (now);
387 THREAD_SETMEM (pd, cpuclock_offset, now);
388#endif
389
0e9d6240
UD
390 /* Initialize resolver state pointer. */
391 __resp = &pd->res;
392
fd5bdc09
UD
393 /* Initialize pointers to locale data. */
394 __ctype_init ();
395
66f1b8ee 396 /* Allow setxid from now onwards. */
a1ffb40e 397 if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) == -2))
a2f0363f 398 futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE);
66f1b8ee 399
0f6699ea
UD
400#ifdef __NR_set_robust_list
401# ifndef __ASSUME_SET_ROBUST_LIST
402 if (__set_robust_list_avail >= 0)
403# endif
404 {
405 INTERNAL_SYSCALL_DECL (err);
406 /* This call should never fail because the initial call in init.c
407 succeeded. */
408 INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
409 sizeof (struct robust_list_head));
410 }
411#endif
412
327ae257 413#ifdef SIGCANCEL
b051fc44
UD
414 /* If the parent was running cancellation handlers while creating
415 the thread the new thread inherited the signal mask. Reset the
416 cancellation signal mask. */
a1ffb40e 417 if (__glibc_unlikely (pd->parent_cancelhandling & CANCELING_BITMASK))
b051fc44
UD
418 {
419 INTERNAL_SYSCALL_DECL (err);
420 sigset_t mask;
421 __sigemptyset (&mask);
422 __sigaddset (&mask, SIGCANCEL);
423 (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &mask,
424 NULL, _NSIG / 8);
425 }
327ae257 426#endif
b051fc44 427
76a50749
UD
428 /* This is where the try/finally block should be created. For
429 compilers without that support we do use setjmp. */
877e51b2
UD
430 struct pthread_unwind_buf unwind_buf;
431
432 /* No previous handlers. */
433 unwind_buf.priv.data.prev = NULL;
434 unwind_buf.priv.data.cleanup = NULL;
435
436 int not_first_call;
437 not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
a1ffb40e 438 if (__glibc_likely (! not_first_call))
76a50749 439 {
877e51b2
UD
440 /* Store the new cleanup handler info. */
441 THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf);
442
f8bf15fe
CD
443 /* We are either in (a) or (b), and in either case we either own
444 PD already (2) or are about to own PD (1), and so our only
445 restriction would be that we can't free PD until we know we
446 have ownership (see CONCURRENCY NOTES above). */
a1ffb40e 447 if (__glibc_unlikely (pd->stopped_start))
5f66b766
UD
448 {
449 int oldtype = CANCEL_ASYNC ();
362038b0 450
5f66b766 451 /* Get the lock the parent locked to force synchronization. */
e51deae7 452 lll_lock (pd->lock, LLL_PRIVATE);
f8bf15fe
CD
453
454 /* We have ownership of PD now. */
455
5f66b766 456 /* And give it up right away. */
e51deae7 457 lll_unlock (pd->lock, LLL_PRIVATE);
362038b0 458
5f66b766
UD
459 CANCEL_RESET (oldtype);
460 }
362038b0 461
3a097cc7
RM
462 LIBC_PROBE (pthread_start, 3, (pthread_t) pd, pd->start_routine, pd->arg);
463
76a50749 464 /* Run the code the user provided. */
cc775edf 465 THREAD_SETMEM (pd, result, pd->start_routine (pd->arg));
76a50749
UD
466 }
467
ba384f6e 468 /* Call destructors for the thread_local TLS variables. */
e57b0c61
RM
469#ifndef SHARED
470 if (&__call_tls_dtors != NULL)
471#endif
472 __call_tls_dtors ();
ba384f6e 473
6b4686a5 474 /* Run the destructor for the thread-local data. */
3fa21fd8 475 __nptl_deallocate_tsd ();
6b4686a5 476
12d7ca07
RM
477 /* Clean up any state libc stored in thread-local variables. */
478 __libc_thread_freeres ();
76a50749 479
47202270
UD
480 /* If this is the last thread we terminate the process now. We
481 do not notify the debugger, it might just irritate it if there
482 is no thread left. */
a1ffb40e 483 if (__glibc_unlikely (atomic_decrement_and_test (&__nptl_nthreads)))
47202270
UD
484 /* This was the last thread. */
485 exit (0);
486
76a50749 487 /* Report the death of the thread if this is wanted. */
a1ffb40e 488 if (__glibc_unlikely (pd->report_events))
76a50749
UD
489 {
490 /* See whether TD_DEATH is in any of the mask. */
491 const int idx = __td_eventword (TD_DEATH);
492 const uint32_t mask = __td_eventmask (TD_DEATH);
493
494 if ((mask & (__nptl_threads_events.event_bits[idx]
495 | pd->eventbuf.eventmask.event_bits[idx])) != 0)
496 {
497 /* Yep, we have to signal the death. Add the descriptor to
498 the list but only if it is not already on it. */
499 if (pd->nextevent == NULL)
500 {
501 pd->eventbuf.eventnum = TD_DEATH;
502 pd->eventbuf.eventdata = pd;
503
504 do
505 pd->nextevent = __nptl_last_event;
5a3ab2fc
UD
506 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
507 pd, pd->nextevent));
76a50749
UD
508 }
509
f8bf15fe
CD
510 /* Now call the function which signals the event. See
511 CONCURRENCY NOTES for the nptl_db interface comments. */
76a50749
UD
512 __nptl_death_event ();
513 }
514 }
515
6461e577
RM
516 /* The thread is exiting now. Don't set this bit until after we've hit
517 the event-reporting breakpoint, so that td_thr_get_info on us while at
518 the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
519 atomic_bit_set (&pd->cancelhandling, EXITING_BIT);
76a50749 520
0f6699ea 521#ifndef __ASSUME_SET_ROBUST_LIST
1bcfb5a5 522 /* If this thread has any robust mutexes locked, handle them now. */
c252ec15 523# ifdef __PTHREAD_MUTEX_HAVE_PREV
0f6699ea
UD
524 void *robust = pd->robust_head.list;
525# else
b007ce7c 526 __pthread_slist_t *robust = pd->robust_list.__next;
0f6699ea 527# endif
df47504c
UD
528 /* We let the kernel do the notification if it is able to do so.
529 If we have to do it here there for sure are no PI mutexes involved
530 since the kernel support for them is even more recent. */
0f6699ea 531 if (__set_robust_list_avail < 0
df47504c 532 && __builtin_expect (robust != (void *) &pd->robust_head, 0))
1bcfb5a5
UD
533 {
534 do
535 {
b007ce7c 536 struct __pthread_mutex_s *this = (struct __pthread_mutex_s *)
0f6699ea
UD
537 ((char *) robust - offsetof (struct __pthread_mutex_s,
538 __list.__next));
539 robust = *((void **) robust);
d804f5df 540
0f6699ea 541# ifdef __PTHREAD_MUTEX_HAVE_PREV
b007ce7c 542 this->__list.__prev = NULL;
0f6699ea
UD
543# endif
544 this->__list.__next = NULL;
1bcfb5a5 545
c0c6bac9 546 atomic_or (&this->__lock, FUTEX_OWNER_DIED);
a2f0363f
TR
547 futex_wake ((unsigned int *) &this->__lock, 1,
548 /* XYZ */ FUTEX_SHARED);
1bcfb5a5 549 }
df47504c 550 while (robust != (void *) &pd->robust_head);
1bcfb5a5 551 }
0f6699ea 552#endif
1bcfb5a5 553
b42a214c
UD
554 /* Mark the memory of the stack as usable to the kernel. We free
555 everything except for the space used for the TCB itself. */
556 size_t pagesize_m1 = __getpagesize () - 1;
557#ifdef _STACK_GROWS_DOWN
558 char *sp = CURRENT_STACK_FRAME;
559 size_t freesize = (sp - (char *) pd->stackblock) & ~pagesize_m1;
b42a214c
UD
560 assert (freesize < pd->stackblock_size);
561 if (freesize > PTHREAD_STACK_MIN)
9043e228 562 __madvise (pd->stackblock, freesize - PTHREAD_STACK_MIN, MADV_DONTNEED);
d615a473
CD
563#else
564 /* Page aligned start of memory to free (higher than or equal
565 to current sp plus the minimum stack size). */
566 void *freeblock = (void*)((size_t)(CURRENT_STACK_FRAME
567 + PTHREAD_STACK_MIN
568 + pagesize_m1)
569 & ~pagesize_m1);
570 char *free_end = (char *) (((uintptr_t) pd - pd->guardsize) & ~pagesize_m1);
571 /* Is there any space to free? */
572 if (free_end > (char *)freeblock)
573 {
574 size_t freesize = (size_t)(free_end - (char *)freeblock);
575 assert (freesize < pd->stackblock_size);
576 __madvise (freeblock, freesize, MADV_DONTNEED);
577 }
578#endif
b42a214c 579
76a50749
UD
580 /* If the thread is detached free the TCB. */
581 if (IS_DETACHED (pd))
582 /* Free the TCB. */
583 __free_tcb (pd);
a1ffb40e 584 else if (__glibc_unlikely (pd->cancelhandling & SETXID_BITMASK))
dff9a7a1
UD
585 {
586 /* Some other thread might call any of the setXid functions and expect
587 us to reply. In this case wait until we did that. */
588 do
a2f0363f
TR
589 /* XXX This differs from the typical futex_wait_simple pattern in that
590 the futex_wait condition (setxid_futex) is different from the
591 condition used in the surrounding loop (cancelhandling). We need
592 to check and document why this is correct. */
593 futex_wait_simple (&pd->setxid_futex, 0, FUTEX_PRIVATE);
dff9a7a1
UD
594 while (pd->cancelhandling & SETXID_BITMASK);
595
596 /* Reset the value so that the stack can be reused. */
597 pd->setxid_futex = 0;
598 }
76a50749
UD
599
600 /* We cannot call '_exit' here. '_exit' will terminate the process.
601
602 The 'exit' implementation in the kernel will signal when the
adcdc775 603 process is really dead since 'clone' got passed the CLONE_CHILD_CLEARTID
76a50749
UD
604 flag. The 'tid' field in the TCB will be set to zero.
605
606 The exit code is zero since in case all threads exit by calling
607 'pthread_exit' the exit status must be 0 (zero). */
e0db6517 608 __exit_thread ();
76a50749
UD
609
610 /* NOTREACHED */
32fed10f
RM
611}
612
613
614/* Return true iff obliged to report TD_CREATE events. */
615static bool
616report_thread_creation (struct pthread *pd)
617{
618 if (__glibc_unlikely (THREAD_GETMEM (THREAD_SELF, report_events)))
619 {
620 /* The parent thread is supposed to report events.
621 Check whether the TD_CREATE event is needed, too. */
622 const size_t idx = __td_eventword (TD_CREATE);
623 const uint32_t mask = __td_eventmask (TD_CREATE);
624
625 return ((mask & (__nptl_threads_events.event_bits[idx]
626 | pd->eventbuf.eventmask.event_bits[idx])) != 0);
627 }
628 return false;
76a50749
UD
629}
630
631
76a50749 632int
80d9be81
JM
633__pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr,
634 void *(*start_routine) (void *), void *arg)
76a50749
UD
635{
636 STACK_VARIABLES;
76a50749 637
1e6da2b0 638 const struct pthread_attr *iattr = (struct pthread_attr *) attr;
61dd6208
SP
639 struct pthread_attr default_attr;
640 bool free_cpuset = false;
76a50749 641 if (iattr == NULL)
61dd6208
SP
642 {
643 lll_lock (__default_pthread_attr_lock, LLL_PRIVATE);
644 default_attr = __default_pthread_attr;
645 size_t cpusetsize = default_attr.cpusetsize;
646 if (cpusetsize > 0)
647 {
648 cpu_set_t *cpuset;
649 if (__glibc_likely (__libc_use_alloca (cpusetsize)))
650 cpuset = __alloca (cpusetsize);
651 else
652 {
653 cpuset = malloc (cpusetsize);
654 if (cpuset == NULL)
655 {
656 lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
657 return ENOMEM;
658 }
659 free_cpuset = true;
660 }
661 memcpy (cpuset, default_attr.cpuset, cpusetsize);
662 default_attr.cpuset = cpuset;
663 }
664 lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
665 iattr = &default_attr;
666 }
76a50749 667
dff9a7a1 668 struct pthread *pd = NULL;
1e6da2b0 669 int err = ALLOCATE_STACK (iattr, &pd);
61dd6208
SP
670 int retval = 0;
671
a1ffb40e 672 if (__glibc_unlikely (err != 0))
76a50749 673 /* Something went wrong. Maybe a parameter of the attributes is
e988dba9
JL
674 invalid or we could not allocate memory. Note we have to
675 translate error codes. */
61dd6208
SP
676 {
677 retval = err == ENOMEM ? EAGAIN : err;
678 goto out;
679 }
76a50749
UD
680
681
682 /* Initialize the TCB. All initializations with zero should be
683 performed in 'get_cached_stack'. This way we avoid doing this if
684 the stack freshly allocated with 'mmap'. */
685
d7329d4b 686#if TLS_TCB_AT_TP
76a50749 687 /* Reference to the TCB itself. */
55c11fbd 688 pd->header.self = pd;
76a50749 689
d4f64e1a 690 /* Self-reference for TLS. */
55c11fbd 691 pd->header.tcb = pd;
76a50749
UD
692#endif
693
694 /* Store the address of the start routine and the parameter. Since
695 we do not start the function directly the stillborn thread will
696 get the information from its thread descriptor. */
697 pd->start_routine = start_routine;
698 pd->arg = arg;
699
700 /* Copy the thread attribute flags. */
14ffbc83
UD
701 struct pthread *self = THREAD_SELF;
702 pd->flags = ((iattr->flags & ~(ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
703 | (self->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)));
76a50749
UD
704
705 /* Initialize the field for the ID of the thread which is waiting
706 for us. This is a self-reference in case the thread is created
707 detached. */
708 pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL;
709
710 /* The debug events are inherited from the parent. */
14ffbc83
UD
711 pd->eventbuf = self->eventbuf;
712
76a50749 713
14ffbc83
UD
714 /* Copy the parent's scheduling parameters. The flags will say what
715 is valid and what is not. */
716 pd->schedpolicy = self->schedpolicy;
717 pd->schedparam = self->schedparam;
76a50749 718
35f1e827
UD
719 /* Copy the stack guard canary. */
720#ifdef THREAD_COPY_STACK_GUARD
721 THREAD_COPY_STACK_GUARD (pd);
722#endif
723
827b7087
UD
724 /* Copy the pointer guard value. */
725#ifdef THREAD_COPY_POINTER_GUARD
726 THREAD_COPY_POINTER_GUARD (pd);
727#endif
728
32fed10f
RM
729 /* Verify the sysinfo bits were copied in allocate_stack if needed. */
730#ifdef NEED_DL_SYSINFO
731 CHECK_THREAD_SYSINFO (pd);
732#endif
733
734 /* Inform start_thread (above) about cancellation state that might
735 translate into inherited signal state. */
736 pd->parent_cancelhandling = THREAD_GETMEM (THREAD_SELF, cancelhandling);
737
14ffbc83 738 /* Determine scheduling parameters for the thread. */
61dd6208 739 if (__builtin_expect ((iattr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0, 0)
14ffbc83 740 && (iattr->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) != 0)
76a50749 741 {
14ffbc83
UD
742 /* Use the scheduling parameters the user provided. */
743 if (iattr->flags & ATTR_FLAG_POLICY_SET)
33cd1f74
RM
744 {
745 pd->schedpolicy = iattr->schedpolicy;
746 pd->flags |= ATTR_FLAG_POLICY_SET;
747 }
14ffbc83 748 if (iattr->flags & ATTR_FLAG_SCHED_SET)
33cd1f74
RM
749 {
750 /* The values were validated in pthread_attr_setschedparam. */
751 pd->schedparam = iattr->schedparam;
752 pd->flags |= ATTR_FLAG_SCHED_SET;
753 }
f214ff74
RM
754
755 if ((pd->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
756 != (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
757 collect_default_sched (pd);
76a50749
UD
758 }
759
d2e04918
SN
760 if (__glibc_unlikely (__nptl_nthreads == 1))
761 _IO_enable_locks ();
762
76a50749
UD
763 /* Pass the descriptor to the caller. */
764 *newthread = (pthread_t) pd;
765
5acf7263
RM
766 LIBC_PROBE (pthread_create, 4, newthread, attr, start_routine, arg);
767
32fed10f
RM
768 /* One more thread. We cannot have the thread do this itself, since it
769 might exist but not have been scheduled yet by the time we've returned
770 and need to check the value to behave correctly. We must do it before
771 creating the thread, in case it does get scheduled first and then
772 might mistakenly think it was the only thread. In the failure case,
773 we momentarily store a false value; this doesn't matter because there
774 is no kosher thing a signal handler interrupting us right here can do
775 that cares whether the thread count is correct. */
776 atomic_increment (&__nptl_nthreads);
777
f8bf15fe
CD
778 /* Our local value of stopped_start and thread_ran can be accessed at
779 any time. The PD->stopped_start may only be accessed if we have
780 ownership of PD (see CONCURRENCY NOTES above). */
781 bool stopped_start = false; bool thread_ran = false;
32fed10f 782
76a50749 783 /* Start the thread. */
32fed10f
RM
784 if (__glibc_unlikely (report_thread_creation (pd)))
785 {
f8bf15fe
CD
786 stopped_start = true;
787
788 /* We always create the thread stopped at startup so we can
789 notify the debugger. */
790 retval = create_thread (pd, iattr, &stopped_start,
791 STACK_VARIABLES_ARGS, &thread_ran);
32fed10f
RM
792 if (retval == 0)
793 {
f8bf15fe
CD
794 /* We retain ownership of PD until (a) (see CONCURRENCY NOTES
795 above). */
796
797 /* Assert stopped_start is true in both our local copy and the
798 PD copy. */
799 assert (stopped_start);
32fed10f
RM
800 assert (pd->stopped_start);
801
802 /* Now fill in the information about the new thread in
803 the newly created thread's data structure. We cannot let
804 the new thread do this since we don't know whether it was
805 already scheduled when we send the event. */
806 pd->eventbuf.eventnum = TD_CREATE;
807 pd->eventbuf.eventdata = pd;
808
809 /* Enqueue the descriptor. */
810 do
811 pd->nextevent = __nptl_last_event;
812 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
813 pd, pd->nextevent)
814 != 0);
815
f8bf15fe
CD
816 /* Now call the function which signals the event. See
817 CONCURRENCY NOTES for the nptl_db interface comments. */
32fed10f
RM
818 __nptl_create_event ();
819 }
820 }
821 else
f8bf15fe
CD
822 retval = create_thread (pd, iattr, &stopped_start,
823 STACK_VARIABLES_ARGS, &thread_ran);
32fed10f
RM
824
825 if (__glibc_unlikely (retval != 0))
826 {
32fed10f 827 if (thread_ran)
f8bf15fe
CD
828 /* State (c) or (d) and we may not have PD ownership (see
829 CONCURRENCY NOTES above). We can assert that STOPPED_START
830 must have been true because thread creation didn't fail, but
831 thread attribute setting did. */
832 /* See bug 19511 which explains why doing nothing here is a
833 resource leak for a joinable thread. */
834 assert (stopped_start);
32fed10f
RM
835 else
836 {
f8bf15fe
CD
837 /* State (e) and we have ownership of PD (see CONCURRENCY
838 NOTES above). */
839
32fed10f
RM
840 /* Oops, we lied for a second. */
841 atomic_decrement (&__nptl_nthreads);
842
843 /* Perhaps a thread wants to change the IDs and is waiting for this
844 stillborn thread. */
845 if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0)
846 == -2))
a2f0363f 847 futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE);
32fed10f
RM
848
849 /* Free the resources. */
850 __deallocate_stack (pd);
851 }
852
853 /* We have to translate error codes. */
854 if (retval == ENOMEM)
855 retval = EAGAIN;
856 }
857 else
858 {
f8bf15fe
CD
859 /* We don't know if we have PD ownership. Once we check the local
860 stopped_start we'll know if we're in state (a) or (b) (see
861 CONCURRENCY NOTES above). */
862 if (stopped_start)
863 /* State (a), we own PD. The thread blocked on this lock either
864 because we're doing TD_CREATE event reporting, or for some
865 other reason that create_thread chose. Now let it run
866 free. */
32fed10f
RM
867 lll_unlock (pd->lock, LLL_PRIVATE);
868
869 /* We now have for sure more than one thread. The main thread might
870 not yet have the flag set. No need to set the global variable
871 again if this is what we use. */
872 THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
873 }
61dd6208
SP
874
875 out:
876 if (__glibc_unlikely (free_cpuset))
877 free (default_attr.cpuset);
878
879 return retval;
76a50749
UD
880}
881versioned_symbol (libpthread, __pthread_create_2_1, pthread_create, GLIBC_2_1);
882
883
884#if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
885int
80d9be81
JM
886__pthread_create_2_0 (pthread_t *newthread, const pthread_attr_t *attr,
887 void *(*start_routine) (void *), void *arg)
76a50749
UD
888{
889 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
890 the old size and access to the new members might crash the program.
891 We convert the struct now. */
892 struct pthread_attr new_attr;
893
894 if (attr != NULL)
895 {
896 struct pthread_attr *iattr = (struct pthread_attr *) attr;
897 size_t ps = __getpagesize ();
898
899 /* Copy values from the user-provided attributes. */
900 new_attr.schedparam = iattr->schedparam;
901 new_attr.schedpolicy = iattr->schedpolicy;
902 new_attr.flags = iattr->flags;
903
904 /* Fill in default values for the fields not present in the old
905 implementation. */
906 new_attr.guardsize = ps;
907 new_attr.stackaddr = NULL;
908 new_attr.stacksize = 0;
ca85ede0 909 new_attr.cpuset = NULL;
76a50749
UD
910
911 /* We will pass this value on to the real implementation. */
912 attr = (pthread_attr_t *) &new_attr;
913 }
914
915 return __pthread_create_2_1 (newthread, attr, start_routine, arg);
916}
917compat_symbol (libpthread, __pthread_create_2_0, pthread_create,
918 GLIBC_2_0);
919#endif
7f08f55a
RM
920\f
921/* Information for libthread_db. */
922
923#include "../nptl_db/db_info.c"
b639d0c9
UD
924\f
925/* If pthread_create is present, libgcc_eh.a and libsupc++.a expects some other POSIX thread
926 functions to be present as well. */
fa872e1b
AZ
927PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_lock)
928PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_trylock)
929PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_unlock)
b639d0c9 930
fa872e1b
AZ
931PTHREAD_STATIC_FN_REQUIRE (__pthread_once)
932PTHREAD_STATIC_FN_REQUIRE (__pthread_cancel)
b639d0c9 933
fa872e1b
AZ
934PTHREAD_STATIC_FN_REQUIRE (__pthread_key_create)
935PTHREAD_STATIC_FN_REQUIRE (__pthread_key_delete)
936PTHREAD_STATIC_FN_REQUIRE (__pthread_setspecific)
937PTHREAD_STATIC_FN_REQUIRE (__pthread_getspecific)