xref: /linux/tools/testing/selftests/futex/functional/futex_wait_wouldblock.c (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
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 
31 
32 TEST(futex_wait_wouldblock)
33 {
34 	struct timespec to = {.tv_sec = 0, .tv_nsec = timeout_ns};
35 	futex_t f1 = FUTEX_INITIALIZER;
36 	int res;
37 
38 	TH_LOG("Calling futex_wait on f1: %u @ %p with val=%u", f1, &f1, f1+1);
39 	res = futex_wait(&f1, f1+1, &to, FUTEX_PRIVATE_FLAG);
40 	EXPECT_EQ(res, -1)
41 		TH_LOG("futex_wait returned unexpected result: %d", res);
42 	if (res == -1) {
43 		EXPECT_EQ(errno, EWOULDBLOCK)
44 			TH_LOG("futex_wait returned unexpected errno: %d", errno);
45 	}
46 }
47 
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 (!is_futex_waitv_supported())
61 		SKIP(return, "futex_waitv syscall not supported");
62 
63 	ASSERT_EQ(clock_gettime(CLOCK_MONOTONIC, &to), 0)
64 		TH_LOG("clock_gettime failed");
65 
66 	to.tv_nsec += timeout_ns;
67 
68 	if (to.tv_nsec >= 1000000000) {
69 		to.tv_sec++;
70 		to.tv_nsec -= 1000000000;
71 	}
72 
73 	TH_LOG("Calling futex_waitv on f1: %u @ %p with val=%u", f1, &f1, f1+1);
74 	res = futex_waitv(&waitv, 1, 0, &to, CLOCK_MONOTONIC);
75 	EXPECT_EQ(res, -1)
76 		TH_LOG("futex_waitv returned unexpected result: %d", res);
77 	if (res == -1) {
78 		EXPECT_EQ(errno, EWOULDBLOCK)
79 			TH_LOG("futex_waitv returned unexpected errno: %d", errno);
80 	}
81 }
82 
83 TEST_HARNESS_MAIN
84