1 /* $NetBSD: kern_condvar.c,v 1.27 2009/10/21 21:12:06 rmind Exp $ */
4 * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Kernel condition variable implementation.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.27 2009/10/21 21:12:06 rmind Exp $");
39 #include <sys/param.h>
41 #include <sys/sched.h>
42 #include <sys/systm.h>
43 #include <sys/condvar.h>
44 #include <sys/sleepq.h>
45 #include <sys/lockdebug.h>
48 #include <uvm/uvm_extern.h>
51 * Accessors for the private contents of the kcondvar_t data type.
53 * cv_opaque[0] sleepq...
54 * cv_opaque[1] ...pointers
55 * cv_opaque[2] description for ps(1)
57 * cv_opaque[0..1] is protected by the interlock passed to cv_wait() (enqueue
58 * only), and the sleep queue lock acquired with sleeptab_lookup() (enqueue
61 * cv_opaque[2] (the wmesg) is static and does not change throughout the life
64 #define CV_SLEEPQ(cv) ((sleepq_t *)(cv)->cv_opaque)
65 #define CV_WMESG(cv) ((const char *)(cv)->cv_opaque[2])
66 #define CV_SET_WMESG(cv, v) (cv)->cv_opaque[2] = __UNCONST(v)
68 #define CV_DEBUG_P(cv) (CV_WMESG(cv) != nodebug)
69 #define CV_RA ((uintptr_t)__builtin_return_address(0))
71 static void cv_unsleep(lwp_t
*, bool);
72 static void cv_wakeup_one(kcondvar_t
*);
73 static void cv_wakeup_all(kcondvar_t
*);
75 static syncobj_t cv_syncobj
= {
83 lockops_t cv_lockops
= {
89 static const char deadcv
[] = "deadcv";
90 static const char nodebug
[] = "nodebug";
95 * Initialize a condition variable for use.
98 cv_init(kcondvar_t
*cv
, const char *wmesg
)
103 dodebug
= LOCKDEBUG_ALLOC(cv
, &cv_lockops
,
104 (uintptr_t)__builtin_return_address(0));
106 /* XXX This will break vfs_lockf. */
110 KASSERT(wmesg
!= NULL
);
111 CV_SET_WMESG(cv
, wmesg
);
112 sleepq_init(CV_SLEEPQ(cv
));
118 * Tear down a condition variable.
121 cv_destroy(kcondvar_t
*cv
)
124 LOCKDEBUG_FREE(CV_DEBUG_P(cv
), cv
);
126 KASSERT(cv_is_valid(cv
));
127 CV_SET_WMESG(cv
, deadcv
);
134 * Look up and lock the sleep queue corresponding to the given
135 * condition variable, and increment the number of waiters.
138 cv_enter(kcondvar_t
*cv
, kmutex_t
*mtx
, lwp_t
*l
)
143 KASSERT(cv_is_valid(cv
));
144 KASSERT(!cpu_intr_p());
145 KASSERT((l
->l_pflag
& LP_INTR
) == 0 || panicstr
!= NULL
);
147 LOCKDEBUG_LOCKED(CV_DEBUG_P(cv
), cv
, mtx
, CV_RA
, 0);
149 l
->l_kpriority
= true;
150 mp
= sleepq_hashlock(cv
);
152 sleepq_enter(sq
, l
, mp
);
153 sleepq_enqueue(sq
, cv
, CV_WMESG(cv
), &cv_syncobj
);
155 KASSERT(cv_has_waiters(cv
));
161 * After resuming execution, check to see if we have been restarted
162 * as a result of cv_signal(). If we have, but cannot take the
163 * wakeup (because of eg a pending Unix signal or timeout) then try
164 * to ensure that another LWP sees it. This is necessary because
165 * there may be multiple waiters, and at least one should take the
166 * wakeup if possible.
169 cv_exit(kcondvar_t
*cv
, kmutex_t
*mtx
, lwp_t
*l
, const int error
)
173 if (__predict_false(error
!= 0))
176 LOCKDEBUG_UNLOCKED(CV_DEBUG_P(cv
), cv
, CV_RA
, 0);
177 KASSERT(cv_is_valid(cv
));
185 * Remove an LWP from the condition variable and sleep queue. This
186 * is called when the LWP has not been awoken normally but instead
187 * interrupted: for example, when a signal is received. Must be
188 * called with the LWP locked, and must return it unlocked.
191 cv_unsleep(lwp_t
*l
, bool cleanup
)
195 cv
= (kcondvar_t
*)(uintptr_t)l
->l_wchan
;
197 KASSERT(l
->l_wchan
== (wchan_t
)cv
);
198 KASSERT(l
->l_sleepq
== CV_SLEEPQ(cv
));
199 KASSERT(cv_is_valid(cv
));
200 KASSERT(cv_has_waiters(cv
));
202 sleepq_unsleep(l
, cleanup
);
208 * Wait non-interruptably on a condition variable until awoken.
211 cv_wait(kcondvar_t
*cv
, kmutex_t
*mtx
)
215 KASSERT(mutex_owned(mtx
));
217 cv_enter(cv
, mtx
, l
);
218 (void)sleepq_block(0, false);
219 (void)cv_exit(cv
, mtx
, l
, 0);
225 * Wait on a condition variable until a awoken or a signal is received.
226 * Will also return early if the process is exiting. Returns zero if
227 * awoken normallly, ERESTART if a signal was received and the system
228 * call is restartable, or EINTR otherwise.
231 cv_wait_sig(kcondvar_t
*cv
, kmutex_t
*mtx
)
236 KASSERT(mutex_owned(mtx
));
238 cv_enter(cv
, mtx
, l
);
239 error
= sleepq_block(0, true);
240 return cv_exit(cv
, mtx
, l
, error
);
246 * Wait on a condition variable until awoken or the specified timeout
247 * expires. Returns zero if awoken normally or EWOULDBLOCK if the
251 cv_timedwait(kcondvar_t
*cv
, kmutex_t
*mtx
, int timo
)
256 KASSERT(mutex_owned(mtx
));
258 cv_enter(cv
, mtx
, l
);
259 error
= sleepq_block(timo
, false);
260 return cv_exit(cv
, mtx
, l
, error
);
266 * Wait on a condition variable until a timeout expires, awoken or a
267 * signal is received. Will also return early if the process is
268 * exiting. Returns zero if awoken normallly, EWOULDBLOCK if the
269 * timeout expires, ERESTART if a signal was received and the system
270 * call is restartable, or EINTR otherwise.
273 cv_timedwait_sig(kcondvar_t
*cv
, kmutex_t
*mtx
, int timo
)
278 KASSERT(mutex_owned(mtx
));
280 cv_enter(cv
, mtx
, l
);
281 error
= sleepq_block(timo
, true);
282 return cv_exit(cv
, mtx
, l
, error
);
288 * Wake the highest priority LWP waiting on a condition variable.
289 * Must be called with the interlocking mutex held.
292 cv_signal(kcondvar_t
*cv
)
295 /* LOCKDEBUG_WAKEUP(CV_DEBUG_P(cv), cv, CV_RA); */
296 KASSERT(cv_is_valid(cv
));
298 if (__predict_false(!TAILQ_EMPTY(CV_SLEEPQ(cv
))))
302 static void __noinline
303 cv_wakeup_one(kcondvar_t
*cv
)
309 KASSERT(cv_is_valid(cv
));
311 mp
= sleepq_hashlock(cv
);
318 KASSERT(l
->l_sleepq
== sq
);
319 KASSERT(l
->l_mutex
== mp
);
320 KASSERT(l
->l_wchan
== cv
);
321 sleepq_remove(sq
, l
);
324 KASSERT(cv_is_valid(cv
));
330 * Wake all LWPs waiting on a condition variable. Must be called
331 * with the interlocking mutex held.
334 cv_broadcast(kcondvar_t
*cv
)
337 /* LOCKDEBUG_WAKEUP(CV_DEBUG_P(cv), cv, CV_RA); */
338 KASSERT(cv_is_valid(cv
));
340 if (__predict_false(!TAILQ_EMPTY(CV_SLEEPQ(cv
))))
344 static void __noinline
345 cv_wakeup_all(kcondvar_t
*cv
)
351 KASSERT(cv_is_valid(cv
));
353 mp
= sleepq_hashlock(cv
);
355 for (l
= TAILQ_FIRST(sq
); l
!= NULL
; l
= next
) {
356 KASSERT(l
->l_sleepq
== sq
);
357 KASSERT(l
->l_mutex
== mp
);
358 KASSERT(l
->l_wchan
== cv
);
359 next
= TAILQ_NEXT(l
, l_sleepchain
);
360 sleepq_remove(sq
, l
);
364 KASSERT(cv_is_valid(cv
));
370 * For diagnostic assertions: return non-zero if a condition
371 * variable has waiters.
374 cv_has_waiters(kcondvar_t
*cv
)
377 return !TAILQ_EMPTY(CV_SLEEPQ(cv
));
383 * For diagnostic assertions: return non-zero if a condition
384 * variable appears to be valid. No locks need be held.
387 cv_is_valid(kcondvar_t
*cv
)
390 return CV_WMESG(cv
) != deadcv
&& CV_WMESG(cv
) != NULL
;