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