1#include <errno.h>
2#include <semaphore.h>
3#include <stdio.h>
4#include <unistd.h>
5#include <pthread.h>
6#include <internaltypes.h>
7#include <support/check.h>
8
9/* A bogus clock value that tells run_test to use sem_timedwait rather than
10 sem_clockwait. */
11#define CLOCK_USE_TIMEDWAIT (-1)
12
13typedef int (*waitfn_t)(sem_t *, struct timespec *);
14
15static void
16do_test_wait (waitfn_t waitfn, const char *fnname)
17{
18 union
19 {
20 sem_t s;
21 struct new_sem ns;
22 } u;
23
24 printf (format: "do_test_wait: %s\n", fnname);
25
26 TEST_COMPARE (sem_init (&u.s, 0, 0), 0);
27
28 struct timespec ts = { 0, 1000000001 }; /* Invalid. */
29 errno = 0;
30 TEST_VERIFY_EXIT (waitfn (&u.s, &ts) < 0);
31 TEST_COMPARE (errno, EINVAL);
32
33#if __HAVE_64B_ATOMICS
34 unsigned int nwaiters = (u.ns.data >> SEM_NWAITERS_SHIFT);
35#else
36 unsigned int nwaiters = u.ns.nwaiters;
37#endif
38 TEST_COMPARE (nwaiters, 0);
39
40 ts.tv_sec = /* Invalid. */ -2;
41 ts.tv_nsec = 0;
42 errno = 0;
43 TEST_VERIFY_EXIT (waitfn (&u.s, &ts) < 0);
44 TEST_COMPARE (errno, ETIMEDOUT);
45#if __HAVE_64B_ATOMICS
46 nwaiters = (u.ns.data >> SEM_NWAITERS_SHIFT);
47#else
48 nwaiters = u.ns.nwaiters;
49#endif
50 TEST_COMPARE (nwaiters, 0);
51}
52
53int test_sem_timedwait (sem_t *sem, struct timespec *ts)
54{
55 return sem_timedwait (sem: sem, abstime: ts);
56}
57
58int test_sem_clockwait_monotonic (sem_t *sem, struct timespec *ts)
59{
60 return sem_clockwait (sem: sem, CLOCK_MONOTONIC, abstime: ts);
61}
62
63int test_sem_clockwait_realtime (sem_t *sem, struct timespec *ts)
64{
65 return sem_clockwait (sem: sem, CLOCK_REALTIME, abstime: ts);
66}
67
68static int do_test (void)
69{
70 do_test_wait (waitfn: &test_sem_timedwait,
71 fnname: "sem_timedwait");
72 do_test_wait (waitfn: &test_sem_clockwait_monotonic,
73 fnname: "sem_clockwait(monotonic)");
74 do_test_wait (waitfn: &test_sem_clockwait_realtime,
75 fnname: "sem_clockwait(realtime)");
76 return 0;
77}
78
79#include <support/test-driver.c>
80

source code of glibc/nptl/tst-sem13.c