• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright Collabora Ltd., 2021
4  *
5  * futex cmp requeue test by André Almeida <andrealmeid@collabora.com>
6  */
7 
8 #include <pthread.h>
9 #include <limits.h>
10 #include "logging.h"
11 #include "futextest.h"
12 
13 #define TEST_NAME "futex-requeue"
14 #define timeout_ns  30000000
15 
16 volatile futex_t *f1;
17 static pthread_barrier_t barrier;
18 
usage(char * prog)19 void usage(char *prog)
20 {
21 	printf("Usage: %s\n", prog);
22 	printf("  -c	Use color\n");
23 	printf("  -h	Display this help message\n");
24 	printf("  -v L	Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
25 	       VQUIET, VCRITICAL, VINFO);
26 }
27 
waiterfn(void * arg)28 void *waiterfn(void *arg)
29 {
30 	struct timespec64 to;
31 
32 	to.tv_sec = 0;
33 	to.tv_nsec = timeout_ns;
34 
35 	pthread_barrier_wait(&barrier);
36 
37 	if (futex_wait(f1, *f1, &to, 0))
38 		printf("waiter failed errno %d\n", errno);
39 
40 	return NULL;
41 }
42 
main(int argc,char * argv[])43 int main(int argc, char *argv[])
44 {
45 	pthread_t waiter[10];
46 	int res, ret = RET_PASS;
47 	int c, i;
48 	volatile futex_t _f1 = 0;
49 	volatile futex_t f2 = 0;
50 
51 	f1 = &_f1;
52 
53 	while ((c = getopt(argc, argv, "cht:v:")) != -1) {
54 		switch (c) {
55 		case 'c':
56 			log_color(1);
57 			break;
58 		case 'h':
59 			usage(basename(argv[0]));
60 			exit(0);
61 		case 'v':
62 			log_verbosity(atoi(optarg));
63 			break;
64 		default:
65 			usage(basename(argv[0]));
66 			exit(1);
67 		}
68 	}
69 
70 	ksft_print_header();
71 	ksft_set_plan(2);
72 	ksft_print_msg("%s: Test futex_requeue\n",
73 		       basename(argv[0]));
74 
75 	pthread_barrier_init(&barrier, NULL, 2);
76 	/*
77 	 * Requeue a waiter from f1 to f2, and wake f2.
78 	 */
79 	if (pthread_create(&waiter[0], NULL, waiterfn, NULL))
80 		error("pthread_create failed\n", errno);
81 
82 	pthread_barrier_wait(&barrier);
83 	pthread_barrier_destroy(&barrier);
84 
85 	info("Requeuing 1 futex from f1 to f2\n");
86 	res = futex_cmp_requeue(f1, 0, &f2, 0, 1, 0);
87 	if (res != 1) {
88 		ksft_test_result_fail("futex_requeue simple returned: %d %s\n",
89 				      res ? errno : res,
90 				      res ? strerror(errno) : "");
91 		ret = RET_FAIL;
92 	}
93 
94 
95 	info("Waking 1 futex at f2\n");
96 	res = futex_wake(&f2, 1, 0);
97 	if (res != 1) {
98 		ksft_test_result_fail("futex_requeue simple returned: %d %s\n",
99 				      res ? errno : res,
100 				      res ? strerror(errno) : "");
101 		ret = RET_FAIL;
102 	} else {
103 		ksft_test_result_pass("futex_requeue simple succeeds\n");
104 	}
105 
106 	pthread_barrier_init(&barrier, NULL, 11);
107 
108 	/*
109 	 * Create 10 waiters at f1. At futex_requeue, wake 3 and requeue 7.
110 	 * At futex_wake, wake INT_MAX (should be exactly 7).
111 	 */
112 	for (i = 0; i < 10; i++) {
113 		if (pthread_create(&waiter[i], NULL, waiterfn, NULL))
114 			error("pthread_create failed\n", errno);
115 	}
116 
117 	pthread_barrier_wait(&barrier);
118 	pthread_barrier_destroy(&barrier);
119 
120 	info("Waking 3 futexes at f1 and requeuing 7 futexes from f1 to f2\n");
121 	res = futex_cmp_requeue(f1, 0, &f2, 3, 7, 0);
122 	if (res != 10) {
123 		ksft_test_result_fail("futex_requeue many returned: %d %s\n",
124 				      res ? errno : res,
125 				      res ? strerror(errno) : "");
126 		ret = RET_FAIL;
127 	}
128 
129 	info("Waking INT_MAX futexes at f2\n");
130 	res = futex_wake(&f2, INT_MAX, 0);
131 	if (res != 7) {
132 		ksft_test_result_fail("futex_requeue many returned: %d %s\n",
133 				      res ? errno : res,
134 				      res ? strerror(errno) : "");
135 		ret = RET_FAIL;
136 	} else {
137 		ksft_test_result_pass("futex_requeue many succeeds\n");
138 	}
139 
140 	ksft_print_cnts();
141 	return ret;
142 }
143