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"
35 #define TEST_NAME "futex-requeue-pi-signal-restart"
38 futex_t f1
= FUTEX_INITIALIZER
;
39 futex_t f2
= FUTEX_INITIALIZER
;
40 atomic_t requeued
= ATOMIC_INITIALIZER
;
44 void usage(char *prog
)
46 printf("Usage: %s\n", prog
);
47 printf(" -c Use color\n");
48 printf(" -h Display this help message\n");
49 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
50 VQUIET
, VCRITICAL
, VINFO
);
53 int create_rt_thread(pthread_t
*pth
, void*(*func
)(void *), void *arg
,
56 struct sched_param schedp
;
60 pthread_attr_init(&attr
);
61 memset(&schedp
, 0, sizeof(schedp
));
63 ret
= pthread_attr_setinheritsched(&attr
, PTHREAD_EXPLICIT_SCHED
);
65 error("pthread_attr_setinheritsched\n", ret
);
69 ret
= pthread_attr_setschedpolicy(&attr
, policy
);
71 error("pthread_attr_setschedpolicy\n", ret
);
75 schedp
.sched_priority
= prio
;
76 ret
= pthread_attr_setschedparam(&attr
, &schedp
);
78 error("pthread_attr_setschedparam\n", ret
);
82 ret
= pthread_create(pth
, &attr
, func
, arg
);
84 error("pthread_create\n", ret
);
90 void handle_signal(int signo
)
92 info("signal received %s requeue\n",
93 requeued
.val
? "after" : "prior to");
96 void *waiterfn(void *arg
)
101 waiter_ret
= RET_PASS
;
103 info("Waiter running\n");
104 info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2
, &f2
);
106 res
= futex_wait_requeue_pi(&f1
, old_val
, &(f2
), NULL
,
108 if (!requeued
.val
|| errno
!= EWOULDBLOCK
) {
109 fail("unexpected return from futex_wait_requeue_pi: %d (%s)\n",
110 res
, strerror(errno
));
111 info("w2:futex: %x\n", f2
);
113 futex_unlock_pi(&f2
, FUTEX_PRIVATE_FLAG
);
114 waiter_ret
= RET_FAIL
;
117 info("Waiter exiting with %d\n", waiter_ret
);
122 int main(int argc
, char *argv
[])
124 unsigned int old_val
;
127 int c
, res
, ret
= RET_PASS
;
129 while ((c
= getopt(argc
, argv
, "chv:")) != -1) {
135 usage(basename(argv
[0]));
138 log_verbosity(atoi(optarg
));
141 usage(basename(argv
[0]));
147 ksft_print_msg("%s: Test signal handling during requeue_pi\n",
149 ksft_print_msg("\tArguments: <none>\n");
151 sa
.sa_handler
= handle_signal
;
152 sigemptyset(&sa
.sa_mask
);
154 if (sigaction(SIGUSR1
, &sa
, NULL
)) {
155 error("sigaction\n", errno
);
159 info("m1:f2: %x\n", f2
);
160 info("Creating waiter\n");
161 res
= create_rt_thread(&waiter
, waiterfn
, NULL
, SCHED_FIFO
, 1);
163 error("Creating waiting thread failed", res
);
168 info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2
, &f2
);
169 info("m2:f2: %x\n", f2
);
170 futex_lock_pi(&f2
, 0, 0, FUTEX_PRIVATE_FLAG
);
171 info("m3:f2: %x\n", f2
);
175 * signal the waiter before requeue, waiter should automatically
176 * restart futex_wait_requeue_pi() in the kernel. Wait for the
177 * waiter to block on f1 again.
179 info("Issuing SIGUSR1 to waiter\n");
180 pthread_kill(waiter
, SIGUSR1
);
183 info("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n");
185 res
= futex_cmp_requeue_pi(&f1
, old_val
, &(f2
), 1, 0,
188 * If res is non-zero, we either requeued the waiter or hit an
189 * error, break out and handle it. If it is zero, then the
190 * signal may have hit before the the waiter was blocked on f1.
194 atomic_set(&requeued
, 1);
196 } else if (res
< 0) {
197 error("FUTEX_CMP_REQUEUE_PI failed\n", errno
);
202 info("m4:f2: %x\n", f2
);
205 * Signal the waiter after requeue, waiter should return from
206 * futex_wait_requeue_pi() with EWOULDBLOCK. Join the thread here so the
207 * futex_unlock_pi() can't happen before the signal wakeup is detected
210 info("Issuing SIGUSR1 to waiter\n");
211 pthread_kill(waiter
, SIGUSR1
);
212 info("Waiting for waiter to return\n");
213 pthread_join(waiter
, NULL
);
215 info("Calling FUTEX_UNLOCK_PI on mutex=%x @ %p\n", f2
, &f2
);
216 futex_unlock_pi(&f2
, FUTEX_PRIVATE_FLAG
);
217 info("m5:f2: %x\n", f2
);
220 if (ret
== RET_PASS
&& waiter_ret
)
223 print_result(TEST_NAME
, ret
);