The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_turnstile.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 50e70d5f4b4247ca12c60f64a2c44af8


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.