Home | History | Annotate | Line # | Download | only in kern
kern_turnstile.c revision 1.23.12.1
      1  1.23.12.1     matt /*	$NetBSD: kern_turnstile.c,v 1.23.12.1 2010/04/21 00:28:17 matt Exp $	*/
      2        1.2       ad 
      3        1.2       ad /*-
      4        1.2       ad  * Copyright (c) 2002, 2006, 2007 The NetBSD Foundation, Inc.
      5        1.2       ad  * All rights reserved.
      6        1.2       ad  *
      7        1.2       ad  * This code is derived from software contributed to The NetBSD Foundation
      8        1.2       ad  * by Jason R. Thorpe and Andrew Doran.
      9        1.2       ad  *
     10        1.2       ad  * Redistribution and use in source and binary forms, with or without
     11        1.2       ad  * modification, are permitted provided that the following conditions
     12        1.2       ad  * are met:
     13        1.2       ad  * 1. Redistributions of source code must retain the above copyright
     14        1.2       ad  *    notice, this list of conditions and the following disclaimer.
     15        1.2       ad  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.2       ad  *    notice, this list of conditions and the following disclaimer in the
     17        1.2       ad  *    documentation and/or other materials provided with the distribution.
     18        1.2       ad  *
     19        1.2       ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.2       ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.2       ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.2       ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.2       ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.2       ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.2       ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.2       ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.2       ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.2       ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.2       ad  * POSSIBILITY OF SUCH DAMAGE.
     30        1.2       ad  */
     31        1.2       ad 
     32        1.2       ad /*
     33        1.2       ad  * Turnstiles are described in detail in:
     34        1.2       ad  *
     35        1.2       ad  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     36        1.2       ad  *	    Richard McDougall.
     37        1.2       ad  *
     38        1.2       ad  * Turnstiles are kept in a hash table.  There are likely to be many more
     39        1.2       ad  * synchronisation objects than there are threads.  Since a thread can block
     40        1.2       ad  * on only one lock at a time, we only need one turnstile per thread, and
     41        1.2       ad  * so they are allocated at thread creation time.
     42        1.2       ad  *
     43        1.2       ad  * When a thread decides it needs to block on a lock, it looks up the
     44        1.2       ad  * active turnstile for that lock.  If no active turnstile exists, then
     45        1.2       ad  * the process lends its turnstile to the lock.  If there is already an
     46        1.2       ad  * active turnstile for the lock, the thread places its turnstile on a
     47        1.2       ad  * list of free turnstiles, and references the active one instead.
     48        1.2       ad  *
     49        1.2       ad  * The act of looking up the turnstile acquires an interlock on the sleep
     50        1.2       ad  * queue.  If a thread decides it doesn't need to block after all, then this
     51        1.2       ad  * interlock must be released by explicitly aborting the turnstile
     52        1.2       ad  * operation.
     53        1.2       ad  *
     54        1.2       ad  * When a thread is awakened, it needs to get its turnstile back.  If there
     55       1.18      alc  * are still other threads waiting in the active turnstile, the thread
     56        1.2       ad  * grabs a free turnstile off the free list.  Otherwise, it can take back
     57        1.2       ad  * the active turnstile from the lock (thus deactivating the turnstile).
     58        1.2       ad  *
     59        1.4     yamt  * Turnstiles are the place to do priority inheritence.
     60        1.2       ad  */
     61        1.2       ad 
     62        1.2       ad #include <sys/cdefs.h>
     63  1.23.12.1     matt __KERNEL_RCSID(0, "$NetBSD: kern_turnstile.c,v 1.23.12.1 2010/04/21 00:28:17 matt Exp $");
     64        1.2       ad 
     65        1.2       ad #include <sys/param.h>
     66        1.4     yamt #include <sys/lockdebug.h>
     67        1.2       ad #include <sys/pool.h>
     68        1.2       ad #include <sys/proc.h>
     69        1.2       ad #include <sys/sleepq.h>
     70        1.2       ad #include <sys/systm.h>
     71        1.2       ad 
     72        1.3       ad #include <uvm/uvm_extern.h>
     73        1.3       ad 
     74        1.2       ad #define	TS_HASH_SIZE	64
     75        1.2       ad #define	TS_HASH_MASK	(TS_HASH_SIZE - 1)
     76        1.2       ad #define	TS_HASH(obj)	(((uintptr_t)(obj) >> 3) & TS_HASH_MASK)
     77        1.2       ad 
     78        1.2       ad tschain_t	turnstile_tab[TS_HASH_SIZE];
     79       1.12       ad pool_cache_t	turnstile_cache;
     80        1.2       ad 
     81        1.2       ad int	turnstile_ctor(void *, void *, int);
     82        1.2       ad 
     83        1.2       ad extern turnstile_t turnstile0;
     84        1.2       ad 
     85        1.2       ad /*
     86        1.2       ad  * turnstile_init:
     87        1.2       ad  *
     88        1.2       ad  *	Initialize the turnstile mechanism.
     89        1.2       ad  */
     90        1.2       ad void
     91        1.2       ad turnstile_init(void)
     92        1.2       ad {
     93        1.2       ad 	tschain_t *tc;
     94        1.2       ad 	int i;
     95        1.2       ad 
     96        1.2       ad 	for (i = 0; i < TS_HASH_SIZE; i++) {
     97        1.2       ad 		tc = &turnstile_tab[i];
     98        1.2       ad 		LIST_INIT(&tc->tc_chain);
     99       1.13       ad 		mutex_init(&tc->tc_mutex, MUTEX_DEFAULT, IPL_SCHED);
    100        1.2       ad 	}
    101        1.2       ad 
    102       1.12       ad 	turnstile_cache = pool_cache_init(sizeof(turnstile_t), 0, 0, 0,
    103       1.12       ad 	    "tstilepl", NULL, IPL_NONE, turnstile_ctor, NULL, NULL);
    104       1.12       ad 	KASSERT(turnstile_cache != NULL);
    105        1.2       ad 
    106        1.2       ad 	(void)turnstile_ctor(NULL, &turnstile0, 0);
    107        1.2       ad }
    108        1.2       ad 
    109        1.2       ad /*
    110        1.2       ad  * turnstile_ctor:
    111        1.2       ad  *
    112        1.2       ad  *	Constructor for turnstiles.
    113        1.2       ad  */
    114        1.2       ad int
    115        1.2       ad turnstile_ctor(void *arg, void *obj, int flags)
    116        1.2       ad {
    117        1.2       ad 	turnstile_t *ts = obj;
    118        1.2       ad 
    119        1.2       ad 	memset(ts, 0, sizeof(*ts));
    120       1.21       ad 	sleepq_init(&ts->ts_sleepq[TS_READER_Q]);
    121       1.21       ad 	sleepq_init(&ts->ts_sleepq[TS_WRITER_Q]);
    122        1.2       ad 	return (0);
    123        1.2       ad }
    124        1.2       ad 
    125        1.2       ad /*
    126        1.2       ad  * turnstile_remove:
    127        1.2       ad  *
    128        1.2       ad  *	Remove an LWP from a turnstile sleep queue and wake it.
    129        1.2       ad  */
    130        1.9     yamt static inline void
    131       1.21       ad turnstile_remove(turnstile_t *ts, lwp_t *l, int q)
    132        1.2       ad {
    133        1.2       ad 	turnstile_t *nts;
    134        1.2       ad 
    135        1.2       ad 	KASSERT(l->l_ts == ts);
    136        1.2       ad 
    137        1.2       ad 	/*
    138        1.2       ad 	 * This process is no longer using the active turnstile.
    139        1.2       ad 	 * Find an inactive one on the free list to give to it.
    140        1.2       ad 	 */
    141        1.2       ad 	if ((nts = ts->ts_free) != NULL) {
    142        1.2       ad 		KASSERT(TS_ALL_WAITERS(ts) > 1);
    143        1.2       ad 		l->l_ts = nts;
    144        1.2       ad 		ts->ts_free = nts->ts_free;
    145        1.2       ad 		nts->ts_free = NULL;
    146        1.2       ad 	} else {
    147        1.2       ad 		/*
    148        1.2       ad 		 * If the free list is empty, this is the last
    149        1.2       ad 		 * waiter.
    150        1.2       ad 		 */
    151        1.2       ad 		KASSERT(TS_ALL_WAITERS(ts) == 1);
    152        1.2       ad 		LIST_REMOVE(ts, ts_chain);
    153        1.2       ad 	}
    154        1.2       ad 
    155       1.21       ad 	ts->ts_waiters[q]--;
    156       1.21       ad 	(void)sleepq_remove(&ts->ts_sleepq[q], l);
    157        1.2       ad }
    158        1.2       ad 
    159        1.2       ad /*
    160        1.2       ad  * turnstile_lookup:
    161        1.2       ad  *
    162        1.2       ad  *	Look up the turnstile for the specified lock.  This acquires and
    163        1.2       ad  *	holds the turnstile chain lock (sleep queue interlock).
    164        1.2       ad  */
    165        1.2       ad turnstile_t *
    166        1.2       ad turnstile_lookup(wchan_t obj)
    167        1.2       ad {
    168        1.2       ad 	turnstile_t *ts;
    169        1.2       ad 	tschain_t *tc;
    170        1.2       ad 
    171        1.2       ad 	tc = &turnstile_tab[TS_HASH(obj)];
    172        1.9     yamt 	mutex_spin_enter(&tc->tc_mutex);
    173        1.2       ad 
    174        1.2       ad 	LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
    175        1.2       ad 		if (ts->ts_obj == obj)
    176        1.2       ad 			return (ts);
    177        1.2       ad 
    178        1.2       ad 	/*
    179        1.2       ad 	 * No turnstile yet for this lock.  No problem, turnstile_block()
    180        1.2       ad 	 * handles this by fetching the turnstile from the blocking thread.
    181        1.2       ad 	 */
    182        1.2       ad 	return (NULL);
    183        1.2       ad }
    184        1.2       ad 
    185        1.2       ad /*
    186        1.2       ad  * turnstile_exit:
    187        1.2       ad  *
    188        1.2       ad  *	Abort a turnstile operation.
    189        1.2       ad  */
    190        1.2       ad void
    191        1.2       ad turnstile_exit(wchan_t obj)
    192        1.2       ad {
    193        1.2       ad 	tschain_t *tc;
    194        1.2       ad 
    195        1.2       ad 	tc = &turnstile_tab[TS_HASH(obj)];
    196        1.9     yamt 	mutex_spin_exit(&tc->tc_mutex);
    197        1.2       ad }
    198        1.2       ad 
    199        1.2       ad /*
    200        1.2       ad  * turnstile_block:
    201        1.2       ad  *
    202        1.2       ad  *	 Enter an object into the turnstile chain and prepare the current
    203        1.2       ad  *	 LWP for sleep.
    204        1.2       ad  */
    205        1.2       ad void
    206        1.4     yamt turnstile_block(turnstile_t *ts, int q, wchan_t obj, syncobj_t *sobj)
    207        1.2       ad {
    208       1.10       ad 	lwp_t *l;
    209       1.10       ad 	lwp_t *cur; /* cached curlwp */
    210       1.10       ad 	lwp_t *owner;
    211        1.2       ad 	turnstile_t *ots;
    212        1.2       ad 	tschain_t *tc;
    213        1.2       ad 	sleepq_t *sq;
    214       1.17       ad 	pri_t prio, obase;
    215        1.2       ad 
    216        1.2       ad 	tc = &turnstile_tab[TS_HASH(obj)];
    217        1.4     yamt 	l = cur = curlwp;
    218        1.2       ad 
    219        1.2       ad 	KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
    220        1.9     yamt 	KASSERT(mutex_owned(&tc->tc_mutex));
    221        1.2       ad 	KASSERT(l != NULL && l->l_ts != NULL);
    222        1.2       ad 
    223        1.2       ad 	if (ts == NULL) {
    224        1.2       ad 		/*
    225        1.2       ad 		 * We are the first thread to wait for this object;
    226        1.2       ad 		 * lend our turnstile to it.
    227        1.2       ad 		 */
    228        1.2       ad 		ts = l->l_ts;
    229        1.2       ad 		KASSERT(TS_ALL_WAITERS(ts) == 0);
    230       1.21       ad 		KASSERT(TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q]) &&
    231       1.21       ad 			TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q]));
    232        1.2       ad 		ts->ts_obj = obj;
    233        1.4     yamt 		ts->ts_inheritor = NULL;
    234        1.2       ad 		LIST_INSERT_HEAD(&tc->tc_chain, ts, ts_chain);
    235        1.2       ad 	} else {
    236        1.2       ad 		/*
    237        1.2       ad 		 * Object already has a turnstile.  Put our turnstile
    238        1.2       ad 		 * onto the free list, and reference the existing
    239        1.2       ad 		 * turnstile instead.
    240        1.2       ad 		 */
    241        1.2       ad 		ots = l->l_ts;
    242       1.23  thorpej 		KASSERT(ots->ts_free == NULL);
    243        1.2       ad 		ots->ts_free = ts->ts_free;
    244        1.2       ad 		ts->ts_free = ots;
    245        1.2       ad 		l->l_ts = ts;
    246        1.2       ad 
    247        1.4     yamt 		KASSERT(ts->ts_obj == obj);
    248        1.2       ad 		KASSERT(TS_ALL_WAITERS(ts) != 0);
    249       1.21       ad 		KASSERT(!TAILQ_EMPTY(&ts->ts_sleepq[TS_READER_Q]) ||
    250       1.21       ad 			!TAILQ_EMPTY(&ts->ts_sleepq[TS_WRITER_Q]));
    251        1.2       ad 	}
    252        1.2       ad 
    253        1.2       ad 	sq = &ts->ts_sleepq[q];
    254       1.21       ad 	ts->ts_waiters[q]++;
    255       1.21       ad 	sleepq_enter(sq, l, &tc->tc_mutex);
    256  1.23.12.1     matt 	/* now tc->tc_mutex is also cur->l_mutex and l->l_mutex */
    257        1.9     yamt 	LOCKDEBUG_BARRIER(&tc->tc_mutex, 1);
    258       1.11       ad 	l->l_kpriority = true;
    259       1.17       ad 	obase = l->l_kpribase;
    260       1.17       ad 	if (obase < PRI_KTHREAD)
    261       1.17       ad 		l->l_kpribase = PRI_KTHREAD;
    262       1.11       ad 	sleepq_enqueue(sq, obj, "tstile", sobj);
    263        1.4     yamt 
    264        1.4     yamt 	/*
    265       1.19       ad 	 * Disable preemption across this entire block, as we may drop
    266       1.19       ad 	 * scheduler locks (allowing preemption), and would prefer not
    267       1.19       ad 	 * to be interrupted while in a state of flux.
    268       1.19       ad 	 */
    269       1.19       ad 	KPREEMPT_DISABLE(l);
    270       1.19       ad 
    271       1.19       ad 	/*
    272       1.22       ad 	 * Lend our priority to lwps on the blocking chain.
    273       1.22       ad 	 *
    274       1.22       ad 	 * NOTE: if you get a panic in this code block, it is likely that
    275       1.22       ad 	 * a lock has been destroyed or corrupted while still in use.  Try
    276       1.22       ad 	 * compiling a kernel with LOCKDEBUG to pinpoint the problem.
    277        1.4     yamt 	 */
    278       1.11       ad 	prio = lwp_eprio(l);
    279  1.23.12.1     matt 
    280        1.4     yamt 	for (;;) {
    281        1.4     yamt 		bool dolock;
    282        1.4     yamt 
    283        1.4     yamt 		if (l->l_wchan == NULL)
    284        1.4     yamt 			break;
    285        1.4     yamt 
    286        1.4     yamt 		owner = (*l->l_syncobj->sobj_owner)(l->l_wchan);
    287        1.4     yamt 		if (owner == NULL)
    288        1.4     yamt 			break;
    289        1.4     yamt 
    290  1.23.12.1     matt 		/* The owner may have changed as we have dropped the tc lock */
    291  1.23.12.1     matt 		if (cur == owner) {
    292  1.23.12.1     matt 			/*
    293  1.23.12.1     matt 			 * we own the lock: stop here, sleepq_block()
    294  1.23.12.1     matt 			 * should wake up immediatly
    295  1.23.12.1     matt 			 */
    296  1.23.12.1     matt 			break;
    297  1.23.12.1     matt 		}
    298        1.4     yamt 
    299  1.23.12.1     matt 		if (l == owner) {
    300  1.23.12.1     matt 			/* owner has changed, restart from curlwp */
    301  1.23.12.1     matt 			lwp_unlock(l);
    302  1.23.12.1     matt 			l = cur;
    303  1.23.12.1     matt 			lwp_lock(l);
    304  1.23.12.1     matt 			prio = lwp_eprio(l);
    305  1.23.12.1     matt 			continue;
    306  1.23.12.1     matt 		}
    307  1.23.12.1     matt 
    308        1.4     yamt 		if (l->l_mutex != owner->l_mutex)
    309        1.4     yamt 			dolock = true;
    310        1.4     yamt 		else
    311        1.4     yamt 			dolock = false;
    312        1.4     yamt 		if (dolock && !lwp_trylock(owner)) {
    313        1.4     yamt 			/*
    314        1.4     yamt 			 * restart from curlwp.
    315  1.23.12.1     matt 			 * Note that there may be a livelock here:
    316  1.23.12.1     matt 			 * the owner may try grabing cur's lock (which is
    317  1.23.12.1     matt 			 * the tc lock) while we're trying to grab
    318  1.23.12.1     matt 			 * the owner's lock.
    319        1.4     yamt 			 */
    320        1.4     yamt 			lwp_unlock(l);
    321        1.4     yamt 			l = cur;
    322        1.4     yamt 			lwp_lock(l);
    323        1.4     yamt 			prio = lwp_eprio(l);
    324        1.4     yamt 			continue;
    325        1.4     yamt 		}
    326       1.11       ad 		if (prio <= lwp_eprio(owner)) {
    327        1.4     yamt 			if (dolock)
    328        1.4     yamt 				lwp_unlock(owner);
    329        1.4     yamt 			break;
    330        1.4     yamt 		}
    331        1.4     yamt 		ts = l->l_ts;
    332        1.4     yamt 		KASSERT(ts->ts_inheritor == owner || ts->ts_inheritor == NULL);
    333        1.4     yamt 		if (ts->ts_inheritor == NULL) {
    334        1.4     yamt 			ts->ts_inheritor = owner;
    335        1.4     yamt 			ts->ts_eprio = prio;
    336        1.4     yamt 			SLIST_INSERT_HEAD(&owner->l_pi_lenders, ts, ts_pichain);
    337        1.4     yamt 			lwp_lendpri(owner, prio);
    338       1.11       ad 		} else if (prio > ts->ts_eprio) {
    339        1.4     yamt 			ts->ts_eprio = prio;
    340        1.4     yamt 			lwp_lendpri(owner, prio);
    341        1.4     yamt 		}
    342        1.4     yamt 		if (dolock)
    343        1.4     yamt 			lwp_unlock(l);
    344        1.4     yamt 		l = owner;
    345        1.4     yamt 	}
    346        1.4     yamt 	LOCKDEBUG_BARRIER(l->l_mutex, 1);
    347        1.4     yamt 	if (cur->l_mutex != l->l_mutex) {
    348        1.4     yamt 		lwp_unlock(l);
    349        1.4     yamt 		lwp_lock(cur);
    350        1.4     yamt 	}
    351        1.4     yamt 	LOCKDEBUG_BARRIER(cur->l_mutex, 1);
    352        1.4     yamt 
    353        1.9     yamt 	sleepq_block(0, false);
    354       1.17       ad 	cur->l_kpribase = obase;
    355       1.19       ad 	KPREEMPT_ENABLE(cur);
    356        1.2       ad }
    357        1.2       ad 
    358        1.2       ad /*
    359        1.2       ad  * turnstile_wakeup:
    360        1.2       ad  *
    361        1.2       ad  *	Wake up the specified number of threads that are blocked
    362        1.2       ad  *	in a turnstile.
    363        1.2       ad  */
    364        1.2       ad void
    365       1.10       ad turnstile_wakeup(turnstile_t *ts, int q, int count, lwp_t *nl)
    366        1.2       ad {
    367        1.2       ad 	sleepq_t *sq;
    368        1.2       ad 	tschain_t *tc;
    369       1.10       ad 	lwp_t *l;
    370        1.2       ad 
    371        1.2       ad 	tc = &turnstile_tab[TS_HASH(ts->ts_obj)];
    372        1.2       ad 	sq = &ts->ts_sleepq[q];
    373        1.2       ad 
    374        1.2       ad 	KASSERT(q == TS_READER_Q || q == TS_WRITER_Q);
    375        1.2       ad 	KASSERT(count > 0 && count <= TS_WAITERS(ts, q));
    376       1.21       ad 	KASSERT(mutex_owned(&tc->tc_mutex));
    377        1.4     yamt 	KASSERT(ts->ts_inheritor == curlwp || ts->ts_inheritor == NULL);
    378        1.4     yamt 
    379        1.4     yamt 	/*
    380        1.4     yamt 	 * restore inherited priority if necessary.
    381        1.4     yamt 	 */
    382        1.4     yamt 
    383        1.4     yamt 	if (ts->ts_inheritor != NULL) {
    384        1.4     yamt 		turnstile_t *iter;
    385        1.4     yamt 		turnstile_t *next;
    386        1.4     yamt 		turnstile_t *prev = NULL;
    387        1.6     yamt 		pri_t prio;
    388        1.9     yamt 		bool dolock;
    389        1.4     yamt 
    390        1.4     yamt 		ts->ts_inheritor = NULL;
    391        1.4     yamt 		l = curlwp;
    392        1.8       ad 
    393       1.15       ad 		dolock = l->l_mutex == l->l_cpu->ci_schedstate.spc_lwplock;
    394        1.9     yamt 		if (dolock) {
    395        1.9     yamt 			lwp_lock(l);
    396        1.8       ad 		}
    397        1.4     yamt 
    398        1.4     yamt 		/*
    399        1.4     yamt 		 * the following loop does two things.
    400        1.4     yamt 		 *
    401        1.4     yamt 		 * - remove ts from the list.
    402        1.4     yamt 		 *
    403        1.4     yamt 		 * - from the rest of the list, find the highest priority.
    404        1.4     yamt 		 */
    405        1.4     yamt 
    406       1.11       ad 		prio = -1;
    407        1.4     yamt 		KASSERT(!SLIST_EMPTY(&l->l_pi_lenders));
    408        1.4     yamt 		for (iter = SLIST_FIRST(&l->l_pi_lenders);
    409        1.4     yamt 		    iter != NULL; iter = next) {
    410       1.11       ad 			KASSERT(lwp_eprio(l) >= ts->ts_eprio);
    411        1.4     yamt 			next = SLIST_NEXT(iter, ts_pichain);
    412        1.4     yamt 			if (iter == ts) {
    413        1.4     yamt 				if (prev == NULL) {
    414        1.4     yamt 					SLIST_REMOVE_HEAD(&l->l_pi_lenders,
    415        1.4     yamt 					    ts_pichain);
    416        1.4     yamt 				} else {
    417        1.4     yamt 					SLIST_REMOVE_AFTER(prev, ts_pichain);
    418        1.4     yamt 				}
    419       1.11       ad 			} else if (prio < iter->ts_eprio) {
    420        1.4     yamt 				prio = iter->ts_eprio;
    421        1.4     yamt 			}
    422        1.4     yamt 			prev = iter;
    423        1.4     yamt 		}
    424        1.4     yamt 
    425        1.4     yamt 		lwp_lendpri(l, prio);
    426        1.8       ad 
    427        1.9     yamt 		if (dolock) {
    428        1.9     yamt 			lwp_unlock(l);
    429        1.8       ad 		}
    430        1.4     yamt 	}
    431        1.2       ad 
    432        1.2       ad 	if (nl != NULL) {
    433        1.2       ad #if defined(DEBUG) || defined(LOCKDEBUG)
    434       1.21       ad 		TAILQ_FOREACH(l, sq, l_sleepchain) {
    435        1.2       ad 			if (l == nl)
    436        1.2       ad 				break;
    437        1.2       ad 		}
    438        1.2       ad 		if (l == NULL)
    439        1.2       ad 			panic("turnstile_wakeup: nl not on sleepq");
    440        1.2       ad #endif
    441       1.21       ad 		turnstile_remove(ts, nl, q);
    442        1.2       ad 	} else {
    443        1.2       ad 		while (count-- > 0) {
    444       1.21       ad 			l = TAILQ_FIRST(sq);
    445        1.2       ad 			KASSERT(l != NULL);
    446       1.21       ad 			turnstile_remove(ts, l, q);
    447        1.2       ad 		}
    448        1.2       ad 	}
    449        1.9     yamt 	mutex_spin_exit(&tc->tc_mutex);
    450        1.2       ad }
    451        1.2       ad 
    452        1.2       ad /*
    453        1.2       ad  * turnstile_unsleep:
    454        1.2       ad  *
    455        1.2       ad  *	Remove an LWP from the turnstile.  This is called when the LWP has
    456        1.2       ad  *	not been awoken normally but instead interrupted: for example, if it
    457        1.2       ad  *	has received a signal.  It's not a valid action for turnstiles,
    458        1.2       ad  *	since LWPs blocking on a turnstile are not interruptable.
    459        1.2       ad  */
    460       1.16       ad u_int
    461       1.16       ad turnstile_unsleep(lwp_t *l, bool cleanup)
    462        1.2       ad {
    463        1.2       ad 
    464        1.2       ad 	lwp_unlock(l);
    465        1.2       ad 	panic("turnstile_unsleep");
    466        1.2       ad }
    467        1.2       ad 
    468        1.2       ad /*
    469        1.2       ad  * turnstile_changepri:
    470        1.2       ad  *
    471        1.4     yamt  *	Adjust the priority of an LWP residing on a turnstile.
    472        1.2       ad  */
    473        1.2       ad void
    474       1.10       ad turnstile_changepri(lwp_t *l, pri_t pri)
    475        1.2       ad {
    476        1.2       ad 
    477        1.4     yamt 	/* XXX priority inheritance */
    478        1.4     yamt 	sleepq_changepri(l, pri);
    479        1.2       ad }
    480        1.2       ad 
    481        1.2       ad #if defined(LOCKDEBUG)
    482        1.2       ad /*
    483        1.2       ad  * turnstile_print:
    484        1.2       ad  *
    485        1.2       ad  *	Given the address of a lock object, print the contents of a
    486        1.2       ad  *	turnstile.
    487        1.2       ad  */
    488        1.2       ad void
    489        1.2       ad turnstile_print(volatile void *obj, void (*pr)(const char *, ...))
    490        1.2       ad {
    491        1.2       ad 	turnstile_t *ts;
    492        1.2       ad 	tschain_t *tc;
    493        1.2       ad 	sleepq_t *rsq, *wsq;
    494       1.10       ad 	lwp_t *l;
    495        1.2       ad 
    496        1.2       ad 	tc = &turnstile_tab[TS_HASH(obj)];
    497        1.2       ad 
    498        1.2       ad 	LIST_FOREACH(ts, &tc->tc_chain, ts_chain)
    499        1.2       ad 		if (ts->ts_obj == obj)
    500        1.2       ad 			break;
    501        1.2       ad 
    502        1.9     yamt 	(*pr)("Turnstile chain at %p.\n", tc);
    503        1.2       ad 	if (ts == NULL) {
    504        1.2       ad 		(*pr)("=> No active turnstile for this lock.\n");
    505        1.2       ad 		return;
    506        1.2       ad 	}
    507        1.2       ad 
    508        1.2       ad 	rsq = &ts->ts_sleepq[TS_READER_Q];
    509        1.2       ad 	wsq = &ts->ts_sleepq[TS_WRITER_Q];
    510        1.2       ad 
    511        1.2       ad 	(*pr)("=> Turnstile at %p (wrq=%p, rdq=%p).\n", ts, rsq, wsq);
    512        1.2       ad 
    513       1.21       ad 	(*pr)("=> %d waiting readers:", TS_WAITERS(ts, TS_READER_Q));
    514       1.21       ad 	TAILQ_FOREACH(l, rsq, l_sleepchain) {
    515        1.2       ad 		(*pr)(" %p", l);
    516        1.2       ad 	}
    517        1.2       ad 	(*pr)("\n");
    518        1.2       ad 
    519       1.21       ad 	(*pr)("=> %d waiting writers:", TS_WAITERS(ts, TS_WRITER_Q));
    520       1.21       ad 	TAILQ_FOREACH(l, wsq, l_sleepchain) {
    521        1.2       ad 		(*pr)(" %p", l);
    522        1.2       ad 	}
    523        1.2       ad 	(*pr)("\n");
    524        1.2       ad }
    525        1.2       ad #endif	/* LOCKDEBUG */
    526