2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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
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 $
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.
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.
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.
59 #include "opt_turnstile_profiling.h"
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/mutex.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>
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.
84 #define TC_TABLESIZE 128 /* Must be power of 2. */
85 #define TC_MASK (TC_TABLESIZE - 1)
87 #define TC_HASH(lock) (((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
88 #define TC_LOOKUP(lock) &turnstile_chains[TC_HASH(lock)]
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.
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
106 * c - turnstile chain lock
107 * q - td_contested lock
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. */
119 struct 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. */
128 #ifdef TURNSTILE_PROFILING
129 u_int turnstile_max_depth;
130 SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0, "turnstile profiling");
131 SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
132 "turnstile chain stats");
133 SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
134 &turnstile_max_depth, 0, "maxmimum depth achieved of a single chain");
136 static struct mtx td_contested_lock;
137 static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
139 static MALLOC_DEFINE(M_TURNSTILE, "turnstiles", "turnstiles");
142 * Prototypes for non-exported routines.
144 static void init_turnstile0(void *dummy);
145 #ifdef TURNSTILE_PROFILING
146 static void init_turnstile_profiling(void *arg);
148 static void propagate_priority(struct thread *td);
149 static int turnstile_adjust_thread(struct turnstile *ts,
151 static void turnstile_setowner(struct turnstile *ts, struct thread *owner);
154 * Walks the chain of turnstiles and their owners to propagate the priority
155 * of the thread being blocked to all the threads holding locks that have to
156 * release their locks before this thread can run again.
159 propagate_priority(struct thread *td)
161 struct turnstile_chain *tc;
162 struct turnstile *ts;
165 mtx_assert(&sched_lock, MA_OWNED);
166 pri = td->td_priority;
173 * This really isn't quite right. Really
174 * ought to bump priority of thread that
175 * next acquires the lock.
180 MPASS(td->td_proc != NULL);
181 MPASS(td->td_proc->p_magic == P_MAGIC);
184 * XXX: The owner of a turnstile can be stale if it is the
185 * first thread to grab a slock of a sx lock. In that case
186 * it is possible for us to be at SSLEEP or some other
187 * weird state. We should probably just return if the state
188 * isn't SRUN or SLOCK.
190 KASSERT(!TD_IS_SLEEPING(td),
191 ("sleeping thread (tid %d) owns a non-sleepable lock",
195 * If this thread already has higher priority than the
196 * thread that is being blocked, we are finished.
198 if (td->td_priority <= pri)
202 * Bump this thread's priority.
204 sched_lend_prio(td, pri);
207 * If lock holder is actually running or on the run queue
210 if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
211 MPASS(td->td_blocked == NULL);
217 * For UP, we check to see if td is curthread (this shouldn't
218 * ever happen however as it would mean we are in a deadlock.)
220 KASSERT(td != curthread, ("Deadlock detected"));
224 * If we aren't blocked on a lock, we should be.
226 KASSERT(TD_ON_LOCK(td), (
227 "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
228 td->td_tid, td->td_proc->p_comm, td->td_state,
229 ts->ts_lockobj->lo_name));
232 * Pick up the lock that td is blocked on.
236 tc = TC_LOOKUP(ts->ts_lockobj);
237 mtx_lock_spin(&tc->tc_lock);
239 /* Resort td on the list if needed. */
240 if (!turnstile_adjust_thread(ts, td)) {
241 mtx_unlock_spin(&tc->tc_lock);
244 mtx_unlock_spin(&tc->tc_lock);
249 * Adjust the thread's position on a turnstile after its priority has been
253 turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
255 struct turnstile_chain *tc;
256 struct thread *td1, *td2;
258 mtx_assert(&sched_lock, MA_OWNED);
259 MPASS(TD_ON_LOCK(td));
262 * This thread may not be blocked on this turnstile anymore
263 * but instead might already be woken up on another CPU
264 * that is waiting on sched_lock in turnstile_unpend() to
265 * finish waking this thread up. We can detect this case
266 * by checking to see if this thread has been given a
267 * turnstile by either turnstile_signal() or
268 * turnstile_broadcast(). In this case, treat the thread as
269 * if it was already running.
271 if (td->td_turnstile != NULL)
275 * Check if the thread needs to be moved on the blocked chain.
276 * It needs to be moved if either its priority is lower than
277 * the previous thread or higher than the next thread.
279 tc = TC_LOOKUP(ts->ts_lockobj);
280 mtx_assert(&tc->tc_lock, MA_OWNED);
281 td1 = TAILQ_PREV(td, threadqueue, td_lockq);
282 td2 = TAILQ_NEXT(td, td_lockq);
283 if ((td1 != NULL && td->td_priority < td1->td_priority) ||
284 (td2 != NULL && td->td_priority > td2->td_priority)) {
287 * Remove thread from blocked chain and determine where
288 * it should be moved to.
290 mtx_lock_spin(&td_contested_lock);
291 TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
292 TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq) {
293 MPASS(td1->td_proc->p_magic == P_MAGIC);
294 if (td1->td_priority > td->td_priority)
299 TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
301 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
302 mtx_unlock_spin(&td_contested_lock);
305 "turnstile_adjust_thread: td %d put at tail on [%p] %s",
306 td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
309 "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
310 td->td_tid, td1->td_tid, ts->ts_lockobj,
311 ts->ts_lockobj->lo_name);
317 * Early initialization of turnstiles. This is not done via a SYSINIT()
318 * since this needs to be initialized very early when mutexes are first
322 init_turnstiles(void)
326 for (i = 0; i < TC_TABLESIZE; i++) {
327 LIST_INIT(&turnstile_chains[i].tc_turnstiles);
328 mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
331 mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
332 thread0.td_turnstile = NULL;
335 #ifdef TURNSTILE_PROFILING
337 init_turnstile_profiling(void *arg)
339 struct sysctl_oid *chain_oid;
343 for (i = 0; i < TC_TABLESIZE; i++) {
344 snprintf(chain_name, sizeof(chain_name), "%d", i);
345 chain_oid = SYSCTL_ADD_NODE(NULL,
346 SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
347 chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
348 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
349 "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
351 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
352 "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
356 SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
357 init_turnstile_profiling, NULL);
361 init_turnstile0(void *dummy)
364 thread0.td_turnstile = turnstile_alloc();
366 SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
369 * Update a thread on the turnstile list after it's priority has been changed.
370 * The old priority is passed in as an argument.
373 turnstile_adjust(struct thread *td, u_char oldpri)
375 struct turnstile_chain *tc;
376 struct turnstile *ts;
378 mtx_assert(&sched_lock, MA_OWNED);
379 MPASS(TD_ON_LOCK(td));
382 * Pick up the lock that td is blocked on.
386 tc = TC_LOOKUP(ts->ts_lockobj);
387 mtx_lock_spin(&tc->tc_lock);
389 /* Resort the turnstile on the list. */
390 if (!turnstile_adjust_thread(ts, td)) {
391 mtx_unlock_spin(&tc->tc_lock);
396 * If our priority was lowered and we are at the head of the
397 * turnstile, then propagate our new priority up the chain.
398 * Note that we currently don't try to revoke lent priorities
399 * when our priority goes up.
401 if (td == TAILQ_FIRST(&ts->ts_blocked) && td->td_priority < oldpri) {
402 mtx_unlock_spin(&tc->tc_lock);
403 propagate_priority(td);
405 mtx_unlock_spin(&tc->tc_lock);
409 * Set the owner of the lock this turnstile is attached to.
412 turnstile_setowner(struct turnstile *ts, struct thread *owner)
415 mtx_assert(&td_contested_lock, MA_OWNED);
416 MPASS(owner->td_proc->p_magic == P_MAGIC);
417 MPASS(ts->ts_owner == NULL);
418 ts->ts_owner = owner;
419 LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
423 * Malloc a turnstile for a new thread, initialize it and return it.
426 turnstile_alloc(void)
428 struct turnstile *ts;
430 ts = malloc(sizeof(struct turnstile), M_TURNSTILE, M_WAITOK | M_ZERO);
431 TAILQ_INIT(&ts->ts_blocked);
432 TAILQ_INIT(&ts->ts_pending);
433 LIST_INIT(&ts->ts_free);
438 * Free a turnstile when a thread is destroyed.
441 turnstile_free(struct turnstile *ts)
445 MPASS(TAILQ_EMPTY(&ts->ts_blocked));
446 MPASS(TAILQ_EMPTY(&ts->ts_pending));
447 free(ts, M_TURNSTILE);
451 * Lock the turnstile chain associated with the specified lock.
454 turnstile_lock(struct lock_object *lock)
456 struct turnstile_chain *tc;
458 tc = TC_LOOKUP(lock);
459 mtx_lock_spin(&tc->tc_lock);
463 * Look up the turnstile for a lock in the hash table locking the associated
464 * turnstile chain along the way. If no turnstile is found in the hash
465 * table, NULL is returned.
468 turnstile_lookup(struct lock_object *lock)
470 struct turnstile_chain *tc;
471 struct turnstile *ts;
473 tc = TC_LOOKUP(lock);
474 mtx_assert(&tc->tc_lock, MA_OWNED);
475 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
476 if (ts->ts_lockobj == lock)
482 * Unlock the turnstile chain associated with a given lock.
485 turnstile_release(struct lock_object *lock)
487 struct turnstile_chain *tc;
489 tc = TC_LOOKUP(lock);
490 mtx_unlock_spin(&tc->tc_lock);
494 * Take ownership of a turnstile and adjust the priority of the new
495 * owner appropriately.
498 turnstile_claim(struct lock_object *lock)
500 struct turnstile_chain *tc;
501 struct turnstile *ts;
502 struct thread *td, *owner;
504 tc = TC_LOOKUP(lock);
505 mtx_assert(&tc->tc_lock, MA_OWNED);
506 ts = turnstile_lookup(lock);
510 mtx_lock_spin(&td_contested_lock);
511 turnstile_setowner(ts, owner);
512 mtx_unlock_spin(&td_contested_lock);
514 td = TAILQ_FIRST(&ts->ts_blocked);
516 MPASS(td->td_proc->p_magic == P_MAGIC);
517 mtx_unlock_spin(&tc->tc_lock);
520 * Update the priority of the new owner if needed.
522 mtx_lock_spin(&sched_lock);
523 if (td->td_priority < owner->td_priority)
524 sched_lend_prio(owner, td->td_priority);
525 mtx_unlock_spin(&sched_lock);
529 * Block the current thread on the turnstile assicated with 'lock'. This
530 * function will context switch and not return until this thread has been
531 * woken back up. This function must be called with the appropriate
532 * turnstile chain locked and will return with it unlocked.
535 turnstile_wait(struct lock_object *lock, struct thread *owner)
537 struct turnstile_chain *tc;
538 struct turnstile *ts;
539 struct thread *td, *td1;
542 tc = TC_LOOKUP(lock);
543 mtx_assert(&tc->tc_lock, MA_OWNED);
544 MPASS(td->td_turnstile != NULL);
545 MPASS(owner != NULL);
546 MPASS(owner->td_proc->p_magic == P_MAGIC);
548 /* Look up the turnstile associated with the lock 'lock'. */
549 ts = turnstile_lookup(lock);
552 * If the lock does not already have a turnstile, use this thread's
553 * turnstile. Otherwise insert the current thread into the
554 * turnstile already in use by this lock.
557 #ifdef TURNSTILE_PROFILING
559 if (tc->tc_depth > tc->tc_max_depth) {
560 tc->tc_max_depth = tc->tc_depth;
561 if (tc->tc_max_depth > turnstile_max_depth)
562 turnstile_max_depth = tc->tc_max_depth;
565 ts = td->td_turnstile;
566 LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
567 KASSERT(TAILQ_EMPTY(&ts->ts_pending),
568 ("thread's turnstile has pending threads"));
569 KASSERT(TAILQ_EMPTY(&ts->ts_blocked),
570 ("thread's turnstile has a non-empty queue"));
571 KASSERT(LIST_EMPTY(&ts->ts_free),
572 ("thread's turnstile has a non-empty free list"));
573 KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
574 ts->ts_lockobj = lock;
575 mtx_lock_spin(&td_contested_lock);
576 TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
577 turnstile_setowner(ts, owner);
578 mtx_unlock_spin(&td_contested_lock);
580 TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq)
581 if (td1->td_priority > td->td_priority)
583 mtx_lock_spin(&td_contested_lock);
585 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
587 TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
588 mtx_unlock_spin(&td_contested_lock);
589 MPASS(td->td_turnstile != NULL);
590 LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
591 MPASS(owner == ts->ts_owner);
593 td->td_turnstile = NULL;
594 mtx_unlock_spin(&tc->tc_lock);
596 mtx_lock_spin(&sched_lock);
598 * Handle race condition where a thread on another CPU that owns
599 * lock 'lock' could have woken us in between us dropping the
600 * turnstile chain lock and acquiring the sched_lock.
602 if (td->td_flags & TDF_TSNOBLOCK) {
603 td->td_flags &= ~TDF_TSNOBLOCK;
604 mtx_unlock_spin(&sched_lock);
610 * If we're borrowing an interrupted thread's VM context, we
611 * must clean up before going to sleep.
613 if (td->td_ithd != NULL) {
614 struct ithd *it = td->td_ithd;
616 if (it->it_interrupted) {
617 if (LOCK_LOG_TEST(lock, 0))
618 CTR3(KTR_LOCK, "%s: %p interrupted %p",
619 __func__, it, it->it_interrupted);
625 /* Save who we are blocked on and switch. */
627 td->td_lockname = lock->lo_name;
629 propagate_priority(td);
631 if (LOCK_LOG_TEST(lock, 0))
632 CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
633 td->td_tid, lock, lock->lo_name);
635 mi_switch(SW_VOL, NULL);
637 if (LOCK_LOG_TEST(lock, 0))
638 CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
639 __func__, td->td_tid, lock, lock->lo_name);
641 mtx_unlock_spin(&sched_lock);
645 * Pick the highest priority thread on this turnstile and put it on the
646 * pending list. This must be called with the turnstile chain locked.
649 turnstile_signal(struct turnstile *ts)
651 struct turnstile_chain *tc;
656 MPASS(curthread->td_proc->p_magic == P_MAGIC);
657 MPASS(ts->ts_owner == curthread);
658 tc = TC_LOOKUP(ts->ts_lockobj);
659 mtx_assert(&tc->tc_lock, MA_OWNED);
662 * Pick the highest priority thread blocked on this lock and
663 * move it to the pending list.
665 td = TAILQ_FIRST(&ts->ts_blocked);
666 MPASS(td->td_proc->p_magic == P_MAGIC);
667 mtx_lock_spin(&td_contested_lock);
668 TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
669 mtx_unlock_spin(&td_contested_lock);
670 TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
673 * If the turnstile is now empty, remove it from its chain and
674 * give it to the about-to-be-woken thread. Otherwise take a
675 * turnstile from the free list and give it to the thread.
677 empty = TAILQ_EMPTY(&ts->ts_blocked);
679 MPASS(LIST_EMPTY(&ts->ts_free));
680 #ifdef TURNSTILE_PROFILING
684 ts = LIST_FIRST(&ts->ts_free);
686 LIST_REMOVE(ts, ts_hash);
687 td->td_turnstile = ts;
693 * Put all blocked threads on the pending list. This must be called with
694 * the turnstile chain locked.
697 turnstile_broadcast(struct turnstile *ts)
699 struct turnstile_chain *tc;
700 struct turnstile *ts1;
704 MPASS(curthread->td_proc->p_magic == P_MAGIC);
705 MPASS(ts->ts_owner == curthread);
706 tc = TC_LOOKUP(ts->ts_lockobj);
707 mtx_assert(&tc->tc_lock, MA_OWNED);
710 * Transfer the blocked list to the pending list.
712 mtx_lock_spin(&td_contested_lock);
713 TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked, td_lockq);
714 mtx_unlock_spin(&td_contested_lock);
717 * Give a turnstile to each thread. The last thread gets
720 TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
721 if (LIST_EMPTY(&ts->ts_free)) {
722 MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
724 #ifdef TURNSTILE_PROFILING
728 ts1 = LIST_FIRST(&ts->ts_free);
730 LIST_REMOVE(ts1, ts_hash);
731 td->td_turnstile = ts1;
736 * Wakeup all threads on the pending list and adjust the priority of the
737 * current thread appropriately. This must be called with the turnstile
741 turnstile_unpend(struct turnstile *ts)
743 TAILQ_HEAD( ,thread) pending_threads;
744 struct turnstile_chain *tc;
749 MPASS(ts->ts_owner == curthread);
750 tc = TC_LOOKUP(ts->ts_lockobj);
751 mtx_assert(&tc->tc_lock, MA_OWNED);
752 MPASS(!TAILQ_EMPTY(&ts->ts_pending));
755 * Move the list of pending threads out of the turnstile and
756 * into a local variable.
758 TAILQ_INIT(&pending_threads);
759 TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
761 if (TAILQ_EMPTY(&ts->ts_blocked))
762 ts->ts_lockobj = NULL;
766 * Remove the turnstile from this thread's list of contested locks
767 * since this thread doesn't own it anymore. New threads will
768 * not be blocking on the turnstile until it is claimed by a new
771 mtx_lock_spin(&td_contested_lock);
773 LIST_REMOVE(ts, ts_link);
774 mtx_unlock_spin(&td_contested_lock);
776 mtx_unlock_spin(&tc->tc_lock);
779 * Adjust the priority of curthread based on other contested
780 * locks it owns. Don't lower the priority below the base
785 mtx_lock_spin(&sched_lock);
786 mtx_lock_spin(&td_contested_lock);
787 LIST_FOREACH(ts, &td->td_contested, ts_link) {
788 cp = TAILQ_FIRST(&ts->ts_blocked)->td_priority;
792 mtx_unlock_spin(&td_contested_lock);
793 sched_unlend_prio(td, pri);
796 * Wake up all the pending threads. If a thread is not blocked
797 * on a lock, then it is currently executing on another CPU in
798 * turnstile_wait() or sitting on a run queue waiting to resume
799 * in turnstile_wait(). Set a flag to force it to try to acquire
800 * the lock again instead of blocking.
802 while (!TAILQ_EMPTY(&pending_threads)) {
803 td = TAILQ_FIRST(&pending_threads);
804 TAILQ_REMOVE(&pending_threads, td, td_lockq);
805 MPASS(td->td_proc->p_magic == P_MAGIC);
806 if (TD_ON_LOCK(td)) {
807 td->td_blocked = NULL;
808 td->td_lockname = NULL;
810 MPASS(TD_CAN_RUN(td));
811 setrunqueue(td, SRQ_BORING);
813 td->td_flags |= TDF_TSNOBLOCK;
814 MPASS(TD_IS_RUNNING(td) || TD_ON_RUNQ(td));
818 mtx_unlock_spin(&sched_lock);
822 * Return the first thread in a turnstile.
825 turnstile_head(struct turnstile *ts)
828 struct turnstile_chain *tc;
831 tc = TC_LOOKUP(ts->ts_lockobj);
832 mtx_assert(&tc->tc_lock, MA_OWNED);
834 return (TAILQ_FIRST(&ts->ts_blocked));
838 * Returns true if a turnstile is empty.
841 turnstile_empty(struct turnstile *ts)
844 struct turnstile_chain *tc;
847 tc = TC_LOOKUP(ts->ts_lockobj);
848 mtx_assert(&tc->tc_lock, MA_OWNED);
850 return (TAILQ_EMPTY(&ts->ts_blocked));