subr_turnstile.c revision 126884
1/*-
2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 *    promote products derived from this software without specific prior
14 *    written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
29 *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
30 */
31
32/*
33 * Implementation of turnstiles used to hold queue of threads blocked on
34 * non-sleepable locks.  Sleepable locks use condition variables to
35 * implement their queues.  Turnstiles differ from a sleep queue in that
36 * turnstile queue's are assigned to a lock held by an owning thread.  Thus,
37 * when one thread is enqueued onto a turnstile, it can lend its priority
38 * to the owning thread.
39 *
40 * We wish to avoid bloating locks with an embedded turnstile and we do not
41 * want to use back-pointers in the locks for the same reason.  Thus, we
42 * use a similar approach to that of Solaris 7 as described in Solaris
43 * Internals by Jim Mauro and Richard McDougall.  Turnstiles are looked up
44 * in a hash table based on the address of the lock.  Each entry in the
45 * hash table is a linked-lists of turnstiles and is called a turnstile
46 * chain.  Each chain contains a spin mutex that protects all of the
47 * turnstiles in the chain.
48 *
49 * Each time a thread is created, a turnstile is malloc'd and attached to
50 * that thread.  When a thread blocks on a lock, if it is the first thread
51 * to block, it lends its turnstile to the lock.  If the lock already has
52 * a turnstile, then it gives its turnstile to the lock's turnstile's free
53 * list.  When a thread is woken up, it takes a turnstile from the free list
54 * if there are any other waiters.  If it is the only thread blocked on the
55 * lock, then it reclaims the turnstile associated with the lock and removes
56 * it from the hash table.
57 */
58
59#include <sys/cdefs.h>
60__FBSDID("$FreeBSD: head/sys/kern/subr_turnstile.c 126884 2004-03-12 19:05:46Z jhb $");
61
62#include <sys/param.h>
63#include <sys/systm.h>
64#include <sys/kernel.h>
65#include <sys/ktr.h>
66#include <sys/lock.h>
67#include <sys/malloc.h>
68#include <sys/mutex.h>
69#include <sys/proc.h>
70#include <sys/queue.h>
71#include <sys/resourcevar.h>
72#include <sys/turnstile.h>
73#include <sys/sched.h>
74
75/*
76 * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
77 * number chosen because the sleep queue's use the same value for the
78 * shift.  Basically, we ignore the lower 8 bits of the address.
79 * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
80 */
81#define	TC_TABLESIZE	128			/* Must be power of 2. */
82#define	TC_MASK		(TC_TABLESIZE - 1)
83#define	TC_SHIFT	8
84#define	TC_HASH(lock)	(((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
85#define	TC_LOOKUP(lock)	&turnstile_chains[TC_HASH(lock)]
86
87/*
88 * There are three different lists of turnstiles as follows.  The list
89 * connected by ts_link entries is a per-thread list of all the turnstiles
90 * attached to locks that we own.  This is used to fixup our priority when
91 * a lock is released.  The other two lists use the ts_hash entries.  The
92 * first of these two is the turnstile chain list that a turnstile is on
93 * when it is attached to a lock.  The second list to use ts_hash is the
94 * free list hung off of a turnstile that is attached to a lock.
95 *
96 * Each turnstile contains two lists of threads.  The ts_blocked list is
97 * a linked list of threads blocked on the turnstile's lock.  The
98 * ts_pending list is a linked list of threads previously awakened by
99 * turnstile_signal() or turnstile_wait() that are waiting to be put on
100 * the run queue.
101 *
102 * Locking key:
103 *  c - turnstile chain lock
104 *  q - td_contested lock
105 */
106struct turnstile {
107	TAILQ_HEAD(, thread) ts_blocked;	/* (c + q) Blocked threads. */
108	TAILQ_HEAD(, thread) ts_pending;	/* (c) Pending threads. */
109	LIST_ENTRY(turnstile) ts_hash;		/* (c) Chain and free list. */
110	LIST_ENTRY(turnstile) ts_link;		/* (q) Contested locks. */
111	LIST_HEAD(, turnstile) ts_free;		/* (c) Free turnstiles. */
112	struct lock_object *ts_lockobj;		/* (c) Lock we reference. */
113	struct thread *ts_owner;		/* (c + q) Who owns the lock. */
114};
115
116struct turnstile_chain {
117	LIST_HEAD(, turnstile) tc_turnstiles;	/* List of turnstiles. */
118	struct mtx tc_lock;			/* Spin lock for this chain. */
119};
120
121static struct mtx td_contested_lock;
122static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
123
124MALLOC_DEFINE(M_TURNSTILE, "turnstiles", "turnstiles");
125
126/*
127 * Prototypes for non-exported routines.
128 */
129static void	init_turnstile0(void *dummy);
130static void	propagate_priority(struct thread *);
131static void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
132
133/*
134 * Walks the chain of turnstiles and their owners to propagate the priority
135 * of the thread being blocked to all the threads holding locks that have to
136 * release their locks before this thread can run again.
137 */
138static void
139propagate_priority(struct thread *td)
140{
141	struct turnstile_chain *tc;
142	struct turnstile *ts;
143	struct thread *td1;
144	int pri;
145
146	mtx_assert(&sched_lock, MA_OWNED);
147	pri = td->td_priority;
148	ts = td->td_blocked;
149	for (;;) {
150		td = ts->ts_owner;
151
152		if (td == NULL) {
153			/*
154			 * This really isn't quite right. Really
155			 * ought to bump priority of thread that
156			 * next acquires the lock.
157			 */
158			return;
159		}
160
161		MPASS(td->td_proc != NULL);
162		MPASS(td->td_proc->p_magic == P_MAGIC);
163
164		/*
165		 * XXX: The owner of a turnstile can be stale if it is the
166		 * first thread to grab a slock of a sx lock.  In that case
167		 * it is possible for us to be at SSLEEP or some other
168		 * weird state.  We should probably just return if the state
169		 * isn't SRUN or SLOCK.
170		 */
171		KASSERT(!TD_IS_SLEEPING(td),
172		    ("sleeping thread (pid %d) owns a non-sleepable lock",
173		    td->td_proc->p_pid));
174
175		/*
176		 * If this thread already has higher priority than the
177		 * thread that is being blocked, we are finished.
178		 */
179		if (td->td_priority <= pri)
180			return;
181
182		/*
183		 * If lock holder is actually running, just bump priority.
184		 */
185		if (TD_IS_RUNNING(td)) {
186			td->td_priority = pri;
187			return;
188		}
189
190#ifndef SMP
191		/*
192		 * For UP, we check to see if td is curthread (this shouldn't
193		 * ever happen however as it would mean we are in a deadlock.)
194		 */
195		KASSERT(td != curthread, ("Deadlock detected"));
196#endif
197
198		/*
199		 * If on run queue move to new run queue, and quit.
200		 * XXXKSE this gets a lot more complicated under threads
201		 * but try anyhow.
202		 */
203		if (TD_ON_RUNQ(td)) {
204			MPASS(td->td_blocked == NULL);
205			sched_prio(td, pri);
206			return;
207		}
208
209		/*
210		 * Bump this thread's priority.
211		 */
212		td->td_priority = pri;
213
214		/*
215		 * If we aren't blocked on a lock, we should be.
216		 */
217		KASSERT(TD_ON_LOCK(td), (
218		    "process %d(%s):%d holds %s but isn't blocked on a lock\n",
219		    td->td_proc->p_pid, td->td_proc->p_comm, td->td_state,
220		    ts->ts_lockobj->lo_name));
221
222		/*
223		 * Pick up the lock that td is blocked on.
224		 */
225		ts = td->td_blocked;
226		MPASS(ts != NULL);
227		tc = TC_LOOKUP(ts->ts_lockobj);
228		mtx_lock_spin(&tc->tc_lock);
229
230		/*
231		 * This thread may not be blocked on this turnstile anymore
232		 * but instead might already be woken up on another CPU
233		 * that is waiting on sched_lock in turnstile_unpend() to
234		 * finish waking this thread up.  We can detect this case
235		 * by checking to see if this thread has been given a
236		 * turnstile by either turnstile_signal() or
237		 * turnstile_wakeup().  In this case, treat the thread as
238		 * if it was already running.
239		 */
240		if (td->td_turnstile != NULL) {
241			mtx_unlock_spin(&tc->tc_lock);
242			return;
243		}
244
245		/*
246		 * Check if the thread needs to be moved up on
247		 * the blocked chain.  It doesn't need to be moved
248		 * if it is already at the head of the list or if
249		 * the item in front of it still has a higher priority.
250		 */
251		if (td == TAILQ_FIRST(&ts->ts_blocked)) {
252			mtx_unlock_spin(&tc->tc_lock);
253			continue;
254		}
255
256		td1 = TAILQ_PREV(td, threadqueue, td_lockq);
257		if (td1->td_priority <= pri) {
258			mtx_unlock_spin(&tc->tc_lock);
259			continue;
260		}
261
262		/*
263		 * Remove thread from blocked chain and determine where
264		 * it should be moved up to.  Since we know that td1 has
265		 * a lower priority than td, we know that at least one
266		 * thread in the chain has a lower priority and that
267		 * td1 will thus not be NULL after the loop.
268		 */
269		mtx_lock_spin(&td_contested_lock);
270		TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
271		TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq) {
272			MPASS(td1->td_proc->p_magic == P_MAGIC);
273			if (td1->td_priority > pri)
274				break;
275		}
276
277		MPASS(td1 != NULL);
278		TAILQ_INSERT_BEFORE(td1, td, td_lockq);
279		mtx_unlock_spin(&td_contested_lock);
280		CTR4(KTR_LOCK,
281		    "propagate_priority: td %p moved before %p on [%p] %s",
282		    td, td1, ts->ts_lockobj, ts->ts_lockobj->lo_name);
283		mtx_unlock_spin(&tc->tc_lock);
284	}
285}
286
287/*
288 * Early initialization of turnstiles.  This is not done via a SYSINIT()
289 * since this needs to be initialized very early when mutexes are first
290 * initialized.
291 */
292void
293init_turnstiles(void)
294{
295	int i;
296
297	for (i = 0; i < TC_TABLESIZE; i++) {
298		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
299		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
300		    NULL, MTX_SPIN);
301	}
302	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
303	thread0.td_turnstile = NULL;
304}
305
306static void
307init_turnstile0(void *dummy)
308{
309
310	thread0.td_turnstile = turnstile_alloc();
311}
312SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
313
314/*
315 * Set the owner of the lock this turnstile is attached to.
316 */
317static void
318turnstile_setowner(struct turnstile *ts, struct thread *owner)
319{
320
321	mtx_assert(&td_contested_lock, MA_OWNED);
322	MPASS(owner->td_proc->p_magic == P_MAGIC);
323	MPASS(ts->ts_owner == NULL);
324	ts->ts_owner = owner;
325	LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
326}
327
328/*
329 * Malloc a turnstile for a new thread, initialize it and return it.
330 */
331struct turnstile *
332turnstile_alloc(void)
333{
334	struct turnstile *ts;
335
336	ts = malloc(sizeof(struct turnstile), M_TURNSTILE, M_WAITOK | M_ZERO);
337	TAILQ_INIT(&ts->ts_blocked);
338	TAILQ_INIT(&ts->ts_pending);
339	LIST_INIT(&ts->ts_free);
340	return (ts);
341}
342
343/*
344 * Free a turnstile when a thread is destroyed.
345 */
346void
347turnstile_free(struct turnstile *ts)
348{
349
350	MPASS(ts != NULL);
351	MPASS(TAILQ_EMPTY(&ts->ts_blocked));
352	MPASS(TAILQ_EMPTY(&ts->ts_pending));
353	free(ts, M_TURNSTILE);
354}
355
356/*
357 * Look up the turnstile for a lock in the hash table locking the associated
358 * turnstile chain along the way.  Return with the turnstile chain locked.
359 * If no turnstile is found in the hash table, NULL is returned.
360 */
361struct turnstile *
362turnstile_lookup(struct lock_object *lock)
363{
364	struct turnstile_chain *tc;
365	struct turnstile *ts;
366
367	tc = TC_LOOKUP(lock);
368	mtx_lock_spin(&tc->tc_lock);
369	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
370		if (ts->ts_lockobj == lock)
371			return (ts);
372	return (NULL);
373}
374
375/*
376 * Unlock the turnstile chain associated with a given lock.
377 */
378void
379turnstile_release(struct lock_object *lock)
380{
381	struct turnstile_chain *tc;
382
383	tc = TC_LOOKUP(lock);
384	mtx_unlock_spin(&tc->tc_lock);
385}
386
387/*
388 * Take ownership of a turnstile and adjust the priority of the new
389 * owner appropriately.
390 */
391void
392turnstile_claim(struct turnstile *ts)
393{
394	struct turnstile_chain *tc;
395	struct thread *td, *owner;
396
397	tc = TC_LOOKUP(ts->ts_lockobj);
398	mtx_assert(&tc->tc_lock, MA_OWNED);
399
400	owner = curthread;
401	mtx_lock_spin(&td_contested_lock);
402	turnstile_setowner(ts, owner);
403	mtx_unlock_spin(&td_contested_lock);
404
405	td = TAILQ_FIRST(&ts->ts_blocked);
406	MPASS(td != NULL);
407	MPASS(td->td_proc->p_magic == P_MAGIC);
408	mtx_unlock_spin(&tc->tc_lock);
409
410	/*
411	 * Update the priority of the new owner if needed.
412	 */
413	mtx_lock_spin(&sched_lock);
414	if (td->td_priority < owner->td_priority)
415		owner->td_priority = td->td_priority;
416	mtx_unlock_spin(&sched_lock);
417}
418
419/*
420 * Block the current thread on the turnstile ts.  This function will context
421 * switch and not return until this thread has been woken back up.  This
422 * function must be called with the appropriate turnstile chain locked and
423 * will return with it unlocked.
424 */
425void
426turnstile_wait(struct turnstile *ts, struct lock_object *lock,
427    struct thread *owner)
428{
429	struct turnstile_chain *tc;
430	struct thread *td, *td1;
431
432	td = curthread;
433	tc = TC_LOOKUP(lock);
434	mtx_assert(&tc->tc_lock, MA_OWNED);
435	MPASS(td->td_turnstile != NULL);
436	MPASS(owner != NULL);
437	MPASS(owner->td_proc->p_magic == P_MAGIC);
438
439	/* If the passed in turnstile is NULL, use this thread's turnstile. */
440	if (ts == NULL) {
441		ts = td->td_turnstile;
442		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
443		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
444		    ("thread's turnstile has pending threads"));
445		KASSERT(TAILQ_EMPTY(&ts->ts_blocked),
446		    ("thread's turnstile has a non-empty queue"));
447		KASSERT(LIST_EMPTY(&ts->ts_free),
448		    ("thread's turnstile has a non-empty free list"));
449		KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
450		ts->ts_lockobj = lock;
451		mtx_lock_spin(&td_contested_lock);
452		TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
453		turnstile_setowner(ts, owner);
454		mtx_unlock_spin(&td_contested_lock);
455	} else {
456		TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq)
457			if (td1->td_priority > td->td_priority)
458				break;
459		mtx_lock_spin(&td_contested_lock);
460		if (td1 != NULL)
461			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
462		else
463			TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
464		mtx_unlock_spin(&td_contested_lock);
465		MPASS(td->td_turnstile != NULL);
466		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
467		MPASS(owner == ts->ts_owner);
468	}
469	td->td_turnstile = NULL;
470	mtx_unlock_spin(&tc->tc_lock);
471
472	mtx_lock_spin(&sched_lock);
473	/*
474	 * Handle race condition where a thread on another CPU that owns
475	 * lock 'lock' could have woken us in between us dropping the
476	 * turnstile chain lock and acquiring the sched_lock.
477	 */
478	if (td->td_flags & TDF_TSNOBLOCK) {
479		td->td_flags &= ~TDF_TSNOBLOCK;
480		mtx_unlock_spin(&sched_lock);
481		return;
482	}
483
484#ifdef notyet
485	/*
486	 * If we're borrowing an interrupted thread's VM context, we
487	 * must clean up before going to sleep.
488	 */
489	if (td->td_ithd != NULL) {
490		struct ithd *it = td->td_ithd;
491
492		if (it->it_interrupted) {
493			if (LOCK_LOG_TEST(lock, 0))
494				CTR3(KTR_LOCK, "%s: %p interrupted %p",
495				    __func__, it, it->it_interrupted);
496			intr_thd_fixup(it);
497		}
498	}
499#endif
500
501	/* Save who we are blocked on and switch. */
502	td->td_blocked = ts;
503	td->td_lockname = lock->lo_name;
504	TD_SET_LOCK(td);
505	propagate_priority(td);
506
507	if (LOCK_LOG_TEST(lock, 0))
508		CTR4(KTR_LOCK, "%s: td %p blocked on [%p] %s", __func__, td,
509		    lock, lock->lo_name);
510
511	mi_switch(SW_VOL);
512
513	if (LOCK_LOG_TEST(lock, 0))
514		CTR4(KTR_LOCK, "%s: td %p free from blocked on [%p] %s",
515		    __func__, td, lock, lock->lo_name);
516
517	mtx_unlock_spin(&sched_lock);
518}
519
520/*
521 * Pick the highest priority thread on this turnstile and put it on the
522 * pending list.  This must be called with the turnstile chain locked.
523 */
524int
525turnstile_signal(struct turnstile *ts)
526{
527	struct turnstile_chain *tc;
528	struct thread *td;
529	int empty;
530
531	MPASS(ts != NULL);
532	MPASS(curthread->td_proc->p_magic == P_MAGIC);
533	MPASS(ts->ts_owner == curthread);
534	tc = TC_LOOKUP(ts->ts_lockobj);
535	mtx_assert(&tc->tc_lock, MA_OWNED);
536
537	/*
538	 * Pick the highest priority thread blocked on this lock and
539	 * move it to the pending list.
540	 */
541	td = TAILQ_FIRST(&ts->ts_blocked);
542	MPASS(td->td_proc->p_magic == P_MAGIC);
543	mtx_lock_spin(&td_contested_lock);
544	TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
545	mtx_unlock_spin(&td_contested_lock);
546	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
547
548	/*
549	 * If the turnstile is now empty, remove it from its chain and
550	 * give it to the about-to-be-woken thread.  Otherwise take a
551	 * turnstile from the free list and give it to the thread.
552	 */
553	empty = TAILQ_EMPTY(&ts->ts_blocked);
554	if (empty)
555		MPASS(LIST_EMPTY(&ts->ts_free));
556	else
557		ts = LIST_FIRST(&ts->ts_free);
558	MPASS(ts != NULL);
559	LIST_REMOVE(ts, ts_hash);
560	td->td_turnstile = ts;
561
562	return (empty);
563}
564
565/*
566 * Put all blocked threads on the pending list.  This must be called with
567 * the turnstile chain locked.
568 */
569void
570turnstile_wakeup(struct turnstile *ts)
571{
572	struct turnstile_chain *tc;
573	struct turnstile *ts1;
574	struct thread *td;
575
576	MPASS(ts != NULL);
577	MPASS(curthread->td_proc->p_magic == P_MAGIC);
578	MPASS(ts->ts_owner == curthread);
579	tc = TC_LOOKUP(ts->ts_lockobj);
580	mtx_assert(&tc->tc_lock, MA_OWNED);
581
582	/*
583	 * Transfer the blocked list to the pending list.
584	 */
585	mtx_lock_spin(&td_contested_lock);
586	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked, td_lockq);
587	mtx_unlock_spin(&td_contested_lock);
588
589	/*
590	 * Give a turnstile to each thread.  The last thread gets
591	 * this turnstile.
592	 */
593	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
594		if (LIST_EMPTY(&ts->ts_free)) {
595			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
596			ts1 = ts;
597		} else
598			ts1 = LIST_FIRST(&ts->ts_free);
599		MPASS(ts1 != NULL);
600		LIST_REMOVE(ts1, ts_hash);
601		td->td_turnstile = ts1;
602	}
603}
604
605/*
606 * Wakeup all threads on the pending list and adjust the priority of the
607 * current thread appropriately.  This must be called with the turnstile
608 * chain locked.
609 */
610void
611turnstile_unpend(struct turnstile *ts)
612{
613	TAILQ_HEAD( ,thread) pending_threads;
614	struct turnstile_chain *tc;
615	struct thread *td;
616	int cp, pri;
617
618	MPASS(ts != NULL);
619	MPASS(ts->ts_owner == curthread);
620	tc = TC_LOOKUP(ts->ts_lockobj);
621	mtx_assert(&tc->tc_lock, MA_OWNED);
622	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
623
624	/*
625	 * Move the list of pending threads out of the turnstile and
626	 * into a local variable.
627	 */
628	TAILQ_INIT(&pending_threads);
629	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
630#ifdef INVARIANTS
631	if (TAILQ_EMPTY(&ts->ts_blocked))
632		ts->ts_lockobj = NULL;
633#endif
634
635	/*
636	 * Remove the turnstile from this thread's list of contested locks
637	 * since this thread doesn't own it anymore.  New threads will
638	 * not be blocking on the turnstile until it is claimed by a new
639	 * owner.
640	 */
641	mtx_lock_spin(&td_contested_lock);
642	ts->ts_owner = NULL;
643	LIST_REMOVE(ts, ts_link);
644	mtx_unlock_spin(&td_contested_lock);
645	mtx_unlock_spin(&tc->tc_lock);
646
647	/*
648	 * Adjust the priority of curthread based on other contested
649	 * locks it owns.  Don't lower the priority below the base
650	 * priority however.
651	 */
652	td = curthread;
653	pri = PRI_MAX;
654	mtx_lock_spin(&sched_lock);
655	mtx_lock_spin(&td_contested_lock);
656	LIST_FOREACH(ts, &td->td_contested, ts_link) {
657		cp = TAILQ_FIRST(&ts->ts_blocked)->td_priority;
658		if (cp < pri)
659			pri = cp;
660	}
661	mtx_unlock_spin(&td_contested_lock);
662	if (pri > td->td_base_pri)
663		pri = td->td_base_pri;
664	td->td_priority = pri;
665
666	/*
667	 * Wake up all the pending threads.  If a thread is not blocked
668	 * on a lock, then it is currently executing on another CPU in
669	 * turnstile_wait() or sitting on a run queue waiting to resume
670	 * in turnstile_wait().  Set a flag to force it to try to acquire
671	 * the lock again instead of blocking.
672	 */
673	while (!TAILQ_EMPTY(&pending_threads)) {
674		td = TAILQ_FIRST(&pending_threads);
675		TAILQ_REMOVE(&pending_threads, td, td_lockq);
676		MPASS(td->td_proc->p_magic == P_MAGIC);
677		if (TD_ON_LOCK(td)) {
678			td->td_blocked = NULL;
679			td->td_lockname = NULL;
680			TD_CLR_LOCK(td);
681			MPASS(TD_CAN_RUN(td));
682			setrunqueue(td);
683		} else {
684			td->td_flags |= TDF_TSNOBLOCK;
685			MPASS(TD_IS_RUNNING(td) || TD_ON_RUNQ(td));
686		}
687	}
688	mtx_unlock_spin(&sched_lock);
689}
690
691/*
692 * Return the first thread in a turnstile.
693 */
694struct thread *
695turnstile_head(struct turnstile *ts)
696{
697#ifdef INVARIANTS
698	struct turnstile_chain *tc;
699
700	MPASS(ts != NULL);
701	tc = TC_LOOKUP(ts->ts_lockobj);
702	mtx_assert(&tc->tc_lock, MA_OWNED);
703#endif
704	return (TAILQ_FIRST(&ts->ts_blocked));
705}
706
707/*
708 * Returns true if a turnstile is empty.
709 */
710int
711turnstile_empty(struct turnstile *ts)
712{
713#ifdef INVARIANTS
714	struct turnstile_chain *tc;
715
716	MPASS(ts != NULL);
717	tc = TC_LOOKUP(ts->ts_lockobj);
718	mtx_assert(&tc->tc_lock, MA_OWNED);
719#endif
720	return (TAILQ_EMPTY(&ts->ts_blocked));
721}
722