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 static long timeout_ns = 100000;	/* 100us default timeout */
31 
32 void usage(char *prog)
33 {
34 	printf("Usage: %s\n", prog);
35 	printf("  -c	Use color\n");
36 	printf("  -h	Display this help message\n");
37 	printf("  -t N	Timeout in nanoseconds (default: 100,000)\n");
38 	printf("  -v L	Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
39 	       VQUIET, VCRITICAL, VINFO);
40 }
41 
42 int main(int argc, char *argv[])
43 {
44 	futex_t f1 = FUTEX_INITIALIZER;
45 	struct timespec to;
46 	int res, ret = RET_PASS;
47 	int c;
48 
49 	while ((c = getopt(argc, argv, "cht:v:")) != -1) {
50 		switch (c) {
51 		case 'c':
52 			log_color(1);
53 			break;
54 		case 'h':
55 			usage(basename(argv[0]));
56 			exit(0);
57 		case 't':
58 			timeout_ns = atoi(optarg);
59 			break;
60 		case 'v':
61 			log_verbosity(atoi(optarg));
62 			break;
63 		default:
64 			usage(basename(argv[0]));
65 			exit(1);
66 		}
67 	}
68 
69 	printf("%s: Block on a futex and wait for timeout\n",
70 	       basename(argv[0]));
71 	printf("\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(ret);
85 	return ret;
86 }
87