1 /******************************************************************************
3 * Copyright © International Business Machines Corp., 2006-2008
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.
11 * This test exercises the futex_wait_requeue_pi() signal handling both
12 * before and after the requeue. The first should be restarted by the
13 * kernel. The latter should return EWOULDBLOCK to the waiter.
16 * Darren Hart <dvhart@linux.intel.com>
19 * 2008-May-5: Initial version by Darren Hart <dvhart@linux.intel.com>
21 *****************************************************************************/
32 #include "futextest.h"
37 futex_t f1
= FUTEX_INITIALIZER
;
38 futex_t f2
= FUTEX_INITIALIZER
;
39 atomic_t requeued
= ATOMIC_INITIALIZER
;
43 void usage(char *prog
)
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
);
52 int create_rt_thread(pthread_t
*pth
, void*(*func
)(void *), void *arg
,
55 struct sched_param schedp
;
59 pthread_attr_init(&attr
);
60 memset(&schedp
, 0, sizeof(schedp
));
62 ret
= pthread_attr_setinheritsched(&attr
, PTHREAD_EXPLICIT_SCHED
);
64 error("pthread_attr_setinheritsched\n", ret
);
68 ret
= pthread_attr_setschedpolicy(&attr
, policy
);
70 error("pthread_attr_setschedpolicy\n", ret
);
74 schedp
.sched_priority
= prio
;
75 ret
= pthread_attr_setschedparam(&attr
, &schedp
);
77 error("pthread_attr_setschedparam\n", ret
);
81 ret
= pthread_create(pth
, &attr
, func
, arg
);
83 error("pthread_create\n", ret
);
89 void handle_signal(int signo
)
91 info("signal received %s requeue\n",
92 requeued
.val
? "after" : "prior to");
95 void *waiterfn(void *arg
)
100 waiter_ret
= RET_PASS
;
102 info("Waiter running\n");
103 info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2
, &f2
);
105 res
= futex_wait_requeue_pi(&f1
, old_val
, &(f2
), NULL
,
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
);
112 futex_unlock_pi(&f2
, FUTEX_PRIVATE_FLAG
);
113 waiter_ret
= RET_FAIL
;
116 info("Waiter exiting with %d\n", waiter_ret
);
121 int main(int argc
, char *argv
[])
123 unsigned int old_val
;
126 int c
, res
, ret
= RET_PASS
;
128 while ((c
= getopt(argc
, argv
, "chv:")) != -1) {
134 usage(basename(argv
[0]));
137 log_verbosity(atoi(optarg
));
140 usage(basename(argv
[0]));
145 printf("%s: Test signal handling during requeue_pi\n",
147 printf("\tArguments: <none>\n");
149 sa
.sa_handler
= handle_signal
;
150 sigemptyset(&sa
.sa_mask
);
152 if (sigaction(SIGUSR1
, &sa
, NULL
)) {
153 error("sigaction\n", errno
);
157 info("m1:f2: %x\n", f2
);
158 info("Creating waiter\n");
159 res
= create_rt_thread(&waiter
, waiterfn
, NULL
, SCHED_FIFO
, 1);
161 error("Creating waiting thread failed", res
);
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
);
173 * signal the waiter before requeue, waiter should automatically
174 * restart futex_wait_requeue_pi() in the kernel. Wait for the
175 * waiter to block on f1 again.
177 info("Issuing SIGUSR1 to waiter\n");
178 pthread_kill(waiter
, SIGUSR1
);
181 info("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n");
183 res
= futex_cmp_requeue_pi(&f1
, old_val
, &(f2
), 1, 0,
186 * If res is non-zero, we either requeued the waiter or hit an
187 * error, break out and handle it. If it is zero, then the
188 * signal may have hit before the the waiter was blocked on f1.
192 atomic_set(&requeued
, 1);
194 } else if (res
< 0) {
195 error("FUTEX_CMP_REQUEUE_PI failed\n", errno
);
200 info("m4:f2: %x\n", f2
);
203 * Signal the waiter after requeue, waiter should return from
204 * futex_wait_requeue_pi() with EWOULDBLOCK. Join the thread here so the
205 * futex_unlock_pi() can't happen before the signal wakeup is detected
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
);
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
);
218 if (ret
== RET_PASS
&& waiter_ret
)