Home | History | Annotate | Line # | Download | only in kern
kern_turnstile.c revision 1.1.2.2
      1  1.1.2.2  thorpej /*	$NetBSD: kern_turnstile.c,v 1.1.2.2 2002/03/10 20:05:31 thorpej Exp $	*/
      2  1.1.2.1  thorpej 
      3  1.1.2.1  thorpej /*-
      4  1.1.2.1  thorpej  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1.2.1  thorpej  * All rights reserved.
      6  1.1.2.1  thorpej  *
      7  1.1.2.1  thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.1  thorpej  * by Jason R. Thorpe.
      9  1.1.2.1  thorpej  *
     10  1.1.2.1  thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.1  thorpej  * modification, are permitted provided that the following conditions
     12  1.1.2.1  thorpej  * are met:
     13  1.1.2.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.1  thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.1.2.1  thorpej  *    must display the following acknowledgement:
     20  1.1.2.1  thorpej  *	This product includes software developed by the NetBSD
     21  1.1.2.1  thorpej  *	Foundation, Inc. and its contributors.
     22  1.1.2.1  thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.2.1  thorpej  *    contributors may be used to endorse or promote products derived
     24  1.1.2.1  thorpej  *    from this software without specific prior written permission.
     25  1.1.2.1  thorpej  *
     26  1.1.2.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.2.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.2.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.2.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.2.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.2.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.2.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.2.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.2.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.2.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.2.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.2.1  thorpej  */
     38  1.1.2.1  thorpej 
     39  1.1.2.1  thorpej /*
     40  1.1.2.1  thorpej  * Turnsiles are specialized sleep queues for use by locks.  Turnstiles
     41  1.1.2.1  thorpej  * are described in detail in:
     42  1.1.2.1  thorpej  *
     43  1.1.2.1  thorpej  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     44  1.1.2.1  thorpej  *	    Richard McDougall.
     45  1.1.2.1  thorpej  *
     46  1.1.2.1  thorpej  * Turnstiles are kept in a hash table.  Since there are likely to
     47  1.1.2.1  thorpej  * be many more lock objects than there are threads.  Since a thread
     48  1.1.2.1  thorpej  * can block on only one lock at a time, we only need one turnstile
     49  1.1.2.1  thorpej  * per thread, and so they are allocated at thread creation time.
     50  1.1.2.1  thorpej  *
     51  1.1.2.1  thorpej  * When a thread decides it needs to block on a lock, it looks up the
     52  1.1.2.1  thorpej  * active turnstile for that lock.  If no active turnstile exists, then
     53  1.1.2.1  thorpej  * the process lends its turnstile to the lock.  If there is already
     54  1.1.2.1  thorpej  * an active turnstile for the lock, the thread places its turnstile on
     55  1.1.2.1  thorpej  * a list of free turnstiles, and references the active one instead.
     56  1.1.2.1  thorpej  *
     57  1.1.2.1  thorpej  * The act of looking up the turnstile acquires an interlock on the sleep
     58  1.1.2.1  thorpej  * queue.  If a thread decides it doesn't need to block after all, then
     59  1.1.2.1  thorpej  * this interlock must be released by explicitly aborting the turnstile
     60  1.1.2.1  thorpej  * operation.
     61  1.1.2.1  thorpej  *
     62  1.1.2.1  thorpej  * When a thread is awakened, it needs to get its turnstile back.  If
     63  1.1.2.1  thorpej  * there are still other threads waiting in the active turnstile, the
     64  1.1.2.1  thorpej  * the thread grabs a free turnstile off the free list.  Otherwise, it
     65  1.1.2.1  thorpej  * can take back the active turnstile from the lock (thus deactivating
     66  1.1.2.1  thorpej  * the turnstile).
     67  1.1.2.1  thorpej  *
     68  1.1.2.1  thorpej  * Turnstiles are the place to do priority inheritence.  However, we do
     69  1.1.2.1  thorpej  * not currently implement that.
     70  1.1.2.1  thorpej  *
     71  1.1.2.1  thorpej  * We also do not differentiate between the reader and writer queues,
     72  1.1.2.1  thorpej  * although we currently provide for it in the API so that we can add
     73  1.1.2.1  thorpej  * support for it later.
     74  1.1.2.1  thorpej  *
     75  1.1.2.1  thorpej  * XXX We currently have to interlock with the sched_lock.  The locking
     76  1.1.2.1  thorpej  * order is:
     77  1.1.2.1  thorpej  *
     78  1.1.2.1  thorpej  *	turnstile chain -> sched_lock
     79  1.1.2.1  thorpej  */
     80  1.1.2.1  thorpej 
     81  1.1.2.1  thorpej #include <sys/cdefs.h>
     82  1.1.2.2  thorpej __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.1.2.2 2002/03/10 20:05:31 thorpej Exp $");
     83  1.1.2.1  thorpej 
     84  1.1.2.1  thorpej #include <sys/param.h>
     85  1.1.2.1  thorpej #include <sys/lock.h>
     86  1.1.2.1  thorpej #include <sys/pool.h>
     87  1.1.2.1  thorpej #include <sys/proc.h>
     88  1.1.2.1  thorpej #include <sys/resourcevar.h>
     89  1.1.2.1  thorpej #include <sys/sched.h>
     90  1.1.2.1  thorpej #include <sys/systm.h>
     91  1.1.2.1  thorpej 
     92  1.1.2.1  thorpej /*
     93  1.1.2.1  thorpej  * Turnstile hash -- shift the lock object to eliminate the zero bits
     94  1.1.2.1  thorpej  * of the address, and mask it off with the turnstile table's size.
     95  1.1.2.1  thorpej  */
     96  1.1.2.1  thorpej #if LONG_BIT == 64
     97  1.1.2.1  thorpej #define	TURNSTILE_HASH_SHIFT	3
     98  1.1.2.1  thorpej #elif LONG_BIT == 32
     99  1.1.2.1  thorpej #define	TURNSTILE_HASH_SHIFT	2
    100  1.1.2.1  thorpej #else
    101  1.1.2.1  thorpej #error "Don't know how big your pointers are."
    102  1.1.2.1  thorpej #endif
    103  1.1.2.1  thorpej 
    104  1.1.2.1  thorpej #define	TURNSTILE_HASH_SIZE	64	/* XXXJRT tune */
    105  1.1.2.1  thorpej #define	TURNSTILE_HASH_MASK	(TURNSTILE_HASH_SIZE - 1)
    106  1.1.2.1  thorpej 
    107  1.1.2.1  thorpej #define	TURNSTILE_HASH(obj)						\
    108  1.1.2.1  thorpej 	((((u_long)(obj)) >> TURNSTILE_HASH_SHIFT) & TURNSTILE_HASH_MASK)
    109  1.1.2.1  thorpej 
    110  1.1.2.1  thorpej struct turnstile_chain {
    111  1.1.2.1  thorpej 	__cpu_simple_lock_t tc_lock;	/* lock on hash chain */
    112  1.1.2.1  thorpej 	int		    tc_oldspl;	/* saved spl of lock holder */
    113  1.1.2.1  thorpej 	LIST_HEAD(, turnstile) tc_chain;/* turnstile chain */
    114  1.1.2.1  thorpej } turnstile_table[TURNSTILE_HASH_SIZE];
    115  1.1.2.1  thorpej 
    116  1.1.2.1  thorpej #define	TURNSTILE_CHAIN(obj)						\
    117  1.1.2.1  thorpej 	&turnstile_table[TURNSTILE_HASH(obj)]
    118  1.1.2.1  thorpej 
    119  1.1.2.1  thorpej #define	TURNSTILE_CHAIN_LOCK(tc)					\
    120  1.1.2.1  thorpej do {									\
    121  1.1.2.1  thorpej 	int _s_ = splsched();						\
    122  1.1.2.1  thorpej 	__cpu_simple_lock(&(tc)->tc_lock);				\
    123  1.1.2.1  thorpej 	(tc)->tc_oldspl = _s_;						\
    124  1.1.2.1  thorpej } while (/*CONSTCOND*/0)
    125  1.1.2.1  thorpej 
    126  1.1.2.1  thorpej #define	TURNSTILE_CHAIN_UNLOCK(tc)					\
    127  1.1.2.1  thorpej do {									\
    128  1.1.2.2  thorpej 	int _s_ = (tc)->tc_oldspl;					\
    129  1.1.2.1  thorpej 	__cpu_simple_unlock(&(tc)->tc_lock);				\
    130  1.1.2.2  thorpej 	splx(_s_);							\
    131  1.1.2.1  thorpej } while (/*CONSTCOND*/0)
    132  1.1.2.1  thorpej 
    133  1.1.2.1  thorpej static const char turnstile_wmesg[] = "tstile";
    134  1.1.2.1  thorpej 
    135  1.1.2.1  thorpej struct pool turnstile_pool;
    136  1.1.2.1  thorpej 
    137  1.1.2.1  thorpej /*
    138  1.1.2.1  thorpej  * turnstile_init:
    139  1.1.2.1  thorpej  *
    140  1.1.2.1  thorpej  *	Initialize the turnstile mechanism.
    141  1.1.2.1  thorpej  */
    142  1.1.2.1  thorpej void
    143  1.1.2.1  thorpej turnstile_init(void)
    144  1.1.2.1  thorpej {
    145  1.1.2.1  thorpej 	struct turnstile_chain *tc;
    146  1.1.2.1  thorpej 	int i;
    147  1.1.2.1  thorpej 
    148  1.1.2.1  thorpej 	for (i = 0; i < TURNSTILE_HASH_SIZE; i++) {
    149  1.1.2.1  thorpej 		tc = &turnstile_table[i];
    150  1.1.2.1  thorpej 		__cpu_simple_lock_init(&tc->tc_lock);
    151  1.1.2.1  thorpej 		LIST_INIT(&tc->tc_chain);
    152  1.1.2.1  thorpej 	}
    153  1.1.2.1  thorpej 
    154  1.1.2.1  thorpej 	pool_init(&turnstile_pool, sizeof(struct turnstile), 0, 0, 0,
    155  1.1.2.1  thorpej 	    "tspool", &pool_allocator_nointr);
    156  1.1.2.1  thorpej }
    157  1.1.2.1  thorpej 
    158  1.1.2.1  thorpej static void
    159  1.1.2.1  thorpej turnstile_remque(struct turnstile *ts, struct proc *p, struct slpque *qp)
    160  1.1.2.1  thorpej {
    161  1.1.2.1  thorpej 	struct proc **q = &qp->sq_head;
    162  1.1.2.1  thorpej 	struct turnstile *nts;
    163  1.1.2.1  thorpej 
    164  1.1.2.1  thorpej 	KASSERT(p->p_ts == ts);
    165  1.1.2.1  thorpej 
    166  1.1.2.1  thorpej 	/*
    167  1.1.2.1  thorpej 	 * This process is no longer using the active turnstile.
    168  1.1.2.1  thorpej 	 * Find an inactive one on the free list to give to it.
    169  1.1.2.1  thorpej 	 */
    170  1.1.2.1  thorpej 	if ((nts == ts->ts_free) != NULL) {
    171  1.1.2.1  thorpej 		KASSERT(ts->ts_waiters > 1);
    172  1.1.2.1  thorpej 		p->p_ts = nts;
    173  1.1.2.1  thorpej 		ts->ts_free = nts->ts_free;
    174  1.1.2.1  thorpej 		nts->ts_free = NULL;
    175  1.1.2.1  thorpej 	} else {
    176  1.1.2.1  thorpej 		/*
    177  1.1.2.1  thorpej 		 * If the free list is empty, this is the last
    178  1.1.2.1  thorpej 		 * waiter.
    179  1.1.2.1  thorpej 		 */
    180  1.1.2.1  thorpej 		KASSERT(ts->ts_waiters == 1);
    181  1.1.2.1  thorpej 		LIST_REMOVE(ts, ts_chain);
    182  1.1.2.1  thorpej 	}
    183  1.1.2.1  thorpej 
    184  1.1.2.1  thorpej 	ts->ts_waiters--;
    185  1.1.2.1  thorpej 
    186  1.1.2.1  thorpej 	*q = p->p_forw;
    187  1.1.2.1  thorpej 	if (qp->sq_tailp == &p->p_forw)
    188  1.1.2.1  thorpej 		qp->sq_tailp = q;
    189  1.1.2.1  thorpej }
    190  1.1.2.1  thorpej 
    191  1.1.2.1  thorpej /*
    192  1.1.2.1  thorpej  * turnstile_lookup:
    193  1.1.2.1  thorpej  *
    194  1.1.2.1  thorpej  *	Look up the turnstile for the specified lock object.  This
    195  1.1.2.1  thorpej  *	acquires and holds the turnstile chain lock (sleep queue
    196  1.1.2.1  thorpej  *	interlock).
    197  1.1.2.1  thorpej  */
    198  1.1.2.1  thorpej struct turnstile *
    199  1.1.2.1  thorpej turnstile_lookup(void *lp)
    200  1.1.2.1  thorpej {
    201  1.1.2.1  thorpej 	struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
    202  1.1.2.1  thorpej 	struct turnstile *ts;
    203  1.1.2.1  thorpej 
    204  1.1.2.1  thorpej 	TURNSTILE_CHAIN_LOCK(tc);
    205  1.1.2.1  thorpej 
    206  1.1.2.1  thorpej 	LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
    207  1.1.2.1  thorpej 		if (ts->ts_obj == lp)
    208  1.1.2.1  thorpej 			return (ts);
    209  1.1.2.1  thorpej 
    210  1.1.2.1  thorpej 	/*
    211  1.1.2.1  thorpej 	 * No turnstile yet for this lock.  No problem, turnstile_block()
    212  1.1.2.1  thorpej 	 * handle this by fetching the turnstile from the blocking thread.
    213  1.1.2.1  thorpej 	 */
    214  1.1.2.1  thorpej 	return (NULL);
    215  1.1.2.1  thorpej }
    216  1.1.2.1  thorpej 
    217  1.1.2.1  thorpej /*
    218  1.1.2.1  thorpej  * turnstile_exit:
    219  1.1.2.1  thorpej  *
    220  1.1.2.1  thorpej  *	Abort a turnstile operation.
    221  1.1.2.1  thorpej  */
    222  1.1.2.1  thorpej void
    223  1.1.2.1  thorpej turnstile_exit(void *lp)
    224  1.1.2.1  thorpej {
    225  1.1.2.1  thorpej 	struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
    226  1.1.2.1  thorpej 
    227  1.1.2.1  thorpej 	TURNSTILE_CHAIN_UNLOCK(tc);
    228  1.1.2.1  thorpej }
    229  1.1.2.1  thorpej 
    230  1.1.2.1  thorpej /*
    231  1.1.2.1  thorpej  * turnstile_block:
    232  1.1.2.1  thorpej  *
    233  1.1.2.1  thorpej  *	Block a thread on a lock object.
    234  1.1.2.1  thorpej  */
    235  1.1.2.1  thorpej int
    236  1.1.2.1  thorpej turnstile_block(struct turnstile *ts, int rw, void *lp)
    237  1.1.2.1  thorpej {
    238  1.1.2.1  thorpej 	struct turnstile_chain *tc = TURNSTILE_CHAIN(lp);
    239  1.1.2.1  thorpej 	struct proc *p = curproc;
    240  1.1.2.1  thorpej 	struct turnstile *ots;
    241  1.1.2.1  thorpej 	struct slpque *qp;
    242  1.1.2.1  thorpej 	int s;
    243  1.1.2.1  thorpej 
    244  1.1.2.1  thorpej 	KASSERT(p->p_ts != NULL);
    245  1.1.2.1  thorpej 
    246  1.1.2.1  thorpej 	if (ts == NULL) {
    247  1.1.2.1  thorpej 		/*
    248  1.1.2.1  thorpej 		 * We are the first thread to wait for this lock;
    249  1.1.2.1  thorpej 		 * lend our turnstile to it.
    250  1.1.2.1  thorpej 		 */
    251  1.1.2.1  thorpej 		ts = p->p_ts;
    252  1.1.2.1  thorpej 		KASSERT(ts->ts_waiters == 0);
    253  1.1.2.1  thorpej 		KASSERT(ts->ts_sleepq.sq_head == NULL);
    254  1.1.2.1  thorpej 		ts->ts_obj = lp;
    255  1.1.2.1  thorpej 		LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
    256  1.1.2.1  thorpej 	} else {
    257  1.1.2.1  thorpej 		/*
    258  1.1.2.1  thorpej 		 * Lock already has a turnstile.  Put our turnstile
    259  1.1.2.1  thorpej 		 * onto the free list, and reference the existing
    260  1.1.2.1  thorpej 		 * turnstile instead.
    261  1.1.2.1  thorpej 		 */
    262  1.1.2.1  thorpej 		ots = p->p_ts;
    263  1.1.2.1  thorpej 		ots->ts_free = ts->ts_free;
    264  1.1.2.1  thorpej 		ts->ts_free = ots;
    265  1.1.2.1  thorpej 		p->p_ts = ts;
    266  1.1.2.1  thorpej 	}
    267  1.1.2.1  thorpej 
    268  1.1.2.1  thorpej #ifdef DIAGNOSTIC
    269  1.1.2.1  thorpej 	if (p->p_stat != SONPROC)
    270  1.1.2.1  thorpej 		panic("turnstile_block: p_stat %d != SONPROC", p->p_stat);
    271  1.1.2.1  thorpej 	if (p->p_back != NULL)
    272  1.1.2.1  thorpej 		panic("turnstile_block: p_back != NULL");
    273  1.1.2.1  thorpej #endif
    274  1.1.2.1  thorpej 
    275  1.1.2.1  thorpej #ifdef KTRACE
    276  1.1.2.1  thorpej 	if (KTRPOINT(p, KTR_CSW))
    277  1.1.2.1  thorpej 		ktrcsw(p, 1, 0);
    278  1.1.2.1  thorpej #endif
    279  1.1.2.1  thorpej 
    280  1.1.2.1  thorpej 	/* XXXJRT PCATCH? */
    281  1.1.2.1  thorpej 
    282  1.1.2.1  thorpej 	p->p_wchan = lp;
    283  1.1.2.1  thorpej 	p->p_wmesg = turnstile_wmesg;
    284  1.1.2.1  thorpej 	p->p_slptime = 0;
    285  1.1.2.1  thorpej 	/* p->p_priority = XXXJRT */
    286  1.1.2.1  thorpej 
    287  1.1.2.1  thorpej 	ts->ts_waiters++;
    288  1.1.2.1  thorpej 
    289  1.1.2.1  thorpej 	qp = &ts->ts_sleepq;
    290  1.1.2.1  thorpej 	if (qp->sq_head == NULL)
    291  1.1.2.1  thorpej 		qp->sq_head = p;
    292  1.1.2.1  thorpej 	else
    293  1.1.2.1  thorpej 		*qp->sq_tailp = p;
    294  1.1.2.1  thorpej 	*(qp->sq_tailp = &p->p_forw) = NULL;
    295  1.1.2.1  thorpej 
    296  1.1.2.1  thorpej 	p->p_stat = SSLEEP;
    297  1.1.2.1  thorpej 	p->p_stats->p_ru.ru_nvcsw++;
    298  1.1.2.1  thorpej 
    299  1.1.2.1  thorpej 	/*
    300  1.1.2.1  thorpej 	 * XXX We currently need to interlock with sched_lock.
    301  1.1.2.1  thorpej 	 * Note we're already at splsched().
    302  1.1.2.1  thorpej 	 */
    303  1.1.2.1  thorpej 	_SCHED_LOCK;
    304  1.1.2.1  thorpej 
    305  1.1.2.1  thorpej 	/*
    306  1.1.2.1  thorpej 	 * We can now release the turnstile chain interlock; the
    307  1.1.2.1  thorpej 	 * scheduler lock is held, so a thread can't get in to
    308  1.1.2.1  thorpej 	 * do a turnstile_wakeup() before we do the switch.
    309  1.1.2.1  thorpej 	 *
    310  1.1.2.1  thorpej 	 * Note: we need to remember our old spl which is currently
    311  1.1.2.1  thorpej 	 * stored in the turnstile chain, because we have to stay
    312  1.1.2.1  thorpej 	 * st splsched while the sched_lock is held.
    313  1.1.2.1  thorpej 	 */
    314  1.1.2.1  thorpej 	s = tc->tc_oldspl;
    315  1.1.2.1  thorpej 	__cpu_simple_unlock(&tc->tc_lock);
    316  1.1.2.1  thorpej 
    317  1.1.2.1  thorpej 	mi_switch(p);
    318  1.1.2.1  thorpej 
    319  1.1.2.1  thorpej 	SCHED_ASSERT_UNLOCKED();
    320  1.1.2.1  thorpej 	splx(s);
    321  1.1.2.1  thorpej 
    322  1.1.2.1  thorpej 	/*
    323  1.1.2.1  thorpej 	 * We are now back to the base spl level we were at when the
    324  1.1.2.1  thorpej 	 * caller called turnstile_lookup().
    325  1.1.2.1  thorpej 	 */
    326  1.1.2.1  thorpej 
    327  1.1.2.1  thorpej 	KDASSERT(p->p_cpu != NULL);
    328  1.1.2.1  thorpej 	KDASSERT(p->p_cpu == curcpu());
    329  1.1.2.1  thorpej 	p->p_cpu->ci_schedstate.spc_curpriority = p->p_usrpri;
    330  1.1.2.1  thorpej 
    331  1.1.2.1  thorpej 	KDASSERT((p->p_flag & (P_SINTR|P_TIMEOUT)) == 0);
    332  1.1.2.1  thorpej 
    333  1.1.2.1  thorpej #ifdef KTRACE
    334  1.1.2.1  thorpej 	if (KTRPOINT(p, KTR_CSW))
    335  1.1.2.1  thorpej 		ktrcsw(p, 0, 0);
    336  1.1.2.1  thorpej #endif
    337  1.1.2.1  thorpej 
    338  1.1.2.1  thorpej 	return (0);
    339  1.1.2.1  thorpej }
    340  1.1.2.1  thorpej 
    341  1.1.2.1  thorpej /*
    342  1.1.2.1  thorpej  * turnstile_wakeup:
    343  1.1.2.1  thorpej  *
    344  1.1.2.1  thorpej  *	Wake up the specified number of threads that are blocked
    345  1.1.2.1  thorpej  *	in a turnstile.
    346  1.1.2.1  thorpej  */
    347  1.1.2.1  thorpej void
    348  1.1.2.1  thorpej turnstile_wakeup(struct turnstile *ts, int rw, int count)
    349  1.1.2.1  thorpej {
    350  1.1.2.1  thorpej 	struct turnstile_chain *tc = TURNSTILE_CHAIN(ts->ts_obj);
    351  1.1.2.1  thorpej 	struct slpque *qp = &ts->ts_sleepq;
    352  1.1.2.1  thorpej 	struct proc *p;
    353  1.1.2.1  thorpej 
    354  1.1.2.1  thorpej 	/* XXX We currently interlock with sched_lock. */
    355  1.1.2.1  thorpej 	_SCHED_LOCK;
    356  1.1.2.1  thorpej 
    357  1.1.2.1  thorpej 	while (count-- > 0) {
    358  1.1.2.1  thorpej 		p = qp->sq_head;
    359  1.1.2.1  thorpej 
    360  1.1.2.1  thorpej 		KASSERT(p != NULL);
    361  1.1.2.1  thorpej 
    362  1.1.2.1  thorpej 		turnstile_remque(ts, p, qp);
    363  1.1.2.1  thorpej 
    364  1.1.2.1  thorpej 		p->p_wchan = NULL;
    365  1.1.2.1  thorpej 
    366  1.1.2.1  thorpej 		if (p->p_stat == SSLEEP)
    367  1.1.2.1  thorpej 			awaken(p);
    368  1.1.2.1  thorpej 	}
    369  1.1.2.1  thorpej 
    370  1.1.2.1  thorpej 	_SCHED_UNLOCK;
    371  1.1.2.1  thorpej 
    372  1.1.2.1  thorpej 	TURNSTILE_CHAIN_UNLOCK(tc);
    373  1.1.2.1  thorpej }
    374