1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <errno.h>
24#include <getopt.h>
25#include <limits.h>
26#include <pthread.h>
27#include <signal.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include "atomic.h"
32#include "futextest.h"
33#include "logging.h"
34
35#define DELAY_US 100
36
37futex_t f1 = FUTEX_INITIALIZER;
38futex_t f2 = FUTEX_INITIALIZER;
39atomic_t requeued = ATOMIC_INITIALIZER;
40
41int waiter_ret = 0;
42
43void usage(char *prog)
44{
45 printf("Usage: %s\n", prog);
46 printf(" -c Use color\n");
47 printf(" -h Display this help message\n");
48 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
49 VQUIET, VCRITICAL, VINFO);
50}
51
52int create_rt_thread(pthread_t *pth, void*(*func)(void *), void *arg,
53 int policy, int prio)
54{
55 struct sched_param schedp;
56 pthread_attr_t attr;
57 int ret;
58
59 pthread_attr_init(&attr);
60 memset(&schedp, 0, sizeof(schedp));
61
62 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
63 if (ret) {
64 error("pthread_attr_setinheritsched\n", ret);
65 return -1;
66 }
67
68 ret = pthread_attr_setschedpolicy(&attr, policy);
69 if (ret) {
70 error("pthread_attr_setschedpolicy\n", ret);
71 return -1;
72 }
73
74 schedp.sched_priority = prio;
75 ret = pthread_attr_setschedparam(&attr, &schedp);
76 if (ret) {
77 error("pthread_attr_setschedparam\n", ret);
78 return -1;
79 }
80
81 ret = pthread_create(pth, &attr, func, arg);
82 if (ret) {
83 error("pthread_create\n", ret);
84 return -1;
85 }
86 return 0;
87}
88
89void handle_signal(int signo)
90{
91 info("signal received %s requeue\n",
92 requeued.val ? "after" : "prior to");
93}
94
95void *waiterfn(void *arg)
96{
97 unsigned int old_val;
98 int res;
99
100 waiter_ret = RET_PASS;
101
102 info("Waiter running\n");
103 info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
104 old_val = f1;
105 res = futex_wait_requeue_pi(&f1, old_val, &(f2), NULL,
106 FUTEX_PRIVATE_FLAG);
107 if (!requeued.val || errno != EWOULDBLOCK) {
108 fail("unexpected return from futex_wait_requeue_pi: %d (%s)\n",
109 res, strerror(errno));
110 info("w2:futex: %x\n", f2);
111 if (!res)
112 futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
113 waiter_ret = RET_FAIL;
114 }
115
116 info("Waiter exiting with %d\n", waiter_ret);
117 pthread_exit(NULL);
118}
119
120
121int main(int argc, char *argv[])
122{
123 unsigned int old_val;
124 struct sigaction sa;
125 pthread_t waiter;
126 int c, res, ret = RET_PASS;
127
128 while ((c = getopt(argc, argv, "chv:")) != -1) {
129 switch (c) {
130 case 'c':
131 log_color(1);
132 break;
133 case 'h':
134 usage(basename(argv[0]));
135 exit(0);
136 case 'v':
137 log_verbosity(atoi(optarg));
138 break;
139 default:
140 usage(basename(argv[0]));
141 exit(1);
142 }
143 }
144
145 printf("%s: Test signal handling during requeue_pi\n",
146 basename(argv[0]));
147 printf("\tArguments: <none>\n");
148
149 sa.sa_handler = handle_signal;
150 sigemptyset(&sa.sa_mask);
151 sa.sa_flags = 0;
152 if (sigaction(SIGUSR1, &sa, NULL)) {
153 error("sigaction\n", errno);
154 exit(1);
155 }
156
157 info("m1:f2: %x\n", f2);
158 info("Creating waiter\n");
159 res = create_rt_thread(&waiter, waiterfn, NULL, SCHED_FIFO, 1);
160 if (res) {
161 error("Creating waiting thread failed", res);
162 ret = RET_ERROR;
163 goto out;
164 }
165
166 info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
167 info("m2:f2: %x\n", f2);
168 futex_lock_pi(&f2, 0, 0, FUTEX_PRIVATE_FLAG);
169 info("m3:f2: %x\n", f2);
170
171 while (1) {
172
173
174
175
176
177 info("Issuing SIGUSR1 to waiter\n");
178 pthread_kill(waiter, SIGUSR1);
179 usleep(DELAY_US);
180
181 info("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n");
182 old_val = f1;
183 res = futex_cmp_requeue_pi(&f1, old_val, &(f2), 1, 0,
184 FUTEX_PRIVATE_FLAG);
185
186
187
188
189
190
191 if (res > 0) {
192 atomic_set(&requeued, 1);
193 break;
194 } else if (res < 0) {
195 error("FUTEX_CMP_REQUEUE_PI failed\n", errno);
196 ret = RET_ERROR;
197 break;
198 }
199 }
200 info("m4:f2: %x\n", f2);
201
202
203
204
205
206
207
208 info("Issuing SIGUSR1 to waiter\n");
209 pthread_kill(waiter, SIGUSR1);
210 info("Waiting for waiter to return\n");
211 pthread_join(waiter, NULL);
212
213 info("Calling FUTEX_UNLOCK_PI on mutex=%x @ %p\n", f2, &f2);
214 futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
215 info("m5:f2: %x\n", f2);
216
217 out:
218 if (ret == RET_PASS && waiter_ret)
219 ret = waiter_ret;
220
221 print_result(ret);
222 return ret;
223}
224