2017-01-25 19:08:51 +00:00
|
|
|
/* Common definition for pthread_{timed,try}join{_np}.
|
2019-01-01 00:11:28 +00:00
|
|
|
Copyright (C) 2017-2019 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>
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/* 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. */
|
|
|
|
static int
|
|
|
|
timedwait_tid (pid_t *tidp, const struct timespec *abstime)
|
|
|
|
{
|
|
|
|
pid_t tid;
|
|
|
|
|
2019-10-24 14:20:56 +00:00
|
|
|
if (! valid_nanoseconds (abstime->tv_nsec))
|
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
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
/* Repeat until thread terminated. */
|
|
|
|
while ((tid = *tidp) != 0)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
struct timespec rt;
|
|
|
|
|
|
|
|
/* Get the current time. */
|
|
|
|
__gettimeofday (&tv, NULL);
|
|
|
|
|
|
|
|
/* Compute relative timeout. */
|
|
|
|
rt.tv_sec = abstime->tv_sec - tv.tv_sec;
|
|
|
|
rt.tv_nsec = abstime->tv_nsec - tv.tv_usec * 1000;
|
|
|
|
if (rt.tv_nsec < 0)
|
|
|
|
{
|
|
|
|
rt.tv_nsec += 1000000000;
|
|
|
|
--rt.tv_sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Already timed out? */
|
|
|
|
if (rt.tv_sec < 0)
|
|
|
|
return ETIMEDOUT;
|
|
|
|
|
|
|
|
/* If *tidp == tid, wait until thread terminates or the wait times out.
|
|
|
|
The kernel up to version 3.16.3 does not use the private futex
|
|
|
|
operations for futex wake-up when the clone terminates. */
|
|
|
|
if (lll_futex_timed_wait_cancel (tidp, tid, &rt, LLL_SHARED)
|
|
|
|
== -ETIMEDOUT)
|
|
|
|
return ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:08:51 +00:00
|
|
|
int
|
|
|
|
__pthread_timedjoin_ex (pthread_t threadid, void **thread_return,
|
|
|
|
const struct timespec *abstime, bool block)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
& (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
|
|
|
|
| TERMINATED_BITMASK)) == 0))
|
|
|
|
&& !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling))
|
|
|
|
/* 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
|
|
|
|
same rationale for posix_timedjoin_np. Both timedwait_tid and the futex
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
if (abstime != NULL)
|
|
|
|
result = timedwait_tid (&pd->tid, abstime);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pid_t tid;
|
|
|
|
/* We need acquire MO here so that we synchronize with the
|
|
|
|
kernel's store to 0 when the clone terminates. (see above) */
|
|
|
|
while ((tid = atomic_load_acquire (&pd->tid)) != 0)
|
|
|
|
lll_futex_wait_cancel (&pd->tid, tid, LLL_SHARED);
|
|
|
|
}
|
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. */
|
|
|
|
__free_tcb (pd);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
hidden_def (__pthread_timedjoin_ex)
|