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