xref: /freebsd/sys/kern/kern_condvar.c (revision 503916a7c1d38ccdfbc10aa3e2e68f95b03727c8)
1238510fcSJason Evans /*-
2238510fcSJason Evans  * Copyright (c) 2000 Jake Burkholder <jake@freebsd.org>.
3238510fcSJason Evans  * All rights reserved.
4238510fcSJason Evans  *
5238510fcSJason Evans  * Redistribution and use in source and binary forms, with or without
6238510fcSJason Evans  * modification, are permitted provided that the following conditions
7238510fcSJason Evans  * are met:
8238510fcSJason Evans  * 1. Redistributions of source code must retain the above copyright
9238510fcSJason Evans  *    notice, this list of conditions and the following disclaimer.
10238510fcSJason Evans  * 2. Redistributions in binary form must reproduce the above copyright
11238510fcSJason Evans  *    notice, this list of conditions and the following disclaimer in the
12238510fcSJason Evans  *    documentation and/or other materials provided with the distribution.
13238510fcSJason Evans  *
14238510fcSJason Evans  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15238510fcSJason Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16238510fcSJason Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17238510fcSJason Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18238510fcSJason Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19238510fcSJason Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20238510fcSJason Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21238510fcSJason Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22238510fcSJason Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23238510fcSJason Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24238510fcSJason Evans  * SUCH DAMAGE.
25238510fcSJason Evans  */
26238510fcSJason Evans 
27677b542eSDavid E. O'Brien #include <sys/cdefs.h>
28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
29677b542eSDavid E. O'Brien 
30238510fcSJason Evans #include "opt_ktrace.h"
31238510fcSJason Evans 
32238510fcSJason Evans #include <sys/param.h>
33238510fcSJason Evans #include <sys/systm.h>
34fb919e4dSMark Murray #include <sys/lock.h>
35fb919e4dSMark Murray #include <sys/mutex.h>
36238510fcSJason Evans #include <sys/proc.h>
37238510fcSJason Evans #include <sys/kernel.h>
38238510fcSJason Evans #include <sys/ktr.h>
39238510fcSJason Evans #include <sys/condvar.h>
404e997f4bSJeff Roberson #include <sys/sched.h>
41238510fcSJason Evans #include <sys/signalvar.h>
4244f3b092SJohn Baldwin #include <sys/sleepqueue.h>
43238510fcSJason Evans #include <sys/resourcevar.h>
44238510fcSJason Evans #ifdef KTRACE
45238510fcSJason Evans #include <sys/uio.h>
46238510fcSJason Evans #include <sys/ktrace.h>
47238510fcSJason Evans #endif
48238510fcSJason Evans 
49238510fcSJason Evans /*
50238510fcSJason Evans  * Common sanity checks for cv_wait* functions.
51238510fcSJason Evans  */
52b40ce416SJulian Elischer #define	CV_ASSERT(cvp, mp, td) do {					\
53a48740b6SDavid E. O'Brien 	KASSERT((td) != NULL, ("%s: curthread NULL", __func__));	\
5471fad9fdSJulian Elischer 	KASSERT(TD_IS_RUNNING(td), ("%s: not TDS_RUNNING", __func__));	\
55a48740b6SDavid E. O'Brien 	KASSERT((cvp) != NULL, ("%s: cvp NULL", __func__));		\
56a48740b6SDavid E. O'Brien 	KASSERT((mp) != NULL, ("%s: mp NULL", __func__));		\
57238510fcSJason Evans 	mtx_assert((mp), MA_OWNED | MA_NOTRECURSED);			\
58238510fcSJason Evans } while (0)
59238510fcSJason Evans 
60238510fcSJason Evans /*
61238510fcSJason Evans  * Initialize a condition variable.  Must be called before use.
62238510fcSJason Evans  */
63238510fcSJason Evans void
64238510fcSJason Evans cv_init(struct cv *cvp, const char *desc)
65238510fcSJason Evans {
66238510fcSJason Evans 
67238510fcSJason Evans 	cvp->cv_description = desc;
689000d57dSJohn Baldwin 	cvp->cv_waiters = 0;
69238510fcSJason Evans }
70238510fcSJason Evans 
71238510fcSJason Evans /*
72238510fcSJason Evans  * Destroy a condition variable.  The condition variable must be re-initialized
73238510fcSJason Evans  * in order to be re-used.
74238510fcSJason Evans  */
75238510fcSJason Evans void
76238510fcSJason Evans cv_destroy(struct cv *cvp)
77238510fcSJason Evans {
7844f3b092SJohn Baldwin #ifdef INVARIANTS
7944f3b092SJohn Baldwin 	struct sleepqueue *sq;
80238510fcSJason Evans 
812ff0e645SJohn Baldwin 	sleepq_lock(cvp);
8244f3b092SJohn Baldwin 	sq = sleepq_lookup(cvp);
8344f3b092SJohn Baldwin 	sleepq_release(cvp);
8444f3b092SJohn Baldwin 	KASSERT(sq == NULL, ("%s: associated sleep queue non-empty", __func__));
8544f3b092SJohn Baldwin #endif
86238510fcSJason Evans }
87238510fcSJason Evans 
88238510fcSJason Evans /*
89b40ce416SJulian Elischer  * Wait on a condition variable.  The current thread is placed on the condition
90238510fcSJason Evans  * variable's wait queue and suspended.  A cv_signal or cv_broadcast on the same
91b40ce416SJulian Elischer  * condition variable will resume the thread.  The mutex is released before
92238510fcSJason Evans  * sleeping and will be held on return.  It is recommended that the mutex be
93238510fcSJason Evans  * held when cv_signal or cv_broadcast are called.
94238510fcSJason Evans  */
95238510fcSJason Evans void
96238510fcSJason Evans cv_wait(struct cv *cvp, struct mtx *mp)
97238510fcSJason Evans {
98238510fcSJason Evans 	WITNESS_SAVE_DECL(mp);
99503916a7SJohn Baldwin 	struct thread *td;
100238510fcSJason Evans 
101503916a7SJohn Baldwin 	td = curthread;
102503916a7SJohn Baldwin #ifdef KTRACE
103503916a7SJohn Baldwin 	if (KTRPOINT(td, KTR_CSW))
104503916a7SJohn Baldwin 		ktrcsw(1, 0);
105503916a7SJohn Baldwin #endif
106503916a7SJohn Baldwin 	CV_ASSERT(cvp, mp, td);
107503916a7SJohn Baldwin 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
108503916a7SJohn Baldwin 	    "Waiting on \"%s\"", cvp->cv_description);
10919284646SJohn Baldwin 	WITNESS_SAVE(&mp->mtx_object, mp);
110238510fcSJason Evans 
11144f3b092SJohn Baldwin 	if (cold || panicstr) {
112238510fcSJason Evans 		/*
113fe799533SAndrew Gallatin 		 * During autoconfiguration, just give interrupts
114fe799533SAndrew Gallatin 		 * a chance, then just return.  Don't run any other
115fe799533SAndrew Gallatin 		 * thread or panic below, in case this is the idle
116fe799533SAndrew Gallatin 		 * process and already asleep.
117238510fcSJason Evans 		 */
118238510fcSJason Evans 		return;
119238510fcSJason Evans 	}
1204bc37205SJeffrey Hsu 
121503916a7SJohn Baldwin 	sleepq_lock(cvp);
122503916a7SJohn Baldwin 
123503916a7SJohn Baldwin 	cvp->cv_waiters++;
124503916a7SJohn Baldwin 	DROP_GIANT();
125503916a7SJohn Baldwin 	mtx_unlock(mp);
126503916a7SJohn Baldwin 
127503916a7SJohn Baldwin 	sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR,
128503916a7SJohn Baldwin 		   0);
129503916a7SJohn Baldwin 	sleepq_wait(cvp);
130503916a7SJohn Baldwin 
131503916a7SJohn Baldwin #ifdef KTRACE
132503916a7SJohn Baldwin 	if (KTRPOINT(td, KTR_CSW))
133503916a7SJohn Baldwin 		ktrcsw(0, 0);
134503916a7SJohn Baldwin #endif
135503916a7SJohn Baldwin 	PICKUP_GIANT();
13692f44a3fSCraig Rodrigues 	mtx_lock(mp);
13792f44a3fSCraig Rodrigues 	WITNESS_RESTORE(&mp->mtx_object, mp);
13892f44a3fSCraig Rodrigues }
13992f44a3fSCraig Rodrigues 
14092f44a3fSCraig Rodrigues /*
14192f44a3fSCraig Rodrigues  * Wait on a condition variable.  This function differs from cv_wait by
14292f44a3fSCraig Rodrigues  * not aquiring the mutex after condition variable was signaled.
14392f44a3fSCraig Rodrigues  */
14492f44a3fSCraig Rodrigues void
14592f44a3fSCraig Rodrigues cv_wait_unlock(struct cv *cvp, struct mtx *mp)
14692f44a3fSCraig Rodrigues {
14792f44a3fSCraig Rodrigues 	struct thread *td;
14892f44a3fSCraig Rodrigues 
14992f44a3fSCraig Rodrigues 	td = curthread;
15092f44a3fSCraig Rodrigues #ifdef KTRACE
15192f44a3fSCraig Rodrigues 	if (KTRPOINT(td, KTR_CSW))
15292f44a3fSCraig Rodrigues 		ktrcsw(1, 0);
15392f44a3fSCraig Rodrigues #endif
15492f44a3fSCraig Rodrigues 	CV_ASSERT(cvp, mp, td);
15592f44a3fSCraig Rodrigues 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
15692f44a3fSCraig Rodrigues 	    "Waiting on \"%s\"", cvp->cv_description);
15792f44a3fSCraig Rodrigues 
15892f44a3fSCraig Rodrigues 	if (cold || panicstr) {
15992f44a3fSCraig Rodrigues 		/*
16092f44a3fSCraig Rodrigues 		 * During autoconfiguration, just give interrupts
16192f44a3fSCraig Rodrigues 		 * a chance, then just return.  Don't run any other
16292f44a3fSCraig Rodrigues 		 * thread or panic below, in case this is the idle
16392f44a3fSCraig Rodrigues 		 * process and already asleep.
16492f44a3fSCraig Rodrigues 		 */
16592f44a3fSCraig Rodrigues 		mtx_unlock(mp);
16692f44a3fSCraig Rodrigues 		return;
16792f44a3fSCraig Rodrigues 	}
16892f44a3fSCraig Rodrigues 
1692ff0e645SJohn Baldwin 	sleepq_lock(cvp);
170238510fcSJason Evans 
1719000d57dSJohn Baldwin 	cvp->cv_waiters++;
172c86b6ff5SJohn Baldwin 	DROP_GIANT();
173c86b6ff5SJohn Baldwin 	mtx_unlock(mp);
174238510fcSJason Evans 
1756cbb70e2SKip Macy 	sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR,
1766cbb70e2SKip Macy 		   0);
17744f3b092SJohn Baldwin 	sleepq_wait(cvp);
178238510fcSJason Evans 
179503916a7SJohn Baldwin #ifdef KTRACE
180503916a7SJohn Baldwin 	if (KTRPOINT(td, KTR_CSW))
181503916a7SJohn Baldwin 		ktrcsw(0, 0);
182503916a7SJohn Baldwin #endif
183238510fcSJason Evans 	PICKUP_GIANT();
184238510fcSJason Evans }
185238510fcSJason Evans 
186238510fcSJason Evans /*
187238510fcSJason Evans  * Wait on a condition variable, allowing interruption by signals.  Return 0 if
188b40ce416SJulian Elischer  * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if
189238510fcSJason Evans  * a signal was caught.  If ERESTART is returned the system call should be
190238510fcSJason Evans  * restarted if possible.
191238510fcSJason Evans  */
192238510fcSJason Evans int
193238510fcSJason Evans cv_wait_sig(struct cv *cvp, struct mtx *mp)
194238510fcSJason Evans {
195b40ce416SJulian Elischer 	struct thread *td;
19666f769feSPeter Wemm 	struct proc *p;
19794f0972bSDavid Xu 	int rval;
198238510fcSJason Evans 	WITNESS_SAVE_DECL(mp);
199238510fcSJason Evans 
200b40ce416SJulian Elischer 	td = curthread;
2019ef3a985SJohn Baldwin 	p = td->td_proc;
202238510fcSJason Evans #ifdef KTRACE
2039ba7fe1bSJohn Baldwin 	if (KTRPOINT(td, KTR_CSW))
2049ba7fe1bSJohn Baldwin 		ktrcsw(1, 0);
205238510fcSJason Evans #endif
206b40ce416SJulian Elischer 	CV_ASSERT(cvp, mp, td);
20726306795SJohn Baldwin 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
20826306795SJohn Baldwin 	    "Waiting on \"%s\"", cvp->cv_description);
20919284646SJohn Baldwin 	WITNESS_SAVE(&mp->mtx_object, mp);
210238510fcSJason Evans 
211238510fcSJason Evans 	if (cold || panicstr) {
212238510fcSJason Evans 		/*
213238510fcSJason Evans 		 * After a panic, or during autoconfiguration, just give
214238510fcSJason Evans 		 * interrupts a chance, then just return; don't run any other
215238510fcSJason Evans 		 * procs or panic below, in case this is the idle process and
216238510fcSJason Evans 		 * already asleep.
217238510fcSJason Evans 		 */
218274f8f48SJohn Baldwin 		return (0);
219238510fcSJason Evans 	}
2204bc37205SJeffrey Hsu 
2212ff0e645SJohn Baldwin 	sleepq_lock(cvp);
2224bc37205SJeffrey Hsu 
2239000d57dSJohn Baldwin 	cvp->cv_waiters++;
224c86b6ff5SJohn Baldwin 	DROP_GIANT();
225c86b6ff5SJohn Baldwin 	mtx_unlock(mp);
226238510fcSJason Evans 
2277ee07175SPawel Jakub Dawidek 	sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR |
2286cbb70e2SKip Macy 	    SLEEPQ_INTERRUPTIBLE, 0);
22944f3b092SJohn Baldwin 	rval = sleepq_wait_sig(cvp);
230238510fcSJason Evans 
231238510fcSJason Evans #ifdef KTRACE
2329ba7fe1bSJohn Baldwin 	if (KTRPOINT(td, KTR_CSW))
2339ba7fe1bSJohn Baldwin 		ktrcsw(0, 0);
234238510fcSJason Evans #endif
2359ba7fe1bSJohn Baldwin 	PICKUP_GIANT();
2369ed346baSBosko Milekic 	mtx_lock(mp);
23719284646SJohn Baldwin 	WITNESS_RESTORE(&mp->mtx_object, mp);
238238510fcSJason Evans 
239238510fcSJason Evans 	return (rval);
240238510fcSJason Evans }
241238510fcSJason Evans 
242238510fcSJason Evans /*
243238510fcSJason Evans  * Wait on a condition variable for at most timo/hz seconds.  Returns 0 if the
244238510fcSJason Evans  * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout
245238510fcSJason Evans  * expires.
246238510fcSJason Evans  */
247238510fcSJason Evans int
248238510fcSJason Evans cv_timedwait(struct cv *cvp, struct mtx *mp, int timo)
249238510fcSJason Evans {
250b40ce416SJulian Elischer 	struct thread *td;
251238510fcSJason Evans 	int rval;
252238510fcSJason Evans 	WITNESS_SAVE_DECL(mp);
253238510fcSJason Evans 
254b40ce416SJulian Elischer 	td = curthread;
255238510fcSJason Evans 	rval = 0;
256238510fcSJason Evans #ifdef KTRACE
2579ba7fe1bSJohn Baldwin 	if (KTRPOINT(td, KTR_CSW))
2589ba7fe1bSJohn Baldwin 		ktrcsw(1, 0);
259238510fcSJason Evans #endif
260b40ce416SJulian Elischer 	CV_ASSERT(cvp, mp, td);
26126306795SJohn Baldwin 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
26226306795SJohn Baldwin 	    "Waiting on \"%s\"", cvp->cv_description);
26319284646SJohn Baldwin 	WITNESS_SAVE(&mp->mtx_object, mp);
264238510fcSJason Evans 
265238510fcSJason Evans 	if (cold || panicstr) {
266238510fcSJason Evans 		/*
267238510fcSJason Evans 		 * After a panic, or during autoconfiguration, just give
268238510fcSJason Evans 		 * interrupts a chance, then just return; don't run any other
269b40ce416SJulian Elischer 		 * thread or panic below, in case this is the idle process and
270238510fcSJason Evans 		 * already asleep.
271238510fcSJason Evans 		 */
272238510fcSJason Evans 		return 0;
273238510fcSJason Evans 	}
2744bc37205SJeffrey Hsu 
2752ff0e645SJohn Baldwin 	sleepq_lock(cvp);
276238510fcSJason Evans 
2779000d57dSJohn Baldwin 	cvp->cv_waiters++;
278c86b6ff5SJohn Baldwin 	DROP_GIANT();
279c86b6ff5SJohn Baldwin 	mtx_unlock(mp);
280238510fcSJason Evans 
2816cbb70e2SKip Macy 	sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR,
2826cbb70e2SKip Macy 		   0);
2831ed3e44fSJohn Baldwin 	sleepq_set_timeout(cvp, timo);
284a5471e4eSJohn Baldwin 	rval = sleepq_timedwait(cvp);
285238510fcSJason Evans 
286238510fcSJason Evans #ifdef KTRACE
2879ba7fe1bSJohn Baldwin 	if (KTRPOINT(td, KTR_CSW))
2889ba7fe1bSJohn Baldwin 		ktrcsw(0, 0);
289238510fcSJason Evans #endif
290238510fcSJason Evans 	PICKUP_GIANT();
2919ed346baSBosko Milekic 	mtx_lock(mp);
29219284646SJohn Baldwin 	WITNESS_RESTORE(&mp->mtx_object, mp);
293238510fcSJason Evans 
294238510fcSJason Evans 	return (rval);
295238510fcSJason Evans }
296238510fcSJason Evans 
297238510fcSJason Evans /*
298238510fcSJason Evans  * Wait on a condition variable for at most timo/hz seconds, allowing
299b40ce416SJulian Elischer  * interruption by signals.  Returns 0 if the thread was resumed by cv_signal
300238510fcSJason Evans  * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if
301238510fcSJason Evans  * a signal was caught.
302238510fcSJason Evans  */
303238510fcSJason Evans int
304238510fcSJason Evans cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo)
305238510fcSJason Evans {
306b40ce416SJulian Elischer 	struct thread *td;
3079ef3a985SJohn Baldwin 	struct proc *p;
308238510fcSJason Evans 	int rval;
309238510fcSJason Evans 	WITNESS_SAVE_DECL(mp);
310238510fcSJason Evans 
311b40ce416SJulian Elischer 	td = curthread;
3129ef3a985SJohn Baldwin 	p = td->td_proc;
313238510fcSJason Evans 	rval = 0;
314238510fcSJason Evans #ifdef KTRACE
3159ba7fe1bSJohn Baldwin 	if (KTRPOINT(td, KTR_CSW))
3169ba7fe1bSJohn Baldwin 		ktrcsw(1, 0);
317238510fcSJason Evans #endif
318b40ce416SJulian Elischer 	CV_ASSERT(cvp, mp, td);
31926306795SJohn Baldwin 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object,
32026306795SJohn Baldwin 	    "Waiting on \"%s\"", cvp->cv_description);
32119284646SJohn Baldwin 	WITNESS_SAVE(&mp->mtx_object, mp);
322238510fcSJason Evans 
323238510fcSJason Evans 	if (cold || panicstr) {
324238510fcSJason Evans 		/*
325238510fcSJason Evans 		 * After a panic, or during autoconfiguration, just give
326238510fcSJason Evans 		 * interrupts a chance, then just return; don't run any other
327b40ce416SJulian Elischer 		 * thread or panic below, in case this is the idle process and
328238510fcSJason Evans 		 * already asleep.
329238510fcSJason Evans 		 */
330238510fcSJason Evans 		return 0;
331238510fcSJason Evans 	}
3324bc37205SJeffrey Hsu 
3332ff0e645SJohn Baldwin 	sleepq_lock(cvp);
334238510fcSJason Evans 
3359000d57dSJohn Baldwin 	cvp->cv_waiters++;
336c86b6ff5SJohn Baldwin 	DROP_GIANT();
337c86b6ff5SJohn Baldwin 	mtx_unlock(mp);
338238510fcSJason Evans 
3397ee07175SPawel Jakub Dawidek 	sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR |
3406cbb70e2SKip Macy 	    SLEEPQ_INTERRUPTIBLE, 0);
3411ed3e44fSJohn Baldwin 	sleepq_set_timeout(cvp, timo);
34294f0972bSDavid Xu 	rval = sleepq_timedwait_sig(cvp);
343238510fcSJason Evans 
344238510fcSJason Evans #ifdef KTRACE
3459ba7fe1bSJohn Baldwin 	if (KTRPOINT(td, KTR_CSW))
3469ba7fe1bSJohn Baldwin 		ktrcsw(0, 0);
347238510fcSJason Evans #endif
3489ba7fe1bSJohn Baldwin 	PICKUP_GIANT();
3499ed346baSBosko Milekic 	mtx_lock(mp);
35019284646SJohn Baldwin 	WITNESS_RESTORE(&mp->mtx_object, mp);
351238510fcSJason Evans 
352238510fcSJason Evans 	return (rval);
353238510fcSJason Evans }
354238510fcSJason Evans 
355238510fcSJason Evans /*
356b40ce416SJulian Elischer  * Signal a condition variable, wakes up one waiting thread.  Will also wakeup
357238510fcSJason Evans  * the swapper if the process is not in memory, so that it can bring the
358b40ce416SJulian Elischer  * sleeping process in.  Note that this may also result in additional threads
359238510fcSJason Evans  * being made runnable.  Should be called with the same mutex as was passed to
360238510fcSJason Evans  * cv_wait held.
361238510fcSJason Evans  */
362238510fcSJason Evans void
363238510fcSJason Evans cv_signal(struct cv *cvp)
364238510fcSJason Evans {
365238510fcSJason Evans 
3662ff0e645SJohn Baldwin 	sleepq_lock(cvp);
3679000d57dSJohn Baldwin 	if (cvp->cv_waiters > 0) {
3689000d57dSJohn Baldwin 		cvp->cv_waiters--;
3696cbb70e2SKip Macy 		sleepq_signal(cvp, SLEEPQ_CONDVAR, -1, 0);
3702ff0e645SJohn Baldwin 	} else
3712ff0e645SJohn Baldwin 		sleepq_release(cvp);
3729000d57dSJohn Baldwin }
373238510fcSJason Evans 
374238510fcSJason Evans /*
375b40ce416SJulian Elischer  * Broadcast a signal to a condition variable.  Wakes up all waiting threads.
376238510fcSJason Evans  * Should be called with the same mutex as was passed to cv_wait held.
377238510fcSJason Evans  */
378238510fcSJason Evans void
379512824f8SSeigo Tanimura cv_broadcastpri(struct cv *cvp, int pri)
380238510fcSJason Evans {
381238510fcSJason Evans 
3822ff0e645SJohn Baldwin 	sleepq_lock(cvp);
3839000d57dSJohn Baldwin 	if (cvp->cv_waiters > 0) {
3849000d57dSJohn Baldwin 		cvp->cv_waiters = 0;
3856cbb70e2SKip Macy 		sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0);
3862ff0e645SJohn Baldwin 	} else
3872ff0e645SJohn Baldwin 		sleepq_release(cvp);
3889000d57dSJohn Baldwin }
389