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, mp, td) do { \
53 KASSERT((td) != NULL, ("%s: curthread NULL", __func__)); \
54 KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__)); \
55 KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__)); \
56 KASSERT((mp) != NULL, ("%s: mp NULL", __func__)); \
57 mtx_assert((mp), MA_OWNED | MA_NOTRECURSED); \
61 * Initialize a condition variable. Must be called before use.
64 cv_init(struct cv *cvp, const char *desc)
67 cvp->cv_description = desc;
72 * Destroy a condition variable. The condition variable must be re-initialized
73 * in order to be re-used.
76 cv_destroy(struct cv *cvp)
79 struct sleepqueue *sq;
82 sq = sleepq_lookup(cvp);
84 KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__));
89 * Wait on a condition variable. The current thread is placed on the condition
90 * variable's wait queue and suspended. A cv_signal or cv_broadcast on the same
91 * condition variable will resume the thread. The mutex is released before
92 * sleeping and will be held on return. It is recommended that the mutex be
93 * held when cv_signal or cv_broadcast are called.
96 cv_wait(struct cv *cvp, struct mtx *mp)
98 WITNESS_SAVE_DECL(mp);
100 WITNESS_SAVE(&mp->mtx_object, mp);
102 if (cold || panicstr) {
104 * During autoconfiguration, just give interrupts
105 * a chance, then just return. Don't run any other
106 * thread or panic below, in case this is the idle
107 * process and already asleep.
112 cv_wait_unlock(cvp, mp);
114 WITNESS_RESTORE(&mp->mtx_object, mp);
118 * Wait on a condition variable. This function differs from cv_wait by
119 * not aquiring the mutex after condition variable was signaled.
122 cv_wait_unlock(struct cv *cvp, struct mtx *mp)
128 if (KTRPOINT(td, KTR_CSW))
131 CV_ASSERT(cvp, mp, td);
132 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
133 "Waiting on \"%s\"", cvp->cv_description);
135 if (cold || panicstr) {
137 * During autoconfiguration, just give interrupts
138 * a chance, then just return. Don't run any other
139 * thread or panic below, in case this is the idle
140 * process and already asleep.
152 sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR,
160 * Wait on a condition variable, allowing interruption by signals. Return 0 if
161 * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
162 * a signal was caught. If ERESTART is returned the system call should be
163 * restarted if possible.
166 cv_wait_sig(struct cv *cvp, struct mtx *mp)
171 WITNESS_SAVE_DECL(mp);
176 if (KTRPOINT(td, KTR_CSW))
179 CV_ASSERT(cvp, mp, td);
180 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
181 "Waiting on \"%s\"", cvp->cv_description);
182 WITNESS_SAVE(&mp->mtx_object, mp);
184 if (cold || panicstr) {
186 * After a panic, or during autoconfiguration, just give
187 * interrupts a chance, then just return; don't run any other
188 * procs or panic below, in case this is the idle process and
200 sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR |
201 SLEEPQ_INTERRUPTIBLE, 0);
202 rval = sleepq_wait_sig(cvp);
205 if (KTRPOINT(td, KTR_CSW))
210 WITNESS_RESTORE(&mp->mtx_object, mp);
216 * Wait on a condition variable for at most timo/hz seconds. Returns 0 if the
217 * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout
221 cv_timedwait(struct cv *cvp, struct mtx *mp, int timo)
225 WITNESS_SAVE_DECL(mp);
230 if (KTRPOINT(td, KTR_CSW))
233 CV_ASSERT(cvp, mp, td);
234 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
235 "Waiting on \"%s\"", cvp->cv_description);
236 WITNESS_SAVE(&mp->mtx_object, mp);
238 if (cold || panicstr) {
240 * After a panic, or during autoconfiguration, just give
241 * interrupts a chance, then just return; don't run any other
242 * thread or panic below, in case this is the idle process and
254 sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR,
256 sleepq_set_timeout(cvp, timo);
257 rval = sleepq_timedwait(cvp);
260 if (KTRPOINT(td, KTR_CSW))
265 WITNESS_RESTORE(&mp->mtx_object, mp);
271 * Wait on a condition variable for at most timo/hz seconds, allowing
272 * interruption by signals. Returns 0 if the thread was resumed by cv_signal
273 * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if
274 * a signal was caught.
277 cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo)
282 WITNESS_SAVE_DECL(mp);
288 if (KTRPOINT(td, KTR_CSW))
291 CV_ASSERT(cvp, mp, td);
292 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
293 "Waiting on \"%s\"", cvp->cv_description);
294 WITNESS_SAVE(&mp->mtx_object, mp);
296 if (cold || panicstr) {
298 * After a panic, or during autoconfiguration, just give
299 * interrupts a chance, then just return; don't run any other
300 * thread or panic below, in case this is the idle process and
312 sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR |
313 SLEEPQ_INTERRUPTIBLE, 0);
314 sleepq_set_timeout(cvp, timo);
315 rval = sleepq_timedwait_sig(cvp);
318 if (KTRPOINT(td, KTR_CSW))
323 WITNESS_RESTORE(&mp->mtx_object, mp);
329 * Signal a condition variable, wakes up one waiting thread. Will also wakeup
330 * the swapper if the process is not in memory, so that it can bring the
331 * sleeping process in. Note that this may also result in additional threads
332 * being made runnable. Should be called with the same mutex as was passed to
336 cv_signal(struct cv *cvp)
340 if (cvp->cv_waiters > 0) {
342 sleepq_signal(cvp, SLEEPQ_CONDVAR, -1, 0);
348 * Broadcast a signal to a condition variable. Wakes up all waiting threads.
349 * Should be called with the same mutex as was passed to cv_wait held.
352 cv_broadcastpri(struct cv *cvp, int pri)
356 if (cvp->cv_waiters > 0) {
358 sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);