|
Packit |
6c4009 |
/* futex operations for glibc-internal use. Linux version.
|
|
Packit |
6c4009 |
Copyright (C) 2014-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef FUTEX_INTERNAL_H
|
|
Packit |
6c4009 |
#define FUTEX_INTERNAL_H
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <sysdeps/nptl/futex-internal.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <lowlevellock-futex.h>
|
|
Packit |
6c4009 |
#include <nptl/pthreadP.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* See sysdeps/nptl/futex-internal.h for documentation; this file only
|
|
Packit |
6c4009 |
contains Linux-specific comments.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The Linux kernel treats provides absolute timeouts based on the
|
|
Packit |
6c4009 |
CLOCK_REALTIME clock and relative timeouts measured against the
|
|
Packit |
6c4009 |
CLOCK_MONOTONIC clock.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
We expect a Linux kernel version of 2.6.22 or more recent (since this
|
|
Packit |
6c4009 |
version, EINTR is not returned on spurious wake-ups anymore). */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* FUTEX_SHARED is always supported by the Linux kernel. */
|
|
Packit |
6c4009 |
static __always_inline int
|
|
Packit |
6c4009 |
futex_supports_pshared (int pshared)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (__glibc_likely (pshared == PTHREAD_PROCESS_PRIVATE))
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
else if (pshared == PTHREAD_PROCESS_SHARED)
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
return EINVAL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The Linux kernel supports relative timeouts measured against the
|
|
Packit |
6c4009 |
CLOCK_MONOTONIC clock. */
|
|
Packit |
6c4009 |
static __always_inline bool
|
|
Packit |
6c4009 |
futex_supports_exact_relative_timeouts (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return true;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* See sysdeps/nptl/futex-internal.h for details. */
|
|
Packit |
6c4009 |
static __always_inline int
|
|
Packit |
6c4009 |
futex_wait (unsigned int *futex_word, unsigned int expected, int private)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int err = lll_futex_timed_wait (futex_word, expected, NULL, private);
|
|
Packit |
6c4009 |
switch (err)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case 0:
|
|
Packit |
6c4009 |
case -EAGAIN:
|
|
Packit |
6c4009 |
case -EINTR:
|
|
Packit |
6c4009 |
return -err;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case -ETIMEDOUT: /* Cannot have happened as we provided no timeout. */
|
|
Packit |
6c4009 |
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
Packit |
6c4009 |
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
Packit |
6c4009 |
being normalized. Must have been caused by a glibc or
|
|
Packit |
6c4009 |
application bug. */
|
|
Packit |
6c4009 |
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
Packit |
6c4009 |
/* No other errors are documented at this time. */
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
futex_fatal_error ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* See sysdeps/nptl/futex-internal.h for details. */
|
|
Packit |
6c4009 |
static __always_inline int
|
|
Packit |
6c4009 |
futex_wait_cancelable (unsigned int *futex_word, unsigned int expected,
|
|
Packit |
6c4009 |
int private)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int oldtype;
|
|
Packit |
6c4009 |
oldtype = __pthread_enable_asynccancel ();
|
|
Packit |
6c4009 |
int err = lll_futex_timed_wait (futex_word, expected, NULL, private);
|
|
Packit |
6c4009 |
__pthread_disable_asynccancel (oldtype);
|
|
Packit |
6c4009 |
switch (err)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case 0:
|
|
Packit |
6c4009 |
case -EAGAIN:
|
|
Packit |
6c4009 |
case -EINTR:
|
|
Packit |
6c4009 |
return -err;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case -ETIMEDOUT: /* Cannot have happened as we provided no timeout. */
|
|
Packit |
6c4009 |
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
Packit |
6c4009 |
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
Packit |
6c4009 |
being normalized. Must have been caused by a glibc or
|
|
Packit |
6c4009 |
application bug. */
|
|
Packit |
6c4009 |
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
Packit |
6c4009 |
/* No other errors are documented at this time. */
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
futex_fatal_error ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* See sysdeps/nptl/futex-internal.h for details. */
|
|
Packit |
6c4009 |
static __always_inline int
|
|
Packit |
6c4009 |
futex_reltimed_wait (unsigned int *futex_word, unsigned int expected,
|
|
Packit |
6c4009 |
const struct timespec *reltime, int private)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int err = lll_futex_timed_wait (futex_word, expected, reltime, private);
|
|
Packit |
6c4009 |
switch (err)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case 0:
|
|
Packit |
6c4009 |
case -EAGAIN:
|
|
Packit |
6c4009 |
case -EINTR:
|
|
Packit |
6c4009 |
case -ETIMEDOUT:
|
|
Packit |
6c4009 |
return -err;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
Packit |
6c4009 |
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
Packit |
6c4009 |
being normalized. Must have been caused by a glibc or
|
|
Packit |
6c4009 |
application bug. */
|
|
Packit |
6c4009 |
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
Packit |
6c4009 |
/* No other errors are documented at this time. */
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
futex_fatal_error ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* See sysdeps/nptl/futex-internal.h for details. */
|
|
Packit |
6c4009 |
static __always_inline int
|
|
Packit |
6c4009 |
futex_reltimed_wait_cancelable (unsigned int *futex_word,
|
|
Packit |
6c4009 |
unsigned int expected,
|
|
Packit |
6c4009 |
const struct timespec *reltime, int private)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int oldtype;
|
|
Packit |
6c4009 |
oldtype = __pthread_enable_asynccancel ();
|
|
Packit |
6c4009 |
int err = lll_futex_timed_wait (futex_word, expected, reltime, private);
|
|
Packit |
6c4009 |
__pthread_disable_asynccancel (oldtype);
|
|
Packit |
6c4009 |
switch (err)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case 0:
|
|
Packit |
6c4009 |
case -EAGAIN:
|
|
Packit |
6c4009 |
case -EINTR:
|
|
Packit |
6c4009 |
case -ETIMEDOUT:
|
|
Packit |
6c4009 |
return -err;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
Packit |
6c4009 |
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
Packit |
6c4009 |
being normalized. Must have been caused by a glibc or
|
|
Packit |
6c4009 |
application bug. */
|
|
Packit |
6c4009 |
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
Packit |
6c4009 |
/* No other errors are documented at this time. */
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
futex_fatal_error ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* See sysdeps/nptl/futex-internal.h for details. */
|
|
Packit |
6c4009 |
static __always_inline int
|
|
Packit |
6c4009 |
futex_abstimed_wait (unsigned int *futex_word, unsigned int expected,
|
|
Packit |
6c4009 |
const struct timespec *abstime, int private)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Work around the fact that the kernel rejects negative timeout values
|
|
Packit |
6c4009 |
despite them being valid. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0)))
|
|
Packit |
6c4009 |
return ETIMEDOUT;
|
|
Packit |
6c4009 |
int err = lll_futex_timed_wait_bitset (futex_word, expected, abstime,
|
|
Packit |
6c4009 |
FUTEX_CLOCK_REALTIME, private);
|
|
Packit |
6c4009 |
switch (err)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case 0:
|
|
Packit |
6c4009 |
case -EAGAIN:
|
|
Packit |
6c4009 |
case -EINTR:
|
|
Packit |
6c4009 |
case -ETIMEDOUT:
|
|
Packit |
6c4009 |
return -err;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
Packit |
6c4009 |
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
Packit |
6c4009 |
being normalized. Must have been caused by a glibc or
|
|
Packit |
6c4009 |
application bug. */
|
|
Packit |
6c4009 |
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
Packit |
6c4009 |
/* No other errors are documented at this time. */
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
futex_fatal_error ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* See sysdeps/nptl/futex-internal.h for details. */
|
|
Packit |
6c4009 |
static __always_inline int
|
|
Packit |
6c4009 |
futex_abstimed_wait_cancelable (unsigned int *futex_word,
|
|
Packit |
6c4009 |
unsigned int expected,
|
|
Packit |
6c4009 |
const struct timespec *abstime, int private)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Work around the fact that the kernel rejects negative timeout values
|
|
Packit |
6c4009 |
despite them being valid. */
|
|
Packit |
6c4009 |
if (__glibc_unlikely ((abstime != NULL) && (abstime->tv_sec < 0)))
|
|
Packit |
6c4009 |
return ETIMEDOUT;
|
|
Packit |
6c4009 |
int oldtype;
|
|
Packit |
6c4009 |
oldtype = __pthread_enable_asynccancel ();
|
|
Packit |
6c4009 |
int err = lll_futex_timed_wait_bitset (futex_word, expected, abstime,
|
|
Packit |
6c4009 |
FUTEX_CLOCK_REALTIME, private);
|
|
Packit |
6c4009 |
__pthread_disable_asynccancel (oldtype);
|
|
Packit |
6c4009 |
switch (err)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case 0:
|
|
Packit |
6c4009 |
case -EAGAIN:
|
|
Packit |
6c4009 |
case -EINTR:
|
|
Packit |
6c4009 |
case -ETIMEDOUT:
|
|
Packit |
6c4009 |
return -err;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case -EFAULT: /* Must have been caused by a glibc or application bug. */
|
|
Packit |
6c4009 |
case -EINVAL: /* Either due to wrong alignment or due to the timeout not
|
|
Packit |
6c4009 |
being normalized. Must have been caused by a glibc or
|
|
Packit |
6c4009 |
application bug. */
|
|
Packit |
6c4009 |
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
Packit |
6c4009 |
/* No other errors are documented at this time. */
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
futex_fatal_error ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* See sysdeps/nptl/futex-internal.h for details. */
|
|
Packit |
6c4009 |
static __always_inline void
|
|
Packit |
6c4009 |
futex_wake (unsigned int *futex_word, int processes_to_wake, int private)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int res = lll_futex_wake (futex_word, processes_to_wake, private);
|
|
Packit |
6c4009 |
/* No error. Ignore the number of woken processes. */
|
|
Packit |
6c4009 |
if (res >= 0)
|
|
Packit |
6c4009 |
return;
|
|
Packit |
6c4009 |
switch (res)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case -EFAULT: /* Could have happened due to memory reuse. */
|
|
Packit |
6c4009 |
case -EINVAL: /* Could be either due to incorrect alignment (a bug in
|
|
Packit |
6c4009 |
glibc or in the application) or due to memory being
|
|
Packit |
6c4009 |
reused for a PI futex. We cannot distinguish between the
|
|
Packit |
6c4009 |
two causes, and one of them is correct use, so we do not
|
|
Packit |
6c4009 |
act in this case. */
|
|
Packit |
6c4009 |
return;
|
|
Packit |
6c4009 |
case -ENOSYS: /* Must have been caused by a glibc bug. */
|
|
Packit |
6c4009 |
/* No other errors are documented at this time. */
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
futex_fatal_error ();
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#endif /* futex-internal.h */
|