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