subr_turnstile.c revision 131259
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 "opt_turnstile_profiling.h"
60
61#include <sys/cdefs.h>
62__FBSDID("$FreeBSD: head/sys/kern/subr_turnstile.c 131259 2004-06-29 02:30:12Z jhb $");
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/kernel.h>
67#include <sys/ktr.h>
68#include <sys/lock.h>
69#include <sys/malloc.h>
70#include <sys/mutex.h>
71#include <sys/proc.h>
72#include <sys/queue.h>
73#include <sys/resourcevar.h>
74#include <sys/sched.h>
75#include <sys/sysctl.h>
76#include <sys/turnstile.h>
77
78/*
79 * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
80 * number chosen because the sleep queue's use the same value for the
81 * shift.  Basically, we ignore the lower 8 bits of the address.
82 * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
83 */
84#define	TC_TABLESIZE	128			/* Must be power of 2. */
85#define	TC_MASK		(TC_TABLESIZE - 1)
86#define	TC_SHIFT	8
87#define	TC_HASH(lock)	(((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
88#define	TC_LOOKUP(lock)	&turnstile_chains[TC_HASH(lock)]
89
90/*
91 * There are three different lists of turnstiles as follows.  The list
92 * connected by ts_link entries is a per-thread list of all the turnstiles
93 * attached to locks that we own.  This is used to fixup our priority when
94 * a lock is released.  The other two lists use the ts_hash entries.  The
95 * first of these two is the turnstile chain list that a turnstile is on
96 * when it is attached to a lock.  The second list to use ts_hash is the
97 * free list hung off of a turnstile that is attached to a lock.
98 *
99 * Each turnstile contains two lists of threads.  The ts_blocked list is
100 * a linked list of threads blocked on the turnstile's lock.  The
101 * ts_pending list is a linked list of threads previously awakened by
102 * turnstile_signal() or turnstile_wait() that are waiting to be put on
103 * the run queue.
104 *
105 * Locking key:
106 *  c - turnstile chain lock
107 *  q - td_contested lock
108 */
109struct turnstile {
110	TAILQ_HEAD(, thread) ts_blocked;	/* (c + q) Blocked threads. */
111	TAILQ_HEAD(, thread) ts_pending;	/* (c) Pending threads. */
112	LIST_ENTRY(turnstile) ts_hash;		/* (c) Chain and free list. */
113	LIST_ENTRY(turnstile) ts_link;		/* (q) Contested locks. */
114	LIST_HEAD(, turnstile) ts_free;		/* (c) Free turnstiles. */
115	struct lock_object *ts_lockobj;		/* (c) Lock we reference. */
116	struct thread *ts_owner;		/* (c + q) Who owns the lock. */
117};
118
119struct turnstile_chain {
120	LIST_HEAD(, turnstile) tc_turnstiles;	/* List of turnstiles. */
121	struct mtx tc_lock;			/* Spin lock for this chain. */
122#ifdef TURNSTILE_PROFILING
123	u_int	tc_depth;			/* Length of tc_queues. */
124	u_int	tc_max_depth;			/* Max length of tc_queues. */
125#endif
126};
127
128#ifdef TURNSTILE_PROFILING
129u_int turnstile_max_depth;
130SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0, "turnstile profiling");
131SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
132    "turnstile chain stats");
133SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
134    &turnstile_max_depth, 0, "maxmimum depth achieved of a single chain");
135#endif
136static struct mtx td_contested_lock;
137static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
138
139MALLOC_DEFINE(M_TURNSTILE, "turnstiles", "turnstiles");
140
141/*
142 * Prototypes for non-exported routines.
143 */
144static void	init_turnstile0(void *dummy);
145static void	propagate_priority(struct thread *);
146static void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
147
148/*
149 * Walks the chain of turnstiles and their owners to propagate the priority
150 * of the thread being blocked to all the threads holding locks that have to
151 * release their locks before this thread can run again.
152 */
153static void
154propagate_priority(struct thread *td)
155{
156	struct turnstile_chain *tc;
157	struct turnstile *ts;
158	struct thread *td1;
159	int pri;
160
161	mtx_assert(&sched_lock, MA_OWNED);
162	pri = td->td_priority;
163	ts = td->td_blocked;
164	for (;;) {
165		td = ts->ts_owner;
166
167		if (td == NULL) {
168			/*
169			 * This really isn't quite right. Really
170			 * ought to bump priority of thread that
171			 * next acquires the lock.
172			 */
173			return;
174		}
175
176		MPASS(td->td_proc != NULL);
177		MPASS(td->td_proc->p_magic == P_MAGIC);
178
179		/*
180		 * XXX: The owner of a turnstile can be stale if it is the
181		 * first thread to grab a slock of a sx lock.  In that case
182		 * it is possible for us to be at SSLEEP or some other
183		 * weird state.  We should probably just return if the state
184		 * isn't SRUN or SLOCK.
185		 */
186		KASSERT(!TD_IS_SLEEPING(td),
187		    ("sleeping thread (pid %d) owns a non-sleepable lock",
188		    td->td_proc->p_pid));
189
190		/*
191		 * If this thread already has higher priority than the
192		 * thread that is being blocked, we are finished.
193		 */
194		if (td->td_priority <= pri)
195			return;
196
197		/*
198		 * If lock holder is actually running, just bump priority.
199		 */
200		if (TD_IS_RUNNING(td)) {
201			td->td_priority = pri;
202			return;
203		}
204
205#ifndef SMP
206		/*
207		 * For UP, we check to see if td is curthread (this shouldn't
208		 * ever happen however as it would mean we are in a deadlock.)
209		 */
210		KASSERT(td != curthread, ("Deadlock detected"));
211#endif
212
213		/*
214		 * If on run queue move to new run queue, and quit.
215		 * XXXKSE this gets a lot more complicated under threads
216		 * but try anyhow.
217		 */
218		if (TD_ON_RUNQ(td)) {
219			MPASS(td->td_blocked == NULL);
220			sched_prio(td, pri);
221			return;
222		}
223
224		/*
225		 * Bump this thread's priority.
226		 */
227		td->td_priority = pri;
228
229		/*
230		 * If we aren't blocked on a lock, we should be.
231		 */
232		KASSERT(TD_ON_LOCK(td), (
233		    "process %d(%s):%d holds %s but isn't blocked on a lock\n",
234		    td->td_proc->p_pid, td->td_proc->p_comm, td->td_state,
235		    ts->ts_lockobj->lo_name));
236
237		/*
238		 * Pick up the lock that td is blocked on.
239		 */
240		ts = td->td_blocked;
241		MPASS(ts != NULL);
242		tc = TC_LOOKUP(ts->ts_lockobj);
243		mtx_lock_spin(&tc->tc_lock);
244
245		/*
246		 * This thread may not be blocked on this turnstile anymore
247		 * but instead might already be woken up on another CPU
248		 * that is waiting on sched_lock in turnstile_unpend() to
249		 * finish waking this thread up.  We can detect this case
250		 * by checking to see if this thread has been given a
251		 * turnstile by either turnstile_signal() or
252		 * turnstile_broadcast().  In this case, treat the thread as
253		 * if it was already running.
254		 */
255		if (td->td_turnstile != NULL) {
256			mtx_unlock_spin(&tc->tc_lock);
257			return;
258		}
259
260		/*
261		 * Check if the thread needs to be moved up on
262		 * the blocked chain.  It doesn't need to be moved
263		 * if it is already at the head of the list or if
264		 * the item in front of it still has a higher priority.
265		 */
266		if (td == TAILQ_FIRST(&ts->ts_blocked)) {
267			mtx_unlock_spin(&tc->tc_lock);
268			continue;
269		}
270
271		td1 = TAILQ_PREV(td, threadqueue, td_lockq);
272		if (td1->td_priority <= pri) {
273			mtx_unlock_spin(&tc->tc_lock);
274			continue;
275		}
276
277		/*
278		 * Remove thread from blocked chain and determine where
279		 * it should be moved up to.  Since we know that td1 has
280		 * a lower priority than td, we know that at least one
281		 * thread in the chain has a lower priority and that
282		 * td1 will thus not be NULL after the loop.
283		 */
284		mtx_lock_spin(&td_contested_lock);
285		TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
286		TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq) {
287			MPASS(td1->td_proc->p_magic == P_MAGIC);
288			if (td1->td_priority > pri)
289				break;
290		}
291
292		MPASS(td1 != NULL);
293		TAILQ_INSERT_BEFORE(td1, td, td_lockq);
294		mtx_unlock_spin(&td_contested_lock);
295		CTR4(KTR_LOCK,
296		    "propagate_priority: td %p moved before %p on [%p] %s",
297		    td, td1, ts->ts_lockobj, ts->ts_lockobj->lo_name);
298		mtx_unlock_spin(&tc->tc_lock);
299	}
300}
301
302/*
303 * Early initialization of turnstiles.  This is not done via a SYSINIT()
304 * since this needs to be initialized very early when mutexes are first
305 * initialized.
306 */
307void
308init_turnstiles(void)
309{
310#ifdef TURNSTILE_PROFILING
311	struct sysctl_oid *chain_oid;
312	char chain_name[10];
313#endif
314	int i;
315
316	for (i = 0; i < TC_TABLESIZE; i++) {
317		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
318		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
319		    NULL, MTX_SPIN);
320#ifdef TURNSTILE_PROFILING
321		snprintf(chain_name, sizeof(chain_name), "%d", i);
322		chain_oid = SYSCTL_ADD_NODE(NULL,
323		    SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
324		    chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
325		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
326		    "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
327		    NULL);
328		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
329		    "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
330		    0, NULL);
331#endif
332	}
333	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
334	thread0.td_turnstile = NULL;
335}
336
337static void
338init_turnstile0(void *dummy)
339{
340
341	thread0.td_turnstile = turnstile_alloc();
342}
343SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
344
345/*
346 * Set the owner of the lock this turnstile is attached to.
347 */
348static void
349turnstile_setowner(struct turnstile *ts, struct thread *owner)
350{
351
352	mtx_assert(&td_contested_lock, MA_OWNED);
353	MPASS(owner->td_proc->p_magic == P_MAGIC);
354	MPASS(ts->ts_owner == NULL);
355	ts->ts_owner = owner;
356	LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
357}
358
359/*
360 * Malloc a turnstile for a new thread, initialize it and return it.
361 */
362struct turnstile *
363turnstile_alloc(void)
364{
365	struct turnstile *ts;
366
367	ts = malloc(sizeof(struct turnstile), M_TURNSTILE, M_WAITOK | M_ZERO);
368	TAILQ_INIT(&ts->ts_blocked);
369	TAILQ_INIT(&ts->ts_pending);
370	LIST_INIT(&ts->ts_free);
371	return (ts);
372}
373
374/*
375 * Free a turnstile when a thread is destroyed.
376 */
377void
378turnstile_free(struct turnstile *ts)
379{
380
381	MPASS(ts != NULL);
382	MPASS(TAILQ_EMPTY(&ts->ts_blocked));
383	MPASS(TAILQ_EMPTY(&ts->ts_pending));
384	free(ts, M_TURNSTILE);
385}
386
387/*
388 * Look up the turnstile for a lock in the hash table locking the associated
389 * turnstile chain along the way.  Return with the turnstile chain locked.
390 * If no turnstile is found in the hash table, NULL is returned.
391 */
392struct turnstile *
393turnstile_lookup(struct lock_object *lock)
394{
395	struct turnstile_chain *tc;
396	struct turnstile *ts;
397
398	tc = TC_LOOKUP(lock);
399	mtx_lock_spin(&tc->tc_lock);
400	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
401		if (ts->ts_lockobj == lock)
402			return (ts);
403	return (NULL);
404}
405
406/*
407 * Unlock the turnstile chain associated with a given lock.
408 */
409void
410turnstile_release(struct lock_object *lock)
411{
412	struct turnstile_chain *tc;
413
414	tc = TC_LOOKUP(lock);
415	mtx_unlock_spin(&tc->tc_lock);
416}
417
418/*
419 * Take ownership of a turnstile and adjust the priority of the new
420 * owner appropriately.
421 */
422void
423turnstile_claim(struct turnstile *ts)
424{
425	struct turnstile_chain *tc;
426	struct thread *td, *owner;
427
428	tc = TC_LOOKUP(ts->ts_lockobj);
429	mtx_assert(&tc->tc_lock, MA_OWNED);
430
431	owner = curthread;
432	mtx_lock_spin(&td_contested_lock);
433	turnstile_setowner(ts, owner);
434	mtx_unlock_spin(&td_contested_lock);
435
436	td = TAILQ_FIRST(&ts->ts_blocked);
437	MPASS(td != NULL);
438	MPASS(td->td_proc->p_magic == P_MAGIC);
439	mtx_unlock_spin(&tc->tc_lock);
440
441	/*
442	 * Update the priority of the new owner if needed.
443	 */
444	mtx_lock_spin(&sched_lock);
445	if (td->td_priority < owner->td_priority)
446		owner->td_priority = td->td_priority;
447	mtx_unlock_spin(&sched_lock);
448}
449
450/*
451 * Block the current thread on the turnstile ts.  This function will context
452 * switch and not return until this thread has been woken back up.  This
453 * function must be called with the appropriate turnstile chain locked and
454 * will return with it unlocked.
455 */
456void
457turnstile_wait(struct turnstile *ts, struct lock_object *lock,
458    struct thread *owner)
459{
460	struct turnstile_chain *tc;
461	struct thread *td, *td1;
462
463	td = curthread;
464	tc = TC_LOOKUP(lock);
465	mtx_assert(&tc->tc_lock, MA_OWNED);
466	MPASS(td->td_turnstile != NULL);
467	MPASS(owner != NULL);
468	MPASS(owner->td_proc->p_magic == P_MAGIC);
469
470	/* If the passed in turnstile is NULL, use this thread's turnstile. */
471	if (ts == NULL) {
472#ifdef TURNSTILE_PROFILING
473		tc->tc_depth++;
474		if (tc->tc_depth > tc->tc_max_depth) {
475			tc->tc_max_depth = tc->tc_depth;
476			if (tc->tc_max_depth > turnstile_max_depth)
477				turnstile_max_depth = tc->tc_max_depth;
478		}
479#endif
480		ts = td->td_turnstile;
481		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
482		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
483		    ("thread's turnstile has pending threads"));
484		KASSERT(TAILQ_EMPTY(&ts->ts_blocked),
485		    ("thread's turnstile has a non-empty queue"));
486		KASSERT(LIST_EMPTY(&ts->ts_free),
487		    ("thread's turnstile has a non-empty free list"));
488		KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
489		ts->ts_lockobj = lock;
490		mtx_lock_spin(&td_contested_lock);
491		TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
492		turnstile_setowner(ts, owner);
493		mtx_unlock_spin(&td_contested_lock);
494	} else {
495		TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq)
496			if (td1->td_priority > td->td_priority)
497				break;
498		mtx_lock_spin(&td_contested_lock);
499		if (td1 != NULL)
500			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
501		else
502			TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
503		mtx_unlock_spin(&td_contested_lock);
504		MPASS(td->td_turnstile != NULL);
505		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
506		MPASS(owner == ts->ts_owner);
507	}
508	td->td_turnstile = NULL;
509	mtx_unlock_spin(&tc->tc_lock);
510
511	mtx_lock_spin(&sched_lock);
512	/*
513	 * Handle race condition where a thread on another CPU that owns
514	 * lock 'lock' could have woken us in between us dropping the
515	 * turnstile chain lock and acquiring the sched_lock.
516	 */
517	if (td->td_flags & TDF_TSNOBLOCK) {
518		td->td_flags &= ~TDF_TSNOBLOCK;
519		mtx_unlock_spin(&sched_lock);
520		return;
521	}
522
523#ifdef notyet
524	/*
525	 * If we're borrowing an interrupted thread's VM context, we
526	 * must clean up before going to sleep.
527	 */
528	if (td->td_ithd != NULL) {
529		struct ithd *it = td->td_ithd;
530
531		if (it->it_interrupted) {
532			if (LOCK_LOG_TEST(lock, 0))
533				CTR3(KTR_LOCK, "%s: %p interrupted %p",
534				    __func__, it, it->it_interrupted);
535			intr_thd_fixup(it);
536		}
537	}
538#endif
539
540	/* Save who we are blocked on and switch. */
541	td->td_blocked = ts;
542	td->td_lockname = lock->lo_name;
543	TD_SET_LOCK(td);
544	propagate_priority(td);
545
546	if (LOCK_LOG_TEST(lock, 0))
547		CTR4(KTR_LOCK, "%s: td %p blocked on [%p] %s", __func__, td,
548		    lock, lock->lo_name);
549
550	mi_switch(SW_VOL);
551
552	if (LOCK_LOG_TEST(lock, 0))
553		CTR4(KTR_LOCK, "%s: td %p free from blocked on [%p] %s",
554		    __func__, td, lock, lock->lo_name);
555
556	mtx_unlock_spin(&sched_lock);
557}
558
559/*
560 * Pick the highest priority thread on this turnstile and put it on the
561 * pending list.  This must be called with the turnstile chain locked.
562 */
563int
564turnstile_signal(struct turnstile *ts)
565{
566	struct turnstile_chain *tc;
567	struct thread *td;
568	int empty;
569
570	MPASS(ts != NULL);
571	MPASS(curthread->td_proc->p_magic == P_MAGIC);
572	MPASS(ts->ts_owner == curthread);
573	tc = TC_LOOKUP(ts->ts_lockobj);
574	mtx_assert(&tc->tc_lock, MA_OWNED);
575
576	/*
577	 * Pick the highest priority thread blocked on this lock and
578	 * move it to the pending list.
579	 */
580	td = TAILQ_FIRST(&ts->ts_blocked);
581	MPASS(td->td_proc->p_magic == P_MAGIC);
582	mtx_lock_spin(&td_contested_lock);
583	TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
584	mtx_unlock_spin(&td_contested_lock);
585	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
586
587	/*
588	 * If the turnstile is now empty, remove it from its chain and
589	 * give it to the about-to-be-woken thread.  Otherwise take a
590	 * turnstile from the free list and give it to the thread.
591	 */
592	empty = TAILQ_EMPTY(&ts->ts_blocked);
593	if (empty) {
594		MPASS(LIST_EMPTY(&ts->ts_free));
595#ifdef TURNSTILE_PROFILING
596		tc->tc_depth--;
597#endif
598	} else
599		ts = LIST_FIRST(&ts->ts_free);
600	MPASS(ts != NULL);
601	LIST_REMOVE(ts, ts_hash);
602	td->td_turnstile = ts;
603
604	return (empty);
605}
606
607/*
608 * Put all blocked threads on the pending list.  This must be called with
609 * the turnstile chain locked.
610 */
611void
612turnstile_broadcast(struct turnstile *ts)
613{
614	struct turnstile_chain *tc;
615	struct turnstile *ts1;
616	struct thread *td;
617
618	MPASS(ts != NULL);
619	MPASS(curthread->td_proc->p_magic == P_MAGIC);
620	MPASS(ts->ts_owner == curthread);
621	tc = TC_LOOKUP(ts->ts_lockobj);
622	mtx_assert(&tc->tc_lock, MA_OWNED);
623
624	/*
625	 * Transfer the blocked list to the pending list.
626	 */
627	mtx_lock_spin(&td_contested_lock);
628	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked, td_lockq);
629	mtx_unlock_spin(&td_contested_lock);
630
631	/*
632	 * Give a turnstile to each thread.  The last thread gets
633	 * this turnstile.
634	 */
635	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
636		if (LIST_EMPTY(&ts->ts_free)) {
637			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
638			ts1 = ts;
639#ifdef TURNSTILE_PROFILING
640			tc->tc_depth--;
641#endif
642		} else
643			ts1 = LIST_FIRST(&ts->ts_free);
644		MPASS(ts1 != NULL);
645		LIST_REMOVE(ts1, ts_hash);
646		td->td_turnstile = ts1;
647	}
648}
649
650/*
651 * Wakeup all threads on the pending list and adjust the priority of the
652 * current thread appropriately.  This must be called with the turnstile
653 * chain locked.
654 */
655void
656turnstile_unpend(struct turnstile *ts)
657{
658	TAILQ_HEAD( ,thread) pending_threads;
659	struct turnstile_chain *tc;
660	struct thread *td;
661	int cp, pri;
662
663	MPASS(ts != NULL);
664	MPASS(ts->ts_owner == curthread);
665	tc = TC_LOOKUP(ts->ts_lockobj);
666	mtx_assert(&tc->tc_lock, MA_OWNED);
667	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
668
669	/*
670	 * Move the list of pending threads out of the turnstile and
671	 * into a local variable.
672	 */
673	TAILQ_INIT(&pending_threads);
674	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
675#ifdef INVARIANTS
676	if (TAILQ_EMPTY(&ts->ts_blocked))
677		ts->ts_lockobj = NULL;
678#endif
679
680	/*
681	 * Remove the turnstile from this thread's list of contested locks
682	 * since this thread doesn't own it anymore.  New threads will
683	 * not be blocking on the turnstile until it is claimed by a new
684	 * owner.
685	 */
686	mtx_lock_spin(&td_contested_lock);
687	ts->ts_owner = NULL;
688	LIST_REMOVE(ts, ts_link);
689	mtx_unlock_spin(&td_contested_lock);
690	mtx_unlock_spin(&tc->tc_lock);
691
692	/*
693	 * Adjust the priority of curthread based on other contested
694	 * locks it owns.  Don't lower the priority below the base
695	 * priority however.
696	 */
697	td = curthread;
698	pri = PRI_MAX;
699	mtx_lock_spin(&sched_lock);
700	mtx_lock_spin(&td_contested_lock);
701	LIST_FOREACH(ts, &td->td_contested, ts_link) {
702		cp = TAILQ_FIRST(&ts->ts_blocked)->td_priority;
703		if (cp < pri)
704			pri = cp;
705	}
706	mtx_unlock_spin(&td_contested_lock);
707	if (pri > td->td_base_pri)
708		pri = td->td_base_pri;
709	td->td_priority = pri;
710
711	/*
712	 * Wake up all the pending threads.  If a thread is not blocked
713	 * on a lock, then it is currently executing on another CPU in
714	 * turnstile_wait() or sitting on a run queue waiting to resume
715	 * in turnstile_wait().  Set a flag to force it to try to acquire
716	 * the lock again instead of blocking.
717	 */
718	while (!TAILQ_EMPTY(&pending_threads)) {
719		td = TAILQ_FIRST(&pending_threads);
720		TAILQ_REMOVE(&pending_threads, td, td_lockq);
721		MPASS(td->td_proc->p_magic == P_MAGIC);
722		if (TD_ON_LOCK(td)) {
723			td->td_blocked = NULL;
724			td->td_lockname = NULL;
725			TD_CLR_LOCK(td);
726			MPASS(TD_CAN_RUN(td));
727			setrunqueue(td);
728		} else {
729			td->td_flags |= TDF_TSNOBLOCK;
730			MPASS(TD_IS_RUNNING(td) || TD_ON_RUNQ(td));
731		}
732	}
733	mtx_unlock_spin(&sched_lock);
734}
735
736/*
737 * Return the first thread in a turnstile.
738 */
739struct thread *
740turnstile_head(struct turnstile *ts)
741{
742#ifdef INVARIANTS
743	struct turnstile_chain *tc;
744
745	MPASS(ts != NULL);
746	tc = TC_LOOKUP(ts->ts_lockobj);
747	mtx_assert(&tc->tc_lock, MA_OWNED);
748#endif
749	return (TAILQ_FIRST(&ts->ts_blocked));
750}
751
752/*
753 * Returns true if a turnstile is empty.
754 */
755int
756turnstile_empty(struct turnstile *ts)
757{
758#ifdef INVARIANTS
759	struct turnstile_chain *tc;
760
761	MPASS(ts != NULL);
762	tc = TC_LOOKUP(ts->ts_lockobj);
763	mtx_assert(&tc->tc_lock, MA_OWNED);
764#endif
765	return (TAILQ_EMPTY(&ts->ts_blocked));
766}
767