glibc/nptl/tst-sem13.c
Mike Crowe 6615f77978 nptl: Add POSIX-proposed sem_clockwait
Add:

 int sem_clockwait (sem_t *sem, clockid_t clock, const struct timespec
*abstime)

which behaves just like sem_timedwait, but measures abstime against the
specified clock. Currently supports CLOCK_REALTIME and CLOCK_MONOTONIC
and sets errno == EINVAL if any other clock is specified.

	* nptl/sem_waitcommon.c (do_futex_wait, __new_sem_wait_slow): Add
	clockid parameters to indicate the clock which abstime should be
	measured against.
	* nptl/sem_timedwait.c (sem_timedwait), nptl/sem_wait.c
	(__new_sem_wait): Pass CLOCK_REALTIME as clockid to
	__new_sem_wait_slow.
	* nptl/sem_clockwait.c: New file to implement sem_clockwait based
	on sem_timedwait.c.
	* nptl/Makefile: Add sem_clockwait.c source file. Add CFLAGS for
	sem_clockwait.c to match those used for sem_timedwait.c.
	* sysdeps/pthread/semaphore.h: Add sem_clockwait.
	* nptl/Versions (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/aarch64/libpthread.abilist (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/alpha/libpthread.abilist (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/arm/libpthread.abilist (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/csky/libpthread.abilist (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/hppa/libpthread.abilist (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/i386/libpthread.abilist (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/ia64/libpthread.abilist (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/m68k/coldfire/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/m68k/m680x0/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/microblaze/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/mips/mips32/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/mips/mips64/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/nios2/libpthread.abilist (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc32/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc64/be/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc64/le/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/riscv/rv64/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/s390/s390-32/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/s390/s390-64/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/sh/libpthread.abilist (GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/sparc/sparc32/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/sparc/sparc64/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/x86_64/64/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* sysdeps/unix/sysv/linux/x86_64/x32/libpthread.abilist
	(GLIBC_2.30): Likewise.
	* nptl/tst-sem17.c: Add new test for passing invalid clock to
	sem_clockwait.
	* nptl/tst-sem13.c, nptl/tst-sem5.c: Modify existing sem_timedwait
	tests to also test sem_clockwait.
	* manual/threads.texi: Document sem_clockwait.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2019-07-12 13:36:23 +00:00

80 lines
1.8 KiB
C

#include <errno.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <internaltypes.h>
#include <support/check.h>
/* A bogus clock value that tells run_test to use sem_timedwait rather than
sem_clockwait. */
#define CLOCK_USE_TIMEDWAIT (-1)
typedef int (*waitfn_t)(sem_t *, struct timespec *);
static void
do_test_wait (waitfn_t waitfn, const char *fnname)
{
union
{
sem_t s;
struct new_sem ns;
} u;
printf ("do_test_wait: %s\n", fnname);
TEST_COMPARE (sem_init (&u.s, 0, 0), 0);
struct timespec ts = { 0, 1000000001 }; /* Invalid. */
errno = 0;
TEST_VERIFY_EXIT (waitfn (&u.s, &ts) < 0);
TEST_COMPARE (errno, EINVAL);
#if __HAVE_64B_ATOMICS
unsigned int nwaiters = (u.ns.data >> SEM_NWAITERS_SHIFT);
#else
unsigned int nwaiters = u.ns.nwaiters;
#endif
TEST_COMPARE (nwaiters, 0);
ts.tv_sec = /* Invalid. */ -2;
ts.tv_nsec = 0;
errno = 0;
TEST_VERIFY_EXIT (waitfn (&u.s, &ts) < 0);
TEST_COMPARE (errno, ETIMEDOUT);
#if __HAVE_64B_ATOMICS
nwaiters = (u.ns.data >> SEM_NWAITERS_SHIFT);
#else
nwaiters = u.ns.nwaiters;
#endif
TEST_COMPARE (nwaiters, 0);
}
int test_sem_timedwait (sem_t *sem, struct timespec *ts)
{
return sem_timedwait (sem, ts);
}
int test_sem_clockwait_monotonic (sem_t *sem, struct timespec *ts)
{
return sem_clockwait (sem, CLOCK_MONOTONIC, ts);
}
int test_sem_clockwait_realtime (sem_t *sem, struct timespec *ts)
{
return sem_clockwait (sem, CLOCK_REALTIME, ts);
}
static int do_test (void)
{
do_test_wait (&test_sem_timedwait,
"sem_timedwait");
do_test_wait (&test_sem_clockwait_monotonic,
"sem_clockwait(monotonic)");
do_test_wait (&test_sem_clockwait_realtime,
"sem_clockwait(realtime)");
return 0;
}
#include <support/test-driver.c>