Home | History | Annotate | Line # | Download | only in kern
kern_sleepq.c revision 1.5.2.2
      1  1.5.2.2  yamt /*	$NetBSD: kern_sleepq.c,v 1.5.2.2 2007/02/26 09:11:11 yamt Exp $	*/
      2  1.5.2.2  yamt 
      3  1.5.2.2  yamt /*-
      4  1.5.2.2  yamt  * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
      5  1.5.2.2  yamt  * All rights reserved.
      6  1.5.2.2  yamt  *
      7  1.5.2.2  yamt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5.2.2  yamt  * by Andrew Doran.
      9  1.5.2.2  yamt  *
     10  1.5.2.2  yamt  * Redistribution and use in source and binary forms, with or without
     11  1.5.2.2  yamt  * modification, are permitted provided that the following conditions
     12  1.5.2.2  yamt  * are met:
     13  1.5.2.2  yamt  * 1. Redistributions of source code must retain the above copyright
     14  1.5.2.2  yamt  *    notice, this list of conditions and the following disclaimer.
     15  1.5.2.2  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.5.2.2  yamt  *    notice, this list of conditions and the following disclaimer in the
     17  1.5.2.2  yamt  *    documentation and/or other materials provided with the distribution.
     18  1.5.2.2  yamt  * 3. All advertising materials mentioning features or use of this software
     19  1.5.2.2  yamt  *    must display the following acknowledgement:
     20  1.5.2.2  yamt  *	This product includes software developed by the NetBSD
     21  1.5.2.2  yamt  *	Foundation, Inc. and its contributors.
     22  1.5.2.2  yamt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.5.2.2  yamt  *    contributors may be used to endorse or promote products derived
     24  1.5.2.2  yamt  *    from this software without specific prior written permission.
     25  1.5.2.2  yamt  *
     26  1.5.2.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.5.2.2  yamt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.5.2.2  yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.5.2.2  yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.5.2.2  yamt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.5.2.2  yamt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.5.2.2  yamt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.5.2.2  yamt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.5.2.2  yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.5.2.2  yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.5.2.2  yamt  * POSSIBILITY OF SUCH DAMAGE.
     37  1.5.2.2  yamt  */
     38  1.5.2.2  yamt 
     39  1.5.2.2  yamt /*
     40  1.5.2.2  yamt  * Sleep queue implementation, used by turnstiles and general sleep/wakeup
     41  1.5.2.2  yamt  * interfaces.
     42  1.5.2.2  yamt  */
     43  1.5.2.2  yamt 
     44  1.5.2.2  yamt #include <sys/cdefs.h>
     45  1.5.2.2  yamt __KERNEL_RCSID(0, "$NetBSD: kern_sleepq.c,v 1.5.2.2 2007/02/26 09:11:11 yamt Exp $");
     46  1.5.2.2  yamt 
     47  1.5.2.2  yamt #include "opt_multiprocessor.h"
     48  1.5.2.2  yamt #include "opt_lockdebug.h"
     49  1.5.2.2  yamt #include "opt_ktrace.h"
     50  1.5.2.2  yamt 
     51  1.5.2.2  yamt #include <sys/param.h>
     52  1.5.2.2  yamt #include <sys/lock.h>
     53  1.5.2.2  yamt #include <sys/kernel.h>
     54  1.5.2.2  yamt #include <sys/pool.h>
     55  1.5.2.2  yamt #include <sys/proc.h>
     56  1.5.2.2  yamt #include <sys/resourcevar.h>
     57  1.5.2.2  yamt #include <sys/sched.h>
     58  1.5.2.2  yamt #include <sys/systm.h>
     59  1.5.2.2  yamt #include <sys/sleepq.h>
     60  1.5.2.2  yamt #ifdef KTRACE
     61  1.5.2.2  yamt #include <sys/ktrace.h>
     62  1.5.2.2  yamt #endif
     63  1.5.2.2  yamt 
     64  1.5.2.2  yamt #include <uvm/uvm_extern.h>
     65  1.5.2.2  yamt 
     66  1.5.2.2  yamt int	sleepq_sigtoerror(struct lwp *, int);
     67  1.5.2.2  yamt void	updatepri(struct lwp *);
     68  1.5.2.2  yamt 
     69  1.5.2.2  yamt /* General purpose sleep table, used by ltsleep() and condition variables. */
     70  1.5.2.2  yamt sleeptab_t	sleeptab;
     71  1.5.2.2  yamt 
     72  1.5.2.2  yamt /*
     73  1.5.2.2  yamt  * sleeptab_init:
     74  1.5.2.2  yamt  *
     75  1.5.2.2  yamt  *	Initialize a sleep table.
     76  1.5.2.2  yamt  */
     77  1.5.2.2  yamt void
     78  1.5.2.2  yamt sleeptab_init(sleeptab_t *st)
     79  1.5.2.2  yamt {
     80  1.5.2.2  yamt 	sleepq_t *sq;
     81  1.5.2.2  yamt 	int i;
     82  1.5.2.2  yamt 
     83  1.5.2.2  yamt 	for (i = 0; i < SLEEPTAB_HASH_SIZE; i++) {
     84  1.5.2.2  yamt #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
     85  1.5.2.2  yamt 		sq = &st->st_queues[i].st_queue;
     86  1.5.2.2  yamt 		mutex_init(&st->st_queues[i].st_mutex, MUTEX_SPIN, IPL_SCHED);
     87  1.5.2.2  yamt 		sleepq_init(sq, &st->st_queues[i].st_mutex);
     88  1.5.2.2  yamt #else
     89  1.5.2.2  yamt 		sq = &st->st_queues[i];
     90  1.5.2.2  yamt 		sleepq_init(sq, &sched_mutex);
     91  1.5.2.2  yamt #endif
     92  1.5.2.2  yamt 	}
     93  1.5.2.2  yamt }
     94  1.5.2.2  yamt 
     95  1.5.2.2  yamt /*
     96  1.5.2.2  yamt  * sleepq_init:
     97  1.5.2.2  yamt  *
     98  1.5.2.2  yamt  *	Prepare a sleep queue for use.
     99  1.5.2.2  yamt  */
    100  1.5.2.2  yamt void
    101  1.5.2.2  yamt sleepq_init(sleepq_t *sq, kmutex_t *mtx)
    102  1.5.2.2  yamt {
    103  1.5.2.2  yamt 
    104  1.5.2.2  yamt 	sq->sq_waiters = 0;
    105  1.5.2.2  yamt 	sq->sq_mutex = mtx;
    106  1.5.2.2  yamt 	TAILQ_INIT(&sq->sq_queue);
    107  1.5.2.2  yamt }
    108  1.5.2.2  yamt 
    109  1.5.2.2  yamt /*
    110  1.5.2.2  yamt  * sleepq_remove:
    111  1.5.2.2  yamt  *
    112  1.5.2.2  yamt  *	Remove an LWP from a sleep queue and wake it up.  Return non-zero if
    113  1.5.2.2  yamt  *	the LWP is swapped out; if so the caller needs to awaken the swapper
    114  1.5.2.2  yamt  *	to bring the LWP into memory.
    115  1.5.2.2  yamt  */
    116  1.5.2.2  yamt int
    117  1.5.2.2  yamt sleepq_remove(sleepq_t *sq, struct lwp *l)
    118  1.5.2.2  yamt {
    119  1.5.2.2  yamt 	struct cpu_info *ci;
    120  1.5.2.2  yamt 
    121  1.5.2.2  yamt 	KASSERT(lwp_locked(l, sq->sq_mutex));
    122  1.5.2.2  yamt 	KASSERT(sq->sq_waiters > 0);
    123  1.5.2.2  yamt 
    124  1.5.2.2  yamt 	sq->sq_waiters--;
    125  1.5.2.2  yamt 	TAILQ_REMOVE(&sq->sq_queue, l, l_sleepchain);
    126  1.5.2.2  yamt 
    127  1.5.2.2  yamt #ifdef DIAGNOSTIC
    128  1.5.2.2  yamt 	if (sq->sq_waiters == 0)
    129  1.5.2.2  yamt 		KASSERT(TAILQ_FIRST(&sq->sq_queue) == NULL);
    130  1.5.2.2  yamt 	else
    131  1.5.2.2  yamt 		KASSERT(TAILQ_FIRST(&sq->sq_queue) != NULL);
    132  1.5.2.2  yamt #endif
    133  1.5.2.2  yamt 
    134  1.5.2.2  yamt 	l->l_syncobj = &sched_syncobj;
    135  1.5.2.2  yamt 	l->l_wchan = NULL;
    136  1.5.2.2  yamt 	l->l_sleepq = NULL;
    137  1.5.2.2  yamt 	l->l_flag &= ~LW_SINTR;
    138  1.5.2.2  yamt 
    139  1.5.2.2  yamt 	/*
    140  1.5.2.2  yamt 	 * If not sleeping, the LWP must have been suspended.  Let whoever
    141  1.5.2.2  yamt 	 * holds it stopped set it running again.
    142  1.5.2.2  yamt 	 */
    143  1.5.2.2  yamt 	if (l->l_stat != LSSLEEP) {
    144  1.5.2.2  yamt 	 	KASSERT(l->l_stat == LSSTOP || l->l_stat == LSSUSPENDED);
    145  1.5.2.2  yamt 		lwp_setlock(l, &sched_mutex);
    146  1.5.2.2  yamt 		return 0;
    147  1.5.2.2  yamt 	}
    148  1.5.2.2  yamt 
    149  1.5.2.2  yamt 	sched_lock(1);
    150  1.5.2.2  yamt 	lwp_setlock(l, &sched_mutex);
    151  1.5.2.2  yamt 
    152  1.5.2.2  yamt 	/*
    153  1.5.2.2  yamt 	 * If the LWP is still on the CPU, mark it as LSONPROC.  It may be
    154  1.5.2.2  yamt 	 * about to call mi_switch(), in which case it will yield.
    155  1.5.2.2  yamt 	 *
    156  1.5.2.2  yamt 	 * XXXSMP Will need to change for preemption.
    157  1.5.2.2  yamt 	 */
    158  1.5.2.2  yamt 	ci = l->l_cpu;
    159  1.5.2.2  yamt #ifdef MULTIPROCESSOR
    160  1.5.2.2  yamt 	if (ci->ci_curlwp == l) {
    161  1.5.2.2  yamt #else
    162  1.5.2.2  yamt 	if (l == curlwp) {
    163  1.5.2.2  yamt #endif
    164  1.5.2.2  yamt 		l->l_stat = LSONPROC;
    165  1.5.2.2  yamt 		l->l_slptime = 0;
    166  1.5.2.2  yamt 		sched_unlock(1);
    167  1.5.2.2  yamt 		return 0;
    168  1.5.2.2  yamt 	}
    169  1.5.2.2  yamt 
    170  1.5.2.2  yamt 	/*
    171  1.5.2.2  yamt 	 * Set it running.  We'll try to get the last CPU that ran
    172  1.5.2.2  yamt 	 * this LWP to pick it up again.
    173  1.5.2.2  yamt 	 */
    174  1.5.2.2  yamt 	if (l->l_slptime > 1)
    175  1.5.2.2  yamt 		updatepri(l);
    176  1.5.2.2  yamt 	l->l_stat = LSRUN;
    177  1.5.2.2  yamt 	l->l_slptime = 0;
    178  1.5.2.2  yamt 	if ((l->l_flag & LW_INMEM) != 0) {
    179  1.5.2.2  yamt 		setrunqueue(l);
    180  1.5.2.2  yamt 		if (l->l_priority < ci->ci_schedstate.spc_curpriority)
    181  1.5.2.2  yamt 			cpu_need_resched(ci);
    182  1.5.2.2  yamt 		sched_unlock(1);
    183  1.5.2.2  yamt 		return 0;
    184  1.5.2.2  yamt 	}
    185  1.5.2.2  yamt 
    186  1.5.2.2  yamt 	sched_unlock(1);
    187  1.5.2.2  yamt 	return 1;
    188  1.5.2.2  yamt }
    189  1.5.2.2  yamt 
    190  1.5.2.2  yamt /*
    191  1.5.2.2  yamt  * sleepq_insert:
    192  1.5.2.2  yamt  *
    193  1.5.2.2  yamt  *	Insert an LWP into the sleep queue, optionally sorting by priority.
    194  1.5.2.2  yamt  */
    195  1.5.2.2  yamt inline void
    196  1.5.2.2  yamt sleepq_insert(sleepq_t *sq, struct lwp *l, int pri, syncobj_t *sobj)
    197  1.5.2.2  yamt {
    198  1.5.2.2  yamt 	struct lwp *l2;
    199  1.5.2.2  yamt 
    200  1.5.2.2  yamt 	if ((sobj->sobj_flag & SOBJ_SLEEPQ_SORTED) != 0) {
    201  1.5.2.2  yamt 		TAILQ_FOREACH(l2, &sq->sq_queue, l_sleepchain) {
    202  1.5.2.2  yamt 			if (l2->l_priority > pri) {
    203  1.5.2.2  yamt 				TAILQ_INSERT_BEFORE(l2, l, l_sleepchain);
    204  1.5.2.2  yamt 				return;
    205  1.5.2.2  yamt 			}
    206  1.5.2.2  yamt 		}
    207  1.5.2.2  yamt 	}
    208  1.5.2.2  yamt 
    209  1.5.2.2  yamt 	TAILQ_INSERT_TAIL(&sq->sq_queue, l, l_sleepchain);
    210  1.5.2.2  yamt }
    211  1.5.2.2  yamt 
    212  1.5.2.2  yamt /*
    213  1.5.2.2  yamt  * sleepq_block:
    214  1.5.2.2  yamt  *
    215  1.5.2.2  yamt  *	Enter an LWP into the sleep queue and prepare for sleep.  The sleep
    216  1.5.2.2  yamt  *	queue must already be locked, and any interlock (such as the kernel
    217  1.5.2.2  yamt  *	lock) must have be released (see sleeptab_lookup(), sleepq_enter()).
    218  1.5.2.2  yamt  *
    219  1.5.2.2  yamt  * 	sleepq_block() may return early under exceptional conditions, for
    220  1.5.2.2  yamt  * 	example if the LWP's containing process is exiting.
    221  1.5.2.2  yamt  */
    222  1.5.2.2  yamt void
    223  1.5.2.2  yamt sleepq_block(sleepq_t *sq, int pri, wchan_t wchan, const char *wmesg, int timo,
    224  1.5.2.2  yamt 	     int catch, syncobj_t *sobj)
    225  1.5.2.2  yamt {
    226  1.5.2.2  yamt 	struct lwp *l = curlwp;
    227  1.5.2.2  yamt 
    228  1.5.2.2  yamt 	KASSERT(mutex_owned(sq->sq_mutex));
    229  1.5.2.2  yamt 	KASSERT(l->l_stat == LSONPROC);
    230  1.5.2.2  yamt 	KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
    231  1.5.2.2  yamt 
    232  1.5.2.2  yamt 	l->l_syncobj = sobj;
    233  1.5.2.2  yamt 	l->l_wchan = wchan;
    234  1.5.2.2  yamt 	l->l_sleepq = sq;
    235  1.5.2.2  yamt 	l->l_wmesg = wmesg;
    236  1.5.2.2  yamt 	l->l_slptime = 0;
    237  1.5.2.2  yamt 	l->l_priority = pri;
    238  1.5.2.2  yamt 	l->l_stat = LSSLEEP;
    239  1.5.2.2  yamt 	l->l_sleeperr = 0;
    240  1.5.2.2  yamt 	l->l_nvcsw++;
    241  1.5.2.2  yamt 
    242  1.5.2.2  yamt 	sq->sq_waiters++;
    243  1.5.2.2  yamt 	sleepq_insert(sq, l, pri, sobj);
    244  1.5.2.2  yamt 
    245  1.5.2.2  yamt #ifdef KTRACE
    246  1.5.2.2  yamt 	if (KTRPOINT(l->l_proc, KTR_CSW))
    247  1.5.2.2  yamt 		ktrcsw(l, 1, 0);
    248  1.5.2.2  yamt #endif
    249  1.5.2.2  yamt 
    250  1.5.2.2  yamt 	/*
    251  1.5.2.2  yamt 	 * If sleeping interruptably, check for pending signals, exits or
    252  1.5.2.2  yamt 	 * core dump events.
    253  1.5.2.2  yamt 	 */
    254  1.5.2.2  yamt 	if (catch) {
    255  1.5.2.2  yamt 		l->l_flag |= LW_SINTR;
    256  1.5.2.2  yamt 		if ((l->l_flag & LW_PENDSIG) != 0 && sigispending(l, 0)) {
    257  1.5.2.2  yamt 			l->l_sleeperr = EPASSTHROUGH;
    258  1.5.2.2  yamt 			/* lwp_unsleep() will release the lock */
    259  1.5.2.2  yamt 			lwp_unsleep(l);
    260  1.5.2.2  yamt 			return;
    261  1.5.2.2  yamt 		}
    262  1.5.2.2  yamt 		if ((l->l_flag & (LW_CANCELLED|LW_WEXIT|LW_WCORE)) != 0) {
    263  1.5.2.2  yamt 			l->l_flag &= ~LW_CANCELLED;
    264  1.5.2.2  yamt 			l->l_sleeperr = EINTR;
    265  1.5.2.2  yamt 			/* lwp_unsleep() will release the lock */
    266  1.5.2.2  yamt 			lwp_unsleep(l);
    267  1.5.2.2  yamt 			return;
    268  1.5.2.2  yamt 		}
    269  1.5.2.2  yamt 	}
    270  1.5.2.2  yamt 
    271  1.5.2.2  yamt 	if (timo)
    272  1.5.2.2  yamt 		callout_reset(&l->l_tsleep_ch, timo, sleepq_timeout, l);
    273  1.5.2.2  yamt 
    274  1.5.2.2  yamt 	mi_switch(l, NULL);
    275  1.5.2.2  yamt 	l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
    276  1.5.2.2  yamt 
    277  1.5.2.2  yamt 	/*
    278  1.5.2.2  yamt 	 * When we reach this point, the LWP and sleep queue are unlocked.
    279  1.5.2.2  yamt 	 */
    280  1.5.2.2  yamt 	KASSERT(l->l_wchan == NULL && l->l_sleepq == NULL);
    281  1.5.2.2  yamt }
    282  1.5.2.2  yamt 
    283  1.5.2.2  yamt /*
    284  1.5.2.2  yamt  * sleepq_unblock:
    285  1.5.2.2  yamt  *
    286  1.5.2.2  yamt  *	After any intermediate step such as updating statistics, re-acquire
    287  1.5.2.2  yamt  *	the kernel lock and record the switch for ktrace.  Note that we are
    288  1.5.2.2  yamt  *	no longer on the sleep queue at this point.
    289  1.5.2.2  yamt  *
    290  1.5.2.2  yamt  *	This is split out from sleepq_block() in expectation that at some
    291  1.5.2.2  yamt  *	point in the future, LWPs may awake on different kernel stacks than
    292  1.5.2.2  yamt  *	those they went asleep on.
    293  1.5.2.2  yamt  */
    294  1.5.2.2  yamt int
    295  1.5.2.2  yamt sleepq_unblock(int timo, int catch)
    296  1.5.2.2  yamt {
    297  1.5.2.2  yamt 	int error, expired, sig;
    298  1.5.2.2  yamt 	struct proc *p;
    299  1.5.2.2  yamt 	struct lwp *l;
    300  1.5.2.2  yamt 
    301  1.5.2.2  yamt 	l = curlwp;
    302  1.5.2.2  yamt 	error = l->l_sleeperr;
    303  1.5.2.2  yamt 
    304  1.5.2.2  yamt 	if (timo) {
    305  1.5.2.2  yamt 		/*
    306  1.5.2.2  yamt 		 * Even if the callout appears to have fired, we need to
    307  1.5.2.2  yamt 		 * stop it in order to synchronise with other CPUs.
    308  1.5.2.2  yamt 		 */
    309  1.5.2.2  yamt 		expired = callout_expired(&l->l_tsleep_ch);
    310  1.5.2.2  yamt 		callout_stop(&l->l_tsleep_ch);
    311  1.5.2.2  yamt 		if (expired && error == 0)
    312  1.5.2.2  yamt 			error = EWOULDBLOCK;
    313  1.5.2.2  yamt 	}
    314  1.5.2.2  yamt 
    315  1.5.2.2  yamt 	if (catch && (error == 0 || error == EPASSTHROUGH)) {
    316  1.5.2.2  yamt 		l->l_sleeperr = 0;
    317  1.5.2.2  yamt 		p = l->l_proc;
    318  1.5.2.2  yamt 		if ((l->l_flag & (LW_CANCELLED | LW_WEXIT | LW_WCORE)) != 0)
    319  1.5.2.2  yamt 			error = EINTR;
    320  1.5.2.2  yamt 		else if ((l->l_flag & LW_PENDSIG) != 0) {
    321  1.5.2.2  yamt 			KERNEL_LOCK(1, l);	/* XXXSMP pool_put() */
    322  1.5.2.2  yamt 			mutex_enter(&p->p_smutex);
    323  1.5.2.2  yamt 			if ((sig = issignal(l)) != 0)
    324  1.5.2.2  yamt 				error = sleepq_sigtoerror(l, sig);
    325  1.5.2.2  yamt 			mutex_exit(&p->p_smutex);
    326  1.5.2.2  yamt 			KERNEL_UNLOCK_LAST(l);
    327  1.5.2.2  yamt 		}
    328  1.5.2.2  yamt 		if (error == EPASSTHROUGH) {
    329  1.5.2.2  yamt 			/* Raced */
    330  1.5.2.2  yamt 			error = EINTR;
    331  1.5.2.2  yamt 		}
    332  1.5.2.2  yamt 	}
    333  1.5.2.2  yamt 
    334  1.5.2.2  yamt #ifdef KTRACE
    335  1.5.2.2  yamt 	if (KTRPOINT(l->l_proc, KTR_CSW))
    336  1.5.2.2  yamt 		ktrcsw(l, 0, 0);
    337  1.5.2.2  yamt #endif
    338  1.5.2.2  yamt 
    339  1.5.2.2  yamt 	KERNEL_LOCK(l->l_biglocks, l);
    340  1.5.2.2  yamt 	return error;
    341  1.5.2.2  yamt }
    342  1.5.2.2  yamt 
    343  1.5.2.2  yamt /*
    344  1.5.2.2  yamt  * sleepq_wake:
    345  1.5.2.2  yamt  *
    346  1.5.2.2  yamt  *	Wake zero or more LWPs blocked on a single wait channel.
    347  1.5.2.2  yamt  */
    348  1.5.2.2  yamt void
    349  1.5.2.2  yamt sleepq_wake(sleepq_t *sq, wchan_t wchan, u_int expected)
    350  1.5.2.2  yamt {
    351  1.5.2.2  yamt 	struct lwp *l, *next;
    352  1.5.2.2  yamt 	int swapin = 0;
    353  1.5.2.2  yamt 
    354  1.5.2.2  yamt 	KASSERT(mutex_owned(sq->sq_mutex));
    355  1.5.2.2  yamt 
    356  1.5.2.2  yamt 	for (l = TAILQ_FIRST(&sq->sq_queue); l != NULL; l = next) {
    357  1.5.2.2  yamt 		KASSERT(l->l_sleepq == sq);
    358  1.5.2.2  yamt 		next = TAILQ_NEXT(l, l_sleepchain);
    359  1.5.2.2  yamt 		if (l->l_wchan != wchan)
    360  1.5.2.2  yamt 			continue;
    361  1.5.2.2  yamt 		swapin |= sleepq_remove(sq, l);
    362  1.5.2.2  yamt 		if (--expected == 0)
    363  1.5.2.2  yamt 			break;
    364  1.5.2.2  yamt 	}
    365  1.5.2.2  yamt 
    366  1.5.2.2  yamt 	sleepq_unlock(sq);
    367  1.5.2.2  yamt 
    368  1.5.2.2  yamt 	/*
    369  1.5.2.2  yamt 	 * If there are newly awakend threads that need to be swapped in,
    370  1.5.2.2  yamt 	 * then kick the swapper into action.
    371  1.5.2.2  yamt 	 */
    372  1.5.2.2  yamt 	if (swapin)
    373  1.5.2.2  yamt 		uvm_kick_scheduler();
    374  1.5.2.2  yamt }
    375  1.5.2.2  yamt 
    376  1.5.2.2  yamt /*
    377  1.5.2.2  yamt  * sleepq_unsleep:
    378  1.5.2.2  yamt  *
    379  1.5.2.2  yamt  *	Remove an LWP from its sleep queue and set it runnable again.
    380  1.5.2.2  yamt  *	sleepq_unsleep() is called with the LWP's mutex held, and will
    381  1.5.2.2  yamt  *	always release it.
    382  1.5.2.2  yamt  */
    383  1.5.2.2  yamt void
    384  1.5.2.2  yamt sleepq_unsleep(struct lwp *l)
    385  1.5.2.2  yamt {
    386  1.5.2.2  yamt 	sleepq_t *sq = l->l_sleepq;
    387  1.5.2.2  yamt 	int swapin;
    388  1.5.2.2  yamt 
    389  1.5.2.2  yamt 	KASSERT(lwp_locked(l, NULL));
    390  1.5.2.2  yamt 	KASSERT(l->l_wchan != NULL);
    391  1.5.2.2  yamt 	KASSERT(l->l_mutex == sq->sq_mutex);
    392  1.5.2.2  yamt 
    393  1.5.2.2  yamt 	swapin = sleepq_remove(sq, l);
    394  1.5.2.2  yamt 	sleepq_unlock(sq);
    395  1.5.2.2  yamt 
    396  1.5.2.2  yamt 	if (swapin)
    397  1.5.2.2  yamt 		uvm_kick_scheduler();
    398  1.5.2.2  yamt }
    399  1.5.2.2  yamt 
    400  1.5.2.2  yamt /*
    401  1.5.2.2  yamt  * sleepq_timeout:
    402  1.5.2.2  yamt  *
    403  1.5.2.2  yamt  *	Entered via the callout(9) subsystem to time out an LWP that is on a
    404  1.5.2.2  yamt  *	sleep queue.
    405  1.5.2.2  yamt  */
    406  1.5.2.2  yamt void
    407  1.5.2.2  yamt sleepq_timeout(void *arg)
    408  1.5.2.2  yamt {
    409  1.5.2.2  yamt 	struct lwp *l = arg;
    410  1.5.2.2  yamt 
    411  1.5.2.2  yamt 	/*
    412  1.5.2.2  yamt 	 * Lock the LWP.  Assuming it's still on the sleep queue, its
    413  1.5.2.2  yamt 	 * current mutex will also be the sleep queue mutex.
    414  1.5.2.2  yamt 	 */
    415  1.5.2.2  yamt 	lwp_lock(l);
    416  1.5.2.2  yamt 
    417  1.5.2.2  yamt 	if (l->l_wchan == NULL) {
    418  1.5.2.2  yamt 		/* Somebody beat us to it. */
    419  1.5.2.2  yamt 		lwp_unlock(l);
    420  1.5.2.2  yamt 		return;
    421  1.5.2.2  yamt 	}
    422  1.5.2.2  yamt 
    423  1.5.2.2  yamt 	lwp_unsleep(l);
    424  1.5.2.2  yamt }
    425  1.5.2.2  yamt 
    426  1.5.2.2  yamt /*
    427  1.5.2.2  yamt  * sleepq_sigtoerror:
    428  1.5.2.2  yamt  *
    429  1.5.2.2  yamt  *	Given a signal number, interpret and return an error code.
    430  1.5.2.2  yamt  */
    431  1.5.2.2  yamt int
    432  1.5.2.2  yamt sleepq_sigtoerror(struct lwp *l, int sig)
    433  1.5.2.2  yamt {
    434  1.5.2.2  yamt 	struct proc *p = l->l_proc;
    435  1.5.2.2  yamt 	int error;
    436  1.5.2.2  yamt 
    437  1.5.2.2  yamt 	KASSERT(mutex_owned(&p->p_smutex));
    438  1.5.2.2  yamt 
    439  1.5.2.2  yamt 	/*
    440  1.5.2.2  yamt 	 * If this sleep was canceled, don't let the syscall restart.
    441  1.5.2.2  yamt 	 */
    442  1.5.2.2  yamt 	if ((SIGACTION(p, sig).sa_flags & SA_RESTART) == 0)
    443  1.5.2.2  yamt 		error = EINTR;
    444  1.5.2.2  yamt 	else
    445  1.5.2.2  yamt 		error = ERESTART;
    446  1.5.2.2  yamt 
    447  1.5.2.2  yamt 	return error;
    448  1.5.2.2  yamt }
    449  1.5.2.2  yamt 
    450  1.5.2.2  yamt /*
    451  1.5.2.2  yamt  * sleepq_abort:
    452  1.5.2.2  yamt  *
    453  1.5.2.2  yamt  *	After a panic or during autoconfiguration, lower the interrupt
    454  1.5.2.2  yamt  *	priority level to give pending interrupts a chance to run, and
    455  1.5.2.2  yamt  *	then return.  Called if sleepq_dontsleep() returns non-zero, and
    456  1.5.2.2  yamt  *	always returns zero.
    457  1.5.2.2  yamt  */
    458  1.5.2.2  yamt int
    459  1.5.2.2  yamt sleepq_abort(kmutex_t *mtx, int unlock)
    460  1.5.2.2  yamt {
    461  1.5.2.2  yamt 	extern int safepri;
    462  1.5.2.2  yamt 	int s;
    463  1.5.2.2  yamt 
    464  1.5.2.2  yamt 	s = splhigh();
    465  1.5.2.2  yamt 	splx(safepri);
    466  1.5.2.2  yamt 	splx(s);
    467  1.5.2.2  yamt 	if (mtx != NULL && unlock != 0)
    468  1.5.2.2  yamt 		mutex_exit(mtx);
    469  1.5.2.2  yamt 
    470  1.5.2.2  yamt 	return 0;
    471  1.5.2.2  yamt }
    472  1.5.2.2  yamt 
    473  1.5.2.2  yamt /*
    474  1.5.2.2  yamt  * sleepq_changepri:
    475  1.5.2.2  yamt  *
    476  1.5.2.2  yamt  *	Adjust the priority of an LWP residing on a sleepq.  This method
    477  1.5.2.2  yamt  *	will only alter the user priority; the effective priority is
    478  1.5.2.2  yamt  *	assumed to have been fixed at the time of insertion into the queue.
    479  1.5.2.2  yamt  */
    480  1.5.2.2  yamt void
    481  1.5.2.2  yamt sleepq_changepri(struct lwp *l, int pri)
    482  1.5.2.2  yamt {
    483  1.5.2.2  yamt 
    484  1.5.2.2  yamt 	KASSERT(lwp_locked(l, l->l_sleepq->sq_mutex));
    485  1.5.2.2  yamt 	l->l_usrpri = pri;
    486  1.5.2.2  yamt }
    487