staging: rtl8192u: remove redundant assignment to pointer crypt
[linux/fpc-iii.git] / tools / testing / selftests / futex / functional / futex_requeue_pi_signal_restart.c
blobf8c43ce8fe663ed1a3839e20d1630bac85648eda
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /******************************************************************************
4 * Copyright © International Business Machines Corp., 2006-2008
6 * DESCRIPTION
7 * This test exercises the futex_wait_requeue_pi() signal handling both
8 * before and after the requeue. The first should be restarted by the
9 * kernel. The latter should return EWOULDBLOCK to the waiter.
11 * AUTHORS
12 * Darren Hart <dvhart@linux.intel.com>
14 * HISTORY
15 * 2008-May-5: Initial version by Darren Hart <dvhart@linux.intel.com>
17 *****************************************************************************/
19 #include <errno.h>
20 #include <getopt.h>
21 #include <limits.h>
22 #include <pthread.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "atomic.h"
28 #include "futextest.h"
29 #include "logging.h"
31 #define TEST_NAME "futex-requeue-pi-signal-restart"
32 #define DELAY_US 100
34 futex_t f1 = FUTEX_INITIALIZER;
35 futex_t f2 = FUTEX_INITIALIZER;
36 atomic_t requeued = ATOMIC_INITIALIZER;
38 int waiter_ret = 0;
40 void usage(char *prog)
42 printf("Usage: %s\n", prog);
43 printf(" -c Use color\n");
44 printf(" -h Display this help message\n");
45 printf(" -v L Verbosity level: %d=QUIET %d=CRITICAL %d=INFO\n",
46 VQUIET, VCRITICAL, VINFO);
49 int create_rt_thread(pthread_t *pth, void*(*func)(void *), void *arg,
50 int policy, int prio)
52 struct sched_param schedp;
53 pthread_attr_t attr;
54 int ret;
56 pthread_attr_init(&attr);
57 memset(&schedp, 0, sizeof(schedp));
59 ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
60 if (ret) {
61 error("pthread_attr_setinheritsched\n", ret);
62 return -1;
65 ret = pthread_attr_setschedpolicy(&attr, policy);
66 if (ret) {
67 error("pthread_attr_setschedpolicy\n", ret);
68 return -1;
71 schedp.sched_priority = prio;
72 ret = pthread_attr_setschedparam(&attr, &schedp);
73 if (ret) {
74 error("pthread_attr_setschedparam\n", ret);
75 return -1;
78 ret = pthread_create(pth, &attr, func, arg);
79 if (ret) {
80 error("pthread_create\n", ret);
81 return -1;
83 return 0;
86 void handle_signal(int signo)
88 info("signal received %s requeue\n",
89 requeued.val ? "after" : "prior to");
92 void *waiterfn(void *arg)
94 unsigned int old_val;
95 int res;
97 waiter_ret = RET_PASS;
99 info("Waiter running\n");
100 info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
101 old_val = f1;
102 res = futex_wait_requeue_pi(&f1, old_val, &(f2), NULL,
103 FUTEX_PRIVATE_FLAG);
104 if (!requeued.val || errno != EWOULDBLOCK) {
105 fail("unexpected return from futex_wait_requeue_pi: %d (%s)\n",
106 res, strerror(errno));
107 info("w2:futex: %x\n", f2);
108 if (!res)
109 futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
110 waiter_ret = RET_FAIL;
113 info("Waiter exiting with %d\n", waiter_ret);
114 pthread_exit(NULL);
118 int main(int argc, char *argv[])
120 unsigned int old_val;
121 struct sigaction sa;
122 pthread_t waiter;
123 int c, res, ret = RET_PASS;
125 while ((c = getopt(argc, argv, "chv:")) != -1) {
126 switch (c) {
127 case 'c':
128 log_color(1);
129 break;
130 case 'h':
131 usage(basename(argv[0]));
132 exit(0);
133 case 'v':
134 log_verbosity(atoi(optarg));
135 break;
136 default:
137 usage(basename(argv[0]));
138 exit(1);
142 ksft_print_header();
143 ksft_set_plan(1);
144 ksft_print_msg("%s: Test signal handling during requeue_pi\n",
145 basename(argv[0]));
146 ksft_print_msg("\tArguments: <none>\n");
148 sa.sa_handler = handle_signal;
149 sigemptyset(&sa.sa_mask);
150 sa.sa_flags = 0;
151 if (sigaction(SIGUSR1, &sa, NULL)) {
152 error("sigaction\n", errno);
153 exit(1);
156 info("m1:f2: %x\n", f2);
157 info("Creating waiter\n");
158 res = create_rt_thread(&waiter, waiterfn, NULL, SCHED_FIFO, 1);
159 if (res) {
160 error("Creating waiting thread failed", res);
161 ret = RET_ERROR;
162 goto out;
165 info("Calling FUTEX_LOCK_PI on f2=%x @ %p\n", f2, &f2);
166 info("m2:f2: %x\n", f2);
167 futex_lock_pi(&f2, 0, 0, FUTEX_PRIVATE_FLAG);
168 info("m3:f2: %x\n", f2);
170 while (1) {
172 * signal the waiter before requeue, waiter should automatically
173 * restart futex_wait_requeue_pi() in the kernel. Wait for the
174 * waiter to block on f1 again.
176 info("Issuing SIGUSR1 to waiter\n");
177 pthread_kill(waiter, SIGUSR1);
178 usleep(DELAY_US);
180 info("Requeueing waiter via FUTEX_CMP_REQUEUE_PI\n");
181 old_val = f1;
182 res = futex_cmp_requeue_pi(&f1, old_val, &(f2), 1, 0,
183 FUTEX_PRIVATE_FLAG);
185 * If res is non-zero, we either requeued the waiter or hit an
186 * error, break out and handle it. If it is zero, then the
187 * signal may have hit before the the waiter was blocked on f1.
188 * Try again.
190 if (res > 0) {
191 atomic_set(&requeued, 1);
192 break;
193 } else if (res < 0) {
194 error("FUTEX_CMP_REQUEUE_PI failed\n", errno);
195 ret = RET_ERROR;
196 break;
199 info("m4:f2: %x\n", f2);
202 * Signal the waiter after requeue, waiter should return from
203 * futex_wait_requeue_pi() with EWOULDBLOCK. Join the thread here so the
204 * futex_unlock_pi() can't happen before the signal wakeup is detected
205 * in the kernel.
207 info("Issuing SIGUSR1 to waiter\n");
208 pthread_kill(waiter, SIGUSR1);
209 info("Waiting for waiter to return\n");
210 pthread_join(waiter, NULL);
212 info("Calling FUTEX_UNLOCK_PI on mutex=%x @ %p\n", f2, &f2);
213 futex_unlock_pi(&f2, FUTEX_PRIVATE_FLAG);
214 info("m5:f2: %x\n", f2);
216 out:
217 if (ret == RET_PASS && waiter_ret)
218 ret = waiter_ret;
220 print_result(TEST_NAME, ret);
221 return ret;