2 * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Implementation of sleep queues used to hold queue of threads blocked on
32 * a wait channel. Sleep queues different from turnstiles in that wait
33 * channels are not owned by anyone, so there is no priority propagation.
34 * Sleep queues can also provide a timeout and can also be interrupted by
35 * signals. That said, there are several similarities between the turnstile
36 * and sleep queue implementations. (Note: turnstiles were implemented
37 * first.) For example, both use a hash table of the same size where each
38 * bucket is referred to as a "chain" that contains both a spin lock and
39 * a linked list of queues. An individual queue is located by using a hash
40 * to pick a chain, locking the chain, and then walking the chain searching
41 * for the queue. This means that a wait channel object does not need to
42 * embed it's queue head just as locks do not embed their turnstile queue
43 * head. Threads also carry around a sleep queue that they lend to the
44 * wait channel when blocking. Just as in turnstiles, the queue includes
45 * a free list of the sleep queues of other threads blocked on the same
46 * wait channel in the case of multiple waiters.
48 * Some additional functionality provided by sleep queues include the
49 * ability to set a timeout. The timeout is managed using a per-thread
50 * callout that resumes a thread if it is asleep. A thread may also
51 * catch signals while it is asleep (aka an interruptible sleep). The
52 * signal code uses sleepq_abort() to interrupt a sleeping thread. Finally,
53 * sleep queues also provide some extra assertions. One is not allowed to
54 * mix the sleep/wakeup and cv APIs for a given wait channel. Also, one
55 * must consistently use the same lock to synchronize with a wait channel,
56 * though this check is currently only a warning for sleep/wakeup due to
57 * pre-existing abuse of that API. The same lock must also be held when
58 * awakening threads, though that is currently only enforced for condition
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD$");
65 #include "opt_sleepqueue_profiling.h"
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/kernel.h>
73 #include <sys/malloc.h>
74 #include <sys/mutex.h>
76 #include <sys/sched.h>
77 #include <sys/signalvar.h>
78 #include <sys/sleepqueue.h>
79 #include <sys/sysctl.h>
86 * Constants for the hash table of sleep queue chains. These constants are
87 * the same ones that 4BSD (and possibly earlier versions of BSD) used.
88 * Basically, we ignore the lower 8 bits of the address since most wait
89 * channel pointers are aligned and only look at the next 7 bits for the
90 * hash. SC_TABLESIZE must be a power of two for SC_MASK to work properly.
92 #define SC_TABLESIZE 128 /* Must be power of 2. */
93 #define SC_MASK (SC_TABLESIZE - 1)
95 #define SC_HASH(wc) (((uintptr_t)(wc) >> SC_SHIFT) & SC_MASK)
96 #define SC_LOOKUP(wc) &sleepq_chains[SC_HASH(wc)]
99 * There two different lists of sleep queues. Both lists are connected
100 * via the sq_hash entries. The first list is the sleep queue chain list
101 * that a sleep queue is on when it is attached to a wait channel. The
102 * second list is the free list hung off of a sleep queue that is attached
105 * Each sleep queue also contains the wait channel it is attached to, the
106 * list of threads blocked on that wait channel, flags specific to the
107 * wait channel, and the lock used to synchronize with a wait channel.
108 * The flags are used to catch mismatches between the various consumers
109 * of the sleep queue API (e.g. sleep/wakeup and condition variables).
110 * The lock pointer is only used when invariants are enabled for various
114 * c - sleep queue chain lock
117 TAILQ_HEAD(, thread) sq_blocked; /* (c) Blocked threads. */
118 LIST_ENTRY(sleepqueue) sq_hash; /* (c) Chain and free list. */
119 LIST_HEAD(, sleepqueue) sq_free; /* (c) Free queues. */
120 void *sq_wchan; /* (c) Wait channel. */
122 int sq_type; /* (c) Queue type. */
123 struct mtx *sq_lock; /* (c) Associated lock. */
127 struct sleepqueue_chain {
128 LIST_HEAD(, sleepqueue) sc_queues; /* List of sleep queues. */
129 struct mtx sc_lock; /* Spin lock for this chain. */
130 #ifdef SLEEPQUEUE_PROFILING
131 u_int sc_depth; /* Length of sc_queues. */
132 u_int sc_max_depth; /* Max length of sc_queues. */
136 #ifdef SLEEPQUEUE_PROFILING
137 u_int sleepq_max_depth;
138 SYSCTL_NODE(_debug, OID_AUTO, sleepq, CTLFLAG_RD, 0, "sleepq profiling");
139 SYSCTL_NODE(_debug_sleepq, OID_AUTO, chains, CTLFLAG_RD, 0,
140 "sleepq chain stats");
141 SYSCTL_UINT(_debug_sleepq, OID_AUTO, max_depth, CTLFLAG_RD, &sleepq_max_depth,
142 0, "maxmimum depth achieved of a single chain");
144 static struct sleepqueue_chain sleepq_chains[SC_TABLESIZE];
146 static MALLOC_DEFINE(M_SLEEPQUEUE, "sleepqueue", "sleep queues");
149 * Prototypes for non-exported routines.
151 static int sleepq_check_timeout(void);
152 static void sleepq_switch(void *wchan);
153 static void sleepq_timeout(void *arg);
154 static void sleepq_resume_thread(struct sleepqueue *sq, struct thread *td, int pri);
157 * Early initialization of sleep queues that is called from the sleepinit()
161 init_sleepqueues(void)
163 #ifdef SLEEPQUEUE_PROFILING
164 struct sysctl_oid *chain_oid;
169 for (i = 0; i < SC_TABLESIZE; i++) {
170 LIST_INIT(&sleepq_chains[i].sc_queues);
171 mtx_init(&sleepq_chains[i].sc_lock, "sleepq chain", NULL,
173 #ifdef SLEEPQUEUE_PROFILING
174 snprintf(chain_name, sizeof(chain_name), "%d", i);
175 chain_oid = SYSCTL_ADD_NODE(NULL,
176 SYSCTL_STATIC_CHILDREN(_debug_sleepq_chains), OID_AUTO,
177 chain_name, CTLFLAG_RD, NULL, "sleepq chain stats");
178 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
179 "depth", CTLFLAG_RD, &sleepq_chains[i].sc_depth, 0, NULL);
180 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
181 "max_depth", CTLFLAG_RD, &sleepq_chains[i].sc_max_depth, 0,
185 thread0.td_sleepqueue = sleepq_alloc();
189 * Malloc and initialize a new sleep queue for a new thread.
194 struct sleepqueue *sq;
196 sq = malloc(sizeof(struct sleepqueue), M_SLEEPQUEUE, M_WAITOK | M_ZERO);
197 TAILQ_INIT(&sq->sq_blocked);
198 LIST_INIT(&sq->sq_free);
203 * Free a sleep queue when a thread is destroyed.
206 sleepq_free(struct sleepqueue *sq)
210 MPASS(TAILQ_EMPTY(&sq->sq_blocked));
211 free(sq, M_SLEEPQUEUE);
215 * Lock the sleep queue chain associated with the specified wait channel.
218 sleepq_lock(void *wchan)
220 struct sleepqueue_chain *sc;
222 sc = SC_LOOKUP(wchan);
223 mtx_lock_spin(&sc->sc_lock);
227 * Look up the sleep queue associated with a given wait channel in the hash
228 * table locking the associated sleep queue chain. If no queue is found in
229 * the table, NULL is returned.
232 sleepq_lookup(void *wchan)
234 struct sleepqueue_chain *sc;
235 struct sleepqueue *sq;
237 KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
238 sc = SC_LOOKUP(wchan);
239 mtx_assert(&sc->sc_lock, MA_OWNED);
240 LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
241 if (sq->sq_wchan == wchan)
247 * Unlock the sleep queue chain associated with a given wait channel.
250 sleepq_release(void *wchan)
252 struct sleepqueue_chain *sc;
254 sc = SC_LOOKUP(wchan);
255 mtx_unlock_spin(&sc->sc_lock);
259 * Places the current thread on the sleep queue for the specified wait
260 * channel. If INVARIANTS is enabled, then it associates the passed in
261 * lock with the sleepq to make sure it is held when that sleep queue is
265 sleepq_add(void *wchan, struct mtx *lock, const char *wmesg, int flags)
267 struct sleepqueue_chain *sc;
268 struct sleepqueue *sq;
272 sc = SC_LOOKUP(wchan);
273 mtx_assert(&sc->sc_lock, MA_OWNED);
274 MPASS(td->td_sleepqueue != NULL);
275 MPASS(wchan != NULL);
277 /* If this thread is not allowed to sleep, die a horrible death. */
278 KASSERT(!(td->td_pflags & TDP_NOSLEEPING),
279 ("Trying sleep, but thread marked as sleeping prohibited"));
281 /* Look up the sleep queue associated with the wait channel 'wchan'. */
282 sq = sleepq_lookup(wchan);
285 * If the wait channel does not already have a sleep queue, use
286 * this thread's sleep queue. Otherwise, insert the current thread
287 * into the sleep queue already in use by this wait channel.
290 #ifdef SLEEPQUEUE_PROFILING
292 if (sc->sc_depth > sc->sc_max_depth) {
293 sc->sc_max_depth = sc->sc_depth;
294 if (sc->sc_max_depth > sleepq_max_depth)
295 sleepq_max_depth = sc->sc_max_depth;
298 sq = td->td_sleepqueue;
299 LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
300 KASSERT(TAILQ_EMPTY(&sq->sq_blocked),
301 ("thread's sleep queue has a non-empty queue"));
302 KASSERT(LIST_EMPTY(&sq->sq_free),
303 ("thread's sleep queue has a non-empty free list"));
304 KASSERT(sq->sq_wchan == NULL, ("stale sq_wchan pointer"));
305 sq->sq_wchan = wchan;
308 sq->sq_type = flags & SLEEPQ_TYPE;
311 MPASS(wchan == sq->sq_wchan);
312 MPASS(lock == sq->sq_lock);
313 MPASS((flags & SLEEPQ_TYPE) == sq->sq_type);
314 LIST_INSERT_HEAD(&sq->sq_free, td->td_sleepqueue, sq_hash);
316 TAILQ_INSERT_TAIL(&sq->sq_blocked, td, td_slpq);
317 td->td_sleepqueue = NULL;
318 mtx_lock_spin(&sched_lock);
319 td->td_wchan = wchan;
320 td->td_wmesg = wmesg;
321 if (flags & SLEEPQ_INTERRUPTIBLE) {
322 td->td_flags |= TDF_SINTR;
323 td->td_flags &= ~TDF_SLEEPABORT;
325 mtx_unlock_spin(&sched_lock);
329 * Sets a timeout that will remove the current thread from the specified
330 * sleep queue after timo ticks if the thread has not already been awakened.
333 sleepq_set_timeout(void *wchan, int timo)
335 struct sleepqueue_chain *sc;
339 sc = SC_LOOKUP(wchan);
340 mtx_assert(&sc->sc_lock, MA_OWNED);
341 MPASS(TD_ON_SLEEPQ(td));
342 MPASS(td->td_sleepqueue == NULL);
343 MPASS(wchan != NULL);
344 callout_reset(&td->td_slpcallout, timo, sleepq_timeout, td);
348 * Marks the pending sleep of the current thread as interruptible and
349 * makes an initial check for pending signals before putting a thread
350 * to sleep. Return with sleep queue and scheduler lock held.
353 sleepq_catch_signals(void *wchan)
355 struct sleepqueue_chain *sc;
356 struct sleepqueue *sq;
364 sc = SC_LOOKUP(wchan);
365 mtx_assert(&sc->sc_lock, MA_OWNED);
366 MPASS(wchan != NULL);
367 CTR3(KTR_PROC, "sleepq catching signals: thread %p (pid %ld, %s)",
368 (void *)td, (long)p->p_pid, p->p_comm);
370 MPASS(td->td_flags & TDF_SINTR);
371 mtx_unlock_spin(&sc->sc_lock);
373 /* See if there are any pending signals for this thread. */
376 mtx_lock(&ps->ps_mtx);
379 mtx_unlock(&ps->ps_mtx);
380 ret = thread_suspend_check(1);
381 MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
383 if (SIGISMEMBER(ps->ps_sigintr, sig))
387 mtx_unlock(&ps->ps_mtx);
391 mtx_lock_spin(&sc->sc_lock);
393 * Lock sched_lock before unlocking proc lock,
394 * without this, we could lose a race.
396 mtx_lock_spin(&sched_lock);
398 if (!(td->td_flags & TDF_INTERRUPT))
400 /* KSE threads tried unblocking us. */
401 ret = td->td_intrval;
402 mtx_unlock_spin(&sched_lock);
403 MPASS(ret == EINTR || ret == ERESTART);
406 mtx_lock_spin(&sc->sc_lock);
409 * There were pending signals and this thread is still
410 * on the sleep queue, remove it from the sleep queue.
412 sq = sleepq_lookup(wchan);
413 mtx_lock_spin(&sched_lock);
414 if (TD_ON_SLEEPQ(td))
415 sleepq_resume_thread(sq, td, -1);
420 * Switches to another thread if we are still asleep on a sleep queue and
421 * drop the lock on the sleep queue chain. Returns with sched_lock held.
424 sleepq_switch(void *wchan)
426 struct sleepqueue_chain *sc;
430 sc = SC_LOOKUP(wchan);
431 mtx_assert(&sc->sc_lock, MA_OWNED);
432 mtx_assert(&sched_lock, MA_OWNED);
435 * If we have a sleep queue, then we've already been woken up, so
438 if (td->td_sleepqueue != NULL) {
439 MPASS(!TD_ON_SLEEPQ(td));
440 mtx_unlock_spin(&sc->sc_lock);
445 * Otherwise, actually go to sleep.
447 mtx_unlock_spin(&sc->sc_lock);
450 mi_switch(SW_VOL, NULL);
451 KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING"));
452 CTR3(KTR_PROC, "sleepq resume: thread %p (pid %ld, %s)",
453 (void *)td, (long)td->td_proc->p_pid, (void *)td->td_proc->p_comm);
457 * Check to see if we timed out.
460 sleepq_check_timeout(void)
464 mtx_assert(&sched_lock, MA_OWNED);
468 * If TDF_TIMEOUT is set, we timed out.
470 if (td->td_flags & TDF_TIMEOUT) {
471 td->td_flags &= ~TDF_TIMEOUT;
472 return (EWOULDBLOCK);
476 * If TDF_TIMOFAIL is set, the timeout ran after we had
477 * already been woken up.
479 if (td->td_flags & TDF_TIMOFAIL)
480 td->td_flags &= ~TDF_TIMOFAIL;
483 * If callout_stop() fails, then the timeout is running on
484 * another CPU, so synchronize with it to avoid having it
485 * accidentally wake up a subsequent sleep.
487 else if (callout_stop(&td->td_slpcallout) == 0) {
488 td->td_flags |= TDF_TIMEOUT;
490 mi_switch(SW_INVOL, NULL);
496 * Check to see if we were awoken by a signal.
499 sleepq_check_signals(void)
503 mtx_assert(&sched_lock, MA_OWNED);
506 /* We are no longer in an interruptible sleep. */
507 if (td->td_flags & TDF_SINTR)
508 td->td_flags &= ~TDF_SINTR;
510 if (td->td_flags & TDF_SLEEPABORT) {
511 td->td_flags &= ~TDF_SLEEPABORT;
512 return (td->td_intrval);
515 if (td->td_flags & TDF_INTERRUPT)
516 return (td->td_intrval);
522 * Block the current thread until it is awakened from its sleep queue.
525 sleepq_wait(void *wchan)
528 MPASS(!(curthread->td_flags & TDF_SINTR));
529 mtx_lock_spin(&sched_lock);
530 sleepq_switch(wchan);
531 mtx_unlock_spin(&sched_lock);
535 * Block the current thread until it is awakened from its sleep queue
536 * or it is interrupted by a signal.
539 sleepq_wait_sig(void *wchan)
544 rcatch = sleepq_catch_signals(wchan);
546 sleepq_switch(wchan);
548 sleepq_release(wchan);
549 rval = sleepq_check_signals();
550 mtx_unlock_spin(&sched_lock);
557 * Block the current thread until it is awakened from its sleep queue
558 * or it times out while waiting.
561 sleepq_timedwait(void *wchan)
565 MPASS(!(curthread->td_flags & TDF_SINTR));
566 mtx_lock_spin(&sched_lock);
567 sleepq_switch(wchan);
568 rval = sleepq_check_timeout();
569 mtx_unlock_spin(&sched_lock);
574 * Block the current thread until it is awakened from its sleep queue,
575 * it is interrupted by a signal, or it times out waiting to be awakened.
578 sleepq_timedwait_sig(void *wchan)
580 int rcatch, rvalt, rvals;
582 rcatch = sleepq_catch_signals(wchan);
584 sleepq_switch(wchan);
586 sleepq_release(wchan);
587 rvalt = sleepq_check_timeout();
588 rvals = sleepq_check_signals();
589 mtx_unlock_spin(&sched_lock);
598 * Removes a thread from a sleep queue and makes it
602 sleepq_resume_thread(struct sleepqueue *sq, struct thread *td, int pri)
604 struct sleepqueue_chain *sc;
607 MPASS(sq->sq_wchan != NULL);
608 MPASS(td->td_wchan == sq->sq_wchan);
609 sc = SC_LOOKUP(sq->sq_wchan);
610 mtx_assert(&sc->sc_lock, MA_OWNED);
611 mtx_assert(&sched_lock, MA_OWNED);
613 /* Remove the thread from the queue. */
614 TAILQ_REMOVE(&sq->sq_blocked, td, td_slpq);
617 * Get a sleep queue for this thread. If this is the last waiter,
618 * use the queue itself and take it out of the chain, otherwise,
619 * remove a queue from the free list.
621 if (LIST_EMPTY(&sq->sq_free)) {
622 td->td_sleepqueue = sq;
626 #ifdef SLEEPQUEUE_PROFILING
630 td->td_sleepqueue = LIST_FIRST(&sq->sq_free);
631 LIST_REMOVE(td->td_sleepqueue, sq_hash);
635 td->td_flags &= ~TDF_SINTR;
638 * Note that thread td might not be sleeping if it is running
639 * sleepq_catch_signals() on another CPU or is blocked on
640 * its proc lock to check signals. It doesn't hurt to clear
641 * the sleeping flag if it isn't set though, so we just always
642 * do it. However, we can't assert that it is set.
644 CTR3(KTR_PROC, "sleepq_wakeup: thread %p (pid %ld, %s)",
645 (void *)td, (long)td->td_proc->p_pid, td->td_proc->p_comm);
648 /* Adjust priority if requested. */
649 MPASS(pri == -1 || (pri >= PRI_MIN && pri <= PRI_MAX));
650 if (pri != -1 && td->td_priority > pri)
656 * Find the highest priority thread sleeping on a wait channel and resume it.
659 sleepq_signal(void *wchan, int flags, int pri)
661 struct sleepqueue *sq;
662 struct thread *td, *besttd;
664 CTR2(KTR_PROC, "sleepq_signal(%p, %d)", wchan, flags);
665 KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
666 sq = sleepq_lookup(wchan);
668 sleepq_release(wchan);
671 KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
672 ("%s: mismatch between sleep/wakeup and cv_*", __func__));
675 * Find the highest priority thread on the queue. If there is a
676 * tie, use the thread that first appears in the queue as it has
677 * been sleeping the longest since threads are always added to
678 * the tail of sleep queues.
681 TAILQ_FOREACH(td, &sq->sq_blocked, td_slpq) {
682 if (besttd == NULL || td->td_priority < besttd->td_priority)
685 MPASS(besttd != NULL);
686 mtx_lock_spin(&sched_lock);
687 sleepq_resume_thread(sq, besttd, pri);
688 mtx_unlock_spin(&sched_lock);
689 sleepq_release(wchan);
693 * Resume all threads sleeping on a specified wait channel.
696 sleepq_broadcast(void *wchan, int flags, int pri)
698 struct sleepqueue *sq;
700 CTR2(KTR_PROC, "sleepq_broadcast(%p, %d)", wchan, flags);
701 KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
702 sq = sleepq_lookup(wchan);
704 sleepq_release(wchan);
707 KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
708 ("%s: mismatch between sleep/wakeup and cv_*", __func__));
710 /* Resume all blocked threads on the sleep queue. */
711 mtx_lock_spin(&sched_lock);
712 while (!TAILQ_EMPTY(&sq->sq_blocked))
713 sleepq_resume_thread(sq, TAILQ_FIRST(&sq->sq_blocked), pri);
714 mtx_unlock_spin(&sched_lock);
715 sleepq_release(wchan);
719 * Time sleeping threads out. When the timeout expires, the thread is
720 * removed from the sleep queue and made runnable if it is still asleep.
723 sleepq_timeout(void *arg)
725 struct sleepqueue *sq;
730 CTR3(KTR_PROC, "sleepq_timeout: thread %p (pid %ld, %s)",
731 (void *)td, (long)td->td_proc->p_pid, (void *)td->td_proc->p_comm);
734 * First, see if the thread is asleep and get the wait channel if
737 mtx_lock_spin(&sched_lock);
738 if (TD_ON_SLEEPQ(td)) {
739 wchan = td->td_wchan;
740 mtx_unlock_spin(&sched_lock);
742 sq = sleepq_lookup(wchan);
743 mtx_lock_spin(&sched_lock);
750 * At this point, if the thread is still on the sleep queue,
751 * we have that sleep queue locked as it cannot migrate sleep
752 * queues while we dropped sched_lock. If it had resumed and
753 * was on another CPU while the lock was dropped, it would have
754 * seen that TDF_TIMEOUT and TDF_TIMOFAIL are clear and the
755 * call to callout_stop() to stop this routine would have failed
756 * meaning that it would have already set TDF_TIMEOUT to
757 * synchronize with this function.
759 if (TD_ON_SLEEPQ(td)) {
760 MPASS(td->td_wchan == wchan);
762 td->td_flags |= TDF_TIMEOUT;
763 sleepq_resume_thread(sq, td, -1);
764 mtx_unlock_spin(&sched_lock);
765 sleepq_release(wchan);
767 } else if (wchan != NULL)
768 sleepq_release(wchan);
771 * Now check for the edge cases. First, if TDF_TIMEOUT is set,
772 * then the other thread has already yielded to us, so clear
773 * the flag and resume it. If TDF_TIMEOUT is not set, then the
774 * we know that the other thread is not on a sleep queue, but it
775 * hasn't resumed execution yet. In that case, set TDF_TIMOFAIL
776 * to let it know that the timeout has already run and doesn't
777 * need to be canceled.
779 if (td->td_flags & TDF_TIMEOUT) {
780 MPASS(TD_IS_SLEEPING(td));
781 td->td_flags &= ~TDF_TIMEOUT;
785 td->td_flags |= TDF_TIMOFAIL;
786 mtx_unlock_spin(&sched_lock);
790 * Resumes a specific thread from the sleep queue associated with a specific
791 * wait channel if it is on that queue.
794 sleepq_remove(struct thread *td, void *wchan)
796 struct sleepqueue *sq;
799 * Look up the sleep queue for this wait channel, then re-check
800 * that the thread is asleep on that channel, if it is not, then
803 MPASS(wchan != NULL);
805 sq = sleepq_lookup(wchan);
806 mtx_lock_spin(&sched_lock);
807 if (!TD_ON_SLEEPQ(td) || td->td_wchan != wchan) {
808 mtx_unlock_spin(&sched_lock);
809 sleepq_release(wchan);
814 /* Thread is asleep on sleep queue sq, so wake it up. */
815 sleepq_resume_thread(sq, td, -1);
816 sleepq_release(wchan);
817 mtx_unlock_spin(&sched_lock);
821 * Abort a thread as if an interrupt had occurred. Only abort
822 * interruptible waits (unfortunately it isn't safe to abort others).
824 * XXX: What in the world does the comment below mean?
825 * Also, whatever the signal code does...
828 sleepq_abort(struct thread *td, int intrval)
832 mtx_assert(&sched_lock, MA_OWNED);
833 MPASS(TD_ON_SLEEPQ(td));
834 MPASS(td->td_flags & TDF_SINTR);
835 MPASS(intrval == EINTR || intrval == ERESTART);
838 * If the TDF_TIMEOUT flag is set, just leave. A
839 * timeout is scheduled anyhow.
841 if (td->td_flags & TDF_TIMEOUT)
844 CTR3(KTR_PROC, "sleepq_abort: thread %p (pid %ld, %s)",
845 (void *)td, (long)td->td_proc->p_pid, (void *)td->td_proc->p_comm);
846 wchan = td->td_wchan;
848 td->td_intrval = intrval;
849 td->td_flags |= TDF_SLEEPABORT;
851 mtx_unlock_spin(&sched_lock);
852 sleepq_remove(td, wchan);
853 mtx_lock_spin(&sched_lock);
857 DB_SHOW_COMMAND(sleepq, db_show_sleepqueue)
859 struct sleepqueue_chain *sc;
860 struct sleepqueue *sq;
862 struct lock_object *lock;
872 * First, see if there is an active sleep queue for the wait channel
873 * indicated by the address.
875 wchan = (void *)addr;
876 sc = SC_LOOKUP(wchan);
877 LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
878 if (sq->sq_wchan == wchan)
882 * Second, see if there is an active sleep queue at the address
885 for (i = 0; i < SC_TABLESIZE; i++)
886 LIST_FOREACH(sq, &sleepq_chains[i].sc_queues, sq_hash) {
887 if (sq == (struct sleepqueue *)addr)
891 db_printf("Unable to locate a sleep queue via %p\n", (void *)addr);
894 db_printf("Wait channel: %p\n", sq->sq_wchan);
896 db_printf("Queue type: %d\n", sq->sq_type);
898 lock = &sq->sq_lock->mtx_object;
899 db_printf("Associated Interlock: %p - (%s) %s\n", lock,
900 LOCK_CLASS(lock)->lc_name, lock->lo_name);
903 db_printf("Blocked threads:\n");
904 if (TAILQ_EMPTY(&sq->sq_blocked))
905 db_printf("\tempty\n");
907 TAILQ_FOREACH(td, &sq->sq_blocked, td_slpq) {
908 db_printf("\t%p (tid %d, pid %d, \"%s\")\n", td,
909 td->td_tid, td->td_proc->p_pid,
910 td->td_name[0] != '\0' ? td->td_name :
911 td->td_proc->p_comm);
915 /* Alias 'show sleepqueue' to 'show sleepq'. */
916 DB_SET(sleepqueue, db_show_sleepqueue, db_show_cmd_set, 0, NULL);