Home | History | Annotate | Line # | Download | only in kern
kern_condvar.c revision 1.1.2.1
      1 /*	$NetBSD: kern_condvar.c,v 1.1.2.1 2006/10/20 19:40:17 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 variables 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.1 2006/10/20 19:40:17 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 /*
     58  * cv_init:
     59  *
     60  *	Initialize a condition variable for use.
     61  */
     62 void
     63 cv_init(kcondvar_t *cv, kcondvar_type_t type, const char *wmesg)
     64 {
     65 
     66 	KASSERT(type == CV_DEFAULT);
     67 	cv->cv_wmesg = wmesg;
     68 	cv->cv_ptr = NULL;
     69 }
     70 
     71 /*
     72  * cv_destroy:
     73  *
     74  *	Tear down a condition variable.
     75  */
     76 void
     77 cv_destroy(kcondvar_t *cv)
     78 {
     79 
     80 	KASSERT(cv->cv_waiters == 0);
     81 }
     82 
     83 /*
     84  * cv_wait:
     85  *
     86  *	Wait non-interruptably on a condition variable until awoken.
     87  */
     88 void
     89 cv_wait(kcondvar_t *cv, kmutex_t *mtx)
     90 {
     91 	struct lwp *l = curlwp;
     92 	sleepq_t *sq;
     93 
     94 	LOCK_ASSERT(mutex_owned(mtx));
     95 
     96 	if (sleepq_dontsleep(l)) {
     97 		(void)sleepq_abort(mtx, 0);
     98 		return;
     99 	}
    100 
    101 	sq = sleeptab_lookup(cv);
    102 	cv->cv_waiters++;
    103 
    104 	sleepq_enter(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 0);
    105 
    106 	mutex_exit_linked(mtx, l->l_mutex);
    107 
    108 	(void)sleepq_block(sq, 0);
    109 
    110 	mutex_enter(mtx);
    111 }
    112 
    113 /*
    114  * cv_wait_sig:
    115  *
    116  *	Wait on a condition variable until a awoken or a signal is received.
    117  *	Will also return early if the process is exiting.  Returns zero if
    118  *	awoken normallly, ERESTART if a signal was received and the system
    119  *	call is restartable, or EINTR otherwise.
    120  */
    121 int
    122 cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    123 {
    124 	struct lwp *l = curlwp;
    125 	sleepq_t *sq;
    126 	int error;
    127 
    128 	LOCK_ASSERT(mutex_owned(mtx));
    129 
    130 	if (sleepq_dontsleep(l))
    131 		return sleepq_abort(mtx, 0);
    132 
    133 	sq = sleeptab_lookup(cv);
    134 	cv->cv_waiters++;
    135 
    136 	sleepq_enter(sq, sched_kpri(l), cv, cv->cv_wmesg, 0, 1);
    137 
    138 	mutex_exit_linked(mtx, l->l_mutex);
    139 
    140 	error = sleepq_block(sq, 0);
    141 
    142 	mutex_enter(mtx);
    143 
    144 	return error;
    145 }
    146 
    147 /*
    148  * cv_timedwait:
    149  *
    150  *	Wait on a condition variable until awoken or the specified timeout
    151  *	expires.  Returns zero if awoken normally or EWOULDBLOCK if the
    152  *	timeout expired.
    153  */
    154 int
    155 cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
    156 {
    157 	struct lwp *l = curlwp;
    158 	sleepq_t *sq;
    159 	int error;
    160 
    161 	LOCK_ASSERT(mutex_owned(mtx));
    162 
    163 	if (sleepq_dontsleep(l))
    164 		return sleepq_abort(mtx, 0);
    165 
    166 	sq = sleeptab_lookup(cv);
    167 	cv->cv_waiters++;
    168 
    169 	sleepq_enter(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 0);
    170 
    171 	mutex_exit_linked(mtx, l->l_mutex);
    172 
    173 	error = sleepq_block(sq, timo);
    174 
    175 	mutex_enter(mtx);
    176 
    177 	return error;
    178 }
    179 
    180 /*
    181  * cv_timedwait_sig:
    182  *
    183  *	Wait on a condition variable until a timeout expires, awoken or a
    184  *	signal is received.  Will also return early if the process is
    185  *	exiting.  Returns zero if awoken normallly, EWOULDBLOCK if the
    186  *	timeout expires, ERESTART if a signal was received and the system
    187  *	call is restartable, or EINTR otherwise.
    188  */
    189 int
    190 cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
    191 {
    192 	struct lwp *l = curlwp;
    193 	sleepq_t *sq;
    194 	int error;
    195 
    196 	LOCK_ASSERT(mutex_owned(mtx));
    197 
    198 	if (sleepq_dontsleep(l))
    199 		return sleepq_abort(mtx, 0);
    200 
    201 	sq = sleeptab_lookup(cv);
    202 	cv->cv_waiters++;
    203 
    204 	sleepq_enter(sq, sched_kpri(l), cv, cv->cv_wmesg, timo, 1);
    205 
    206 	mutex_exit_linked(mtx, l->l_mutex);
    207 
    208 	error = sleepq_block(sq, timo);
    209 
    210 	mutex_enter(mtx);
    211 
    212 	return error;
    213 }
    214 
    215 /*
    216  * cv_signal:
    217  *
    218  *	Wake the highest priority LWP waiting on a condition variable.
    219  */
    220 void
    221 cv_signal(kcondvar_t *cv)
    222 {
    223 	sleepq_t *sq;
    224 
    225 	sq = sleeptab_lookup(cv);
    226 	if (cv->cv_waiters != 0) {
    227 		cv->cv_waiters--;
    228 		sleepq_wakeone(sq, cv);
    229 	} else
    230 		sleepq_unlock(sq);
    231 }
    232 
    233 /*
    234  * cv_broadcast:
    235  *
    236  *	Wake all LWPs waiting on a condition variable.
    237  */
    238 void
    239 cv_broadcast(kcondvar_t *cv)
    240 {
    241 	sleepq_t *sq;
    242 
    243 	sq = sleeptab_lookup(cv);
    244 	if (cv->cv_waiters != 0) {
    245 		sleepq_wakeall(sq, cv, cv->cv_waiters);
    246 		cv->cv_waiters = 0;
    247 	} else
    248 		sleepq_unlock(sq);
    249 }
    250