xref: /linux/tools/testing/selftests/futex/functional/futex_wait_wouldblock.c (revision c574fb2ed7c96f87fc0e5295e910e646a7ee4dfa)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
3  *
4  *   Copyright © International Business Machines  Corp., 2009
5  *
6  * DESCRIPTION
7  *      Test if FUTEX_WAIT op returns -EWOULDBLOCK if the futex value differs
8  *      from the expected one.
9  *
10  * AUTHOR
11  *      Gowrishankar <gowrishankar.m@in.ibm.com>
12  *
13  * HISTORY
14  *      2009-Nov-14: Initial version by Gowrishankar <gowrishankar.m@in.ibm.com>
15  *
16  *****************************************************************************/
17 
18 #include <errno.h>
19 #include <getopt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 
25 #include "futextest.h"
26 #include "futex2test.h"
27 #include "../../kselftest_harness.h"
28 
29 #define timeout_ns 100000
30 
TEST(futex_wait_wouldblock)31 TEST(futex_wait_wouldblock)
32 {
33 	struct timespec to = {.tv_sec = 0, .tv_nsec = timeout_ns};
34 	futex_t f1 = FUTEX_INITIALIZER;
35 	int res;
36 
37 	ksft_print_dbg_msg("Calling futex_wait on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
38 	res = futex_wait(&f1, f1+1, &to, FUTEX_PRIVATE_FLAG);
39 	if (!res || errno != EWOULDBLOCK) {
40 		ksft_test_result_fail("futex_wait returned: %d %s\n",
41 				      res ? errno : res,
42 				      res ? strerror(errno) : "");
43 	} else {
44 		ksft_test_result_pass("futex_wait\n");
45 	}
46 }
47 
TEST(futex_waitv_wouldblock)48 TEST(futex_waitv_wouldblock)
49 {
50 	struct timespec to = {.tv_sec = 0, .tv_nsec = timeout_ns};
51 	futex_t f1 = FUTEX_INITIALIZER;
52 	struct futex_waitv waitv = {
53 		.uaddr		= (uintptr_t)&f1,
54 		.val		= f1 + 1,
55 		.flags		= FUTEX_32,
56 		.__reserved	= 0,
57 	};
58 	int res;
59 
60 	if (clock_gettime(CLOCK_MONOTONIC, &to))
61 		ksft_exit_fail_msg("clock_gettime failed %d\n", errno);
62 
63 	to.tv_nsec += timeout_ns;
64 
65 	if (to.tv_nsec >= 1000000000) {
66 		to.tv_sec++;
67 		to.tv_nsec -= 1000000000;
68 	}
69 
70 	ksft_print_dbg_msg("Calling futex_waitv on f1: %u @ %p with val=%u\n", f1, &f1, f1+1);
71 	res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
72 	if (!res || errno != EWOULDBLOCK) {
73 		ksft_test_result_fail("futex_waitv returned: %d %s\n",
74 				      res ? errno : res,
75 				      res ? strerror(errno) : "");
76 	} else {
77 		ksft_test_result_pass("futex_waitv\n");
78 	}
79 }
80 
81 TEST_HARNESS_MAIN
82