1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
3  *
4  *   Copyright © International Business Machines  Corp., 2009
5  *
6  * DESCRIPTION
7  *      Block on a futex and wait for timeout.
8  *
9  * AUTHOR
10  *      Darren Hart <dvhart@linux.intel.com>
11  *
12  * HISTORY
13  *      2009-Nov-6: Initial version by Darren Hart <dvhart@linux.intel.com>
14  *
15  *****************************************************************************/
16 
17 #include <errno.h>
18 #include <getopt.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <time.h>
23 #include "futextest.h"
24 #include "logging.h"
25 
26 #define TEST_NAME "futex-wait-timeout"
27 
28 static long timeout_ns = 100000;	/* 100us default timeout */
29 
30 void usage(char *prog)
31 {
32 	printf("Usage: %s\n", prog);
33 	printf("  -c	Use color\n");
34 	printf("  -h	Display this help message\n");
35 	printf("  -t N	Timeout in nanoseconds (default: 100,000)\n");
36 	printf("  -v L	Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
37 	       VQUIET, VCRITICAL, VINFO);
38 }
39 
40 int main(int argc, char *argv[])
41 {
42 	futex_t f1 = FUTEX_INITIALIZER;
43 	struct timespec to;
44 	int res, ret = RET_PASS;
45 	int c;
46 
47 	while ((c = getopt(argc, argv, "cht:v:")) != -1) {
48 		switch (c) {
49 		case 'c':
50 			log_color(1);
51 			break;
52 		case 'h':
53 			usage(basename(argv[0]));
54 			exit(0);
55 		case 't':
56 			timeout_ns = atoi(optarg);
57 			break;
58 		case 'v':
59 			log_verbosity(atoi(optarg));
60 			break;
61 		default:
62 			usage(basename(argv[0]));
63 			exit(1);
64 		}
65 	}
66 
67 	ksft_print_header();
68 	ksft_set_plan(1);
69 	ksft_print_msg("%s: Block on a futex and wait for timeout\n",
70 	       basename(argv[0]));
71 	ksft_print_msg("\tArguments: timeout=%ldns\n", timeout_ns);
72 
73 	/* initialize timeout */
74 	to.tv_sec = 0;
75 	to.tv_nsec = timeout_ns;
76 
77 	info("Calling futex_wait on f1: %u @ %p\n", f1, &f1);
78 	res = futex_wait(&f1, f1, &to, FUTEX_PRIVATE_FLAG);
79 	if (!res || errno != ETIMEDOUT) {
80 		fail("futex_wait returned %d\n", ret < 0 ? errno : ret);
81 		ret = RET_FAIL;
82 	}
83 
84 	print_result(TEST_NAME, ret);
85 	return ret;
86 }
87