2018-04-01 23:43:22 +00:00
|
|
|
/* Wait on a semaphore with a timeout. Generic version.
|
2024-01-01 18:12:26 +00:00
|
|
|
Copyright (C) 2005-2024 Free Software Foundation, Inc.
|
2018-04-01 23:43:22 +00:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2018-04-02 14:37:36 +00:00
|
|
|
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.
|
2018-04-01 23:43:22 +00:00
|
|
|
|
|
|
|
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
|
2018-04-02 14:37:36 +00:00
|
|
|
Lesser General Public License for more details.
|
2018-04-01 23:43:22 +00:00
|
|
|
|
2018-04-02 14:37:36 +00:00
|
|
|
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 05:40:42 +00:00
|
|
|
<https://www.gnu.org/licenses/>. */
|
2018-04-01 23:43:22 +00:00
|
|
|
|
|
|
|
#include <semaphore.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2019-10-24 14:20:56 +00:00
|
|
|
#include <time.h>
|
2020-12-13 15:14:40 +00:00
|
|
|
#include <hurdlock.h>
|
|
|
|
#include <hurd/hurd.h>
|
|
|
|
#include <sysdep-cancel.h>
|
2018-04-01 23:43:22 +00:00
|
|
|
|
|
|
|
#include <pt-internal.h>
|
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
#if !__HAVE_64B_ATOMICS
|
|
|
|
static void
|
|
|
|
__sem_wait_32_finish (struct new_sem *isem);
|
|
|
|
#endif
|
2020-06-23 22:43:32 +00:00
|
|
|
|
|
|
|
static void
|
2020-12-13 15:14:40 +00:00
|
|
|
__sem_wait_cleanup (void *arg)
|
2020-06-23 22:43:32 +00:00
|
|
|
{
|
2020-12-13 15:14:40 +00:00
|
|
|
struct new_sem *isem = arg;
|
2020-06-23 22:43:32 +00:00
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
#if __HAVE_64B_ATOMICS
|
|
|
|
atomic_fetch_add_relaxed (&isem->data, -((uint64_t) 1 << SEM_NWAITERS_SHIFT));
|
|
|
|
#else
|
|
|
|
__sem_wait_32_finish (isem);
|
|
|
|
#endif
|
2020-06-23 22:43:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 23:43:22 +00:00
|
|
|
int
|
|
|
|
__sem_timedwait_internal (sem_t *restrict sem,
|
2020-06-07 01:12:36 +00:00
|
|
|
clockid_t clock_id,
|
2018-04-01 23:43:22 +00:00
|
|
|
const struct timespec *restrict timeout)
|
|
|
|
{
|
2020-12-13 15:14:40 +00:00
|
|
|
struct new_sem *isem = (struct new_sem *) sem;
|
|
|
|
int err, ret = 0;
|
|
|
|
int flags = isem->pshared ? GSYNC_SHARED : 0;
|
2020-06-23 22:43:32 +00:00
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
__pthread_testcancel ();
|
2018-04-01 23:43:22 +00:00
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
if (__sem_waitfast (isem, 0) == 0)
|
|
|
|
return 0;
|
2020-06-23 22:43:32 +00:00
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
int cancel_oldtype = LIBC_CANCEL_ASYNC();
|
2018-04-01 23:43:22 +00:00
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
#if __HAVE_64B_ATOMICS
|
2023-02-12 11:10:36 +00:00
|
|
|
uint64_t d = atomic_fetch_add_relaxed (&isem->data,
|
2020-12-13 15:14:40 +00:00
|
|
|
(uint64_t) 1 << SEM_NWAITERS_SHIFT);
|
|
|
|
|
|
|
|
pthread_cleanup_push (__sem_wait_cleanup, isem);
|
2018-04-01 23:43:22 +00:00
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
for (;;)
|
2018-04-01 23:43:22 +00:00
|
|
|
{
|
2020-12-13 15:14:40 +00:00
|
|
|
if ((d & SEM_VALUE_MASK) == 0)
|
|
|
|
{
|
|
|
|
/* No token, sleep. */
|
|
|
|
if (timeout)
|
|
|
|
err = __lll_abstimed_wait_intr (
|
2023-02-12 11:10:36 +00:00
|
|
|
((unsigned int *) &isem->data) + SEM_VALUE_OFFSET,
|
2020-12-13 15:14:40 +00:00
|
|
|
0, timeout, flags, clock_id);
|
|
|
|
else
|
|
|
|
err = __lll_wait_intr (
|
2023-02-12 11:10:36 +00:00
|
|
|
((unsigned int *) &isem->data) + SEM_VALUE_OFFSET,
|
2020-12-13 15:14:40 +00:00
|
|
|
0, flags);
|
|
|
|
|
2022-12-29 23:40:18 +00:00
|
|
|
if (err != 0 && err != KERN_INVALID_ARGUMENT)
|
2020-12-13 15:14:40 +00:00
|
|
|
{
|
|
|
|
/* Error, interruption or timeout, abort. */
|
|
|
|
if (err == KERN_TIMEDOUT)
|
|
|
|
err = ETIMEDOUT;
|
|
|
|
if (err == KERN_INTERRUPTED)
|
|
|
|
err = EINTR;
|
|
|
|
ret = __hurd_fail (err);
|
|
|
|
__sem_wait_cleanup (isem);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Token changed */
|
2023-02-12 11:10:36 +00:00
|
|
|
d = atomic_load_relaxed (&isem->data);
|
2020-12-13 15:14:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Try to acquire and dequeue. */
|
2023-02-12 11:10:36 +00:00
|
|
|
if (atomic_compare_exchange_weak_acquire (&isem->data,
|
2020-12-13 15:14:40 +00:00
|
|
|
&d, d - 1 - ((uint64_t) 1 << SEM_NWAITERS_SHIFT)))
|
|
|
|
{
|
|
|
|
/* Success */
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 23:43:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
pthread_cleanup_pop (0);
|
|
|
|
#else
|
|
|
|
unsigned int v;
|
|
|
|
|
|
|
|
atomic_fetch_add_acquire (&isem->nwaiters, 1);
|
2018-04-01 23:43:22 +00:00
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
pthread_cleanup_push (__sem_wait_cleanup, isem);
|
|
|
|
|
|
|
|
v = atomic_load_relaxed (&isem->value);
|
|
|
|
do
|
2018-04-01 23:43:22 +00:00
|
|
|
{
|
2020-12-13 15:14:40 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if ((v & SEM_NWAITERS_MASK) != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (!atomic_compare_exchange_weak_release (&isem->value,
|
|
|
|
&v, v | SEM_NWAITERS_MASK));
|
|
|
|
|
|
|
|
if ((v >> SEM_VALUE_SHIFT) == 0)
|
|
|
|
{
|
|
|
|
/* No token, sleep. */
|
|
|
|
if (timeout)
|
|
|
|
err = __lll_abstimed_wait_intr (&isem->value,
|
|
|
|
SEM_NWAITERS_MASK, timeout, flags, clock_id);
|
|
|
|
else
|
|
|
|
err = __lll_wait_intr (&isem->value,
|
|
|
|
SEM_NWAITERS_MASK, flags);
|
|
|
|
|
2022-12-29 23:40:18 +00:00
|
|
|
if (err != 0 && err != KERN_INVALID_ARGUMENT)
|
2020-12-13 15:14:40 +00:00
|
|
|
{
|
|
|
|
/* Error, interruption or timeout, abort. */
|
|
|
|
if (err == KERN_TIMEDOUT)
|
|
|
|
err = ETIMEDOUT;
|
|
|
|
if (err == KERN_INTERRUPTED)
|
|
|
|
err = EINTR;
|
|
|
|
ret = __hurd_fail (err);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Token changed */
|
|
|
|
v = atomic_load_relaxed (&isem->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((v >> SEM_VALUE_SHIFT) == 0);
|
2018-04-01 23:43:22 +00:00
|
|
|
}
|
2020-12-13 15:14:40 +00:00
|
|
|
while (!atomic_compare_exchange_weak_acquire (&isem->value,
|
|
|
|
&v, v - (1 << SEM_VALUE_SHIFT)));
|
2018-04-01 23:43:22 +00:00
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
error:
|
|
|
|
pthread_cleanup_pop (0);
|
2020-06-24 00:18:45 +00:00
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
__sem_wait_32_finish (isem);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LIBC_CANCEL_RESET (cancel_oldtype);
|
2020-06-23 22:43:32 +00:00
|
|
|
|
|
|
|
return ret;
|
2018-04-01 23:43:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-13 15:14:40 +00:00
|
|
|
#if !__HAVE_64B_ATOMICS
|
|
|
|
/* Stop being a registered waiter (non-64b-atomics code only). */
|
|
|
|
static void
|
|
|
|
__sem_wait_32_finish (struct new_sem *isem)
|
|
|
|
{
|
|
|
|
unsigned int wguess = atomic_load_relaxed (&isem->nwaiters);
|
|
|
|
if (wguess == 1)
|
|
|
|
atomic_fetch_and_acquire (&isem->value, ~SEM_NWAITERS_MASK);
|
|
|
|
|
|
|
|
unsigned int wfinal = atomic_fetch_add_release (&isem->nwaiters, -1);
|
|
|
|
if (wfinal > 1 && wguess == 1)
|
|
|
|
{
|
|
|
|
unsigned int v = atomic_fetch_or_relaxed (&isem->value,
|
|
|
|
SEM_NWAITERS_MASK);
|
|
|
|
v >>= SEM_VALUE_SHIFT;
|
|
|
|
while (v--)
|
|
|
|
__lll_wake (&isem->value, isem->pshared ? GSYNC_SHARED : 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-06-07 01:12:36 +00:00
|
|
|
int
|
|
|
|
__sem_clockwait (sem_t *sem, clockid_t clockid,
|
|
|
|
const struct timespec *restrict timeout)
|
|
|
|
{
|
|
|
|
return __sem_timedwait_internal (sem, clockid, timeout);
|
|
|
|
}
|
|
|
|
weak_alias (__sem_clockwait, sem_clockwait);
|
|
|
|
|
2018-04-01 23:43:22 +00:00
|
|
|
int
|
|
|
|
__sem_timedwait (sem_t *restrict sem, const struct timespec *restrict timeout)
|
|
|
|
{
|
2020-06-07 01:12:36 +00:00
|
|
|
return __sem_timedwait_internal (sem, CLOCK_REALTIME, timeout);
|
2018-04-01 23:43:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
weak_alias (__sem_timedwait, sem_timedwait);
|