2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Jake Burkholder <jake@freebsd.org>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_ktrace.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/limits.h>
38 #include <sys/mutex.h>
40 #include <sys/kernel.h>
42 #include <sys/condvar.h>
43 #include <sys/sched.h>
44 #include <sys/signalvar.h>
45 #include <sys/sleepqueue.h>
46 #include <sys/resourcevar.h>
49 #include <sys/ktrace.h>
53 * A bound below which cv_waiters is valid. Once cv_waiters reaches this bound,
54 * cv_signal must manually check the wait queue for threads.
56 #define CV_WAITERS_BOUND INT_MAX
58 #define CV_WAITERS_INC(cvp) do { \
59 if ((cvp)->cv_waiters < CV_WAITERS_BOUND) \
60 (cvp)->cv_waiters++; \
64 * Common sanity checks for cv_wait* functions.
66 #define CV_ASSERT(cvp, lock, td) do { \
67 KASSERT((td) != NULL, ("%s: td NULL", __func__)); \
68 KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__)); \
69 KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__)); \
70 KASSERT((lock) != NULL, ("%s: lock NULL", __func__)); \
74 * Initialize a condition variable. Must be called before use.
77 cv_init(struct cv *cvp, const char *desc)
80 cvp->cv_description = desc;
85 * Destroy a condition variable. The condition variable must be re-initialized
86 * in order to be re-used.
89 cv_destroy(struct cv *cvp)
92 struct sleepqueue *sq;
95 sq = sleepq_lookup(cvp);
97 KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__));
102 * Wait on a condition variable. The current thread is placed on the condition
103 * variable's wait queue and suspended. A cv_signal or cv_broadcast on the same
104 * condition variable will resume the thread. The mutex is released before
105 * sleeping and will be held on return. It is recommended that the mutex be
106 * held when cv_signal or cv_broadcast are called.
109 _cv_wait(struct cv *cvp, struct lock_object *lock)
111 WITNESS_SAVE_DECL(lock_witness);
112 struct lock_class *class;
114 uintptr_t lock_state;
119 if (KTRPOINT(td, KTR_CSW))
120 ktrcsw(1, 0, cv_wmesg(cvp));
122 CV_ASSERT(cvp, lock, td);
123 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
124 "Waiting on \"%s\"", cvp->cv_description);
125 class = LOCK_CLASS(lock);
127 if (SCHEDULER_STOPPED_TD(td))
133 if (lock == &Giant.lock_object)
134 mtx_assert(&Giant, MA_OWNED);
137 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
138 if (lock != &Giant.lock_object) {
139 if (class->lc_flags & LC_SLEEPABLE)
141 WITNESS_SAVE(lock, lock_witness);
142 lock_state = class->lc_unlock(lock);
143 if (class->lc_flags & LC_SLEEPABLE)
149 if (KTRPOINT(td, KTR_CSW))
150 ktrcsw(0, 0, cv_wmesg(cvp));
153 if (lock != &Giant.lock_object) {
154 class->lc_lock(lock, lock_state);
155 WITNESS_RESTORE(lock, lock_witness);
160 * Wait on a condition variable. This function differs from cv_wait by
161 * not acquiring the mutex after condition variable was signaled.
164 _cv_wait_unlock(struct cv *cvp, struct lock_object *lock)
166 struct lock_class *class;
171 if (KTRPOINT(td, KTR_CSW))
172 ktrcsw(1, 0, cv_wmesg(cvp));
174 CV_ASSERT(cvp, lock, td);
175 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
176 "Waiting on \"%s\"", cvp->cv_description);
177 KASSERT(lock != &Giant.lock_object,
178 ("cv_wait_unlock cannot be used with Giant"));
179 class = LOCK_CLASS(lock);
181 if (SCHEDULER_STOPPED_TD(td)) {
182 class->lc_unlock(lock);
191 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
192 if (class->lc_flags & LC_SLEEPABLE)
194 class->lc_unlock(lock);
195 if (class->lc_flags & LC_SLEEPABLE)
200 if (KTRPOINT(td, KTR_CSW))
201 ktrcsw(0, 0, cv_wmesg(cvp));
207 * Wait on a condition variable, allowing interruption by signals. Return 0 if
208 * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
209 * a signal was caught. If ERESTART is returned the system call should be
210 * restarted if possible.
213 _cv_wait_sig(struct cv *cvp, struct lock_object *lock)
215 WITNESS_SAVE_DECL(lock_witness);
216 struct lock_class *class;
218 uintptr_t lock_state;
224 if (KTRPOINT(td, KTR_CSW))
225 ktrcsw(1, 0, cv_wmesg(cvp));
227 CV_ASSERT(cvp, lock, td);
228 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
229 "Waiting on \"%s\"", cvp->cv_description);
230 class = LOCK_CLASS(lock);
232 if (SCHEDULER_STOPPED_TD(td))
238 if (lock == &Giant.lock_object)
239 mtx_assert(&Giant, MA_OWNED);
242 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
243 SLEEPQ_INTERRUPTIBLE, 0);
244 if (lock != &Giant.lock_object) {
245 if (class->lc_flags & LC_SLEEPABLE)
247 WITNESS_SAVE(lock, lock_witness);
248 lock_state = class->lc_unlock(lock);
249 if (class->lc_flags & LC_SLEEPABLE)
252 rval = sleepq_wait_sig(cvp, 0);
255 if (KTRPOINT(td, KTR_CSW))
256 ktrcsw(0, 0, cv_wmesg(cvp));
259 if (lock != &Giant.lock_object) {
260 class->lc_lock(lock, lock_state);
261 WITNESS_RESTORE(lock, lock_witness);
268 * Wait on a condition variable for (at most) the value specified in sbt
269 * argument. Returns 0 if the process was resumed by cv_signal or cv_broadcast,
270 * EWOULDBLOCK if the timeout expires.
273 _cv_timedwait_sbt(struct cv *cvp, struct lock_object *lock, sbintime_t sbt,
274 sbintime_t pr, int flags)
276 WITNESS_SAVE_DECL(lock_witness);
277 struct lock_class *class;
279 int lock_state, rval;
284 if (KTRPOINT(td, KTR_CSW))
285 ktrcsw(1, 0, cv_wmesg(cvp));
287 CV_ASSERT(cvp, lock, td);
288 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
289 "Waiting on \"%s\"", cvp->cv_description);
290 class = LOCK_CLASS(lock);
292 if (SCHEDULER_STOPPED_TD(td))
298 if (lock == &Giant.lock_object)
299 mtx_assert(&Giant, MA_OWNED);
302 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
303 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
304 if (lock != &Giant.lock_object) {
305 if (class->lc_flags & LC_SLEEPABLE)
307 WITNESS_SAVE(lock, lock_witness);
308 lock_state = class->lc_unlock(lock);
309 if (class->lc_flags & LC_SLEEPABLE)
312 rval = sleepq_timedwait(cvp, 0);
315 if (KTRPOINT(td, KTR_CSW))
316 ktrcsw(0, 0, cv_wmesg(cvp));
319 if (lock != &Giant.lock_object) {
320 class->lc_lock(lock, lock_state);
321 WITNESS_RESTORE(lock, lock_witness);
328 * Wait on a condition variable for (at most) the value specified in sbt
329 * argument, allowing interruption by signals.
330 * Returns 0 if the thread was resumed by cv_signal or cv_broadcast,
331 * EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if a signal
335 _cv_timedwait_sig_sbt(struct cv *cvp, struct lock_object *lock,
336 sbintime_t sbt, sbintime_t pr, int flags)
338 WITNESS_SAVE_DECL(lock_witness);
339 struct lock_class *class;
341 int lock_state, rval;
346 if (KTRPOINT(td, KTR_CSW))
347 ktrcsw(1, 0, cv_wmesg(cvp));
349 CV_ASSERT(cvp, lock, td);
350 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
351 "Waiting on \"%s\"", cvp->cv_description);
352 class = LOCK_CLASS(lock);
354 if (SCHEDULER_STOPPED_TD(td))
360 if (lock == &Giant.lock_object)
361 mtx_assert(&Giant, MA_OWNED);
364 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
365 SLEEPQ_INTERRUPTIBLE, 0);
366 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
367 if (lock != &Giant.lock_object) {
368 if (class->lc_flags & LC_SLEEPABLE)
370 WITNESS_SAVE(lock, lock_witness);
371 lock_state = class->lc_unlock(lock);
372 if (class->lc_flags & LC_SLEEPABLE)
375 rval = sleepq_timedwait_sig(cvp, 0);
378 if (KTRPOINT(td, KTR_CSW))
379 ktrcsw(0, 0, cv_wmesg(cvp));
382 if (lock != &Giant.lock_object) {
383 class->lc_lock(lock, lock_state);
384 WITNESS_RESTORE(lock, lock_witness);
391 * Signal a condition variable, wakes up one waiting thread. Will also wakeup
392 * the swapper if the process is not in memory, so that it can bring the
393 * sleeping process in. Note that this may also result in additional threads
394 * being made runnable. Should be called with the same mutex as was passed to
398 cv_signal(struct cv *cvp)
401 if (cvp->cv_waiters == 0)
404 if (cvp->cv_waiters == 0) {
408 if (cvp->cv_waiters == CV_WAITERS_BOUND && sleepq_lookup(cvp) == NULL) {
412 if (cvp->cv_waiters < CV_WAITERS_BOUND)
414 if (sleepq_signal(cvp, SLEEPQ_CONDVAR | SLEEPQ_DROP, 0, 0))
420 * Broadcast a signal to a condition variable. Wakes up all waiting threads.
421 * Should be called with the same mutex as was passed to cv_wait held.
424 cv_broadcastpri(struct cv *cvp, int pri)
428 if (cvp->cv_waiters == 0)
431 * XXX sleepq_broadcast pri argument changed from -1 meaning
432 * no pri to 0 meaning no pri.
438 if (cvp->cv_waiters > 0) {
440 wakeup_swapper = sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);