1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
4 * Copyright © International Business Machines Corp., 2009
7 * Block on a futex and wait for timeout.
16 *****************************************************************************/
19 #include "futextest.h"
20 #include "futex2test.h"
23 #define TEST_NAME "futex-wait-timeout"
25 static long timeout_ns = 100000; /* 100us default timeout */
26 static futex_t futex_pi;
28 void usage(char *prog)
30 printf("Usage: %s\n", prog);
31 printf(" -c Use color\n");
32 printf(" -h Display this help message\n");
33 printf(" -t N Timeout in nanoseconds (default: 100,000)\n");
34 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
35 VQUIET, VCRITICAL, VINFO);
39 * Get a PI lock and hold it forever, so the main thread lock_pi will block
40 * and we can test the timeout
42 void *get_pi_lock(void *arg)
45 volatile futex_t lock = 0;
47 ret = futex_lock_pi(&futex_pi, NULL, 0, 0);
49 error("futex_lock_pi failed\n", ret);
52 ret = futex_wait(&lock, 0, NULL, 0);
53 error("futex_wait failed\n", ret);
59 * Check if the function returned the expected error
61 static void test_timeout(int res, int *ret, char *test_name, int err)
63 if (!res || errno != err) {
64 ksft_test_result_fail("%s returned %d\n", test_name,
65 res < 0 ? errno : res);
68 ksft_test_result_pass("%s succeeds\n", test_name);
73 * Calculate absolute timeout and correct overflow
75 static int futex_get_abs_timeout(clockid_t clockid, struct timespec *to,
78 if (clock_gettime(clockid, to)) {
79 error("clock_gettime failed\n", errno);
83 to->tv_nsec += timeout_ns;
85 if (to->tv_nsec >= 1000000000) {
87 to->tv_nsec -= 1000000000;
93 int main(int argc, char *argv[])
95 futex_t f1 = FUTEX_INITIALIZER;
96 int res, ret = RET_PASS;
100 struct futex_waitv waitv = {
101 .uaddr = (uintptr_t)&f1,
107 while ((c = getopt(argc, argv, "cht:v:")) != -1) {
113 usage(basename(argv[0]));
116 timeout_ns = atoi(optarg);
119 log_verbosity(atoi(optarg));
122 usage(basename(argv[0]));
129 ksft_print_msg("%s: Block on a futex and wait for timeout\n",
131 ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
133 pthread_create(&thread, NULL, get_pi_lock, NULL);
135 /* initialize relative timeout */
137 to.tv_nsec = timeout_ns;
139 res = futex_wait(&f1, f1, &to, 0);
140 test_timeout(res, &ret, "futex_wait relative", ETIMEDOUT);
142 /* FUTEX_WAIT_BITSET with CLOCK_REALTIME */
143 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
145 res = futex_wait_bitset(&f1, f1, &to, 1, FUTEX_CLOCK_REALTIME);
146 test_timeout(res, &ret, "futex_wait_bitset realtime", ETIMEDOUT);
148 /* FUTEX_WAIT_BITSET with CLOCK_MONOTONIC */
149 if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
151 res = futex_wait_bitset(&f1, f1, &to, 1, 0);
152 test_timeout(res, &ret, "futex_wait_bitset monotonic", ETIMEDOUT);
154 /* FUTEX_WAIT_REQUEUE_PI with CLOCK_REALTIME */
155 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
157 res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, FUTEX_CLOCK_REALTIME);
158 test_timeout(res, &ret, "futex_wait_requeue_pi realtime", ETIMEDOUT);
160 /* FUTEX_WAIT_REQUEUE_PI with CLOCK_MONOTONIC */
161 if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
163 res = futex_wait_requeue_pi(&f1, f1, &futex_pi, &to, 0);
164 test_timeout(res, &ret, "futex_wait_requeue_pi monotonic", ETIMEDOUT);
167 * FUTEX_LOCK_PI with CLOCK_REALTIME
168 * Due to historical reasons, FUTEX_LOCK_PI supports only realtime
169 * clock, but requires the caller to not set CLOCK_REALTIME flag.
171 * If you call FUTEX_LOCK_PI with a monotonic clock, it'll be
172 * interpreted as a realtime clock, and (unless you mess your machine's
173 * time or your time machine) the monotonic clock value is always
174 * smaller than realtime and the syscall will timeout immediately.
176 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
178 res = futex_lock_pi(&futex_pi, &to, 0, 0);
179 test_timeout(res, &ret, "futex_lock_pi realtime", ETIMEDOUT);
181 /* Test operations that don't support FUTEX_CLOCK_REALTIME */
182 res = futex_lock_pi(&futex_pi, NULL, 0, FUTEX_CLOCK_REALTIME);
183 test_timeout(res, &ret, "futex_lock_pi invalid timeout flag", ENOSYS);
185 /* futex_waitv with CLOCK_MONOTONIC */
186 if (futex_get_abs_timeout(CLOCK_MONOTONIC, &to, timeout_ns))
188 res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
189 test_timeout(res, &ret, "futex_waitv monotonic", ETIMEDOUT);
191 /* futex_waitv with CLOCK_REALTIME */
192 if (futex_get_abs_timeout(CLOCK_REALTIME, &to, timeout_ns))
194 res = futex_waitv(&waitv, 1, 0, &to, CLOCK_REALTIME);
195 test_timeout(res, &ret, "futex_waitv realtime", ETIMEDOUT);