Blame sysdeps/unix/sysv/linux/futex-internal.h

Packit Service 82fcde
/* futex operations for glibc-internal use.  Linux version.
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 FUTEX_INTERNAL_H
Packit Service 82fcde
#define FUTEX_INTERNAL_H
Packit Service 82fcde
Packit Service 82fcde
#include <sysdeps/nptl/futex-internal.h>
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <lowlevellock-futex.h>
Packit Service 82fcde
#include <nptl/pthreadP.h>
Packit Service 82fcde
Packit Service 82fcde
/* See sysdeps/nptl/futex-internal.h for documentation; this file only
Packit Service 82fcde
   contains Linux-specific comments.
Packit Service 82fcde
Packit Service 82fcde
   The Linux kernel treats provides absolute timeouts based on the
Packit Service 82fcde
   CLOCK_REALTIME clock and relative timeouts measured against the
Packit Service 82fcde
   CLOCK_MONOTONIC clock.
Packit Service 82fcde
Packit Service 82fcde
   We expect a Linux kernel version of 2.6.22 or more recent (since this
Packit Service 82fcde
   version, EINTR is not returned on spurious wake-ups anymore).  */
Packit Service 82fcde
Packit Service 82fcde
/* FUTEX_SHARED is always supported by the Linux kernel.  */
Packit Service 82fcde
static __always_inline int
Packit Service 82fcde
futex_supports_pshared (int pshared)
Packit Service 82fcde
{
Packit Service 82fcde
  if (__glibc_likely (pshared == PTHREAD_PROCESS_PRIVATE))
Packit Service 82fcde
    return 0;
Packit Service 82fcde
  else if (pshared == PTHREAD_PROCESS_SHARED)
Packit Service 82fcde
    return 0;
Packit Service 82fcde
  else
Packit Service 82fcde
    return EINVAL;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* The Linux kernel supports relative timeouts measured against the
Packit Service 82fcde
   CLOCK_MONOTONIC clock.  */
Packit Service 82fcde
static __always_inline bool
Packit Service 82fcde
futex_supports_exact_relative_timeouts (void)
Packit Service 82fcde
{
Packit Service 82fcde
  return true;
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* See sysdeps/nptl/futex-internal.h for details.  */
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
  int err = lll_futex_timed_wait (futex_word, expected, NULL, private);
Packit Service 82fcde
  switch (err)
Packit Service 82fcde
    {
Packit Service 82fcde
    case 0:
Packit Service 82fcde
    case -EAGAIN:
Packit Service 82fcde
    case -EINTR:
Packit Service 82fcde
      return -err;
Packit Service 82fcde
Packit Service 82fcde
    case -ETIMEDOUT: /* Cannot have happened as we provided no timeout.  */
Packit Service 82fcde
    case -EFAULT: /* Must have been caused by a glibc or application bug.  */
Packit Service 82fcde
    case -EINVAL: /* Either due to wrong alignment or due to the timeout not
Packit Service 82fcde
		     being normalized.  Must have been caused by a glibc or
Packit Service 82fcde
		     application bug.  */
Packit Service 82fcde
    case -ENOSYS: /* Must have been caused by a glibc bug.  */
Packit Service 82fcde
    /* No other errors are documented at this time.  */
Packit Service 82fcde
    default:
Packit Service 82fcde
      futex_fatal_error ();
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* See sysdeps/nptl/futex-internal.h for details.  */
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
  int oldtype;
Packit Service 82fcde
  oldtype = __pthread_enable_asynccancel ();
Packit Service 82fcde
  int err = lll_futex_timed_wait (futex_word, expected, NULL, private);
Packit Service 82fcde
  __pthread_disable_asynccancel (oldtype);
Packit Service 82fcde
  switch (err)
Packit Service 82fcde
    {
Packit Service 82fcde
    case 0:
Packit Service 82fcde
    case -EAGAIN:
Packit Service 82fcde
    case -EINTR:
Packit Service 82fcde
      return -err;
Packit Service 82fcde
Packit Service 82fcde
    case -ETIMEDOUT: /* Cannot have happened as we provided no timeout.  */
Packit Service 82fcde
    case -EFAULT: /* Must have been caused by a glibc or application bug.  */
Packit Service 82fcde
    case -EINVAL: /* Either due to wrong alignment or due to the timeout not
Packit Service 82fcde
		     being normalized.  Must have been caused by a glibc or
Packit Service 82fcde
		     application bug.  */
Packit Service 82fcde
    case -ENOSYS: /* Must have been caused by a glibc bug.  */
Packit Service 82fcde
    /* No other errors are documented at this time.  */
Packit Service 82fcde
    default:
Packit Service 82fcde
      futex_fatal_error ();
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* See sysdeps/nptl/futex-internal.h for details.  */
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
  int err = lll_futex_timed_wait (futex_word, expected, reltime, private);
Packit Service 82fcde
  switch (err)
Packit Service 82fcde
    {
Packit Service 82fcde
    case 0:
Packit Service 82fcde
    case -EAGAIN:
Packit Service 82fcde
    case -EINTR:
Packit Service 82fcde
    case -ETIMEDOUT:
Packit Service 82fcde
      return -err;
Packit Service 82fcde
Packit Service 82fcde
    case -EFAULT: /* Must have been caused by a glibc or application bug.  */
Packit Service 82fcde
    case -EINVAL: /* Either due to wrong alignment or due to the timeout not
Packit Service 82fcde
		     being normalized.  Must have been caused by a glibc or
Packit Service 82fcde
		     application bug.  */
Packit Service 82fcde
    case -ENOSYS: /* Must have been caused by a glibc bug.  */
Packit Service 82fcde
    /* No other errors are documented at this time.  */
Packit Service 82fcde
    default:
Packit Service 82fcde
      futex_fatal_error ();
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* See sysdeps/nptl/futex-internal.h for details.  */
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
  int oldtype;
Packit Service 82fcde
  oldtype = __pthread_enable_asynccancel ();
Packit Service 82fcde
  int err = lll_futex_timed_wait (futex_word, expected, reltime, private);
Packit Service 82fcde
  __pthread_disable_asynccancel (oldtype);
Packit Service 82fcde
  switch (err)
Packit Service 82fcde
    {
Packit Service 82fcde
    case 0:
Packit Service 82fcde
    case -EAGAIN:
Packit Service 82fcde
    case -EINTR:
Packit Service 82fcde
    case -ETIMEDOUT:
Packit Service 82fcde
      return -err;
Packit Service 82fcde
Packit Service 82fcde
    case -EFAULT: /* Must have been caused by a glibc or application bug.  */
Packit Service 82fcde
    case -EINVAL: /* Either due to wrong alignment or due to the timeout not
Packit Service 82fcde
		     being normalized.  Must have been caused by a glibc or
Packit Service 82fcde
		     application bug.  */
Packit Service 82fcde
    case -ENOSYS: /* Must have been caused by a glibc bug.  */
Packit Service 82fcde
    /* No other errors are documented at this time.  */
Packit Service 82fcde
    default:
Packit Service 82fcde
      futex_fatal_error ();
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* See sysdeps/nptl/futex-internal.h for details.  */
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
  /* Work around the fact that the kernel rejects negative timeout values
Packit Service 82fcde
     despite them being valid.  */
Packit Service 82fcde
  if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0)))
Packit Service 82fcde
    return ETIMEDOUT;
Packit Service 82fcde
  int err = lll_futex_timed_wait_bitset (futex_word, expected, abstime,
Packit Service 82fcde
					 FUTEX_CLOCK_REALTIME, private);
Packit Service 82fcde
  switch (err)
Packit Service 82fcde
    {
Packit Service 82fcde
    case 0:
Packit Service 82fcde
    case -EAGAIN:
Packit Service 82fcde
    case -EINTR:
Packit Service 82fcde
    case -ETIMEDOUT:
Packit Service 82fcde
      return -err;
Packit Service 82fcde
Packit Service 82fcde
    case -EFAULT: /* Must have been caused by a glibc or application bug.  */
Packit Service 82fcde
    case -EINVAL: /* Either due to wrong alignment or due to the timeout not
Packit Service 82fcde
		     being normalized.  Must have been caused by a glibc or
Packit Service 82fcde
		     application bug.  */
Packit Service 82fcde
    case -ENOSYS: /* Must have been caused by a glibc bug.  */
Packit Service 82fcde
    /* No other errors are documented at this time.  */
Packit Service 82fcde
    default:
Packit Service 82fcde
      futex_fatal_error ();
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* See sysdeps/nptl/futex-internal.h for details.  */
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
  /* Work around the fact that the kernel rejects negative timeout values
Packit Service 82fcde
     despite them being valid.  */
Packit Service 82fcde
  if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0)))
Packit Service 82fcde
    return ETIMEDOUT;
Packit Service 82fcde
  int oldtype;
Packit Service 82fcde
  oldtype = __pthread_enable_asynccancel ();
Packit Service 82fcde
  int err = lll_futex_timed_wait_bitset (futex_word, expected, abstime,
Packit Service 82fcde
					 FUTEX_CLOCK_REALTIME, private);
Packit Service 82fcde
  __pthread_disable_asynccancel (oldtype);
Packit Service 82fcde
  switch (err)
Packit Service 82fcde
    {
Packit Service 82fcde
    case 0:
Packit Service 82fcde
    case -EAGAIN:
Packit Service 82fcde
    case -EINTR:
Packit Service 82fcde
    case -ETIMEDOUT:
Packit Service 82fcde
      return -err;
Packit Service 82fcde
Packit Service 82fcde
    case -EFAULT: /* Must have been caused by a glibc or application bug.  */
Packit Service 82fcde
    case -EINVAL: /* Either due to wrong alignment or due to the timeout not
Packit Service 82fcde
		     being normalized.  Must have been caused by a glibc or
Packit Service 82fcde
		     application bug.  */
Packit Service 82fcde
    case -ENOSYS: /* Must have been caused by a glibc bug.  */
Packit Service 82fcde
    /* No other errors are documented at this time.  */
Packit Service 82fcde
    default:
Packit Service 82fcde
      futex_fatal_error ();
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
/* See sysdeps/nptl/futex-internal.h for details.  */
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
  int res = lll_futex_wake (futex_word, processes_to_wake, private);
Packit Service 82fcde
  /* No error.  Ignore the number of woken processes.  */
Packit Service 82fcde
  if (res >= 0)
Packit Service 82fcde
    return;
Packit Service 82fcde
  switch (res)
Packit Service 82fcde
    {
Packit Service 82fcde
    case -EFAULT: /* Could have happened due to memory reuse.  */
Packit Service 82fcde
    case -EINVAL: /* Could be either due to incorrect alignment (a bug in
Packit Service 82fcde
		     glibc or in the application) or due to memory being
Packit Service 82fcde
		     reused for a PI futex.  We cannot distinguish between the
Packit Service 82fcde
		     two causes, and one of them is correct use, so we do not
Packit Service 82fcde
		     act in this case.  */
Packit Service 82fcde
      return;
Packit Service 82fcde
    case -ENOSYS: /* Must have been caused by a glibc bug.  */
Packit Service 82fcde
    /* No other errors are documented at this time.  */
Packit Service 82fcde
    default:
Packit Service 82fcde
      futex_fatal_error ();
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#endif  /* futex-internal.h */