• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
4  */
5 
6 #define TRESHOLD_US 100000
7 #define DEFAULT_TIMEOUT_US 100010
8 
verify_futex_wait_bitset(long long wait_us,clock_t clk_id)9 static void verify_futex_wait_bitset(long long wait_us, clock_t clk_id)
10 {
11 	struct timespec start, to, end;
12 	futex_t futex = FUTEX_INITIALIZER;
13 	u_int32_t bitset = 0xffffffff;
14 	int flags = clk_id == CLOCK_REALTIME ? FUTEX_CLOCK_REALTIME : 0;
15 
16 	tst_res(TINFO, "testing futex_wait_bitset() timeout with %s",
17 		clk_id == CLOCK_REALTIME ? "CLOCK_REALTIME" : "CLOCK_MONOTONIC");
18 
19 	clock_gettime(clk_id, &start);
20 	to = tst_timespec_add_us(start, wait_us);
21 
22 	TEST(futex_wait_bitset(&futex, futex, &to, bitset, flags));
23 
24 	clock_gettime(clk_id, &end);
25 
26 	if (TST_RET != -1) {
27 		tst_res(TFAIL, "futex_wait_bitset() returned %li, expected -1",
28 			TST_RET);
29 		return;
30 	}
31 
32 	if (TST_ERR == ENOSYS) {
33 		tst_res(TCONF,
34 			"In this kernel, futex() does not support FUTEX_WAIT_BITSET operation");
35 		return;
36 	}
37 
38 	if (TST_ERR != ETIMEDOUT) {
39 		tst_res(TFAIL | TTERRNO, "expected %s",
40 			tst_strerrno(ETIMEDOUT));
41 		return;
42 	}
43 
44 	if (tst_timespec_lt(end, to)) {
45 		tst_res(TFAIL,
46 			"futex_wait_bitset() woken up prematurely %llius, expected %llius",
47 			tst_timespec_diff_us(end, start), wait_us);
48 		return;
49 	}
50 
51 	if (tst_timespec_diff_us(end, to) > TRESHOLD_US) {
52 		tst_res(TFAIL,
53 			"futex_wait_bitset() waited too long %llius, expected %llius",
54 			tst_timespec_diff_us(end, start), wait_us);
55 		return;
56 	}
57 
58 	tst_res(TPASS, "futex_wait_bitset() waited %llius, expected %llius",
59 		tst_timespec_diff_us(end, start), wait_us);
60 }
61 
setup(void)62 static void setup(void)
63 {
64 	tst_timer_check(USE_CLOCK);
65 }
66 
run(void)67 static void run(void)
68 {
69 	verify_futex_wait_bitset(DEFAULT_TIMEOUT_US, USE_CLOCK);
70 }
71 
72 static struct tst_test test = {
73 	.setup = setup,
74 	.test_all = run,
75 };
76