Home | History | Annotate | Line # | Download | only in kern
kern_condvar.c revision 1.1.2.5
      1 /*	$NetBSD: kern_condvar.c,v 1.1.2.5 2007/02/03 16:32:50 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Kernel condition variable implementation, modeled after those found in
     41  * Solaris, a description of which can be found in:
     42  *
     43  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     44  *	    Richard McDougall.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.1.2.5 2007/02/03 16:32:50 ad Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/proc.h>
     52 #include <sys/sched.h>
     53 #include <sys/systm.h>
     54 #include <sys/condvar.h>
     55 #include <sys/sleepq.h>
     56 
     57 void	cv_unsleep(struct lwp *);
     58 
     59 syncobj_t cv_syncobj = {
     60 	SOBJ_SLEEPQ_SORTED,
     61 	cv_unsleep,
     62 	sleepq_changepri
     63 };
     64 
     65 /*
     66  * cv_init:
     67  *
     68  *	Initialize a condition variable for use.
     69  */
     70 void
     71 cv_init(kcondvar_t *cv, const char *wmesg)
     72 {
     73 
     74 	KASSERT(wmesg != NULL);
     75 
     76 	cv->cv_wmesg = wmesg;
     77 	cv->cv_waiters = 0;
     78 }
     79 
     80 /*
     81  * cv_destroy:
     82  *
     83  *	Tear down a condition variable.
     84  */
     85 void
     86 cv_destroy(kcondvar_t *cv)
     87 {
     88 
     89 #ifdef DIAGNOSTIC
     90 	KASSERT(cv->cv_waiters == 0 && cv->cv_wmesg != NULL);
     91 	cv->cv_wmesg = NULL;
     92 #endif
     93 }
     94 
     95 /*
     96  * cv_enter:
     97  *
     98  *	Look up and lock the sleep queue corresponding to the given
     99  *	condition variable, and increment the number of waiters.
    100  */
    101 static inline sleepq_t *
    102 cv_enter(kcondvar_t *cv, kmutex_t *mtx, struct lwp *l)
    103 {
    104 	sleepq_t *sq;
    105 
    106 	KASSERT(cv->cv_wmesg != NULL);
    107 
    108 	sq = sleeptab_lookup(&sleeptab, cv);
    109 	cv->cv_waiters++;
    110 	sleepq_enter(sq, l);
    111 	mutex_exit(mtx);
    112 
    113 	return sq;
    114 }
    115 
    116 /*
    117  * cv_unsleep:
    118  *
    119  *	Remove an LWP from the condition variable and sleep queue.  This
    120  *	is called when the LWP has not been awoken normally but instead
    121  *	interrupted: for example, when a signal is received.  Must be
    122  *	called with the LWP locked, and must return it unlocked.
    123  */
    124 void
    125 cv_unsleep(struct lwp *l)
    126 {
    127 	uintptr_t addr;
    128 
    129 	KASSERT(l->l_wchan != NULL);
    130 	LOCK_ASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
    131 
    132 	addr = (uintptr_t)l->l_wchan;
    133 	((kcondvar_t *)addr)->cv_waiters--;
    134 
    135 	sleepq_unsleep(l);
    136 }
    137 
    138 /*
    139  * cv_wait:
    140  *
    141  *	Wait non-interruptably on a condition variable until awoken.
    142  */
    143 void
    144 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    145 {
    146 	struct lwp *l = curlwp;
    147 	sleepq_t *sq;
    148 
    149 	LOCK_ASSERT(mutex_owned(mtx));
    150 
    151 	if (sleepq_dontsleep(l)) {
    152 		(void)sleepq_abort(mtx, 0);
    153 		return;
    154 	}
    155 
    156 	sq = cv_enter(cv, mtx, l);
    157 	sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 0,
    158 	    &cv_syncobj);
    159 	(void)sleepq_unblock(0, 0);
    160 	mutex_enter(mtx);
    161 }
    162 
    163 /*
    164  * cv_wait_sig:
    165  *
    166  *	Wait on a condition variable until a awoken or a signal is received.
    167  *	Will also return early if the process is exiting.  Returns zero if
    168  *	awoken normallly, ERESTART if a signal was received and the system
    169  *	call is restartable, or EINTR otherwise.
    170  */
    171 int
    172 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    173 {
    174 	struct lwp *l = curlwp;
    175 	sleepq_t *sq;
    176 	int error;
    177 
    178 	LOCK_ASSERT(mutex_owned(mtx));
    179 
    180 	if (sleepq_dontsleep(l))
    181 		return sleepq_abort(mtx, 0);
    182 
    183 	sq = cv_enter(cv, mtx, l);
    184 	sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 1,
    185 	    &cv_syncobj);
    186 	error = sleepq_unblock(0, 1);
    187 	mutex_enter(mtx);
    188 
    189 	return error;
    190 }
    191 
    192 /*
    193  * cv_timedwait:
    194  *
    195  *	Wait on a condition variable until awoken or the specified timeout
    196  *	expires.  Returns zero if awoken normally or EWOULDBLOCK if the
    197  *	timeout expired.
    198  */
    199 int
    200 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
    201 {
    202 	struct lwp *l = curlwp;
    203 	sleepq_t *sq;
    204 	int error;
    205 
    206 	LOCK_ASSERT(mutex_owned(mtx));
    207 
    208 	if (sleepq_dontsleep(l))
    209 		return sleepq_abort(mtx, 0);
    210 
    211 	sq = cv_enter(cv, mtx, l);
    212 	sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 0,
    213 	    &cv_syncobj);
    214 	error = sleepq_unblock(timo, 0);
    215 	mutex_enter(mtx);
    216 
    217  	return error;
    218 }
    219 
    220 /*
    221  * cv_timedwait_sig:
    222  *
    223  *	Wait on a condition variable until a timeout expires, awoken or a
    224  *	signal is received.  Will also return early if the process is
    225  *	exiting.  Returns zero if awoken normallly, EWOULDBLOCK if the
    226  *	timeout expires, ERESTART if a signal was received and the system
    227  *	call is restartable, or EINTR otherwise.
    228  */
    229 int
    230 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
    231 {
    232 	struct lwp *l = curlwp;
    233 	sleepq_t *sq;
    234 	int error;
    235 
    236 	LOCK_ASSERT(mutex_owned(mtx));
    237 
    238 	if (sleepq_dontsleep(l))
    239 		return sleepq_abort(mtx, 0);
    240 
    241 	sq = cv_enter(cv, mtx, l);
    242 	sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 1,
    243 	    &cv_syncobj);
    244 	error = sleepq_unblock(timo, 1);
    245 	mutex_enter(mtx);
    246 
    247  	return error;
    248 }
    249 
    250 /*
    251  * cv_signal:
    252  *
    253  *	Wake the highest priority LWP waiting on a condition variable.
    254  *	Must be called with the interlocking mutex held.
    255  */
    256 void
    257 cv_signal(kcondvar_t *cv)
    258 {
    259 	sleepq_t *sq;
    260 
    261 	if (cv->cv_waiters == 0)
    262 		return;
    263 
    264 	/*
    265 	 * cv->cv_waiters may be stale and have dropped to zero, but
    266 	 * while holding the interlock (the mutex passed to cv_wait()
    267 	 * and similar) we will see non-zero values when it matters.
    268 	 */
    269 
    270 	sq = sleeptab_lookup(&sleeptab, cv);
    271 	if (cv->cv_waiters != 0) {
    272 		cv->cv_waiters--;
    273 		sleepq_wake(sq, cv, 1);
    274 	} else
    275 		sleepq_unlock(sq);
    276 }
    277 
    278 /*
    279  * cv_broadcast:
    280  *
    281  *	Wake all LWPs waiting on a condition variable.  Must be called
    282  *	with the interlocking mutex held.
    283  */
    284 void
    285 cv_broadcast(kcondvar_t *cv)
    286 {
    287 	sleepq_t *sq;
    288 	u_int cnt;
    289 
    290 	if (cv->cv_waiters == 0)
    291 		return;
    292 
    293 	sq = sleeptab_lookup(&sleeptab, cv);
    294 	if ((cnt = cv->cv_waiters) != 0) {
    295 		cv->cv_waiters = 0;
    296 		sleepq_wake(sq, cv, cnt);
    297 	} else
    298 		sleepq_unlock(sq);
    299 }
    300 
    301 /*
    302  * cv_signal_async:
    303  *
    304  *	Wake the highest priority LWP waiting on a condition variable.
    305  */
    306 void
    307 cv_signal_async(kcondvar_t *cv)
    308 {
    309 	sleepq_t *sq;
    310 
    311 	sq = sleeptab_lookup(&sleeptab, cv);
    312 	if (cv->cv_waiters != 0) {
    313 		cv->cv_waiters--;
    314 		sleepq_wake(sq, cv, 1);
    315 	} else
    316 		sleepq_unlock(sq);
    317 }
    318 
    319 /*
    320  * cv_broadcast_async:
    321  *
    322  *	Wake all LWPs waiting on a condition variable.
    323  */
    324 void
    325 cv_broadcast_async(kcondvar_t *cv)
    326 {
    327 	sleepq_t *sq;
    328 	u_int cnt;
    329 
    330 	sq = sleeptab_lookup(&sleeptab, cv);
    331 	if ((cnt = cv->cv_waiters) != 0) {
    332 		cv->cv_waiters = 0;
    333 		sleepq_wake(sq, cv, cnt);
    334 	} else
    335 		sleepq_unlock(sq);
    336 }
    337 
    338 /*
    339  * cv_has_waiters:
    340  *
    341  *	For diagnostic assertions: return non-zero if a condition
    342  *	variable has waiters.
    343  */
    344 int
    345 cv_has_waiters(kcondvar_t *cv)
    346 {
    347 
    348 	/* No need to interlock here */
    349 	return (int)cv->cv_waiters;
    350 }
    351