Home | History | Annotate | Line # | Download | only in kern
kern_turnstile.c revision 1.1.36.1
      1  1.1.36.1  ad /*	$NetBSD: kern_turnstile.c,v 1.1.36.1 2006/09/10 23:42:42 ad Exp $	*/
      2  1.1.36.1  ad 
      3  1.1.36.1  ad /*-
      4  1.1.36.1  ad  * Copyright (c) 2002, 2006 The NetBSD Foundation, Inc.
      5  1.1.36.1  ad  * All rights reserved.
      6  1.1.36.1  ad  *
      7  1.1.36.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.36.1  ad  * by Jason R. Thorpe and Andrew Doran.
      9  1.1.36.1  ad  *
     10  1.1.36.1  ad  * Redistribution and use in source and binary forms, with or without
     11  1.1.36.1  ad  * modification, are permitted provided that the following conditions
     12  1.1.36.1  ad  * are met:
     13  1.1.36.1  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1.36.1  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1.36.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.36.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.36.1  ad  *    documentation and/or other materials provided with the distribution.
     18  1.1.36.1  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1.36.1  ad  *    must display the following acknowledgement:
     20  1.1.36.1  ad  *	This product includes software developed by the NetBSD
     21  1.1.36.1  ad  *	Foundation, Inc. and its contributors.
     22  1.1.36.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.36.1  ad  *    contributors may be used to endorse or promote products derived
     24  1.1.36.1  ad  *    from this software without specific prior written permission.
     25  1.1.36.1  ad  *
     26  1.1.36.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.36.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.36.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.36.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.36.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.36.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.36.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.36.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.36.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.36.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.36.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.36.1  ad  */
     38  1.1.36.1  ad 
     39  1.1.36.1  ad /*
     40  1.1.36.1  ad  * Turnsiles are specialized sleep queues for use by locks.  Turnstiles
     41  1.1.36.1  ad  * are described in detail in:
     42  1.1.36.1  ad  *
     43  1.1.36.1  ad  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     44  1.1.36.1  ad  *	    Richard McDougall.
     45  1.1.36.1  ad  *
     46  1.1.36.1  ad  * Turnstiles are kept in a hash table.  There are likely to be many more
     47  1.1.36.1  ad  * lock objects than there are threads.  Since a thread can block on only
     48  1.1.36.1  ad  * one lock at a time, we only need one turnstile per thread, and so they
     49  1.1.36.1  ad  * are allocated at thread creation time.
     50  1.1.36.1  ad  *
     51  1.1.36.1  ad  * When a thread decides it needs to block on a lock, it looks up the
     52  1.1.36.1  ad  * active turnstile for that lock.  If no active turnstile exists, then
     53  1.1.36.1  ad  * the process lends its turnstile to the lock.  If there is already
     54  1.1.36.1  ad  * an active turnstile for the lock, the thread places its turnstile on
     55  1.1.36.1  ad  * a list of free turnstiles, and references the active one instead.
     56  1.1.36.1  ad  *
     57  1.1.36.1  ad  * The act of looking up the turnstile acquires an interlock on the sleep
     58  1.1.36.1  ad  * queue.  If a thread decides it doesn't need to block after all, then
     59  1.1.36.1  ad  * this interlock must be released by explicitly aborting the turnstile
     60  1.1.36.1  ad  * operation.
     61  1.1.36.1  ad  *
     62  1.1.36.1  ad  * When a thread is awakened, it needs to get its turnstile back.  If
     63  1.1.36.1  ad  * there are still other threads waiting in the active turnstile, the
     64  1.1.36.1  ad  * the thread grabs a free turnstile off the free list.  Otherwise, it
     65  1.1.36.1  ad  * can take back the active turnstile from the lock (thus deactivating
     66  1.1.36.1  ad  * the turnstile).
     67  1.1.36.1  ad  *
     68  1.1.36.1  ad  * Turnstiles are the place to do priority inheritence.  However, we do
     69  1.1.36.1  ad  * not currently implement that.
     70  1.1.36.1  ad  *
     71  1.1.36.1  ad  * XXX We currently have to interlock with the sched_lock.  The locking
     72  1.1.36.1  ad  * order is:
     73  1.1.36.1  ad  *
     74  1.1.36.1  ad  *	turnstile chain -> sched_lock
     75  1.1.36.1  ad  */
     76  1.1.36.1  ad 
     77  1.1.36.1  ad #include "opt_lockdebug.h"
     78  1.1.36.1  ad #include "opt_multiprocessor.h"
     79  1.1.36.1  ad 
     80  1.1.36.1  ad #include <sys/cdefs.h>
     81  1.1.36.1  ad __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.1.36.1 2006/09/10 23:42:42 ad Exp $");
     82  1.1.36.1  ad 
     83  1.1.36.1  ad #include <sys/param.h>
     84  1.1.36.1  ad #include <sys/lock.h>
     85  1.1.36.1  ad #include <sys/pool.h>
     86  1.1.36.1  ad #include <sys/proc.h>
     87  1.1.36.1  ad #include <sys/resourcevar.h>
     88  1.1.36.1  ad #include <sys/sched.h>
     89  1.1.36.1  ad #include <sys/turnstile.h>
     90  1.1.36.1  ad #include <sys/systm.h>
     91  1.1.36.1  ad #include <sys/sa.h>
     92  1.1.36.1  ad #include <sys/savar.h>
     93  1.1.36.1  ad 
     94  1.1.36.1  ad /* XXX */
     95  1.1.36.1  ad #ifdef MULTIPROCESSOR
     96  1.1.36.1  ad #define	_SCHED_LOCK	simple_lock(&sched_lock);
     97  1.1.36.1  ad #define	_SCHED_UNLOCK	simple_unlock(&sched_lock);
     98  1.1.36.1  ad #else
     99  1.1.36.1  ad #define	_SCHED_LOCK	/* nothing */
    100  1.1.36.1  ad #define	_SCHED_UNLOCK	/* nothing */
    101  1.1.36.1  ad #endif
    102  1.1.36.1  ad 
    103  1.1.36.1  ad /*
    104  1.1.36.1  ad  * Turnstile hash -- shift the lock object to eliminate the zero bits
    105  1.1.36.1  ad  * of the address, and mask it off with the turnstile table's size.
    106  1.1.36.1  ad  */
    107  1.1.36.1  ad #if LONG_BIT == 64
    108  1.1.36.1  ad #define	TURNSTILE_HASH_SHIFT	3
    109  1.1.36.1  ad #elif LONG_BIT == 32
    110  1.1.36.1  ad #define	TURNSTILE_HASH_SHIFT	2
    111  1.1.36.1  ad #else
    112  1.1.36.1  ad #error "Don't know how big your pointers are."
    113  1.1.36.1  ad #endif
    114  1.1.36.1  ad 
    115  1.1.36.1  ad #define	TURNSTILE_HASH_SIZE	64
    116  1.1.36.1  ad #define	TURNSTILE_HASH_MASK	(TURNSTILE_HASH_SIZE - 1)
    117  1.1.36.1  ad 
    118  1.1.36.1  ad #define	TURNSTILE_HASH(obj)						\
    119  1.1.36.1  ad 	((((u_long)(obj)) >> TURNSTILE_HASH_SHIFT) & TURNSTILE_HASH_MASK)
    120  1.1.36.1  ad 
    121  1.1.36.1  ad struct turnstile_chain {
    122  1.1.36.1  ad 	struct simplelock tc_lock;	/* lock on hash chain */
    123  1.1.36.1  ad 	int		tc_oldspl;	/* saved spl of lock holder
    124  1.1.36.1  ad 					   (only valid while tc_lock held) */
    125  1.1.36.1  ad 	LIST_HEAD(, turnstile) tc_chain;/* turnstile chain */
    126  1.1.36.1  ad } turnstile_table[TURNSTILE_HASH_SIZE];
    127  1.1.36.1  ad 
    128  1.1.36.1  ad #define	TURNSTILE_CHAIN(obj)						\
    129  1.1.36.1  ad 	&turnstile_table[TURNSTILE_HASH(obj)]
    130  1.1.36.1  ad 
    131  1.1.36.1  ad #define	TURNSTILE_CHAIN_LOCK(tc)					\
    132  1.1.36.1  ad do {									\
    133  1.1.36.1  ad 	int _s_ = spllock();						\
    134  1.1.36.1  ad 	simple_lock(&(tc)->tc_lock);					\
    135  1.1.36.1  ad 	(tc)->tc_oldspl = _s_;						\
    136  1.1.36.1  ad } while (/*CONSTCOND*/0)
    137  1.1.36.1  ad 
    138  1.1.36.1  ad #define	TURNSTILE_CHAIN_UNLOCK(tc)					\
    139  1.1.36.1  ad do {									\
    140  1.1.36.1  ad 	int _s_ = (tc)->tc_oldspl;					\
    141  1.1.36.1  ad 	simple_unlock(&(tc)->tc_lock);					\
    142  1.1.36.1  ad 	splx(_s_);							\
    143  1.1.36.1  ad } while (/*CONSTCOND*/0)
    144  1.1.36.1  ad 
    145  1.1.36.1  ad struct pool turnstile_pool;
    146  1.1.36.1  ad struct pool_cache turnstile_cache;
    147  1.1.36.1  ad 
    148  1.1.36.1  ad int	turnstile_ctor(void *, void *, int);
    149  1.1.36.1  ad 
    150  1.1.36.1  ad /*
    151  1.1.36.1  ad  * turnstile_init:
    152  1.1.36.1  ad  *
    153  1.1.36.1  ad  *	Initialize the turnstile mechanism.
    154  1.1.36.1  ad  */
    155  1.1.36.1  ad void
    156  1.1.36.1  ad turnstile_init(void)
    157  1.1.36.1  ad {
    158  1.1.36.1  ad 	struct turnstile_chain *tc;
    159  1.1.36.1  ad 	int i;
    160  1.1.36.1  ad 
    161  1.1.36.1  ad 	for (i = 0; i < TURNSTILE_HASH_SIZE; i++) {
    162  1.1.36.1  ad 		tc = &turnstile_table[i];
    163  1.1.36.1  ad 		simple_lock_init(&tc->tc_lock);
    164  1.1.36.1  ad 		LIST_INIT(&tc->tc_chain);
    165  1.1.36.1  ad 	}
    166  1.1.36.1  ad 
    167  1.1.36.1  ad 	pool_init(&turnstile_pool, sizeof(struct turnstile), 0, 0, 0,
    168  1.1.36.1  ad 	    "tspool", &pool_allocator_nointr);
    169  1.1.36.1  ad 	pool_cache_init(&turnstile_cache, &turnstile_pool,
    170  1.1.36.1  ad 	    turnstile_ctor, NULL, NULL);
    171  1.1.36.1  ad }
    172  1.1.36.1  ad 
    173  1.1.36.1  ad /*
    174  1.1.36.1  ad  * turnstile_ctor:
    175  1.1.36.1  ad  *
    176  1.1.36.1  ad  *	Constructor for turnstiles.
    177  1.1.36.1  ad  */
    178  1.1.36.1  ad int
    179  1.1.36.1  ad turnstile_ctor(void *arg, void *obj, int flags)
    180  1.1.36.1  ad {
    181  1.1.36.1  ad 	struct turnstile *ts = obj;
    182  1.1.36.1  ad 
    183  1.1.36.1  ad 	memset(ts, 0, sizeof(*ts));
    184  1.1.36.1  ad 	return (0);
    185  1.1.36.1  ad }
    186  1.1.36.1  ad 
    187  1.1.36.1  ad static void
    188  1.1.36.1  ad turnstile_remque(struct turnstile *ts, struct lwp *l,
    189  1.1.36.1  ad 		 struct turnstile_sleepq *tsq)
    190  1.1.36.1  ad {
    191  1.1.36.1  ad 	struct lwp **q = &tsq->tsq_q.sq_head;
    192  1.1.36.1  ad 	struct turnstile *nts;
    193  1.1.36.1  ad 
    194  1.1.36.1  ad 	KASSERT(l->l_ts == ts);
    195  1.1.36.1  ad 
    196  1.1.36.1  ad 	/*
    197  1.1.36.1  ad 	 * This process is no longer using the active turnstile.
    198  1.1.36.1  ad 	 * Find an inactive one on the free list to give to it.
    199  1.1.36.1  ad 	 */
    200  1.1.36.1  ad 	if ((nts = ts->ts_free) != NULL) {
    201  1.1.36.1  ad 		KASSERT(TS_ALL_WAITERS(ts) > 1);
    202  1.1.36.1  ad 		l->l_ts = nts;
    203  1.1.36.1  ad 		ts->ts_free = nts->ts_free;
    204  1.1.36.1  ad 		nts->ts_free = NULL;
    205  1.1.36.1  ad 	} else {
    206  1.1.36.1  ad 		/*
    207  1.1.36.1  ad 		 * If the free list is empty, this is the last
    208  1.1.36.1  ad 		 * waiter.
    209  1.1.36.1  ad 		 */
    210  1.1.36.1  ad 		KASSERT(TS_ALL_WAITERS(ts) == 1);
    211  1.1.36.1  ad 		LIST_REMOVE(ts, ts_chain);
    212  1.1.36.1  ad 	}
    213  1.1.36.1  ad 
    214  1.1.36.1  ad 	tsq->tsq_waiters--;
    215  1.1.36.1  ad 
    216  1.1.36.1  ad 	*q = l->l_forw;
    217  1.1.36.1  ad 	if (tsq->tsq_q.sq_tailp == &l->l_forw)
    218  1.1.36.1  ad 		tsq->tsq_q.sq_tailp = q;
    219  1.1.36.1  ad 
    220  1.1.36.1  ad 	KASSERT(ts->ts_sleepq[TS_READER_Q].tsq_waiters != 0 ||
    221  1.1.36.1  ad 		ts->ts_sleepq[TS_READER_Q].tsq_q.sq_head == NULL);
    222  1.1.36.1  ad 	KASSERT(ts->ts_sleepq[TS_WRITER_Q].tsq_waiters != 0 ||
    223  1.1.36.1  ad 		ts->ts_sleepq[TS_WRITER_Q].tsq_q.sq_head == NULL);
    224  1.1.36.1  ad 
    225  1.1.36.1  ad 	KASSERT(ts->ts_sleepq[TS_READER_Q].tsq_waiters == 0 ||
    226  1.1.36.1  ad 		ts->ts_sleepq[TS_READER_Q].tsq_q.sq_head != NULL);
    227  1.1.36.1  ad 	KASSERT(ts->ts_sleepq[TS_WRITER_Q].tsq_waiters == 0 ||
    228  1.1.36.1  ad 		ts->ts_sleepq[TS_WRITER_Q].tsq_q.sq_head != NULL);
    229  1.1.36.1  ad }
    230  1.1.36.1  ad 
    231  1.1.36.1  ad /*
    232  1.1.36.1  ad  * turnstile_lookup:
    233  1.1.36.1  ad  *
    234  1.1.36.1  ad  *	Look up the turnstile for the specified lock object.  This
    235  1.1.36.1  ad  *	acquires and holds the turnstile chain lock (sleep queue
    236  1.1.36.1  ad  *	interlock).
    237  1.1.36.1  ad  */
    238  1.1.36.1  ad struct turnstile *
    239  1.1.36.1  ad turnstile_lookup(void *lp)
    240  1.1.36.1  ad {
    241  1.1.36.1  ad 	struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
    242  1.1.36.1  ad 	struct turnstile *ts;
    243  1.1.36.1  ad 
    244  1.1.36.1  ad 	TURNSTILE_CHAIN_LOCK(tc);
    245  1.1.36.1  ad 
    246  1.1.36.1  ad 	LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
    247  1.1.36.1  ad 		if (ts->ts_obj == lp)
    248  1.1.36.1  ad 			return (ts);
    249  1.1.36.1  ad 
    250  1.1.36.1  ad 	/*
    251  1.1.36.1  ad 	 * No turnstile yet for this lock.  No problem, turnstile_block()
    252  1.1.36.1  ad 	 * handles this by fetching the turnstile from the blocking thread.
    253  1.1.36.1  ad 	 */
    254  1.1.36.1  ad 	return (NULL);
    255  1.1.36.1  ad }
    256  1.1.36.1  ad 
    257  1.1.36.1  ad /*
    258  1.1.36.1  ad  * turnstile_exit:
    259  1.1.36.1  ad  *
    260  1.1.36.1  ad  *	Abort a turnstile operation.
    261  1.1.36.1  ad  */
    262  1.1.36.1  ad void
    263  1.1.36.1  ad turnstile_exit(void *lp)
    264  1.1.36.1  ad {
    265  1.1.36.1  ad 	struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
    266  1.1.36.1  ad 
    267  1.1.36.1  ad 	TURNSTILE_CHAIN_UNLOCK(tc);
    268  1.1.36.1  ad }
    269  1.1.36.1  ad 
    270  1.1.36.1  ad /*
    271  1.1.36.1  ad  * turnstile_block:
    272  1.1.36.1  ad  *
    273  1.1.36.1  ad  *	Block a thread on a lock object.
    274  1.1.36.1  ad  */
    275  1.1.36.1  ad int
    276  1.1.36.1  ad turnstile_block(struct turnstile *ts, int rw, int pri, void *lp,
    277  1.1.36.1  ad 		const char *wmesg)
    278  1.1.36.1  ad {
    279  1.1.36.1  ad 	struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
    280  1.1.36.1  ad 	struct lwp *l = curlwp;
    281  1.1.36.1  ad 	struct proc *p = l->l_proc;
    282  1.1.36.1  ad 	struct turnstile *ots;
    283  1.1.36.1  ad 	struct turnstile_sleepq *tsq;
    284  1.1.36.1  ad 	struct sadata_upcall *sau;
    285  1.1.36.1  ad 	struct slpque *qp;
    286  1.1.36.1  ad 	int s, hold_count;
    287  1.1.36.1  ad 
    288  1.1.36.1  ad 	KASSERT(l->l_ts != NULL);
    289  1.1.36.1  ad 	KASSERT(rw == TS_READER_Q || rw == TS_WRITER_Q);
    290  1.1.36.1  ad 
    291  1.1.36.1  ad 	if (ts == NULL) {
    292  1.1.36.1  ad 		/*
    293  1.1.36.1  ad 		 * We are the first thread to wait for this lock;
    294  1.1.36.1  ad 		 * lend our turnstile to it.
    295  1.1.36.1  ad 		 */
    296  1.1.36.1  ad 		ts = l->l_ts;
    297  1.1.36.1  ad 		KASSERT(TS_ALL_WAITERS(ts) == 0);
    298  1.1.36.1  ad 		KASSERT(ts->ts_sleepq[TS_READER_Q].tsq_q.sq_head == NULL &&
    299  1.1.36.1  ad 			ts->ts_sleepq[TS_WRITER_Q].tsq_q.sq_head == NULL);
    300  1.1.36.1  ad 		ts->ts_obj = lp;
    301  1.1.36.1  ad 		LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
    302  1.1.36.1  ad 	} else {
    303  1.1.36.1  ad 		/*
    304  1.1.36.1  ad 		 * Lock already has a turnstile.  Put our turnstile
    305  1.1.36.1  ad 		 * onto the free list, and reference the existing
    306  1.1.36.1  ad 		 * turnstile instead.
    307  1.1.36.1  ad 		 */
    308  1.1.36.1  ad 		ots = l->l_ts;
    309  1.1.36.1  ad 		ots->ts_free = ts->ts_free;
    310  1.1.36.1  ad 		ts->ts_free = ots;
    311  1.1.36.1  ad 		l->l_ts = ts;
    312  1.1.36.1  ad 	}
    313  1.1.36.1  ad 
    314  1.1.36.1  ad #ifdef DIAGNOSTIC
    315  1.1.36.1  ad 	if (l->l_stat != LSONPROC)
    316  1.1.36.1  ad 		panic("turnstile_block: l_stat %d != LSONPROC", l->l_stat);
    317  1.1.36.1  ad 	if (l->l_back != NULL)
    318  1.1.36.1  ad 		panic("turnstile_block: l_back != NULL");
    319  1.1.36.1  ad #endif
    320  1.1.36.1  ad 
    321  1.1.36.1  ad #ifdef KTRACE
    322  1.1.36.1  ad 	if (KTRPOINT(p, KTR_CSW))
    323  1.1.36.1  ad 		ktrcsw(l, 1, 0);
    324  1.1.36.1  ad #endif
    325  1.1.36.1  ad 
    326  1.1.36.1  ad 	/*
    327  1.1.36.1  ad 	 * XXX We need to allocate the sadata_upcall structure here,
    328  1.1.36.1  ad 	 * XXX since we can't sleep while waiting for memory inside
    329  1.1.36.1  ad 	 * XXX sa_upcall().  It would be nice if we could safely
    330  1.1.36.1  ad 	 * XXX allocate the sadata_upcall structure on the stack, here.
    331  1.1.36.1  ad 	 */
    332  1.1.36.1  ad 	if ((l->l_flag & L_SA) != 0)
    333  1.1.36.1  ad 		sau = sadata_upcall_alloc(0);
    334  1.1.36.1  ad 	else
    335  1.1.36.1  ad 		sau = NULL;
    336  1.1.36.1  ad 
    337  1.1.36.1  ad 	l->l_wchan = lp;
    338  1.1.36.1  ad 	l->l_wmesg = wmesg;
    339  1.1.36.1  ad 	l->l_slptime = 0;
    340  1.1.36.1  ad 	l->l_priority = pri & PRIMASK;
    341  1.1.36.1  ad 
    342  1.1.36.1  ad 	tsq = &ts->ts_sleepq[rw];
    343  1.1.36.1  ad 	qp = &tsq->tsq_q;
    344  1.1.36.1  ad 	tsq->tsq_waiters++;
    345  1.1.36.1  ad 	if (qp->sq_head == NULL)
    346  1.1.36.1  ad 		qp->sq_head = l;
    347  1.1.36.1  ad 	else
    348  1.1.36.1  ad 		*qp->sq_tailp = l;
    349  1.1.36.1  ad 	*(qp->sq_tailp = &l->l_forw) = NULL;
    350  1.1.36.1  ad 
    351  1.1.36.1  ad 	/*
    352  1.1.36.1  ad 	 * XXX We currently need to interlock with sched_lock.
    353  1.1.36.1  ad 	 * Note we're already at spllock(), which will block
    354  1.1.36.1  ad 	 * scheduler interrupts.
    355  1.1.36.1  ad 	 */
    356  1.1.36.1  ad 	_SCHED_LOCK;
    357  1.1.36.1  ad 
    358  1.1.36.1  ad 	/*
    359  1.1.36.1  ad 	 * Release the kernel_lock, as we are about to yield the CPU.
    360  1.1.36.1  ad 	 * The scheduler lock is still held until cpu_switch()
    361  1.1.36.1  ad 	 * selects a new process and removes it from the run queue.
    362  1.1.36.1  ad 	 */
    363  1.1.36.1  ad 	hold_count = KERNEL_LOCK_RELEASE_ALL();
    364  1.1.36.1  ad 
    365  1.1.36.1  ad 	l->l_stat = LSSLEEP;
    366  1.1.36.1  ad 	p->p_nrlwps--;
    367  1.1.36.1  ad 	p->p_stats->p_ru.ru_nvcsw++;
    368  1.1.36.1  ad 
    369  1.1.36.1  ad 	/*
    370  1.1.36.1  ad 	 * We can now release the turnstile chain interlock; the
    371  1.1.36.1  ad 	 * scheduler lock is held, so a thread can't get in to
    372  1.1.36.1  ad 	 * do a turnstile_wakeup() before we do the switch.
    373  1.1.36.1  ad 	 *
    374  1.1.36.1  ad 	 * Note: we need to remember our old spl which is currently
    375  1.1.36.1  ad 	 * stored in the turnstile chain, because we have to stay
    376  1.1.36.1  ad 	 * st spllock while the sched_lock is held.
    377  1.1.36.1  ad 	 */
    378  1.1.36.1  ad 	s = tc->tc_oldspl;
    379  1.1.36.1  ad 	simple_unlock(&tc->tc_lock);
    380  1.1.36.1  ad 
    381  1.1.36.1  ad 	if ((l->l_flag & L_SA) != 0)
    382  1.1.36.1  ad 		sa_switch(l, sau, SA_UPCALL_BLOCKED);
    383  1.1.36.1  ad 	else
    384  1.1.36.1  ad 		mi_switch(l, NULL);
    385  1.1.36.1  ad 
    386  1.1.36.1  ad 	SCHED_ASSERT_UNLOCKED();
    387  1.1.36.1  ad 	splx(s);
    388  1.1.36.1  ad 
    389  1.1.36.1  ad 	/*
    390  1.1.36.1  ad 	 * We are now back to the base spl level we were at when the
    391  1.1.36.1  ad 	 * caller called turnstile_lookup().
    392  1.1.36.1  ad 	 */
    393  1.1.36.1  ad 	KDASSERT(l->l_cpu != NULL);
    394  1.1.36.1  ad 	KDASSERT(l->l_cpu == curcpu());
    395  1.1.36.1  ad 	l->l_cpu->ci_schedstate.spc_curpriority = l->l_usrpri;
    396  1.1.36.1  ad 
    397  1.1.36.1  ad 	/*
    398  1.1.36.1  ad 	 * Reacquire the kernel_lock now.  We do this after we've
    399  1.1.36.1  ad 	 * released the scheduler lock to avoid deadlock, and before
    400  1.1.36.1  ad 	 * we reacquire other locks.
    401  1.1.36.1  ad 	 */
    402  1.1.36.1  ad 	KERNEL_LOCK_ACQUIRE_COUNT(hold_count);
    403  1.1.36.1  ad 
    404  1.1.36.1  ad 	KDASSERT((p->p_flag & (L_SINTR|L_TIMEOUT)) == 0);
    405  1.1.36.1  ad 
    406  1.1.36.1  ad #ifdef KTRACE
    407  1.1.36.1  ad 	if (KTRPOINT(p, KTR_CSW))
    408  1.1.36.1  ad 		ktrcsw(l, 0, 0);
    409  1.1.36.1  ad #endif
    410  1.1.36.1  ad 
    411  1.1.36.1  ad 	return (0);
    412  1.1.36.1  ad }
    413  1.1.36.1  ad 
    414  1.1.36.1  ad /*
    415  1.1.36.1  ad  * turnstile_wakeup:
    416  1.1.36.1  ad  *
    417  1.1.36.1  ad  *	Wake up the specified number of threads that are blocked
    418  1.1.36.1  ad  *	in a turnstile.
    419  1.1.36.1  ad  */
    420  1.1.36.1  ad void
    421  1.1.36.1  ad turnstile_wakeup(struct turnstile *ts, int rw, int count,
    422  1.1.36.1  ad 		 struct lwp *nextlwp)
    423  1.1.36.1  ad {
    424  1.1.36.1  ad 	struct turnstile_chain *tc = TURNSTILE_CHAIN(ts->ts_obj);
    425  1.1.36.1  ad 	struct turnstile_sleepq *tsq;
    426  1.1.36.1  ad 	struct lwp *l;
    427  1.1.36.1  ad 
    428  1.1.36.1  ad 	KASSERT(rw == TS_READER_Q || rw == TS_WRITER_Q);
    429  1.1.36.1  ad 	KASSERT(count > 0);
    430  1.1.36.1  ad 
    431  1.1.36.1  ad 	tsq = &ts->ts_sleepq[rw];
    432  1.1.36.1  ad 
    433  1.1.36.1  ad 	/* XXX We currently interlock with sched_lock. */
    434  1.1.36.1  ad 	_SCHED_LOCK;
    435  1.1.36.1  ad 
    436  1.1.36.1  ad 	if (nextlwp != NULL) {
    437  1.1.36.1  ad #if defined(DEBUG) || defined(LOCKDEBUG)
    438  1.1.36.1  ad 		for (l = tsq->tsq_q.sq_head; l != NULL;
    439  1.1.36.1  ad 		     l = l->l_forw) {
    440  1.1.36.1  ad 			if (l == nextlwp)
    441  1.1.36.1  ad 				break;
    442  1.1.36.1  ad 		}
    443  1.1.36.1  ad 		if (l == NULL)
    444  1.1.36.1  ad 			panic("turnstile_wakeup: nextlwp not on sleepq");
    445  1.1.36.1  ad #endif
    446  1.1.36.1  ad 		turnstile_remque(ts, nextlwp, tsq);
    447  1.1.36.1  ad 		nextlwp->l_wchan = NULL;
    448  1.1.36.1  ad 		if (nextlwp->l_stat == LSSLEEP)
    449  1.1.36.1  ad 			awaken(nextlwp);
    450  1.1.36.1  ad 	} else {
    451  1.1.36.1  ad 		while (count-- > 0) {
    452  1.1.36.1  ad 			l = tsq->tsq_q.sq_head;
    453  1.1.36.1  ad 			KASSERT(l != NULL);
    454  1.1.36.1  ad 			turnstile_remque(ts, l, tsq);
    455  1.1.36.1  ad 			l->l_wchan = NULL;
    456  1.1.36.1  ad 			if (l->l_stat == LSSLEEP)
    457  1.1.36.1  ad 				awaken(l);
    458  1.1.36.1  ad 		}
    459  1.1.36.1  ad 	}
    460  1.1.36.1  ad 
    461  1.1.36.1  ad 	_SCHED_UNLOCK;
    462  1.1.36.1  ad 
    463  1.1.36.1  ad 	TURNSTILE_CHAIN_UNLOCK(tc);
    464  1.1.36.1  ad }
    465  1.1.36.1  ad 
    466