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); 99238510fcSJason Evans 10019284646SJohn Baldwin WITNESS_SAVE(&mp->mtx_object, mp); 101238510fcSJason Evans 10244f3b092SJohn Baldwin if (cold || panicstr) { 103238510fcSJason Evans /* 104fe799533SAndrew Gallatin * During autoconfiguration, just give interrupts 105fe799533SAndrew Gallatin * a chance, then just return. Don't run any other 106fe799533SAndrew Gallatin * thread or panic below, in case this is the idle 107fe799533SAndrew Gallatin * process and already asleep. 108238510fcSJason Evans */ 109238510fcSJason Evans return; 110238510fcSJason Evans } 1114bc37205SJeffrey Hsu 11292f44a3fSCraig Rodrigues cv_wait_unlock(cvp, mp); 11392f44a3fSCraig Rodrigues mtx_lock(mp); 11492f44a3fSCraig Rodrigues WITNESS_RESTORE(&mp->mtx_object, mp); 11592f44a3fSCraig Rodrigues } 11692f44a3fSCraig Rodrigues 11792f44a3fSCraig Rodrigues /* 11892f44a3fSCraig Rodrigues * Wait on a condition variable. This function differs from cv_wait by 11992f44a3fSCraig Rodrigues * not aquiring the mutex after condition variable was signaled. 12092f44a3fSCraig Rodrigues */ 12192f44a3fSCraig Rodrigues void 12292f44a3fSCraig Rodrigues cv_wait_unlock(struct cv *cvp, struct mtx *mp) 12392f44a3fSCraig Rodrigues { 12492f44a3fSCraig Rodrigues struct thread *td; 12592f44a3fSCraig Rodrigues 12692f44a3fSCraig Rodrigues td = curthread; 12792f44a3fSCraig Rodrigues #ifdef KTRACE 12892f44a3fSCraig Rodrigues if (KTRPOINT(td, KTR_CSW)) 12992f44a3fSCraig Rodrigues ktrcsw(1, 0); 13092f44a3fSCraig Rodrigues #endif 13192f44a3fSCraig Rodrigues CV_ASSERT(cvp, mp, td); 13292f44a3fSCraig Rodrigues WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object, 13392f44a3fSCraig Rodrigues "Waiting on \"%s\"", cvp->cv_description); 13492f44a3fSCraig Rodrigues 13592f44a3fSCraig Rodrigues if (cold || panicstr) { 13692f44a3fSCraig Rodrigues /* 13792f44a3fSCraig Rodrigues * During autoconfiguration, just give interrupts 13892f44a3fSCraig Rodrigues * a chance, then just return. Don't run any other 13992f44a3fSCraig Rodrigues * thread or panic below, in case this is the idle 14092f44a3fSCraig Rodrigues * process and already asleep. 14192f44a3fSCraig Rodrigues */ 14292f44a3fSCraig Rodrigues mtx_unlock(mp); 14392f44a3fSCraig Rodrigues return; 14492f44a3fSCraig Rodrigues } 14592f44a3fSCraig Rodrigues 1462ff0e645SJohn Baldwin sleepq_lock(cvp); 147238510fcSJason Evans 1489000d57dSJohn Baldwin cvp->cv_waiters++; 149c86b6ff5SJohn Baldwin DROP_GIANT(); 150c86b6ff5SJohn Baldwin mtx_unlock(mp); 151238510fcSJason Evans 1526cbb70e2SKip Macy sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR, 1536cbb70e2SKip Macy 0); 15444f3b092SJohn Baldwin sleepq_wait(cvp); 155238510fcSJason Evans 156238510fcSJason Evans PICKUP_GIANT(); 157238510fcSJason Evans } 158238510fcSJason Evans 159238510fcSJason Evans /* 160238510fcSJason Evans * Wait on a condition variable, allowing interruption by signals. Return 0 if 161b40ce416SJulian Elischer * the thread was resumed with cv_signal or cv_broadcast, EINTR or ERESTART if 162238510fcSJason Evans * a signal was caught. If ERESTART is returned the system call should be 163238510fcSJason Evans * restarted if possible. 164238510fcSJason Evans */ 165238510fcSJason Evans int 166238510fcSJason Evans cv_wait_sig(struct cv *cvp, struct mtx *mp) 167238510fcSJason Evans { 168b40ce416SJulian Elischer struct thread *td; 16966f769feSPeter Wemm struct proc *p; 17094f0972bSDavid Xu int rval; 171238510fcSJason Evans WITNESS_SAVE_DECL(mp); 172238510fcSJason Evans 173b40ce416SJulian Elischer td = curthread; 1749ef3a985SJohn Baldwin p = td->td_proc; 175238510fcSJason Evans #ifdef KTRACE 1769ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 1779ba7fe1bSJohn Baldwin ktrcsw(1, 0); 178238510fcSJason Evans #endif 179b40ce416SJulian Elischer CV_ASSERT(cvp, mp, td); 18026306795SJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object, 18126306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 18219284646SJohn Baldwin WITNESS_SAVE(&mp->mtx_object, mp); 183238510fcSJason Evans 184238510fcSJason Evans if (cold || panicstr) { 185238510fcSJason Evans /* 186238510fcSJason Evans * After a panic, or during autoconfiguration, just give 187238510fcSJason Evans * interrupts a chance, then just return; don't run any other 188238510fcSJason Evans * procs or panic below, in case this is the idle process and 189238510fcSJason Evans * already asleep. 190238510fcSJason Evans */ 191274f8f48SJohn Baldwin return (0); 192238510fcSJason Evans } 1934bc37205SJeffrey Hsu 1942ff0e645SJohn Baldwin sleepq_lock(cvp); 1954bc37205SJeffrey Hsu 1969000d57dSJohn Baldwin cvp->cv_waiters++; 197c86b6ff5SJohn Baldwin DROP_GIANT(); 198c86b6ff5SJohn Baldwin mtx_unlock(mp); 199238510fcSJason Evans 2007ee07175SPawel Jakub Dawidek sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR | 2016cbb70e2SKip Macy SLEEPQ_INTERRUPTIBLE, 0); 20244f3b092SJohn Baldwin rval = sleepq_wait_sig(cvp); 203238510fcSJason Evans 204238510fcSJason Evans #ifdef KTRACE 2059ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 2069ba7fe1bSJohn Baldwin ktrcsw(0, 0); 207238510fcSJason Evans #endif 2089ba7fe1bSJohn Baldwin PICKUP_GIANT(); 2099ed346baSBosko Milekic mtx_lock(mp); 21019284646SJohn Baldwin WITNESS_RESTORE(&mp->mtx_object, mp); 211238510fcSJason Evans 212238510fcSJason Evans return (rval); 213238510fcSJason Evans } 214238510fcSJason Evans 215238510fcSJason Evans /* 216238510fcSJason Evans * Wait on a condition variable for at most timo/hz seconds. Returns 0 if the 217238510fcSJason Evans * process was resumed by cv_signal or cv_broadcast, EWOULDBLOCK if the timeout 218238510fcSJason Evans * expires. 219238510fcSJason Evans */ 220238510fcSJason Evans int 221238510fcSJason Evans cv_timedwait(struct cv *cvp, struct mtx *mp, int timo) 222238510fcSJason Evans { 223b40ce416SJulian Elischer struct thread *td; 224238510fcSJason Evans int rval; 225238510fcSJason Evans WITNESS_SAVE_DECL(mp); 226238510fcSJason Evans 227b40ce416SJulian Elischer td = curthread; 228238510fcSJason Evans rval = 0; 229238510fcSJason Evans #ifdef KTRACE 2309ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 2319ba7fe1bSJohn Baldwin ktrcsw(1, 0); 232238510fcSJason Evans #endif 233b40ce416SJulian Elischer CV_ASSERT(cvp, mp, td); 23426306795SJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object, 23526306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 23619284646SJohn Baldwin WITNESS_SAVE(&mp->mtx_object, mp); 237238510fcSJason Evans 238238510fcSJason Evans if (cold || panicstr) { 239238510fcSJason Evans /* 240238510fcSJason Evans * After a panic, or during autoconfiguration, just give 241238510fcSJason Evans * interrupts a chance, then just return; don't run any other 242b40ce416SJulian Elischer * thread or panic below, in case this is the idle process and 243238510fcSJason Evans * already asleep. 244238510fcSJason Evans */ 245238510fcSJason Evans return 0; 246238510fcSJason Evans } 2474bc37205SJeffrey Hsu 2482ff0e645SJohn Baldwin sleepq_lock(cvp); 249238510fcSJason Evans 2509000d57dSJohn Baldwin cvp->cv_waiters++; 251c86b6ff5SJohn Baldwin DROP_GIANT(); 252c86b6ff5SJohn Baldwin mtx_unlock(mp); 253238510fcSJason Evans 2546cbb70e2SKip Macy sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR, 2556cbb70e2SKip Macy 0); 2561ed3e44fSJohn Baldwin sleepq_set_timeout(cvp, timo); 257a5471e4eSJohn Baldwin rval = sleepq_timedwait(cvp); 258238510fcSJason Evans 259238510fcSJason Evans #ifdef KTRACE 2609ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 2619ba7fe1bSJohn Baldwin ktrcsw(0, 0); 262238510fcSJason Evans #endif 263238510fcSJason Evans PICKUP_GIANT(); 2649ed346baSBosko Milekic mtx_lock(mp); 26519284646SJohn Baldwin WITNESS_RESTORE(&mp->mtx_object, mp); 266238510fcSJason Evans 267238510fcSJason Evans return (rval); 268238510fcSJason Evans } 269238510fcSJason Evans 270238510fcSJason Evans /* 271238510fcSJason Evans * Wait on a condition variable for at most timo/hz seconds, allowing 272b40ce416SJulian Elischer * interruption by signals. Returns 0 if the thread was resumed by cv_signal 273238510fcSJason Evans * or cv_broadcast, EWOULDBLOCK if the timeout expires, and EINTR or ERESTART if 274238510fcSJason Evans * a signal was caught. 275238510fcSJason Evans */ 276238510fcSJason Evans int 277238510fcSJason Evans cv_timedwait_sig(struct cv *cvp, struct mtx *mp, int timo) 278238510fcSJason Evans { 279b40ce416SJulian Elischer struct thread *td; 2809ef3a985SJohn Baldwin struct proc *p; 281238510fcSJason Evans int rval; 282238510fcSJason Evans WITNESS_SAVE_DECL(mp); 283238510fcSJason Evans 284b40ce416SJulian Elischer td = curthread; 2859ef3a985SJohn Baldwin p = td->td_proc; 286238510fcSJason Evans rval = 0; 287238510fcSJason Evans #ifdef KTRACE 2889ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 2899ba7fe1bSJohn Baldwin ktrcsw(1, 0); 290238510fcSJason Evans #endif 291b40ce416SJulian Elischer CV_ASSERT(cvp, mp, td); 29226306795SJohn Baldwin WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mp->mtx_object, 29326306795SJohn Baldwin "Waiting on \"%s\"", cvp->cv_description); 29419284646SJohn Baldwin WITNESS_SAVE(&mp->mtx_object, mp); 295238510fcSJason Evans 296238510fcSJason Evans if (cold || panicstr) { 297238510fcSJason Evans /* 298238510fcSJason Evans * After a panic, or during autoconfiguration, just give 299238510fcSJason Evans * interrupts a chance, then just return; don't run any other 300b40ce416SJulian Elischer * thread or panic below, in case this is the idle process and 301238510fcSJason Evans * already asleep. 302238510fcSJason Evans */ 303238510fcSJason Evans return 0; 304238510fcSJason Evans } 3054bc37205SJeffrey Hsu 3062ff0e645SJohn Baldwin sleepq_lock(cvp); 307238510fcSJason Evans 3089000d57dSJohn Baldwin cvp->cv_waiters++; 309c86b6ff5SJohn Baldwin DROP_GIANT(); 310c86b6ff5SJohn Baldwin mtx_unlock(mp); 311238510fcSJason Evans 3127ee07175SPawel Jakub Dawidek sleepq_add(cvp, &mp->mtx_object, cvp->cv_description, SLEEPQ_CONDVAR | 3136cbb70e2SKip Macy SLEEPQ_INTERRUPTIBLE, 0); 3141ed3e44fSJohn Baldwin sleepq_set_timeout(cvp, timo); 31594f0972bSDavid Xu rval = sleepq_timedwait_sig(cvp); 316238510fcSJason Evans 317238510fcSJason Evans #ifdef KTRACE 3189ba7fe1bSJohn Baldwin if (KTRPOINT(td, KTR_CSW)) 3199ba7fe1bSJohn Baldwin ktrcsw(0, 0); 320238510fcSJason Evans #endif 3219ba7fe1bSJohn Baldwin PICKUP_GIANT(); 3229ed346baSBosko Milekic mtx_lock(mp); 32319284646SJohn Baldwin WITNESS_RESTORE(&mp->mtx_object, mp); 324238510fcSJason Evans 325238510fcSJason Evans return (rval); 326238510fcSJason Evans } 327238510fcSJason Evans 328238510fcSJason Evans /* 329b40ce416SJulian Elischer * Signal a condition variable, wakes up one waiting thread. Will also wakeup 330238510fcSJason Evans * the swapper if the process is not in memory, so that it can bring the 331b40ce416SJulian Elischer * sleeping process in. Note that this may also result in additional threads 332238510fcSJason Evans * being made runnable. Should be called with the same mutex as was passed to 333238510fcSJason Evans * cv_wait held. 334238510fcSJason Evans */ 335238510fcSJason Evans void 336238510fcSJason Evans cv_signal(struct cv *cvp) 337238510fcSJason Evans { 338238510fcSJason Evans 3392ff0e645SJohn Baldwin sleepq_lock(cvp); 3409000d57dSJohn Baldwin if (cvp->cv_waiters > 0) { 3419000d57dSJohn Baldwin cvp->cv_waiters--; 3426cbb70e2SKip Macy sleepq_signal(cvp, SLEEPQ_CONDVAR, -1, 0); 3432ff0e645SJohn Baldwin } else 3442ff0e645SJohn Baldwin sleepq_release(cvp); 3459000d57dSJohn Baldwin } 346238510fcSJason Evans 347238510fcSJason Evans /* 348b40ce416SJulian Elischer * Broadcast a signal to a condition variable. Wakes up all waiting threads. 349238510fcSJason Evans * Should be called with the same mutex as was passed to cv_wait held. 350238510fcSJason Evans */ 351238510fcSJason Evans void 352512824f8SSeigo Tanimura cv_broadcastpri(struct cv *cvp, int pri) 353238510fcSJason Evans { 354238510fcSJason Evans 3552ff0e645SJohn Baldwin sleepq_lock(cvp); 3569000d57dSJohn Baldwin if (cvp->cv_waiters > 0) { 3579000d57dSJohn Baldwin cvp->cv_waiters = 0; 3586cbb70e2SKip Macy sleepq_broadcast(cvp, SLEEPQ_CONDVAR, pri, 0); 3592ff0e645SJohn Baldwin } else 3602ff0e645SJohn Baldwin sleepq_release(cvp); 3619000d57dSJohn Baldwin } 362