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: curthread 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;
105 if (KTRPOINT(td, KTR_CSW))
108 CV_ASSERT(cvp, lock, td);
109 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
110 "Waiting on \"%s\"", cvp->cv_description);
111 WITNESS_SAVE(lock, lock_witness);
112 class = LOCK_CLASS(lock);
114 if (cold || panicstr) {
116 * During autoconfiguration, just give interrupts
117 * a chance, then just return. Don't run any other
118 * thread or panic below, in case this is the idle
119 * process and already asleep.
129 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
130 if (lock != &Giant.lock_object) {
131 if (class->lc_flags & LC_SLEEPABLE)
133 lock_state = class->lc_unlock(lock);
134 if (class->lc_flags & LC_SLEEPABLE)
140 if (KTRPOINT(td, KTR_CSW))
144 if (lock != &Giant.lock_object) {
145 class->lc_lock(lock, lock_state);
146 WITNESS_RESTORE(lock, lock_witness);
151 * Wait on a condition variable. This function differs from cv_wait by
152 * not aquiring the mutex after condition variable was signaled.
155 _cv_wait_unlock(struct cv *cvp, struct lock_object *lock)
157 struct lock_class *class;
162 if (KTRPOINT(td, KTR_CSW))
165 CV_ASSERT(cvp, lock, td);
166 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
167 "Waiting on \"%s\"", cvp->cv_description);
168 KASSERT(lock != &Giant.lock_object,
169 ("cv_wait_unlock cannot be used with Giant"));
170 class = LOCK_CLASS(lock);
172 if (cold || panicstr) {
174 * During autoconfiguration, just give interrupts
175 * a chance, then just return. Don't run any other
176 * thread or panic below, in case this is the idle
177 * process and already asleep.
179 class->lc_unlock(lock);
188 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
189 if (class->lc_flags & LC_SLEEPABLE)
191 class->lc_unlock(lock);
192 if (class->lc_flags & LC_SLEEPABLE)
197 if (KTRPOINT(td, KTR_CSW))
204 * Wait on a condition variable, allowing interruption by signals. Return 0 if
205 * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
206 * a signal was caught. If ERESTART is returned the system call should be
207 * restarted if possible.
210 _cv_wait_sig(struct cv *cvp, struct lock_object *lock)
212 WITNESS_SAVE_DECL(lock_witness);
213 struct lock_class *class;
216 int lock_state, rval;
222 if (KTRPOINT(td, KTR_CSW))
225 CV_ASSERT(cvp, lock, td);
226 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
227 "Waiting on \"%s\"", cvp->cv_description);
228 WITNESS_SAVE(lock, lock_witness);
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
246 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
247 SLEEPQ_INTERRUPTIBLE, 0);
248 if (lock != &Giant.lock_object) {
249 if (class->lc_flags & LC_SLEEPABLE)
251 lock_state = class->lc_unlock(lock);
252 if (class->lc_flags & LC_SLEEPABLE)
255 rval = sleepq_wait_sig(cvp);
258 if (KTRPOINT(td, KTR_CSW))
262 if (lock != &Giant.lock_object) {
263 class->lc_lock(lock, lock_state);
264 WITNESS_RESTORE(lock, lock_witness);
271 * Wait on a condition variable for at most timo/hz seconds. Returns 0 if the
272 * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout
276 _cv_timedwait(struct cv *cvp, struct lock_object *lock, int timo)
278 WITNESS_SAVE_DECL(lock_witness);
279 struct lock_class *class;
281 int lock_state, rval;
287 if (KTRPOINT(td, KTR_CSW))
290 CV_ASSERT(cvp, lock, td);
291 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
292 "Waiting on \"%s\"", cvp->cv_description);
293 WITNESS_SAVE(lock, lock_witness);
294 class = LOCK_CLASS(lock);
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
311 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
312 sleepq_set_timeout(cvp, timo);
313 if (lock != &Giant.lock_object) {
314 if (class->lc_flags & LC_SLEEPABLE)
316 lock_state = class->lc_unlock(lock);
317 if (class->lc_flags & LC_SLEEPABLE)
320 rval = sleepq_timedwait(cvp);
323 if (KTRPOINT(td, KTR_CSW))
327 if (lock != &Giant.lock_object) {
328 class->lc_lock(lock, lock_state);
329 WITNESS_RESTORE(lock, lock_witness);
336 * Wait on a condition variable for at most timo/hz seconds, allowing
337 * interruption by signals. Returns 0 if the thread was resumed by cv_signal
338 * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if
339 * a signal was caught.
342 _cv_timedwait_sig(struct cv *cvp, struct lock_object *lock, int timo)
344 WITNESS_SAVE_DECL(lock_witness);
345 struct lock_class *class;
348 int lock_state, rval;
355 if (KTRPOINT(td, KTR_CSW))
358 CV_ASSERT(cvp, lock, td);
359 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
360 "Waiting on \"%s\"", cvp->cv_description);
361 WITNESS_SAVE(lock, lock_witness);
362 class = LOCK_CLASS(lock);
364 if (cold || panicstr) {
366 * After a panic, or during autoconfiguration, just give
367 * interrupts a chance, then just return; don't run any other
368 * thread or panic below, in case this is the idle process and
379 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
380 SLEEPQ_INTERRUPTIBLE, 0);
381 sleepq_set_timeout(cvp, timo);
382 if (lock != &Giant.lock_object) {
383 if (class->lc_flags & LC_SLEEPABLE)
385 lock_state = class->lc_unlock(lock);
386 if (class->lc_flags & LC_SLEEPABLE)
389 rval = sleepq_timedwait_sig(cvp);
392 if (KTRPOINT(td, KTR_CSW))
396 if (lock != &Giant.lock_object) {
397 class->lc_lock(lock, lock_state);
398 WITNESS_RESTORE(lock, lock_witness);
405 * Signal a condition variable, wakes up one waiting thread. Will also wakeup
406 * the swapper if the process is not in memory, so that it can bring the
407 * sleeping process in. Note that this may also result in additional threads
408 * being made runnable. Should be called with the same mutex as was passed to
412 cv_signal(struct cv *cvp)
418 if (cvp->cv_waiters > 0) {
420 wakeup_swapper = sleepq_signal(cvp, SLEEPQ_CONDVAR, -1, 0);
428 * Broadcast a signal to a condition variable. Wakes up all waiting threads.
429 * Should be called with the same mutex as was passed to cv_wait held.
432 cv_broadcastpri(struct cv *cvp, int pri)
438 if (cvp->cv_waiters > 0) {
440 wakeup_swapper = sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);