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