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.
10 * Darren Hart <dvhart@linux.intel.com>
13 * 2009-Nov-6: Initial version by Darren Hart <dvhart@linux.intel.com>
14 * 2021-Apr-26: More test cases by André Almeida <andrealmeid@collabora.com>
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
;
27 static pthread_barrier_t barrier
;
29 void usage(char *prog
)
31 printf("Usage: %s\n", prog
);
32 printf(" -c Use color\n");
33 printf(" -h Display this help message\n");
34 printf(" -t N Timeout in nanoseconds (default: 100,000)\n");
35 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
36 VQUIET
, VCRITICAL
, VINFO
);
40 * Get a PI lock and hold it forever, so the main thread lock_pi will block
41 * and we can test the timeout
43 void *get_pi_lock(void *arg
)
46 volatile futex_t lock
= 0;
48 ret
= futex_lock_pi(&futex_pi
, NULL
, 0, 0);
50 error("futex_lock_pi failed\n", ret
);
52 pthread_barrier_wait(&barrier
);
55 ret
= futex_wait(&lock
, 0, NULL
, 0);
56 error("futex_wait failed\n", ret
);
62 * Check if the function returned the expected error
64 static void test_timeout(int res
, int *ret
, char *test_name
, int err
)
66 if (!res
|| errno
!= err
) {
67 ksft_test_result_fail("%s returned %d\n", test_name
,
68 res
< 0 ? errno
: res
);
71 ksft_test_result_pass("%s succeeds\n", test_name
);
76 * Calculate absolute timeout and correct overflow
78 static int futex_get_abs_timeout(clockid_t clockid
, struct timespec
*to
,
81 if (clock_gettime(clockid
, to
)) {
82 error("clock_gettime failed\n", errno
);
86 to
->tv_nsec
+= timeout_ns
;
88 if (to
->tv_nsec
>= 1000000000) {
90 to
->tv_nsec
-= 1000000000;
96 int main(int argc
, char *argv
[])
98 futex_t f1
= FUTEX_INITIALIZER
;
99 int res
, ret
= RET_PASS
;
103 struct futex_waitv waitv
= {
104 .uaddr
= (uintptr_t)&f1
,
110 while ((c
= getopt(argc
, argv
, "cht:v:")) != -1) {
116 usage(basename(argv
[0]));
119 timeout_ns
= atoi(optarg
);
122 log_verbosity(atoi(optarg
));
125 usage(basename(argv
[0]));
132 ksft_print_msg("%s: Block on a futex and wait for timeout\n",
134 ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns
);
136 pthread_barrier_init(&barrier
, NULL
, 2);
137 pthread_create(&thread
, NULL
, get_pi_lock
, NULL
);
139 /* initialize relative timeout */
141 to
.tv_nsec
= timeout_ns
;
143 res
= futex_wait(&f1
, f1
, &to
, 0);
144 test_timeout(res
, &ret
, "futex_wait relative", ETIMEDOUT
);
146 /* FUTEX_WAIT_BITSET with CLOCK_REALTIME */
147 if (futex_get_abs_timeout(CLOCK_REALTIME
, &to
, timeout_ns
))
149 res
= futex_wait_bitset(&f1
, f1
, &to
, 1, FUTEX_CLOCK_REALTIME
);
150 test_timeout(res
, &ret
, "futex_wait_bitset realtime", ETIMEDOUT
);
152 /* FUTEX_WAIT_BITSET with CLOCK_MONOTONIC */
153 if (futex_get_abs_timeout(CLOCK_MONOTONIC
, &to
, timeout_ns
))
155 res
= futex_wait_bitset(&f1
, f1
, &to
, 1, 0);
156 test_timeout(res
, &ret
, "futex_wait_bitset monotonic", ETIMEDOUT
);
158 /* FUTEX_WAIT_REQUEUE_PI with CLOCK_REALTIME */
159 if (futex_get_abs_timeout(CLOCK_REALTIME
, &to
, timeout_ns
))
161 res
= futex_wait_requeue_pi(&f1
, f1
, &futex_pi
, &to
, FUTEX_CLOCK_REALTIME
);
162 test_timeout(res
, &ret
, "futex_wait_requeue_pi realtime", ETIMEDOUT
);
164 /* FUTEX_WAIT_REQUEUE_PI with CLOCK_MONOTONIC */
165 if (futex_get_abs_timeout(CLOCK_MONOTONIC
, &to
, timeout_ns
))
167 res
= futex_wait_requeue_pi(&f1
, f1
, &futex_pi
, &to
, 0);
168 test_timeout(res
, &ret
, "futex_wait_requeue_pi monotonic", ETIMEDOUT
);
170 /* Wait until the other thread calls futex_lock_pi() */
171 pthread_barrier_wait(&barrier
);
172 pthread_barrier_destroy(&barrier
);
174 * FUTEX_LOCK_PI with CLOCK_REALTIME
175 * Due to historical reasons, FUTEX_LOCK_PI supports only realtime
176 * clock, but requires the caller to not set CLOCK_REALTIME flag.
178 * If you call FUTEX_LOCK_PI with a monotonic clock, it'll be
179 * interpreted as a realtime clock, and (unless you mess your machine's
180 * time or your time machine) the monotonic clock value is always
181 * smaller than realtime and the syscall will timeout immediately.
183 if (futex_get_abs_timeout(CLOCK_REALTIME
, &to
, timeout_ns
))
185 res
= futex_lock_pi(&futex_pi
, &to
, 0, 0);
186 test_timeout(res
, &ret
, "futex_lock_pi realtime", ETIMEDOUT
);
188 /* Test operations that don't support FUTEX_CLOCK_REALTIME */
189 res
= futex_lock_pi(&futex_pi
, NULL
, 0, FUTEX_CLOCK_REALTIME
);
190 test_timeout(res
, &ret
, "futex_lock_pi invalid timeout flag", ENOSYS
);
192 /* futex_waitv with CLOCK_MONOTONIC */
193 if (futex_get_abs_timeout(CLOCK_MONOTONIC
, &to
, timeout_ns
))
195 res
= futex_waitv(&waitv
, 1, 0, &to
, CLOCK_MONOTONIC
);
196 test_timeout(res
, &ret
, "futex_waitv monotonic", ETIMEDOUT
);
198 /* futex_waitv with CLOCK_REALTIME */
199 if (futex_get_abs_timeout(CLOCK_REALTIME
, &to
, timeout_ns
))
201 res
= futex_waitv(&waitv
, 1, 0, &to
, CLOCK_REALTIME
);
202 test_timeout(res
, &ret
, "futex_waitv realtime", ETIMEDOUT
);