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);
103 if (KTRPOINT(td, KTR_CSW))
106 CV_ASSERT(cvp, mp, td);
107 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
108 "Waiting on \"%s\"", cvp->cv_description);
109 WITNESS_SAVE(&mp->mtx_object, mp);
111 if (cold || panicstr) {
113 * During autoconfiguration, just give interrupts
114 * a chance, then just return. Don't run any other
115 * thread or panic below, in case this is the idle
116 * process and already asleep.
127 sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR,
132 if (KTRPOINT(td, KTR_CSW))
137 WITNESS_RESTORE(&mp->mtx_object, mp);
141 * Wait on a condition variable. This function differs from cv_wait by
142 * not aquiring the mutex after condition variable was signaled.
145 cv_wait_unlock(struct cv *cvp, struct mtx *mp)
151 if (KTRPOINT(td, KTR_CSW))
154 CV_ASSERT(cvp, mp, td);
155 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
156 "Waiting on \"%s\"", cvp->cv_description);
158 if (cold || panicstr) {
160 * During autoconfiguration, just give interrupts
161 * a chance, then just return. Don't run any other
162 * thread or panic below, in case this is the idle
163 * process and already asleep.
175 sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR,
180 if (KTRPOINT(td, KTR_CSW))
187 * Wait on a condition variable, allowing interruption by signals. Return 0 if
188 * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
189 * a signal was caught. If ERESTART is returned the system call should be
190 * restarted if possible.
193 cv_wait_sig(struct cv *cvp, struct mtx *mp)
198 WITNESS_SAVE_DECL(mp);
203 if (KTRPOINT(td, KTR_CSW))
206 CV_ASSERT(cvp, mp, td);
207 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
208 "Waiting on \"%s\"", cvp->cv_description);
209 WITNESS_SAVE(&mp->mtx_object, mp);
211 if (cold || panicstr) {
213 * After a panic, or during autoconfiguration, just give
214 * interrupts a chance, then just return; don't run any other
215 * procs or panic below, in case this is the idle process and
227 sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR |
228 SLEEPQ_INTERRUPTIBLE, 0);
229 rval = sleepq_wait_sig(cvp);
232 if (KTRPOINT(td, KTR_CSW))
237 WITNESS_RESTORE(&mp->mtx_object, mp);
243 * Wait on a condition variable for at most timo/hz seconds. Returns 0 if the
244 * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout
248 cv_timedwait(struct cv *cvp, struct mtx *mp, int timo)
252 WITNESS_SAVE_DECL(mp);
257 if (KTRPOINT(td, KTR_CSW))
260 CV_ASSERT(cvp, mp, td);
261 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
262 "Waiting on \"%s\"", cvp->cv_description);
263 WITNESS_SAVE(&mp->mtx_object, mp);
265 if (cold || panicstr) {
267 * After a panic, or during autoconfiguration, just give
268 * interrupts a chance, then just return; don't run any other
269 * thread or panic below, in case this is the idle process and
281 sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR,
283 sleepq_set_timeout(cvp, timo);
284 rval = sleepq_timedwait(cvp);
287 if (KTRPOINT(td, KTR_CSW))
292 WITNESS_RESTORE(&mp->mtx_object, mp);
298 * Wait on a condition variable for at most timo/hz seconds, allowing
299 * interruption by signals. Returns 0 if the thread was resumed by cv_signal
300 * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if
301 * a signal was caught.
304 cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo)
309 WITNESS_SAVE_DECL(mp);
315 if (KTRPOINT(td, KTR_CSW))
318 CV_ASSERT(cvp, mp, td);
319 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
320 "Waiting on \"%s\"", cvp->cv_description);
321 WITNESS_SAVE(&mp->mtx_object, mp);
323 if (cold || panicstr) {
325 * After a panic, or during autoconfiguration, just give
326 * interrupts a chance, then just return; don't run any other
327 * thread or panic below, in case this is the idle process and
339 sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR |
340 SLEEPQ_INTERRUPTIBLE, 0);
341 sleepq_set_timeout(cvp, timo);
342 rval = sleepq_timedwait_sig(cvp);
345 if (KTRPOINT(td, KTR_CSW))
350 WITNESS_RESTORE(&mp->mtx_object, mp);
356 * Signal a condition variable, wakes up one waiting thread. Will also wakeup
357 * the swapper if the process is not in memory, so that it can bring the
358 * sleeping process in. Note that this may also result in additional threads
359 * being made runnable. Should be called with the same mutex as was passed to
363 cv_signal(struct cv *cvp)
367 if (cvp->cv_waiters > 0) {
369 sleepq_signal(cvp, SLEEPQ_CONDVAR, -1, 0);
375 * Broadcast a signal to a condition variable. Wakes up all waiting threads.
376 * Should be called with the same mutex as was passed to cv_wait held.
379 cv_broadcastpri(struct cv *cvp, int pri)
383 if (cvp->cv_waiters > 0) {
385 sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);