completion.h revision 1.12 1 1.12 riastrad /* $NetBSD: completion.h,v 1.12 2021/12/19 12:35:37 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 riastrad * by Taylor R. Campbell.
9 1.1 riastrad *
10 1.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1 riastrad * modification, are permitted provided that the following conditions
12 1.1 riastrad * are met:
13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1 riastrad *
19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 riastrad */
31 1.1 riastrad
32 1.4 riastrad /*
33 1.4 riastrad * Notes on porting:
34 1.4 riastrad *
35 1.4 riastrad * - Linux does not have destroy_completion. You must add it yourself
36 1.4 riastrad * in the appropriate place.
37 1.4 riastrad *
38 1.4 riastrad * - Some Linux code does `completion->done++' or similar. Convert
39 1.4 riastrad * that to complete(completion) and suggest the same change upstream,
40 1.4 riastrad * unless it turns out there actually is a good reason to do that, in
41 1.4 riastrad * which case the Linux completion API should be extended with a
42 1.4 riastrad * sensible name for this that doesn't expose the guts of `struct
43 1.4 riastrad * completion'.
44 1.4 riastrad */
45 1.4 riastrad
46 1.1 riastrad #ifndef _LINUX_COMPLETION_H_
47 1.1 riastrad #define _LINUX_COMPLETION_H_
48 1.1 riastrad
49 1.1 riastrad #include <sys/types.h>
50 1.9 riastrad #include <sys/param.h>
51 1.10 riastrad #include <sys/kernel.h>
52 1.1 riastrad #include <sys/condvar.h>
53 1.1 riastrad #include <sys/mutex.h>
54 1.1 riastrad
55 1.1 riastrad #include <machine/limits.h>
56 1.1 riastrad
57 1.1 riastrad #include <linux/errno.h>
58 1.1 riastrad
59 1.1 riastrad struct completion {
60 1.1 riastrad kmutex_t c_lock;
61 1.1 riastrad kcondvar_t c_cv;
62 1.1 riastrad
63 1.1 riastrad /*
64 1.1 riastrad * c_done is either
65 1.1 riastrad *
66 1.1 riastrad * . -1, meaning it's open season and we're done for good and
67 1.1 riastrad * nobody need wait any more;
68 1.1 riastrad *
69 1.1 riastrad * . 0, meaning nothing is done, so waiters must block; or
70 1.1 riastrad *
71 1.1 riastrad * . a positive integer, meaning that many waiters can
72 1.1 riastrad * proceed before further waiters must block.
73 1.1 riastrad *
74 1.1 riastrad * Negative values other than -1 are not allowed.
75 1.1 riastrad */
76 1.1 riastrad int c_done;
77 1.1 riastrad };
78 1.1 riastrad
79 1.1 riastrad /*
80 1.1 riastrad * Initialize a new completion object.
81 1.1 riastrad */
82 1.1 riastrad static inline void
83 1.1 riastrad init_completion(struct completion *completion)
84 1.1 riastrad {
85 1.1 riastrad
86 1.5 jmcneill mutex_init(&completion->c_lock, MUTEX_DEFAULT, IPL_SCHED);
87 1.1 riastrad cv_init(&completion->c_cv, "lnxcmplt");
88 1.1 riastrad completion->c_done = 0;
89 1.1 riastrad }
90 1.1 riastrad
91 1.1 riastrad /*
92 1.6 skrll * re-initialize a completion object.
93 1.6 skrll */
94 1.6 skrll static inline void
95 1.6 skrll reinit_completion(struct completion *completion)
96 1.6 skrll {
97 1.6 skrll
98 1.6 skrll completion->c_done = 0;
99 1.6 skrll }
100 1.6 skrll
101 1.6 skrll /*
102 1.1 riastrad * Destroy a completion object.
103 1.1 riastrad */
104 1.1 riastrad static inline void
105 1.1 riastrad destroy_completion(struct completion *completion)
106 1.1 riastrad {
107 1.1 riastrad KASSERT(!cv_has_waiters(&completion->c_cv));
108 1.1 riastrad cv_destroy(&completion->c_cv);
109 1.1 riastrad mutex_destroy(&completion->c_lock);
110 1.1 riastrad }
111 1.1 riastrad
112 1.1 riastrad /*
113 1.1 riastrad * Notify one waiter of completion, but not any future ones.
114 1.1 riastrad */
115 1.1 riastrad static inline void
116 1.1 riastrad complete(struct completion *completion)
117 1.1 riastrad {
118 1.1 riastrad
119 1.1 riastrad mutex_enter(&completion->c_lock);
120 1.1 riastrad
121 1.1 riastrad /* If it's not open season, wake one waiter. */
122 1.1 riastrad if (completion->c_done >= 0) {
123 1.1 riastrad KASSERT(completion->c_done < INT_MAX); /* XXX check */
124 1.1 riastrad completion->c_done++;
125 1.1 riastrad cv_signal(&completion->c_cv);
126 1.1 riastrad } else {
127 1.1 riastrad KASSERT(completion->c_done == -1);
128 1.1 riastrad }
129 1.1 riastrad
130 1.1 riastrad mutex_exit(&completion->c_lock);
131 1.1 riastrad }
132 1.1 riastrad
133 1.1 riastrad /*
134 1.1 riastrad * Notify all waiters, present and future (until INIT_COMPLETION), of
135 1.1 riastrad * completion.
136 1.1 riastrad */
137 1.1 riastrad static inline void
138 1.1 riastrad complete_all(struct completion *completion)
139 1.1 riastrad {
140 1.1 riastrad
141 1.1 riastrad mutex_enter(&completion->c_lock);
142 1.1 riastrad
143 1.1 riastrad /* If it's not open season, make it open season and wake everyone. */
144 1.1 riastrad if (completion->c_done >= 0) {
145 1.1 riastrad completion->c_done = -1;
146 1.1 riastrad cv_broadcast(&completion->c_cv);
147 1.1 riastrad } else {
148 1.1 riastrad KASSERT(completion->c_done == -1);
149 1.1 riastrad }
150 1.1 riastrad
151 1.1 riastrad mutex_exit(&completion->c_lock);
152 1.1 riastrad }
153 1.1 riastrad
154 1.1 riastrad /*
155 1.1 riastrad * Reverse the effect of complete_all so that subsequent waiters block
156 1.1 riastrad * until someone calls complete or complete_all.
157 1.1 riastrad *
158 1.1 riastrad * This operation is very different from its lowercase counterpart.
159 1.1 riastrad *
160 1.1 riastrad * For some reason this works on the completion object itself, not on a
161 1.1 riastrad * pointer thereto, so it must be a macro.
162 1.1 riastrad */
163 1.1 riastrad #define INIT_COMPLETION(COMPLETION) INIT_COMPLETION_blorp(&(COMPLETION))
164 1.1 riastrad
165 1.1 riastrad static inline void
166 1.1 riastrad INIT_COMPLETION_blorp(struct completion *completion)
167 1.1 riastrad {
168 1.1 riastrad
169 1.1 riastrad mutex_enter(&completion->c_lock);
170 1.1 riastrad completion->c_done = 0;
171 1.1 riastrad /* No notify -- waiters are interested only in nonzero values. */
172 1.1 riastrad mutex_exit(&completion->c_lock);
173 1.1 riastrad }
174 1.1 riastrad
175 1.1 riastrad static inline void
176 1.1 riastrad _completion_claim(struct completion *completion)
177 1.1 riastrad {
178 1.1 riastrad
179 1.1 riastrad KASSERT(mutex_owned(&completion->c_lock));
180 1.2 riastrad KASSERT(completion->c_done != 0);
181 1.1 riastrad if (completion->c_done > 0)
182 1.1 riastrad completion->c_done--;
183 1.1 riastrad else
184 1.1 riastrad KASSERT(completion->c_done == -1);
185 1.1 riastrad }
186 1.1 riastrad
187 1.1 riastrad /*
188 1.1 riastrad * Wait interruptibly with a timeout for someone to call complete or
189 1.1 riastrad * complete_all.
190 1.1 riastrad */
191 1.1 riastrad static inline int
192 1.1 riastrad wait_for_completion_interruptible_timeout(struct completion *completion,
193 1.1 riastrad unsigned long ticks)
194 1.1 riastrad {
195 1.1 riastrad /* XXX Arithmetic overflow...? */
196 1.7 maxv unsigned int start = getticks(), now;
197 1.1 riastrad int error;
198 1.1 riastrad
199 1.1 riastrad mutex_enter(&completion->c_lock);
200 1.1 riastrad
201 1.12 riastrad /* Wait until c_done is nonzero, timeout, or signal. */
202 1.1 riastrad while (completion->c_done == 0) {
203 1.12 riastrad if (ticks == 0) {
204 1.12 riastrad error = EWOULDBLOCK;
205 1.12 riastrad goto out;
206 1.12 riastrad }
207 1.1 riastrad error = cv_timedwait_sig(&completion->c_cv,
208 1.12 riastrad &completion->c_lock, MIN(ticks, INT_MAX/2));
209 1.12 riastrad now = getticks();
210 1.1 riastrad if (error)
211 1.1 riastrad goto out;
212 1.12 riastrad ticks -= MIN(ticks, (now - start));
213 1.1 riastrad start = now;
214 1.1 riastrad }
215 1.1 riastrad
216 1.1 riastrad /* Success! */
217 1.1 riastrad _completion_claim(completion);
218 1.1 riastrad error = 0;
219 1.1 riastrad
220 1.1 riastrad out: mutex_exit(&completion->c_lock);
221 1.1 riastrad if (error == EWOULDBLOCK) {
222 1.1 riastrad return 0;
223 1.1 riastrad } else if ((error == EINTR) || (error == ERESTART)) {
224 1.1 riastrad return -ERESTARTSYS;
225 1.1 riastrad } else {
226 1.1 riastrad KASSERTMSG((error == 0), "error = %d", error);
227 1.12 riastrad return MAX(1, MIN(ticks, INT_MAX/2));
228 1.1 riastrad }
229 1.1 riastrad }
230 1.1 riastrad
231 1.8 riastrad static inline int
232 1.8 riastrad wait_for_completion_timeout(struct completion *completion, unsigned long ticks)
233 1.8 riastrad {
234 1.8 riastrad /* XXX Arithmetic overflow...? */
235 1.12 riastrad unsigned int start = getticks(), now;
236 1.8 riastrad int error;
237 1.8 riastrad
238 1.8 riastrad mutex_enter(&completion->c_lock);
239 1.8 riastrad
240 1.12 riastrad /* Wait until c_done is nonzero or timeout. */
241 1.8 riastrad while (completion->c_done == 0) {
242 1.12 riastrad if (ticks == 0) {
243 1.12 riastrad error = EWOULDBLOCK;
244 1.12 riastrad goto out;
245 1.12 riastrad }
246 1.8 riastrad error = cv_timedwait(&completion->c_cv, &completion->c_lock,
247 1.12 riastrad MIN(ticks, INT_MAX/2));
248 1.12 riastrad now = getticks();
249 1.8 riastrad if (error)
250 1.8 riastrad goto out;
251 1.12 riastrad ticks -= MIN(ticks, (now - start));
252 1.8 riastrad start = now;
253 1.8 riastrad }
254 1.8 riastrad
255 1.8 riastrad /* Success! */
256 1.8 riastrad _completion_claim(completion);
257 1.8 riastrad error = 0;
258 1.8 riastrad
259 1.8 riastrad out: mutex_exit(&completion->c_lock);
260 1.8 riastrad if (error == EWOULDBLOCK) {
261 1.8 riastrad return 0;
262 1.8 riastrad } else {
263 1.8 riastrad KASSERTMSG((error == 0), "error = %d", error);
264 1.12 riastrad return MAX(1, MIN(ticks, INT_MAX/2));
265 1.8 riastrad }
266 1.8 riastrad }
267 1.8 riastrad
268 1.1 riastrad /*
269 1.1 riastrad * Wait interruptibly for someone to call complete or complete_all.
270 1.1 riastrad */
271 1.1 riastrad static inline int
272 1.1 riastrad wait_for_completion_interruptible(struct completion *completion)
273 1.1 riastrad {
274 1.1 riastrad int error;
275 1.1 riastrad
276 1.1 riastrad mutex_enter(&completion->c_lock);
277 1.1 riastrad
278 1.12 riastrad /* Wait until c_done is nonzero or signal. */
279 1.1 riastrad while (completion->c_done == 0) {
280 1.1 riastrad error = cv_wait_sig(&completion->c_cv, &completion->c_lock);
281 1.1 riastrad if (error)
282 1.1 riastrad goto out;
283 1.1 riastrad }
284 1.1 riastrad
285 1.1 riastrad /* Success! */
286 1.1 riastrad _completion_claim(completion);
287 1.1 riastrad error = 0;
288 1.1 riastrad
289 1.1 riastrad out: mutex_exit(&completion->c_lock);
290 1.12 riastrad if ((error == EINTR) || (error == ERESTART)) {
291 1.12 riastrad return -ERESTARTSYS;
292 1.12 riastrad } else {
293 1.12 riastrad KASSERTMSG((error == 0), "error = %d", error);
294 1.12 riastrad return 0;
295 1.12 riastrad }
296 1.1 riastrad }
297 1.1 riastrad
298 1.1 riastrad /*
299 1.1 riastrad * Wait uninterruptibly, except by SIGKILL, for someone to call
300 1.1 riastrad * complete or complete_all.
301 1.1 riastrad *
302 1.1 riastrad * XXX In this implementation, any signal will actually wake us, not
303 1.1 riastrad * just SIGKILL.
304 1.1 riastrad */
305 1.1 riastrad static inline int
306 1.1 riastrad wait_for_completion_killable(struct completion *completion)
307 1.1 riastrad {
308 1.1 riastrad
309 1.1 riastrad return wait_for_completion_interruptible(completion);
310 1.1 riastrad }
311 1.1 riastrad
312 1.11 riastrad static inline void
313 1.11 riastrad wait_for_completion(struct completion *completion)
314 1.11 riastrad {
315 1.11 riastrad
316 1.11 riastrad mutex_enter(&completion->c_lock);
317 1.11 riastrad while (completion->c_done == 0)
318 1.11 riastrad cv_wait(&completion->c_cv, &completion->c_lock);
319 1.11 riastrad _completion_claim(completion);
320 1.11 riastrad mutex_exit(&completion->c_lock);
321 1.11 riastrad }
322 1.11 riastrad
323 1.1 riastrad /*
324 1.1 riastrad * Try to claim a completion immediately. Return true on success, false
325 1.1 riastrad * if it would block.
326 1.1 riastrad */
327 1.1 riastrad static inline bool
328 1.1 riastrad try_wait_for_completion(struct completion *completion)
329 1.1 riastrad {
330 1.1 riastrad bool ok;
331 1.1 riastrad
332 1.1 riastrad mutex_enter(&completion->c_lock);
333 1.1 riastrad if (completion->c_done == 0) {
334 1.1 riastrad ok = false;
335 1.1 riastrad } else {
336 1.1 riastrad _completion_claim(completion);
337 1.1 riastrad ok = true;
338 1.1 riastrad }
339 1.1 riastrad mutex_exit(&completion->c_lock);
340 1.1 riastrad
341 1.1 riastrad return ok;
342 1.1 riastrad }
343 1.1 riastrad
344 1.1 riastrad #endif /* _LINUX_COMPLETION_H_ */
345