Linux: Make generic fcntl.h the default one
[glibc.git] / nptl / tst-sem13.c
blobd7baa2ae582d563b172b3d4e1eca5ff1ba388fb8
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>
9 /* A bogus clock value that tells run_test to use sem_timedwait rather than
10 sem_clockwait. */
11 #define CLOCK_USE_TIMEDWAIT (-1)
13 typedef int (*waitfn_t)(sem_t *, struct timespec *);
15 static void
16 do_test_wait (waitfn_t waitfn, const char *fnname)
18 union
20 sem_t s;
21 struct new_sem ns;
22 } u;
24 printf ("do_test_wait: %s\n", fnname);
26 TEST_COMPARE (sem_init (&u.s, 0, 0), 0);
28 struct timespec ts = { 0, 1000000001 }; /* Invalid. */
29 errno = 0;
30 TEST_VERIFY_EXIT (waitfn (&u.s, &ts) < 0);
31 TEST_COMPARE (errno, EINVAL);
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);
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);
53 int test_sem_timedwait (sem_t *sem, struct timespec *ts)
55 return sem_timedwait (sem, ts);
58 int test_sem_clockwait_monotonic (sem_t *sem, struct timespec *ts)
60 return sem_clockwait (sem, CLOCK_MONOTONIC, ts);
63 int test_sem_clockwait_realtime (sem_t *sem, struct timespec *ts)
65 return sem_clockwait (sem, CLOCK_REALTIME, ts);
68 static int do_test (void)
70 do_test_wait (&test_sem_timedwait,
71 "sem_timedwait");
72 do_test_wait (&test_sem_clockwait_monotonic,
73 "sem_clockwait(monotonic)");
74 do_test_wait (&test_sem_clockwait_realtime,
75 "sem_clockwait(realtime)");
76 return 0;
79 #include <support/test-driver.c>