Home | History | Annotate | Line # | Download | only in kern
kern_condvar.c revision 1.3.4.5
      1  1.3.4.5  yamt /*	$NetBSD: kern_condvar.c,v 1.3.4.5 2007/11/15 11:44:39 yamt Exp $	*/
      2  1.3.4.2  yamt 
      3  1.3.4.2  yamt /*-
      4  1.3.4.2  yamt  * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
      5  1.3.4.2  yamt  * All rights reserved.
      6  1.3.4.2  yamt  *
      7  1.3.4.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.3.4.2  yamt  * by Andrew Doran.
      9  1.3.4.2  yamt  *
     10  1.3.4.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.3.4.2  yamt  * modification, are permitted provided that the following conditions
     12  1.3.4.2  yamt  * are met:
     13  1.3.4.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.3.4.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.3.4.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.3.4.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.3.4.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.3.4.2  yamt  * 3. All advertising materials mentioning features or use of this software
     19  1.3.4.2  yamt  *    must display the following acknowledgement:
     20  1.3.4.2  yamt  *	This product includes software developed by the NetBSD
     21  1.3.4.2  yamt  *	Foundation, Inc. and its contributors.
     22  1.3.4.2  yamt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.3.4.2  yamt  *    contributors may be used to endorse or promote products derived
     24  1.3.4.2  yamt  *    from this software without specific prior written permission.
     25  1.3.4.2  yamt  *
     26  1.3.4.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.3.4.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.3.4.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.3.4.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.3.4.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.3.4.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.3.4.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.3.4.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.3.4.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.3.4.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.3.4.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     37  1.3.4.2  yamt  */
     38  1.3.4.2  yamt 
     39  1.3.4.2  yamt /*
     40  1.3.4.2  yamt  * Kernel condition variable implementation, modeled after those found in
     41  1.3.4.2  yamt  * Solaris, a description of which can be found in:
     42  1.3.4.2  yamt  *
     43  1.3.4.2  yamt  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     44  1.3.4.2  yamt  *	    Richard McDougall.
     45  1.3.4.2  yamt  */
     46  1.3.4.2  yamt 
     47  1.3.4.2  yamt #include <sys/cdefs.h>
     48  1.3.4.5  yamt __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.3.4.5 2007/11/15 11:44:39 yamt Exp $");
     49  1.3.4.2  yamt 
     50  1.3.4.2  yamt #include <sys/param.h>
     51  1.3.4.2  yamt #include <sys/proc.h>
     52  1.3.4.2  yamt #include <sys/sched.h>
     53  1.3.4.2  yamt #include <sys/systm.h>
     54  1.3.4.2  yamt #include <sys/condvar.h>
     55  1.3.4.2  yamt #include <sys/sleepq.h>
     56  1.3.4.2  yamt 
     57  1.3.4.3  yamt static void	cv_unsleep(lwp_t *);
     58  1.3.4.2  yamt 
     59  1.3.4.3  yamt static syncobj_t cv_syncobj = {
     60  1.3.4.2  yamt 	SOBJ_SLEEPQ_SORTED,
     61  1.3.4.2  yamt 	cv_unsleep,
     62  1.3.4.5  yamt 	sleepq_changepri,
     63  1.3.4.3  yamt 	sleepq_lendpri,
     64  1.3.4.3  yamt 	syncobj_noowner,
     65  1.3.4.2  yamt };
     66  1.3.4.2  yamt 
     67  1.3.4.3  yamt static const char deadcv[] = "deadcv";
     68  1.3.4.3  yamt 
     69  1.3.4.2  yamt /*
     70  1.3.4.2  yamt  * cv_init:
     71  1.3.4.2  yamt  *
     72  1.3.4.2  yamt  *	Initialize a condition variable for use.
     73  1.3.4.2  yamt  */
     74  1.3.4.2  yamt void
     75  1.3.4.2  yamt cv_init(kcondvar_t *cv, const char *wmesg)
     76  1.3.4.2  yamt {
     77  1.3.4.2  yamt 
     78  1.3.4.2  yamt 	KASSERT(wmesg != NULL);
     79  1.3.4.2  yamt 
     80  1.3.4.2  yamt 	cv->cv_wmesg = wmesg;
     81  1.3.4.2  yamt 	cv->cv_waiters = 0;
     82  1.3.4.2  yamt }
     83  1.3.4.2  yamt 
     84  1.3.4.2  yamt /*
     85  1.3.4.2  yamt  * cv_destroy:
     86  1.3.4.2  yamt  *
     87  1.3.4.2  yamt  *	Tear down a condition variable.
     88  1.3.4.2  yamt  */
     89  1.3.4.2  yamt void
     90  1.3.4.2  yamt cv_destroy(kcondvar_t *cv)
     91  1.3.4.2  yamt {
     92  1.3.4.2  yamt 
     93  1.3.4.2  yamt #ifdef DIAGNOSTIC
     94  1.3.4.3  yamt 	KASSERT(cv->cv_wmesg != deadcv && cv->cv_wmesg != NULL);
     95  1.3.4.3  yamt 	KASSERT(cv->cv_waiters == 0);
     96  1.3.4.3  yamt 	cv->cv_wmesg = deadcv;
     97  1.3.4.2  yamt #endif
     98  1.3.4.2  yamt }
     99  1.3.4.2  yamt 
    100  1.3.4.2  yamt /*
    101  1.3.4.2  yamt  * cv_enter:
    102  1.3.4.2  yamt  *
    103  1.3.4.2  yamt  *	Look up and lock the sleep queue corresponding to the given
    104  1.3.4.2  yamt  *	condition variable, and increment the number of waiters.
    105  1.3.4.2  yamt  */
    106  1.3.4.2  yamt static inline sleepq_t *
    107  1.3.4.3  yamt cv_enter(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l)
    108  1.3.4.2  yamt {
    109  1.3.4.2  yamt 	sleepq_t *sq;
    110  1.3.4.2  yamt 
    111  1.3.4.3  yamt 	KASSERT(cv->cv_wmesg != deadcv && cv->cv_wmesg != NULL);
    112  1.3.4.5  yamt 	KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
    113  1.3.4.2  yamt 
    114  1.3.4.3  yamt 	l->l_cv_signalled = 0;
    115  1.3.4.5  yamt 	l->l_kpriority = true;
    116  1.3.4.2  yamt 	sq = sleeptab_lookup(&sleeptab, cv);
    117  1.3.4.2  yamt 	cv->cv_waiters++;
    118  1.3.4.2  yamt 	sleepq_enter(sq, l);
    119  1.3.4.5  yamt 	sleepq_enqueue(sq, cv, cv->cv_wmesg, &cv_syncobj);
    120  1.3.4.2  yamt 	mutex_exit(mtx);
    121  1.3.4.2  yamt 
    122  1.3.4.2  yamt 	return sq;
    123  1.3.4.2  yamt }
    124  1.3.4.2  yamt 
    125  1.3.4.2  yamt /*
    126  1.3.4.3  yamt  * cv_exit:
    127  1.3.4.3  yamt  *
    128  1.3.4.3  yamt  *	After resuming execution, check to see if we have been restarted
    129  1.3.4.3  yamt  *	as a result of cv_signal().  If we have, but cannot take the
    130  1.3.4.3  yamt  *	wakeup (because of eg a pending Unix signal or timeout) then try
    131  1.3.4.3  yamt  *	to ensure that another LWP sees it.  This is necessary because
    132  1.3.4.3  yamt  *	there may be multiple waiters, and at least one should take the
    133  1.3.4.3  yamt  *	wakeup if possible.
    134  1.3.4.3  yamt  */
    135  1.3.4.3  yamt static inline int
    136  1.3.4.3  yamt cv_exit(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l, const int error)
    137  1.3.4.3  yamt {
    138  1.3.4.3  yamt 
    139  1.3.4.3  yamt 	mutex_enter(mtx);
    140  1.3.4.3  yamt 	if (__predict_false(error != 0) && l->l_cv_signalled != 0)
    141  1.3.4.3  yamt 		cv_signal(cv);
    142  1.3.4.3  yamt 
    143  1.3.4.3  yamt 	KASSERT(cv->cv_wmesg != deadcv && cv->cv_wmesg != NULL);
    144  1.3.4.3  yamt 
    145  1.3.4.3  yamt 	return error;
    146  1.3.4.3  yamt }
    147  1.3.4.3  yamt 
    148  1.3.4.3  yamt /*
    149  1.3.4.2  yamt  * cv_unsleep:
    150  1.3.4.2  yamt  *
    151  1.3.4.2  yamt  *	Remove an LWP from the condition variable and sleep queue.  This
    152  1.3.4.2  yamt  *	is called when the LWP has not been awoken normally but instead
    153  1.3.4.2  yamt  *	interrupted: for example, when a signal is received.  Must be
    154  1.3.4.2  yamt  *	called with the LWP locked, and must return it unlocked.
    155  1.3.4.2  yamt  */
    156  1.3.4.2  yamt static void
    157  1.3.4.3  yamt cv_unsleep(lwp_t *l)
    158  1.3.4.2  yamt {
    159  1.3.4.3  yamt 	kcondvar_t *cv;
    160  1.3.4.2  yamt 
    161  1.3.4.2  yamt 	KASSERT(l->l_wchan != NULL);
    162  1.3.4.3  yamt 	KASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
    163  1.3.4.2  yamt 
    164  1.3.4.3  yamt 	cv = (kcondvar_t *)(uintptr_t)l->l_wchan;
    165  1.3.4.3  yamt 	KASSERT(cv->cv_wmesg != deadcv && cv->cv_wmesg != NULL);
    166  1.3.4.3  yamt 	cv->cv_waiters--;
    167  1.3.4.2  yamt 
    168  1.3.4.2  yamt 	sleepq_unsleep(l);
    169  1.3.4.2  yamt }
    170  1.3.4.2  yamt 
    171  1.3.4.2  yamt /*
    172  1.3.4.2  yamt  * cv_wait:
    173  1.3.4.2  yamt  *
    174  1.3.4.2  yamt  *	Wait non-interruptably on a condition variable until awoken.
    175  1.3.4.2  yamt  */
    176  1.3.4.2  yamt void
    177  1.3.4.2  yamt cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    178  1.3.4.2  yamt {
    179  1.3.4.3  yamt 	lwp_t *l = curlwp;
    180  1.3.4.2  yamt 	sleepq_t *sq;
    181  1.3.4.2  yamt 
    182  1.3.4.3  yamt 	KASSERT(mutex_owned(mtx));
    183  1.3.4.2  yamt 
    184  1.3.4.2  yamt 	if (sleepq_dontsleep(l)) {
    185  1.3.4.2  yamt 		(void)sleepq_abort(mtx, 0);
    186  1.3.4.2  yamt 		return;
    187  1.3.4.2  yamt 	}
    188  1.3.4.2  yamt 
    189  1.3.4.2  yamt 	sq = cv_enter(cv, mtx, l);
    190  1.3.4.3  yamt 	(void)sleepq_block(0, false);
    191  1.3.4.3  yamt 	(void)cv_exit(cv, mtx, l, 0);
    192  1.3.4.2  yamt }
    193  1.3.4.2  yamt 
    194  1.3.4.2  yamt /*
    195  1.3.4.2  yamt  * cv_wait_sig:
    196  1.3.4.2  yamt  *
    197  1.3.4.2  yamt  *	Wait on a condition variable until a awoken or a signal is received.
    198  1.3.4.2  yamt  *	Will also return early if the process is exiting.  Returns zero if
    199  1.3.4.2  yamt  *	awoken normallly, ERESTART if a signal was received and the system
    200  1.3.4.2  yamt  *	call is restartable, or EINTR otherwise.
    201  1.3.4.2  yamt  */
    202  1.3.4.2  yamt int
    203  1.3.4.2  yamt cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    204  1.3.4.2  yamt {
    205  1.3.4.3  yamt 	lwp_t *l = curlwp;
    206  1.3.4.2  yamt 	sleepq_t *sq;
    207  1.3.4.2  yamt 	int error;
    208  1.3.4.2  yamt 
    209  1.3.4.3  yamt 	KASSERT(mutex_owned(mtx));
    210  1.3.4.2  yamt 
    211  1.3.4.2  yamt 	if (sleepq_dontsleep(l))
    212  1.3.4.2  yamt 		return sleepq_abort(mtx, 0);
    213  1.3.4.2  yamt 
    214  1.3.4.2  yamt 	sq = cv_enter(cv, mtx, l);
    215  1.3.4.3  yamt 	error = sleepq_block(0, true);
    216  1.3.4.3  yamt 	return cv_exit(cv, mtx, l, error);
    217  1.3.4.2  yamt }
    218  1.3.4.2  yamt 
    219  1.3.4.2  yamt /*
    220  1.3.4.2  yamt  * cv_timedwait:
    221  1.3.4.2  yamt  *
    222  1.3.4.2  yamt  *	Wait on a condition variable until awoken or the specified timeout
    223  1.3.4.2  yamt  *	expires.  Returns zero if awoken normally or EWOULDBLOCK if the
    224  1.3.4.2  yamt  *	timeout expired.
    225  1.3.4.2  yamt  */
    226  1.3.4.2  yamt int
    227  1.3.4.2  yamt cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
    228  1.3.4.2  yamt {
    229  1.3.4.3  yamt 	lwp_t *l = curlwp;
    230  1.3.4.2  yamt 	sleepq_t *sq;
    231  1.3.4.2  yamt 	int error;
    232  1.3.4.2  yamt 
    233  1.3.4.3  yamt 	KASSERT(mutex_owned(mtx));
    234  1.3.4.2  yamt 
    235  1.3.4.2  yamt 	if (sleepq_dontsleep(l))
    236  1.3.4.2  yamt 		return sleepq_abort(mtx, 0);
    237  1.3.4.2  yamt 
    238  1.3.4.2  yamt 	sq = cv_enter(cv, mtx, l);
    239  1.3.4.3  yamt 	error = sleepq_block(timo, false);
    240  1.3.4.3  yamt 	return cv_exit(cv, mtx, l, error);
    241  1.3.4.2  yamt }
    242  1.3.4.2  yamt 
    243  1.3.4.2  yamt /*
    244  1.3.4.2  yamt  * cv_timedwait_sig:
    245  1.3.4.2  yamt  *
    246  1.3.4.2  yamt  *	Wait on a condition variable until a timeout expires, awoken or a
    247  1.3.4.2  yamt  *	signal is received.  Will also return early if the process is
    248  1.3.4.2  yamt  *	exiting.  Returns zero if awoken normallly, EWOULDBLOCK if the
    249  1.3.4.2  yamt  *	timeout expires, ERESTART if a signal was received and the system
    250  1.3.4.2  yamt  *	call is restartable, or EINTR otherwise.
    251  1.3.4.2  yamt  */
    252  1.3.4.2  yamt int
    253  1.3.4.2  yamt cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
    254  1.3.4.2  yamt {
    255  1.3.4.3  yamt 	lwp_t *l = curlwp;
    256  1.3.4.2  yamt 	sleepq_t *sq;
    257  1.3.4.2  yamt 	int error;
    258  1.3.4.2  yamt 
    259  1.3.4.3  yamt 	KASSERT(mutex_owned(mtx));
    260  1.3.4.2  yamt 
    261  1.3.4.2  yamt 	if (sleepq_dontsleep(l))
    262  1.3.4.2  yamt 		return sleepq_abort(mtx, 0);
    263  1.3.4.2  yamt 
    264  1.3.4.2  yamt 	sq = cv_enter(cv, mtx, l);
    265  1.3.4.3  yamt 	error = sleepq_block(timo, true);
    266  1.3.4.3  yamt 	return cv_exit(cv, mtx, l, error);
    267  1.3.4.2  yamt }
    268  1.3.4.2  yamt 
    269  1.3.4.2  yamt /*
    270  1.3.4.2  yamt  * cv_signal:
    271  1.3.4.2  yamt  *
    272  1.3.4.2  yamt  *	Wake the highest priority LWP waiting on a condition variable.
    273  1.3.4.2  yamt  *	Must be called with the interlocking mutex held.
    274  1.3.4.2  yamt  */
    275  1.3.4.2  yamt void
    276  1.3.4.2  yamt cv_signal(kcondvar_t *cv)
    277  1.3.4.2  yamt {
    278  1.3.4.3  yamt 	lwp_t *l;
    279  1.3.4.2  yamt 	sleepq_t *sq;
    280  1.3.4.2  yamt 
    281  1.3.4.2  yamt 	if (cv->cv_waiters == 0)
    282  1.3.4.2  yamt 		return;
    283  1.3.4.2  yamt 
    284  1.3.4.2  yamt 	/*
    285  1.3.4.2  yamt 	 * cv->cv_waiters may be stale and have dropped to zero, but
    286  1.3.4.2  yamt 	 * while holding the interlock (the mutex passed to cv_wait()
    287  1.3.4.2  yamt 	 * and similar) we will see non-zero values when it matters.
    288  1.3.4.2  yamt 	 */
    289  1.3.4.2  yamt 
    290  1.3.4.2  yamt 	sq = sleeptab_lookup(&sleeptab, cv);
    291  1.3.4.2  yamt 	if (cv->cv_waiters != 0) {
    292  1.3.4.2  yamt 		cv->cv_waiters--;
    293  1.3.4.3  yamt 		l = sleepq_wake(sq, cv, 1);
    294  1.3.4.3  yamt 		l->l_cv_signalled = 1;
    295  1.3.4.2  yamt 	} else
    296  1.3.4.2  yamt 		sleepq_unlock(sq);
    297  1.3.4.2  yamt }
    298  1.3.4.2  yamt 
    299  1.3.4.2  yamt /*
    300  1.3.4.2  yamt  * cv_broadcast:
    301  1.3.4.2  yamt  *
    302  1.3.4.2  yamt  *	Wake all LWPs waiting on a condition variable.  Must be called
    303  1.3.4.2  yamt  *	with the interlocking mutex held.
    304  1.3.4.2  yamt  */
    305  1.3.4.2  yamt void
    306  1.3.4.2  yamt cv_broadcast(kcondvar_t *cv)
    307  1.3.4.2  yamt {
    308  1.3.4.2  yamt 	sleepq_t *sq;
    309  1.3.4.2  yamt 	u_int cnt;
    310  1.3.4.2  yamt 
    311  1.3.4.2  yamt 	if (cv->cv_waiters == 0)
    312  1.3.4.2  yamt 		return;
    313  1.3.4.2  yamt 
    314  1.3.4.2  yamt 	sq = sleeptab_lookup(&sleeptab, cv);
    315  1.3.4.2  yamt 	if ((cnt = cv->cv_waiters) != 0) {
    316  1.3.4.2  yamt 		cv->cv_waiters = 0;
    317  1.3.4.2  yamt 		sleepq_wake(sq, cv, cnt);
    318  1.3.4.2  yamt 	} else
    319  1.3.4.2  yamt 		sleepq_unlock(sq);
    320  1.3.4.2  yamt }
    321  1.3.4.2  yamt 
    322  1.3.4.2  yamt /*
    323  1.3.4.2  yamt  * cv_wakeup:
    324  1.3.4.2  yamt  *
    325  1.3.4.3  yamt  *	Wake all LWPs waiting on a condition variable.  For cases
    326  1.3.4.3  yamt  *	where the address may be waited on by mtsleep()/tsleep().
    327  1.3.4.3  yamt  *	Not a documented call.
    328  1.3.4.2  yamt  */
    329  1.3.4.2  yamt void
    330  1.3.4.2  yamt cv_wakeup(kcondvar_t *cv)
    331  1.3.4.2  yamt {
    332  1.3.4.2  yamt 	sleepq_t *sq;
    333  1.3.4.2  yamt 
    334  1.3.4.2  yamt 	sq = sleeptab_lookup(&sleeptab, cv);
    335  1.3.4.3  yamt 	cv->cv_waiters = 0;
    336  1.3.4.3  yamt 	sleepq_wake(sq, cv, (u_int)-1);
    337  1.3.4.2  yamt }
    338  1.3.4.2  yamt 
    339  1.3.4.2  yamt /*
    340  1.3.4.2  yamt  * cv_has_waiters:
    341  1.3.4.2  yamt  *
    342  1.3.4.2  yamt  *	For diagnostic assertions: return non-zero if a condition
    343  1.3.4.2  yamt  *	variable has waiters.
    344  1.3.4.2  yamt  */
    345  1.3.4.3  yamt bool
    346  1.3.4.2  yamt cv_has_waiters(kcondvar_t *cv)
    347  1.3.4.2  yamt {
    348  1.3.4.2  yamt 
    349  1.3.4.2  yamt 	/* No need to interlock here */
    350  1.3.4.3  yamt 	return cv->cv_waiters != 0;
    351  1.3.4.2  yamt }
    352