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.
--- 43 unchanged lines hidden (view full) ---
52 * already has a turnstile, then it gives its turnstile to the lock's
53 * turnstile's free list. When a thread is woken up, it takes a turnstile from
54 * the free list if there are any other waiters. If it is the only thread
55 * blocked on the lock, then it reclaims the turnstile associated with the lock
56 * and removes it from the hash table.
57 */
58
59#include <sys/cdefs.h>
60__FBSDID("$FreeBSD: head/sys/kern/subr_turnstile.c 169666 2007-05-18 06:32:24Z jeff $");
61
62#include "opt_ddb.h"
63#include "opt_turnstile_profiling.h"
64
65#include <sys/param.h>
66#include <sys/systm.h>
67#include <sys/kernel.h>
68#include <sys/ktr.h>
--- 42 unchanged lines hidden (view full) ---
111 * turnstile_signal() or turnstile_wait() that are waiting to be put on
112 * the run queue.
113 *
114 * Locking key:
115 * c - turnstile chain lock
116 * q - td_contested lock
117 */
118struct turnstile {
119 struct threadqueue ts_blocked[2]; /* (c + q) Blocked threads. */
120 struct threadqueue ts_pending; /* (c) Pending threads. */
121 LIST_ENTRY(turnstile) ts_hash; /* (c) Chain and free list. */
122 LIST_ENTRY(turnstile) ts_link; /* (q) Contested locks. */
123 LIST_HEAD(, turnstile) ts_free; /* (c) Free turnstiles. */
124 struct lock_object *ts_lockobj; /* (c) Lock we reference. */
125 struct thread *ts_owner; /* (c + q) Who owns the lock. */
126};
--- 30 unchanged lines hidden (view full) ---
157static int turnstile_adjust_thread(struct turnstile *ts,
158 struct thread *td);
159static struct thread *turnstile_first_waiter(struct turnstile *ts);
160static void turnstile_setowner(struct turnstile *ts, struct thread *owner);
161#ifdef INVARIANTS
162static void turnstile_dtor(void *mem, int size, void *arg);
163#endif
164static int turnstile_init(void *mem, int size, int flags);
165
166/*
167 * Walks the chain of turnstiles and their owners to propagate the priority
168 * of the thread being blocked to all the threads holding locks that have to
169 * release their locks before this thread can run again.
170 */
171static void
172propagate_priority(struct thread *td)
173{
174 struct turnstile_chain *tc;
175 struct turnstile *ts;
176 int pri;
177
178 mtx_assert(&sched_lock, MA_OWNED);
179 pri = td->td_priority;
180 ts = td->td_blocked;
181 for (;;) {
182 td = ts->ts_owner;
183
184 if (td == NULL) {
185 /*
186 * This might be a read lock with no owner. There's
187 * not much we can do, so just bail.
188 */
189 return;
190 }
191
192 MPASS(td->td_proc != NULL);
193 MPASS(td->td_proc->p_magic == P_MAGIC);
194
195 /*
196 * If the thread is asleep, then we are probably about
197 * to deadlock. To make debugging this easier, just
198 * panic and tell the user which thread misbehaved so
199 * they can hopefully get a stack trace from the truly
--- 8 unchanged lines hidden (view full) ---
208#endif
209 panic("sleeping thread");
210 }
211
212 /*
213 * If this thread already has higher priority than the
214 * thread that is being blocked, we are finished.
215 */
216 if (td->td_priority <= pri)
217 return;
218
219 /*
220 * Bump this thread's priority.
221 */
222 sched_lend_prio(td, pri);
223
224 /*
225 * If lock holder is actually running or on the run queue
226 * then we are done.
227 */
228 if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
229 MPASS(td->td_blocked == NULL);
230 return;
231 }
232
233#ifndef SMP
234 /*
235 * For UP, we check to see if td is curthread (this shouldn't
236 * ever happen however as it would mean we are in a deadlock.)
237 */
--- 8 unchanged lines hidden (view full) ---
246 td->td_tid, td->td_proc->p_comm, td->td_state,
247 ts->ts_lockobj->lo_name));
248
249 /*
250 * Pick up the lock that td is blocked on.
251 */
252 ts = td->td_blocked;
253 MPASS(ts != NULL);
254 tc = TC_LOOKUP(ts->ts_lockobj);
255 mtx_lock_spin(&tc->tc_lock);
256
257 /* Resort td on the list if needed. */
258 if (!turnstile_adjust_thread(ts, td)) {
259 mtx_unlock_spin(&tc->tc_lock);
260 return;
261 }
262 mtx_unlock_spin(&tc->tc_lock);
263 }
264}
265
266/*
267 * Adjust the thread's position on a turnstile after its priority has been
268 * changed.
269 */
270static int
271turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
272{
273 struct turnstile_chain *tc;
274 struct thread *td1, *td2;
275 int queue;
276
277 mtx_assert(&sched_lock, MA_OWNED);
278 MPASS(TD_ON_LOCK(td));
279
280 /*
281 * This thread may not be blocked on this turnstile anymore
282 * but instead might already be woken up on another CPU
283 * that is waiting on sched_lock in turnstile_unpend() to
284 * finish waking this thread up. We can detect this case
285 * by checking to see if this thread has been given a
286 * turnstile by either turnstile_signal() or
287 * turnstile_broadcast(). In this case, treat the thread as
288 * if it was already running.
289 */
290 if (td->td_turnstile != NULL)
291 return (0);
292
293 /*
294 * Check if the thread needs to be moved on the blocked chain.
295 * It needs to be moved if either its priority is lower than
296 * the previous thread or higher than the next thread.
297 */
298 tc = TC_LOOKUP(ts->ts_lockobj);
299 mtx_assert(&tc->tc_lock, MA_OWNED);
300 td1 = TAILQ_PREV(td, threadqueue, td_lockq);
301 td2 = TAILQ_NEXT(td, td_lockq);
302 if ((td1 != NULL && td->td_priority < td1->td_priority) ||
303 (td2 != NULL && td->td_priority > td2->td_priority)) {
304
305 /*
306 * Remove thread from blocked chain and determine where
307 * it should be moved to.
--- 72 unchanged lines hidden (view full) ---
380#endif
381
382static void
383init_turnstile0(void *dummy)
384{
385
386 turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
387#ifdef INVARIANTS
388 NULL, turnstile_dtor, turnstile_init, NULL, UMA_ALIGN_CACHE, 0);
389#else
390 NULL, NULL, turnstile_init, NULL, UMA_ALIGN_CACHE, 0);
391#endif
392 thread0.td_turnstile = turnstile_alloc();
393}
394SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
395
396/*
397 * Update a thread on the turnstile list after it's priority has been changed.
398 * The old priority is passed in as an argument.
399 */
400void
401turnstile_adjust(struct thread *td, u_char oldpri)
402{
403 struct turnstile_chain *tc;
404 struct turnstile *ts;
405
406 mtx_assert(&sched_lock, MA_OWNED);
407 MPASS(TD_ON_LOCK(td));
408
409 /*
410 * Pick up the lock that td is blocked on.
411 */
412 ts = td->td_blocked;
413 MPASS(ts != NULL);
414 tc = TC_LOOKUP(ts->ts_lockobj);
415 mtx_lock_spin(&tc->tc_lock);
416
417 /* Resort the turnstile on the list. */
418 if (!turnstile_adjust_thread(ts, td)) {
419 mtx_unlock_spin(&tc->tc_lock);
420 return;
421 }
422
423 /*
424 * If our priority was lowered and we are at the head of the
425 * turnstile, then propagate our new priority up the chain.
426 * Note that we currently don't try to revoke lent priorities
427 * when our priority goes up.
428 */
429 MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
430 td->td_tsqueue == TS_SHARED_QUEUE);
431 if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
432 td->td_priority < oldpri) {
433 mtx_unlock_spin(&tc->tc_lock);
434 critical_enter();
435 propagate_priority(td);
436 critical_exit();
437 } else
438 mtx_unlock_spin(&tc->tc_lock);
439}
440
441/*
442 * Set the owner of the lock this turnstile is attached to.
443 */
444static void
445turnstile_setowner(struct turnstile *ts, struct thread *owner)
446{
--- 35 unchanged lines hidden (view full) ---
482 struct turnstile *ts;
483
484 bzero(mem, size);
485 ts = mem;
486 TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
487 TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
488 TAILQ_INIT(&ts->ts_pending);
489 LIST_INIT(&ts->ts_free);
490 return (0);
491}
492
493/*
494 * Get a turnstile for a new thread.
495 */
496struct turnstile *
497turnstile_alloc(void)
498{
499
500 return (uma_zalloc(turnstile_zone, M_WAITOK));
--- 8 unchanged lines hidden (view full) ---
509
510 uma_zfree(turnstile_zone, ts);
511}
512
513/*
514 * Lock the turnstile chain associated with the specified lock.
515 */
516void
517turnstile_lock(struct lock_object *lock)
518{
519 struct turnstile_chain *tc;
520
521 tc = TC_LOOKUP(lock);
522 mtx_lock_spin(&tc->tc_lock);
523}
524
525/*
526 * Look up the turnstile for a lock in the hash table locking the associated
527 * turnstile chain along the way. If no turnstile is found in the hash
528 * table, NULL is returned.
529 */
530struct turnstile *
531turnstile_lookup(struct lock_object *lock)
532{
533 struct turnstile_chain *tc;
534 struct turnstile *ts;
535
536 tc = TC_LOOKUP(lock);
537 mtx_assert(&tc->tc_lock, MA_OWNED);
538 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
539 if (ts->ts_lockobj == lock)
540 return (ts);
541 return (NULL);
542}
543
544/*
545 * Unlock the turnstile chain associated with a given lock.
546 */
547void
548turnstile_release(struct lock_object *lock)
549{
550 struct turnstile_chain *tc;
551
552 tc = TC_LOOKUP(lock);
553 mtx_unlock_spin(&tc->tc_lock);
554}
555
556/*
--- 12 unchanged lines hidden (view full) ---
569 return (xtd);
570}
571
572/*
573 * Take ownership of a turnstile and adjust the priority of the new
574 * owner appropriately.
575 */
576void
577turnstile_claim(struct lock_object *lock)
578{
579 struct turnstile_chain *tc;
580 struct turnstile *ts;
581 struct thread *td, *owner;
582
583 tc = TC_LOOKUP(lock);
584 mtx_assert(&tc->tc_lock, MA_OWNED);
585 ts = turnstile_lookup(lock);
586 MPASS(ts != NULL);
587
588 owner = curthread;
589 mtx_lock_spin(&td_contested_lock);
590 turnstile_setowner(ts, owner);
591 mtx_unlock_spin(&td_contested_lock);
592
593 td = turnstile_first_waiter(ts);
594 MPASS(td != NULL);
595 MPASS(td->td_proc->p_magic == P_MAGIC);
596 mtx_unlock_spin(&tc->tc_lock);
597
598 /*
599 * Update the priority of the new owner if needed.
600 */
601 mtx_lock_spin(&sched_lock);
602 if (td->td_priority < owner->td_priority)
603 sched_lend_prio(owner, td->td_priority);
604 mtx_unlock_spin(&sched_lock);
605}
606
607/*
608 * Block the current thread on the turnstile assicated with 'lock'. This
609 * function will context switch and not return until this thread has been
610 * woken back up. This function must be called with the appropriate
611 * turnstile chain locked and will return with it unlocked.
612 */
613void
614turnstile_wait(struct lock_object *lock, struct thread *owner, int queue)
615{
616 struct turnstile_chain *tc;
617 struct turnstile *ts;
618 struct thread *td, *td1;
619
620 td = curthread;
621 tc = TC_LOOKUP(lock);
622 mtx_assert(&tc->tc_lock, MA_OWNED);
623 MPASS(td->td_turnstile != NULL);
624 if (queue == TS_SHARED_QUEUE)
625 MPASS(owner != NULL);
626 if (owner)
627 MPASS(owner->td_proc->p_magic == P_MAGIC);
628 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
629
630 /* Look up the turnstile associated with the lock 'lock'. */
631 ts = turnstile_lookup(lock);
632
633 /*
634 * If the lock does not already have a turnstile, use this thread's
635 * turnstile. Otherwise insert the current thread into the
636 * turnstile already in use by this lock.
637 */
638 if (ts == NULL) {
639#ifdef TURNSTILE_PROFILING
640 tc->tc_depth++;
641 if (tc->tc_depth > tc->tc_max_depth) {
642 tc->tc_max_depth = tc->tc_depth;
643 if (tc->tc_max_depth > turnstile_max_depth)
644 turnstile_max_depth = tc->tc_max_depth;
645 }
646#endif
647 ts = td->td_turnstile;
648 LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
649 KASSERT(TAILQ_EMPTY(&ts->ts_pending),
650 ("thread's turnstile has pending threads"));
651 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
652 ("thread's turnstile has exclusive waiters"));
653 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
654 ("thread's turnstile has shared waiters"));
655 KASSERT(LIST_EMPTY(&ts->ts_free),
656 ("thread's turnstile has a non-empty free list"));
657 KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
658 ts->ts_lockobj = lock;
659 mtx_lock_spin(&td_contested_lock);
660 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
661 turnstile_setowner(ts, owner);
662 mtx_unlock_spin(&td_contested_lock);
663 } else {
664 TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
665 if (td1->td_priority > td->td_priority)
666 break;
667 mtx_lock_spin(&td_contested_lock);
668 if (td1 != NULL)
669 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
670 else
671 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
672 MPASS(owner == ts->ts_owner);
673 mtx_unlock_spin(&td_contested_lock);
674 MPASS(td->td_turnstile != NULL);
675 LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
676 }
677 td->td_turnstile = NULL;
678 mtx_unlock_spin(&tc->tc_lock);
679
680 mtx_lock_spin(&sched_lock);
681 /*
682 * Handle race condition where a thread on another CPU that owns
683 * lock 'lock' could have woken us in between us dropping the
684 * turnstile chain lock and acquiring the sched_lock.
685 */
686 if (td->td_flags & TDF_TSNOBLOCK) {
687 td->td_flags &= ~TDF_TSNOBLOCK;
688 mtx_unlock_spin(&sched_lock);
689 return;
690 }
691
692#ifdef notyet
693 /*
694 * If we're borrowing an interrupted thread's VM context, we
695 * must clean up before going to sleep.
696 */
697 if (td->td_ithd != NULL) {
698 struct ithd *it = td->td_ithd;
699
700 if (it->it_interrupted) {
701 if (LOCK_LOG_TEST(lock, 0))
702 CTR3(KTR_LOCK, "%s: %p interrupted %p",
703 __func__, it, it->it_interrupted);
704 intr_thd_fixup(it);
705 }
706 }
707#endif
708
709 /* Save who we are blocked on and switch. */
710 td->td_tsqueue = queue;
711 td->td_blocked = ts;
712 td->td_lockname = lock->lo_name;
713 TD_SET_LOCK(td);
714 critical_enter();
715 propagate_priority(td);
716 critical_exit();
717
718 if (LOCK_LOG_TEST(lock, 0))
719 CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
720 td->td_tid, lock, lock->lo_name);
721
722 mi_switch(SW_VOL, NULL);
723
724 if (LOCK_LOG_TEST(lock, 0))
725 CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
726 __func__, td->td_tid, lock, lock->lo_name);
727
728 mtx_unlock_spin(&sched_lock);
729}
730
731/*
732 * Pick the highest priority thread on this turnstile and put it on the
733 * pending list. This must be called with the turnstile chain locked.
734 */
735int
736turnstile_signal(struct turnstile *ts, int queue)
737{
738 struct turnstile_chain *tc;
739 struct thread *td;
740 int empty;
741
742 MPASS(ts != NULL);
743 MPASS(curthread->td_proc->p_magic == P_MAGIC);
744 MPASS(ts->ts_owner == curthread ||
745 (queue == TS_EXCLUSIVE_QUEUE && ts->ts_owner == NULL));
746 tc = TC_LOOKUP(ts->ts_lockobj);
747 mtx_assert(&tc->tc_lock, MA_OWNED);
748 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
749
750 /*
751 * Pick the highest priority thread blocked on this lock and
752 * move it to the pending list.
753 */
754 td = TAILQ_FIRST(&ts->ts_blocked[queue]);
755 MPASS(td->td_proc->p_magic == P_MAGIC);
--- 5 unchanged lines hidden (view full) ---
761 /*
762 * If the turnstile is now empty, remove it from its chain and
763 * give it to the about-to-be-woken thread. Otherwise take a
764 * turnstile from the free list and give it to the thread.
765 */
766 empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
767 TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
768 if (empty) {
769 MPASS(LIST_EMPTY(&ts->ts_free));
770#ifdef TURNSTILE_PROFILING
771 tc->tc_depth--;
772#endif
773 } else
774 ts = LIST_FIRST(&ts->ts_free);
775 MPASS(ts != NULL);
776 LIST_REMOVE(ts, ts_hash);
--- 9 unchanged lines hidden (view full) ---
786void
787turnstile_broadcast(struct turnstile *ts, int queue)
788{
789 struct turnstile_chain *tc;
790 struct turnstile *ts1;
791 struct thread *td;
792
793 MPASS(ts != NULL);
794 MPASS(curthread->td_proc->p_magic == P_MAGIC);
795 MPASS(ts->ts_owner == curthread ||
796 (queue == TS_EXCLUSIVE_QUEUE && ts->ts_owner == NULL));
797 tc = TC_LOOKUP(ts->ts_lockobj);
798 mtx_assert(&tc->tc_lock, MA_OWNED);
799 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
800
801 /*
802 * Transfer the blocked list to the pending list.
803 */
804 mtx_lock_spin(&td_contested_lock);
--- 23 unchanged lines hidden (view full) ---
828 * Wakeup all threads on the pending list and adjust the priority of the
829 * current thread appropriately. This must be called with the turnstile
830 * chain locked.
831 */
832void
833turnstile_unpend(struct turnstile *ts, int owner_type)
834{
835 TAILQ_HEAD( ,thread) pending_threads;
836 struct turnstile_chain *tc;
837 struct thread *td;
838 u_char cp, pri;
839
840 MPASS(ts != NULL);
841 MPASS(ts->ts_owner == curthread ||
842 (owner_type == TS_SHARED_LOCK && ts->ts_owner == NULL));
843 tc = TC_LOOKUP(ts->ts_lockobj);
844 mtx_assert(&tc->tc_lock, MA_OWNED);
845 MPASS(!TAILQ_EMPTY(&ts->ts_pending));
846
847 /*
848 * Move the list of pending threads out of the turnstile and
849 * into a local variable.
850 */
851 TAILQ_INIT(&pending_threads);
852 TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
853#ifdef INVARIANTS
854 if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
855 TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
856 ts->ts_lockobj = NULL;
857#endif
858
859 /*
860 * Remove the turnstile from this thread's list of contested locks
861 * since this thread doesn't own it anymore. New threads will
862 * not be blocking on the turnstile until it is claimed by a new
863 * owner. There might not be a current owner if this is a shared
864 * lock.
865 */
866 if (ts->ts_owner != NULL) {
867 mtx_lock_spin(&td_contested_lock);
868 ts->ts_owner = NULL;
869 LIST_REMOVE(ts, ts_link);
870 mtx_unlock_spin(&td_contested_lock);
871 }
872 critical_enter();
873 mtx_unlock_spin(&tc->tc_lock);
874
875 /*
876 * Adjust the priority of curthread based on other contested
877 * locks it owns. Don't lower the priority below the base
878 * priority however.
879 */
880 td = curthread;
881 pri = PRI_MAX;
882 mtx_lock_spin(&sched_lock);
883 mtx_lock_spin(&td_contested_lock);
884 LIST_FOREACH(ts, &td->td_contested, ts_link) {
885 cp = turnstile_first_waiter(ts)->td_priority;
886 if (cp < pri)
887 pri = cp;
888 }
889 mtx_unlock_spin(&td_contested_lock);
890 sched_unlend_prio(td, pri);
891
892 /*
893 * Wake up all the pending threads. If a thread is not blocked
894 * on a lock, then it is currently executing on another CPU in
895 * turnstile_wait() or sitting on a run queue waiting to resume
896 * in turnstile_wait(). Set a flag to force it to try to acquire
897 * the lock again instead of blocking.
898 */
899 while (!TAILQ_EMPTY(&pending_threads)) {
900 td = TAILQ_FIRST(&pending_threads);
901 TAILQ_REMOVE(&pending_threads, td, td_lockq);
902 MPASS(td->td_proc->p_magic == P_MAGIC);
903 if (TD_ON_LOCK(td)) {
904 td->td_blocked = NULL;
905 td->td_lockname = NULL;
906#ifdef INVARIANTS
907 td->td_tsqueue = 0xff;
908#endif
909 TD_CLR_LOCK(td);
910 MPASS(TD_CAN_RUN(td));
911 sched_add(td, SRQ_BORING);
912 } else {
913 td->td_flags |= TDF_TSNOBLOCK;
914 MPASS(TD_IS_RUNNING(td) || TD_ON_RUNQ(td));
915 }
916 }
917 critical_exit();
918 mtx_unlock_spin(&sched_lock);
919}
920
921/*
922 * Give up ownership of a turnstile. This must be called with the
923 * turnstile chain locked.
924 */
925void
926turnstile_disown(struct turnstile *ts)
927{
928 struct turnstile_chain *tc;
929 struct thread *td;
930 u_char cp, pri;
931
932 MPASS(ts != NULL);
933 MPASS(ts->ts_owner == curthread);
934 tc = TC_LOOKUP(ts->ts_lockobj);
935 mtx_assert(&tc->tc_lock, MA_OWNED);
936 MPASS(TAILQ_EMPTY(&ts->ts_pending));
937 MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
938 !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
939
940 /*
941 * Remove the turnstile from this thread's list of contested locks
942 * since this thread doesn't own it anymore. New threads will
943 * not be blocking on the turnstile until it is claimed by a new
944 * owner.
945 */
946 mtx_lock_spin(&td_contested_lock);
947 ts->ts_owner = NULL;
948 LIST_REMOVE(ts, ts_link);
949 mtx_unlock_spin(&td_contested_lock);
950 mtx_unlock_spin(&tc->tc_lock);
951
952 /*
953 * Adjust the priority of curthread based on other contested
954 * locks it owns. Don't lower the priority below the base
955 * priority however.
956 */
957 td = curthread;
958 pri = PRI_MAX;
959 mtx_lock_spin(&sched_lock);
960 mtx_lock_spin(&td_contested_lock);
961 LIST_FOREACH(ts, &td->td_contested, ts_link) {
962 cp = turnstile_first_waiter(ts)->td_priority;
963 if (cp < pri)
964 pri = cp;
965 }
966 mtx_unlock_spin(&td_contested_lock);
967 sched_unlend_prio(td, pri);
968 mtx_unlock_spin(&sched_lock);
969}
970
971/*
972 * Return the first thread in a turnstile.
973 */
974struct thread *
975turnstile_head(struct turnstile *ts, int queue)
976{
977#ifdef INVARIANTS
978 struct turnstile_chain *tc;
979
980 MPASS(ts != NULL);
981 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
982 tc = TC_LOOKUP(ts->ts_lockobj);
983 mtx_assert(&tc->tc_lock, MA_OWNED);
984#endif
985 return (TAILQ_FIRST(&ts->ts_blocked[queue]));
986}
987
988/*
989 * Returns true if a sub-queue of a turnstile is empty.
990 */
991int
992turnstile_empty(struct turnstile *ts, int queue)
993{
994#ifdef INVARIANTS
995 struct turnstile_chain *tc;
996
997 MPASS(ts != NULL);
998 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
999 tc = TC_LOOKUP(ts->ts_lockobj);
1000 mtx_assert(&tc->tc_lock, MA_OWNED);
1001#endif
1002 return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
1003}
1004
1005#ifdef DDB
1006static void
1007print_thread(struct thread *td, const char *prefix)
1008{
--- 280 unchanged lines hidden ---
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.
--- 43 unchanged lines hidden (view full) ---
52 * already has a turnstile, then it gives its turnstile to the lock's
53 * turnstile's free list. When a thread is woken up, it takes a turnstile from
54 * the free list if there are any other waiters. If it is the only thread
55 * blocked on the lock, then it reclaims the turnstile associated with the lock
56 * and removes it from the hash table.
57 */
58
59#include <sys/cdefs.h>
60__FBSDID("$FreeBSD: head/sys/kern/subr_turnstile.c 169666 2007-05-18 06:32:24Z jeff $");
61
62#include "opt_ddb.h"
63#include "opt_turnstile_profiling.h"
64
65#include <sys/param.h>
66#include <sys/systm.h>
67#include <sys/kernel.h>
68#include <sys/ktr.h>
--- 42 unchanged lines hidden (view full) ---
111 * turnstile_signal() or turnstile_wait() that are waiting to be put on
112 * the run queue.
113 *
114 * Locking key:
115 * c - turnstile chain lock
116 * q - td_contested lock
117 */
118struct turnstile {
119 struct threadqueue ts_blocked[2]; /* (c + q) Blocked threads. */
120 struct threadqueue ts_pending; /* (c) Pending threads. */
121 LIST_ENTRY(turnstile) ts_hash; /* (c) Chain and free list. */
122 LIST_ENTRY(turnstile) ts_link; /* (q) Contested locks. */
123 LIST_HEAD(, turnstile) ts_free; /* (c) Free turnstiles. */
124 struct lock_object *ts_lockobj; /* (c) Lock we reference. */
125 struct thread *ts_owner; /* (c + q) Who owns the lock. */
126};
--- 30 unchanged lines hidden (view full) ---
157static int turnstile_adjust_thread(struct turnstile *ts,
158 struct thread *td);
159static struct thread *turnstile_first_waiter(struct turnstile *ts);
160static void turnstile_setowner(struct turnstile *ts, struct thread *owner);
161#ifdef INVARIANTS
162static void turnstile_dtor(void *mem, int size, void *arg);
163#endif
164static int turnstile_init(void *mem, int size, int flags);
165
166/*
167 * Walks the chain of turnstiles and their owners to propagate the priority
168 * of the thread being blocked to all the threads holding locks that have to
169 * release their locks before this thread can run again.
170 */
171static void
172propagate_priority(struct thread *td)
173{
174 struct turnstile_chain *tc;
175 struct turnstile *ts;
176 int pri;
177
178 mtx_assert(&sched_lock, MA_OWNED);
179 pri = td->td_priority;
180 ts = td->td_blocked;
181 for (;;) {
182 td = ts->ts_owner;
183
184 if (td == NULL) {
185 /*
186 * This might be a read lock with no owner. There's
187 * not much we can do, so just bail.
188 */
189 return;
190 }
191
192 MPASS(td->td_proc != NULL);
193 MPASS(td->td_proc->p_magic == P_MAGIC);
194
195 /*
196 * If the thread is asleep, then we are probably about
197 * to deadlock. To make debugging this easier, just
198 * panic and tell the user which thread misbehaved so
199 * they can hopefully get a stack trace from the truly
--- 8 unchanged lines hidden (view full) ---
208#endif
209 panic("sleeping thread");
210 }
211
212 /*
213 * If this thread already has higher priority than the
214 * thread that is being blocked, we are finished.
215 */
216 if (td->td_priority <= pri)
217 return;
218
219 /*
220 * Bump this thread's priority.
221 */
222 sched_lend_prio(td, pri);
223
224 /*
225 * If lock holder is actually running or on the run queue
226 * then we are done.
227 */
228 if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
229 MPASS(td->td_blocked == NULL);
230 return;
231 }
232
233#ifndef SMP
234 /*
235 * For UP, we check to see if td is curthread (this shouldn't
236 * ever happen however as it would mean we are in a deadlock.)
237 */
--- 8 unchanged lines hidden (view full) ---
246 td->td_tid, td->td_proc->p_comm, td->td_state,
247 ts->ts_lockobj->lo_name));
248
249 /*
250 * Pick up the lock that td is blocked on.
251 */
252 ts = td->td_blocked;
253 MPASS(ts != NULL);
254 tc = TC_LOOKUP(ts->ts_lockobj);
255 mtx_lock_spin(&tc->tc_lock);
256
257 /* Resort td on the list if needed. */
258 if (!turnstile_adjust_thread(ts, td)) {
259 mtx_unlock_spin(&tc->tc_lock);
260 return;
261 }
262 mtx_unlock_spin(&tc->tc_lock);
263 }
264}
265
266/*
267 * Adjust the thread's position on a turnstile after its priority has been
268 * changed.
269 */
270static int
271turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
272{
273 struct turnstile_chain *tc;
274 struct thread *td1, *td2;
275 int queue;
276
277 mtx_assert(&sched_lock, MA_OWNED);
278 MPASS(TD_ON_LOCK(td));
279
280 /*
281 * This thread may not be blocked on this turnstile anymore
282 * but instead might already be woken up on another CPU
283 * that is waiting on sched_lock in turnstile_unpend() to
284 * finish waking this thread up. We can detect this case
285 * by checking to see if this thread has been given a
286 * turnstile by either turnstile_signal() or
287 * turnstile_broadcast(). In this case, treat the thread as
288 * if it was already running.
289 */
290 if (td->td_turnstile != NULL)
291 return (0);
292
293 /*
294 * Check if the thread needs to be moved on the blocked chain.
295 * It needs to be moved if either its priority is lower than
296 * the previous thread or higher than the next thread.
297 */
298 tc = TC_LOOKUP(ts->ts_lockobj);
299 mtx_assert(&tc->tc_lock, MA_OWNED);
300 td1 = TAILQ_PREV(td, threadqueue, td_lockq);
301 td2 = TAILQ_NEXT(td, td_lockq);
302 if ((td1 != NULL && td->td_priority < td1->td_priority) ||
303 (td2 != NULL && td->td_priority > td2->td_priority)) {
304
305 /*
306 * Remove thread from blocked chain and determine where
307 * it should be moved to.
--- 72 unchanged lines hidden (view full) ---
380#endif
381
382static void
383init_turnstile0(void *dummy)
384{
385
386 turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
387#ifdef INVARIANTS
388 NULL, turnstile_dtor, turnstile_init, NULL, UMA_ALIGN_CACHE, 0);
389#else
390 NULL, NULL, turnstile_init, NULL, UMA_ALIGN_CACHE, 0);
391#endif
392 thread0.td_turnstile = turnstile_alloc();
393}
394SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
395
396/*
397 * Update a thread on the turnstile list after it's priority has been changed.
398 * The old priority is passed in as an argument.
399 */
400void
401turnstile_adjust(struct thread *td, u_char oldpri)
402{
403 struct turnstile_chain *tc;
404 struct turnstile *ts;
405
406 mtx_assert(&sched_lock, MA_OWNED);
407 MPASS(TD_ON_LOCK(td));
408
409 /*
410 * Pick up the lock that td is blocked on.
411 */
412 ts = td->td_blocked;
413 MPASS(ts != NULL);
414 tc = TC_LOOKUP(ts->ts_lockobj);
415 mtx_lock_spin(&tc->tc_lock);
416
417 /* Resort the turnstile on the list. */
418 if (!turnstile_adjust_thread(ts, td)) {
419 mtx_unlock_spin(&tc->tc_lock);
420 return;
421 }
422
423 /*
424 * If our priority was lowered and we are at the head of the
425 * turnstile, then propagate our new priority up the chain.
426 * Note that we currently don't try to revoke lent priorities
427 * when our priority goes up.
428 */
429 MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
430 td->td_tsqueue == TS_SHARED_QUEUE);
431 if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
432 td->td_priority < oldpri) {
433 mtx_unlock_spin(&tc->tc_lock);
434 critical_enter();
435 propagate_priority(td);
436 critical_exit();
437 } else
438 mtx_unlock_spin(&tc->tc_lock);
439}
440
441/*
442 * Set the owner of the lock this turnstile is attached to.
443 */
444static void
445turnstile_setowner(struct turnstile *ts, struct thread *owner)
446{
--- 35 unchanged lines hidden (view full) ---
482 struct turnstile *ts;
483
484 bzero(mem, size);
485 ts = mem;
486 TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
487 TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
488 TAILQ_INIT(&ts->ts_pending);
489 LIST_INIT(&ts->ts_free);
490 return (0);
491}
492
493/*
494 * Get a turnstile for a new thread.
495 */
496struct turnstile *
497turnstile_alloc(void)
498{
499
500 return (uma_zalloc(turnstile_zone, M_WAITOK));
--- 8 unchanged lines hidden (view full) ---
509
510 uma_zfree(turnstile_zone, ts);
511}
512
513/*
514 * Lock the turnstile chain associated with the specified lock.
515 */
516void
517turnstile_lock(struct lock_object *lock)
518{
519 struct turnstile_chain *tc;
520
521 tc = TC_LOOKUP(lock);
522 mtx_lock_spin(&tc->tc_lock);
523}
524
525/*
526 * Look up the turnstile for a lock in the hash table locking the associated
527 * turnstile chain along the way. If no turnstile is found in the hash
528 * table, NULL is returned.
529 */
530struct turnstile *
531turnstile_lookup(struct lock_object *lock)
532{
533 struct turnstile_chain *tc;
534 struct turnstile *ts;
535
536 tc = TC_LOOKUP(lock);
537 mtx_assert(&tc->tc_lock, MA_OWNED);
538 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
539 if (ts->ts_lockobj == lock)
540 return (ts);
541 return (NULL);
542}
543
544/*
545 * Unlock the turnstile chain associated with a given lock.
546 */
547void
548turnstile_release(struct lock_object *lock)
549{
550 struct turnstile_chain *tc;
551
552 tc = TC_LOOKUP(lock);
553 mtx_unlock_spin(&tc->tc_lock);
554}
555
556/*
--- 12 unchanged lines hidden (view full) ---
569 return (xtd);
570}
571
572/*
573 * Take ownership of a turnstile and adjust the priority of the new
574 * owner appropriately.
575 */
576void
577turnstile_claim(struct lock_object *lock)
578{
579 struct turnstile_chain *tc;
580 struct turnstile *ts;
581 struct thread *td, *owner;
582
583 tc = TC_LOOKUP(lock);
584 mtx_assert(&tc->tc_lock, MA_OWNED);
585 ts = turnstile_lookup(lock);
586 MPASS(ts != NULL);
587
588 owner = curthread;
589 mtx_lock_spin(&td_contested_lock);
590 turnstile_setowner(ts, owner);
591 mtx_unlock_spin(&td_contested_lock);
592
593 td = turnstile_first_waiter(ts);
594 MPASS(td != NULL);
595 MPASS(td->td_proc->p_magic == P_MAGIC);
596 mtx_unlock_spin(&tc->tc_lock);
597
598 /*
599 * Update the priority of the new owner if needed.
600 */
601 mtx_lock_spin(&sched_lock);
602 if (td->td_priority < owner->td_priority)
603 sched_lend_prio(owner, td->td_priority);
604 mtx_unlock_spin(&sched_lock);
605}
606
607/*
608 * Block the current thread on the turnstile assicated with 'lock'. This
609 * function will context switch and not return until this thread has been
610 * woken back up. This function must be called with the appropriate
611 * turnstile chain locked and will return with it unlocked.
612 */
613void
614turnstile_wait(struct lock_object *lock, struct thread *owner, int queue)
615{
616 struct turnstile_chain *tc;
617 struct turnstile *ts;
618 struct thread *td, *td1;
619
620 td = curthread;
621 tc = TC_LOOKUP(lock);
622 mtx_assert(&tc->tc_lock, MA_OWNED);
623 MPASS(td->td_turnstile != NULL);
624 if (queue == TS_SHARED_QUEUE)
625 MPASS(owner != NULL);
626 if (owner)
627 MPASS(owner->td_proc->p_magic == P_MAGIC);
628 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
629
630 /* Look up the turnstile associated with the lock 'lock'. */
631 ts = turnstile_lookup(lock);
632
633 /*
634 * If the lock does not already have a turnstile, use this thread's
635 * turnstile. Otherwise insert the current thread into the
636 * turnstile already in use by this lock.
637 */
638 if (ts == NULL) {
639#ifdef TURNSTILE_PROFILING
640 tc->tc_depth++;
641 if (tc->tc_depth > tc->tc_max_depth) {
642 tc->tc_max_depth = tc->tc_depth;
643 if (tc->tc_max_depth > turnstile_max_depth)
644 turnstile_max_depth = tc->tc_max_depth;
645 }
646#endif
647 ts = td->td_turnstile;
648 LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
649 KASSERT(TAILQ_EMPTY(&ts->ts_pending),
650 ("thread's turnstile has pending threads"));
651 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
652 ("thread's turnstile has exclusive waiters"));
653 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
654 ("thread's turnstile has shared waiters"));
655 KASSERT(LIST_EMPTY(&ts->ts_free),
656 ("thread's turnstile has a non-empty free list"));
657 KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
658 ts->ts_lockobj = lock;
659 mtx_lock_spin(&td_contested_lock);
660 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
661 turnstile_setowner(ts, owner);
662 mtx_unlock_spin(&td_contested_lock);
663 } else {
664 TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
665 if (td1->td_priority > td->td_priority)
666 break;
667 mtx_lock_spin(&td_contested_lock);
668 if (td1 != NULL)
669 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
670 else
671 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
672 MPASS(owner == ts->ts_owner);
673 mtx_unlock_spin(&td_contested_lock);
674 MPASS(td->td_turnstile != NULL);
675 LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
676 }
677 td->td_turnstile = NULL;
678 mtx_unlock_spin(&tc->tc_lock);
679
680 mtx_lock_spin(&sched_lock);
681 /*
682 * Handle race condition where a thread on another CPU that owns
683 * lock 'lock' could have woken us in between us dropping the
684 * turnstile chain lock and acquiring the sched_lock.
685 */
686 if (td->td_flags & TDF_TSNOBLOCK) {
687 td->td_flags &= ~TDF_TSNOBLOCK;
688 mtx_unlock_spin(&sched_lock);
689 return;
690 }
691
692#ifdef notyet
693 /*
694 * If we're borrowing an interrupted thread's VM context, we
695 * must clean up before going to sleep.
696 */
697 if (td->td_ithd != NULL) {
698 struct ithd *it = td->td_ithd;
699
700 if (it->it_interrupted) {
701 if (LOCK_LOG_TEST(lock, 0))
702 CTR3(KTR_LOCK, "%s: %p interrupted %p",
703 __func__, it, it->it_interrupted);
704 intr_thd_fixup(it);
705 }
706 }
707#endif
708
709 /* Save who we are blocked on and switch. */
710 td->td_tsqueue = queue;
711 td->td_blocked = ts;
712 td->td_lockname = lock->lo_name;
713 TD_SET_LOCK(td);
714 critical_enter();
715 propagate_priority(td);
716 critical_exit();
717
718 if (LOCK_LOG_TEST(lock, 0))
719 CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
720 td->td_tid, lock, lock->lo_name);
721
722 mi_switch(SW_VOL, NULL);
723
724 if (LOCK_LOG_TEST(lock, 0))
725 CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
726 __func__, td->td_tid, lock, lock->lo_name);
727
728 mtx_unlock_spin(&sched_lock);
729}
730
731/*
732 * Pick the highest priority thread on this turnstile and put it on the
733 * pending list. This must be called with the turnstile chain locked.
734 */
735int
736turnstile_signal(struct turnstile *ts, int queue)
737{
738 struct turnstile_chain *tc;
739 struct thread *td;
740 int empty;
741
742 MPASS(ts != NULL);
743 MPASS(curthread->td_proc->p_magic == P_MAGIC);
744 MPASS(ts->ts_owner == curthread ||
745 (queue == TS_EXCLUSIVE_QUEUE && ts->ts_owner == NULL));
746 tc = TC_LOOKUP(ts->ts_lockobj);
747 mtx_assert(&tc->tc_lock, MA_OWNED);
748 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
749
750 /*
751 * Pick the highest priority thread blocked on this lock and
752 * move it to the pending list.
753 */
754 td = TAILQ_FIRST(&ts->ts_blocked[queue]);
755 MPASS(td->td_proc->p_magic == P_MAGIC);
--- 5 unchanged lines hidden (view full) ---
761 /*
762 * If the turnstile is now empty, remove it from its chain and
763 * give it to the about-to-be-woken thread. Otherwise take a
764 * turnstile from the free list and give it to the thread.
765 */
766 empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
767 TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
768 if (empty) {
769 MPASS(LIST_EMPTY(&ts->ts_free));
770#ifdef TURNSTILE_PROFILING
771 tc->tc_depth--;
772#endif
773 } else
774 ts = LIST_FIRST(&ts->ts_free);
775 MPASS(ts != NULL);
776 LIST_REMOVE(ts, ts_hash);
--- 9 unchanged lines hidden (view full) ---
786void
787turnstile_broadcast(struct turnstile *ts, int queue)
788{
789 struct turnstile_chain *tc;
790 struct turnstile *ts1;
791 struct thread *td;
792
793 MPASS(ts != NULL);
794 MPASS(curthread->td_proc->p_magic == P_MAGIC);
795 MPASS(ts->ts_owner == curthread ||
796 (queue == TS_EXCLUSIVE_QUEUE && ts->ts_owner == NULL));
797 tc = TC_LOOKUP(ts->ts_lockobj);
798 mtx_assert(&tc->tc_lock, MA_OWNED);
799 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
800
801 /*
802 * Transfer the blocked list to the pending list.
803 */
804 mtx_lock_spin(&td_contested_lock);
--- 23 unchanged lines hidden (view full) ---
828 * Wakeup all threads on the pending list and adjust the priority of the
829 * current thread appropriately. This must be called with the turnstile
830 * chain locked.
831 */
832void
833turnstile_unpend(struct turnstile *ts, int owner_type)
834{
835 TAILQ_HEAD( ,thread) pending_threads;
836 struct turnstile_chain *tc;
837 struct thread *td;
838 u_char cp, pri;
839
840 MPASS(ts != NULL);
841 MPASS(ts->ts_owner == curthread ||
842 (owner_type == TS_SHARED_LOCK && ts->ts_owner == NULL));
843 tc = TC_LOOKUP(ts->ts_lockobj);
844 mtx_assert(&tc->tc_lock, MA_OWNED);
845 MPASS(!TAILQ_EMPTY(&ts->ts_pending));
846
847 /*
848 * Move the list of pending threads out of the turnstile and
849 * into a local variable.
850 */
851 TAILQ_INIT(&pending_threads);
852 TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
853#ifdef INVARIANTS
854 if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
855 TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
856 ts->ts_lockobj = NULL;
857#endif
858
859 /*
860 * Remove the turnstile from this thread's list of contested locks
861 * since this thread doesn't own it anymore. New threads will
862 * not be blocking on the turnstile until it is claimed by a new
863 * owner. There might not be a current owner if this is a shared
864 * lock.
865 */
866 if (ts->ts_owner != NULL) {
867 mtx_lock_spin(&td_contested_lock);
868 ts->ts_owner = NULL;
869 LIST_REMOVE(ts, ts_link);
870 mtx_unlock_spin(&td_contested_lock);
871 }
872 critical_enter();
873 mtx_unlock_spin(&tc->tc_lock);
874
875 /*
876 * Adjust the priority of curthread based on other contested
877 * locks it owns. Don't lower the priority below the base
878 * priority however.
879 */
880 td = curthread;
881 pri = PRI_MAX;
882 mtx_lock_spin(&sched_lock);
883 mtx_lock_spin(&td_contested_lock);
884 LIST_FOREACH(ts, &td->td_contested, ts_link) {
885 cp = turnstile_first_waiter(ts)->td_priority;
886 if (cp < pri)
887 pri = cp;
888 }
889 mtx_unlock_spin(&td_contested_lock);
890 sched_unlend_prio(td, pri);
891
892 /*
893 * Wake up all the pending threads. If a thread is not blocked
894 * on a lock, then it is currently executing on another CPU in
895 * turnstile_wait() or sitting on a run queue waiting to resume
896 * in turnstile_wait(). Set a flag to force it to try to acquire
897 * the lock again instead of blocking.
898 */
899 while (!TAILQ_EMPTY(&pending_threads)) {
900 td = TAILQ_FIRST(&pending_threads);
901 TAILQ_REMOVE(&pending_threads, td, td_lockq);
902 MPASS(td->td_proc->p_magic == P_MAGIC);
903 if (TD_ON_LOCK(td)) {
904 td->td_blocked = NULL;
905 td->td_lockname = NULL;
906#ifdef INVARIANTS
907 td->td_tsqueue = 0xff;
908#endif
909 TD_CLR_LOCK(td);
910 MPASS(TD_CAN_RUN(td));
911 sched_add(td, SRQ_BORING);
912 } else {
913 td->td_flags |= TDF_TSNOBLOCK;
914 MPASS(TD_IS_RUNNING(td) || TD_ON_RUNQ(td));
915 }
916 }
917 critical_exit();
918 mtx_unlock_spin(&sched_lock);
919}
920
921/*
922 * Give up ownership of a turnstile. This must be called with the
923 * turnstile chain locked.
924 */
925void
926turnstile_disown(struct turnstile *ts)
927{
928 struct turnstile_chain *tc;
929 struct thread *td;
930 u_char cp, pri;
931
932 MPASS(ts != NULL);
933 MPASS(ts->ts_owner == curthread);
934 tc = TC_LOOKUP(ts->ts_lockobj);
935 mtx_assert(&tc->tc_lock, MA_OWNED);
936 MPASS(TAILQ_EMPTY(&ts->ts_pending));
937 MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
938 !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
939
940 /*
941 * Remove the turnstile from this thread's list of contested locks
942 * since this thread doesn't own it anymore. New threads will
943 * not be blocking on the turnstile until it is claimed by a new
944 * owner.
945 */
946 mtx_lock_spin(&td_contested_lock);
947 ts->ts_owner = NULL;
948 LIST_REMOVE(ts, ts_link);
949 mtx_unlock_spin(&td_contested_lock);
950 mtx_unlock_spin(&tc->tc_lock);
951
952 /*
953 * Adjust the priority of curthread based on other contested
954 * locks it owns. Don't lower the priority below the base
955 * priority however.
956 */
957 td = curthread;
958 pri = PRI_MAX;
959 mtx_lock_spin(&sched_lock);
960 mtx_lock_spin(&td_contested_lock);
961 LIST_FOREACH(ts, &td->td_contested, ts_link) {
962 cp = turnstile_first_waiter(ts)->td_priority;
963 if (cp < pri)
964 pri = cp;
965 }
966 mtx_unlock_spin(&td_contested_lock);
967 sched_unlend_prio(td, pri);
968 mtx_unlock_spin(&sched_lock);
969}
970
971/*
972 * Return the first thread in a turnstile.
973 */
974struct thread *
975turnstile_head(struct turnstile *ts, int queue)
976{
977#ifdef INVARIANTS
978 struct turnstile_chain *tc;
979
980 MPASS(ts != NULL);
981 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
982 tc = TC_LOOKUP(ts->ts_lockobj);
983 mtx_assert(&tc->tc_lock, MA_OWNED);
984#endif
985 return (TAILQ_FIRST(&ts->ts_blocked[queue]));
986}
987
988/*
989 * Returns true if a sub-queue of a turnstile is empty.
990 */
991int
992turnstile_empty(struct turnstile *ts, int queue)
993{
994#ifdef INVARIANTS
995 struct turnstile_chain *tc;
996
997 MPASS(ts != NULL);
998 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
999 tc = TC_LOOKUP(ts->ts_lockobj);
1000 mtx_assert(&tc->tc_lock, MA_OWNED);
1001#endif
1002 return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
1003}
1004
1005#ifdef DDB
1006static void
1007print_thread(struct thread *td, const char *prefix)
1008{
--- 280 unchanged lines hidden ---