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