1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright Collabora Ltd., 2021
11 #include "futextest.h"
13 #define TEST_NAME "futex-requeue"
14 #define timeout_ns 30000000
15 #define WAKE_WAIT_US 10000
19 void usage(char *prog)
21 printf("Usage: %s\n", prog);
22 printf(" -c Use color\n");
23 printf(" -h Display this help message\n");
24 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
25 VQUIET, VCRITICAL, VINFO);
28 void *waiterfn(void *arg)
33 to.tv_nsec = timeout_ns;
35 if (futex_wait(f1, *f1, &to, 0))
36 printf("waiter failed errno %d\n", errno);
41 int main(int argc, char *argv[])
44 int res, ret = RET_PASS;
46 volatile futex_t _f1 = 0;
47 volatile futex_t f2 = 0;
51 while ((c = getopt(argc, argv, "cht:v:")) != -1) {
57 usage(basename(argv[0]));
60 log_verbosity(atoi(optarg));
63 usage(basename(argv[0]));
70 ksft_print_msg("%s: Test futex_requeue\n",
74 * Requeue a waiter from f1 to f2, and wake f2.
76 if (pthread_create(&waiter[0], NULL, waiterfn, NULL))
77 error("pthread_create failed\n", errno);
81 info("Requeuing 1 futex from f1 to f2\n");
82 res = futex_cmp_requeue(f1, 0, &f2, 0, 1, 0);
84 ksft_test_result_fail("futex_requeue simple returned: %d %s\n",
86 res ? strerror(errno) : "");
91 info("Waking 1 futex at f2\n");
92 res = futex_wake(&f2, 1, 0);
94 ksft_test_result_fail("futex_requeue simple returned: %d %s\n",
96 res ? strerror(errno) : "");
99 ksft_test_result_pass("futex_requeue simple succeeds\n");
104 * Create 10 waiters at f1. At futex_requeue, wake 3 and requeue 7.
105 * At futex_wake, wake INT_MAX (should be exactly 7).
107 for (i = 0; i < 10; i++) {
108 if (pthread_create(&waiter[i], NULL, waiterfn, NULL))
109 error("pthread_create failed\n", errno);
112 usleep(WAKE_WAIT_US);
114 info("Waking 3 futexes at f1 and requeuing 7 futexes from f1 to f2\n");
115 res = futex_cmp_requeue(f1, 0, &f2, 3, 7, 0);
117 ksft_test_result_fail("futex_requeue many returned: %d %s\n",
119 res ? strerror(errno) : "");
123 info("Waking INT_MAX futexes at f2\n");
124 res = futex_wake(&f2, INT_MAX, 0);
126 ksft_test_result_fail("futex_requeue many returned: %d %s\n",
128 res ? strerror(errno) : "");
131 ksft_test_result_pass("futex_requeue many succeeds\n");