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