Home | History | Annotate | Line # | Download | only in kern
kern_condvar.c revision 1.19
      1  1.19    ad /*	$NetBSD: kern_condvar.c,v 1.19 2008/05/26 12:58:24 ad 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.19    ad __KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.19 2008/05/26 12:58:24 ad 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.2    ad 
     50  1.16    ad static u_int	cv_unsleep(lwp_t *, bool);
     51   1.2    ad 
     52  1.10    ad static syncobj_t cv_syncobj = {
     53   1.2    ad 	SOBJ_SLEEPQ_SORTED,
     54   1.2    ad 	cv_unsleep,
     55  1.14    ad 	sleepq_changepri,
     56   1.4  yamt 	sleepq_lendpri,
     57   1.4  yamt 	syncobj_noowner,
     58   1.2    ad };
     59   1.2    ad 
     60  1.10    ad static const char deadcv[] = "deadcv";
     61  1.10    ad 
     62   1.2    ad /*
     63   1.2    ad  * cv_init:
     64   1.2    ad  *
     65   1.2    ad  *	Initialize a condition variable for use.
     66   1.2    ad  */
     67   1.2    ad void
     68   1.2    ad cv_init(kcondvar_t *cv, const char *wmesg)
     69   1.2    ad {
     70   1.2    ad 
     71   1.2    ad 	KASSERT(wmesg != NULL);
     72   1.2    ad 
     73   1.2    ad 	cv->cv_wmesg = wmesg;
     74   1.2    ad 	cv->cv_waiters = 0;
     75   1.2    ad }
     76   1.2    ad 
     77   1.2    ad /*
     78   1.2    ad  * cv_destroy:
     79   1.2    ad  *
     80   1.2    ad  *	Tear down a condition variable.
     81   1.2    ad  */
     82   1.2    ad void
     83   1.2    ad cv_destroy(kcondvar_t *cv)
     84   1.2    ad {
     85   1.2    ad 
     86   1.2    ad #ifdef DIAGNOSTIC
     87  1.15    ad 	KASSERT(cv_is_valid(cv));
     88  1.10    ad 	cv->cv_wmesg = deadcv;
     89  1.15    ad 	cv->cv_waiters = -3;
     90   1.2    ad #endif
     91   1.2    ad }
     92   1.2    ad 
     93   1.2    ad /*
     94   1.2    ad  * cv_enter:
     95   1.2    ad  *
     96   1.2    ad  *	Look up and lock the sleep queue corresponding to the given
     97   1.2    ad  *	condition variable, and increment the number of waiters.
     98   1.2    ad  */
     99   1.2    ad static inline sleepq_t *
    100   1.6    ad cv_enter(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l)
    101   1.2    ad {
    102   1.2    ad 	sleepq_t *sq;
    103  1.18    ad 	kmutex_t *mp;
    104   1.2    ad 
    105  1.15    ad 	KASSERT(cv_is_valid(cv));
    106  1.14    ad 	KASSERT((l->l_pflag & LP_INTR) == 0 || panicstr != NULL);
    107   1.2    ad 
    108   1.6    ad 	l->l_cv_signalled = 0;
    109  1.14    ad 	l->l_kpriority = true;
    110  1.18    ad 	sq = sleeptab_lookup(&sleeptab, cv, &mp);
    111   1.2    ad 	cv->cv_waiters++;
    112  1.18    ad 	sleepq_enter(sq, l, mp);
    113  1.14    ad 	sleepq_enqueue(sq, cv, cv->cv_wmesg, &cv_syncobj);
    114   1.2    ad 	mutex_exit(mtx);
    115   1.2    ad 
    116   1.2    ad 	return sq;
    117   1.2    ad }
    118   1.2    ad 
    119   1.2    ad /*
    120   1.6    ad  * cv_exit:
    121   1.6    ad  *
    122   1.6    ad  *	After resuming execution, check to see if we have been restarted
    123   1.6    ad  *	as a result of cv_signal().  If we have, but cannot take the
    124   1.6    ad  *	wakeup (because of eg a pending Unix signal or timeout) then try
    125   1.6    ad  *	to ensure that another LWP sees it.  This is necessary because
    126   1.6    ad  *	there may be multiple waiters, and at least one should take the
    127   1.6    ad  *	wakeup if possible.
    128   1.6    ad  */
    129   1.6    ad static inline int
    130   1.6    ad cv_exit(kcondvar_t *cv, kmutex_t *mtx, lwp_t *l, const int error)
    131   1.6    ad {
    132   1.6    ad 
    133   1.6    ad 	mutex_enter(mtx);
    134   1.6    ad 	if (__predict_false(error != 0) && l->l_cv_signalled != 0)
    135   1.6    ad 		cv_signal(cv);
    136   1.6    ad 
    137  1.15    ad 	KASSERT(cv_is_valid(cv));
    138  1.10    ad 
    139   1.6    ad 	return error;
    140   1.6    ad }
    141   1.6    ad 
    142   1.6    ad /*
    143   1.2    ad  * cv_unsleep:
    144   1.2    ad  *
    145   1.2    ad  *	Remove an LWP from the condition variable and sleep queue.  This
    146   1.2    ad  *	is called when the LWP has not been awoken normally but instead
    147   1.2    ad  *	interrupted: for example, when a signal is received.  Must be
    148   1.2    ad  *	called with the LWP locked, and must return it unlocked.
    149   1.2    ad  */
    150  1.16    ad static u_int
    151  1.16    ad cv_unsleep(lwp_t *l, bool cleanup)
    152   1.2    ad {
    153  1.10    ad 	kcondvar_t *cv;
    154   1.2    ad 
    155  1.15    ad 	cv = (kcondvar_t *)(uintptr_t)l->l_wchan;
    156  1.15    ad 
    157   1.2    ad 	KASSERT(l->l_wchan != NULL);
    158  1.19    ad 	KASSERT(lwp_locked(l, NULL));
    159  1.15    ad 	KASSERT(cv_is_valid(cv));
    160  1.15    ad 	KASSERT(cv->cv_waiters > 0);
    161   1.2    ad 
    162  1.10    ad 	cv->cv_waiters--;
    163  1.16    ad 	return sleepq_unsleep(l, cleanup);
    164   1.2    ad }
    165   1.2    ad 
    166   1.2    ad /*
    167   1.2    ad  * cv_wait:
    168   1.2    ad  *
    169   1.2    ad  *	Wait non-interruptably on a condition variable until awoken.
    170   1.2    ad  */
    171   1.2    ad void
    172   1.2    ad cv_wait(kcondvar_t *cv, kmutex_t *mtx)
    173   1.2    ad {
    174   1.6    ad 	lwp_t *l = curlwp;
    175   1.2    ad 	sleepq_t *sq;
    176   1.2    ad 
    177   1.8  yamt 	KASSERT(mutex_owned(mtx));
    178   1.2    ad 
    179   1.2    ad 	if (sleepq_dontsleep(l)) {
    180   1.2    ad 		(void)sleepq_abort(mtx, 0);
    181   1.2    ad 		return;
    182   1.2    ad 	}
    183   1.2    ad 
    184   1.2    ad 	sq = cv_enter(cv, mtx, l);
    185   1.8  yamt 	(void)sleepq_block(0, false);
    186   1.6    ad 	(void)cv_exit(cv, mtx, l, 0);
    187   1.2    ad }
    188   1.2    ad 
    189   1.2    ad /*
    190   1.2    ad  * cv_wait_sig:
    191   1.2    ad  *
    192   1.2    ad  *	Wait on a condition variable until a awoken or a signal is received.
    193   1.2    ad  *	Will also return early if the process is exiting.  Returns zero if
    194   1.2    ad  *	awoken normallly, ERESTART if a signal was received and the system
    195   1.2    ad  *	call is restartable, or EINTR otherwise.
    196   1.2    ad  */
    197   1.2    ad int
    198   1.2    ad cv_wait_sig(kcondvar_t *cv, kmutex_t *mtx)
    199   1.2    ad {
    200   1.6    ad 	lwp_t *l = curlwp;
    201   1.2    ad 	sleepq_t *sq;
    202   1.2    ad 	int error;
    203   1.2    ad 
    204   1.8  yamt 	KASSERT(mutex_owned(mtx));
    205   1.2    ad 
    206   1.2    ad 	if (sleepq_dontsleep(l))
    207   1.2    ad 		return sleepq_abort(mtx, 0);
    208   1.2    ad 
    209   1.2    ad 	sq = cv_enter(cv, mtx, l);
    210   1.8  yamt 	error = sleepq_block(0, true);
    211   1.6    ad 	return cv_exit(cv, mtx, l, error);
    212   1.2    ad }
    213   1.2    ad 
    214   1.2    ad /*
    215   1.2    ad  * cv_timedwait:
    216   1.2    ad  *
    217   1.2    ad  *	Wait on a condition variable until awoken or the specified timeout
    218   1.2    ad  *	expires.  Returns zero if awoken normally or EWOULDBLOCK if the
    219   1.2    ad  *	timeout expired.
    220   1.2    ad  */
    221   1.2    ad int
    222   1.2    ad cv_timedwait(kcondvar_t *cv, kmutex_t *mtx, int timo)
    223   1.2    ad {
    224   1.6    ad 	lwp_t *l = curlwp;
    225   1.2    ad 	sleepq_t *sq;
    226   1.2    ad 	int error;
    227   1.2    ad 
    228   1.8  yamt 	KASSERT(mutex_owned(mtx));
    229   1.2    ad 
    230   1.2    ad 	if (sleepq_dontsleep(l))
    231   1.2    ad 		return sleepq_abort(mtx, 0);
    232   1.2    ad 
    233   1.2    ad 	sq = cv_enter(cv, mtx, l);
    234   1.8  yamt 	error = sleepq_block(timo, false);
    235   1.6    ad 	return cv_exit(cv, mtx, l, error);
    236   1.2    ad }
    237   1.2    ad 
    238   1.2    ad /*
    239   1.2    ad  * cv_timedwait_sig:
    240   1.2    ad  *
    241   1.2    ad  *	Wait on a condition variable until a timeout expires, awoken or a
    242   1.2    ad  *	signal is received.  Will also return early if the process is
    243   1.2    ad  *	exiting.  Returns zero if awoken normallly, EWOULDBLOCK if the
    244   1.2    ad  *	timeout expires, ERESTART if a signal was received and the system
    245   1.2    ad  *	call is restartable, or EINTR otherwise.
    246   1.2    ad  */
    247   1.2    ad int
    248   1.2    ad cv_timedwait_sig(kcondvar_t *cv, kmutex_t *mtx, int timo)
    249   1.2    ad {
    250   1.6    ad 	lwp_t *l = curlwp;
    251   1.2    ad 	sleepq_t *sq;
    252   1.2    ad 	int error;
    253   1.2    ad 
    254   1.8  yamt 	KASSERT(mutex_owned(mtx));
    255   1.2    ad 
    256   1.2    ad 	if (sleepq_dontsleep(l))
    257   1.2    ad 		return sleepq_abort(mtx, 0);
    258   1.2    ad 
    259   1.2    ad 	sq = cv_enter(cv, mtx, l);
    260   1.8  yamt 	error = sleepq_block(timo, true);
    261   1.6    ad 	return cv_exit(cv, mtx, l, error);
    262   1.2    ad }
    263   1.2    ad 
    264   1.2    ad /*
    265   1.2    ad  * cv_signal:
    266   1.2    ad  *
    267   1.2    ad  *	Wake the highest priority LWP waiting on a condition variable.
    268   1.2    ad  *	Must be called with the interlocking mutex held.
    269   1.2    ad  */
    270   1.2    ad void
    271   1.2    ad cv_signal(kcondvar_t *cv)
    272   1.2    ad {
    273   1.6    ad 	lwp_t *l;
    274   1.2    ad 	sleepq_t *sq;
    275  1.18    ad 	kmutex_t *mp;
    276   1.2    ad 
    277  1.15    ad 	KASSERT(cv_is_valid(cv));
    278  1.15    ad 
    279   1.2    ad 	if (cv->cv_waiters == 0)
    280   1.2    ad 		return;
    281   1.2    ad 
    282   1.2    ad 	/*
    283   1.2    ad 	 * cv->cv_waiters may be stale and have dropped to zero, but
    284   1.2    ad 	 * while holding the interlock (the mutex passed to cv_wait()
    285   1.2    ad 	 * and similar) we will see non-zero values when it matters.
    286   1.2    ad 	 */
    287   1.2    ad 
    288  1.18    ad 	sq = sleeptab_lookup(&sleeptab, cv, &mp);
    289   1.2    ad 	if (cv->cv_waiters != 0) {
    290   1.2    ad 		cv->cv_waiters--;
    291  1.18    ad 		l = sleepq_wake(sq, cv, 1, mp);
    292   1.6    ad 		l->l_cv_signalled = 1;
    293   1.2    ad 	} else
    294  1.18    ad 		mutex_spin_exit(mp);
    295  1.15    ad 
    296  1.15    ad 	KASSERT(cv_is_valid(cv));
    297   1.2    ad }
    298   1.2    ad 
    299   1.2    ad /*
    300   1.2    ad  * cv_broadcast:
    301   1.2    ad  *
    302   1.2    ad  *	Wake all LWPs waiting on a condition variable.  Must be called
    303   1.2    ad  *	with the interlocking mutex held.
    304   1.2    ad  */
    305   1.2    ad void
    306   1.2    ad cv_broadcast(kcondvar_t *cv)
    307   1.2    ad {
    308   1.2    ad 	sleepq_t *sq;
    309  1.18    ad 	kmutex_t *mp;
    310   1.2    ad 	u_int cnt;
    311   1.2    ad 
    312  1.15    ad 	KASSERT(cv_is_valid(cv));
    313  1.15    ad 
    314   1.2    ad 	if (cv->cv_waiters == 0)
    315   1.2    ad 		return;
    316   1.2    ad 
    317  1.18    ad 	sq = sleeptab_lookup(&sleeptab, cv, &mp);
    318   1.2    ad 	if ((cnt = cv->cv_waiters) != 0) {
    319   1.2    ad 		cv->cv_waiters = 0;
    320  1.18    ad 		sleepq_wake(sq, cv, cnt, mp);
    321   1.2    ad 	} else
    322  1.18    ad 		mutex_spin_exit(mp);
    323  1.15    ad 
    324  1.15    ad 	KASSERT(cv_is_valid(cv));
    325   1.2    ad }
    326   1.2    ad 
    327   1.2    ad /*
    328  1.11    ad  * cv_wakeup:
    329  1.11    ad  *
    330  1.11    ad  *	Wake all LWPs waiting on a condition variable.  For cases
    331  1.11    ad  *	where the address may be waited on by mtsleep()/tsleep().
    332  1.11    ad  *	Not a documented call.
    333  1.11    ad  */
    334  1.11    ad void
    335  1.11    ad cv_wakeup(kcondvar_t *cv)
    336  1.11    ad {
    337  1.11    ad 	sleepq_t *sq;
    338  1.18    ad 	kmutex_t *mp;
    339  1.11    ad 
    340  1.15    ad 	KASSERT(cv_is_valid(cv));
    341  1.15    ad 
    342  1.18    ad 	sq = sleeptab_lookup(&sleeptab, cv, &mp);
    343  1.12    ad 	cv->cv_waiters = 0;
    344  1.18    ad 	sleepq_wake(sq, cv, (u_int)-1, mp);
    345  1.15    ad 
    346  1.15    ad 	KASSERT(cv_is_valid(cv));
    347  1.11    ad }
    348  1.11    ad 
    349  1.11    ad /*
    350   1.2    ad  * cv_has_waiters:
    351   1.2    ad  *
    352   1.2    ad  *	For diagnostic assertions: return non-zero if a condition
    353   1.2    ad  *	variable has waiters.
    354   1.2    ad  */
    355   1.7    ad bool
    356   1.2    ad cv_has_waiters(kcondvar_t *cv)
    357   1.2    ad {
    358   1.2    ad 
    359   1.2    ad 	/* No need to interlock here */
    360   1.7    ad 	return cv->cv_waiters != 0;
    361   1.2    ad }
    362  1.15    ad 
    363  1.15    ad /*
    364  1.15    ad  * cv_is_valid:
    365  1.15    ad  *
    366  1.15    ad  *	For diagnostic assertions: return non-zero if a condition
    367  1.15    ad  *	variable appears to be valid.  No locks need be held.
    368  1.15    ad  */
    369  1.15    ad bool
    370  1.15    ad cv_is_valid(kcondvar_t *cv)
    371  1.15    ad {
    372  1.15    ad 
    373  1.15    ad 	if (cv->cv_wmesg == deadcv || cv->cv_wmesg == NULL)
    374  1.15    ad 		return false;
    375  1.15    ad 	if ((cv->cv_waiters & 0xff000000) != 0) {
    376  1.15    ad 		/* Arbitrary: invalid number of waiters. */
    377  1.15    ad 		return false;
    378  1.15    ad 	}
    379  1.15    ad 	return cv->cv_waiters >= 0;
    380  1.15    ad }
    381