subr_sleepqueue.c revision 170640
1139804Simp/*-
2126324Sjhb * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org>
3126324Sjhb * All rights reserved.
4126324Sjhb *
5126324Sjhb * Redistribution and use in source and binary forms, with or without
6126324Sjhb * modification, are permitted provided that the following conditions
7126324Sjhb * are met:
8126324Sjhb * 1. Redistributions of source code must retain the above copyright
9126324Sjhb *    notice, this list of conditions and the following disclaimer.
10126324Sjhb * 2. Redistributions in binary form must reproduce the above copyright
11126324Sjhb *    notice, this list of conditions and the following disclaimer in the
12126324Sjhb *    documentation and/or other materials provided with the distribution.
13126324Sjhb * 3. Neither the name of the author nor the names of any co-contributors
14126324Sjhb *    may be used to endorse or promote products derived from this software
15126324Sjhb *    without specific prior written permission.
16126324Sjhb *
17126324Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18126324Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19126324Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20126324Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21126324Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22126324Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23126324Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24126324Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25126324Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26126324Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27126324Sjhb * SUCH DAMAGE.
28126324Sjhb */
29126324Sjhb
30126324Sjhb/*
31126324Sjhb * Implementation of sleep queues used to hold queue of threads blocked on
32126324Sjhb * a wait channel.  Sleep queues different from turnstiles in that wait
33126324Sjhb * channels are not owned by anyone, so there is no priority propagation.
34126324Sjhb * Sleep queues can also provide a timeout and can also be interrupted by
35126324Sjhb * signals.  That said, there are several similarities between the turnstile
36126324Sjhb * and sleep queue implementations.  (Note: turnstiles were implemented
37126324Sjhb * first.)  For example, both use a hash table of the same size where each
38126324Sjhb * bucket is referred to as a "chain" that contains both a spin lock and
39126324Sjhb * a linked list of queues.  An individual queue is located by using a hash
40126324Sjhb * to pick a chain, locking the chain, and then walking the chain searching
41126324Sjhb * for the queue.  This means that a wait channel object does not need to
42126324Sjhb * embed it's queue head just as locks do not embed their turnstile queue
43126324Sjhb * head.  Threads also carry around a sleep queue that they lend to the
44126324Sjhb * wait channel when blocking.  Just as in turnstiles, the queue includes
45126324Sjhb * a free list of the sleep queues of other threads blocked on the same
46126324Sjhb * wait channel in the case of multiple waiters.
47126324Sjhb *
48126324Sjhb * Some additional functionality provided by sleep queues include the
49126324Sjhb * ability to set a timeout.  The timeout is managed using a per-thread
50126324Sjhb * callout that resumes a thread if it is asleep.  A thread may also
51126324Sjhb * catch signals while it is asleep (aka an interruptible sleep).  The
52126324Sjhb * signal code uses sleepq_abort() to interrupt a sleeping thread.  Finally,
53126324Sjhb * sleep queues also provide some extra assertions.  One is not allowed to
54126324Sjhb * mix the sleep/wakeup and cv APIs for a given wait channel.  Also, one
55126324Sjhb * must consistently use the same lock to synchronize with a wait channel,
56126324Sjhb * though this check is currently only a warning for sleep/wakeup due to
57126324Sjhb * pre-existing abuse of that API.  The same lock must also be held when
58126324Sjhb * awakening threads, though that is currently only enforced for condition
59126324Sjhb * variables.
60126324Sjhb */
61126324Sjhb
62126324Sjhb#include <sys/cdefs.h>
63126324Sjhb__FBSDID("$FreeBSD: head/sys/kern/subr_sleepqueue.c 170640 2007-06-12 23:27:31Z jeff $");
64126324Sjhb
65154936Sjhb#include "opt_sleepqueue_profiling.h"
66154936Sjhb#include "opt_ddb.h"
67170640Sjeff#include "opt_sched.h"
68154936Sjhb
69126324Sjhb#include <sys/param.h>
70126324Sjhb#include <sys/systm.h>
71126324Sjhb#include <sys/lock.h>
72126324Sjhb#include <sys/kernel.h>
73126324Sjhb#include <sys/ktr.h>
74126324Sjhb#include <sys/mutex.h>
75126324Sjhb#include <sys/proc.h>
76126324Sjhb#include <sys/sched.h>
77126324Sjhb#include <sys/signalvar.h>
78126324Sjhb#include <sys/sleepqueue.h>
79131259Sjhb#include <sys/sysctl.h>
80126324Sjhb
81169666Sjeff#include <vm/uma.h>
82169666Sjeff
83154936Sjhb#ifdef DDB
84154936Sjhb#include <ddb/ddb.h>
85154936Sjhb#endif
86154936Sjhb
87126324Sjhb/*
88126324Sjhb * Constants for the hash table of sleep queue chains.  These constants are
89126324Sjhb * the same ones that 4BSD (and possibly earlier versions of BSD) used.
90126324Sjhb * Basically, we ignore the lower 8 bits of the address since most wait
91126324Sjhb * channel pointers are aligned and only look at the next 7 bits for the
92126324Sjhb * hash.  SC_TABLESIZE must be a power of two for SC_MASK to work properly.
93126324Sjhb */
94126324Sjhb#define	SC_TABLESIZE	128			/* Must be power of 2. */
95126324Sjhb#define	SC_MASK		(SC_TABLESIZE - 1)
96126324Sjhb#define	SC_SHIFT	8
97126324Sjhb#define	SC_HASH(wc)	(((uintptr_t)(wc) >> SC_SHIFT) & SC_MASK)
98126324Sjhb#define	SC_LOOKUP(wc)	&sleepq_chains[SC_HASH(wc)]
99165272Skmacy#define NR_SLEEPQS      2
100126324Sjhb/*
101126324Sjhb * There two different lists of sleep queues.  Both lists are connected
102126324Sjhb * via the sq_hash entries.  The first list is the sleep queue chain list
103126324Sjhb * that a sleep queue is on when it is attached to a wait channel.  The
104126324Sjhb * second list is the free list hung off of a sleep queue that is attached
105126324Sjhb * to a wait channel.
106126324Sjhb *
107126324Sjhb * Each sleep queue also contains the wait channel it is attached to, the
108126324Sjhb * list of threads blocked on that wait channel, flags specific to the
109126324Sjhb * wait channel, and the lock used to synchronize with a wait channel.
110126324Sjhb * The flags are used to catch mismatches between the various consumers
111126324Sjhb * of the sleep queue API (e.g. sleep/wakeup and condition variables).
112126324Sjhb * The lock pointer is only used when invariants are enabled for various
113126324Sjhb * debugging checks.
114126324Sjhb *
115126324Sjhb * Locking key:
116126324Sjhb *  c - sleep queue chain lock
117126324Sjhb */
118126324Sjhbstruct sleepqueue {
119165272Skmacy	TAILQ_HEAD(, thread) sq_blocked[NR_SLEEPQS];	/* (c) Blocked threads. */
120126324Sjhb	LIST_ENTRY(sleepqueue) sq_hash;		/* (c) Chain and free list. */
121126324Sjhb	LIST_HEAD(, sleepqueue) sq_free;	/* (c) Free queues. */
122126324Sjhb	void	*sq_wchan;			/* (c) Wait channel. */
123136445Sjhb#ifdef INVARIANTS
124134013Sjhb	int	sq_type;			/* (c) Queue type. */
125164325Spjd	struct lock_object *sq_lock;		/* (c) Associated lock. */
126126324Sjhb#endif
127126324Sjhb};
128126324Sjhb
129126324Sjhbstruct sleepqueue_chain {
130126324Sjhb	LIST_HEAD(, sleepqueue) sc_queues;	/* List of sleep queues. */
131126324Sjhb	struct mtx sc_lock;			/* Spin lock for this chain. */
132131259Sjhb#ifdef SLEEPQUEUE_PROFILING
133131259Sjhb	u_int	sc_depth;			/* Length of sc_queues. */
134131259Sjhb	u_int	sc_max_depth;			/* Max length of sc_queues. */
135131259Sjhb#endif
136126324Sjhb};
137126324Sjhb
138131259Sjhb#ifdef SLEEPQUEUE_PROFILING
139131259Sjhbu_int sleepq_max_depth;
140131259SjhbSYSCTL_NODE(_debug, OID_AUTO, sleepq, CTLFLAG_RD, 0, "sleepq profiling");
141131259SjhbSYSCTL_NODE(_debug_sleepq, OID_AUTO, chains, CTLFLAG_RD, 0,
142131259Sjhb    "sleepq chain stats");
143131259SjhbSYSCTL_UINT(_debug_sleepq, OID_AUTO, max_depth, CTLFLAG_RD, &sleepq_max_depth,
144131259Sjhb    0, "maxmimum depth achieved of a single chain");
145131259Sjhb#endif
146126324Sjhbstatic struct sleepqueue_chain sleepq_chains[SC_TABLESIZE];
147169666Sjeffstatic uma_zone_t sleepq_zone;
148126324Sjhb
149126324Sjhb/*
150126324Sjhb * Prototypes for non-exported routines.
151126324Sjhb */
152165272Skmacystatic int	sleepq_catch_signals(void *wchan);
153165272Skmacystatic int	sleepq_check_signals(void);
154126324Sjhbstatic int	sleepq_check_timeout(void);
155169666Sjeff#ifdef INVARIANTS
156169666Sjeffstatic void	sleepq_dtor(void *mem, int size, void *arg);
157169666Sjeff#endif
158169666Sjeffstatic int	sleepq_init(void *mem, int size, int flags);
159169666Sjeffstatic void	sleepq_resume_thread(struct sleepqueue *sq, struct thread *td,
160169666Sjeff		    int pri);
161126324Sjhbstatic void	sleepq_switch(void *wchan);
162126324Sjhbstatic void	sleepq_timeout(void *arg);
163126324Sjhb
164126324Sjhb/*
165126324Sjhb * Early initialization of sleep queues that is called from the sleepinit()
166126324Sjhb * SYSINIT.
167126324Sjhb */
168126324Sjhbvoid
169126324Sjhbinit_sleepqueues(void)
170126324Sjhb{
171131259Sjhb#ifdef SLEEPQUEUE_PROFILING
172131259Sjhb	struct sysctl_oid *chain_oid;
173131259Sjhb	char chain_name[10];
174131259Sjhb#endif
175126324Sjhb	int i;
176126324Sjhb
177126324Sjhb	for (i = 0; i < SC_TABLESIZE; i++) {
178126324Sjhb		LIST_INIT(&sleepq_chains[i].sc_queues);
179126324Sjhb		mtx_init(&sleepq_chains[i].sc_lock, "sleepq chain", NULL,
180126324Sjhb		    MTX_SPIN);
181131259Sjhb#ifdef SLEEPQUEUE_PROFILING
182131259Sjhb		snprintf(chain_name, sizeof(chain_name), "%d", i);
183131259Sjhb		chain_oid = SYSCTL_ADD_NODE(NULL,
184131259Sjhb		    SYSCTL_STATIC_CHILDREN(_debug_sleepq_chains), OID_AUTO,
185131259Sjhb		    chain_name, CTLFLAG_RD, NULL, "sleepq chain stats");
186131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
187131259Sjhb		    "depth", CTLFLAG_RD, &sleepq_chains[i].sc_depth, 0, NULL);
188131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
189131259Sjhb		    "max_depth", CTLFLAG_RD, &sleepq_chains[i].sc_max_depth, 0,
190131259Sjhb		    NULL);
191131259Sjhb#endif
192126324Sjhb	}
193169666Sjeff	sleepq_zone = uma_zcreate("SLEEPQUEUE", sizeof(struct sleepqueue),
194169666Sjeff#ifdef INVARIANTS
195169666Sjeff	    NULL, sleepq_dtor, sleepq_init, NULL, UMA_ALIGN_CACHE, 0);
196169666Sjeff#else
197169666Sjeff	    NULL, NULL, sleepq_init, NULL, UMA_ALIGN_CACHE, 0);
198169666Sjeff#endif
199169666Sjeff
200126324Sjhb	thread0.td_sleepqueue = sleepq_alloc();
201126324Sjhb}
202126324Sjhb
203126324Sjhb/*
204169666Sjeff * Get a sleep queue for a new thread.
205126324Sjhb */
206126324Sjhbstruct sleepqueue *
207126324Sjhbsleepq_alloc(void)
208126324Sjhb{
209126324Sjhb
210169666Sjeff	return (uma_zalloc(sleepq_zone, M_WAITOK));
211126324Sjhb}
212126324Sjhb
213126324Sjhb/*
214126324Sjhb * Free a sleep queue when a thread is destroyed.
215126324Sjhb */
216126324Sjhbvoid
217126324Sjhbsleepq_free(struct sleepqueue *sq)
218126324Sjhb{
219126324Sjhb
220169666Sjeff	uma_zfree(sleepq_zone, sq);
221126324Sjhb}
222126324Sjhb
223126324Sjhb/*
224136445Sjhb * Lock the sleep queue chain associated with the specified wait channel.
225136445Sjhb */
226136445Sjhbvoid
227136445Sjhbsleepq_lock(void *wchan)
228136445Sjhb{
229136445Sjhb	struct sleepqueue_chain *sc;
230136445Sjhb
231136445Sjhb	sc = SC_LOOKUP(wchan);
232136445Sjhb	mtx_lock_spin(&sc->sc_lock);
233136445Sjhb}
234136445Sjhb
235136445Sjhb/*
236126324Sjhb * Look up the sleep queue associated with a given wait channel in the hash
237136445Sjhb * table locking the associated sleep queue chain.  If no queue is found in
238136445Sjhb * the table, NULL is returned.
239126324Sjhb */
240126324Sjhbstruct sleepqueue *
241126324Sjhbsleepq_lookup(void *wchan)
242126324Sjhb{
243126324Sjhb	struct sleepqueue_chain *sc;
244126324Sjhb	struct sleepqueue *sq;
245126324Sjhb
246126324Sjhb	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
247126324Sjhb	sc = SC_LOOKUP(wchan);
248136445Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
249126324Sjhb	LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
250126324Sjhb		if (sq->sq_wchan == wchan)
251126324Sjhb			return (sq);
252126324Sjhb	return (NULL);
253126324Sjhb}
254126324Sjhb
255126324Sjhb/*
256126324Sjhb * Unlock the sleep queue chain associated with a given wait channel.
257126324Sjhb */
258126324Sjhbvoid
259126324Sjhbsleepq_release(void *wchan)
260126324Sjhb{
261126324Sjhb	struct sleepqueue_chain *sc;
262126324Sjhb
263126324Sjhb	sc = SC_LOOKUP(wchan);
264126324Sjhb	mtx_unlock_spin(&sc->sc_lock);
265126324Sjhb}
266126324Sjhb
267126324Sjhb/*
268137277Sjhb * Places the current thread on the sleep queue for the specified wait
269126324Sjhb * channel.  If INVARIANTS is enabled, then it associates the passed in
270126324Sjhb * lock with the sleepq to make sure it is held when that sleep queue is
271126324Sjhb * woken up.
272126324Sjhb */
273126324Sjhbvoid
274165272Skmacysleepq_add(void *wchan, struct lock_object *lock, const char *wmesg, int flags,
275165272Skmacy    int queue)
276126324Sjhb{
277126324Sjhb	struct sleepqueue_chain *sc;
278136445Sjhb	struct sleepqueue *sq;
279137277Sjhb	struct thread *td;
280126324Sjhb
281126324Sjhb	td = curthread;
282126324Sjhb	sc = SC_LOOKUP(wchan);
283126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
284126324Sjhb	MPASS(td->td_sleepqueue != NULL);
285126324Sjhb	MPASS(wchan != NULL);
286165272Skmacy	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
287126324Sjhb
288150177Sjhb	/* If this thread is not allowed to sleep, die a horrible death. */
289150177Sjhb	KASSERT(!(td->td_pflags & TDP_NOSLEEPING),
290152221Simp	    ("Trying sleep, but thread marked as sleeping prohibited"));
291150177Sjhb
292136445Sjhb	/* Look up the sleep queue associated with the wait channel 'wchan'. */
293136445Sjhb	sq = sleepq_lookup(wchan);
294136445Sjhb
295136445Sjhb	/*
296136445Sjhb	 * If the wait channel does not already have a sleep queue, use
297136445Sjhb	 * this thread's sleep queue.  Otherwise, insert the current thread
298136445Sjhb	 * into the sleep queue already in use by this wait channel.
299136445Sjhb	 */
300126324Sjhb	if (sq == NULL) {
301165272Skmacy#ifdef INVARIANTS
302165292Skmacy		int i;
303165291Sache
304165292Skmacy		sq = td->td_sleepqueue;
305165292Skmacy		for (i = 0; i < NR_SLEEPQS; i++)
306165292Skmacy			KASSERT(TAILQ_EMPTY(&sq->sq_blocked[i]),
307165272Skmacy				("thread's sleep queue %d is not empty", i));
308165272Skmacy		KASSERT(LIST_EMPTY(&sq->sq_free),
309165272Skmacy		    ("thread's sleep queue has a non-empty free list"));
310165272Skmacy		KASSERT(sq->sq_wchan == NULL, ("stale sq_wchan pointer"));
311165292Skmacy		sq->sq_lock = lock;
312165292Skmacy		sq->sq_type = flags & SLEEPQ_TYPE;
313165272Skmacy#endif
314131259Sjhb#ifdef SLEEPQUEUE_PROFILING
315131259Sjhb		sc->sc_depth++;
316131259Sjhb		if (sc->sc_depth > sc->sc_max_depth) {
317131259Sjhb			sc->sc_max_depth = sc->sc_depth;
318131259Sjhb			if (sc->sc_max_depth > sleepq_max_depth)
319131259Sjhb				sleepq_max_depth = sc->sc_max_depth;
320131259Sjhb		}
321131259Sjhb#endif
322165292Skmacy		sq = td->td_sleepqueue;
323126324Sjhb		LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
324126324Sjhb		sq->sq_wchan = wchan;
325126324Sjhb	} else {
326126324Sjhb		MPASS(wchan == sq->sq_wchan);
327126488Sjhb		MPASS(lock == sq->sq_lock);
328136445Sjhb		MPASS((flags & SLEEPQ_TYPE) == sq->sq_type);
329126324Sjhb		LIST_INSERT_HEAD(&sq->sq_free, td->td_sleepqueue, sq_hash);
330126324Sjhb	}
331165272Skmacy	TAILQ_INSERT_TAIL(&sq->sq_blocked[queue], td, td_slpq);
332126324Sjhb	td->td_sleepqueue = NULL;
333165272Skmacy	td->td_sqqueue = queue;
334126324Sjhb	td->td_wchan = wchan;
335126324Sjhb	td->td_wmesg = wmesg;
336155741Sdavidxu	if (flags & SLEEPQ_INTERRUPTIBLE) {
337134013Sjhb		td->td_flags |= TDF_SINTR;
338155741Sdavidxu		td->td_flags &= ~TDF_SLEEPABORT;
339155741Sdavidxu	}
340126324Sjhb}
341126324Sjhb
342126324Sjhb/*
343126324Sjhb * Sets a timeout that will remove the current thread from the specified
344126324Sjhb * sleep queue after timo ticks if the thread has not already been awakened.
345126324Sjhb */
346126324Sjhbvoid
347126885Sjhbsleepq_set_timeout(void *wchan, int timo)
348126324Sjhb{
349126324Sjhb	struct sleepqueue_chain *sc;
350126324Sjhb	struct thread *td;
351126324Sjhb
352126324Sjhb	td = curthread;
353126324Sjhb	sc = SC_LOOKUP(wchan);
354126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
355126324Sjhb	MPASS(TD_ON_SLEEPQ(td));
356126324Sjhb	MPASS(td->td_sleepqueue == NULL);
357126324Sjhb	MPASS(wchan != NULL);
358126324Sjhb	callout_reset(&td->td_slpcallout, timo, sleepq_timeout, td);
359126324Sjhb}
360126324Sjhb
361126324Sjhb/*
362126324Sjhb * Marks the pending sleep of the current thread as interruptible and
363126324Sjhb * makes an initial check for pending signals before putting a thread
364170294Sjeff * to sleep. Enters and exits with the thread lock held.  Thread lock
365170294Sjeff * may have transitioned from the sleepq lock to a run lock.
366126324Sjhb */
367155741Sdavidxustatic int
368126324Sjhbsleepq_catch_signals(void *wchan)
369126324Sjhb{
370126324Sjhb	struct sleepqueue_chain *sc;
371126324Sjhb	struct sleepqueue *sq;
372126324Sjhb	struct thread *td;
373126324Sjhb	struct proc *p;
374155741Sdavidxu	struct sigacts *ps;
375155741Sdavidxu	int sig, ret;
376126324Sjhb
377126324Sjhb	td = curthread;
378155741Sdavidxu	p = curproc;
379126324Sjhb	sc = SC_LOOKUP(wchan);
380126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
381126324Sjhb	MPASS(wchan != NULL);
382129241Sbde	CTR3(KTR_PROC, "sleepq catching signals: thread %p (pid %ld, %s)",
383155741Sdavidxu		(void *)td, (long)p->p_pid, p->p_comm);
384126324Sjhb
385155741Sdavidxu	mtx_unlock_spin(&sc->sc_lock);
386126324Sjhb
387126324Sjhb	/* See if there are any pending signals for this thread. */
388126324Sjhb	PROC_LOCK(p);
389155741Sdavidxu	ps = p->p_sigacts;
390155741Sdavidxu	mtx_lock(&ps->ps_mtx);
391126324Sjhb	sig = cursig(td);
392155741Sdavidxu	if (sig == 0) {
393155741Sdavidxu		mtx_unlock(&ps->ps_mtx);
394155741Sdavidxu		ret = thread_suspend_check(1);
395155741Sdavidxu		MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
396155741Sdavidxu	} else {
397155741Sdavidxu		if (SIGISMEMBER(ps->ps_sigintr, sig))
398155741Sdavidxu			ret = EINTR;
399155741Sdavidxu		else
400155741Sdavidxu			ret = ERESTART;
401155741Sdavidxu		mtx_unlock(&ps->ps_mtx);
402155741Sdavidxu	}
403170294Sjeff	/*
404170294Sjeff	 * Lock sleepq chain before unlocking proc
405170294Sjeff	 * without this, we could lose a race.
406170294Sjeff	 */
407170294Sjeff	mtx_lock_spin(&sc->sc_lock);
408170294Sjeff	PROC_UNLOCK(p);
409170294Sjeff	thread_lock(td);
410155932Sdavidxu	if (ret == 0) {
411170294Sjeff		if (!(td->td_flags & TDF_INTERRUPT)) {
412170294Sjeff			sleepq_switch(wchan);
413155932Sdavidxu			return (0);
414170294Sjeff		}
415155932Sdavidxu		/* KSE threads tried unblocking us. */
416155932Sdavidxu		ret = td->td_intrval;
417170294Sjeff		MPASS(ret == EINTR || ret == ERESTART || ret == EWOULDBLOCK);
418155741Sdavidxu	}
419155936Sdavidxu	/*
420155936Sdavidxu	 * There were pending signals and this thread is still
421155936Sdavidxu	 * on the sleep queue, remove it from the sleep queue.
422155936Sdavidxu	 */
423170294Sjeff	if (TD_ON_SLEEPQ(td)) {
424170294Sjeff		sq = sleepq_lookup(wchan);
425155932Sdavidxu		sleepq_resume_thread(sq, td, -1);
426170294Sjeff	}
427170294Sjeff	mtx_unlock_spin(&sc->sc_lock);
428170294Sjeff	MPASS(td->td_lock != &sc->sc_lock);
429155741Sdavidxu	return (ret);
430126324Sjhb}
431126324Sjhb
432126324Sjhb/*
433170294Sjeff * Switches to another thread if we are still asleep on a sleep queue.
434170294Sjeff * Returns with thread lock.
435126324Sjhb */
436126324Sjhbstatic void
437126324Sjhbsleepq_switch(void *wchan)
438126324Sjhb{
439126324Sjhb	struct sleepqueue_chain *sc;
440126324Sjhb	struct thread *td;
441126324Sjhb
442126324Sjhb	td = curthread;
443126324Sjhb	sc = SC_LOOKUP(wchan);
444126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
445170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
446170294Sjeff	/* We were removed */
447126324Sjhb	if (td->td_sleepqueue != NULL) {
448126324Sjhb		mtx_unlock_spin(&sc->sc_lock);
449126324Sjhb		return;
450126324Sjhb	}
451170294Sjeff	thread_lock_set(td, &sc->sc_lock);
452126324Sjhb
453170294Sjeff	MPASS(td->td_sleepqueue == NULL);
454126324Sjhb	sched_sleep(td);
455126324Sjhb	TD_SET_SLEEPING(td);
456170294Sjeff	SCHED_STAT_INC(switch_sleepq);
457131473Sjhb	mi_switch(SW_VOL, NULL);
458126324Sjhb	KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING"));
459129241Sbde	CTR3(KTR_PROC, "sleepq resume: thread %p (pid %ld, %s)",
460129241Sbde	    (void *)td, (long)td->td_proc->p_pid, (void *)td->td_proc->p_comm);
461126324Sjhb}
462126324Sjhb
463126324Sjhb/*
464126324Sjhb * Check to see if we timed out.
465126324Sjhb */
466126324Sjhbstatic int
467126324Sjhbsleepq_check_timeout(void)
468126324Sjhb{
469126324Sjhb	struct thread *td;
470126324Sjhb
471126324Sjhb	td = curthread;
472170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
473126324Sjhb
474126324Sjhb	/*
475126324Sjhb	 * If TDF_TIMEOUT is set, we timed out.
476126324Sjhb	 */
477126324Sjhb	if (td->td_flags & TDF_TIMEOUT) {
478126324Sjhb		td->td_flags &= ~TDF_TIMEOUT;
479126324Sjhb		return (EWOULDBLOCK);
480126324Sjhb	}
481126324Sjhb
482126324Sjhb	/*
483126324Sjhb	 * If TDF_TIMOFAIL is set, the timeout ran after we had
484126324Sjhb	 * already been woken up.
485126324Sjhb	 */
486126324Sjhb	if (td->td_flags & TDF_TIMOFAIL)
487126324Sjhb		td->td_flags &= ~TDF_TIMOFAIL;
488126324Sjhb
489126324Sjhb	/*
490126324Sjhb	 * If callout_stop() fails, then the timeout is running on
491126324Sjhb	 * another CPU, so synchronize with it to avoid having it
492126324Sjhb	 * accidentally wake up a subsequent sleep.
493126324Sjhb	 */
494126324Sjhb	else if (callout_stop(&td->td_slpcallout) == 0) {
495126324Sjhb		td->td_flags |= TDF_TIMEOUT;
496126324Sjhb		TD_SET_SLEEPING(td);
497170294Sjeff		SCHED_STAT_INC(switch_sleepqtimo);
498131473Sjhb		mi_switch(SW_INVOL, NULL);
499126324Sjhb	}
500126324Sjhb	return (0);
501126324Sjhb}
502126324Sjhb
503126324Sjhb/*
504126324Sjhb * Check to see if we were awoken by a signal.
505126324Sjhb */
506126324Sjhbstatic int
507126324Sjhbsleepq_check_signals(void)
508126324Sjhb{
509126324Sjhb	struct thread *td;
510126324Sjhb
511126324Sjhb	td = curthread;
512170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
513126324Sjhb
514126324Sjhb	/* We are no longer in an interruptible sleep. */
515155741Sdavidxu	if (td->td_flags & TDF_SINTR)
516155741Sdavidxu		td->td_flags &= ~TDF_SINTR;
517126324Sjhb
518155741Sdavidxu	if (td->td_flags & TDF_SLEEPABORT) {
519155741Sdavidxu		td->td_flags &= ~TDF_SLEEPABORT;
520155741Sdavidxu		return (td->td_intrval);
521155741Sdavidxu	}
522155741Sdavidxu
523126324Sjhb	if (td->td_flags & TDF_INTERRUPT)
524126324Sjhb		return (td->td_intrval);
525155741Sdavidxu
526126324Sjhb	return (0);
527126324Sjhb}
528126324Sjhb
529126324Sjhb/*
530126324Sjhb * Block the current thread until it is awakened from its sleep queue.
531126324Sjhb */
532126324Sjhbvoid
533126324Sjhbsleepq_wait(void *wchan)
534126324Sjhb{
535170294Sjeff	struct thread *td;
536126324Sjhb
537170294Sjeff	td = curthread;
538170294Sjeff	MPASS(!(td->td_flags & TDF_SINTR));
539170294Sjeff	thread_lock(td);
540126324Sjhb	sleepq_switch(wchan);
541170294Sjeff	thread_unlock(td);
542126324Sjhb}
543126324Sjhb
544126324Sjhb/*
545126324Sjhb * Block the current thread until it is awakened from its sleep queue
546126324Sjhb * or it is interrupted by a signal.
547126324Sjhb */
548126324Sjhbint
549126324Sjhbsleepq_wait_sig(void *wchan)
550126324Sjhb{
551155741Sdavidxu	int rcatch;
552126324Sjhb	int rval;
553126324Sjhb
554155741Sdavidxu	rcatch = sleepq_catch_signals(wchan);
555126324Sjhb	rval = sleepq_check_signals();
556170294Sjeff	thread_unlock(curthread);
557155741Sdavidxu	if (rcatch)
558155741Sdavidxu		return (rcatch);
559126324Sjhb	return (rval);
560126324Sjhb}
561126324Sjhb
562126324Sjhb/*
563126324Sjhb * Block the current thread until it is awakened from its sleep queue
564126324Sjhb * or it times out while waiting.
565126324Sjhb */
566126324Sjhbint
567131249Sjhbsleepq_timedwait(void *wchan)
568126324Sjhb{
569170294Sjeff	struct thread *td;
570126324Sjhb	int rval;
571126324Sjhb
572170294Sjeff	td = curthread;
573170294Sjeff	MPASS(!(td->td_flags & TDF_SINTR));
574170294Sjeff	thread_lock(td);
575126324Sjhb	sleepq_switch(wchan);
576126324Sjhb	rval = sleepq_check_timeout();
577170294Sjeff	thread_unlock(td);
578170294Sjeff
579131249Sjhb	return (rval);
580126324Sjhb}
581126324Sjhb
582126324Sjhb/*
583126324Sjhb * Block the current thread until it is awakened from its sleep queue,
584126324Sjhb * it is interrupted by a signal, or it times out waiting to be awakened.
585126324Sjhb */
586126324Sjhbint
587155741Sdavidxusleepq_timedwait_sig(void *wchan)
588126324Sjhb{
589155741Sdavidxu	int rcatch, rvalt, rvals;
590126324Sjhb
591155741Sdavidxu	rcatch = sleepq_catch_signals(wchan);
592126324Sjhb	rvalt = sleepq_check_timeout();
593126324Sjhb	rvals = sleepq_check_signals();
594170294Sjeff	thread_unlock(curthread);
595155741Sdavidxu	if (rcatch)
596155741Sdavidxu		return (rcatch);
597155741Sdavidxu	if (rvals)
598126324Sjhb		return (rvals);
599155741Sdavidxu	return (rvalt);
600126324Sjhb}
601126324Sjhb
602126324Sjhb/*
603145056Sjhb * Removes a thread from a sleep queue and makes it
604145056Sjhb * runnable.
605126324Sjhb */
606126324Sjhbstatic void
607145056Sjhbsleepq_resume_thread(struct sleepqueue *sq, struct thread *td, int pri)
608126324Sjhb{
609126324Sjhb	struct sleepqueue_chain *sc;
610126324Sjhb
611126324Sjhb	MPASS(td != NULL);
612126324Sjhb	MPASS(sq->sq_wchan != NULL);
613126324Sjhb	MPASS(td->td_wchan == sq->sq_wchan);
614165272Skmacy	MPASS(td->td_sqqueue < NR_SLEEPQS && td->td_sqqueue >= 0);
615170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
616126324Sjhb	sc = SC_LOOKUP(sq->sq_wchan);
617126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
618126324Sjhb
619126324Sjhb	/* Remove the thread from the queue. */
620165272Skmacy	TAILQ_REMOVE(&sq->sq_blocked[td->td_sqqueue], td, td_slpq);
621126324Sjhb
622126324Sjhb	/*
623126324Sjhb	 * Get a sleep queue for this thread.  If this is the last waiter,
624126324Sjhb	 * use the queue itself and take it out of the chain, otherwise,
625126324Sjhb	 * remove a queue from the free list.
626126324Sjhb	 */
627126324Sjhb	if (LIST_EMPTY(&sq->sq_free)) {
628126324Sjhb		td->td_sleepqueue = sq;
629126324Sjhb#ifdef INVARIANTS
630126324Sjhb		sq->sq_wchan = NULL;
631126324Sjhb#endif
632131259Sjhb#ifdef SLEEPQUEUE_PROFILING
633131259Sjhb		sc->sc_depth--;
634131259Sjhb#endif
635126324Sjhb	} else
636126324Sjhb		td->td_sleepqueue = LIST_FIRST(&sq->sq_free);
637126324Sjhb	LIST_REMOVE(td->td_sleepqueue, sq_hash);
638126324Sjhb
639129188Sjhb	td->td_wmesg = NULL;
640129188Sjhb	td->td_wchan = NULL;
641157743Sdavidxu	td->td_flags &= ~TDF_SINTR;
642129188Sjhb
643126324Sjhb	/*
644129188Sjhb	 * Note that thread td might not be sleeping if it is running
645129188Sjhb	 * sleepq_catch_signals() on another CPU or is blocked on
646129188Sjhb	 * its proc lock to check signals.  It doesn't hurt to clear
647129188Sjhb	 * the sleeping flag if it isn't set though, so we just always
648129188Sjhb	 * do it.  However, we can't assert that it is set.
649126324Sjhb	 */
650129241Sbde	CTR3(KTR_PROC, "sleepq_wakeup: thread %p (pid %ld, %s)",
651129241Sbde	    (void *)td, (long)td->td_proc->p_pid, td->td_proc->p_comm);
652126324Sjhb	TD_CLR_SLEEPING(td);
653126324Sjhb
654126324Sjhb	/* Adjust priority if requested. */
655126324Sjhb	MPASS(pri == -1 || (pri >= PRI_MIN && pri <= PRI_MAX));
656126324Sjhb	if (pri != -1 && td->td_priority > pri)
657136439Sups		sched_prio(td, pri);
658126324Sjhb	setrunnable(td);
659126324Sjhb}
660126324Sjhb
661169666Sjeff#ifdef INVARIANTS
662126324Sjhb/*
663169666Sjeff * UMA zone item deallocator.
664169666Sjeff */
665169666Sjeffstatic void
666169666Sjeffsleepq_dtor(void *mem, int size, void *arg)
667169666Sjeff{
668169666Sjeff	struct sleepqueue *sq;
669169666Sjeff	int i;
670169666Sjeff
671169666Sjeff	sq = mem;
672169666Sjeff	for (i = 0; i < NR_SLEEPQS; i++)
673169666Sjeff		MPASS(TAILQ_EMPTY(&sq->sq_blocked[i]));
674169666Sjeff}
675169666Sjeff#endif
676169666Sjeff
677169666Sjeff/*
678169666Sjeff * UMA zone item initializer.
679169666Sjeff */
680169666Sjeffstatic int
681169666Sjeffsleepq_init(void *mem, int size, int flags)
682169666Sjeff{
683169666Sjeff	struct sleepqueue *sq;
684169666Sjeff	int i;
685169666Sjeff
686169666Sjeff	bzero(mem, size);
687169666Sjeff	sq = mem;
688169666Sjeff	for (i = 0; i < NR_SLEEPQS; i++)
689169666Sjeff		TAILQ_INIT(&sq->sq_blocked[i]);
690169666Sjeff	LIST_INIT(&sq->sq_free);
691169666Sjeff	return (0);
692169666Sjeff}
693169666Sjeff
694169666Sjeff/*
695126324Sjhb * Find the highest priority thread sleeping on a wait channel and resume it.
696126324Sjhb */
697126324Sjhbvoid
698165272Skmacysleepq_signal(void *wchan, int flags, int pri, int queue)
699126324Sjhb{
700126324Sjhb	struct sleepqueue *sq;
701137277Sjhb	struct thread *td, *besttd;
702126324Sjhb
703126324Sjhb	CTR2(KTR_PROC, "sleepq_signal(%p, %d)", wchan, flags);
704126324Sjhb	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
705165272Skmacy	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
706126324Sjhb	sq = sleepq_lookup(wchan);
707170294Sjeff	if (sq == NULL)
708126324Sjhb		return;
709134013Sjhb	KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
710126324Sjhb	    ("%s: mismatch between sleep/wakeup and cv_*", __func__));
711129188Sjhb
712137277Sjhb	/*
713137277Sjhb	 * Find the highest priority thread on the queue.  If there is a
714137277Sjhb	 * tie, use the thread that first appears in the queue as it has
715137277Sjhb	 * been sleeping the longest since threads are always added to
716137277Sjhb	 * the tail of sleep queues.
717137277Sjhb	 */
718137277Sjhb	besttd = NULL;
719165272Skmacy	TAILQ_FOREACH(td, &sq->sq_blocked[queue], td_slpq) {
720137277Sjhb		if (besttd == NULL || td->td_priority < besttd->td_priority)
721137277Sjhb			besttd = td;
722137277Sjhb	}
723137277Sjhb	MPASS(besttd != NULL);
724170294Sjeff	thread_lock(besttd);
725145056Sjhb	sleepq_resume_thread(sq, besttd, pri);
726170294Sjeff	thread_unlock(besttd);
727126324Sjhb}
728126324Sjhb
729126324Sjhb/*
730126324Sjhb * Resume all threads sleeping on a specified wait channel.
731126324Sjhb */
732126324Sjhbvoid
733165272Skmacysleepq_broadcast(void *wchan, int flags, int pri, int queue)
734126324Sjhb{
735126324Sjhb	struct sleepqueue *sq;
736170294Sjeff	struct thread *td;
737126324Sjhb
738126324Sjhb	CTR2(KTR_PROC, "sleepq_broadcast(%p, %d)", wchan, flags);
739126324Sjhb	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
740165272Skmacy	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
741126324Sjhb	sq = sleepq_lookup(wchan);
742126324Sjhb	if (sq == NULL) {
743126324Sjhb		sleepq_release(wchan);
744126324Sjhb		return;
745126324Sjhb	}
746134013Sjhb	KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
747126324Sjhb	    ("%s: mismatch between sleep/wakeup and cv_*", __func__));
748129188Sjhb
749145056Sjhb	/* Resume all blocked threads on the sleep queue. */
750170294Sjeff	while (!TAILQ_EMPTY(&sq->sq_blocked[queue])) {
751170294Sjeff		td = TAILQ_FIRST(&sq->sq_blocked[queue]);
752170294Sjeff		thread_lock(td);
753170294Sjeff		sleepq_resume_thread(sq, td, pri);
754170294Sjeff		thread_unlock(td);
755170294Sjeff	}
756126324Sjhb	sleepq_release(wchan);
757126324Sjhb}
758126324Sjhb
759126324Sjhb/*
760126324Sjhb * Time sleeping threads out.  When the timeout expires, the thread is
761126324Sjhb * removed from the sleep queue and made runnable if it is still asleep.
762126324Sjhb */
763126324Sjhbstatic void
764126324Sjhbsleepq_timeout(void *arg)
765126324Sjhb{
766170294Sjeff	struct sleepqueue_chain *sc;
767126324Sjhb	struct sleepqueue *sq;
768126324Sjhb	struct thread *td;
769126324Sjhb	void *wchan;
770126324Sjhb
771129241Sbde	td = arg;
772129241Sbde	CTR3(KTR_PROC, "sleepq_timeout: thread %p (pid %ld, %s)",
773129241Sbde	    (void *)td, (long)td->td_proc->p_pid, (void *)td->td_proc->p_comm);
774126324Sjhb
775126324Sjhb	/*
776126324Sjhb	 * First, see if the thread is asleep and get the wait channel if
777126324Sjhb	 * it is.
778126324Sjhb	 */
779170294Sjeff	thread_lock(td);
780170294Sjeff	if (TD_IS_SLEEPING(td) && TD_ON_SLEEPQ(td)) {
781126324Sjhb		wchan = td->td_wchan;
782170294Sjeff		sc = SC_LOOKUP(wchan);
783170294Sjeff		MPASS(td->td_lock == &sc->sc_lock);
784126324Sjhb		sq = sleepq_lookup(wchan);
785170294Sjeff		MPASS(sq != NULL);
786170294Sjeff		td->td_flags |= TDF_TIMEOUT;
787170294Sjeff		sleepq_resume_thread(sq, td, -1);
788170294Sjeff		thread_unlock(td);
789170294Sjeff		return;
790126324Sjhb	}
791126324Sjhb	/*
792170294Sjeff	 * If the thread is on the SLEEPQ but not sleeping and we have it
793170294Sjeff	 * locked it must be in sleepq_catch_signals().  Let it know we've
794170294Sjeff 	 * timedout here so it can remove itself.
795126324Sjhb	 */
796126324Sjhb	if (TD_ON_SLEEPQ(td)) {
797170294Sjeff		td->td_flags |= TDF_TIMEOUT | TDF_INTERRUPT;
798170294Sjeff		td->td_intrval = EWOULDBLOCK;
799170294Sjeff		thread_unlock(td);
800126324Sjhb		return;
801170294Sjeff	}
802126324Sjhb
803126324Sjhb	/*
804126324Sjhb	 * Now check for the edge cases.  First, if TDF_TIMEOUT is set,
805126324Sjhb	 * then the other thread has already yielded to us, so clear
806126324Sjhb	 * the flag and resume it.  If TDF_TIMEOUT is not set, then the
807126324Sjhb	 * we know that the other thread is not on a sleep queue, but it
808126324Sjhb	 * hasn't resumed execution yet.  In that case, set TDF_TIMOFAIL
809126324Sjhb	 * to let it know that the timeout has already run and doesn't
810126324Sjhb	 * need to be canceled.
811126324Sjhb	 */
812126324Sjhb	if (td->td_flags & TDF_TIMEOUT) {
813127085Sjhb		MPASS(TD_IS_SLEEPING(td));
814126324Sjhb		td->td_flags &= ~TDF_TIMEOUT;
815126324Sjhb		TD_CLR_SLEEPING(td);
816126324Sjhb		setrunnable(td);
817126324Sjhb	} else
818126324Sjhb		td->td_flags |= TDF_TIMOFAIL;
819170294Sjeff	thread_unlock(td);
820126324Sjhb}
821126324Sjhb
822126324Sjhb/*
823126324Sjhb * Resumes a specific thread from the sleep queue associated with a specific
824126324Sjhb * wait channel if it is on that queue.
825126324Sjhb */
826126324Sjhbvoid
827126324Sjhbsleepq_remove(struct thread *td, void *wchan)
828126324Sjhb{
829126324Sjhb	struct sleepqueue *sq;
830126324Sjhb
831126324Sjhb	/*
832126324Sjhb	 * Look up the sleep queue for this wait channel, then re-check
833126324Sjhb	 * that the thread is asleep on that channel, if it is not, then
834126324Sjhb	 * bail.
835126324Sjhb	 */
836126324Sjhb	MPASS(wchan != NULL);
837136445Sjhb	sleepq_lock(wchan);
838126324Sjhb	sq = sleepq_lookup(wchan);
839170294Sjeff	/*
840170294Sjeff	 * We can not lock the thread here as it may be sleeping on a
841170294Sjeff	 * different sleepq.  However, holding the sleepq lock for this
842170294Sjeff	 * wchan can guarantee that we do not miss a wakeup for this
843170294Sjeff	 * channel.  The asserts below will catch any false positives.
844170294Sjeff	 */
845126324Sjhb	if (!TD_ON_SLEEPQ(td) || td->td_wchan != wchan) {
846126324Sjhb		sleepq_release(wchan);
847126324Sjhb		return;
848126324Sjhb	}
849170294Sjeff	/* Thread is asleep on sleep queue sq, so wake it up. */
850170294Sjeff	thread_lock(td);
851126324Sjhb	MPASS(sq != NULL);
852170294Sjeff	MPASS(td->td_wchan == wchan);
853145056Sjhb	sleepq_resume_thread(sq, td, -1);
854170294Sjeff	thread_unlock(td);
855126324Sjhb	sleepq_release(wchan);
856126324Sjhb}
857126324Sjhb
858126324Sjhb/*
859129241Sbde * Abort a thread as if an interrupt had occurred.  Only abort
860129241Sbde * interruptible waits (unfortunately it isn't safe to abort others).
861126324Sjhb */
862126324Sjhbvoid
863155741Sdavidxusleepq_abort(struct thread *td, int intrval)
864126324Sjhb{
865170294Sjeff	struct sleepqueue *sq;
866126324Sjhb	void *wchan;
867126324Sjhb
868170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
869126324Sjhb	MPASS(TD_ON_SLEEPQ(td));
870126324Sjhb	MPASS(td->td_flags & TDF_SINTR);
871155741Sdavidxu	MPASS(intrval == EINTR || intrval == ERESTART);
872126324Sjhb
873126324Sjhb	/*
874126324Sjhb	 * If the TDF_TIMEOUT flag is set, just leave. A
875126324Sjhb	 * timeout is scheduled anyhow.
876126324Sjhb	 */
877126324Sjhb	if (td->td_flags & TDF_TIMEOUT)
878126324Sjhb		return;
879126324Sjhb
880129241Sbde	CTR3(KTR_PROC, "sleepq_abort: thread %p (pid %ld, %s)",
881129241Sbde	    (void *)td, (long)td->td_proc->p_pid, (void *)td->td_proc->p_comm);
882170294Sjeff	td->td_intrval = intrval;
883170294Sjeff	td->td_flags |= TDF_SLEEPABORT;
884170294Sjeff	/*
885170294Sjeff	 * If the thread has not slept yet it will find the signal in
886170294Sjeff	 * sleepq_catch_signals() and call sleepq_resume_thread.  Otherwise
887170294Sjeff	 * we have to do it here.
888170294Sjeff	 */
889170294Sjeff	if (!TD_IS_SLEEPING(td))
890170294Sjeff		return;
891126324Sjhb	wchan = td->td_wchan;
892170294Sjeff	MPASS(wchan != NULL);
893170294Sjeff	sq = sleepq_lookup(wchan);
894170294Sjeff	MPASS(sq != NULL);
895170294Sjeff
896170294Sjeff	/* Thread is asleep on sleep queue sq, so wake it up. */
897170294Sjeff	sleepq_resume_thread(sq, td, -1);
898126324Sjhb}
899154936Sjhb
900154936Sjhb#ifdef DDB
901154936SjhbDB_SHOW_COMMAND(sleepq, db_show_sleepqueue)
902154936Sjhb{
903154936Sjhb	struct sleepqueue_chain *sc;
904154936Sjhb	struct sleepqueue *sq;
905154944Simp#ifdef INVARIANTS
906154936Sjhb	struct lock_object *lock;
907154944Simp#endif
908154936Sjhb	struct thread *td;
909154936Sjhb	void *wchan;
910154936Sjhb	int i;
911154936Sjhb
912154936Sjhb	if (!have_addr)
913154936Sjhb		return;
914154936Sjhb
915154936Sjhb	/*
916154936Sjhb	 * First, see if there is an active sleep queue for the wait channel
917154936Sjhb	 * indicated by the address.
918154936Sjhb	 */
919154936Sjhb	wchan = (void *)addr;
920154936Sjhb	sc = SC_LOOKUP(wchan);
921154936Sjhb	LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
922154936Sjhb		if (sq->sq_wchan == wchan)
923154936Sjhb			goto found;
924154936Sjhb
925154936Sjhb	/*
926154936Sjhb	 * Second, see if there is an active sleep queue at the address
927154936Sjhb	 * indicated.
928154936Sjhb	 */
929154936Sjhb	for (i = 0; i < SC_TABLESIZE; i++)
930154936Sjhb		LIST_FOREACH(sq, &sleepq_chains[i].sc_queues, sq_hash) {
931154936Sjhb			if (sq == (struct sleepqueue *)addr)
932154936Sjhb				goto found;
933154936Sjhb		}
934154936Sjhb
935154936Sjhb	db_printf("Unable to locate a sleep queue via %p\n", (void *)addr);
936154936Sjhb	return;
937154936Sjhbfound:
938154936Sjhb	db_printf("Wait channel: %p\n", sq->sq_wchan);
939154936Sjhb#ifdef INVARIANTS
940154936Sjhb	db_printf("Queue type: %d\n", sq->sq_type);
941154936Sjhb	if (sq->sq_lock) {
942164325Spjd		lock = sq->sq_lock;
943154936Sjhb		db_printf("Associated Interlock: %p - (%s) %s\n", lock,
944154936Sjhb		    LOCK_CLASS(lock)->lc_name, lock->lo_name);
945154936Sjhb	}
946154936Sjhb#endif
947154936Sjhb	db_printf("Blocked threads:\n");
948165272Skmacy	for (i = 0; i < NR_SLEEPQS; i++) {
949165272Skmacy		db_printf("\nQueue[%d]:\n", i);
950165272Skmacy		if (TAILQ_EMPTY(&sq->sq_blocked[i]))
951165272Skmacy			db_printf("\tempty\n");
952165272Skmacy		else
953165272Skmacy			TAILQ_FOREACH(td, &sq->sq_blocked[0],
954165272Skmacy				      td_slpq) {
955165272Skmacy				db_printf("\t%p (tid %d, pid %d, \"%s\")\n", td,
956165272Skmacy					  td->td_tid, td->td_proc->p_pid,
957165272Skmacy					  td->td_name[i] != '\0' ? td->td_name :
958165272Skmacy					  td->td_proc->p_comm);
959165272Skmacy			}
960165272Skmacy	}
961154936Sjhb}
962157823Sjhb
963157823Sjhb/* Alias 'show sleepqueue' to 'show sleepq'. */
964157823SjhbDB_SET(sleepqueue, db_show_sleepqueue, db_show_cmd_set, 0, NULL);
965154936Sjhb#endif
966