1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright Collabora Ltd., 2021
13 #include "futextest.h"
15 #define TEST_NAME "futex-wait"
16 #define timeout_ns 30000000
17 #define WAKE_WAIT_US 10000
18 #define SHM_PATH "futex_shm_file"
22 void usage(char *prog)
24 printf("Usage: %s\n", prog);
25 printf(" -c Use color\n");
26 printf(" -h Display this help message\n");
27 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
28 VQUIET, VCRITICAL, VINFO);
31 static void *waiterfn(void *arg)
34 unsigned int flags = 0;
37 flags = *((unsigned int *) arg);
40 to.tv_nsec = timeout_ns;
42 if (futex_wait(futex, 0, &to, flags))
43 printf("waiter failed errno %d\n", errno);
48 int main(int argc, char *argv[])
50 int res, ret = RET_PASS, fd, c, shm_id;
51 u_int32_t f_private = 0, *shared_data;
52 unsigned int flags = FUTEX_PRIVATE_FLAG;
58 while ((c = getopt(argc, argv, "cht:v:")) != -1) {
64 usage(basename(argv[0]));
67 log_verbosity(atoi(optarg));
70 usage(basename(argv[0]));
77 ksft_print_msg("%s: Test futex_wait\n", basename(argv[0]));
79 /* Testing a private futex */
80 info("Calling private futex_wait on futex: %p\n", futex);
81 if (pthread_create(&waiter, NULL, waiterfn, (void *) &flags))
82 error("pthread_create failed\n", errno);
86 info("Calling private futex_wake on futex: %p\n", futex);
87 res = futex_wake(futex, 1, FUTEX_PRIVATE_FLAG);
89 ksft_test_result_fail("futex_wake private returned: %d %s\n",
90 errno, strerror(errno));
93 ksft_test_result_pass("futex_wake private succeeds\n");
96 /* Testing an anon page shared memory */
97 shm_id = shmget(IPC_PRIVATE, 4096, IPC_CREAT | 0666);
103 shared_data = shmat(shm_id, NULL, 0);
108 info("Calling shared (page anon) futex_wait on futex: %p\n", futex);
109 if (pthread_create(&waiter, NULL, waiterfn, NULL))
110 error("pthread_create failed\n", errno);
112 usleep(WAKE_WAIT_US);
114 info("Calling shared (page anon) futex_wake on futex: %p\n", futex);
115 res = futex_wake(futex, 1, 0);
117 ksft_test_result_fail("futex_wake shared (page anon) returned: %d %s\n",
118 errno, strerror(errno));
121 ksft_test_result_pass("futex_wake shared (page anon) succeeds\n");
125 /* Testing a file backed shared memory */
126 fd = open(SHM_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
132 if (ftruncate(fd, sizeof(f_private))) {
137 shm = mmap(NULL, sizeof(f_private), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
138 if (shm == MAP_FAILED) {
143 memcpy(shm, &f_private, sizeof(f_private));
147 info("Calling shared (file backed) futex_wait on futex: %p\n", futex);
148 if (pthread_create(&waiter, NULL, waiterfn, NULL))
149 error("pthread_create failed\n", errno);
151 usleep(WAKE_WAIT_US);
153 info("Calling shared (file backed) futex_wake on futex: %p\n", futex);
154 res = futex_wake(shm, 1, 0);
156 ksft_test_result_fail("futex_wake shared (file backed) returned: %d %s\n",
157 errno, strerror(errno));
160 ksft_test_result_pass("futex_wake shared (file backed) succeeds\n");
163 /* Freeing resources */
165 munmap(shm, sizeof(f_private));