2 * SPDX-License-Identifier: BSD-2-Clause
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 #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/ktrace.h>
41 #include <sys/condvar.h>
42 #include <sys/sched.h>
43 #include <sys/signalvar.h>
44 #include <sys/sleepqueue.h>
45 #include <sys/resourcevar.h>
52 * A bound below which cv_waiters is valid. Once cv_waiters reaches this bound,
53 * cv_signal must manually check the wait queue for threads.
55 #define CV_WAITERS_BOUND INT_MAX
57 #define CV_WAITERS_INC(cvp) do { \
58 if ((cvp)->cv_waiters < CV_WAITERS_BOUND) \
59 (cvp)->cv_waiters++; \
63 * Common sanity checks for cv_wait* functions.
65 #define CV_ASSERT(cvp, lock, td) do { \
66 KASSERT((td) != NULL, ("%s: td NULL", __func__)); \
67 KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__)); \
68 KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__)); \
69 KASSERT((lock) != NULL, ("%s: lock NULL", __func__)); \
73 * Initialize a condition variable. Must be called before use.
76 cv_init(struct cv *cvp, const char *desc)
79 cvp->cv_description = desc;
84 * Destroy a condition variable. The condition variable must be re-initialized
85 * in order to be re-used.
88 cv_destroy(struct cv *cvp)
91 struct sleepqueue *sq;
94 sq = sleepq_lookup(cvp);
96 KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__));
101 * Wait on a condition variable. The current thread is placed on the condition
102 * variable's wait queue and suspended. A cv_signal or cv_broadcast on the same
103 * condition variable will resume the thread. The mutex is released before
104 * sleeping and will be held on return. It is recommended that the mutex be
105 * held when cv_signal or cv_broadcast are called.
108 _cv_wait(struct cv *cvp, struct lock_object *lock)
110 WITNESS_SAVE_DECL(lock_witness);
112 char wmesg[WMESGLEN + 1];
114 struct lock_class *class;
115 struct thread *td __ktrace_used;
116 uintptr_t lock_state;
119 CV_ASSERT(cvp, lock, td);
120 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
121 "Waiting on \"%s\"", cvp->cv_description);
123 if (SCHEDULER_STOPPED())
127 if (KTRPOINT(td, KTR_CSW)) {
128 strlcpy(wmesg, cv_wmesg(cvp), sizeof(wmesg));
135 class = LOCK_CLASS(lock);
140 if (lock == &Giant.lock_object)
141 mtx_assert(&Giant, MA_OWNED);
144 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
145 if (lock != &Giant.lock_object) {
146 if (class->lc_flags & LC_SLEEPABLE)
148 WITNESS_SAVE(lock, lock_witness);
149 lock_state = class->lc_unlock(lock);
150 if (class->lc_flags & LC_SLEEPABLE)
156 if (KTRPOINT(td, KTR_CSW))
160 if (lock != &Giant.lock_object) {
161 class->lc_lock(lock, lock_state);
162 WITNESS_RESTORE(lock, lock_witness);
167 * Wait on a condition variable. This function differs from cv_wait by
168 * not acquiring the mutex after condition variable was signaled.
171 _cv_wait_unlock(struct cv *cvp, struct lock_object *lock)
174 char wmesg[WMESGLEN + 1];
176 struct lock_class *class;
177 struct thread *td __ktrace_used;
180 CV_ASSERT(cvp, lock, td);
181 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
182 "Waiting on \"%s\"", cvp->cv_description);
183 KASSERT(lock != &Giant.lock_object,
184 ("cv_wait_unlock cannot be used with Giant"));
185 class = LOCK_CLASS(lock);
187 if (SCHEDULER_STOPPED()) {
188 class->lc_unlock(lock);
193 if (KTRPOINT(td, KTR_CSW)) {
194 strlcpy(wmesg, cv_wmesg(cvp), sizeof(wmesg));
206 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
207 if (class->lc_flags & LC_SLEEPABLE)
209 class->lc_unlock(lock);
210 if (class->lc_flags & LC_SLEEPABLE)
215 if (KTRPOINT(td, KTR_CSW))
222 * Wait on a condition variable, allowing interruption by signals. Return 0 if
223 * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
224 * a signal was caught. If ERESTART is returned the system call should be
225 * restarted if possible.
228 _cv_wait_sig(struct cv *cvp, struct lock_object *lock)
230 WITNESS_SAVE_DECL(lock_witness);
232 char wmesg[WMESGLEN + 1];
234 struct lock_class *class;
235 struct thread *td __ktrace_used;
236 uintptr_t lock_state;
240 CV_ASSERT(cvp, lock, td);
241 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
242 "Waiting on \"%s\"", cvp->cv_description);
244 if (SCHEDULER_STOPPED())
248 if (KTRPOINT(td, KTR_CSW)) {
249 strlcpy(wmesg, cv_wmesg(cvp), sizeof(wmesg));
256 class = LOCK_CLASS(lock);
261 if (lock == &Giant.lock_object)
262 mtx_assert(&Giant, MA_OWNED);
265 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
266 SLEEPQ_INTERRUPTIBLE, 0);
267 if (lock != &Giant.lock_object) {
268 if (class->lc_flags & LC_SLEEPABLE)
270 WITNESS_SAVE(lock, lock_witness);
271 lock_state = class->lc_unlock(lock);
272 if (class->lc_flags & LC_SLEEPABLE)
275 rval = sleepq_wait_sig(cvp, 0);
278 if (KTRPOINT(td, KTR_CSW))
282 if (lock != &Giant.lock_object) {
283 class->lc_lock(lock, lock_state);
284 WITNESS_RESTORE(lock, lock_witness);
291 * Wait on a condition variable for (at most) the value specified in sbt
292 * argument. Returns 0 if the process was resumed by cv_signal or cv_broadcast,
293 * EWOULDBLOCK if the timeout expires.
296 _cv_timedwait_sbt(struct cv *cvp, struct lock_object *lock, sbintime_t sbt,
297 sbintime_t pr, int flags)
299 WITNESS_SAVE_DECL(lock_witness);
301 char wmesg[WMESGLEN + 1];
303 struct lock_class *class;
304 struct thread *td __ktrace_used;
305 int lock_state, rval;
308 CV_ASSERT(cvp, lock, td);
309 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
310 "Waiting on \"%s\"", cvp->cv_description);
312 if (SCHEDULER_STOPPED())
316 if (KTRPOINT(td, KTR_CSW)) {
317 strlcpy(wmesg, cv_wmesg(cvp), sizeof(wmesg));
324 class = LOCK_CLASS(lock);
329 if (lock == &Giant.lock_object)
330 mtx_assert(&Giant, MA_OWNED);
333 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR, 0);
334 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
335 if (lock != &Giant.lock_object) {
336 if (class->lc_flags & LC_SLEEPABLE)
338 WITNESS_SAVE(lock, lock_witness);
339 lock_state = class->lc_unlock(lock);
340 if (class->lc_flags & LC_SLEEPABLE)
343 rval = sleepq_timedwait(cvp, 0);
346 if (KTRPOINT(td, KTR_CSW))
350 if (lock != &Giant.lock_object) {
351 class->lc_lock(lock, lock_state);
352 WITNESS_RESTORE(lock, lock_witness);
359 * Wait on a condition variable for (at most) the value specified in sbt
360 * argument, allowing interruption by signals.
361 * Returns 0 if the thread was resumed by cv_signal or cv_broadcast,
362 * EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if a signal
366 _cv_timedwait_sig_sbt(struct cv *cvp, struct lock_object *lock,
367 sbintime_t sbt, sbintime_t pr, int flags)
369 WITNESS_SAVE_DECL(lock_witness);
371 char wmesg[WMESGLEN + 1];
373 struct lock_class *class;
374 struct thread *td __ktrace_used;
375 int lock_state, rval;
378 CV_ASSERT(cvp, lock, td);
379 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
380 "Waiting on \"%s\"", cvp->cv_description);
382 if (SCHEDULER_STOPPED())
386 if (KTRPOINT(td, KTR_CSW)) {
387 strlcpy(wmesg, cv_wmesg(cvp), sizeof(wmesg));
394 class = LOCK_CLASS(lock);
399 if (lock == &Giant.lock_object)
400 mtx_assert(&Giant, MA_OWNED);
403 sleepq_add(cvp, lock, cvp->cv_description, SLEEPQ_CONDVAR |
404 SLEEPQ_INTERRUPTIBLE, 0);
405 sleepq_set_timeout_sbt(cvp, sbt, pr, flags);
406 if (lock != &Giant.lock_object) {
407 if (class->lc_flags & LC_SLEEPABLE)
409 WITNESS_SAVE(lock, lock_witness);
410 lock_state = class->lc_unlock(lock);
411 if (class->lc_flags & LC_SLEEPABLE)
414 rval = sleepq_timedwait_sig(cvp, 0);
417 if (KTRPOINT(td, KTR_CSW))
421 if (lock != &Giant.lock_object) {
422 class->lc_lock(lock, lock_state);
423 WITNESS_RESTORE(lock, lock_witness);
430 * Signal a condition variable, wakes up one waiting thread. Will also wakeup
431 * the swapper if the process is not in memory, so that it can bring the
432 * sleeping process in. Note that this may also result in additional threads
433 * being made runnable. Should be called with the same mutex as was passed to
437 cv_signal(struct cv *cvp)
440 if (cvp->cv_waiters == 0)
443 if (cvp->cv_waiters == 0) {
447 if (cvp->cv_waiters == CV_WAITERS_BOUND && sleepq_lookup(cvp) == NULL) {
451 if (cvp->cv_waiters < CV_WAITERS_BOUND)
453 if (sleepq_signal(cvp, SLEEPQ_CONDVAR | SLEEPQ_DROP, 0, 0))
459 * Broadcast a signal to a condition variable. Wakes up all waiting threads.
460 * Should be called with the same mutex as was passed to cv_wait held.
463 cv_broadcastpri(struct cv *cvp, int pri)
467 if (cvp->cv_waiters == 0)
470 * XXX sleepq_broadcast pri argument changed from -1 meaning
471 * no pri to 0 meaning no pri.
477 if (cvp->cv_waiters > 0) {
479 wakeup_swapper = sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);