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 (SCHEDULER_STOPPED())
131 if (lock == &Giant.lock_object)
132 mtx_assert(&Giant, MA_OWNED);
135 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
136 if (lock != &Giant.lock_object) {
137 if (class->lc_flags & LC_SLEEPABLE)
139 WITNESS_SAVE(lock, lock_witness);
140 lock_state = class->lc_unlock(lock);
141 if (class->lc_flags & LC_SLEEPABLE)
147 if (KTRPOINT(td, KTR_CSW))
148 ktrcsw(0, 0, cv_wmesg(cvp));
151 if (lock != &Giant.lock_object) {
152 class->lc_lock(lock, lock_state);
153 WITNESS_RESTORE(lock, lock_witness);
158 * Wait on a condition variable. This function differs from cv_wait by
159 * not acquiring the mutex after condition variable was signaled.
162 _cv_wait_unlock(struct cv *cvp, struct lock_object *lock)
164 struct lock_class *class;
169 if (KTRPOINT(td, KTR_CSW))
170 ktrcsw(1, 0, cv_wmesg(cvp));
172 CV_ASSERT(cvp, lock, td);
173 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
174 "Waiting on \"%s\"", cvp->cv_description);
175 KASSERT(lock != &Giant.lock_object,
176 ("cv_wait_unlock cannot be used with Giant"));
177 class = LOCK_CLASS(lock);
179 if (SCHEDULER_STOPPED()) {
180 class->lc_unlock(lock);
189 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
190 if (class->lc_flags & LC_SLEEPABLE)
192 class->lc_unlock(lock);
193 if (class->lc_flags & LC_SLEEPABLE)
198 if (KTRPOINT(td, KTR_CSW))
199 ktrcsw(0, 0, cv_wmesg(cvp));
205 * Wait on a condition variable, allowing interruption by signals. Return 0 if
206 * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
207 * a signal was caught. If ERESTART is returned the system call should be
208 * restarted if possible.
211 _cv_wait_sig(struct cv *cvp, struct lock_object *lock)
213 WITNESS_SAVE_DECL(lock_witness);
214 struct lock_class *class;
216 uintptr_t lock_state;
222 if (KTRPOINT(td, KTR_CSW))
223 ktrcsw(1, 0, cv_wmesg(cvp));
225 CV_ASSERT(cvp, lock, td);
226 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
227 "Waiting on \"%s\"", cvp->cv_description);
228 class = LOCK_CLASS(lock);
230 if (SCHEDULER_STOPPED())
236 if (lock == &Giant.lock_object)
237 mtx_assert(&Giant, MA_OWNED);
240 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
241 SLEEPQ_INTERRUPTIBLE, 0);
242 if (lock != &Giant.lock_object) {
243 if (class->lc_flags & LC_SLEEPABLE)
245 WITNESS_SAVE(lock, lock_witness);
246 lock_state = class->lc_unlock(lock);
247 if (class->lc_flags & LC_SLEEPABLE)
250 rval = sleepq_wait_sig(cvp, 0);
253 if (KTRPOINT(td, KTR_CSW))
254 ktrcsw(0, 0, cv_wmesg(cvp));
257 if (lock != &Giant.lock_object) {
258 class->lc_lock(lock, lock_state);
259 WITNESS_RESTORE(lock, lock_witness);
266 * Wait on a condition variable for (at most) the value specified in sbt
267 * argument. Returns 0 if the process was resumed by cv_signal or cv_broadcast,
268 * EWOULDBLOCK if the timeout expires.
271 _cv_timedwait_sbt(struct cv *cvp, struct lock_object *lock, sbintime_t sbt,
272 sbintime_t pr, int flags)
274 WITNESS_SAVE_DECL(lock_witness);
275 struct lock_class *class;
277 int lock_state, rval;
282 if (KTRPOINT(td, KTR_CSW))
283 ktrcsw(1, 0, cv_wmesg(cvp));
285 CV_ASSERT(cvp, lock, td);
286 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
287 "Waiting on \"%s\"", cvp->cv_description);
288 class = LOCK_CLASS(lock);
290 if (SCHEDULER_STOPPED())
296 if (lock == &Giant.lock_object)
297 mtx_assert(&Giant, MA_OWNED);
300 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
301 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
302 if (lock != &Giant.lock_object) {
303 if (class->lc_flags & LC_SLEEPABLE)
305 WITNESS_SAVE(lock, lock_witness);
306 lock_state = class->lc_unlock(lock);
307 if (class->lc_flags & LC_SLEEPABLE)
310 rval = sleepq_timedwait(cvp, 0);
313 if (KTRPOINT(td, KTR_CSW))
314 ktrcsw(0, 0, cv_wmesg(cvp));
317 if (lock != &Giant.lock_object) {
318 class->lc_lock(lock, lock_state);
319 WITNESS_RESTORE(lock, lock_witness);
326 * Wait on a condition variable for (at most) the value specified in sbt
327 * argument, allowing interruption by signals.
328 * Returns 0 if the thread was resumed by cv_signal or cv_broadcast,
329 * EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if a signal
333 _cv_timedwait_sig_sbt(struct cv *cvp, struct lock_object *lock,
334 sbintime_t sbt, sbintime_t pr, int flags)
336 WITNESS_SAVE_DECL(lock_witness);
337 struct lock_class *class;
339 int lock_state, rval;
344 if (KTRPOINT(td, KTR_CSW))
345 ktrcsw(1, 0, cv_wmesg(cvp));
347 CV_ASSERT(cvp, lock, td);
348 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
349 "Waiting on \"%s\"", cvp->cv_description);
350 class = LOCK_CLASS(lock);
352 if (SCHEDULER_STOPPED())
358 if (lock == &Giant.lock_object)
359 mtx_assert(&Giant, MA_OWNED);
362 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
363 SLEEPQ_INTERRUPTIBLE, 0);
364 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
365 if (lock != &Giant.lock_object) {
366 if (class->lc_flags & LC_SLEEPABLE)
368 WITNESS_SAVE(lock, lock_witness);
369 lock_state = class->lc_unlock(lock);
370 if (class->lc_flags & LC_SLEEPABLE)
373 rval = sleepq_timedwait_sig(cvp, 0);
376 if (KTRPOINT(td, KTR_CSW))
377 ktrcsw(0, 0, cv_wmesg(cvp));
380 if (lock != &Giant.lock_object) {
381 class->lc_lock(lock, lock_state);
382 WITNESS_RESTORE(lock, lock_witness);
389 * Signal a condition variable, wakes up one waiting thread. Will also wakeup
390 * the swapper if the process is not in memory, so that it can bring the
391 * sleeping process in. Note that this may also result in additional threads
392 * being made runnable. Should be called with the same mutex as was passed to
396 cv_signal(struct cv *cvp)
402 if (cvp->cv_waiters > 0) {
403 if (cvp->cv_waiters == CV_WAITERS_BOUND &&
404 sleepq_lookup(cvp) == NULL) {
407 if (cvp->cv_waiters < CV_WAITERS_BOUND)
409 wakeup_swapper = sleepq_signal(cvp, SLEEPQ_CONDVAR, 0,
419 * Broadcast a signal to a condition variable. Wakes up all waiting threads.
420 * Should be called with the same mutex as was passed to cv_wait held.
423 cv_broadcastpri(struct cv *cvp, int pri)
428 * XXX sleepq_broadcast pri argument changed from -1 meaning
429 * no pri to 0 meaning no pri.
435 if (cvp->cv_waiters > 0) {
437 wakeup_swapper = sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);