2018-04-01 23:43:22 +00:00
|
|
|
/* Thread termination.
|
2023-01-06 21:08:04 +00:00
|
|
|
Copyright (C) 2000-2023 Free Software Foundation, Inc.
|
2018-04-01 23:43:22 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2018-04-02 14:37:36 +00:00
|
|
|
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.
|
2018-04-01 23:43:22 +00:00
|
|
|
|
|
|
|
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
|
2018-04-02 14:37:36 +00:00
|
|
|
Lesser General Public License for more details.
|
2018-04-01 23:43:22 +00:00
|
|
|
|
2018-04-02 14:37:36 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with the GNU C Library; if not, see
|
Prefer https to http for gnu.org and fsf.org URLs
Also, change sources.redhat.com to sourceware.org.
This patch was automatically generated by running the following shell
script, which uses GNU sed, and which avoids modifying files imported
from upstream:
sed -ri '
s,(http|ftp)(://(.*\.)?(gnu|fsf|sourceware)\.org($|[^.]|\.[^a-z])),https\2,g
s,(http|ftp)(://(.*\.)?)sources\.redhat\.com($|[^.]|\.[^a-z]),https\2sourceware.org\4,g
' \
$(find $(git ls-files) -prune -type f \
! -name '*.po' \
! -name 'ChangeLog*' \
! -path COPYING ! -path COPYING.LIB \
! -path manual/fdl-1.3.texi ! -path manual/lgpl-2.1.texi \
! -path manual/texinfo.tex ! -path scripts/config.guess \
! -path scripts/config.sub ! -path scripts/install-sh \
! -path scripts/mkinstalldirs ! -path scripts/move-if-change \
! -path INSTALL ! -path locale/programs/charmap-kw.h \
! -path po/libc.pot ! -path sysdeps/gnu/errlist.c \
! '(' -name configure \
-execdir test -f configure.ac -o -f configure.in ';' ')' \
! '(' -name preconfigure \
-execdir test -f preconfigure.ac ';' ')' \
-print)
and then by running 'make dist-prepare' to regenerate files built
from the altered files, and then executing the following to cleanup:
chmod a+x sysdeps/unix/sysv/linux/riscv/configure
# Omit irrelevant whitespace and comment-only changes,
# perhaps from a slightly-different Autoconf version.
git checkout -f \
sysdeps/csky/configure \
sysdeps/hppa/configure \
sysdeps/riscv/configure \
sysdeps/unix/sysv/linux/csky/configure
# Omit changes that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/powerpc/powerpc64/ppc-mcount.S: trailing lines
git checkout -f \
sysdeps/powerpc/powerpc64/ppc-mcount.S \
sysdeps/unix/sysv/linux/s390/s390-64/syscall.S
# Omit change that caused a pre-commit check to fail like this:
# remote: *** error: sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S: last line does not end in newline
git checkout -f sysdeps/sparc/sparc64/multiarch/memcpy-ultra3.S
2019-09-07 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2018-04-01 23:43:22 +00:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <pt-internal.h>
|
2018-04-03 00:56:35 +00:00
|
|
|
#include <pthreadP.h>
|
2018-04-01 23:43:22 +00:00
|
|
|
|
|
|
|
#include <atomic.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* Terminate the current thread and make STATUS available to any
|
|
|
|
thread that might join it. */
|
|
|
|
void
|
|
|
|
__pthread_exit (void *status)
|
|
|
|
{
|
|
|
|
struct __pthread *self = _pthread_self ();
|
|
|
|
struct __pthread_cancelation_handler **handlers;
|
|
|
|
int oldstate;
|
|
|
|
|
|
|
|
/* Run any cancelation handlers. According to POSIX, the
|
|
|
|
cancellation cleanup handlers should be called with cancellation
|
|
|
|
disabled. */
|
hurd: Avoid local PLTs in libpthread.
* htl/cthreads-compat.c (__cthread_detach): Call __pthread_detach
instead of pthread_detach.
(__cthread_fork): Call __pthread_create instead of pthread_create.
(__cthread_keycreate): Call __pthread_key_create instead of
pthread_key_create.
(__cthread_getspecific): Call __pthread_getspecific instead of
pthread_getspecific.
(__cthread_setspecific): Call __pthread_setspecific instead of
pthread_setspecific.
* htl/pt-alloc.c (__pthread_alloc): Call __pthread_mutex_lock and
__pthread_mutex_unlock instead of pthread_mutex_lock and
pthread_mutex_unlock.
* htl/pt-cleanup.c (__pthread_get_cleanup_stack): Rename to
___pthread_get_cleanup_stack.
(__pthread_get_cleanup_stack): New strong alias.
* htl/pt-create.c: Include <pthreadP.h>.
(entry_point): Call __pthread_exit instead of pthread_exit.
(pthread_create): Rename to __pthread_create.
(pthread_create): New strong alias.
* htl/pt-detach.c (pthread_detach): Rename to __pthread_detach.
(pthread_detach): New strong alias.
(__pthread_detach): Call __pthread_cond_broadcast instead of
pthread_cond_broadcast.
* htl/pt-exit.c (__pthread_exit): Call __pthread_setcancelstate
instead of pthread_setcancelstate.
* htl/pt-testcancel.c: Include <pthreadP.h>.
(pthread_testcancel): Call __pthread_exit instead of pthread_exit.
* sysdeps/htl/pt-attr-getstack.c: Include <pthreadP.h>
(__pthread_attr_getstack): Call __pthread_attr_getstackaddr and
__pthread_attr_getstacksize instead of pthread_attr_getstackaddr and
pthread_attr_getstacksize.
* sysdeps/htl/pt-attr-getstackaddr.c (pthread_attr_getstackaddr):
Rename to __pthread_attr_getstackaddr.
(pthread_attr_getstackaddr): New strong alias.
* sysdeps/htl/pt-attr-getstacksize.c (pthread_attr_getstacksize):
Rename to __pthread_attr_getstacksize.
(pthread_attr_getstacksize): New strong alias.
* sysdeps/htl/pt-attr-setstack.c: Include <pthreadP.h>.
(pthread_attr_setstack): Rename to __pthread_attr_setstack.
(pthread_attr_setstack): New strong alias.
(__pthread_attr_setstack): Call __pthread_attr_getstacksize,
__pthread_attr_setstacksize and __pthread_attr_setstackaddr instead of
pthread_attr_getstacksize, pthread_attr_setstacksize and
pthread_attr_setstackaddr.
* sysdeps/htl/pt-attr-setstackaddr.c (pthread_attr_setstackaddr):
Rename to __pthread_attr_setstackaddr.
(pthread_attr_setstackaddr): New strong alias.
* sysdeps/htl/pt-attr-setstacksize.c (pthread_attr_setstacksize):
Rename to __pthread_attr_setstacksize.
(pthread_attr_setstacksize): New strong alias.
* sysdeps/htl/pt-cond-timedwait.c: Include <pthreadP.h>.
(__pthread_cond_timedwait_internal): Use __pthread_exit instead of
pthread_exit.
* sysdeps/htl/pt-key-create.c: Include <pthreadP.h>.
(__pthread_key_create): New hidden def.
* sysdeps/htl/pt-key.h: Include <pthreadP.h>.
* sysdeps/htl/pthreadP.h (_pthread_mutex_init,
__pthread_cond_broadcast, __pthread_create, __pthread_detach,
__pthread_exit, __pthread_key_create, __pthread_getspecific,
__pthread_setspecific, __pthread_setcancelstate,
__pthread_attr_getstackaddr, __pthread_attr_setstackaddr,
__pthread_attr_getstacksize, __pthread_attr_setstacksize,
__pthread_attr_setstack, ___pthread_get_cleanup_stack): New
declarations.
(__pthread_key_create, _pthread_mutex_init): New hidden declarations.
* sysdeps/mach/hurd/htl/pt-attr-setstackaddr.c
(pthread_attr_setstackaddr): Rename to __pthread_attr_setstackaddr.
(pthread_attr_setstackaddr): New strong alias.
* sysdeps/mach/hurd/htl/pt-attr-setstacksize.c
(pthread_attr_setstacksize): Rename to __pthread_attr_setstacksize.
(pthread_attr_setstacksize): New strong alias.
* sysdeps/mach/hurd/htl/pt-docancel.c: Include <pthreadP.h>.
(call_exit): Call __pthread_exit instead of pthread_exit.
* sysdeps/mach/hurd/htl/pt-mutex-init.c: Include <pthreadP.h>.
(_pthread_mutex_init): New hidden definition.
* sysdeps/mach/hurd/htl/pt-sysdep.c: Include <pthreadP.h>.
(_init_routine): Call __pthread_attr_init and __pthread_attr_setstack
instead of pthread_attr_init and pthread_attr_setstack.
2018-04-02 18:08:37 +00:00
|
|
|
__pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &oldstate);
|
2018-04-01 23:43:22 +00:00
|
|
|
|
2020-02-09 23:13:24 +00:00
|
|
|
for (handlers = __pthread_get_cleanup_stack ();
|
2018-04-01 23:43:22 +00:00
|
|
|
*handlers != NULL;
|
|
|
|
*handlers = (*handlers)->__next)
|
|
|
|
(*handlers)->__handler ((*handlers)->__arg);
|
|
|
|
|
hurd: Avoid local PLTs in libpthread.
* htl/cthreads-compat.c (__cthread_detach): Call __pthread_detach
instead of pthread_detach.
(__cthread_fork): Call __pthread_create instead of pthread_create.
(__cthread_keycreate): Call __pthread_key_create instead of
pthread_key_create.
(__cthread_getspecific): Call __pthread_getspecific instead of
pthread_getspecific.
(__cthread_setspecific): Call __pthread_setspecific instead of
pthread_setspecific.
* htl/pt-alloc.c (__pthread_alloc): Call __pthread_mutex_lock and
__pthread_mutex_unlock instead of pthread_mutex_lock and
pthread_mutex_unlock.
* htl/pt-cleanup.c (__pthread_get_cleanup_stack): Rename to
___pthread_get_cleanup_stack.
(__pthread_get_cleanup_stack): New strong alias.
* htl/pt-create.c: Include <pthreadP.h>.
(entry_point): Call __pthread_exit instead of pthread_exit.
(pthread_create): Rename to __pthread_create.
(pthread_create): New strong alias.
* htl/pt-detach.c (pthread_detach): Rename to __pthread_detach.
(pthread_detach): New strong alias.
(__pthread_detach): Call __pthread_cond_broadcast instead of
pthread_cond_broadcast.
* htl/pt-exit.c (__pthread_exit): Call __pthread_setcancelstate
instead of pthread_setcancelstate.
* htl/pt-testcancel.c: Include <pthreadP.h>.
(pthread_testcancel): Call __pthread_exit instead of pthread_exit.
* sysdeps/htl/pt-attr-getstack.c: Include <pthreadP.h>
(__pthread_attr_getstack): Call __pthread_attr_getstackaddr and
__pthread_attr_getstacksize instead of pthread_attr_getstackaddr and
pthread_attr_getstacksize.
* sysdeps/htl/pt-attr-getstackaddr.c (pthread_attr_getstackaddr):
Rename to __pthread_attr_getstackaddr.
(pthread_attr_getstackaddr): New strong alias.
* sysdeps/htl/pt-attr-getstacksize.c (pthread_attr_getstacksize):
Rename to __pthread_attr_getstacksize.
(pthread_attr_getstacksize): New strong alias.
* sysdeps/htl/pt-attr-setstack.c: Include <pthreadP.h>.
(pthread_attr_setstack): Rename to __pthread_attr_setstack.
(pthread_attr_setstack): New strong alias.
(__pthread_attr_setstack): Call __pthread_attr_getstacksize,
__pthread_attr_setstacksize and __pthread_attr_setstackaddr instead of
pthread_attr_getstacksize, pthread_attr_setstacksize and
pthread_attr_setstackaddr.
* sysdeps/htl/pt-attr-setstackaddr.c (pthread_attr_setstackaddr):
Rename to __pthread_attr_setstackaddr.
(pthread_attr_setstackaddr): New strong alias.
* sysdeps/htl/pt-attr-setstacksize.c (pthread_attr_setstacksize):
Rename to __pthread_attr_setstacksize.
(pthread_attr_setstacksize): New strong alias.
* sysdeps/htl/pt-cond-timedwait.c: Include <pthreadP.h>.
(__pthread_cond_timedwait_internal): Use __pthread_exit instead of
pthread_exit.
* sysdeps/htl/pt-key-create.c: Include <pthreadP.h>.
(__pthread_key_create): New hidden def.
* sysdeps/htl/pt-key.h: Include <pthreadP.h>.
* sysdeps/htl/pthreadP.h (_pthread_mutex_init,
__pthread_cond_broadcast, __pthread_create, __pthread_detach,
__pthread_exit, __pthread_key_create, __pthread_getspecific,
__pthread_setspecific, __pthread_setcancelstate,
__pthread_attr_getstackaddr, __pthread_attr_setstackaddr,
__pthread_attr_getstacksize, __pthread_attr_setstacksize,
__pthread_attr_setstack, ___pthread_get_cleanup_stack): New
declarations.
(__pthread_key_create, _pthread_mutex_init): New hidden declarations.
* sysdeps/mach/hurd/htl/pt-attr-setstackaddr.c
(pthread_attr_setstackaddr): Rename to __pthread_attr_setstackaddr.
(pthread_attr_setstackaddr): New strong alias.
* sysdeps/mach/hurd/htl/pt-attr-setstacksize.c
(pthread_attr_setstacksize): Rename to __pthread_attr_setstacksize.
(pthread_attr_setstacksize): New strong alias.
* sysdeps/mach/hurd/htl/pt-docancel.c: Include <pthreadP.h>.
(call_exit): Call __pthread_exit instead of pthread_exit.
* sysdeps/mach/hurd/htl/pt-mutex-init.c: Include <pthreadP.h>.
(_pthread_mutex_init): New hidden definition.
* sysdeps/mach/hurd/htl/pt-sysdep.c: Include <pthreadP.h>.
(_init_routine): Call __pthread_attr_init and __pthread_attr_setstack
instead of pthread_attr_init and pthread_attr_setstack.
2018-04-02 18:08:37 +00:00
|
|
|
__pthread_setcancelstate (oldstate, &oldstate);
|
2018-04-01 23:43:22 +00:00
|
|
|
|
|
|
|
/* Decrease the number of threads. We use an atomic operation to
|
|
|
|
make sure that only the last thread calls `exit'. */
|
2022-09-22 14:40:37 +00:00
|
|
|
if (atomic_fetch_add_relaxed (&__pthread_total, -1) == 1)
|
2018-04-01 23:43:22 +00:00
|
|
|
/* We are the last thread. */
|
|
|
|
exit (0);
|
|
|
|
|
2022-02-14 00:39:35 +00:00
|
|
|
/* Destroy any thread specific data. */
|
|
|
|
__pthread_destroy_specific (self);
|
|
|
|
|
2018-04-01 23:43:22 +00:00
|
|
|
/* Note that after this point the process can be terminated at any
|
|
|
|
point if another thread calls `pthread_exit' and happens to be
|
|
|
|
the last thread. */
|
|
|
|
|
|
|
|
__pthread_mutex_lock (&self->state_lock);
|
|
|
|
|
|
|
|
if (self->cancel_state == PTHREAD_CANCEL_ENABLE && self->cancel_pending)
|
|
|
|
status = PTHREAD_CANCELED;
|
|
|
|
|
|
|
|
switch (self->state)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
assert (!"Consistency error: unexpected self->state");
|
|
|
|
abort ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PTHREAD_DETACHED:
|
|
|
|
__pthread_mutex_unlock (&self->state_lock);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PTHREAD_JOINABLE:
|
|
|
|
/* We need to stay around for a while since another thread
|
|
|
|
might want to join us. */
|
|
|
|
self->state = PTHREAD_EXITED;
|
|
|
|
|
|
|
|
/* We need to remember the exit status. A thread joining us
|
|
|
|
might ask for it. */
|
|
|
|
self->status = status;
|
|
|
|
|
|
|
|
/* Broadcast the condition. This will wake up threads that are
|
|
|
|
waiting to join us. */
|
|
|
|
__pthread_cond_broadcast (&self->state_cond);
|
|
|
|
__pthread_mutex_unlock (&self->state_lock);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy any signal state. */
|
|
|
|
__pthread_sigstate_destroy (self);
|
|
|
|
|
|
|
|
/* Self terminating requires TLS, so defer the release of the TCB until
|
|
|
|
the thread structure is reused. */
|
|
|
|
|
|
|
|
/* Release kernel resources, including the kernel thread and the stack,
|
|
|
|
and drop the self reference. */
|
|
|
|
__pthread_thread_terminate (self);
|
|
|
|
|
|
|
|
/* NOTREACHED */
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
2020-02-10 23:45:23 +00:00
|
|
|
weak_alias (__pthread_exit, pthread_exit);
|