2014-07-15 22:23:06 +00:00
|
|
|
/* Low-level locking access to futex facilities. Linux version.
|
2019-01-01 00:11:28 +00:00
|
|
|
Copyright (C) 2005-2019 Free Software Foundation, Inc.
|
2014-07-15 22:23:06 +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
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
#ifndef _LOWLEVELLOCK_FUTEX_H
|
|
|
|
#define _LOWLEVELLOCK_FUTEX_H 1
|
|
|
|
|
2014-12-17 17:55:19 +00:00
|
|
|
#ifndef __ASSEMBLER__
|
2014-07-15 22:23:06 +00:00
|
|
|
#include <sysdep.h>
|
Add single-thread.h header
This patch move the single-thread syscall optimization defintions from
syscall-cancel.h to new header file single-thread.h and also move the
cancellation definitions from pthreadP.h to syscall-cancel.h.
The idea is just simplify the inclusion of both syscall-cancel.h and
single-thread.h (without the requirement of including all pthreadP.h
defintions).
No semantic changes expected, checked on a build for all major ABIs.
* nptl/pthreadP.h (CANCEL_ASYNC, CANCEL_RESET, LIBC_CANCEL_ASYNC,
LIBC_CANCEL_RESET, __libc_enable_asynccancel,
__libc_disable_asynccancel, __librt_enable_asynccancel,
__libc_disable_asynccancel, __librt_enable_asynccancel,
__librt_disable_asynccancel): Move to ...
* sysdeps/unix/sysv/linux/sysdep-cancel.h: ... here.
(SINGLE_THREAD_P, RTLD_SINGLE_THREAD_P): Move to ...
* sysdeps/unix/sysv/linux/single-thread.h: ... here.
* sysdeps/generic/single-thread.h: New file.
* sysdeps/unix/sysdep.h: Include single-thread.h.
* sysdeps/unix/sysv/linux/futex-internal.h: Include sysdep-cancel.h.
* sysdeps/unix/sysv/linux/lowlevellock-futex.h: Likewise.
2019-02-21 17:33:55 +00:00
|
|
|
#include <sysdep-cancel.h>
|
2014-07-15 22:23:06 +00:00
|
|
|
#include <kernel-features.h>
|
2014-12-17 17:55:19 +00:00
|
|
|
#endif
|
2014-07-15 22:23:06 +00:00
|
|
|
|
|
|
|
#define FUTEX_WAIT 0
|
|
|
|
#define FUTEX_WAKE 1
|
|
|
|
#define FUTEX_REQUEUE 3
|
|
|
|
#define FUTEX_CMP_REQUEUE 4
|
|
|
|
#define FUTEX_WAKE_OP 5
|
|
|
|
#define FUTEX_OP_CLEAR_WAKE_IF_GT_ONE ((4 << 24) | 1)
|
|
|
|
#define FUTEX_LOCK_PI 6
|
|
|
|
#define FUTEX_UNLOCK_PI 7
|
|
|
|
#define FUTEX_TRYLOCK_PI 8
|
|
|
|
#define FUTEX_WAIT_BITSET 9
|
|
|
|
#define FUTEX_WAKE_BITSET 10
|
|
|
|
#define FUTEX_WAIT_REQUEUE_PI 11
|
|
|
|
#define FUTEX_CMP_REQUEUE_PI 12
|
|
|
|
#define FUTEX_PRIVATE_FLAG 128
|
|
|
|
#define FUTEX_CLOCK_REALTIME 256
|
|
|
|
|
|
|
|
#define FUTEX_BITSET_MATCH_ANY 0xffffffff
|
|
|
|
|
|
|
|
/* Values for 'private' parameter of locking macros. Yes, the
|
|
|
|
definition seems to be backwards. But it is not. The bit will be
|
|
|
|
reversed before passing to the system call. */
|
|
|
|
#define LLL_PRIVATE 0
|
|
|
|
#define LLL_SHARED FUTEX_PRIVATE_FLAG
|
|
|
|
|
2014-12-17 17:55:19 +00:00
|
|
|
#ifndef __ASSEMBLER__
|
2014-07-15 22:23:06 +00:00
|
|
|
|
2014-11-24 09:33:45 +00:00
|
|
|
#if IS_IN (libc) || IS_IN (rtld)
|
2014-07-15 22:23:06 +00:00
|
|
|
/* In libc.so or ld.so all futexes are private. */
|
2018-05-17 11:24:24 +00:00
|
|
|
# define __lll_private_flag(fl, private) \
|
2015-07-14 19:58:34 +00:00
|
|
|
({ \
|
|
|
|
/* Prevent warnings in callers of this macro. */ \
|
|
|
|
int __lll_private_flag_priv __attribute__ ((unused)); \
|
|
|
|
__lll_private_flag_priv = (private); \
|
|
|
|
((fl) | FUTEX_PRIVATE_FLAG); \
|
|
|
|
})
|
2014-07-15 22:23:06 +00:00
|
|
|
#else
|
2018-05-17 11:24:24 +00:00
|
|
|
# define __lll_private_flag(fl, private) \
|
2014-07-15 22:23:06 +00:00
|
|
|
(((fl) | FUTEX_PRIVATE_FLAG) ^ (private))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define lll_futex_syscall(nargs, futexp, op, ...) \
|
|
|
|
({ \
|
|
|
|
INTERNAL_SYSCALL_DECL (__err); \
|
|
|
|
long int __ret = INTERNAL_SYSCALL (futex, __err, nargs, futexp, op, \
|
|
|
|
__VA_ARGS__); \
|
|
|
|
(__glibc_unlikely (INTERNAL_SYSCALL_ERROR_P (__ret, __err)) \
|
|
|
|
? -INTERNAL_SYSCALL_ERRNO (__ret, __err) : 0); \
|
|
|
|
})
|
|
|
|
|
|
|
|
#define lll_futex_wait(futexp, val, private) \
|
|
|
|
lll_futex_timed_wait (futexp, val, NULL, private)
|
|
|
|
|
|
|
|
#define lll_futex_timed_wait(futexp, val, timeout, private) \
|
|
|
|
lll_futex_syscall (4, futexp, \
|
|
|
|
__lll_private_flag (FUTEX_WAIT, private), \
|
|
|
|
val, timeout)
|
|
|
|
|
2019-06-21 14:53:40 +00:00
|
|
|
/* Verify whether the supplied clockid is supported by
|
|
|
|
lll_futex_clock_wait_bitset. */
|
|
|
|
#define lll_futex_supported_clockid(clockid) \
|
|
|
|
((clockid) == CLOCK_REALTIME || (clockid) == CLOCK_MONOTONIC)
|
|
|
|
|
|
|
|
/* The kernel currently only supports CLOCK_MONOTONIC or
|
|
|
|
CLOCK_REALTIME timeouts for FUTEX_WAIT_BITSET. We could attempt to
|
|
|
|
convert others here but currently do not. */
|
|
|
|
#define lll_futex_clock_wait_bitset(futexp, val, clockid, timeout, private) \
|
|
|
|
({ \
|
|
|
|
long int __ret; \
|
|
|
|
if (lll_futex_supported_clockid (clockid)) \
|
|
|
|
{ \
|
|
|
|
const unsigned int clockbit = \
|
|
|
|
(clockid == CLOCK_REALTIME) ? FUTEX_CLOCK_REALTIME : 0; \
|
|
|
|
const int op = \
|
|
|
|
__lll_private_flag (FUTEX_WAIT_BITSET | clockbit, private); \
|
|
|
|
\
|
|
|
|
__ret = lll_futex_syscall (6, futexp, op, val, \
|
|
|
|
timeout, NULL /* Unused. */, \
|
|
|
|
FUTEX_BITSET_MATCH_ANY); \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
__ret = -EINVAL; \
|
|
|
|
__ret; \
|
|
|
|
})
|
2014-07-15 22:23:06 +00:00
|
|
|
|
|
|
|
#define lll_futex_wake(futexp, nr, private) \
|
|
|
|
lll_futex_syscall (4, futexp, \
|
|
|
|
__lll_private_flag (FUTEX_WAKE, private), nr, 0)
|
|
|
|
|
|
|
|
/* Returns non-zero if error happened, zero if success. */
|
|
|
|
#define lll_futex_requeue(futexp, nr_wake, nr_move, mutex, val, private) \
|
|
|
|
lll_futex_syscall (6, futexp, \
|
|
|
|
__lll_private_flag (FUTEX_CMP_REQUEUE, private), \
|
|
|
|
nr_wake, nr_move, mutex, val)
|
|
|
|
|
|
|
|
/* Returns non-zero if error happened, zero if success. */
|
|
|
|
#define lll_futex_wake_unlock(futexp, nr_wake, nr_wake2, futexp2, private) \
|
|
|
|
lll_futex_syscall (6, futexp, \
|
|
|
|
__lll_private_flag (FUTEX_WAKE_OP, private), \
|
|
|
|
nr_wake, nr_wake2, futexp2, \
|
|
|
|
FUTEX_OP_CLEAR_WAKE_IF_GT_ONE)
|
|
|
|
|
|
|
|
/* Priority Inheritance support. */
|
|
|
|
#define lll_futex_wait_requeue_pi(futexp, val, mutex, private) \
|
|
|
|
lll_futex_timed_wait_requeue_pi (futexp, val, NULL, 0, mutex, private)
|
|
|
|
|
|
|
|
#define lll_futex_timed_wait_requeue_pi(futexp, val, timeout, clockbit, \
|
|
|
|
mutex, private) \
|
|
|
|
lll_futex_syscall (5, futexp, \
|
|
|
|
__lll_private_flag (FUTEX_WAIT_REQUEUE_PI \
|
|
|
|
| (clockbit), private), \
|
|
|
|
val, timeout, mutex)
|
|
|
|
|
|
|
|
|
|
|
|
#define lll_futex_cmp_requeue_pi(futexp, nr_wake, nr_move, mutex, \
|
|
|
|
val, private) \
|
|
|
|
lll_futex_syscall (6, futexp, \
|
|
|
|
__lll_private_flag (FUTEX_CMP_REQUEUE_PI, \
|
|
|
|
private), \
|
|
|
|
nr_wake, nr_move, mutex, val)
|
|
|
|
|
2018-05-10 20:24:56 +00:00
|
|
|
|
|
|
|
/* Cancellable futex macros. */
|
|
|
|
#define lll_futex_wait_cancel(futexp, val, private) \
|
|
|
|
({ \
|
|
|
|
int __oldtype = CANCEL_ASYNC (); \
|
|
|
|
long int __err = lll_futex_wait (futexp, val, LLL_SHARED); \
|
|
|
|
CANCEL_RESET (__oldtype); \
|
|
|
|
__err; \
|
|
|
|
})
|
|
|
|
|
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
|
|
|
#define lll_futex_timed_wait_cancel(futexp, val, timeout, private) \
|
|
|
|
({ \
|
|
|
|
int __oldtype = CANCEL_ASYNC (); \
|
|
|
|
long int __err = lll_futex_timed_wait (futexp, val, timeout, private); \
|
|
|
|
CANCEL_RESET (__oldtype); \
|
|
|
|
__err; \
|
|
|
|
})
|
2018-05-10 20:24:56 +00:00
|
|
|
|
2014-12-17 17:55:19 +00:00
|
|
|
#endif /* !__ASSEMBLER__ */
|
2014-07-15 22:23:06 +00:00
|
|
|
|
|
|
|
#endif /* lowlevellock-futex.h */
|