/* Copyright (C) 2002-2017 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "pthreadP.h"
#include <hp-timing.h>
#include <ldsodefs.h>
#include <atomic.h>
#include <libc-internal.h>
#include <resolv.h>
#include <kernel-features.h>
#include <exit-thread.h>
#include <default-sched.h>
#include <futex-internal.h>
#include <shlib-compat.h>
#include <stap-probe.h>
/* Nozero if debugging mode is enabled. */
int __pthread_debug;
/* Globally enabled events. */
static td_thr_events_t __nptl_threads_events __attribute_used__;
/* Pointer to descriptor with the last event. */
static struct pthread *__nptl_last_event __attribute_used__;
/* Number of threads running. */
unsigned int __nptl_nthreads = 1;
/* Code to allocate and deallocate a stack. */
#include "allocatestack.c"
/* CONCURRENCY NOTES:
Understanding who is the owner of the 'struct pthread' or 'PD'
(refers to the value of the 'struct pthread *pd' function argument)
is critically important in determining exactly which operations are
allowed and which are not and when, particularly when it comes to the
implementation of pthread_create, pthread_join, pthread_detach, and
other functions which all operate on PD.
The owner of PD is responsible for freeing the final resources
associated with PD, and may examine the memory underlying PD at any
point in time until it frees it back to the OS or to reuse by the
runtime.
The thread which calls pthread_create is called the creating thread.
The creating thread begins as the owner of PD.
During startup the new thread may examine PD in coordination with the
owner thread (which may be itself).
The four cases of ownership transfer are:
(1) Ownership of PD is released to the process (all threads may use it)
after the new thread starts in a joinable state
i.e. pthread_create returns a usable pthread_t.
(2) Own