1 /*
2 * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
3 *
4 * Licensed under the GNU GPLv2 or later.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 /*
20 * 1. Block on a futex and wait for timeout.
21 * 2. Check that the futex waited for expected time.
22 */
23
24 #include <errno.h>
25
26 #include "test.h"
27 #include "futextest.h"
28
29 #define TRESHOLD_US 100000
30
31 const char *TCID="futex_wait05";
32 const int TST_TOTAL=1;
33
verify_futex_wait(clock_t clk_id,int fflags)34 static void verify_futex_wait(clock_t clk_id, int fflags)
35 {
36 struct timespec to = {.tv_sec = 0, .tv_nsec = 100010000};
37 futex_t futex = FUTEX_INITIALIZER;
38
39 tst_timer_start(clk_id);
40 TEST(futex_wait(&futex, futex, &to, fflags));
41 tst_timer_stop();
42
43 if (TEST_RETURN != -1) {
44 tst_resm(TFAIL, "futex_wait() returned %li, expected -1",
45 TEST_RETURN);
46 return;
47 }
48
49 if (TEST_ERRNO != ETIMEDOUT) {
50
51 tst_resm(TFAIL | TTERRNO, "expected errno=%s",
52 tst_strerrno(ETIMEDOUT));
53 return;
54 }
55
56 if (tst_timespec_lt(tst_timer_elapsed(), to)) {
57 tst_resm(TFAIL,
58 "futex_wait() woken up prematurely %llius, expected %llius",
59 tst_timer_elapsed_us(), tst_timespec_to_us(to));
60 return;
61 }
62
63 if (tst_timespec_diff_us(tst_timer_elapsed(), to) > TRESHOLD_US) {
64 tst_resm(TFAIL,
65 "futex_wait() waited too long %llius, expected %llius",
66 tst_timer_elapsed_us(), tst_timespec_to_us(to));
67 return;
68 }
69
70 tst_resm(TPASS, "futex_wait() waited %llius, expected %llius",
71 tst_timer_elapsed_us(), tst_timespec_to_us(to));
72 }
73
main(int argc,char * argv[])74 int main(int argc, char *argv[])
75 {
76 int lc;
77
78 tst_timer_check(CLOCK_MONOTONIC);
79
80 tst_parse_opts(argc, argv, NULL, NULL);
81
82 for (lc = 0; TEST_LOOPING(lc); lc++)
83 verify_futex_wait(CLOCK_MONOTONIC, 0);
84
85 tst_exit();
86 }
87