2 * Copyright (c) 2000 Jake Burkholder <jake@freebsd.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_ktrace.h"
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/limits.h>
36 #include <sys/mutex.h>
38 #include <sys/kernel.h>
40 #include <sys/condvar.h>
41 #include <sys/sched.h>
42 #include <sys/signalvar.h>
43 #include <sys/sleepqueue.h>
44 #include <sys/resourcevar.h>
47 #include <sys/ktrace.h>
51 * A bound below which cv_waiters is valid. Once cv_waiters reaches this bound,
52 * cv_signal must manually check the wait queue for threads.
54 #define CV_WAITERS_BOUND INT_MAX
56 #define CV_WAITERS_INC(cvp) do { \
57 if ((cvp)->cv_waiters < CV_WAITERS_BOUND) \
58 (cvp)->cv_waiters++; \
62 * Common sanity checks for cv_wait* functions.
64 #define CV_ASSERT(cvp, lock, td) do { \
65 KASSERT((td) != NULL, ("%s: td NULL", __func__)); \
66 KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__)); \
67 KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__)); \
68 KASSERT((lock) != NULL, ("%s: lock NULL", __func__)); \
72 * Initialize a condition variable. Must be called before use.
75 cv_init(struct cv *cvp, const char *desc)
78 cvp->cv_description = desc;
83 * Destroy a condition variable. The condition variable must be re-initialized
84 * in order to be re-used.
87 cv_destroy(struct cv *cvp)
90 struct sleepqueue *sq;
93 sq = sleepq_lookup(cvp);
95 KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__));
100 * Wait on a condition variable. The current thread is placed on the condition
101 * variable's wait queue and suspended. A cv_signal or cv_broadcast on the same
102 * condition variable will resume the thread. The mutex is released before
103 * sleeping and will be held on return. It is recommended that the mutex be
104 * held when cv_signal or cv_broadcast are called.
107 _cv_wait(struct cv *cvp, struct lock_object *lock)
109 WITNESS_SAVE_DECL(lock_witness);
110 struct lock_class *class;
112 uintptr_t lock_state;
117 if (KTRPOINT(td, KTR_CSW))
118 ktrcsw(1, 0, cv_wmesg(cvp));
120 CV_ASSERT(cvp, lock, td);
121 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
122 "Waiting on \"%s\"", cvp->cv_description);
123 class = LOCK_CLASS(lock);
125 if (cold || panicstr) {
127 * During autoconfiguration, just give interrupts
128 * a chance, then just return. Don't run any other
129 * thread or panic below, in case this is the idle
130 * process and already asleep.
138 if (lock == &Giant.lock_object)
139 mtx_assert(&Giant, MA_OWNED);
142 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
143 if (lock != &Giant.lock_object) {
144 if (class->lc_flags & LC_SLEEPABLE)
146 WITNESS_SAVE(lock, lock_witness);
147 lock_state = class->lc_unlock(lock);
148 if (class->lc_flags & LC_SLEEPABLE)
154 if (KTRPOINT(td, KTR_CSW))
155 ktrcsw(0, 0, cv_wmesg(cvp));
158 if (lock != &Giant.lock_object) {
159 class->lc_lock(lock, lock_state);
160 WITNESS_RESTORE(lock, lock_witness);
165 * Wait on a condition variable. This function differs from cv_wait by
166 * not acquiring the mutex after condition variable was signaled.
169 _cv_wait_unlock(struct cv *cvp, struct lock_object *lock)
171 struct lock_class *class;
176 if (KTRPOINT(td, KTR_CSW))
177 ktrcsw(1, 0, cv_wmesg(cvp));
179 CV_ASSERT(cvp, lock, td);
180 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
181 "Waiting on \"%s\"", cvp->cv_description);
182 KASSERT(lock != &Giant.lock_object,
183 ("cv_wait_unlock cannot be used with Giant"));
184 class = LOCK_CLASS(lock);
186 if (cold || panicstr) {
188 * During autoconfiguration, just give interrupts
189 * a chance, then just return. Don't run any other
190 * thread or panic below, in case this is the idle
191 * process and already asleep.
193 class->lc_unlock(lock);
202 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
203 if (class->lc_flags & LC_SLEEPABLE)
205 class->lc_unlock(lock);
206 if (class->lc_flags & LC_SLEEPABLE)
211 if (KTRPOINT(td, KTR_CSW))
212 ktrcsw(0, 0, cv_wmesg(cvp));
218 * Wait on a condition variable, allowing interruption by signals. Return 0 if
219 * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
220 * a signal was caught. If ERESTART is returned the system call should be
221 * restarted if possible.
224 _cv_wait_sig(struct cv *cvp, struct lock_object *lock)
226 WITNESS_SAVE_DECL(lock_witness);
227 struct lock_class *class;
229 uintptr_t lock_state;
235 if (KTRPOINT(td, KTR_CSW))
236 ktrcsw(1, 0, cv_wmesg(cvp));
238 CV_ASSERT(cvp, lock, td);
239 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
240 "Waiting on \"%s\"", cvp->cv_description);
241 class = LOCK_CLASS(lock);
243 if (cold || panicstr) {
245 * After a panic, or during autoconfiguration, just give
246 * interrupts a chance, then just return; don't run any other
247 * procs or panic below, in case this is the idle process and
256 if (lock == &Giant.lock_object)
257 mtx_assert(&Giant, MA_OWNED);
260 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
261 SLEEPQ_INTERRUPTIBLE, 0);
262 if (lock != &Giant.lock_object) {
263 if (class->lc_flags & LC_SLEEPABLE)
265 WITNESS_SAVE(lock, lock_witness);
266 lock_state = class->lc_unlock(lock);
267 if (class->lc_flags & LC_SLEEPABLE)
270 rval = sleepq_wait_sig(cvp, 0);
273 if (KTRPOINT(td, KTR_CSW))
274 ktrcsw(0, 0, cv_wmesg(cvp));
277 if (lock != &Giant.lock_object) {
278 class->lc_lock(lock, lock_state);
279 WITNESS_RESTORE(lock, lock_witness);
286 * Wait on a condition variable for (at most) the value specified in sbt
287 * argument. Returns 0 if the process was resumed by cv_signal or cv_broadcast,
288 * EWOULDBLOCK if the timeout expires.
291 _cv_timedwait_sbt(struct cv *cvp, struct lock_object *lock, sbintime_t sbt,
292 sbintime_t pr, int flags)
294 WITNESS_SAVE_DECL(lock_witness);
295 struct lock_class *class;
297 int lock_state, rval;
302 if (KTRPOINT(td, KTR_CSW))
303 ktrcsw(1, 0, cv_wmesg(cvp));
305 CV_ASSERT(cvp, lock, td);
306 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
307 "Waiting on \"%s\"", cvp->cv_description);
308 class = LOCK_CLASS(lock);
310 if (cold || panicstr) {
312 * After a panic, or during autoconfiguration, just give
313 * interrupts a chance, then just return; don't run any other
314 * thread or panic below, in case this is the idle process and
323 if (lock == &Giant.lock_object)
324 mtx_assert(&Giant, MA_OWNED);
327 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
328 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
329 if (lock != &Giant.lock_object) {
330 if (class->lc_flags & LC_SLEEPABLE)
332 WITNESS_SAVE(lock, lock_witness);
333 lock_state = class->lc_unlock(lock);
334 if (class->lc_flags & LC_SLEEPABLE)
337 rval = sleepq_timedwait(cvp, 0);
340 if (KTRPOINT(td, KTR_CSW))
341 ktrcsw(0, 0, cv_wmesg(cvp));
344 if (lock != &Giant.lock_object) {
345 class->lc_lock(lock, lock_state);
346 WITNESS_RESTORE(lock, lock_witness);
353 * Wait on a condition variable for (at most) the value specified in sbt
354 * argument, allowing interruption by signals.
355 * Returns 0 if the thread was resumed by cv_signal or cv_broadcast,
356 * EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if a signal
360 _cv_timedwait_sig_sbt(struct cv *cvp, struct lock_object *lock,
361 sbintime_t sbt, sbintime_t pr, int flags)
363 WITNESS_SAVE_DECL(lock_witness);
364 struct lock_class *class;
366 int lock_state, rval;
371 if (KTRPOINT(td, KTR_CSW))
372 ktrcsw(1, 0, cv_wmesg(cvp));
374 CV_ASSERT(cvp, lock, td);
375 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
376 "Waiting on \"%s\"", cvp->cv_description);
377 class = LOCK_CLASS(lock);
379 if (cold || panicstr) {
381 * After a panic, or during autoconfiguration, just give
382 * interrupts a chance, then just return; don't run any other
383 * thread or panic below, in case this is the idle process and
392 if (lock == &Giant.lock_object)
393 mtx_assert(&Giant, MA_OWNED);
396 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
397 SLEEPQ_INTERRUPTIBLE, 0);
398 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
399 if (lock != &Giant.lock_object) {
400 if (class->lc_flags & LC_SLEEPABLE)
402 WITNESS_SAVE(lock, lock_witness);
403 lock_state = class->lc_unlock(lock);
404 if (class->lc_flags & LC_SLEEPABLE)
407 rval = sleepq_timedwait_sig(cvp, 0);
410 if (KTRPOINT(td, KTR_CSW))
411 ktrcsw(0, 0, cv_wmesg(cvp));
414 if (lock != &Giant.lock_object) {
415 class->lc_lock(lock, lock_state);
416 WITNESS_RESTORE(lock, lock_witness);
423 * Signal a condition variable, wakes up one waiting thread. Will also wakeup
424 * the swapper if the process is not in memory, so that it can bring the
425 * sleeping process in. Note that this may also result in additional threads
426 * being made runnable. Should be called with the same mutex as was passed to
430 cv_signal(struct cv *cvp)
436 if (cvp->cv_waiters > 0) {
437 if (cvp->cv_waiters == CV_WAITERS_BOUND &&
438 sleepq_lookup(cvp) == NULL) {
441 if (cvp->cv_waiters < CV_WAITERS_BOUND)
443 wakeup_swapper = sleepq_signal(cvp, SLEEPQ_CONDVAR, 0,
453 * Broadcast a signal to a condition variable. Wakes up all waiting threads.
454 * Should be called with the same mutex as was passed to cv_wait held.
457 cv_broadcastpri(struct cv *cvp, int pri)
462 * XXX sleepq_broadcast pri argument changed from -1 meaning
463 * no pri to 0 meaning no pri.
469 if (cvp->cv_waiters > 0) {
471 wakeup_swapper = sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);