Blame sysdeps/nptl/futex-internal.h

Packit Service 82fcde
/* futex operations for glibc-internal use.  Stub version; do not include
Packit Service 82fcde
   this file directly.
Packit Service 82fcde
   Copyright (C) 2014-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
#ifndef STUB_FUTEX_INTERNAL_H
Packit Service 82fcde
#define STUB_FUTEX_INTERNAL_H
Packit Service 82fcde
Packit Service 82fcde
#include <sys/time.h>
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <stdbool.h>
Packit Service 82fcde
#include <libc-diag.h>
Packit Service 82fcde
Packit Service 82fcde
/* This file defines futex operations used internally in glibc.  A futex
Packit Service 82fcde
   consists of the so-called futex word in userspace, which is of type
Packit Service 82fcde
   unsigned int and represents an application-specific condition, and kernel
Packit Service 82fcde
   state associated with this particular futex word (e.g., wait queues).  The
Packit Service 82fcde
   futex operations we provide are wrappers for the futex syscalls and add
Packit Service 82fcde
   glibc-specific error checking of the syscall return value.  We abort on
Packit Service 82fcde
   error codes that are caused by bugs in glibc or in the calling application,
Packit Service 82fcde
   or when an error code is not known.  We return error codes that can arise
Packit Service 82fcde
   in correct executions to the caller.  Each operation calls out exactly the
Packit Service 82fcde
   return values that callers need to handle.
Packit Service 82fcde
Packit Service 82fcde
   The private flag must be either FUTEX_PRIVATE or FUTEX_SHARED.
Packit Service 82fcde
   FUTEX_PRIVATE is always supported, and the implementation can internally
Packit Service 82fcde
   use FUTEX_SHARED when FUTEX_PRIVATE is requested.  FUTEX_SHARED is not
Packit Service 82fcde
   necessarily supported (use futex_supports_pshared to detect this).
Packit Service 82fcde
Packit Service 82fcde
   We expect callers to only use these operations if futexes and the
Packit Service 82fcde
   specific futex operations being used are supported (e.g., FUTEX_SHARED).
Packit Service 82fcde
Packit Service 82fcde
   Given that waking other threads waiting on a futex involves concurrent
Packit Service 82fcde
   accesses to the futex word, you must use atomic operations to access the
Packit Service 82fcde
   futex word.
Packit Service 82fcde
Packit Service 82fcde
   Both absolute and relative timeouts can be used.  An absolute timeout
Packit Service 82fcde
   expires when the given specific point in time on the CLOCK_REALTIME clock
Packit Service 82fcde
   passes, or when it already has passed.  A relative timeout expires when
Packit Service 82fcde
   the given duration of time on the CLOCK_MONOTONIC clock passes.  Relative
Packit Service 82fcde
   timeouts may be imprecise (see futex_supports_exact_relative_timeouts).
Packit Service 82fcde
Packit Service 82fcde
   Due to POSIX requirements on when synchronization data structures such
Packit Service 82fcde
   as mutexes or semaphores can be destroyed and due to the futex design
Packit Service 82fcde
   having separate fast/slow paths for wake-ups, we need to consider that
Packit Service 82fcde
   futex_wake calls might effectively target a data structure that has been
Packit Service 82fcde
   destroyed and reused for another object, or unmapped; thus, some
Packit Service 82fcde
   errors or spurious wake-ups can happen in correct executions that would
Packit Service 82fcde
   not be possible in a program using just a single futex whose lifetime
Packit Service 82fcde
   does not end before the program terminates.  For background, see:
Packit Service 82fcde
   https://sourceware.org/ml/libc-alpha/2014-04/msg00075.html
Packit Service 82fcde
   https://lkml.org/lkml/2014/11/27/472  */
Packit Service 82fcde
Packit Service 82fcde
/* Defined this way for interoperability with lowlevellock.
Packit Service 82fcde
   FUTEX_PRIVATE must be zero because the initializers for pthread_mutex_t,
Packit Service 82fcde
   pthread_rwlock_t, and pthread_cond_t initialize the respective field of
Packit Service 82fcde
   those structures to zero, and we want FUTEX_PRIVATE to be the default.  */
Packit Service 82fcde
#define FUTEX_PRIVATE LLL_PRIVATE
Packit Service 82fcde
#define FUTEX_SHARED  LLL_SHARED
Packit Service 82fcde
#if FUTEX_PRIVATE != 0
Packit Service 82fcde
# error FUTEX_PRIVATE must be equal to 0
Packit Service 82fcde
#endif
Packit Service 82fcde
Packit Service 82fcde
/* Returns EINVAL if PSHARED is neither PTHREAD_PROCESS_PRIVATE nor
Packit Service 82fcde
   PTHREAD_PROCESS_SHARED; otherwise, returns 0 if PSHARED is supported, and
Packit Service 82fcde
   ENOTSUP if not.  */
Packit Service 82fcde
static __always_inline int
Packit Service 82fcde
futex_supports_pshared (int pshared);
Packit Service 82fcde
Packit Service 82fcde
/* Returns true if relative timeouts are robust to concurrent changes to the
Packit Service 82fcde
   system clock.  If this returns false, relative timeouts can still be used
Packit Service 82fcde
   but might be effectively longer or shorter than requested.  */
Packit Service 82fcde
static __always_inline bool
Packit Service 82fcde
futex_supports_exact_relative_timeouts (void);
Packit Service 82fcde
Packit Service 82fcde
/* Atomically wrt other futex operations on the same futex, this blocks iff
Packit Service 82fcde
   the value *FUTEX_WORD matches the expected value.  This is
Packit Service 82fcde
   semantically equivalent to:
Packit Service 82fcde
     l = <get lock associated with futex> (FUTEX_WORD);
Packit Service 82fcde
     wait_flag = <get wait_flag associated with futex> (FUTEX_WORD);
Packit Service 82fcde
     lock (l);
Packit Service 82fcde
     val = atomic_load_relaxed (FUTEX_WORD);
Packit Service 82fcde
     if (val != expected) { unlock (l); return EAGAIN; }
Packit Service 82fcde
     atomic_store_relaxed (wait_flag, true);
Packit Service 82fcde
     unlock (l);
Packit Service 82fcde
     // Now block; can time out in futex_time_wait (see below)
Packit Service 82fcde
     while (atomic_load_relaxed(wait_flag) && !<spurious wake-up>);
Packit Service 82fcde
Packit Service 82fcde
   Note that no guarantee of a happens-before relation between a woken
Packit Service 82fcde
   futex_wait and a futex_wake is documented; however, this does not matter
Packit Service 82fcde
   in practice because we have to consider spurious wake-ups (see below),
Packit Service 82fcde
   and thus would not be able to reliably reason about which futex_wake woke
Packit Service 82fcde
   us.
Packit Service 82fcde
Packit Service 82fcde
   Returns 0 if woken by a futex operation or spuriously.  (Note that due to
Packit Service 82fcde
   the POSIX requirements mentioned above, we need to conservatively assume
Packit Service 82fcde
   that unrelated futex_wake operations could wake this futex; it is easiest
Packit Service 82fcde
   to just be prepared for spurious wake-ups.)
Packit Service 82fcde
   Returns EAGAIN if the futex word did not match the expected value.
Packit Service 82fcde
   Returns EINTR if waiting was interrupted by a signal.
Packit Service 82fcde
Packit Service 82fcde
   Note that some previous code in glibc assumed the underlying futex
Packit Service 82fcde
   operation (e.g., syscall) to start with or include the equivalent of a
Packit Service 82fcde
   seq_cst fence; this allows one to avoid an explicit seq_cst fence before
Packit Service 82fcde
   a futex_wait call when synchronizing similar to Dekker synchronization.
Packit Service 82fcde
   However, we make no such guarantee here.  */
Packit Service 82fcde
static __always_inline int
Packit Service 82fcde
futex_wait (unsigned int *futex_word, unsigned int expected, int private);
Packit Service 82fcde
Packit Service 82fcde
/* Like futex_wait but does not provide any indication why we stopped waiting.
Packit Service 82fcde
   Thus, when this function returns, you have to always check FUTEX_WORD to
Packit Service 82fcde
   determine whether you need to continue waiting, and you cannot detect
Packit Service 82fcde
   whether the waiting was interrupted by a signal.  Example use:
Packit Service 82fcde
     while (atomic_load_relaxed (&futex_word) == 23)
Packit Service 82fcde
       futex_wait_simple (&futex_word, 23, FUTEX_PRIVATE);
Packit Service 82fcde
   This is common enough to make providing this wrapper worthwhile.  */
Packit Service 82fcde
static __always_inline void
Packit Service 82fcde
futex_wait_simple (unsigned int *futex_word, unsigned int expected,
Packit Service 82fcde
		   int private)
Packit Service 82fcde
{
Packit Service 82fcde
  ignore_value (futex_wait (futex_word, expected, private));
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
/* Like futex_wait but is a POSIX cancellation point.  */
Packit Service 82fcde
static __always_inline int
Packit Service 82fcde
futex_wait_cancelable (unsigned int *futex_word, unsigned int expected,
Packit Service 82fcde
		       int private);
Packit Service 82fcde
Packit Service 82fcde
/* Like futex_wait, but will eventually time out (i.e., stop being
Packit Service 82fcde
   blocked) after the duration of time provided (i.e., RELTIME) has
Packit Service 82fcde
   passed.  The caller must provide a normalized RELTIME.  RELTIME can also
Packit Service 82fcde
   equal NULL, in which case this function behaves equivalent to futex_wait.
Packit Service 82fcde
Packit Service 82fcde
   Returns the same values as futex_wait under those same conditions;
Packit Service 82fcde
   additionally, returns ETIMEDOUT if the timeout expired.
Packit Service 82fcde
   */
Packit Service 82fcde
static __always_inline int
Packit Service 82fcde
futex_reltimed_wait (unsigned int* futex_word, unsigned int expected,
Packit Service 82fcde
		     const struct timespec* reltime, int private);
Packit Service 82fcde
Packit Service 82fcde
/* Like futex_reltimed_wait but is a POSIX cancellation point.  */
Packit Service 82fcde
static __always_inline int
Packit Service 82fcde
futex_reltimed_wait_cancelable (unsigned int* futex_word,
Packit Service 82fcde
				unsigned int expected,
Packit Service 82fcde
			        const struct timespec* reltime, int private);
Packit Service 82fcde
Packit Service 82fcde
/* Like futex_reltimed_wait, but the provided timeout (ABSTIME) is an
Packit Service 82fcde
   absolute point in time; a call will time out after this point in time.  */
Packit Service 82fcde
static __always_inline int
Packit Service 82fcde
futex_abstimed_wait (unsigned int* futex_word, unsigned int expected,
Packit Service 82fcde
		     const struct timespec* abstime, int private);
Packit Service 82fcde
Packit Service 82fcde
/* Like futex_reltimed_wait but is a POSIX cancellation point.  */
Packit Service 82fcde
static __always_inline int
Packit Service 82fcde
futex_abstimed_wait_cancelable (unsigned int* futex_word,
Packit Service 82fcde
				unsigned int expected,
Packit Service 82fcde
			        const struct timespec* abstime, int private);
Packit Service 82fcde
Packit Service 82fcde
/* Atomically wrt other futex operations on the same futex, this unblocks the
Packit Service 82fcde
   specified number of processes, or all processes blocked on this futex if
Packit Service 82fcde
   there are fewer than the specified number.  Semantically, this is
Packit Service 82fcde
   equivalent to:
Packit Service 82fcde
     l = <get lock associated with futex> (FUTEX_WORD);
Packit Service 82fcde
     lock (l);
Packit Service 82fcde
     for (res = 0; PROCESSES_TO_WAKE > 0; PROCESSES_TO_WAKE--, res++) {
Packit Service 82fcde
       if (<no process blocked on futex>) break;
Packit Service 82fcde
       wf = <get wait_flag of a process blocked on futex> (FUTEX_WORD);
Packit Service 82fcde
       // No happens-before guarantee with woken futex_wait (see above)
Packit Service 82fcde
       atomic_store_relaxed (wf, 0);
Packit Service 82fcde
     }
Packit Service 82fcde
     return res;
Packit Service 82fcde
Packit Service 82fcde
   Note that we need to support futex_wake calls to past futexes whose memory
Packit Service 82fcde
   has potentially been reused due to POSIX' requirements on synchronization
Packit Service 82fcde
   object destruction (see above); therefore, we must not report or abort
Packit Service 82fcde
   on most errors.  */
Packit Service 82fcde
static __always_inline void
Packit Service 82fcde
futex_wake (unsigned int* futex_word, int processes_to_wake, int private);
Packit Service 82fcde
Packit Service 82fcde
/* Calls __libc_fatal with an error message.  Convenience function for
Packit Service 82fcde
   concrete implementations of the futex interface.  */
Packit Service 82fcde
static __always_inline __attribute__ ((__noreturn__)) void
Packit Service 82fcde
futex_fatal_error (void)
Packit Service 82fcde
{
Packit Service 6615c7
  __libc_fatal ("The futex facility returned an unexpected error code.\n");
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#endif  /* futex-internal.h */