2014-12-04 21:12:23 +08:00
|
|
|
/* futex operations for glibc-internal use. Stub version; do not include
|
|
|
|
this file directly.
|
2020-01-01 08:14:33 +08:00
|
|
|
Copyright (C) 2014-2020 Free Software Foundation, Inc.
|
2014-12-04 21:12:23 +08: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 13:40:42 +08:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2014-12-04 21:12:23 +08:00
|
|
|
|
|
|
|
#ifndef STUB_FUTEX_INTERNAL_H
|
|
|
|
#define STUB_FUTEX_INTERNAL_H
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
Narrowing the visibility of libc-internal.h even further.
posix/wordexp-test.c used libc-internal.h for PTR_ALIGN_DOWN; similar
to what was done with libc-diag.h, I have split the definitions of
cast_to_integer, ALIGN_UP, ALIGN_DOWN, PTR_ALIGN_UP, and PTR_ALIGN_DOWN
to a new header, libc-pointer-arith.h.
It then occurred to me that the remaining declarations in libc-internal.h
are mostly to do with early initialization, and probably most of the
files including it, even in the core code, don't need it anymore. Indeed,
only 19 files actually need what remains of libc-internal.h. 23 others
need libc-diag.h instead, and 12 need libc-pointer-arith.h instead.
No file needs more than one of them, and 16 don't need any of them!
So, with this patch, libc-internal.h stops including libc-diag.h as
well as losing the pointer arithmetic macros, and all including files
are adjusted.
* include/libc-pointer-arith.h: New file. Define
cast_to_integer, ALIGN_UP, ALIGN_DOWN, PTR_ALIGN_UP, and
PTR_ALIGN_DOWN here.
* include/libc-internal.h: Definitions of above macros
moved from here. Don't include libc-diag.h anymore either.
* posix/wordexp-test.c: Include stdint.h and libc-pointer-arith.h.
Don't include libc-internal.h.
* debug/pcprofile.c, elf/dl-tunables.c, elf/soinit.c, io/openat.c
* io/openat64.c, misc/ptrace.c, nptl/pthread_clock_gettime.c
* nptl/pthread_clock_settime.c, nptl/pthread_cond_common.c
* string/strcoll_l.c, sysdeps/nacl/brk.c
* sysdeps/unix/clock_settime.c
* sysdeps/unix/sysv/linux/i386/get_clockfreq.c
* sysdeps/unix/sysv/linux/ia64/get_clockfreq.c
* sysdeps/unix/sysv/linux/powerpc/get_clockfreq.c
* sysdeps/unix/sysv/linux/sparc/sparc64/get_clockfreq.c:
Don't include libc-internal.h.
* elf/get-dynamic-info.h, iconv/loop.c
* iconvdata/iso-2022-cn-ext.c, locale/weight.h, locale/weightwc.h
* misc/reboot.c, nis/nis_table.c, nptl_db/thread_dbP.h
* nscd/connections.c, resolv/res_send.c, soft-fp/fmadf4.c
* soft-fp/fmasf4.c, soft-fp/fmatf4.c, stdio-common/vfscanf.c
* sysdeps/ieee754/dbl-64/e_lgamma_r.c
* sysdeps/ieee754/dbl-64/k_rem_pio2.c
* sysdeps/ieee754/flt-32/e_lgammaf_r.c
* sysdeps/ieee754/flt-32/k_rem_pio2f.c
* sysdeps/ieee754/ldbl-128/k_tanl.c
* sysdeps/ieee754/ldbl-128ibm/k_tanl.c
* sysdeps/ieee754/ldbl-96/e_lgammal_r.c
* sysdeps/ieee754/ldbl-96/k_tanl.c, sysdeps/nptl/futex-internal.h:
Include libc-diag.h instead of libc-internal.h.
* elf/dl-load.c, elf/dl-reloc.c, locale/programs/locarchive.c
* nptl/nptl-init.c, string/strcspn.c, string/strspn.c
* malloc/malloc.c, sysdeps/i386/nptl/tls.h
* sysdeps/nacl/dl-map-segments.h, sysdeps/x86_64/atomic-machine.h
* sysdeps/unix/sysv/linux/spawni.c
* sysdeps/x86_64/nptl/tls.h:
Include libc-pointer-arith.h instead of libc-internal.h.
* elf/get-dynamic-info.h, sysdeps/nacl/dl-map-segments.h
* sysdeps/x86_64/atomic-machine.h:
Add multiple include guard.
2017-02-27 09:17:52 +08:00
|
|
|
#include <libc-diag.h>
|
2014-12-04 21:12:23 +08:00
|
|
|
|
|
|
|
/* This file defines futex operations used internally in glibc. A futex
|
|
|
|
consists of the so-called futex word in userspace, which is of type
|
|
|
|
unsigned int and represents an application-specific condition, and kernel
|
|
|
|
state associated with this particular futex word (e.g., wait queues). The
|
|
|
|
futex operations we provide are wrappers for the futex syscalls and add
|
|
|
|
glibc-specific error checking of the syscall return value. We abort on
|
|
|
|
error codes that are caused by bugs in glibc or in the calling application,
|
|
|
|
or when an error code is not known. We return error codes that can arise
|
|
|
|
in correct executions to the caller. Each operation calls out exactly the
|
|
|
|
return values that callers need to handle.
|
|
|
|
|
|
|
|
The private flag must be either FUTEX_PRIVATE or FUTEX_SHARED.
|
|
|
|
FUTEX_PRIVATE is always supported, and the implementation can internally
|
|
|
|
use FUTEX_SHARED when FUTEX_PRIVATE is requested. FUTEX_SHARED is not
|
|
|
|
necessarily supported (use futex_supports_pshared to detect this).
|
|
|
|
|
|
|
|
We expect callers to only use these operations if futexes and the
|
|
|
|
specific futex operations being used are supported (e.g., FUTEX_SHARED).
|
|
|
|
|
|
|
|
Given that waking other threads waiting on a futex involves concurrent
|
|
|
|
accesses to the futex word, you must use atomic operations to access the
|
|
|
|
futex word.
|
|
|
|
|
|
|
|
Both absolute and relative timeouts can be used. An absolute timeout
|
2019-06-21 22:53:40 +08:00
|
|
|
expires when the given specific point in time on the specified clock
|
2014-12-04 21:12:23 +08:00
|
|
|
passes, or when it already has passed. A relative timeout expires when
|
2019-06-25 04:18:00 +08:00
|
|
|
the given duration of time on the CLOCK_MONOTONIC clock passes.
|
2014-12-04 21:12:23 +08:00
|
|
|
|
|
|
|
Due to POSIX requirements on when synchronization data structures such
|
|
|
|
as mutexes or semaphores can be destroyed and due to the futex design
|
|
|
|
having separate fast/slow paths for wake-ups, we need to consider that
|
|
|
|
futex_wake calls might effectively target a data structure that has been
|
|
|
|
destroyed and reused for another object, or unmapped; thus, some
|
|
|
|
errors or spurious wake-ups can happen in correct executions that would
|
|
|
|
not be possible in a program using just a single futex whose lifetime
|
|
|
|
does not end before the program terminates. For background, see:
|
|
|
|
https://sourceware.org/ml/libc-alpha/2014-04/msg00075.html
|
|
|
|
https://lkml.org/lkml/2014/11/27/472 */
|
|
|
|
|
|
|
|
/* Defined this way for interoperability with lowlevellock.
|
|
|
|
FUTEX_PRIVATE must be zero because the initializers for pthread_mutex_t,
|
|
|
|
pthread_rwlock_t, and pthread_cond_t initialize the respective field of
|
|
|
|
those structures to zero, and we want FUTEX_PRIVATE to be the default. */
|
|
|
|
#define FUTEX_PRIVATE LLL_PRIVATE
|
|
|
|
#define FUTEX_SHARED LLL_SHARED
|
|
|
|
#if FUTEX_PRIVATE != 0
|
|
|
|
# error FUTEX_PRIVATE must be equal to 0
|
|
|
|
#endif
|
|
|
|
|
2019-10-30 05:52:19 +08:00
|
|
|
/* Calls __libc_fatal with an error message. Convenience function for
|
|
|
|
concrete implementations of the futex interface. */
|
|
|
|
static __always_inline __attribute__ ((__noreturn__)) void
|
|
|
|
futex_fatal_error (void)
|
|
|
|
{
|
|
|
|
__libc_fatal ("The futex facility returned an unexpected error code.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The Linux kernel treats provides absolute timeouts based on the
|
|
|
|
CLOCK_REALTIME clock and relative timeouts measured against the
|
|
|
|
CLOCK_MONOTONIC clock.
|
|
|
|
|
|
|
|
We expect a Linux kernel version of 2.6.22 or more recent (since this
|
|
|
|
version, EINTR is not returned on spurious wake-ups anymore). */
|
|
|
|
|
2014-12-04 21:12:23 +08:00
|
|
|
/* Returns EINVAL if PSHARED is neither PTHREAD_PROCESS_PRIVATE nor
|
|
|
|
PTHREAD_PROCESS_SHARED; otherwise, returns 0 if PSHARED is supported, and
|
|
|
|
ENOTSUP if not. */
|
|
|
|
static __always_inline int
|
2019-10-30 05:52:19 +08:00
|
|
|
futex_supports_pshared (int pshared)
|
|
|
|
{
|
|
|
|
if (__glibc_likely (pshared == PTHREAD_PROCESS_PRIVATE))
|
|
|
|
return 0;
|
|
|
|
else if (pshared == PTHREAD_PROCESS_SHARED)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2014-12-04 21:12:23 +08:00
|
|
|
|
|
|
|
/* Atomically wrt other futex operations on the same futex, this blocks iff
|
|
|
|
the value *FUTEX_WORD matches the expected value. This is
|
|
|
|
semantically equivalent to:
|
|
|
|
l = <get lock associated with futex> (FUTEX_WORD);
|
|
|
|
wait_flag = <get wait_flag associated with futex> (FUTEX_WORD);
|
|
|
|
lock (l);
|
|
|
|
val = atomic_load_relaxed (FUTEX_WORD);
|
|
|
|
if (val != expected) { unlock (l); return EAGAIN; }
|
|
|
|
atomic_store_relaxed (wait_flag, true);
|
|
|
|
unlock (l);
|
|
|
|
// Now block; can time out in futex_time_wait (see below)
|
|
|
|
while (atomic_load_relaxed(wait_flag) && !<spurious wake-up>);
|
|
|
|
|
|
|
|
Note that no guarantee of a happens-before relation between a woken
|
|
|
|
futex_wait and a futex_wake is documented; however, this does not matter
|
|
|
|
in practice because we have to consider spurious wake-ups (see below),
|
|
|
|
and thus would not be able to reliably reason about which futex_wake woke
|
|
|
|
us.
|
|
|
|
|
|
|
|
Returns 0 if woken by a futex operation or spuriously. (Note that due to
|
|
|
|
the POSIX requirements mentioned above, we need to conservatively assume
|
|
|
|
that unrelated futex_wake operations could wake this futex; it is easiest
|
|
|
|
to just be prepared for spurious wake-ups.)
|
|
|
|
Returns EAGAIN if the futex word did not match the expected value.
|
|
|
|
Returns EINTR if waiting was interrupted by a signal.
|
|
|
|
|
|
|
|
Note that some previous code in glibc assumed the underlying futex
|
|
|
|
operation (e.g., syscall) to start with or include the equivalent of a
|
|
|
|
seq_cst fence; this allows one to avoid an explicit seq_cst fence before
|
|
|
|
a futex_wait call when synchronizing similar to Dekker synchronization.
|
|
|
|
However, we make no such guarantee here. */
|
|
|
|
static __always_inline int
|
2019-10-30 05:52:19 +08:00
|
|
|
futex_wait (unsigned int *futex_word, unsigned int expected, int private)
|
|
|
|
{
|
|
|
|
int err = lll_futex_timed_wait (futex_word, expected, NULL, private);
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -EAGAIN:
|
|
|
|
case -EINTR:
|
|
|
|
return -err;
|
|
|
|
|
|
|
|
case -ETIMEDOUT: /* Cannot have happened as we provided no timeout. */
|
|
|
|
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
|
|
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
|
|
being normalized. Must have been caused by a glibc or
|
|
|
|
application bug. */
|
|
|
|
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
|
|
/* No other errors are documented at this time. */
|
|
|
|
default:
|
|
|
|
futex_fatal_error ();
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 21:12:23 +08:00
|
|
|
|
|
|
|
/* Like futex_wait but does not provide any indication why we stopped waiting.
|
|
|
|
Thus, when this function returns, you have to always check FUTEX_WORD to
|
|
|
|
determine whether you need to continue waiting, and you cannot detect
|
|
|
|
whether the waiting was interrupted by a signal. Example use:
|
|
|
|
while (atomic_load_relaxed (&futex_word) == 23)
|
|
|
|
futex_wait_simple (&futex_word, 23, FUTEX_PRIVATE);
|
|
|
|
This is common enough to make providing this wrapper worthwhile. */
|
|
|
|
static __always_inline void
|
|
|
|
futex_wait_simple (unsigned int *futex_word, unsigned int expected,
|
|
|
|
int private)
|
|
|
|
{
|
|
|
|
ignore_value (futex_wait (futex_word, expected, private));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Like futex_wait but is a POSIX cancellation point. */
|
|
|
|
static __always_inline int
|
|
|
|
futex_wait_cancelable (unsigned int *futex_word, unsigned int expected,
|
2019-10-30 05:52:19 +08:00
|
|
|
int private)
|
|
|
|
{
|
|
|
|
int oldtype;
|
|
|
|
oldtype = __pthread_enable_asynccancel ();
|
|
|
|
int err = lll_futex_timed_wait (futex_word, expected, NULL, private);
|
|
|
|
__pthread_disable_asynccancel (oldtype);
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -EAGAIN:
|
|
|
|
case -EINTR:
|
|
|
|
return -err;
|
|
|
|
|
|
|
|
case -ETIMEDOUT: /* Cannot have happened as we provided no timeout. */
|
|
|
|
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
|
|
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
|
|
being normalized. Must have been caused by a glibc or
|
|
|
|
application bug. */
|
|
|
|
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
|
|
/* No other errors are documented at this time. */
|
|
|
|
default:
|
|
|
|
futex_fatal_error ();
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 21:12:23 +08:00
|
|
|
|
|
|
|
/* Like futex_wait, but will eventually time out (i.e., stop being
|
|
|
|
blocked) after the duration of time provided (i.e., RELTIME) has
|
|
|
|
passed. The caller must provide a normalized RELTIME. RELTIME can also
|
|
|
|
equal NULL, in which case this function behaves equivalent to futex_wait.
|
|
|
|
|
|
|
|
Returns the same values as futex_wait under those same conditions;
|
|
|
|
additionally, returns ETIMEDOUT if the timeout expired.
|
|
|
|
*/
|
|
|
|
static __always_inline int
|
|
|
|
futex_reltimed_wait (unsigned int* futex_word, unsigned int expected,
|
2019-10-30 05:52:19 +08:00
|
|
|
const struct timespec* reltime, int private)
|
|
|
|
{
|
|
|
|
int err = lll_futex_timed_wait (futex_word, expected, reltime, private);
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -EAGAIN:
|
|
|
|
case -EINTR:
|
|
|
|
case -ETIMEDOUT:
|
|
|
|
return -err;
|
|
|
|
|
|
|
|
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
|
|
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
|
|
being normalized. Must have been caused by a glibc or
|
|
|
|
application bug. */
|
|
|
|
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
|
|
/* No other errors are documented at this time. */
|
|
|
|
default:
|
|
|
|
futex_fatal_error ();
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 21:12:23 +08:00
|
|
|
|
|
|
|
/* Like futex_reltimed_wait but is a POSIX cancellation point. */
|
|
|
|
static __always_inline int
|
|
|
|
futex_reltimed_wait_cancelable (unsigned int* futex_word,
|
|
|
|
unsigned int expected,
|
2019-10-30 05:52:19 +08:00
|
|
|
const struct timespec* reltime, int private)
|
|
|
|
{
|
|
|
|
int oldtype;
|
|
|
|
oldtype = LIBC_CANCEL_ASYNC ();
|
|
|
|
int err = lll_futex_timed_wait (futex_word, expected, reltime, private);
|
|
|
|
LIBC_CANCEL_RESET (oldtype);
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -EAGAIN:
|
|
|
|
case -EINTR:
|
|
|
|
case -ETIMEDOUT:
|
|
|
|
return -err;
|
|
|
|
|
|
|
|
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
|
|
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
|
|
being normalized. Must have been caused by a glibc or
|
|
|
|
application bug. */
|
|
|
|
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
|
|
/* No other errors are documented at this time. */
|
|
|
|
default:
|
|
|
|
futex_fatal_error ();
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 21:12:23 +08:00
|
|
|
|
2019-06-21 22:53:40 +08:00
|
|
|
/* Check whether the specified clockid is supported by
|
|
|
|
futex_abstimed_wait and futex_abstimed_wait_cancelable. */
|
|
|
|
static __always_inline int
|
2019-10-30 05:52:19 +08:00
|
|
|
futex_abstimed_supported_clockid (clockid_t clockid)
|
|
|
|
{
|
|
|
|
return lll_futex_supported_clockid (clockid);
|
|
|
|
}
|
2019-06-21 22:53:40 +08:00
|
|
|
|
2014-12-04 21:12:23 +08:00
|
|
|
/* Like futex_reltimed_wait, but the provided timeout (ABSTIME) is an
|
|
|
|
absolute point in time; a call will time out after this point in time. */
|
|
|
|
static __always_inline int
|
|
|
|
futex_abstimed_wait (unsigned int* futex_word, unsigned int expected,
|
2019-06-21 22:53:40 +08:00
|
|
|
clockid_t clockid,
|
2019-10-30 05:52:19 +08:00
|
|
|
const struct timespec* abstime, int private)
|
|
|
|
{
|
|
|
|
/* Work around the fact that the kernel rejects negative timeout values
|
|
|
|
despite them being valid. */
|
|
|
|
if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0)))
|
|
|
|
return ETIMEDOUT;
|
|
|
|
int err = lll_futex_clock_wait_bitset (futex_word, expected,
|
|
|
|
clockid, abstime,
|
|
|
|
private);
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -EAGAIN:
|
|
|
|
case -EINTR:
|
|
|
|
case -ETIMEDOUT:
|
|
|
|
return -err;
|
|
|
|
|
|
|
|
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
|
|
case -EINVAL: /* Either due to wrong alignment, unsupported
|
|
|
|
clockid or due to the timeout not being
|
|
|
|
normalized. Must have been caused by a glibc or
|
|
|
|
application bug. */
|
|
|
|
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
|
|
/* No other errors are documented at this time. */
|
|
|
|
default:
|
|
|
|
futex_fatal_error ();
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 21:12:23 +08:00
|
|
|
|
|
|
|
/* Like futex_reltimed_wait but is a POSIX cancellation point. */
|
|
|
|
static __always_inline int
|
|
|
|
futex_abstimed_wait_cancelable (unsigned int* futex_word,
|
|
|
|
unsigned int expected,
|
2019-06-21 22:53:40 +08:00
|
|
|
clockid_t clockid,
|
2019-10-30 05:52:19 +08:00
|
|
|
const struct timespec* abstime, int private)
|
|
|
|
{
|
|
|
|
/* Work around the fact that the kernel rejects negative timeout values
|
|
|
|
despite them being valid. */
|
|
|
|
if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0)))
|
|
|
|
return ETIMEDOUT;
|
|
|
|
int oldtype;
|
|
|
|
oldtype = __pthread_enable_asynccancel ();
|
|
|
|
int err = lll_futex_clock_wait_bitset (futex_word, expected,
|
|
|
|
clockid, abstime,
|
|
|
|
private);
|
|
|
|
__pthread_disable_asynccancel (oldtype);
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -EAGAIN:
|
|
|
|
case -EINTR:
|
|
|
|
case -ETIMEDOUT:
|
|
|
|
return -err;
|
|
|
|
|
|
|
|
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
|
|
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
|
|
being normalized. Must have been caused by a glibc or
|
|
|
|
application bug. */
|
|
|
|
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
|
|
/* No other errors are documented at this time. */
|
|
|
|
default:
|
|
|
|
futex_fatal_error ();
|
|
|
|
}
|
|
|
|
}
|
2014-12-04 21:12:23 +08:00
|
|
|
|
|
|
|
/* Atomically wrt other futex operations on the same futex, this unblocks the
|
|
|
|
specified number of processes, or all processes blocked on this futex if
|
|
|
|
there are fewer than the specified number. Semantically, this is
|
|
|
|
equivalent to:
|
|
|
|
l = <get lock associated with futex> (FUTEX_WORD);
|
|
|
|
lock (l);
|
|
|
|
for (res = 0; PROCESSES_TO_WAKE > 0; PROCESSES_TO_WAKE--, res++) {
|
|
|
|
if (<no process blocked on futex>) break;
|
|
|
|
wf = <get wait_flag of a process blocked on futex> (FUTEX_WORD);
|
|
|
|
// No happens-before guarantee with woken futex_wait (see above)
|
|
|
|
atomic_store_relaxed (wf, 0);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
|
|
|
|
Note that we need to support futex_wake calls to past futexes whose memory
|
|
|
|
has potentially been reused due to POSIX' requirements on synchronization
|
|
|
|
object destruction (see above); therefore, we must not report or abort
|
|
|
|
on most errors. */
|
|
|
|
static __always_inline void
|
2019-10-30 05:52:19 +08:00
|
|
|
futex_wake (unsigned int* futex_word, int processes_to_wake, int private)
|
2014-12-04 21:12:23 +08:00
|
|
|
{
|
2019-10-30 05:52:19 +08:00
|
|
|
int res = lll_futex_wake (futex_word, processes_to_wake, private);
|
|
|
|
/* No error. Ignore the number of woken processes. */
|
|
|
|
if (res >= 0)
|
|
|
|
return;
|
|
|
|
switch (res)
|
|
|
|
{
|
|
|
|
case -EFAULT: /* Could have happened due to memory reuse. */
|
|
|
|
case -EINVAL: /* Could be either due to incorrect alignment (a bug in
|
|
|
|
glibc or in the application) or due to memory being
|
|
|
|
reused for a PI futex. We cannot distinguish between the
|
|
|
|
two causes, and one of them is correct use, so we do not
|
|
|
|
act in this case. */
|
|
|
|
return;
|
|
|
|
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
|
|
/* No other errors are documented at this time. */
|
|
|
|
default:
|
|
|
|
futex_fatal_error ();
|
|
|
|
}
|
2014-12-04 21:12:23 +08:00
|
|
|
}
|
|
|
|
|
2019-10-31 00:29:40 +08:00
|
|
|
/* The operation checks the value of the futex, if the value is 0, then
|
|
|
|
it is atomically set to the caller's thread ID. If the futex value is
|
|
|
|
nonzero, it is atomically sets the FUTEX_WAITERS bit, which signals wrt
|
|
|
|
other futex owner that it cannot unlock the futex in user space by
|
|
|
|
atomically by setting its value to 0.
|
|
|
|
|
|
|
|
If more than one wait operations is issued, the enqueueing of the waiters
|
|
|
|
are done in descending priority order.
|
|
|
|
|
|
|
|
The ABSTIME arguments provides an absolute timeout (measured against the
|
|
|
|
CLOCK_REALTIME clock). If TIMEOUT is NULL, the operation will block
|
|
|
|
indefinitely.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
- 0 if woken by a PI unlock operation or spuriously.
|
|
|
|
- EAGAIN if the futex owner thread ID is about to exit, but has not yet
|
|
|
|
handled the state cleanup.
|
|
|
|
- EDEADLK if the futex is already locked by the caller.
|
|
|
|
- ESRCH if the thread ID int he futex does not exist.
|
|
|
|
- EINVAL is the state is corrupted or if there is a waiter on the
|
|
|
|
futex.
|
|
|
|
- ETIMEDOUT if the ABSTIME expires.
|
|
|
|
*/
|
|
|
|
static __always_inline int
|
|
|
|
futex_lock_pi (unsigned int *futex_word, const struct timespec *abstime,
|
|
|
|
int private)
|
|
|
|
{
|
|
|
|
int err = lll_futex_timed_lock_pi (futex_word, abstime, private);
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -EAGAIN:
|
|
|
|
case -EINTR:
|
|
|
|
case -ETIMEDOUT:
|
|
|
|
case -ESRCH:
|
|
|
|
case -EDEADLK:
|
|
|
|
case -EINVAL: /* This indicates either state corruption or that the kernel
|
|
|
|
found a waiter on futex address which is waiting via
|
|
|
|
FUTEX_WAIT or FUTEX_WAIT_BITSET. This is reported on
|
|
|
|
some futex_lock_pi usage (pthread_mutex_timedlock for
|
|
|
|
instance). */
|
|
|
|
return -err;
|
|
|
|
|
|
|
|
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
|
|
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
|
|
/* No other errors are documented at this time. */
|
|
|
|
default:
|
|
|
|
futex_fatal_error ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wakes the top priority waiter that called a futex_lock_pi operation on
|
|
|
|
the futex.
|
|
|
|
|
|
|
|
Returns the same values as futex_lock_pi under those same conditions;
|
|
|
|
additionally, returns EPERM when the caller is not allowed to attach
|
|
|
|
itself to the futex. */
|
|
|
|
static __always_inline int
|
|
|
|
futex_unlock_pi (unsigned int *futex_word, int private)
|
|
|
|
{
|
|
|
|
int err = lll_futex_timed_unlock_pi (futex_word, private);
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -EAGAIN:
|
|
|
|
case -EINTR:
|
|
|
|
case -ETIMEDOUT:
|
|
|
|
case -ESRCH:
|
|
|
|
case -EDEADLK:
|
|
|
|
case -ENOSYS:
|
|
|
|
case -EPERM: /* The caller is not allowed to attach itself to the futex.
|
|
|
|
Used to check if PI futexes are supported by the
|
|
|
|
kernel. */
|
|
|
|
return -err;
|
|
|
|
|
|
|
|
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
|
|
being normalized. Must have been caused by a glibc or
|
|
|
|
application bug. */
|
|
|
|
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
|
|
/* No other errors are documented at this time. */
|
|
|
|
default:
|
|
|
|
futex_fatal_error ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 21:12:23 +08:00
|
|
|
#endif /* futex-internal.h */
|