Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / testing / selftests / futex / functional / futex_requeue_pi_signal_restart.c
blobf0542a344d95fa9656b325623473b851149f9cd6
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.
10 * DESCRIPTION
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.
15 * AUTHORS
16 * Darren Hart <dvhart@linux.intel.com>
18 * HISTORY
19 * 2008-May-5: Initial version by Darren Hart <dvhart@linux.intel.com>
21 *****************************************************************************/
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"
35 #define TEST_NAME "futex-requeue-pi-signal-restart"
36 #define DELAY_US 100
38 futex_t f1 = FUTEX_INITIALIZER;
39 futex_t f2 = FUTEX_INITIALIZER;
40 atomic_t requeued = ATOMIC_INITIALIZER;
42 int waiter_ret = 0;
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,
54 int policy, int prio)
56 struct sched_param schedp;
57 pthread_attr_t attr;
58 int ret;
60 pthread_attr_init(&attr);
61 memset(&schedp, 0, sizeof(schedp));
63 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
64 if (ret) {
65 error("pthread_attr_setinheritsched\n", ret);
66 return -1;
69 ret = pthread_attr_setschedpolicy(&attr, policy);
70 if (ret) {
71 error("pthread_attr_setschedpolicy\n", ret);
72 return -1;
75 schedp.sched_priority = prio;
76 ret = pthread_attr_setschedparam(&attr, &schedp);
77 if (ret) {
78 error("pthread_attr_setschedparam\n", ret);
79 return -1;
82 ret = pthread_create(pth, &attr, func, arg);
83 if (ret) {
84 error("pthread_create\n", ret);
85 return -1;
87 return 0;
90 void handle_signal(int signo)
92 info("signal received %s requeue\n",
93 requeued.val ? "after" : "prior to");
96 void *waiterfn(void *arg)
98 unsigned int old_val;
99 int res;
101 waiter_ret = RET_PASS;
103 info("Waiter running\n");
104 info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
105 old_val = f1;
106 res = futex_wait_requeue_pi(&f1, old_val, &(f2), NULL,
107 FUTEX_PRIVATE_FLAG);
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);
112 if (!res)
113 futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
114 waiter_ret = RET_FAIL;
117 info("Waiter exiting with %d\n", waiter_ret);
118 pthread_exit(NULL);
122 int main(int argc, char *argv[])
124 unsigned int old_val;
125 struct sigaction sa;
126 pthread_t waiter;
127 int c, res, ret = RET_PASS;
129 while ((c = getopt(argc, argv, "chv:")) != -1) {
130 switch (c) {
131 case 'c':
132 log_color(1);
133 break;
134 case 'h':
135 usage(basename(argv[0]));
136 exit(0);
137 case 'v':
138 log_verbosity(atoi(optarg));
139 break;
140 default:
141 usage(basename(argv[0]));
142 exit(1);
146 ksft_print_header();
147 ksft_print_msg("%s: Test signal handling during requeue_pi\n",
148 basename(argv[0]));
149 ksft_print_msg("\tArguments: <none>\n");
151 sa.sa_handler = handle_signal;
152 sigemptyset(&sa.sa_mask);
153 sa.sa_flags = 0;
154 if (sigaction(SIGUSR1, &sa, NULL)) {
155 error("sigaction\n", errno);
156 exit(1);
159 info("m1:f2: %x\n", f2);
160 info("Creating waiter\n");
161 res = create_rt_thread(&waiter, waiterfn, NULL, SCHED_FIFO, 1);
162 if (res) {
163 error("Creating waiting thread failed", res);
164 ret = RET_ERROR;
165 goto out;
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);
173 while (1) {
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);
181 usleep(DELAY_US);
183 info("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n");
184 old_val = f1;
185 res = futex_cmp_requeue_pi(&f1, old_val, &(f2), 1, 0,
186 FUTEX_PRIVATE_FLAG);
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.
191 * Try again.
193 if (res > 0) {
194 atomic_set(&requeued, 1);
195 break;
196 } else if (res < 0) {
197 error("FUTEX_CMP_REQUEUE_PI failed\n", errno);
198 ret = RET_ERROR;
199 break;
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
208 * in the kernel.
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);
219 out:
220 if (ret == RET_PASS && waiter_ret)
221 ret = waiter_ret;
223 print_result(TEST_NAME, ret);
224 return ret;