kern_condvar.c revision 1.36 1 /* $NetBSD: kern_condvar.c,v 1.36 2017/06/08 01:09:52 chs Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Kernel condition variable implementation.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.36 2017/06/08 01:09:52 chs Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/lwp.h>
42 #include <sys/condvar.h>
43 #include <sys/sleepq.h>
44 #include <sys/lockdebug.h>
45 #include <sys/cpu.h>
46
47 /*
48 * Accessors for the private contents of the kcondvar_t data type.
49 *
50 * cv_opaque[0] sleepq...
51 * cv_opaque[1] ...pointers
52 * cv_opaque[2] description for ps(1)
53 *
54 * cv_opaque[0..1] is protected by the interlock passed to cv_wait() (enqueue
55 * only), and the sleep queue lock acquired with sleeptab_lookup() (enqueue
56 * and dequeue).
57 *
58 * cv_opaque[2] (the wmesg) is static and does not change throughout the life
59 * of the CV.
60 */
61 #define CV_SLEEPQ(cv) ((sleepq_t *)(cv)->cv_opaque)
62 #define CV_WMESG(cv) ((const char *)(cv)->cv_opaque[2])
63 #define CV_SET_WMESG(cv, v) (cv)->cv_opaque[2] = __UNCONST(v)
64
65 #define CV_DEBUG_P(cv) (CV_WMESG(cv) != nodebug)
66 #define CV_RA ((uintptr_t)__builtin_return_address(0))
67
68 static void cv_unsleep(lwp_t *, bool);
69 static inline void cv_wakeup_one(kcondvar_t *);
70 static inline void cv_wakeup_all(kcondvar_t *);
71
72 static syncobj_t cv_syncobj = {
73 SOBJ_SLEEPQ_SORTED,
74 cv_unsleep,
75 sleepq_changepri,
76 sleepq_lendpri,
77 syncobj_noowner,
78 };
79
80 lockops_t cv_lockops = {
81 "Condition variable",
82 LOCKOPS_CV,
83 NULL
84 };
85
86 static const char deadcv[] = "deadcv";
87 #ifdef LOCKDEBUG
88 static const char nodebug[] = "nodebug";
89
90 #define CV_LOCKDEBUG_HANDOFF(l, cv) cv_lockdebug_handoff(l, cv)
91 #define CV_LOCKDEBUG_PROCESS(l, cv) cv_lockdebug_process(l, cv)
92
93 static inline void
94 cv_lockdebug_handoff(lwp_t *l, kcondvar_t *cv)
95 {
96
97 if (CV_DEBUG_P(cv))
98 l->l_flag |= LW_CVLOCKDEBUG;
99 }
100
101 static inline void
102 cv_lockdebug_process(lwp_t *l, kcondvar_t *cv)
103 {
104
105 if ((l->l_flag & LW_CVLOCKDEBUG) == 0)
106 return;
107
108 l->l_flag &= ~LW_CVLOCKDEBUG;
109 LOCKDEBUG_UNLOCKED(true, cv, CV_RA, 0);
110 }
111 #else
112 #define CV_LOCKDEBUG_HANDOFF(l, cv) __nothing
113 #define CV_LOCKDEBUG_PROCESS(l, cv) __nothing
114 #endif
115
116 /*
117 * cv_init:
118 *
119 * Initialize a condition variable for use.
120 */
121 void
122 cv_init(kcondvar_t *cv, const char *wmesg)
123 {
124 #ifdef LOCKDEBUG
125 bool dodebug;
126
127 dodebug = LOCKDEBUG_ALLOC(cv, &cv_lockops,
128 (uintptr_t)__builtin_return_address(0));
129 if (!dodebug) {
130 /* XXX This will break vfs_lockf. */
131 wmesg = nodebug;
132 }
133 #endif
134 KASSERT(wmesg != NULL);
135 CV_SET_WMESG(cv, wmesg);
136 sleepq_init(CV_SLEEPQ(cv));
137 }
138
139 /*
140 * cv_destroy:
141 *
142 * Tear down a condition variable.
143 */
144 void
145 cv_destroy(kcondvar_t *cv)
146 {
147
148 LOCKDEBUG_FREE(CV_DEBUG_P(cv), cv);
149 #ifdef DIAGNOSTIC
150 KASSERT(cv_is_valid(cv));
151 CV_SET_WMESG(cv, deadcv);
152 #endif
153 }
154
155 /*
156 * cv_enter:
157 *
158 * Look up and lock the sleep queue corresponding to the given
159 * condition variable, and increment the number of waiters.
160 */
161 static inline void
162 cv_enter(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l)
163 {
164 sleepq_t *sq;
165 kmutex_t *mp;
166
167 KASSERT(cv_is_valid(cv));
168 KASSERT(!cpu_intr_p());
169 KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
170
171 LOCKDEBUG_LOCKED(CV_DEBUG_P(cv), cv, mtx, CV_RA, 0);
172
173 l->l_kpriority = true;
174 mp = sleepq_hashlock(cv);
175 sq = CV_SLEEPQ(cv);
176 sleepq_enter(sq, l, mp);
177 sleepq_enqueue(sq, cv, CV_WMESG(cv), &cv_syncobj);
178 mutex_exit(mtx);
179 KASSERT(cv_has_waiters(cv));
180 }
181
182 /*
183 * cv_exit:
184 *
185 * After resuming execution, check to see if we have been restarted
186 * as a result of cv_signal(). If we have, but cannot take the
187 * wakeup (because of eg a pending Unix signal or timeout) then try
188 * to ensure that another LWP sees it. This is necessary because
189 * there may be multiple waiters, and at least one should take the
190 * wakeup if possible.
191 */
192 static inline int
193 cv_exit(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l, const int error)
194 {
195
196 mutex_enter(mtx);
197 if (__predict_false(error != 0))
198 cv_signal(cv);
199
200 LOCKDEBUG_UNLOCKED(CV_DEBUG_P(cv), cv, CV_RA, 0);
201 KASSERT(cv_is_valid(cv));
202
203 return error;
204 }
205
206 /*
207 * cv_unsleep:
208 *
209 * Remove an LWP from the condition variable and sleep queue. This
210 * is called when the LWP has not been awoken normally but instead
211 * interrupted: for example, when a signal is received. Must be
212 * called with the LWP locked, and must return it unlocked.
213 */
214 static void
215 cv_unsleep(lwp_t *l, bool cleanup)
216 {
217 kcondvar_t *cv __diagused;
218
219 cv = (kcondvar_t *)(uintptr_t)l->l_wchan;
220
221 KASSERT(l->l_wchan == (wchan_t)cv);
222 KASSERT(l->l_sleepq == CV_SLEEPQ(cv));
223 KASSERT(cv_is_valid(cv));
224 KASSERT(cv_has_waiters(cv));
225
226 sleepq_unsleep(l, cleanup);
227 }
228
229 /*
230 * cv_wait:
231 *
232 * Wait non-interruptably on a condition variable until awoken.
233 */
234 void
235 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
236 {
237 lwp_t *l = curlwp;
238
239 KASSERT(mutex_owned(mtx));
240
241 cv_enter(cv, mtx, l);
242
243 /*
244 * We can't use cv_exit() here since the cv might be destroyed before
245 * this thread gets a chance to run. Instead, hand off the lockdebug
246 * responsibility to the thread that wakes us up.
247 */
248
249 CV_LOCKDEBUG_HANDOFF(l, cv);
250 (void)sleepq_block(0, false);
251 mutex_enter(mtx);
252 }
253
254 /*
255 * cv_wait_sig:
256 *
257 * Wait on a condition variable until a awoken or a signal is received.
258 * Will also return early if the process is exiting. Returns zero if
259 * awoken normally, ERESTART if a signal was received and the system
260 * call is restartable, or EINTR otherwise.
261 */
262 int
263 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
264 {
265 lwp_t *l = curlwp;
266 int error;
267
268 KASSERT(mutex_owned(mtx));
269
270 cv_enter(cv, mtx, l);
271 error = sleepq_block(0, true);
272 return cv_exit(cv, mtx, l, error);
273 }
274
275 /*
276 * cv_timedwait:
277 *
278 * Wait on a condition variable until awoken or the specified timeout
279 * expires. Returns zero if awoken normally or EWOULDBLOCK if the
280 * timeout expired.
281 *
282 * timo is a timeout in ticks. timo = 0 specifies an infinite timeout.
283 */
284 int
285 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
286 {
287 lwp_t *l = curlwp;
288 int error;
289
290 KASSERT(mutex_owned(mtx));
291
292 cv_enter(cv, mtx, l);
293 error = sleepq_block(timo, false);
294 return cv_exit(cv, mtx, l, error);
295 }
296
297 /*
298 * cv_timedwait_sig:
299 *
300 * Wait on a condition variable until a timeout expires, awoken or a
301 * signal is received. Will also return early if the process is
302 * exiting. Returns zero if awoken normally, EWOULDBLOCK if the
303 * timeout expires, ERESTART if a signal was received and the system
304 * call is restartable, or EINTR otherwise.
305 *
306 * timo is a timeout in ticks. timo = 0 specifies an infinite timeout.
307 */
308 int
309 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
310 {
311 lwp_t *l = curlwp;
312 int error;
313
314 KASSERT(mutex_owned(mtx));
315
316 cv_enter(cv, mtx, l);
317 error = sleepq_block(timo, true);
318 return cv_exit(cv, mtx, l, error);
319 }
320
321 /*
322 * cv_signal:
323 *
324 * Wake the highest priority LWP waiting on a condition variable.
325 * Must be called with the interlocking mutex held.
326 */
327 void
328 cv_signal(kcondvar_t *cv)
329 {
330
331 /* LOCKDEBUG_WAKEUP(CV_DEBUG_P(cv), cv, CV_RA); */
332 KASSERT(cv_is_valid(cv));
333
334 if (__predict_false(!TAILQ_EMPTY(CV_SLEEPQ(cv))))
335 cv_wakeup_one(cv);
336 }
337
338 static inline void
339 cv_wakeup_one(kcondvar_t *cv)
340 {
341 sleepq_t *sq;
342 kmutex_t *mp;
343 lwp_t *l;
344
345 KASSERT(cv_is_valid(cv));
346
347 mp = sleepq_hashlock(cv);
348 sq = CV_SLEEPQ(cv);
349 l = TAILQ_FIRST(sq);
350 if (l == NULL) {
351 mutex_spin_exit(mp);
352 return;
353 }
354 KASSERT(l->l_sleepq == sq);
355 KASSERT(l->l_mutex == mp);
356 KASSERT(l->l_wchan == cv);
357 CV_LOCKDEBUG_PROCESS(l, cv);
358 sleepq_remove(sq, l);
359 mutex_spin_exit(mp);
360
361 KASSERT(cv_is_valid(cv));
362 }
363
364 /*
365 * cv_broadcast:
366 *
367 * Wake all LWPs waiting on a condition variable. Must be called
368 * with the interlocking mutex held.
369 */
370 void
371 cv_broadcast(kcondvar_t *cv)
372 {
373
374 /* LOCKDEBUG_WAKEUP(CV_DEBUG_P(cv), cv, CV_RA); */
375 KASSERT(cv_is_valid(cv));
376
377 if (__predict_false(!TAILQ_EMPTY(CV_SLEEPQ(cv))))
378 cv_wakeup_all(cv);
379 }
380
381 static inline void
382 cv_wakeup_all(kcondvar_t *cv)
383 {
384 sleepq_t *sq;
385 kmutex_t *mp;
386 lwp_t *l, *next;
387
388 KASSERT(cv_is_valid(cv));
389
390 mp = sleepq_hashlock(cv);
391 sq = CV_SLEEPQ(cv);
392 for (l = TAILQ_FIRST(sq); l != NULL; l = next) {
393 KASSERT(l->l_sleepq == sq);
394 KASSERT(l->l_mutex == mp);
395 KASSERT(l->l_wchan == cv);
396 next = TAILQ_NEXT(l, l_sleepchain);
397 CV_LOCKDEBUG_PROCESS(l, cv);
398 sleepq_remove(sq, l);
399 }
400 mutex_spin_exit(mp);
401
402 KASSERT(cv_is_valid(cv));
403 }
404
405 /*
406 * cv_has_waiters:
407 *
408 * For diagnostic assertions: return non-zero if a condition
409 * variable has waiters.
410 */
411 bool
412 cv_has_waiters(kcondvar_t *cv)
413 {
414
415 return !TAILQ_EMPTY(CV_SLEEPQ(cv));
416 }
417
418 /*
419 * cv_is_valid:
420 *
421 * For diagnostic assertions: return non-zero if a condition
422 * variable appears to be valid. No locks need be held.
423 */
424 bool
425 cv_is_valid(kcondvar_t *cv)
426 {
427
428 return CV_WMESG(cv) != deadcv && CV_WMESG(cv) != NULL;
429 }
430