Home | History | Annotate | Line # | Download | only in kern
kern_condvar.c revision 1.6
      1 /*	$NetBSD: kern_condvar.c,v 1.6 2007/03/29 17:34:39 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.6 2007/03/29 17:34:39 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 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 	mutex_exit(mtx);
    116 
    117 	return sq;
    118 }
    119 
    120 /*
    121  * cv_exit:
    122  *
    123  *	After resuming execution, check to see if we have been restarted
    124  *	as a result of cv_signal().  If we have, but cannot take the
    125  *	wakeup (because of eg a pending Unix signal or timeout) then try
    126  *	to ensure that another LWP sees it.  This is necessary because
    127  *	there may be multiple waiters, and at least one should take the
    128  *	wakeup if possible.
    129  */
    130 static inline int
    131 cv_exit(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l, const int error)
    132 {
    133 
    134 	mutex_enter(mtx);
    135 	if (__predict_false(error != 0) && l->l_cv_signalled != 0)
    136 		cv_signal(cv);
    137 
    138 	return error;
    139 }
    140 
    141 /*
    142  * cv_unsleep:
    143  *
    144  *	Remove an LWP from the condition variable and sleep queue.  This
    145  *	is called when the LWP has not been awoken normally but instead
    146  *	interrupted: for example, when a signal is received.  Must be
    147  *	called with the LWP locked, and must return it unlocked.
    148  */
    149 static void
    150 cv_unsleep(lwp_t *l)
    151 {
    152 	uintptr_t addr;
    153 
    154 	KASSERT(l->l_wchan != NULL);
    155 	LOCK_ASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
    156 
    157 	addr = (uintptr_t)l->l_wchan;
    158 	((kcondvar_t *)addr)->cv_waiters--;
    159 
    160 	sleepq_unsleep(l);
    161 }
    162 
    163 /*
    164  * cv_changepri:
    165  *
    166  *	Adjust the real (user) priority of an LWP blocked on a CV.
    167  */
    168 static void
    169 cv_changepri(lwp_t *l, pri_t pri)
    170 {
    171 	sleepq_t *sq = l->l_sleepq;
    172 	pri_t opri;
    173 
    174 	KASSERT(lwp_locked(l, sq->sq_mutex));
    175 
    176 	opri = lwp_eprio(l);
    177 	l->l_usrpri = pri;
    178 	l->l_priority = sched_kpri(l);
    179 
    180 	if (lwp_eprio(l) != opri) {
    181 		TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    182 		sleepq_insert(sq, l, l->l_syncobj);
    183 	}
    184 }
    185 
    186 /*
    187  * cv_wait:
    188  *
    189  *	Wait non-interruptably on a condition variable until awoken.
    190  */
    191 void
    192 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    193 {
    194 	lwp_t *l = curlwp;
    195 	sleepq_t *sq;
    196 
    197 	LOCK_ASSERT(mutex_owned(mtx));
    198 
    199 	if (sleepq_dontsleep(l)) {
    200 		(void)sleepq_abort(mtx, 0);
    201 		return;
    202 	}
    203 
    204 	sq = cv_enter(cv, mtx, l);
    205 	sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 0, &cv_syncobj);
    206 	(void)sleepq_unblock(0, 0);
    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 	LOCK_ASSERT(mutex_owned(mtx));
    226 
    227 	if (sleepq_dontsleep(l))
    228 		return sleepq_abort(mtx, 0);
    229 
    230 	sq = cv_enter(cv, mtx, l);
    231 	sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 1, &cv_syncobj);
    232 	error = sleepq_unblock(0, 1);
    233 	return cv_exit(cv, mtx, l, error);
    234 }
    235 
    236 /*
    237  * cv_timedwait:
    238  *
    239  *	Wait on a condition variable until awoken or the specified timeout
    240  *	expires.  Returns zero if awoken normally or EWOULDBLOCK if the
    241  *	timeout expired.
    242  */
    243 int
    244 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
    245 {
    246 	lwp_t *l = curlwp;
    247 	sleepq_t *sq;
    248 	int error;
    249 
    250 	LOCK_ASSERT(mutex_owned(mtx));
    251 
    252 	if (sleepq_dontsleep(l))
    253 		return sleepq_abort(mtx, 0);
    254 
    255 	sq = cv_enter(cv, mtx, l);
    256 	sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 0, &cv_syncobj);
    257 	error = sleepq_unblock(timo, 0);
    258 	return cv_exit(cv, mtx, l, error);
    259 }
    260 
    261 /*
    262  * cv_timedwait_sig:
    263  *
    264  *	Wait on a condition variable until a timeout expires, awoken or a
    265  *	signal is received.  Will also return early if the process is
    266  *	exiting.  Returns zero if awoken normallly, EWOULDBLOCK if the
    267  *	timeout expires, ERESTART if a signal was received and the system
    268  *	call is restartable, or EINTR otherwise.
    269  */
    270 int
    271 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
    272 {
    273 	lwp_t *l = curlwp;
    274 	sleepq_t *sq;
    275 	int error;
    276 
    277 	LOCK_ASSERT(mutex_owned(mtx));
    278 
    279 	if (sleepq_dontsleep(l))
    280 		return sleepq_abort(mtx, 0);
    281 
    282 	sq = cv_enter(cv, mtx, l);
    283 	sleepq_block(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 1, &cv_syncobj);
    284 	error = sleepq_unblock(timo, 1);
    285 	return cv_exit(cv, mtx, l, error);
    286 }
    287 
    288 /*
    289  * cv_signal:
    290  *
    291  *	Wake the highest priority LWP waiting on a condition variable.
    292  *	Must be called with the interlocking mutex held.
    293  */
    294 void
    295 cv_signal(kcondvar_t *cv)
    296 {
    297 	lwp_t *l;
    298 	sleepq_t *sq;
    299 
    300 	if (cv->cv_waiters == 0)
    301 		return;
    302 
    303 	/*
    304 	 * cv->cv_waiters may be stale and have dropped to zero, but
    305 	 * while holding the interlock (the mutex passed to cv_wait()
    306 	 * and similar) we will see non-zero values when it matters.
    307 	 */
    308 
    309 	sq = sleeptab_lookup(&sleeptab, cv);
    310 	if (cv->cv_waiters != 0) {
    311 		cv->cv_waiters--;
    312 		l = sleepq_wake(sq, cv, 1);
    313 		l->l_cv_signalled = 1;
    314 	} else
    315 		sleepq_unlock(sq);
    316 }
    317 
    318 /*
    319  * cv_broadcast:
    320  *
    321  *	Wake all LWPs waiting on a condition variable.  Must be called
    322  *	with the interlocking mutex held.
    323  */
    324 void
    325 cv_broadcast(kcondvar_t *cv)
    326 {
    327 	sleepq_t *sq;
    328 	u_int cnt;
    329 
    330 	if (cv->cv_waiters == 0)
    331 		return;
    332 
    333 	sq = sleeptab_lookup(&sleeptab, cv);
    334 	if ((cnt = cv->cv_waiters) != 0) {
    335 		cv->cv_waiters = 0;
    336 		sleepq_wake(sq, cv, cnt);
    337 	} else
    338 		sleepq_unlock(sq);
    339 }
    340 
    341 /*
    342  * cv_has_waiters:
    343  *
    344  *	For diagnostic assertions: return non-zero if a condition
    345  *	variable has waiters.
    346  */
    347 int
    348 cv_has_waiters(kcondvar_t *cv)
    349 {
    350 
    351 	/* No need to interlock here */
    352 	return (int)cv->cv_waiters;
    353 }
    354