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/sched.h>
74 #include <sys/sysctl.h>
75 #include <sys/turnstile.h>
78 * Constants for the hash table of turnstile chains. TC_SHIFT is a magic
79 * number chosen because the sleep queue's use the same value for the
80 * shift. Basically, we ignore the lower 8 bits of the address.
81 * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
83 #define TC_TABLESIZE 128 /* Must be power of 2. */
84 #define TC_MASK (TC_TABLESIZE - 1)
86 #define TC_HASH(lock) (((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
87 #define TC_LOOKUP(lock) &turnstile_chains[TC_HASH(lock)]
90 * There are three different lists of turnstiles as follows. The list
91 * connected by ts_link entries is a per-thread list of all the turnstiles
92 * attached to locks that we own. This is used to fixup our priority when
93 * a lock is released. The other two lists use the ts_hash entries. The
94 * first of these two is the turnstile chain list that a turnstile is on
95 * when it is attached to a lock. The second list to use ts_hash is the
96 * free list hung off of a turnstile that is attached to a lock.
98 * Each turnstile contains two lists of threads. The ts_blocked list is
99 * a linked list of threads blocked on the turnstile's lock. The
100 * ts_pending list is a linked list of threads previously awakened by
101 * turnstile_signal() or turnstile_wait() that are waiting to be put on
105 * c - turnstile chain lock
106 * q - td_contested lock
109 TAILQ_HEAD(, thread) ts_blocked; /* (c + q) Blocked threads. */
110 TAILQ_HEAD(, thread) ts_pending; /* (c) Pending threads. */
111 LIST_ENTRY(turnstile) ts_hash; /* (c) Chain and free list. */
112 LIST_ENTRY(turnstile) ts_link; /* (q) Contested locks. */
113 LIST_HEAD(, turnstile) ts_free; /* (c) Free turnstiles. */
114 struct lock_object *ts_lockobj; /* (c) Lock we reference. */
115 struct thread *ts_owner; /* (c + q) Who owns the lock. */
118 struct turnstile_chain {
119 LIST_HEAD(, turnstile) tc_turnstiles; /* List of turnstiles. */
120 struct mtx tc_lock; /* Spin lock for this chain. */
121 #ifdef TURNSTILE_PROFILING
122 u_int tc_depth; /* Length of tc_queues. */
123 u_int tc_max_depth; /* Max length of tc_queues. */
127 #ifdef TURNSTILE_PROFILING
128 u_int turnstile_max_depth;
129 SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0, "turnstile profiling");
130 SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
131 "turnstile chain stats");
132 SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
133 &turnstile_max_depth, 0, "maxmimum depth achieved of a single chain");
135 static struct mtx td_contested_lock;
136 static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
138 static MALLOC_DEFINE(M_TURNSTILE, "turnstiles", "turnstiles");
141 * Prototypes for non-exported routines.
143 static void init_turnstile0(void *dummy);
144 #ifdef TURNSTILE_PROFILING
145 static void init_turnstile_profiling(void *arg);
147 static void propagate_priority(struct thread *td);
148 static int turnstile_adjust_thread(struct turnstile *ts,
150 static void turnstile_setowner(struct turnstile *ts, struct thread *owner);
153 * Walks the chain of turnstiles and their owners to propagate the priority
154 * of the thread being blocked to all the threads holding locks that have to
155 * release their locks before this thread can run again.
158 propagate_priority(struct thread *td)
160 struct turnstile_chain *tc;
161 struct turnstile *ts;
164 mtx_assert(&sched_lock, MA_OWNED);
165 pri = td->td_priority;
172 * This really isn't quite right. Really
173 * ought to bump priority of thread that
174 * next acquires the lock.
179 MPASS(td->td_proc != NULL);
180 MPASS(td->td_proc->p_magic == P_MAGIC);
183 * XXX: The owner of a turnstile can be stale if it is the
184 * first thread to grab a slock of a sx lock. In that case
185 * it is possible for us to be at SSLEEP or some other
186 * weird state. We should probably just return if the state
187 * isn't SRUN or SLOCK.
189 KASSERT(!TD_IS_SLEEPING(td),
190 ("sleeping thread (tid %d) owns a non-sleepable lock",
194 * If this thread already has higher priority than the
195 * thread that is being blocked, we are finished.
197 if (td->td_priority <= pri)
201 * Bump this thread's priority.
203 sched_lend_prio(td, pri);
206 * If lock holder is actually running or on the run queue
209 if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
210 MPASS(td->td_blocked == NULL);
216 * For UP, we check to see if td is curthread (this shouldn't
217 * ever happen however as it would mean we are in a deadlock.)
219 KASSERT(td != curthread, ("Deadlock detected"));
223 * If we aren't blocked on a lock, we should be.
225 KASSERT(TD_ON_LOCK(td), (
226 "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
227 td->td_tid, td->td_proc->p_comm, td->td_state,
228 ts->ts_lockobj->lo_name));
231 * Pick up the lock that td is blocked on.
235 tc = TC_LOOKUP(ts->ts_lockobj);
236 mtx_lock_spin(&tc->tc_lock);
238 /* Resort td on the list if needed. */
239 if (!turnstile_adjust_thread(ts, td)) {
240 mtx_unlock_spin(&tc->tc_lock);
243 mtx_unlock_spin(&tc->tc_lock);
248 * Adjust the thread's position on a turnstile after its priority has been
252 turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
254 struct turnstile_chain *tc;
255 struct thread *td1, *td2;
257 mtx_assert(&sched_lock, MA_OWNED);
258 MPASS(TD_ON_LOCK(td));
261 * This thread may not be blocked on this turnstile anymore
262 * but instead might already be woken up on another CPU
263 * that is waiting on sched_lock in turnstile_unpend() to
264 * finish waking this thread up. We can detect this case
265 * by checking to see if this thread has been given a
266 * turnstile by either turnstile_signal() or
267 * turnstile_broadcast(). In this case, treat the thread as
268 * if it was already running.
270 if (td->td_turnstile != NULL)
274 * Check if the thread needs to be moved on the blocked chain.
275 * It needs to be moved if either its priority is lower than
276 * the previous thread or higher than the next thread.
278 tc = TC_LOOKUP(ts->ts_lockobj);
279 mtx_assert(&tc->tc_lock, MA_OWNED);
280 td1 = TAILQ_PREV(td, threadqueue, td_lockq);
281 td2 = TAILQ_NEXT(td, td_lockq);
282 if ((td1 != NULL && td->td_priority < td1->td_priority) ||
283 (td2 != NULL && td->td_priority > td2->td_priority)) {
286 * Remove thread from blocked chain and determine where
287 * it should be moved to.
289 mtx_lock_spin(&td_contested_lock);
290 TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
291 TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq) {
292 MPASS(td1->td_proc->p_magic == P_MAGIC);
293 if (td1->td_priority > td->td_priority)
298 TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
300 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
301 mtx_unlock_spin(&td_contested_lock);
304 "turnstile_adjust_thread: td %d put at tail on [%p] %s",
305 td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
308 "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
309 td->td_tid, td1->td_tid, ts->ts_lockobj,
310 ts->ts_lockobj->lo_name);
316 * Early initialization of turnstiles. This is not done via a SYSINIT()
317 * since this needs to be initialized very early when mutexes are first
321 init_turnstiles(void)
325 for (i = 0; i < TC_TABLESIZE; i++) {
326 LIST_INIT(&turnstile_chains[i].tc_turnstiles);
327 mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
330 mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
331 thread0.td_turnstile = NULL;
334 #ifdef TURNSTILE_PROFILING
336 init_turnstile_profiling(void *arg)
338 struct sysctl_oid *chain_oid;
342 for (i = 0; i < TC_TABLESIZE; i++) {
343 snprintf(chain_name, sizeof(chain_name), "%d", i);
344 chain_oid = SYSCTL_ADD_NODE(NULL,
345 SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
346 chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
347 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
348 "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
350 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
351 "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
355 SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
356 init_turnstile_profiling, NULL);
360 init_turnstile0(void *dummy)
363 thread0.td_turnstile = turnstile_alloc();
365 SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
368 * Update a thread on the turnstile list after it's priority has been changed.
369 * The old priority is passed in as an argument.
372 turnstile_adjust(struct thread *td, u_char oldpri)
374 struct turnstile_chain *tc;
375 struct turnstile *ts;
377 mtx_assert(&sched_lock, MA_OWNED);
378 MPASS(TD_ON_LOCK(td));
381 * Pick up the lock that td is blocked on.
385 tc = TC_LOOKUP(ts->ts_lockobj);
386 mtx_lock_spin(&tc->tc_lock);
388 /* Resort the turnstile on the list. */
389 if (!turnstile_adjust_thread(ts, td)) {
390 mtx_unlock_spin(&tc->tc_lock);
395 * If our priority was lowered and we are at the head of the
396 * turnstile, then propagate our new priority up the chain.
397 * Note that we currently don't try to revoke lent priorities
398 * when our priority goes up.
400 if (td == TAILQ_FIRST(&ts->ts_blocked) && td->td_priority < oldpri) {
401 mtx_unlock_spin(&tc->tc_lock);
402 propagate_priority(td);
404 mtx_unlock_spin(&tc->tc_lock);
408 * Set the owner of the lock this turnstile is attached to.
411 turnstile_setowner(struct turnstile *ts, struct thread *owner)
414 mtx_assert(&td_contested_lock, MA_OWNED);
415 MPASS(owner->td_proc->p_magic == P_MAGIC);
416 MPASS(ts->ts_owner == NULL);
417 ts->ts_owner = owner;
418 LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
422 * Malloc a turnstile for a new thread, initialize it and return it.
425 turnstile_alloc(void)
427 struct turnstile *ts;
429 ts = malloc(sizeof(struct turnstile), M_TURNSTILE, M_WAITOK | M_ZERO);
430 TAILQ_INIT(&ts->ts_blocked);
431 TAILQ_INIT(&ts->ts_pending);
432 LIST_INIT(&ts->ts_free);
437 * Free a turnstile when a thread is destroyed.
440 turnstile_free(struct turnstile *ts)
444 MPASS(TAILQ_EMPTY(&ts->ts_blocked));
445 MPASS(TAILQ_EMPTY(&ts->ts_pending));
446 free(ts, M_TURNSTILE);
450 * Lock the turnstile chain associated with the specified lock.
453 turnstile_lock(struct lock_object *lock)
455 struct turnstile_chain *tc;
457 tc = TC_LOOKUP(lock);
458 mtx_lock_spin(&tc->tc_lock);
462 * Look up the turnstile for a lock in the hash table locking the associated
463 * turnstile chain along the way. If no turnstile is found in the hash
464 * table, NULL is returned.
467 turnstile_lookup(struct lock_object *lock)
469 struct turnstile_chain *tc;
470 struct turnstile *ts;
472 tc = TC_LOOKUP(lock);
473 mtx_assert(&tc->tc_lock, MA_OWNED);
474 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
475 if (ts->ts_lockobj == lock)
481 * Unlock the turnstile chain associated with a given lock.
484 turnstile_release(struct lock_object *lock)
486 struct turnstile_chain *tc;
488 tc = TC_LOOKUP(lock);
489 mtx_unlock_spin(&tc->tc_lock);
493 * Take ownership of a turnstile and adjust the priority of the new
494 * owner appropriately.
497 turnstile_claim(struct lock_object *lock)
499 struct turnstile_chain *tc;
500 struct turnstile *ts;
501 struct thread *td, *owner;
503 tc = TC_LOOKUP(lock);
504 mtx_assert(&tc->tc_lock, MA_OWNED);
505 ts = turnstile_lookup(lock);
509 mtx_lock_spin(&td_contested_lock);
510 turnstile_setowner(ts, owner);
511 mtx_unlock_spin(&td_contested_lock);
513 td = TAILQ_FIRST(&ts->ts_blocked);
515 MPASS(td->td_proc->p_magic == P_MAGIC);
516 mtx_unlock_spin(&tc->tc_lock);
519 * Update the priority of the new owner if needed.
521 mtx_lock_spin(&sched_lock);
522 if (td->td_priority < owner->td_priority)
523 sched_lend_prio(owner, td->td_priority);
524 mtx_unlock_spin(&sched_lock);
528 * Block the current thread on the turnstile assicated with 'lock'. This
529 * function will context switch and not return until this thread has been
530 * woken back up. This function must be called with the appropriate
531 * turnstile chain locked and will return with it unlocked.
534 turnstile_wait(struct lock_object *lock, struct thread *owner)
536 struct turnstile_chain *tc;
537 struct turnstile *ts;
538 struct thread *td, *td1;
541 tc = TC_LOOKUP(lock);
542 mtx_assert(&tc->tc_lock, MA_OWNED);
543 MPASS(td->td_turnstile != NULL);
544 MPASS(owner != NULL);
545 MPASS(owner->td_proc->p_magic == P_MAGIC);
547 /* Look up the turnstile associated with the lock 'lock'. */
548 ts = turnstile_lookup(lock);
551 * If the lock does not already have a turnstile, use this thread's
552 * turnstile. Otherwise insert the current thread into the
553 * turnstile already in use by this lock.
556 #ifdef TURNSTILE_PROFILING
558 if (tc->tc_depth > tc->tc_max_depth) {
559 tc->tc_max_depth = tc->tc_depth;
560 if (tc->tc_max_depth > turnstile_max_depth)
561 turnstile_max_depth = tc->tc_max_depth;
564 ts = td->td_turnstile;
565 LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
566 KASSERT(TAILQ_EMPTY(&ts->ts_pending),
567 ("thread's turnstile has pending threads"));
568 KASSERT(TAILQ_EMPTY(&ts->ts_blocked),
569 ("thread's turnstile has a non-empty queue"));
570 KASSERT(LIST_EMPTY(&ts->ts_free),
571 ("thread's turnstile has a non-empty free list"));
572 KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
573 ts->ts_lockobj = lock;
574 mtx_lock_spin(&td_contested_lock);
575 TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
576 turnstile_setowner(ts, owner);
577 mtx_unlock_spin(&td_contested_lock);
579 TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq)
580 if (td1->td_priority > td->td_priority)
582 mtx_lock_spin(&td_contested_lock);
584 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
586 TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
587 mtx_unlock_spin(&td_contested_lock);
588 MPASS(td->td_turnstile != NULL);
589 LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
590 MPASS(owner == ts->ts_owner);
592 td->td_turnstile = NULL;
593 mtx_unlock_spin(&tc->tc_lock);
595 mtx_lock_spin(&sched_lock);
597 * Handle race condition where a thread on another CPU that owns
598 * lock 'lock' could have woken us in between us dropping the
599 * turnstile chain lock and acquiring the sched_lock.
601 if (td->td_flags & TDF_TSNOBLOCK) {
602 td->td_flags &= ~TDF_TSNOBLOCK;
603 mtx_unlock_spin(&sched_lock);
609 * If we're borrowing an interrupted thread's VM context, we
610 * must clean up before going to sleep.
612 if (td->td_ithd != NULL) {
613 struct ithd *it = td->td_ithd;
615 if (it->it_interrupted) {
616 if (LOCK_LOG_TEST(lock, 0))
617 CTR3(KTR_LOCK, "%s: %p interrupted %p",
618 __func__, it, it->it_interrupted);
624 /* Save who we are blocked on and switch. */
626 td->td_lockname = lock->lo_name;
628 propagate_priority(td);
630 if (LOCK_LOG_TEST(lock, 0))
631 CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
632 td->td_tid, lock, lock->lo_name);
634 mi_switch(SW_VOL, NULL);
636 if (LOCK_LOG_TEST(lock, 0))
637 CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
638 __func__, td->td_tid, lock, lock->lo_name);
640 mtx_unlock_spin(&sched_lock);
644 * Pick the highest priority thread on this turnstile and put it on the
645 * pending list. This must be called with the turnstile chain locked.
648 turnstile_signal(struct turnstile *ts)
650 struct turnstile_chain *tc;
655 MPASS(curthread->td_proc->p_magic == P_MAGIC);
656 MPASS(ts->ts_owner == curthread);
657 tc = TC_LOOKUP(ts->ts_lockobj);
658 mtx_assert(&tc->tc_lock, MA_OWNED);
661 * Pick the highest priority thread blocked on this lock and
662 * move it to the pending list.
664 td = TAILQ_FIRST(&ts->ts_blocked);
665 MPASS(td->td_proc->p_magic == P_MAGIC);
666 mtx_lock_spin(&td_contested_lock);
667 TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
668 mtx_unlock_spin(&td_contested_lock);
669 TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
672 * If the turnstile is now empty, remove it from its chain and
673 * give it to the about-to-be-woken thread. Otherwise take a
674 * turnstile from the free list and give it to the thread.
676 empty = TAILQ_EMPTY(&ts->ts_blocked);
678 MPASS(LIST_EMPTY(&ts->ts_free));
679 #ifdef TURNSTILE_PROFILING
683 ts = LIST_FIRST(&ts->ts_free);
685 LIST_REMOVE(ts, ts_hash);
686 td->td_turnstile = ts;
692 * Put all blocked threads on the pending list. This must be called with
693 * the turnstile chain locked.
696 turnstile_broadcast(struct turnstile *ts)
698 struct turnstile_chain *tc;
699 struct turnstile *ts1;
703 MPASS(curthread->td_proc->p_magic == P_MAGIC);
704 MPASS(ts->ts_owner == curthread);
705 tc = TC_LOOKUP(ts->ts_lockobj);
706 mtx_assert(&tc->tc_lock, MA_OWNED);
709 * Transfer the blocked list to the pending list.
711 mtx_lock_spin(&td_contested_lock);
712 TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked, td_lockq);
713 mtx_unlock_spin(&td_contested_lock);
716 * Give a turnstile to each thread. The last thread gets
719 TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
720 if (LIST_EMPTY(&ts->ts_free)) {
721 MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
723 #ifdef TURNSTILE_PROFILING
727 ts1 = LIST_FIRST(&ts->ts_free);
729 LIST_REMOVE(ts1, ts_hash);
730 td->td_turnstile = ts1;
735 * Wakeup all threads on the pending list and adjust the priority of the
736 * current thread appropriately. This must be called with the turnstile
740 turnstile_unpend(struct turnstile *ts)
742 TAILQ_HEAD( ,thread) pending_threads;
743 struct turnstile_chain *tc;
748 MPASS(ts->ts_owner == curthread);
749 tc = TC_LOOKUP(ts->ts_lockobj);
750 mtx_assert(&tc->tc_lock, MA_OWNED);
751 MPASS(!TAILQ_EMPTY(&ts->ts_pending));
754 * Move the list of pending threads out of the turnstile and
755 * into a local variable.
757 TAILQ_INIT(&pending_threads);
758 TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
760 if (TAILQ_EMPTY(&ts->ts_blocked))
761 ts->ts_lockobj = NULL;
765 * Remove the turnstile from this thread's list of contested locks
766 * since this thread doesn't own it anymore. New threads will
767 * not be blocking on the turnstile until it is claimed by a new
770 mtx_lock_spin(&td_contested_lock);
772 LIST_REMOVE(ts, ts_link);
773 mtx_unlock_spin(&td_contested_lock);
775 mtx_unlock_spin(&tc->tc_lock);
778 * Adjust the priority of curthread based on other contested
779 * locks it owns. Don't lower the priority below the base
784 mtx_lock_spin(&sched_lock);
785 mtx_lock_spin(&td_contested_lock);
786 LIST_FOREACH(ts, &td->td_contested, ts_link) {
787 cp = TAILQ_FIRST(&ts->ts_blocked)->td_priority;
791 mtx_unlock_spin(&td_contested_lock);
792 sched_unlend_prio(td, pri);
795 * Wake up all the pending threads. If a thread is not blocked
796 * on a lock, then it is currently executing on another CPU in
797 * turnstile_wait() or sitting on a run queue waiting to resume
798 * in turnstile_wait(). Set a flag to force it to try to acquire
799 * the lock again instead of blocking.
801 while (!TAILQ_EMPTY(&pending_threads)) {
802 td = TAILQ_FIRST(&pending_threads);
803 TAILQ_REMOVE(&pending_threads, td, td_lockq);
804 MPASS(td->td_proc->p_magic == P_MAGIC);
805 if (TD_ON_LOCK(td)) {
806 td->td_blocked = NULL;
807 td->td_lockname = NULL;
809 MPASS(TD_CAN_RUN(td));
810 setrunqueue(td, SRQ_BORING);
812 td->td_flags |= TDF_TSNOBLOCK;
813 MPASS(TD_IS_RUNNING(td) || TD_ON_RUNQ(td));
817 mtx_unlock_spin(&sched_lock);
821 * Return the first thread in a turnstile.
824 turnstile_head(struct turnstile *ts)
827 struct turnstile_chain *tc;
830 tc = TC_LOOKUP(ts->ts_lockobj);
831 mtx_assert(&tc->tc_lock, MA_OWNED);
833 return (TAILQ_FIRST(&ts->ts_blocked));
837 * Returns true if a turnstile is empty.
840 turnstile_empty(struct turnstile *ts)
843 struct turnstile_chain *tc;
846 tc = TC_LOOKUP(ts->ts_lockobj);
847 mtx_assert(&tc->tc_lock, MA_OWNED);
849 return (TAILQ_EMPTY(&ts->ts_blocked));