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