1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <pthread.h>
19#include "futextest.h"
20#include "logging.h"
21
22#define TEST_NAME "futex-wait-timeout"
23
24static long timeout_ns = 100000;
25static futex_t futex_pi;
26
27void usage(char *prog)
28{
29 printf("Usage: %s\n", prog);
30 printf(" -c Use color\n");
31 printf(" -h Display this help message\n");
32 printf(" -t N Timeout in nanoseconds (default: 100,000)\n");
33 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
34 VQUIET, VCRITICAL, VINFO);
35}
36
37
38
39
40
41void *get_pi_lock(void *arg)
42{
43 int ret;
44 volatile futex_t lock = 0;
45
46 ret = futex_lock_pi(&futex_pi, NULL, 0, 0);
47 if (ret != 0)
48 error("futex_lock_pi failed\n", ret);
49
50
51 ret = futex_wait(&lock, 0, NULL, 0);
52 error("futex_wait failed\n", ret);
53
54 return NULL;
55}
56
57
58
59
60static void test_timeout(int res, int *ret, char *test_name, int err)
61{
62 if (!res || errno != err) {
63 ksft_test_result_fail("%s returned %d\n", test_name,
64 res < 0 ? errno : res);
65 *ret = RET_FAIL;
66 } else {
67 ksft_test_result_pass("%s succeeds\n", test_name);
68 }
69}
70
71
72
73
74static int futex_get_abs_timeout(clockid_t clockid, struct timespec *to,
75 long timeout_ns)
76{
77 if (clock_gettime(clockid, to)) {
78 error("clock_gettime failed\n", errno);
79 return errno;
80 }
81
82 to->tv_nsec += timeout_ns;
83
84 if (to->tv_nsec >= 1000000000) {
85 to->tv_sec++;
86 to->tv_nsec -= 1000000000;
87 }
88
89 return 0;
90}
91
92int main(int argc, char *argv[])
93{
94 futex_t f1 = FUTEX_INITIALIZER;
95 int res, ret = RET_PASS;
96 struct timespec to;
97 pthread_t thread;
98 int c;
99
100 while ((c = getopt(argc, argv, "cht:v:")) != -1) {
101 switch (c) {
102 case 'c':
103 log_color(1);
104 break;
105 case 'h':
106 usage(basename(argv[0]));
107 exit(0);
108 case 't':
109 timeout_ns = atoi(optarg);
110 break;
111 case 'v':
112 log_verbosity(atoi(optarg));
113 break;
114 default:
115 usage(basename(argv[0]));
116 exit(1);
117 }
118 }
119
120 ksft_print_header();
121 ksft_set_plan(7);
122 ksft_print_msg("%s: Block on a futex and wait for timeout\n",
123 basename(argv[0]));
124 ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
125
126 pthread_create(&thread, NULL, get_pi_lock, NULL);
127
128
129 to.tv_sec = 0;
130 to.tv_nsec = timeout_ns;
131
132 res = futex_wait(&f1, f1, &to, 0);
133 test_timeout(res, &ret, "futex_wait relative", ETIMEDOUT);
134
135
136 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
137 return RET_FAIL;
138 res = futex_wait_bitset(&f1, f1, &to, 1, FUTEX_CLOCK_REALTIME);
139 test_timeout(res, &ret, "futex_wait_bitset realtime", ETIMEDOUT);
140
141
142 if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
143 return RET_FAIL;
144 res = futex_wait_bitset(&f1, f1, &to, 1, 0);
145 test_timeout(res, &ret, "futex_wait_bitset monotonic", ETIMEDOUT);
146
147
148 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
149 return RET_FAIL;
150 res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, FUTEX_CLOCK_REALTIME);
151 test_timeout(res, &ret, "futex_wait_requeue_pi realtime", ETIMEDOUT);
152
153
154 if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
155 return RET_FAIL;
156 res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 0);
157 test_timeout(res, &ret, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
158
159
160
161
162
163
164
165
166
167
168
169 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
170 return RET_FAIL;
171 res = futex_lock_pi(&futex_pi, &to, 0, 0);
172 test_timeout(res, &ret, "futex_lock_pi realtime", ETIMEDOUT);
173
174
175 res = futex_lock_pi(&futex_pi, NULL, 0, FUTEX_CLOCK_REALTIME);
176 test_timeout(res, &ret, "futex_lock_pi invalid timeout flag", ENOSYS);
177
178 ksft_print_cnts();
179 return ret;
180}
181