Home | History | Annotate | Line # | Download | only in kern
kern_condvar.c revision 1.1.2.2
      1 /*	$NetBSD: kern_condvar.c,v 1.1.2.2 2006/11/17 16:34:35 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 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.2 2006/11/17 16:34:35 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_ptr = NULL;
     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 	KASSERT(cv->cv_waiters == 0 && cv->cv_wmesg != NULL);
     90 }
     91 
     92 /*
     93  * cv_enter:
     94  *
     95  *	Look up and lock the sleep queue corresponding to the given
     96  *	condition variable, and increment the number of waiters.
     97  */
     98 static inline sleepq_t *
     99 cv_enter(kcondvar_t *cv)
    100 {
    101 	sleepq_t *sq;
    102 
    103 	KASSERT(cv->cv_wmesg != NULL);
    104 
    105 	sq = sleeptab_lookup(&sleeptab, cv);
    106 	cv->cv_waiters++;
    107 
    108 	return sq;
    109 }
    110 
    111 /*
    112  * cv_unsleep:
    113  *
    114  *	Remove an LWP from the condition variable and sleep queue.  This
    115  *	is called when the LWP has not been awoken normally but instead
    116  *	interrupted: for example, when a signal is received.  Must be called
    117  *	with the LWP locked, and must return it unlocked.
    118  */
    119 void
    120 cv_unsleep(struct lwp *l)
    121 {
    122 	uintptr_t addr;
    123 
    124 	KASSERT(l->l_wchan != NULL);
    125 	LOCK_ASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
    126 
    127 	addr = (uintptr_t)l->l_wchan;
    128 	((kcondvar_t *)addr)->cv_waiters--;
    129 
    130 	sleepq_unsleep(l);
    131 }
    132 
    133 /*
    134  * cv_wait:
    135  *
    136  *	Wait non-interruptably on a condition variable until awoken.
    137  */
    138 void
    139 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    140 {
    141 	struct lwp *l = curlwp;
    142 	sleepq_t *sq;
    143 
    144 	LOCK_ASSERT(mutex_owned(mtx));
    145 
    146 	if (sleepq_dontsleep(l)) {
    147 		(void)sleepq_abort(mtx, 0);
    148 		return;
    149 	}
    150 
    151 	sq = cv_enter(cv);
    152 	sleepq_enter(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 0,
    153 	    &cv_syncobj);
    154 	mutex_exit(mtx);
    155 	(void)sleepq_block(sq, 0);
    156 	sleepq_unblock();
    157 	mutex_enter(mtx);
    158 }
    159 
    160 /*
    161  * cv_wait_sig:
    162  *
    163  *	Wait on a condition variable until a awoken or a signal is received.
    164  *	Will also return early if the process is exiting.  Returns zero if
    165  *	awoken normallly, ERESTART if a signal was received and the system
    166  *	call is restartable, or EINTR otherwise.
    167  */
    168 int
    169 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    170 {
    171 	struct lwp *l = curlwp;
    172 	sleepq_t *sq;
    173 	int error;
    174 
    175 	LOCK_ASSERT(mutex_owned(mtx));
    176 
    177 	if (sleepq_dontsleep(l))
    178 		return sleepq_abort(mtx, 0);
    179 
    180 	sq = cv_enter(cv);
    181 	sleepq_enter(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 1,
    182 	    &cv_syncobj);
    183 	mutex_exit(mtx);
    184 	error = sleepq_block(sq, 0);
    185 	sleepq_unblock();
    186 	mutex_enter(mtx);
    187 
    188 	return error;
    189 }
    190 
    191 /*
    192  * cv_timedwait:
    193  *
    194  *	Wait on a condition variable until awoken or the specified timeout
    195  *	expires.  Returns zero if awoken normally or EWOULDBLOCK if the
    196  *	timeout expired.
    197  */
    198 int
    199 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
    200 {
    201 	struct lwp *l = curlwp;
    202 	sleepq_t *sq;
    203 	int error;
    204 
    205 	LOCK_ASSERT(mutex_owned(mtx));
    206 
    207 	if (sleepq_dontsleep(l))
    208 		return sleepq_abort(mtx, 0);
    209 
    210 	sq = cv_enter(cv);
    211 	sleepq_enter(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 0,
    212 	    &cv_syncobj);
    213 	mutex_exit(mtx);
    214 	error = sleepq_block(sq, timo);
    215 	sleepq_unblock();
    216 	mutex_enter(mtx);
    217 
    218  	return error;
    219 }
    220 
    221 /*
    222  * cv_timedwait_sig:
    223  *
    224  *	Wait on a condition variable until a timeout expires, awoken or a
    225  *	signal is received.  Will also return early if the process is
    226  *	exiting.  Returns zero if awoken normallly, EWOULDBLOCK if the
    227  *	timeout expires, ERESTART if a signal was received and the system
    228  *	call is restartable, or EINTR otherwise.
    229  */
    230 int
    231 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
    232 {
    233 	struct lwp *l = curlwp;
    234 	sleepq_t *sq;
    235 	int error;
    236 
    237 	LOCK_ASSERT(mutex_owned(mtx));
    238 
    239 	if (sleepq_dontsleep(l))
    240 		return sleepq_abort(mtx, 0);
    241 
    242 	sq = cv_enter(cv);
    243 	sleepq_enter(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 1,
    244 	    &cv_syncobj);
    245 	mutex_exit(mtx);
    246 	error = sleepq_block(sq, timo);
    247 	sleepq_unblock();
    248 	mutex_enter(mtx);
    249 
    250  	return error;
    251 }
    252 
    253 /*
    254  * cv_signal:
    255  *
    256  *	Wake the highest priority LWP waiting on a condition variable.
    257  */
    258 void
    259 cv_signal(kcondvar_t *cv)
    260 {
    261 	sleepq_t *sq;
    262 
    263 	sq = sleeptab_lookup(&sleeptab, cv);
    264 	if (cv->cv_waiters != 0) {
    265 		cv->cv_waiters--;
    266 		sleepq_wakeone(sq, cv);
    267 	} else
    268 		sleepq_unlock(sq);
    269 }
    270 
    271 /*
    272  * cv_broadcast:
    273  *
    274  *	Wake all LWPs waiting on a condition variable.
    275  */
    276 void
    277 cv_broadcast(kcondvar_t *cv)
    278 {
    279 	sleepq_t *sq;
    280 
    281 	sq = sleeptab_lookup(&sleeptab, cv);
    282 	if (cv->cv_waiters != 0) {
    283 		sleepq_wakeall(sq, cv, cv->cv_waiters);
    284 		cv->cv_waiters = 0;
    285 	} else
    286 		sleepq_unlock(sq);
    287 }
    288