Home | History | Annotate | Line # | Download | only in kern
kern_condvar.c revision 1.8
      1 /*	$NetBSD: kern_condvar.c,v 1.8 2007/05/17 14:51:38 yamt 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.8 2007/05/17 14:51:38 yamt 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 static void	cv_unsleep(lwp_t *);
     58 static void	cv_changepri(lwp_t *, pri_t);
     59 
     60 syncobj_t cv_syncobj = {
     61 	SOBJ_SLEEPQ_SORTED,
     62 	cv_unsleep,
     63 	cv_changepri,
     64 	sleepq_lendpri,
     65 	syncobj_noowner,
     66 };
     67 
     68 /*
     69  * cv_init:
     70  *
     71  *	Initialize a condition variable for use.
     72  */
     73 void
     74 cv_init(kcondvar_t *cv, const char *wmesg)
     75 {
     76 
     77 	KASSERT(wmesg != NULL);
     78 
     79 	cv->cv_wmesg = wmesg;
     80 	cv->cv_waiters = 0;
     81 }
     82 
     83 /*
     84  * cv_destroy:
     85  *
     86  *	Tear down a condition variable.
     87  */
     88 void
     89 cv_destroy(kcondvar_t *cv)
     90 {
     91 
     92 #ifdef DIAGNOSTIC
     93 	KASSERT(cv->cv_waiters == 0 && cv->cv_wmesg != NULL);
     94 	cv->cv_wmesg = NULL;
     95 #endif
     96 }
     97 
     98 /*
     99  * cv_enter:
    100  *
    101  *	Look up and lock the sleep queue corresponding to the given
    102  *	condition variable, and increment the number of waiters.
    103  */
    104 static inline sleepq_t *
    105 cv_enter(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l)
    106 {
    107 	sleepq_t *sq;
    108 
    109 	KASSERT(cv->cv_wmesg != NULL);
    110 
    111 	l->l_cv_signalled = 0;
    112 	sq = sleeptab_lookup(&sleeptab, cv);
    113 	cv->cv_waiters++;
    114 	sleepq_enter(sq, l);
    115 	sleepq_enqueue(sq, sched_kpri(l), cv, cv->cv_wmesg, &cv_syncobj);
    116 	mutex_exit(mtx);
    117 
    118 	return sq;
    119 }
    120 
    121 /*
    122  * cv_exit:
    123  *
    124  *	After resuming execution, check to see if we have been restarted
    125  *	as a result of cv_signal().  If we have, but cannot take the
    126  *	wakeup (because of eg a pending Unix signal or timeout) then try
    127  *	to ensure that another LWP sees it.  This is necessary because
    128  *	there may be multiple waiters, and at least one should take the
    129  *	wakeup if possible.
    130  */
    131 static inline int
    132 cv_exit(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l, const int error)
    133 {
    134 
    135 	mutex_enter(mtx);
    136 	if (__predict_false(error != 0) && l->l_cv_signalled != 0)
    137 		cv_signal(cv);
    138 
    139 	return error;
    140 }
    141 
    142 /*
    143  * cv_unsleep:
    144  *
    145  *	Remove an LWP from the condition variable and sleep queue.  This
    146  *	is called when the LWP has not been awoken normally but instead
    147  *	interrupted: for example, when a signal is received.  Must be
    148  *	called with the LWP locked, and must return it unlocked.
    149  */
    150 static void
    151 cv_unsleep(lwp_t *l)
    152 {
    153 	uintptr_t addr;
    154 
    155 	KASSERT(l->l_wchan != NULL);
    156 	KASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
    157 
    158 	addr = (uintptr_t)l->l_wchan;
    159 	((kcondvar_t *)addr)->cv_waiters--;
    160 
    161 	sleepq_unsleep(l);
    162 }
    163 
    164 /*
    165  * cv_changepri:
    166  *
    167  *	Adjust the real (user) priority of an LWP blocked on a CV.
    168  */
    169 static void
    170 cv_changepri(lwp_t *l, pri_t pri)
    171 {
    172 	sleepq_t *sq = l->l_sleepq;
    173 	pri_t opri;
    174 
    175 	KASSERT(lwp_locked(l, sq->sq_mutex));
    176 
    177 	opri = lwp_eprio(l);
    178 	l->l_usrpri = pri;
    179 	l->l_priority = sched_kpri(l);
    180 
    181 	if (lwp_eprio(l) != opri) {
    182 		TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    183 		sleepq_insert(sq, l, l->l_syncobj);
    184 	}
    185 }
    186 
    187 /*
    188  * cv_wait:
    189  *
    190  *	Wait non-interruptably on a condition variable until awoken.
    191  */
    192 void
    193 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    194 {
    195 	lwp_t *l = curlwp;
    196 	sleepq_t *sq;
    197 
    198 	KASSERT(mutex_owned(mtx));
    199 
    200 	if (sleepq_dontsleep(l)) {
    201 		(void)sleepq_abort(mtx, 0);
    202 		return;
    203 	}
    204 
    205 	sq = cv_enter(cv, mtx, l);
    206 	(void)sleepq_block(0, false);
    207 	(void)cv_exit(cv, mtx, l, 0);
    208 }
    209 
    210 /*
    211  * cv_wait_sig:
    212  *
    213  *	Wait on a condition variable until a awoken or a signal is received.
    214  *	Will also return early if the process is exiting.  Returns zero if
    215  *	awoken normallly, ERESTART if a signal was received and the system
    216  *	call is restartable, or EINTR otherwise.
    217  */
    218 int
    219 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    220 {
    221 	lwp_t *l = curlwp;
    222 	sleepq_t *sq;
    223 	int error;
    224 
    225 	KASSERT(mutex_owned(mtx));
    226 
    227 	if (sleepq_dontsleep(l))
    228 		return sleepq_abort(mtx, 0);
    229 
    230 	sq = cv_enter(cv, mtx, l);
    231 	error = sleepq_block(0, true);
    232 	return cv_exit(cv, mtx, l, error);
    233 }
    234 
    235 /*
    236  * cv_timedwait:
    237  *
    238  *	Wait on a condition variable until awoken or the specified timeout
    239  *	expires.  Returns zero if awoken normally or EWOULDBLOCK if the
    240  *	timeout expired.
    241  */
    242 int
    243 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
    244 {
    245 	lwp_t *l = curlwp;
    246 	sleepq_t *sq;
    247 	int error;
    248 
    249 	KASSERT(mutex_owned(mtx));
    250 
    251 	if (sleepq_dontsleep(l))
    252 		return sleepq_abort(mtx, 0);
    253 
    254 	sq = cv_enter(cv, mtx, l);
    255 	error = sleepq_block(timo, false);
    256 	return cv_exit(cv, mtx, l, error);
    257 }
    258 
    259 /*
    260  * cv_timedwait_sig:
    261  *
    262  *	Wait on a condition variable until a timeout expires, awoken or a
    263  *	signal is received.  Will also return early if the process is
    264  *	exiting.  Returns zero if awoken normallly, EWOULDBLOCK if the
    265  *	timeout expires, ERESTART if a signal was received and the system
    266  *	call is restartable, or EINTR otherwise.
    267  */
    268 int
    269 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
    270 {
    271 	lwp_t *l = curlwp;
    272 	sleepq_t *sq;
    273 	int error;
    274 
    275 	KASSERT(mutex_owned(mtx));
    276 
    277 	if (sleepq_dontsleep(l))
    278 		return sleepq_abort(mtx, 0);
    279 
    280 	sq = cv_enter(cv, mtx, l);
    281 	error = sleepq_block(timo, true);
    282 	return cv_exit(cv, mtx, l, error);
    283 }
    284 
    285 /*
    286  * cv_signal:
    287  *
    288  *	Wake the highest priority LWP waiting on a condition variable.
    289  *	Must be called with the interlocking mutex held.
    290  */
    291 void
    292 cv_signal(kcondvar_t *cv)
    293 {
    294 	lwp_t *l;
    295 	sleepq_t *sq;
    296 
    297 	if (cv->cv_waiters == 0)
    298 		return;
    299 
    300 	/*
    301 	 * cv->cv_waiters may be stale and have dropped to zero, but
    302 	 * while holding the interlock (the mutex passed to cv_wait()
    303 	 * and similar) we will see non-zero values when it matters.
    304 	 */
    305 
    306 	sq = sleeptab_lookup(&sleeptab, cv);
    307 	if (cv->cv_waiters != 0) {
    308 		cv->cv_waiters--;
    309 		l = sleepq_wake(sq, cv, 1);
    310 		l->l_cv_signalled = 1;
    311 	} else
    312 		sleepq_unlock(sq);
    313 }
    314 
    315 /*
    316  * cv_broadcast:
    317  *
    318  *	Wake all LWPs waiting on a condition variable.  Must be called
    319  *	with the interlocking mutex held.
    320  */
    321 void
    322 cv_broadcast(kcondvar_t *cv)
    323 {
    324 	sleepq_t *sq;
    325 	u_int cnt;
    326 
    327 	if (cv->cv_waiters == 0)
    328 		return;
    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 bool
    345 cv_has_waiters(kcondvar_t *cv)
    346 {
    347 
    348 	/* No need to interlock here */
    349 	return cv->cv_waiters != 0;
    350 }
    351