2017-01-25 19:08:51 +00:00
|
|
|
/* Common definition for pthread_{timed,try}join{_np}.
|
2021-01-02 19:32:25 +00:00
|
|
|
Copyright (C) 2017-2021 Free Software Foundation, Inc.
|
2017-01-25 19:08:51 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
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
|
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/>. */
|
2017-01-25 19:08:51 +00:00
|
|
|
|
|
|
|
#include "pthreadP.h"
|
|
|
|
#include <atomic.h>
|
|
|
|
#include <stap-probe.h>
|
2019-10-24 14:20:56 +00:00
|
|
|
#include <time.h>
|
2020-07-20 13:50:12 +00:00
|
|
|
#include <futex-internal.h>
|
2017-01-25 19:08:51 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup (void *arg)
|
|
|
|
{
|
|
|
|
/* If we already changed the waiter ID, reset it. The call cannot
|
|
|
|
fail for any reason but the thread not having done that yet so
|
|
|
|
there is no reason for a loop. */
|
|
|
|
struct pthread *self = THREAD_SELF;
|
|
|
|
atomic_compare_exchange_weak_acquire (&arg, &self, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-31 13:03:21 +00:00
|
|
|
__pthread_clockjoin_ex (pthread_t threadid, void **thread_return,
|
2020-07-20 13:50:12 +00:00
|
|
|
clockid_t clockid,
|
|
|
|
const struct __timespec64 *abstime, bool block)
|
2017-01-25 19:08:51 +00:00
|
|
|
{
|
|
|
|
struct pthread *pd = (struct pthread *) threadid;
|
|
|
|
|
|
|
|
/* Make sure the descriptor is valid. */
|
|
|
|
if (INVALID_NOT_TERMINATED_TD_P (pd))
|
|
|
|
/* Not a valid thread handle. */
|
|
|
|
return ESRCH;
|
|
|
|
|
|
|
|
/* Is the thread joinable?. */
|
|
|
|
if (IS_DETACHED (pd))
|
|
|
|
/* We cannot wait for the thread. */
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
struct pthread *self = THREAD_SELF;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
LIBC_PROBE (pthread_join, 1, threadid);
|
|
|
|
|
|
|
|
if ((pd == self
|
|
|
|
|| (self->joinid == pd
|
|
|
|
&& (pd->cancelhandling
|
2021-05-25 17:31:30 +00:00
|
|
|
& (CANCELED_BITMASK | EXITING_BITMASK
|
2017-01-25 19:08:51 +00:00
|
|
|
| TERMINATED_BITMASK)) == 0))
|
2020-03-31 18:43:25 +00:00
|
|
|
&& !(self->cancelstate == PTHREAD_CANCEL_ENABLE
|
|
|
|
&& (pd->cancelhandling & (CANCELED_BITMASK | EXITING_BITMASK
|
|
|
|
| TERMINATED_BITMASK))
|
|
|
|
== CANCELED_BITMASK))
|
2017-01-25 19:08:51 +00:00
|
|
|
/* This is a deadlock situation. The threads are waiting for each
|
|
|
|
other to finish. Note that this is a "may" error. To be 100%
|
|
|
|
sure we catch this error we would have to lock the data
|
|
|
|
structures but it is not necessary. In the unlikely case that
|
|
|
|
two threads are really caught in this situation they will
|
|
|
|
deadlock. It is the programmer's problem to figure this
|
|
|
|
out. */
|
|
|
|
return EDEADLK;
|
|
|
|
|
|
|
|
/* Wait for the thread to finish. If it is already locked something
|
|
|
|
is wrong. There can only be one waiter. */
|
|
|
|
else if (__glibc_unlikely (atomic_compare_exchange_weak_acquire (&pd->joinid,
|
|
|
|
&self,
|
|
|
|
NULL)))
|
|
|
|
/* There is already somebody waiting for the thread. */
|
|
|
|
return EINVAL;
|
|
|
|
|
nptl: Reinstate pthread_timedjoin_np as a cancellation point (BZ#24215)
Patch ce7eb0e90315 ("nptl: Cleanup cancellation macros") changed the
join sequence for internal common __pthread_timedjoin_ex to use the
new macro lll_wait_tid. The idea was this macro would issue the
cancellable futex operation depending whether the timeout is used or
not. However if a timeout is used, __lll_timedwait_tid is called and
it is not a cancellable entrypoint.
This patch fixes it by simplifying the code in various ways:
- Instead of adding the cancellation handling on __lll_timedwait_tid,
it moves the generic implementation to pthread_join_common.c (called
now timedwait_tid with some fixes to use the correct type for pid).
- The llvm_wait_tid macro is removed, along with its replication on
x86_64, i686, and sparc arch-specific lowlevellock.h.
- sparc32 __lll_timedwait_tid is also removed, since the code is similar
to generic one.
- x86_64 and i386 provides arch-specific __lll_timedwait_tid which is
also removed since they are similar in functionality to generic C code
and there is no indication it is better than compiler generated code.
New tests, tst-join8 and tst-join9, are provided to check if
pthread_timedjoin_np acts as a cancellation point.
Checked on x86_64-linux-gnu, i686-linux-gnu, sparcv9-linux-gnu, and
aarch64-linux-gnu.
[BZ #24215]
* nptl/Makefile (lpthread-routines): Remove lll_timedwait_tid.
(tests): Add tst-join8 tst-join9.
* nptl/lll_timedwait_tid.c: Remove file.
* sysdeps/sparc/sparc32/lll_timedwait_tid.c: Likewise.
* sysdeps/unix/sysv/linux/i386/lll_timedwait_tid.c: Likewise.
* sysdeps/sysv/linux/x86_64/lll_timedwait_tid.c: Likewise.
* nptl/pthread_join_common.c (timedwait_tid): New function.
(__pthread_timedjoin_ex): Act as cancellation entrypoint is block
is set.
* nptl/tst-join5.c (thread_join): New function.
(tf1, tf2, do_test): Use libsupport and add pthread_timedjoin_np
check.
* nptl/tst-join8.c: New file.
* nptl/tst-join9.c: Likewise.
* sysdeps/nptl/lowlevellock-futex.h (lll_futex_wait_cancel,
lll_futex_timed_wait_cancel): Add generic macros.
* sysdeps/nptl/lowlevellock.h (__lll_timedwait_tid, lll_wait_tid):
Remove definitions.
* sysdeps/unix/sysv/linux/i386/lowlevellock.h: Likewise.
* sysdeps/unix/sysv/linux/sparc/lowlevellock.h: Likewise.
* sysdeps/unix/sysv/linux/x86_64/lowlevellock.h: Likewise.
* sysdeps/sparc/sparc32/lowlevellock.c (__lll_timedwait_tid):
Remove function.
* sysdeps/unix/sysv/linux/i386/lowlevellock.S (__lll_timedwait_tid):
Likewise.
* sysdeps/unix/sysv/linux/x86_64/lowlevellock.S: Likewise.
* sysdeps/unix/sysv/linux/lowlevellock-futex.h
(lll_futex_timed_wait_cancel): New macro.
2019-02-12 14:36:46 +00:00
|
|
|
/* BLOCK waits either indefinitely or based on an absolute time. POSIX also
|
|
|
|
states a cancellation point shall occur for pthread_join, and we use the
|
2019-10-31 13:03:21 +00:00
|
|
|
same rationale for posix_timedjoin_np. Both clockwait_tid and the futex
|
nptl: Reinstate pthread_timedjoin_np as a cancellation point (BZ#24215)
Patch ce7eb0e90315 ("nptl: Cleanup cancellation macros") changed the
join sequence for internal common __pthread_timedjoin_ex to use the
new macro lll_wait_tid. The idea was this macro would issue the
cancellable futex operation depending whether the timeout is used or
not. However if a timeout is used, __lll_timedwait_tid is called and
it is not a cancellable entrypoint.
This patch fixes it by simplifying the code in various ways:
- Instead of adding the cancellation handling on __lll_timedwait_tid,
it moves the generic implementation to pthread_join_common.c (called
now timedwait_tid with some fixes to use the correct type for pid).
- The llvm_wait_tid macro is removed, along with its replication on
x86_64, i686, and sparc arch-specific lowlevellock.h.
- sparc32 __lll_timedwait_tid is also removed, since the code is similar
to generic one.
- x86_64 and i386 provides arch-specific __lll_timedwait_tid which is
also removed since they are similar in functionality to generic C code
and there is no indication it is better than compiler generated code.
New tests, tst-join8 and tst-join9, are provided to check if
pthread_timedjoin_np acts as a cancellation point.
Checked on x86_64-linux-gnu, i686-linux-gnu, sparcv9-linux-gnu, and
aarch64-linux-gnu.
[BZ #24215]
* nptl/Makefile (lpthread-routines): Remove lll_timedwait_tid.
(tests): Add tst-join8 tst-join9.
* nptl/lll_timedwait_tid.c: Remove file.
* sysdeps/sparc/sparc32/lll_timedwait_tid.c: Likewise.
* sysdeps/unix/sysv/linux/i386/lll_timedwait_tid.c: Likewise.
* sysdeps/sysv/linux/x86_64/lll_timedwait_tid.c: Likewise.
* nptl/pthread_join_common.c (timedwait_tid): New function.
(__pthread_timedjoin_ex): Act as cancellation entrypoint is block
is set.
* nptl/tst-join5.c (thread_join): New function.
(tf1, tf2, do_test): Use libsupport and add pthread_timedjoin_np
check.
* nptl/tst-join8.c: New file.
* nptl/tst-join9.c: Likewise.
* sysdeps/nptl/lowlevellock-futex.h (lll_futex_wait_cancel,
lll_futex_timed_wait_cancel): Add generic macros.
* sysdeps/nptl/lowlevellock.h (__lll_timedwait_tid, lll_wait_tid):
Remove definitions.
* sysdeps/unix/sysv/linux/i386/lowlevellock.h: Likewise.
* sysdeps/unix/sysv/linux/sparc/lowlevellock.h: Likewise.
* sysdeps/unix/sysv/linux/x86_64/lowlevellock.h: Likewise.
* sysdeps/sparc/sparc32/lowlevellock.c (__lll_timedwait_tid):
Remove function.
* sysdeps/unix/sysv/linux/i386/lowlevellock.S (__lll_timedwait_tid):
Likewise.
* sysdeps/unix/sysv/linux/x86_64/lowlevellock.S: Likewise.
* sysdeps/unix/sysv/linux/lowlevellock-futex.h
(lll_futex_timed_wait_cancel): New macro.
2019-02-12 14:36:46 +00:00
|
|
|
call use the cancellable variant. */
|
2017-01-25 19:08:51 +00:00
|
|
|
if (block)
|
|
|
|
{
|
|
|
|
/* During the wait we change to asynchronous cancellation. If we
|
|
|
|
are cancelled the thread we are waiting for must be marked as
|
|
|
|
un-wait-ed for again. */
|
|
|
|
pthread_cleanup_push (cleanup, &pd->joinid);
|
|
|
|
|
2020-11-23 18:28:57 +00:00
|
|
|
/* We need acquire MO here so that we synchronize with the
|
|
|
|
kernel's store to 0 when the clone terminates. (see above) */
|
|
|
|
pid_t tid;
|
|
|
|
while ((tid = atomic_load_acquire (&pd->tid)) != 0)
|
|
|
|
{
|
|
|
|
/* The kernel notifies a process which uses CLONE_CHILD_CLEARTID via
|
|
|
|
futex wake-up when the clone terminates. The memory location
|
|
|
|
contains the thread ID while the clone is running and is reset to
|
|
|
|
zero by the kernel afterwards. The kernel up to version 3.16.3
|
|
|
|
does not use the private futex operations for futex wake-up when
|
|
|
|
the clone terminates. */
|
|
|
|
int ret = __futex_abstimed_wait_cancelable64 (
|
|
|
|
(unsigned int *) &pd->tid, tid, clockid, abstime, LLL_SHARED);
|
|
|
|
if (ret == ETIMEDOUT || ret == EOVERFLOW)
|
|
|
|
{
|
|
|
|
result = ret;
|
|
|
|
break;
|
|
|
|
}
|
nptl: Reinstate pthread_timedjoin_np as a cancellation point (BZ#24215)
Patch ce7eb0e90315 ("nptl: Cleanup cancellation macros") changed the
join sequence for internal common __pthread_timedjoin_ex to use the
new macro lll_wait_tid. The idea was this macro would issue the
cancellable futex operation depending whether the timeout is used or
not. However if a timeout is used, __lll_timedwait_tid is called and
it is not a cancellable entrypoint.
This patch fixes it by simplifying the code in various ways:
- Instead of adding the cancellation handling on __lll_timedwait_tid,
it moves the generic implementation to pthread_join_common.c (called
now timedwait_tid with some fixes to use the correct type for pid).
- The llvm_wait_tid macro is removed, along with its replication on
x86_64, i686, and sparc arch-specific lowlevellock.h.
- sparc32 __lll_timedwait_tid is also removed, since the code is similar
to generic one.
- x86_64 and i386 provides arch-specific __lll_timedwait_tid which is
also removed since they are similar in functionality to generic C code
and there is no indication it is better than compiler generated code.
New tests, tst-join8 and tst-join9, are provided to check if
pthread_timedjoin_np acts as a cancellation point.
Checked on x86_64-linux-gnu, i686-linux-gnu, sparcv9-linux-gnu, and
aarch64-linux-gnu.
[BZ #24215]
* nptl/Makefile (lpthread-routines): Remove lll_timedwait_tid.
(tests): Add tst-join8 tst-join9.
* nptl/lll_timedwait_tid.c: Remove file.
* sysdeps/sparc/sparc32/lll_timedwait_tid.c: Likewise.
* sysdeps/unix/sysv/linux/i386/lll_timedwait_tid.c: Likewise.
* sysdeps/sysv/linux/x86_64/lll_timedwait_tid.c: Likewise.
* nptl/pthread_join_common.c (timedwait_tid): New function.
(__pthread_timedjoin_ex): Act as cancellation entrypoint is block
is set.
* nptl/tst-join5.c (thread_join): New function.
(tf1, tf2, do_test): Use libsupport and add pthread_timedjoin_np
check.
* nptl/tst-join8.c: New file.
* nptl/tst-join9.c: Likewise.
* sysdeps/nptl/lowlevellock-futex.h (lll_futex_wait_cancel,
lll_futex_timed_wait_cancel): Add generic macros.
* sysdeps/nptl/lowlevellock.h (__lll_timedwait_tid, lll_wait_tid):
Remove definitions.
* sysdeps/unix/sysv/linux/i386/lowlevellock.h: Likewise.
* sysdeps/unix/sysv/linux/sparc/lowlevellock.h: Likewise.
* sysdeps/unix/sysv/linux/x86_64/lowlevellock.h: Likewise.
* sysdeps/sparc/sparc32/lowlevellock.c (__lll_timedwait_tid):
Remove function.
* sysdeps/unix/sysv/linux/i386/lowlevellock.S (__lll_timedwait_tid):
Likewise.
* sysdeps/unix/sysv/linux/x86_64/lowlevellock.S: Likewise.
* sysdeps/unix/sysv/linux/lowlevellock-futex.h
(lll_futex_timed_wait_cancel): New macro.
2019-02-12 14:36:46 +00:00
|
|
|
}
|
2017-01-25 19:08:51 +00:00
|
|
|
|
|
|
|
pthread_cleanup_pop (0);
|
|
|
|
}
|
|
|
|
|
2019-02-15 18:09:00 +00:00
|
|
|
void *pd_result = pd->result;
|
2017-01-25 19:08:51 +00:00
|
|
|
if (__glibc_likely (result == 0))
|
|
|
|
{
|
|
|
|
/* We mark the thread as terminated and as joined. */
|
|
|
|
pd->tid = -1;
|
|
|
|
|
|
|
|
/* Store the return value if the caller is interested. */
|
|
|
|
if (thread_return != NULL)
|
2019-02-15 18:09:00 +00:00
|
|
|
*thread_return = pd_result;
|
2017-01-25 19:08:51 +00:00
|
|
|
|
|
|
|
/* Free the TCB. */
|
2021-05-11 09:08:00 +00:00
|
|
|
__nptl_free_tcb (pd);
|
2017-01-25 19:08:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
pd->joinid = NULL;
|
|
|
|
|
2019-02-15 18:09:00 +00:00
|
|
|
LIBC_PROBE (pthread_join_ret, 3, threadid, result, pd_result);
|
2017-01-25 19:08:51 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|