|
Packit |
6c4009 |
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <ctype.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <stdbool.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <stdint.h>
|
|
Packit |
6c4009 |
#include "pthreadP.h"
|
|
Packit |
6c4009 |
#include <hp-timing.h>
|
|
Packit |
6c4009 |
#include <ldsodefs.h>
|
|
Packit |
6c4009 |
#include <atomic.h>
|
|
Packit |
6c4009 |
#include <libc-internal.h>
|
|
Packit |
6c4009 |
#include <resolv.h>
|
|
Packit |
6c4009 |
#include <kernel-features.h>
|
|
Packit |
6c4009 |
#include <exit-thread.h>
|
|
Packit |
6c4009 |
#include <default-sched.h>
|
|
Packit |
6c4009 |
#include <futex-internal.h>
|
|
Packit |
6c4009 |
#include <tls-setup.h>
|
|
Packit |
6c4009 |
#include "libioP.h"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <shlib-compat.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <stap-probe.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Nozero if debugging mode is enabled. */
|
|
Packit |
6c4009 |
int __pthread_debug;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Globally enabled events. */
|
|
Packit |
6c4009 |
static td_thr_events_t __nptl_threads_events __attribute_used__;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Pointer to descriptor with the last event. */
|
|
Packit |
6c4009 |
static struct pthread *__nptl_last_event __attribute_used__;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Number of threads running. */
|
|
Packit |
6c4009 |
unsigned int __nptl_nthreads = 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Code to allocate and deallocate a stack. */
|
|
Packit |
6c4009 |
#include "allocatestack.c"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* CONCURRENCY NOTES:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
Understanding who is the owner of the 'struct pthread' or 'PD'
|
|
Packit |
6c4009 |
(refers to the value of the 'struct pthread *pd' function argument)
|
|
Packit |
6c4009 |
is critically important in determining exactly which operations are
|
|
Packit |
6c4009 |
allowed and which are not and when, particularly when it comes to the
|
|
Packit |
6c4009 |
implementation of pthread_create, pthread_join, pthread_detach, and
|
|
Packit |
6c4009 |
other functions which all operate on PD.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The owner of PD is responsible for freeing the final resources
|
|
Packit |
6c4009 |
associated with PD, and may examine the memory underlying PD at any
|
|
Packit |
6c4009 |
point in time until it frees it back to the OS or to reuse by the
|
|
Packit |
6c4009 |
runtime.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The thread which calls pthread_create is called the creating thread.
|
|
Packit |
6c4009 |
The creating thread begins as the owner of PD.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
During startup the new thread may examine PD in coordination with the
|
|
Packit |
6c4009 |
owner thread (which may be itself).
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The four cases of ownership transfer are:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(1) Ownership of PD is released to the process (all threads may use it)
|
|
Packit |
6c4009 |
after the new thread starts in a joinable state
|
|
Packit |
6c4009 |
i.e. pthread_create returns a usable pthread_t.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(2) Ownership of PD is released to the new thread starting in a detached
|
|
Packit |
6c4009 |
state.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(3) Ownership of PD is dynamically released to a running thread via
|
|
Packit |
6c4009 |
pthread_detach.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(4) Ownership of PD is acquired by the thread which calls pthread_join.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
Implementation notes:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The PD->stopped_start and thread_ran variables are used to determine
|
|
Packit |
6c4009 |
exactly which of the four ownership states we are in and therefore
|
|
Packit |
6c4009 |
what actions can be taken. For example after (2) we cannot read or
|
|
Packit |
6c4009 |
write from PD anymore since the thread may no longer exist and the
|
|
Packit |
6c4009 |
memory may be unmapped.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
It is important to point out that PD->lock is being used both
|
|
Packit |
6c4009 |
similar to a one-shot semaphore and subsequently as a mutex. The
|
|
Packit |
6c4009 |
lock is taken in the parent to force the child to wait, and then the
|
|
Packit |
6c4009 |
child releases the lock. However, this semaphore-like effect is used
|
|
Packit |
6c4009 |
only for synchronizing the parent and child. After startup the lock
|
|
Packit |
6c4009 |
is used like a mutex to create a critical section during which a
|
|
Packit |
6c4009 |
single owner modifies the thread parameters.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The most complicated cases happen during thread startup:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(a) If the created thread is in a detached (PTHREAD_CREATE_DETACHED),
|
|
Packit |
6c4009 |
or joinable (default PTHREAD_CREATE_JOINABLE) state and
|
|
Packit |
6c4009 |
STOPPED_START is true, then the creating thread has ownership of
|
|
Packit |
6c4009 |
PD until the PD->lock is released by pthread_create. If any
|
|
Packit |
6c4009 |
errors occur we are in states (c), (d), or (e) below.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(b) If the created thread is in a detached state
|
|
Packit |
6c4009 |
(PTHREAD_CREATED_DETACHED), and STOPPED_START is false, then the
|
|
Packit |
6c4009 |
creating thread has ownership of PD until it invokes the OS
|
|
Packit |
6c4009 |
kernel's thread creation routine. If this routine returns
|
|
Packit |
6c4009 |
without error, then the created thread owns PD; otherwise, see
|
|
Packit |
6c4009 |
(c) and (e) below.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(c) If the detached thread setup failed and THREAD_RAN is true, then
|
|
Packit |
6c4009 |
the creating thread releases ownership to the new thread by
|
|
Packit |
6c4009 |
sending a cancellation signal. All threads set THREAD_RAN to
|
|
Packit |
6c4009 |
true as quickly as possible after returning from the OS kernel's
|
|
Packit |
6c4009 |
thread creation routine.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(d) If the joinable thread setup failed and THREAD_RAN is true, then
|
|
Packit |
6c4009 |
then the creating thread retains ownership of PD and must cleanup
|
|
Packit |
6c4009 |
state. Ownership cannot be released to the process via the
|
|
Packit |
6c4009 |
return of pthread_create since a non-zero result entails PD is
|
|
Packit |
6c4009 |
undefined and therefore cannot be joined to free the resources.
|
|
Packit |
6c4009 |
We privately call pthread_join on the thread to finish handling
|
|
Packit |
6c4009 |
the resource shutdown (Or at least we should, see bug 19511).
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(e) If the thread creation failed and THREAD_RAN is false, then the
|
|
Packit |
6c4009 |
creating thread retains ownership of PD and must cleanup state.
|
|
Packit |
6c4009 |
No waiting for the new thread is required because it never
|
|
Packit |
6c4009 |
started.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The nptl_db interface:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The interface with nptl_db requires that we enqueue PD into a linked
|
|
Packit |
6c4009 |
list and then call a function which the debugger will trap. The PD
|
|
Packit |
6c4009 |
will then be dequeued and control returned to the thread. The caller
|
|
Packit |
6c4009 |
at the time must have ownership of PD and such ownership remains
|
|
Packit |
6c4009 |
after control returns to thread. The enqueued PD is removed from the
|
|
Packit |
6c4009 |
linked list by the nptl_db callback td_thr_event_getmsg. The debugger
|
|
Packit |
6c4009 |
must ensure that the thread does not resume execution, otherwise
|
|
Packit |
6c4009 |
ownership of PD may be lost and examining PD will not be possible.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
Note that the GNU Debugger as of (December 10th 2015) commit
|
|
Packit |
6c4009 |
c2c2a31fdb228d41ce3db62b268efea04bd39c18 no longer uses
|
|
Packit |
6c4009 |
td_thr_event_getmsg and several other related nptl_db interfaces. The
|
|
Packit |
6c4009 |
principal reason for this is that nptl_db does not support non-stop
|
|
Packit |
6c4009 |
mode where other threads can run concurrently and modify runtime
|
|
Packit |
6c4009 |
structures currently in use by the debugger and the nptl_db
|
|
Packit |
6c4009 |
interface.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
Axioms:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
* The create_thread function can never set stopped_start to false.
|
|
Packit |
6c4009 |
* The created thread can read stopped_start but never write to it.
|
|
Packit |
6c4009 |
* The variable thread_ran is set some time after the OS thread
|
|
Packit |
6c4009 |
creation routine returns, how much time after the thread is created
|
|
Packit |
6c4009 |
is unspecified, but it should be as quickly as possible.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
*/
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* CREATE THREAD NOTES:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
createthread.c defines the create_thread function, and two macros:
|
|
Packit |
6c4009 |
START_THREAD_DEFN and START_THREAD_SELF (see below).
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
create_thread must initialize PD->stopped_start. It should be true
|
|
Packit |
6c4009 |
if the STOPPED_START parameter is true, or if create_thread needs the
|
|
Packit |
6c4009 |
new thread to synchronize at startup for some other implementation
|
|
Packit |
6c4009 |
reason. If STOPPED_START will be true, then create_thread is obliged
|
|
Packit |
6c4009 |
to lock PD->lock before starting the thread. Then pthread_create
|
|
Packit |
6c4009 |
unlocks PD->lock which synchronizes-with START_THREAD_DEFN in the
|
|
Packit |
6c4009 |
child thread which does an acquire/release of PD->lock as the last
|
|
Packit |
6c4009 |
action before calling the user entry point. The goal of all of this
|
|
Packit |
6c4009 |
is to ensure that the required initial thread attributes are applied
|
|
Packit |
6c4009 |
(by the creating thread) before the new thread runs user code. Note
|
|
Packit |
6c4009 |
that the the functions pthread_getschedparam, pthread_setschedparam,
|
|
Packit |
6c4009 |
pthread_setschedprio, __pthread_tpp_change_priority, and
|
|
Packit |
6c4009 |
__pthread_current_priority reuse the same lock, PD->lock, for a
|
|
Packit |
6c4009 |
similar purpose e.g. synchronizing the setting of similar thread
|
|
Packit |
6c4009 |
attributes. These functions are never called before the thread is
|
|
Packit |
6c4009 |
created, so don't participate in startup syncronization, but given
|
|
Packit |
6c4009 |
that the lock is present already and in the unlocked state, reusing
|
|
Packit |
6c4009 |
it saves space.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The return value is zero for success or an errno code for failure.
|
|
Packit |
6c4009 |
If the return value is ENOMEM, that will be translated to EAGAIN,
|
|
Packit |
6c4009 |
so create_thread need not do that. On failure, *THREAD_RAN should
|
|
Packit |
6c4009 |
be set to true iff the thread actually started up and then got
|
|
Packit |
6c4009 |
canceled before calling user code (*PD->start_routine). */
|
|
Packit |
6c4009 |
static int create_thread (struct pthread *pd, const struct pthread_attr *attr,
|
|
Packit |
6c4009 |
bool *stopped_start, STACK_VARIABLES_PARMS,
|
|
Packit |
6c4009 |
bool *thread_ran);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <createthread.c>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct pthread *
|
|
Packit |
6c4009 |
__find_in_stack_list (struct pthread *pd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
list_t *entry;
|
|
Packit |
6c4009 |
struct pthread *result = NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
lll_lock (stack_cache_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
list_for_each (entry, &stack_used)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct pthread *curp;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
curp = list_entry (entry, struct pthread, list);
|
|
Packit |
6c4009 |
if (curp == pd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
result = curp;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (result == NULL)
|
|
Packit |
6c4009 |
list_for_each (entry, &__stack_user)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct pthread *curp;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
curp = list_entry (entry, struct pthread, list);
|
|
Packit |
6c4009 |
if (curp == pd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
result = curp;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
lll_unlock (stack_cache_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Deallocate POSIX thread-local-storage. */
|
|
Packit |
6c4009 |
void
|
|
Packit |
6c4009 |
attribute_hidden
|
|
Packit |
6c4009 |
__nptl_deallocate_tsd (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct pthread *self = THREAD_SELF;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Maybe no data was ever allocated. This happens often so we have
|
|
Packit |
6c4009 |
a flag for this. */
|
|
Packit |
6c4009 |
if (THREAD_GETMEM (self, specific_used))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t round;
|
|
Packit |
6c4009 |
size_t cnt;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
round = 0;
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t idx;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* So far no new nonzero data entry. */
|
|
Packit |
6c4009 |
THREAD_SETMEM (self, specific_used, false);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (cnt = idx = 0; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct pthread_key_data *level2;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
level2 = THREAD_GETMEM_NC (self, specific, cnt);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (level2 != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t inner;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (inner = 0; inner < PTHREAD_KEY_2NDLEVEL_SIZE;
|
|
Packit |
6c4009 |
++inner, ++idx)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
void *data = level2[inner].data;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (data != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Always clear the data. */
|
|
Packit |
6c4009 |
level2[inner].data = NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Make sure the data corresponds to a valid
|
|
Packit |
6c4009 |
key. This test fails if the key was
|
|
Packit |
6c4009 |
deallocated and also if it was
|
|
Packit |
6c4009 |
re-allocated. It is the user's
|
|
Packit |
6c4009 |
responsibility to free the memory in this
|
|
Packit |
6c4009 |
case. */
|
|
Packit |
6c4009 |
if (level2[inner].seq
|
|
Packit |
6c4009 |
== __pthread_keys[idx].seq
|
|
Packit |
6c4009 |
/* It is not necessary to register a destructor
|
|
Packit |
6c4009 |
function. */
|
|
Packit |
6c4009 |
&& __pthread_keys[idx].destr != NULL)
|
|
Packit |
6c4009 |
/* Call the user-provided destructor. */
|
|
Packit |
6c4009 |
__pthread_keys[idx].destr (data);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
idx += PTHREAD_KEY_1STLEVEL_SIZE;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (THREAD_GETMEM (self, specific_used) == 0)
|
|
Packit |
6c4009 |
/* No data has been modified. */
|
|
Packit |
6c4009 |
goto just_free;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
/* We only repeat the process a fixed number of times. */
|
|
Packit |
6c4009 |
while (__builtin_expect (++round < PTHREAD_DESTRUCTOR_ITERATIONS, 0));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Just clear the memory of the first block for reuse. */
|
|
Packit |
6c4009 |
memset (&THREAD_SELF->specific_1stblock, '\0',
|
|
Packit |
6c4009 |
sizeof (self->specific_1stblock));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
just_free:
|
|
Packit |
6c4009 |
/* Free the memory for the other blocks. */
|
|
Packit |
6c4009 |
for (cnt = 1; cnt < PTHREAD_KEY_1STLEVEL_SIZE; ++cnt)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct pthread_key_data *level2;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
level2 = THREAD_GETMEM_NC (self, specific, cnt);
|
|
Packit |
6c4009 |
if (level2 != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* The first block is allocated as part of the thread
|
|
Packit |
6c4009 |
descriptor. */
|
|
Packit |
6c4009 |
free (level2);
|
|
Packit |
6c4009 |
THREAD_SETMEM_NC (self, specific, cnt, NULL);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
THREAD_SETMEM (self, specific_used, false);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Deallocate a thread's stack after optionally making sure the thread
|
|
Packit |
6c4009 |
descriptor is still valid. */
|
|
Packit |
6c4009 |
void
|
|
Packit |
6c4009 |
__free_tcb (struct pthread *pd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* The thread is exiting now. */
|
|
Packit |
6c4009 |
if (__builtin_expect (atomic_bit_test_set (&pd->cancelhandling,
|
|
Packit |
6c4009 |
TERMINATED_BIT) == 0, 1))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Remove the descriptor from the list. */
|
|
Packit |
6c4009 |
if (DEBUGGING_P && __find_in_stack_list (pd) == NULL)
|
|
Packit |
6c4009 |
/* Something is really wrong. The descriptor for a still
|
|
Packit |
6c4009 |
running thread is gone. */
|
|
Packit |
6c4009 |
abort ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Free TPP data. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely (pd->tpp != NULL))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct priority_protection_data *tpp = pd->tpp;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
pd->tpp = NULL;
|
|
Packit |
6c4009 |
free (tpp);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Queue the stack memory block for reuse and exit the process. The
|
|
Packit |
6c4009 |
kernel will signal via writing to the address returned by
|
|
Packit |
6c4009 |
QUEUE-STACK when the stack is available. */
|
|
Packit |
6c4009 |
__deallocate_stack (pd);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Local function to start thread and handle cleanup.
|
|
Packit |
6c4009 |
createthread.c defines the macro START_THREAD_DEFN to the
|
|
Packit |
6c4009 |
declaration that its create_thread function will refer to, and
|
|
Packit |
6c4009 |
START_THREAD_SELF to the expression to optimally deliver the new
|
|
Packit |
6c4009 |
thread's THREAD_SELF value. */
|
|
Packit |
6c4009 |
START_THREAD_DEFN
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct pthread *pd = START_THREAD_SELF;
|
|
Packit |
6c4009 |
|
|
Packit Service |
3b0880 |
#if HP_TIMING_AVAIL
|
|
Packit Service |
3b0880 |
/* Remember the time when the thread was started. */
|
|
Packit Service |
3b0880 |
hp_timing_t now;
|
|
Packit Service |
3b0880 |
HP_TIMING_NOW (now);
|
|
Packit Service |
3b0880 |
THREAD_SETMEM (pd, cpuclock_offset, now);
|
|
Packit Service |
3b0880 |
#endif
|
|
Packit Service |
3b0880 |
|
|
Packit |
6c4009 |
/* Initialize resolver state pointer. */
|
|
Packit |
6c4009 |
__resp = &pd->res;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Initialize pointers to locale data. */
|
|
Packit |
6c4009 |
__ctype_init ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Allow setxid from now onwards. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0) == -2))
|
|
Packit |
6c4009 |
futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifdef __NR_set_robust_list
|
|
Packit |
6c4009 |
# ifndef __ASSUME_SET_ROBUST_LIST
|
|
Packit |
6c4009 |
if (__set_robust_list_avail >= 0)
|
|
Packit |
6c4009 |
# endif
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
INTERNAL_SYSCALL_DECL (err);
|
|
Packit |
6c4009 |
/* This call should never fail because the initial call in init.c
|
|
Packit |
6c4009 |
succeeded. */
|
|
Packit |
6c4009 |
INTERNAL_SYSCALL (set_robust_list, err, 2, &pd->robust_head,
|
|
Packit |
6c4009 |
sizeof (struct robust_list_head));
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifdef SIGCANCEL
|
|
Packit |
6c4009 |
/* If the parent was running cancellation handlers while creating
|
|
Packit |
6c4009 |
the thread the new thread inherited the signal mask. Reset the
|
|
Packit |
6c4009 |
cancellation signal mask. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely (pd->parent_cancelhandling & CANCELING_BITMASK))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
INTERNAL_SYSCALL_DECL (err);
|
|
Packit |
6c4009 |
sigset_t mask;
|
|
Packit |
6c4009 |
__sigemptyset (&mask);
|
|
Packit |
6c4009 |
__sigaddset (&mask, SIGCANCEL);
|
|
Packit |
6c4009 |
(void) INTERNAL_SYSCALL (rt_sigprocmask, err, 4, SIG_UNBLOCK, &mask,
|
|
Packit |
6c4009 |
NULL, _NSIG / 8);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* This is where the try/finally block should be created. For
|
|
Packit |
6c4009 |
compilers without that support we do use setjmp. */
|
|
Packit |
6c4009 |
struct pthread_unwind_buf unwind_buf;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int not_first_call;
|
|
Packit |
6c4009 |
not_first_call = setjmp ((struct __jmp_buf_tag *) unwind_buf.cancel_jmp_buf);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* No previous handlers. NB: This must be done after setjmp since the
|
|
Packit |
6c4009 |
private space in the unwind jump buffer may overlap space used by
|
|
Packit |
6c4009 |
setjmp to store extra architecture-specific information which is
|
|
Packit |
6c4009 |
never used by the cancellation-specific __libc_unwind_longjmp.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The private space is allowed to overlap because the unwinder never
|
|
Packit |
6c4009 |
has to return through any of the jumped-to call frames, and thus
|
|
Packit |
6c4009 |
only a minimum amount of saved data need be stored, and for example,
|
|
Packit |
6c4009 |
need not include the process signal mask information. This is all
|
|
Packit |
6c4009 |
an optimization to reduce stack usage when pushing cancellation
|
|
Packit |
6c4009 |
handlers. */
|
|
Packit |
6c4009 |
unwind_buf.priv.data.prev = NULL;
|
|
Packit |
6c4009 |
unwind_buf.priv.data.cleanup = NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__glibc_likely (! not_first_call))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Store the new cleanup handler info. */
|
|
Packit |
6c4009 |
THREAD_SETMEM (pd, cleanup_jmp_buf, &unwind_buf);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We are either in (a) or (b), and in either case we either own
|
|
Packit |
6c4009 |
PD already (2) or are about to own PD (1), and so our only
|
|
Packit |
6c4009 |
restriction would be that we can't free PD until we know we
|
|
Packit |
6c4009 |
have ownership (see CONCURRENCY NOTES above). */
|
|
Packit |
6c4009 |
if (__glibc_unlikely (pd->stopped_start))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int oldtype = CANCEL_ASYNC ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the lock the parent locked to force synchronization. */
|
|
Packit |
6c4009 |
lll_lock (pd->lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We have ownership of PD now. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* And give it up right away. */
|
|
Packit |
6c4009 |
lll_unlock (pd->lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
CANCEL_RESET (oldtype);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
LIBC_PROBE (pthread_start, 3, (pthread_t) pd, pd->start_routine, pd->arg);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Run the code the user provided. */
|
|
Packit |
6c4009 |
void *ret;
|
|
Packit |
6c4009 |
if (pd->c11)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* The function pointer of the c11 thread start is cast to an incorrect
|
|
Packit |
6c4009 |
type on __pthread_create_2_1 call, however it is casted back to correct
|
|
Packit |
6c4009 |
one so the call behavior is well-defined (it is assumed that pointers
|
|
Packit |
6c4009 |
to void are able to represent all values of int. */
|
|
Packit |
6c4009 |
int (*start)(void*) = (int (*) (void*)) pd->start_routine;
|
|
Packit |
6c4009 |
ret = (void*) (uintptr_t) start (pd->arg);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
ret = pd->start_routine (pd->arg);
|
|
Packit |
6c4009 |
THREAD_SETMEM (pd, result, ret);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Call destructors for the thread_local TLS variables. */
|
|
Packit |
6c4009 |
#ifndef SHARED
|
|
Packit |
6c4009 |
if (&__call_tls_dtors != NULL)
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
__call_tls_dtors ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Run the destructor for the thread-local data. */
|
|
Packit |
6c4009 |
__nptl_deallocate_tsd ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Clean up any state libc stored in thread-local variables. */
|
|
Packit |
6c4009 |
__libc_thread_freeres ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* If this is the last thread we terminate the process now. We
|
|
Packit |
6c4009 |
do not notify the debugger, it might just irritate it if there
|
|
Packit |
6c4009 |
is no thread left. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely (atomic_decrement_and_test (&__nptl_nthreads)))
|
|
Packit |
6c4009 |
/* This was the last thread. */
|
|
Packit |
6c4009 |
exit (0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Report the death of the thread if this is wanted. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely (pd->report_events))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* See whether TD_DEATH is in any of the mask. */
|
|
Packit |
6c4009 |
const int idx = __td_eventword (TD_DEATH);
|
|
Packit |
6c4009 |
const uint32_t mask = __td_eventmask (TD_DEATH);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if ((mask & (__nptl_threads_events.event_bits[idx]
|
|
Packit |
6c4009 |
| pd->eventbuf.eventmask.event_bits[idx])) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Yep, we have to signal the death. Add the descriptor to
|
|
Packit |
6c4009 |
the list but only if it is not already on it. */
|
|
Packit |
6c4009 |
if (pd->nextevent == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
pd->eventbuf.eventnum = TD_DEATH;
|
|
Packit |
6c4009 |
pd->eventbuf.eventdata = pd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
pd->nextevent = __nptl_last_event;
|
|
Packit |
6c4009 |
while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
|
|
Packit |
6c4009 |
pd, pd->nextevent));
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now call the function which signals the event. See
|
|
Packit |
6c4009 |
CONCURRENCY NOTES for the nptl_db interface comments. */
|
|
Packit |
6c4009 |
__nptl_death_event ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The thread is exiting now. Don't set this bit until after we've hit
|
|
Packit |
6c4009 |
the event-reporting breakpoint, so that td_thr_get_info on us while at
|
|
Packit |
6c4009 |
the breakpoint reports TD_THR_RUN state rather than TD_THR_ZOMBIE. */
|
|
Packit |
6c4009 |
atomic_bit_set (&pd->cancelhandling, EXITING_BIT);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef __ASSUME_SET_ROBUST_LIST
|
|
Packit |
6c4009 |
/* If this thread has any robust mutexes locked, handle them now. */
|
|
Packit |
6c4009 |
# if __PTHREAD_MUTEX_HAVE_PREV
|
|
Packit |
6c4009 |
void *robust = pd->robust_head.list;
|
|
Packit |
6c4009 |
# else
|
|
Packit |
6c4009 |
__pthread_slist_t *robust = pd->robust_list.__next;
|
|
Packit |
6c4009 |
# endif
|
|
Packit |
6c4009 |
/* We let the kernel do the notification if it is able to do so.
|
|
Packit |
6c4009 |
If we have to do it here there for sure are no PI mutexes involved
|
|
Packit |
6c4009 |
since the kernel support for them is even more recent. */
|
|
Packit |
6c4009 |
if (__set_robust_list_avail < 0
|
|
Packit |
6c4009 |
&& __builtin_expect (robust != (void *) &pd->robust_head, 0))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct __pthread_mutex_s *this = (struct __pthread_mutex_s *)
|
|
Packit |
6c4009 |
((char *) robust - offsetof (struct __pthread_mutex_s,
|
|
Packit |
6c4009 |
__list.__next));
|
|
Packit |
6c4009 |
robust = *((void **) robust);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# if __PTHREAD_MUTEX_HAVE_PREV
|
|
Packit |
6c4009 |
this->__list.__prev = NULL;
|
|
Packit |
6c4009 |
# endif
|
|
Packit |
6c4009 |
this->__list.__next = NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
atomic_or (&this->__lock, FUTEX_OWNER_DIED);
|
|
Packit |
6c4009 |
futex_wake ((unsigned int *) &this->__lock, 1,
|
|
Packit |
6c4009 |
/* XYZ */ FUTEX_SHARED);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
while (robust != (void *) &pd->robust_head);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
advise_stack_range (pd->stackblock, pd->stackblock_size, (uintptr_t) pd,
|
|
Packit |
6c4009 |
pd->guardsize);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* If the thread is detached free the TCB. */
|
|
Packit |
6c4009 |
if (IS_DETACHED (pd))
|
|
Packit |
6c4009 |
/* Free the TCB. */
|
|
Packit |
6c4009 |
__free_tcb (pd);
|
|
Packit |
6c4009 |
else if (__glibc_unlikely (pd->cancelhandling & SETXID_BITMASK))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Some other thread might call any of the setXid functions and expect
|
|
Packit |
6c4009 |
us to reply. In this case wait until we did that. */
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
/* XXX This differs from the typical futex_wait_simple pattern in that
|
|
Packit |
6c4009 |
the futex_wait condition (setxid_futex) is different from the
|
|
Packit |
6c4009 |
condition used in the surrounding loop (cancelhandling). We need
|
|
Packit |
6c4009 |
to check and document why this is correct. */
|
|
Packit |
6c4009 |
futex_wait_simple (&pd->setxid_futex, 0, FUTEX_PRIVATE);
|
|
Packit |
6c4009 |
while (pd->cancelhandling & SETXID_BITMASK);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Reset the value so that the stack can be reused. */
|
|
Packit |
6c4009 |
pd->setxid_futex = 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We cannot call '_exit' here. '_exit' will terminate the process.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The 'exit' implementation in the kernel will signal when the
|
|
Packit |
6c4009 |
process is really dead since 'clone' got passed the CLONE_CHILD_CLEARTID
|
|
Packit |
6c4009 |
flag. The 'tid' field in the TCB will be set to zero.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The exit code is zero since in case all threads exit by calling
|
|
Packit |
6c4009 |
'pthread_exit' the exit status must be 0 (zero). */
|
|
Packit |
6c4009 |
__exit_thread ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* NOTREACHED */
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Return true iff obliged to report TD_CREATE events. */
|
|
Packit |
6c4009 |
static bool
|
|
Packit |
6c4009 |
report_thread_creation (struct pthread *pd)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (__glibc_unlikely (THREAD_GETMEM (THREAD_SELF, report_events)))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* The parent thread is supposed to report events.
|
|
Packit |
6c4009 |
Check whether the TD_CREATE event is needed, too. */
|
|
Packit |
6c4009 |
const size_t idx = __td_eventword (TD_CREATE);
|
|
Packit |
6c4009 |
const uint32_t mask = __td_eventmask (TD_CREATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return ((mask & (__nptl_threads_events.event_bits[idx]
|
|
Packit |
6c4009 |
| pd->eventbuf.eventmask.event_bits[idx])) != 0);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return false;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
__pthread_create_2_1 (pthread_t *newthread, const pthread_attr_t *attr,
|
|
Packit |
6c4009 |
void *(*start_routine) (void *), void *arg)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
STACK_VARIABLES;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
const struct pthread_attr *iattr = (struct pthread_attr *) attr;
|
|
Packit |
6c4009 |
struct pthread_attr default_attr;
|
|
Packit |
6c4009 |
bool free_cpuset = false;
|
|
Packit |
6c4009 |
bool c11 = (attr == ATTR_C11_THREAD);
|
|
Packit |
6c4009 |
if (iattr == NULL || c11)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
lll_lock (__default_pthread_attr_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
default_attr = __default_pthread_attr;
|
|
Packit |
6c4009 |
size_t cpusetsize = default_attr.cpusetsize;
|
|
Packit |
6c4009 |
if (cpusetsize > 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
cpu_set_t *cpuset;
|
|
Packit |
6c4009 |
if (__glibc_likely (__libc_use_alloca (cpusetsize)))
|
|
Packit |
6c4009 |
cpuset = __alloca (cpusetsize);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
cpuset = malloc (cpusetsize);
|
|
Packit |
6c4009 |
if (cpuset == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
return ENOMEM;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
free_cpuset = true;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
memcpy (cpuset, default_attr.cpuset, cpusetsize);
|
|
Packit |
6c4009 |
default_attr.cpuset = cpuset;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
lll_unlock (__default_pthread_attr_lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
iattr = &default_attr;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct pthread *pd = NULL;
|
|
Packit |
6c4009 |
int err = ALLOCATE_STACK (iattr, &pd;;
|
|
Packit |
6c4009 |
int retval = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__glibc_unlikely (err != 0))
|
|
Packit |
6c4009 |
/* Something went wrong. Maybe a parameter of the attributes is
|
|
Packit |
6c4009 |
invalid or we could not allocate memory. Note we have to
|
|
Packit |
6c4009 |
translate error codes. */
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
retval = err == ENOMEM ? EAGAIN : err;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Initialize the TCB. All initializations with zero should be
|
|
Packit |
6c4009 |
performed in 'get_cached_stack'. This way we avoid doing this if
|
|
Packit |
6c4009 |
the stack freshly allocated with 'mmap'. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#if TLS_TCB_AT_TP
|
|
Packit |
6c4009 |
/* Reference to the TCB itself. */
|
|
Packit |
6c4009 |
pd->header.self = pd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Self-reference for TLS. */
|
|
Packit |
6c4009 |
pd->header.tcb = pd;
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Store the address of the start routine and the parameter. Since
|
|
Packit |
6c4009 |
we do not start the function directly the stillborn thread will
|
|
Packit |
6c4009 |
get the information from its thread descriptor. */
|
|
Packit |
6c4009 |
pd->start_routine = start_routine;
|
|
Packit |
6c4009 |
pd->arg = arg;
|
|
Packit |
6c4009 |
pd->c11 = c11;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Copy the thread attribute flags. */
|
|
Packit |
6c4009 |
struct pthread *self = THREAD_SELF;
|
|
Packit |
6c4009 |
pd->flags = ((iattr->flags & ~(ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
|
|
Packit |
6c4009 |
| (self->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Initialize the field for the ID of the thread which is waiting
|
|
Packit |
6c4009 |
for us. This is a self-reference in case the thread is created
|
|
Packit |
6c4009 |
detached. */
|
|
Packit |
6c4009 |
pd->joinid = iattr->flags & ATTR_FLAG_DETACHSTATE ? pd : NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The debug events are inherited from the parent. */
|
|
Packit |
6c4009 |
pd->eventbuf = self->eventbuf;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Copy the parent's scheduling parameters. The flags will say what
|
|
Packit |
6c4009 |
is valid and what is not. */
|
|
Packit |
6c4009 |
pd->schedpolicy = self->schedpolicy;
|
|
Packit |
6c4009 |
pd->schedparam = self->schedparam;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Copy the stack guard canary. */
|
|
Packit |
6c4009 |
#ifdef THREAD_COPY_STACK_GUARD
|
|
Packit |
6c4009 |
THREAD_COPY_STACK_GUARD (pd);
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Copy the pointer guard value. */
|
|
Packit |
6c4009 |
#ifdef THREAD_COPY_POINTER_GUARD
|
|
Packit |
6c4009 |
THREAD_COPY_POINTER_GUARD (pd);
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Setup tcbhead. */
|
|
Packit |
6c4009 |
tls_setup_tcbhead (pd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Verify the sysinfo bits were copied in allocate_stack if needed. */
|
|
Packit |
6c4009 |
#ifdef NEED_DL_SYSINFO
|
|
Packit |
6c4009 |
CHECK_THREAD_SYSINFO (pd);
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Inform start_thread (above) about cancellation state that might
|
|
Packit |
6c4009 |
translate into inherited signal state. */
|
|
Packit |
6c4009 |
pd->parent_cancelhandling = THREAD_GETMEM (THREAD_SELF, cancelhandling);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Determine scheduling parameters for the thread. */
|
|
Packit |
6c4009 |
if (__builtin_expect ((iattr->flags & ATTR_FLAG_NOTINHERITSCHED) != 0, 0)
|
|
Packit |
6c4009 |
&& (iattr->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET)) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Use the scheduling parameters the user provided. */
|
|
Packit |
6c4009 |
if (iattr->flags & ATTR_FLAG_POLICY_SET)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
pd->schedpolicy = iattr->schedpolicy;
|
|
Packit |
6c4009 |
pd->flags |= ATTR_FLAG_POLICY_SET;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (iattr->flags & ATTR_FLAG_SCHED_SET)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* The values were validated in pthread_attr_setschedparam. */
|
|
Packit |
6c4009 |
pd->schedparam = iattr->schedparam;
|
|
Packit |
6c4009 |
pd->flags |= ATTR_FLAG_SCHED_SET;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if ((pd->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
|
|
Packit |
6c4009 |
!= (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
|
|
Packit |
6c4009 |
collect_default_sched (pd);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__glibc_unlikely (__nptl_nthreads == 1))
|
|
Packit |
6c4009 |
_IO_enable_locks ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Pass the descriptor to the caller. */
|
|
Packit |
6c4009 |
*newthread = (pthread_t) pd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
LIBC_PROBE (pthread_create, 4, newthread, attr, start_routine, arg);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* One more thread. We cannot have the thread do this itself, since it
|
|
Packit |
6c4009 |
might exist but not have been scheduled yet by the time we've returned
|
|
Packit |
6c4009 |
and need to check the value to behave correctly. We must do it before
|
|
Packit |
6c4009 |
creating the thread, in case it does get scheduled first and then
|
|
Packit |
6c4009 |
might mistakenly think it was the only thread. In the failure case,
|
|
Packit |
6c4009 |
we momentarily store a false value; this doesn't matter because there
|
|
Packit |
6c4009 |
is no kosher thing a signal handler interrupting us right here can do
|
|
Packit |
6c4009 |
that cares whether the thread count is correct. */
|
|
Packit |
6c4009 |
atomic_increment (&__nptl_nthreads);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Our local value of stopped_start and thread_ran can be accessed at
|
|
Packit |
6c4009 |
any time. The PD->stopped_start may only be accessed if we have
|
|
Packit |
6c4009 |
ownership of PD (see CONCURRENCY NOTES above). */
|
|
Packit |
6c4009 |
bool stopped_start = false; bool thread_ran = false;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Start the thread. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely (report_thread_creation (pd)))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
stopped_start = true;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We always create the thread stopped at startup so we can
|
|
Packit |
6c4009 |
notify the debugger. */
|
|
Packit |
6c4009 |
retval = create_thread (pd, iattr, &stopped_start,
|
|
Packit |
6c4009 |
STACK_VARIABLES_ARGS, &thread_ran);
|
|
Packit |
6c4009 |
if (retval == 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* We retain ownership of PD until (a) (see CONCURRENCY NOTES
|
|
Packit |
6c4009 |
above). */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Assert stopped_start is true in both our local copy and the
|
|
Packit |
6c4009 |
PD copy. */
|
|
Packit |
6c4009 |
assert (stopped_start);
|
|
Packit |
6c4009 |
assert (pd->stopped_start);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now fill in the information about the new thread in
|
|
Packit |
6c4009 |
the newly created thread's data structure. We cannot let
|
|
Packit |
6c4009 |
the new thread do this since we don't know whether it was
|
|
Packit |
6c4009 |
already scheduled when we send the event. */
|
|
Packit |
6c4009 |
pd->eventbuf.eventnum = TD_CREATE;
|
|
Packit |
6c4009 |
pd->eventbuf.eventdata = pd;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Enqueue the descriptor. */
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
pd->nextevent = __nptl_last_event;
|
|
Packit |
6c4009 |
while (atomic_compare_and_exchange_bool_acq (&__nptl_last_event,
|
|
Packit |
6c4009 |
pd, pd->nextevent)
|
|
Packit |
6c4009 |
!= 0);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Now call the function which signals the event. See
|
|
Packit |
6c4009 |
CONCURRENCY NOTES for the nptl_db interface comments. */
|
|
Packit |
6c4009 |
__nptl_create_event ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
retval = create_thread (pd, iattr, &stopped_start,
|
|
Packit |
6c4009 |
STACK_VARIABLES_ARGS, &thread_ran);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (__glibc_unlikely (retval != 0))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (thread_ran)
|
|
Packit |
6c4009 |
/* State (c) or (d) and we may not have PD ownership (see
|
|
Packit |
6c4009 |
CONCURRENCY NOTES above). We can assert that STOPPED_START
|
|
Packit |
6c4009 |
must have been true because thread creation didn't fail, but
|
|
Packit |
6c4009 |
thread attribute setting did. */
|
|
Packit |
6c4009 |
/* See bug 19511 which explains why doing nothing here is a
|
|
Packit |
6c4009 |
resource leak for a joinable thread. */
|
|
Packit |
6c4009 |
assert (stopped_start);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* State (e) and we have ownership of PD (see CONCURRENCY
|
|
Packit |
6c4009 |
NOTES above). */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Oops, we lied for a second. */
|
|
Packit |
6c4009 |
atomic_decrement (&__nptl_nthreads);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Perhaps a thread wants to change the IDs and is waiting for this
|
|
Packit |
6c4009 |
stillborn thread. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely (atomic_exchange_acq (&pd->setxid_futex, 0)
|
|
Packit |
6c4009 |
== -2))
|
|
Packit |
6c4009 |
futex_wake (&pd->setxid_futex, 1, FUTEX_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Free the resources. */
|
|
Packit |
6c4009 |
__deallocate_stack (pd);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We have to translate error codes. */
|
|
Packit |
6c4009 |
if (retval == ENOMEM)
|
|
Packit |
6c4009 |
retval = EAGAIN;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* We don't know if we have PD ownership. Once we check the local
|
|
Packit |
6c4009 |
stopped_start we'll know if we're in state (a) or (b) (see
|
|
Packit |
6c4009 |
CONCURRENCY NOTES above). */
|
|
Packit |
6c4009 |
if (stopped_start)
|
|
Packit |
6c4009 |
/* State (a), we own PD. The thread blocked on this lock either
|
|
Packit |
6c4009 |
because we're doing TD_CREATE event reporting, or for some
|
|
Packit |
6c4009 |
other reason that create_thread chose. Now let it run
|
|
Packit |
6c4009 |
free. */
|
|
Packit |
6c4009 |
lll_unlock (pd->lock, LLL_PRIVATE);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We now have for sure more than one thread. The main thread might
|
|
Packit |
6c4009 |
not yet have the flag set. No need to set the global variable
|
|
Packit |
6c4009 |
again if this is what we use. */
|
|
Packit |
6c4009 |
THREAD_SETMEM (THREAD_SELF, header.multiple_threads, 1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
out:
|
|
Packit |
6c4009 |
if (__glibc_unlikely (free_cpuset))
|
|
Packit |
6c4009 |
free (default_attr.cpuset);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return retval;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
versioned_symbol (libpthread, __pthread_create_2_1, pthread_create, GLIBC_2_1);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_1)
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
__pthread_create_2_0 (pthread_t *newthread, const pthread_attr_t *attr,
|
|
Packit |
6c4009 |
void *(*start_routine) (void *), void *arg)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* The ATTR attribute is not really of type `pthread_attr_t *'. It has
|
|
Packit |
6c4009 |
the old size and access to the new members might crash the program.
|
|
Packit |
6c4009 |
We convert the struct now. */
|
|
Packit |
6c4009 |
struct pthread_attr new_attr;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (attr != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct pthread_attr *iattr = (struct pthread_attr *) attr;
|
|
Packit |
6c4009 |
size_t ps = __getpagesize ();
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Copy values from the user-provided attributes. */
|
|
Packit |
6c4009 |
new_attr.schedparam = iattr->schedparam;
|
|
Packit |
6c4009 |
new_attr.schedpolicy = iattr->schedpolicy;
|
|
Packit |
6c4009 |
new_attr.flags = iattr->flags;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Fill in default values for the fields not present in the old
|
|
Packit |
6c4009 |
implementation. */
|
|
Packit |
6c4009 |
new_attr.guardsize = ps;
|
|
Packit |
6c4009 |
new_attr.stackaddr = NULL;
|
|
Packit |
6c4009 |
new_attr.stacksize = 0;
|
|
Packit |
6c4009 |
new_attr.cpuset = NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* We will pass this value on to the real implementation. */
|
|
Packit |
6c4009 |
attr = (pthread_attr_t *) &new_attr;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return __pthread_create_2_1 (newthread, attr, start_routine, arg);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
compat_symbol (libpthread, __pthread_create_2_0, pthread_create,
|
|
Packit |
6c4009 |
GLIBC_2_0);
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Information for libthread_db. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include "../nptl_db/db_info.c"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* If pthread_create is present, libgcc_eh.a and libsupc++.a expects some other POSIX thread
|
|
Packit |
6c4009 |
functions to be present as well. */
|
|
Packit |
6c4009 |
PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_lock)
|
|
Packit |
6c4009 |
PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_trylock)
|
|
Packit |
6c4009 |
PTHREAD_STATIC_FN_REQUIRE (__pthread_mutex_unlock)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
PTHREAD_STATIC_FN_REQUIRE (__pthread_once)
|
|
Packit |
6c4009 |
PTHREAD_STATIC_FN_REQUIRE (__pthread_cancel)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
PTHREAD_STATIC_FN_REQUIRE (__pthread_key_create)
|
|
Packit |
6c4009 |
PTHREAD_STATIC_FN_REQUIRE (__pthread_key_delete)
|
|
Packit |
6c4009 |
PTHREAD_STATIC_FN_REQUIRE (__pthread_setspecific)
|
|
Packit |
6c4009 |
PTHREAD_STATIC_FN_REQUIRE (__pthread_getspecific)
|