1/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
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
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <ctype.h>
20#include <errno.h>
21#include <stdbool.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdint.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>
32#include <exit-thread.h>
33#include <default-sched.h>
34#include <futex-internal.h>
35#include "libioP.h"
36
37#include <shlib-compat.h>
38
39#include <stap-probe.h>
40
41
42/* Nozero if debugging mode is enabled. */
43int __pthread_debug;
44
45/* Globally enabled events. */
46static td_thr_events_t __nptl_threads_events __attribute_used__;
47
48/* Pointer to descriptor with the last event. */
49static struct pthread *__nptl_last_event __attribute_used__;
50
51/* Number of threads running. */
52unsigned int __nptl_nthreads = 1;
53
54
55/* Code to allocate and deallocate a stack. */
56#include "allocatestack.c"
57
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
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:
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:
174 START_THREAD_DEFN and START_THREAD_SELF (see below).
175
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.
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
199 canceled before calling user code (*PD->start_routine). */
200static int create_thread (struct pthread *pd, const struct pthread_attr *attr,
201 bool *stopped_start, STACK_VARIABLES_PARMS,
202 bool *thread_ran);
203
204#include <createthread.c>
205
206
207struct pthread *
208__find_in_stack_list (struct pthread *pd)
209{
210 list_t *entry;
211 struct pthread *result = NULL;
212
213 lll_lock (stack_cache_lock, LLL_PRIVATE);
214
215 list_for_each (entry, &stack_used)
216 {
217 struct pthread *curp;
218
219 curp = list_entry (entry, struct pthread, list);
220 if (curp == pd)
221 {
222 result = curp;
223 break;
224 }
225 }
226
227 if (result == NULL)
228 list_for_each (entry, &__stack_user)
229 {
230 struct pthread *curp;
231
232 curp = list_entry (entry, struct pthread, list);
233 if (curp == pd)
234 {
235 result = curp;
236 break;
237 }
238 }
239
240 lll_unlock (stack_cache_lock, LLL_PRIVATE);
241
242 return result;
243}
244
245
246/* Deallocate POSIX thread-local-storage. */
247void
248attribute_hidden
249__nptl_deallocate_tsd (void)
250{
251 struct pthread *self = THREAD_SELF;
252
253 /* Maybe no data was ever allocated. This happens often so we have
254 a flag for this. */
255 if (THREAD_GETMEM (self, specific_used))
256 {
257 size_t round;
258 size_t cnt;
259
260 round = 0;
261 do
262 {
263 size_t idx;
264
265 /* So far no new nonzero data entry. */
266 THREAD_SETMEM (self, specific_used, false);
267
268 for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
269 {
270 struct pthread_key_data *level2;
271
272 level2 = THREAD_GETMEM_NC (self, specific, cnt);
273
274 if (level2 != NULL)
275 {
276 size_t inner;
277
278 for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE;
279 ++inner, ++idx)
280 {
281 void *data = level2[inner].data;
282
283 if (data != NULL)
284 {
285 /* Always clear the data. */
286 level2[inner].data = NULL;
287
288 /* Make sure the data corresponds to a valid
289 key. This test fails if the key was
290 deallocated and also if it was
291 re-allocated. It is the user's
292 responsibility to free the memory in this
293 case. */
294 if (level2[inner].seq
295 == __pthread_keys[idx].seq
296 /* It is not necessary to register a destructor
297 function. */
298 && __pthread_keys[idx].destr != NULL)
299 /* Call the user-provided destructor. */
300 __pthread_keys[idx].destr (data);
301 }
302 }
303 }
304 else
305 idx += PTHREAD_KEY_1STLEVEL_SIZE;
306 }
307
308 if (THREAD_GETMEM (self, specific_used) == 0)
309 /* No data has been modified. */
310 goto just_free;
311 }
312 /* We only repeat the process a fixed number of times. */
313 while (__builtin_expect (++round < PTHREAD_DESTRUCTOR_ITERATIONS, 0));
314
315 /* Just clear the memory of the first block for reuse. */
316 memset (&THREAD_SELF->specific_1stblock, '\0',
317 sizeof (self->specific_1stblock));
318
319 just_free:
320 /* Free the memory for the other blocks. */
321 for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
322 {
323 struct pthread_key_data *level2;
324
325 level2 = THREAD_GETMEM_NC (self, specific, cnt);
326 if (level2 != NULL)
327 {
328 /* The first block is allocated as part of the thread
329 descriptor. */
330 free (level2);
331 THREAD_SETMEM_NC (self, specific, cnt, NULL);
332 }
333 }
334
335 THREAD_SETMEM (self, specific_used, false);
336 }
337}
338
339
340/* Deallocate a thread's stack after optionally making sure the thread
341 descriptor is still valid. */
342void
343__free_tcb (struct pthread *pd)
344{
345 /* The thread is exiting now. */
346 if (__builtin_expect (atomic_bit_test_set (&pd->cancelhandling,
347 TERMINATED_BIT) == 0, 1))
348 {
349 /* Remove the descriptor from the list. */
350 if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
351 /* Something is really wrong. The descriptor for a still
352 running thread is gone. */
353 abort ();
354
355 /* Free TPP data. */
356 if (__glibc_unlikely (pd->tpp != NULL))
357 {
358 struct priority_protection_data *tpp = pd->tpp;
359
360 pd->tpp = NULL;
361 free (tpp);
362 }
363
364 /* Queue the stack memory block for reuse and exit the process. The
365 kernel will signal via writing to the address returned by
366 QUEUE-STACK when the stack is available. */
367 __deallocate_stack (pd);
368 }
369}
370
371
372/* Local function to start thread and handle cleanup.
373 createthread.c defines the macro START_THREAD_DEFN to the
374 declaration that its create_thread function will refer to, and
375 START_THREAD_SELF to the expression to optimally deliver the new
376 thread's THREAD_SELF value. */
377START_THREAD_DEFN
378{
379 struct pthread *pd = START_THREAD_SELF;
380
381#if HP_TIMING_AVAIL
382 /* Remember the time when the thread was started. */
383 hp_timing_t now;
384 HP_TIMING_NOW (now);
385 THREAD_SETMEM (pd, cpuclock_offset, now);
386#endif
387
388 /* Initialize resolver state pointer. */
389 __resp = &pd->res;
390
391 /* Initialize pointers to locale data. */
392 __ctype_init ();
393
394 /* Allow setxid from now onwards. */
395 if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) == -2))
396 futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE);
397
398#ifdef __NR_set_robust_list
399# ifndef __ASSUME_SET_ROBUST_LIST
400 if (__set_robust_list_avail >= 0)
401# endif
402 {
403 INTERNAL_SYSCALL_DECL (err);
404 /* This call should never fail because the initial call in init.c
405 succeeded. */
406 INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
407 sizeof (struct robust_list_head));
408 }
409#endif
410
411#ifdef SIGCANCEL
412 /* If the parent was running cancellation handlers while creating
413 the thread the new thread inherited the signal mask. Reset the
414 cancellation signal mask. */
415 if (__glibc_unlikely (pd->parent_cancelhandling & CANCELING_BITMASK))
416 {
417 INTERNAL_SYSCALL_DECL (err);
418 sigset_t mask;
419 __sigemptyset (&mask);
420 __sigaddset (&mask, SIGCANCEL);
421 (void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &mask,
422 NULL, _NSIG / 8);
423 }
424#endif
425
426 /* This is where the try/finally block should be created. For
427 compilers without that support we do use setjmp. */
428 struct pthread_unwind_buf unwind_buf;
429
430 /* No previous handlers. */
431 unwind_buf.priv.data.prev = NULL;
432 unwind_buf.priv.data.cleanup = NULL;
433
434 int not_first_call;
435 not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
436 if (__glibc_likely (! not_first_call))
437 {
438 /* Store the new cleanup handler info. */
439 THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf);
440
441 /* We are either in (a) or (b), and in either case we either own
442 PD already (2) or are about to own PD (1), and so our only
443 restriction would be that we can't free PD until we know we
444 have ownership (see CONCURRENCY NOTES above). */
445 if (__glibc_unlikely (pd->stopped_start))
446 {
447 int oldtype = CANCEL_ASYNC ();
448
449 /* Get the lock the parent locked to force synchronization. */
450 lll_lock (pd->lock, LLL_PRIVATE);
451
452 /* We have ownership of PD now. */
453
454 /* And give it up right away. */
455 lll_unlock (pd->lock, LLL_PRIVATE);
456
457 CANCEL_RESET (oldtype);
458 }
459
460 LIBC_PROBE (pthread_start, 3, (pthread_t) pd, pd->start_routine, pd->arg);
461
462 /* Run the code the user provided. */
463 THREAD_SETMEM (pd, result, pd->start_routine (pd->arg));
464 }
465
466 /* Call destructors for the thread_local TLS variables. */
467#ifndef SHARED
468 if (&__call_tls_dtors != NULL)
469#endif
470 __call_tls_dtors ();
471
472 /* Run the destructor for the thread-local data. */
473 __nptl_deallocate_tsd ();
474
475 /* Clean up any state libc stored in thread-local variables. */
476 __libc_thread_freeres ();
477
478 /* If this is the last thread we terminate the process now. We
479 do not notify the debugger, it might just irritate it if there
480 is no thread left. */
481 if (__glibc_unlikely (atomic_decrement_and_test (&__nptl_nthreads)))
482 /* This was the last thread. */
483 exit (0);
484
485 /* Report the death of the thread if this is wanted. */
486 if (__glibc_unlikely (pd->report_events))
487 {
488 /* See whether TD_DEATH is in any of the mask. */
489 const int idx = __td_eventword (TD_DEATH);
490 const uint32_t mask = __td_eventmask (TD_DEATH);
491
492 if ((mask & (__nptl_threads_events.event_bits[idx]
493 | pd->eventbuf.eventmask.event_bits[idx])) != 0)
494 {
495 /* Yep, we have to signal the death. Add the descriptor to
496 the list but only if it is not already on it. */
497 if (pd->nextevent == NULL)
498 {
499 pd->eventbuf.eventnum = TD_DEATH;
500 pd->eventbuf.eventdata = pd;
501
502 do
503 pd->nextevent = __nptl_last_event;
504 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
505 pd, pd->nextevent));
506 }
507
508 /* Now call the function which signals the event. See
509 CONCURRENCY NOTES for the nptl_db interface comments. */
510 __nptl_death_event ();
511 }
512 }
513
514 /* The thread is exiting now. Don't set this bit until after we've hit
515 the event-reporting breakpoint, so that td_thr_get_info on us while at
516 the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
517 atomic_bit_set (&pd->cancelhandling, EXITING_BIT);
518
519#ifndef __ASSUME_SET_ROBUST_LIST
520 /* If this thread has any robust mutexes locked, handle them now. */
521# if __PTHREAD_MUTEX_HAVE_PREV
522 void *robust = pd->robust_head.list;
523# else
524 __pthread_slist_t *robust = pd->robust_list.__next;
525# endif
526 /* We let the kernel do the notification if it is able to do so.
527 If we have to do it here there for sure are no PI mutexes involved
528 since the kernel support for them is even more recent. */
529 if (__set_robust_list_avail < 0
530 && __builtin_expect (robust != (void *) &pd->robust_head, 0))
531 {
532 do
533 {
534 struct __pthread_mutex_s *this = (struct __pthread_mutex_s *)
535 ((char *) robust - offsetof (struct __pthread_mutex_s,
536 __list.__next));
537 robust = *((void **) robust);
538
539# if __PTHREAD_MUTEX_HAVE_PREV
540 this->__list.__prev = NULL;
541# endif
542 this->__list.__next = NULL;
543
544 atomic_or (&this->__lock, FUTEX_OWNER_DIED);
545 futex_wake ((unsigned int *) &this->__lock, 1,
546 /* XYZ */ FUTEX_SHARED);
547 }
548 while (robust != (void *) &pd->robust_head);
549 }
550#endif
551
552 advise_stack_range (pd->stackblock, pd->stackblock_size, (uintptr_t) pd,
553 pd->guardsize);
554
555 /* If the thread is detached free the TCB. */
556 if (IS_DETACHED (pd))
557 /* Free the TCB. */
558 __free_tcb (pd);
559 else if (__glibc_unlikely (pd->cancelhandling & SETXID_BITMASK))
560 {
561 /* Some other thread might call any of the setXid functions and expect
562 us to reply. In this case wait until we did that. */
563 do
564 /* XXX This differs from the typical futex_wait_simple pattern in that
565 the futex_wait condition (setxid_futex) is different from the
566 condition used in the surrounding loop (cancelhandling). We need
567 to check and document why this is correct. */
568 futex_wait_simple (&pd->setxid_futex, 0, FUTEX_PRIVATE);
569 while (pd->cancelhandling & SETXID_BITMASK);
570
571 /* Reset the value so that the stack can be reused. */
572 pd->setxid_futex = 0;
573 }
574
575 /* We cannot call '_exit' here. '_exit' will terminate the process.
576
577 The 'exit' implementation in the kernel will signal when the
578 process is really dead since 'clone' got passed the CLONE_CHILD_CLEARTID
579 flag. The 'tid' field in the TCB will be set to zero.
580
581 The exit code is zero since in case all threads exit by calling
582 'pthread_exit' the exit status must be 0 (zero). */
583 __exit_thread ();
584
585 /* NOTREACHED */
586}
587
588
589/* Return true iff obliged to report TD_CREATE events. */
590static bool
591report_thread_creation (struct pthread *pd)
592{
593 if (__glibc_unlikely (THREAD_GETMEM (THREAD_SELF, report_events)))
594 {
595 /* The parent thread is supposed to report events.
596 Check whether the TD_CREATE event is needed, too. */
597 const size_t idx = __td_eventword (TD_CREATE);
598 const uint32_t mask = __td_eventmask (TD_CREATE);
599
600 return ((mask & (__nptl_threads_events.event_bits[idx]
601 | pd->eventbuf.eventmask.event_bits[idx])) != 0);
602 }
603 return false;
604}
605
606
607int
608__pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr,
609 void *(*start_routine) (void *), void *arg)
610{
611 STACK_VARIABLES;
612
613 const struct pthread_attr *iattr = (struct pthread_attr *) attr;
614 struct pthread_attr default_attr;
615 bool free_cpuset = false;
616 if (iattr == NULL)
617 {
618 lll_lock (__default_pthread_attr_lock, LLL_PRIVATE);
619 default_attr = __default_pthread_attr;
620 size_t cpusetsize = default_attr.cpusetsize;
621 if (cpusetsize > 0)
622 {
623 cpu_set_t *cpuset;
624 if (__glibc_likely (__libc_use_alloca (cpusetsize)))
625 cpuset = __alloca (cpusetsize);
626 else
627 {
628 cpuset = malloc (cpusetsize);
629 if (cpuset == NULL)
630 {
631 lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
632 return ENOMEM;
633 }
634 free_cpuset = true;
635 }
636 memcpy (cpuset, default_attr.cpuset, cpusetsize);
637 default_attr.cpuset = cpuset;
638 }
639 lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
640 iattr = &default_attr;
641 }
642
643 struct pthread *pd = NULL;
644 int err = ALLOCATE_STACK (iattr, &pd);
645 int retval = 0;
646
647 if (__glibc_unlikely (err != 0))
648 /* Something went wrong. Maybe a parameter of the attributes is
649 invalid or we could not allocate memory. Note we have to
650 translate error codes. */
651 {
652 retval = err == ENOMEM ? EAGAIN : err;
653 goto out;
654 }
655
656
657 /* Initialize the TCB. All initializations with zero should be
658 performed in 'get_cached_stack'. This way we avoid doing this if
659 the stack freshly allocated with 'mmap'. */
660
661#if TLS_TCB_AT_TP
662 /* Reference to the TCB itself. */
663 pd->header.self = pd;
664
665 /* Self-reference for TLS. */
666 pd->header.tcb = pd;
667#endif
668
669 /* Store the address of the start routine and the parameter. Since
670 we do not start the function directly the stillborn thread will
671 get the information from its thread descriptor. */
672 pd->start_routine = start_routine;
673 pd->arg = arg;
674
675 /* Copy the thread attribute flags. */
676 struct pthread *self = THREAD_SELF;
677 pd->flags = ((iattr->flags & ~(ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
678 | (self->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)));
679
680 /* Initialize the field for the ID of the thread which is waiting
681 for us. This is a self-reference in case the thread is created
682 detached. */
683 pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL;
684
685 /* The debug events are inherited from the parent. */
686 pd->eventbuf = self->eventbuf;
687
688
689 /* Copy the parent's scheduling parameters. The flags will say what
690 is valid and what is not. */
691 pd->schedpolicy = self->schedpolicy;
692 pd->schedparam = self->schedparam;
693
694 /* Copy the stack guard canary. */
695#ifdef THREAD_COPY_STACK_GUARD
696 THREAD_COPY_STACK_GUARD (pd);
697#endif
698
699 /* Copy the pointer guard value. */
700#ifdef THREAD_COPY_POINTER_GUARD
701 THREAD_COPY_POINTER_GUARD (pd);
702#endif
703
704 /* Verify the sysinfo bits were copied in allocate_stack if needed. */
705#ifdef NEED_DL_SYSINFO
706 CHECK_THREAD_SYSINFO (pd);
707#endif
708
709 /* Inform start_thread (above) about cancellation state that might
710 translate into inherited signal state. */
711 pd->parent_cancelhandling = THREAD_GETMEM (THREAD_SELF, cancelhandling);
712
713 /* Determine scheduling parameters for the thread. */
714 if (__builtin_expect ((iattr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0, 0)
715 && (iattr->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) != 0)
716 {
717 /* Use the scheduling parameters the user provided. */
718 if (iattr->flags & ATTR_FLAG_POLICY_SET)
719 {
720 pd->schedpolicy = iattr->schedpolicy;
721 pd->flags |= ATTR_FLAG_POLICY_SET;
722 }
723 if (iattr->flags & ATTR_FLAG_SCHED_SET)
724 {
725 /* The values were validated in pthread_attr_setschedparam. */
726 pd->schedparam = iattr->schedparam;
727 pd->flags |= ATTR_FLAG_SCHED_SET;
728 }
729
730 if ((pd->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
731 != (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
732 collect_default_sched (pd);
733 }
734
735 if (__glibc_unlikely (__nptl_nthreads == 1))
736 _IO_enable_locks ();
737
738 /* Pass the descriptor to the caller. */
739 *newthread = (pthread_t) pd;
740
741 LIBC_PROBE (pthread_create, 4, newthread, attr, start_routine, arg);
742
743 /* One more thread. We cannot have the thread do this itself, since it
744 might exist but not have been scheduled yet by the time we've returned
745 and need to check the value to behave correctly. We must do it before
746 creating the thread, in case it does get scheduled first and then
747 might mistakenly think it was the only thread. In the failure case,
748 we momentarily store a false value; this doesn't matter because there
749 is no kosher thing a signal handler interrupting us right here can do
750 that cares whether the thread count is correct. */
751 atomic_increment (&__nptl_nthreads);
752
753 /* Our local value of stopped_start and thread_ran can be accessed at
754 any time. The PD->stopped_start may only be accessed if we have
755 ownership of PD (see CONCURRENCY NOTES above). */
756 bool stopped_start = false; bool thread_ran = false;
757
758 /* Start the thread. */
759 if (__glibc_unlikely (report_thread_creation (pd)))
760 {
761 stopped_start = true;
762
763 /* We always create the thread stopped at startup so we can
764 notify the debugger. */
765 retval = create_thread (pd, iattr, &stopped_start,
766 STACK_VARIABLES_ARGS, &thread_ran);
767 if (retval == 0)
768 {
769 /* We retain ownership of PD until (a) (see CONCURRENCY NOTES
770 above). */
771
772 /* Assert stopped_start is true in both our local copy and the
773 PD copy. */
774 assert (stopped_start);
775 assert (pd->stopped_start);
776
777 /* Now fill in the information about the new thread in
778 the newly created thread's data structure. We cannot let
779 the new thread do this since we don't know whether it was
780 already scheduled when we send the event. */
781 pd->eventbuf.eventnum = TD_CREATE;
782 pd->eventbuf.eventdata = pd;
783
784 /* Enqueue the descriptor. */
785 do
786 pd->nextevent = __nptl_last_event;
787 while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
788 pd, pd->nextevent)
789 != 0);
790
791 /* Now call the function which signals the event. See
792 CONCURRENCY NOTES for the nptl_db interface comments. */
793 __nptl_create_event ();
794 }
795 }
796 else
797 retval = create_thread (pd, iattr, &stopped_start,
798 STACK_VARIABLES_ARGS, &thread_ran);
799
800 if (__glibc_unlikely (retval != 0))
801 {
802 if (thread_ran)
803 /* State (c) or (d) and we may not have PD ownership (see
804 CONCURRENCY NOTES above). We can assert that STOPPED_START
805 must have been true because thread creation didn't fail, but
806 thread attribute setting did. */
807 /* See bug 19511 which explains why doing nothing here is a
808 resource leak for a joinable thread. */
809 assert (stopped_start);
810 else
811 {
812 /* State (e) and we have ownership of PD (see CONCURRENCY
813 NOTES above). */
814
815 /* Oops, we lied for a second. */
816 atomic_decrement (&__nptl_nthreads);
817
818 /* Perhaps a thread wants to change the IDs and is waiting for this
819 stillborn thread. */
820 if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0)
821 == -2))
822 futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE);
823
824 /* Free the resources. */
825 __deallocate_stack (pd);
826 }
827
828 /* We have to translate error codes. */
829 if (retval == ENOMEM)
830 retval = EAGAIN;
831 }
832 else
833 {
834 /* We don't know if we have PD ownership. Once we check the local
835 stopped_start we'll know if we're in state (a) or (b) (see
836 CONCURRENCY NOTES above). */
837 if (stopped_start)
838 /* State (a), we own PD. The thread blocked on this lock either
839 because we're doing TD_CREATE event reporting, or for some
840 other reason that create_thread chose. Now let it run
841 free. */
842 lll_unlock (pd->lock, LLL_PRIVATE);
843
844 /* We now have for sure more than one thread. The main thread might
845 not yet have the flag set. No need to set the global variable
846 again if this is what we use. */
847 THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
848 }
849
850 out:
851 if (__glibc_unlikely (free_cpuset))
852 free (default_attr.cpuset);
853
854 return retval;
855}
856versioned_symbol (libpthread, __pthread_create_2_1, pthread_create, GLIBC_2_1);
857
858
859#if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
860int
861__pthread_create_2_0 (pthread_t *newthread, const pthread_attr_t *attr,
862 void *(*start_routine) (void *), void *arg)
863{
864 /* The ATTR attribute is not really of type `pthread_attr_t *'. It has
865 the old size and access to the new members might crash the program.
866 We convert the struct now. */
867 struct pthread_attr new_attr;
868
869 if (attr != NULL)
870 {
871 struct pthread_attr *iattr = (struct pthread_attr *) attr;
872 size_t ps = __getpagesize ();
873
874 /* Copy values from the user-provided attributes. */
875 new_attr.schedparam = iattr->schedparam;
876 new_attr.schedpolicy = iattr->schedpolicy;
877 new_attr.flags = iattr->flags;
878
879 /* Fill in default values for the fields not present in the old
880 implementation. */
881 new_attr.guardsize = ps;
882 new_attr.stackaddr = NULL;
883 new_attr.stacksize = 0;
884 new_attr.cpuset = NULL;
885
886 /* We will pass this value on to the real implementation. */
887 attr = (pthread_attr_t *) &new_attr;
888 }
889
890 return __pthread_create_2_1 (newthread, attr, start_routine, arg);
891}
892compat_symbol (libpthread, __pthread_create_2_0, pthread_create,
893 GLIBC_2_0);
894#endif
895
896/* Information for libthread_db. */
897
898#include "../nptl_db/db_info.c"
899
900/* If pthread_create is present, libgcc_eh.a and libsupc++.a expects some other POSIX thread
901 functions to be present as well. */
902PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_lock)
903PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_trylock)
904PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_unlock)
905
906PTHREAD_STATIC_FN_REQUIRE (__pthread_once)
907PTHREAD_STATIC_FN_REQUIRE (__pthread_cancel)
908
909PTHREAD_STATIC_FN_REQUIRE (__pthread_key_create)
910PTHREAD_STATIC_FN_REQUIRE (__pthread_key_delete)
911PTHREAD_STATIC_FN_REQUIRE (__pthread_setspecific)
912PTHREAD_STATIC_FN_REQUIRE (__pthread_getspecific)
913