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>
35 #include <sys/mutex.h>
37 #include <sys/kernel.h>
39 #include <sys/condvar.h>
40 #include <sys/sched.h>
41 #include <sys/signalvar.h>
42 #include <sys/sleepqueue.h>
43 #include <sys/resourcevar.h>
46 #include <sys/ktrace.h>
50 * Common sanity checks for cv_wait* functions.
52 #define CV_ASSERT(cvp, lock, td) do { \
53 KASSERT((td) != NULL, ("%s: td NULL", __func__)); \
54 KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__)); \
55 KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__)); \
56 KASSERT((lock) != NULL, ("%s: lock NULL", __func__)); \
60 * Initialize a condition variable. Must be called before use.
63 cv_init(struct cv *cvp, const char *desc)
66 cvp->cv_description = desc;
71 * Destroy a condition variable. The condition variable must be re-initialized
72 * in order to be re-used.
75 cv_destroy(struct cv *cvp)
78 struct sleepqueue *sq;
81 sq = sleepq_lookup(cvp);
83 KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__));
88 * Wait on a condition variable. The current thread is placed on the condition
89 * variable's wait queue and suspended. A cv_signal or cv_broadcast on the same
90 * condition variable will resume the thread. The mutex is released before
91 * sleeping and will be held on return. It is recommended that the mutex be
92 * held when cv_signal or cv_broadcast are called.
95 _cv_wait(struct cv *cvp, struct lock_object *lock)
97 WITNESS_SAVE_DECL(lock_witness);
98 struct lock_class *class;
100 uintptr_t lock_state;
105 if (KTRPOINT(td, KTR_CSW))
106 ktrcsw(1, 0, cv_wmesg(cvp));
108 CV_ASSERT(cvp, lock, td);
109 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
110 "Waiting on \"%s\"", cvp->cv_description);
111 class = LOCK_CLASS(lock);
113 if (cold || panicstr) {
115 * During autoconfiguration, just give interrupts
116 * a chance, then just return. Don't run any other
117 * thread or panic below, in case this is the idle
118 * process and already asleep.
126 if (lock == &Giant.lock_object)
127 mtx_assert(&Giant, MA_OWNED);
130 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
131 if (lock != &Giant.lock_object) {
132 if (class->lc_flags & LC_SLEEPABLE)
134 WITNESS_SAVE(lock, lock_witness);
135 lock_state = class->lc_unlock(lock);
136 if (class->lc_flags & LC_SLEEPABLE)
142 if (KTRPOINT(td, KTR_CSW))
143 ktrcsw(0, 0, cv_wmesg(cvp));
146 if (lock != &Giant.lock_object) {
147 class->lc_lock(lock, lock_state);
148 WITNESS_RESTORE(lock, lock_witness);
153 * Wait on a condition variable. This function differs from cv_wait by
154 * not aquiring the mutex after condition variable was signaled.
157 _cv_wait_unlock(struct cv *cvp, struct lock_object *lock)
159 struct lock_class *class;
164 if (KTRPOINT(td, KTR_CSW))
165 ktrcsw(1, 0, cv_wmesg(cvp));
167 CV_ASSERT(cvp, lock, td);
168 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
169 "Waiting on \"%s\"", cvp->cv_description);
170 KASSERT(lock != &Giant.lock_object,
171 ("cv_wait_unlock cannot be used with Giant"));
172 class = LOCK_CLASS(lock);
174 if (cold || panicstr) {
176 * During autoconfiguration, just give interrupts
177 * a chance, then just return. Don't run any other
178 * thread or panic below, in case this is the idle
179 * process and already asleep.
181 class->lc_unlock(lock);
190 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
191 if (class->lc_flags & LC_SLEEPABLE)
193 class->lc_unlock(lock);
194 if (class->lc_flags & LC_SLEEPABLE)
199 if (KTRPOINT(td, KTR_CSW))
200 ktrcsw(0, 0, cv_wmesg(cvp));
206 * Wait on a condition variable, allowing interruption by signals. Return 0 if
207 * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
208 * a signal was caught. If ERESTART is returned the system call should be
209 * restarted if possible.
212 _cv_wait_sig(struct cv *cvp, struct lock_object *lock)
214 WITNESS_SAVE_DECL(lock_witness);
215 struct lock_class *class;
217 uintptr_t lock_state;
223 if (KTRPOINT(td, KTR_CSW))
224 ktrcsw(1, 0, cv_wmesg(cvp));
226 CV_ASSERT(cvp, lock, td);
227 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
228 "Waiting on \"%s\"", cvp->cv_description);
229 class = LOCK_CLASS(lock);
231 if (cold || panicstr) {
233 * After a panic, or during autoconfiguration, just give
234 * interrupts a chance, then just return; don't run any other
235 * procs or panic below, in case this is the idle process and
244 if (lock == &Giant.lock_object)
245 mtx_assert(&Giant, MA_OWNED);
248 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
249 SLEEPQ_INTERRUPTIBLE, 0);
250 if (lock != &Giant.lock_object) {
251 if (class->lc_flags & LC_SLEEPABLE)
253 WITNESS_SAVE(lock, lock_witness);
254 lock_state = class->lc_unlock(lock);
255 if (class->lc_flags & LC_SLEEPABLE)
258 rval = sleepq_wait_sig(cvp, 0);
261 if (KTRPOINT(td, KTR_CSW))
262 ktrcsw(0, 0, cv_wmesg(cvp));
265 if (lock != &Giant.lock_object) {
266 class->lc_lock(lock, lock_state);
267 WITNESS_RESTORE(lock, lock_witness);
274 * Wait on a condition variable for (at most) the value specified in sbt
275 * argument. Returns 0 if the process was resumed by cv_signal or cv_broadcast,
276 * EWOULDBLOCK if the timeout expires.
279 _cv_timedwait_sbt(struct cv *cvp, struct lock_object *lock, sbintime_t sbt,
280 sbintime_t pr, int flags)
282 WITNESS_SAVE_DECL(lock_witness);
283 struct lock_class *class;
285 int lock_state, rval;
290 if (KTRPOINT(td, KTR_CSW))
291 ktrcsw(1, 0, cv_wmesg(cvp));
293 CV_ASSERT(cvp, lock, td);
294 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
295 "Waiting on \"%s\"", cvp->cv_description);
296 class = LOCK_CLASS(lock);
298 if (cold || panicstr) {
300 * After a panic, or during autoconfiguration, just give
301 * interrupts a chance, then just return; don't run any other
302 * thread or panic below, in case this is the idle process and
311 if (lock == &Giant.lock_object)
312 mtx_assert(&Giant, MA_OWNED);
315 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
316 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
317 if (lock != &Giant.lock_object) {
318 if (class->lc_flags & LC_SLEEPABLE)
320 WITNESS_SAVE(lock, lock_witness);
321 lock_state = class->lc_unlock(lock);
322 if (class->lc_flags & LC_SLEEPABLE)
325 rval = sleepq_timedwait(cvp, 0);
328 if (KTRPOINT(td, KTR_CSW))
329 ktrcsw(0, 0, cv_wmesg(cvp));
332 if (lock != &Giant.lock_object) {
333 class->lc_lock(lock, lock_state);
334 WITNESS_RESTORE(lock, lock_witness);
341 * Wait on a condition variable for (at most) the value specified in sbt
342 * argument, allowing interruption by signals.
343 * Returns 0 if the thread was resumed by cv_signal or cv_broadcast,
344 * EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if a signal
348 _cv_timedwait_sig_sbt(struct cv *cvp, struct lock_object *lock,
349 sbintime_t sbt, sbintime_t pr, int flags)
351 WITNESS_SAVE_DECL(lock_witness);
352 struct lock_class *class;
354 int lock_state, rval;
359 if (KTRPOINT(td, KTR_CSW))
360 ktrcsw(1, 0, cv_wmesg(cvp));
362 CV_ASSERT(cvp, lock, td);
363 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
364 "Waiting on \"%s\"", cvp->cv_description);
365 class = LOCK_CLASS(lock);
367 if (cold || panicstr) {
369 * After a panic, or during autoconfiguration, just give
370 * interrupts a chance, then just return; don't run any other
371 * thread or panic below, in case this is the idle process and
380 if (lock == &Giant.lock_object)
381 mtx_assert(&Giant, MA_OWNED);
384 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
385 SLEEPQ_INTERRUPTIBLE, 0);
386 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
387 if (lock != &Giant.lock_object) {
388 if (class->lc_flags & LC_SLEEPABLE)
390 WITNESS_SAVE(lock, lock_witness);
391 lock_state = class->lc_unlock(lock);
392 if (class->lc_flags & LC_SLEEPABLE)
395 rval = sleepq_timedwait_sig(cvp, 0);
398 if (KTRPOINT(td, KTR_CSW))
399 ktrcsw(0, 0, cv_wmesg(cvp));
402 if (lock != &Giant.lock_object) {
403 class->lc_lock(lock, lock_state);
404 WITNESS_RESTORE(lock, lock_witness);
411 * Signal a condition variable, wakes up one waiting thread. Will also wakeup
412 * the swapper if the process is not in memory, so that it can bring the
413 * sleeping process in. Note that this may also result in additional threads
414 * being made runnable. Should be called with the same mutex as was passed to
418 cv_signal(struct cv *cvp)
424 if (cvp->cv_waiters > 0) {
426 wakeup_swapper = sleepq_signal(cvp, SLEEPQ_CONDVAR, 0, 0);
434 * Broadcast a signal to a condition variable. Wakes up all waiting threads.
435 * Should be called with the same mutex as was passed to cv_wait held.
438 cv_broadcastpri(struct cv *cvp, int pri)
443 * XXX sleepq_broadcast pri argument changed from -1 meaning
444 * no pri to 0 meaning no pri.
450 if (cvp->cv_waiters > 0) {
452 wakeup_swapper = sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);