subr_sleepqueue.c revision 173601
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 173601 2007-11-14 06:51:33Z julian $");
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	}
331172155Sattilio	thread_lock(td);
332165272Skmacy	TAILQ_INSERT_TAIL(&sq->sq_blocked[queue], td, td_slpq);
333126324Sjhb	td->td_sleepqueue = NULL;
334165272Skmacy	td->td_sqqueue = queue;
335126324Sjhb	td->td_wchan = wchan;
336126324Sjhb	td->td_wmesg = wmesg;
337155741Sdavidxu	if (flags & SLEEPQ_INTERRUPTIBLE) {
338134013Sjhb		td->td_flags |= TDF_SINTR;
339155741Sdavidxu		td->td_flags &= ~TDF_SLEEPABORT;
340155741Sdavidxu	}
341172155Sattilio	thread_unlock(td);
342126324Sjhb}
343126324Sjhb
344126324Sjhb/*
345126324Sjhb * Sets a timeout that will remove the current thread from the specified
346126324Sjhb * sleep queue after timo ticks if the thread has not already been awakened.
347126324Sjhb */
348126324Sjhbvoid
349126885Sjhbsleepq_set_timeout(void *wchan, int timo)
350126324Sjhb{
351126324Sjhb	struct sleepqueue_chain *sc;
352126324Sjhb	struct thread *td;
353126324Sjhb
354126324Sjhb	td = curthread;
355126324Sjhb	sc = SC_LOOKUP(wchan);
356126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
357126324Sjhb	MPASS(TD_ON_SLEEPQ(td));
358126324Sjhb	MPASS(td->td_sleepqueue == NULL);
359126324Sjhb	MPASS(wchan != NULL);
360126324Sjhb	callout_reset(&td->td_slpcallout, timo, sleepq_timeout, td);
361126324Sjhb}
362126324Sjhb
363126324Sjhb/*
364126324Sjhb * Marks the pending sleep of the current thread as interruptible and
365126324Sjhb * makes an initial check for pending signals before putting a thread
366170294Sjeff * to sleep. Enters and exits with the thread lock held.  Thread lock
367170294Sjeff * may have transitioned from the sleepq lock to a run lock.
368126324Sjhb */
369155741Sdavidxustatic int
370126324Sjhbsleepq_catch_signals(void *wchan)
371126324Sjhb{
372126324Sjhb	struct sleepqueue_chain *sc;
373126324Sjhb	struct sleepqueue *sq;
374126324Sjhb	struct thread *td;
375126324Sjhb	struct proc *p;
376155741Sdavidxu	struct sigacts *ps;
377155741Sdavidxu	int sig, ret;
378126324Sjhb
379126324Sjhb	td = curthread;
380155741Sdavidxu	p = curproc;
381126324Sjhb	sc = SC_LOOKUP(wchan);
382126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
383126324Sjhb	MPASS(wchan != NULL);
384129241Sbde	CTR3(KTR_PROC, "sleepq catching signals: thread %p (pid %ld, %s)",
385173601Sjulian		(void *)td, (long)p->p_pid, td->td_name);
386126324Sjhb
387155741Sdavidxu	mtx_unlock_spin(&sc->sc_lock);
388126324Sjhb
389126324Sjhb	/* See if there are any pending signals for this thread. */
390126324Sjhb	PROC_LOCK(p);
391155741Sdavidxu	ps = p->p_sigacts;
392155741Sdavidxu	mtx_lock(&ps->ps_mtx);
393126324Sjhb	sig = cursig(td);
394155741Sdavidxu	if (sig == 0) {
395155741Sdavidxu		mtx_unlock(&ps->ps_mtx);
396155741Sdavidxu		ret = thread_suspend_check(1);
397155741Sdavidxu		MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
398155741Sdavidxu	} else {
399155741Sdavidxu		if (SIGISMEMBER(ps->ps_sigintr, sig))
400155741Sdavidxu			ret = EINTR;
401155741Sdavidxu		else
402155741Sdavidxu			ret = ERESTART;
403155741Sdavidxu		mtx_unlock(&ps->ps_mtx);
404155741Sdavidxu	}
405170294Sjeff	/*
406170294Sjeff	 * Lock sleepq chain before unlocking proc
407170294Sjeff	 * without this, we could lose a race.
408170294Sjeff	 */
409170294Sjeff	mtx_lock_spin(&sc->sc_lock);
410170294Sjeff	PROC_UNLOCK(p);
411170294Sjeff	thread_lock(td);
412155932Sdavidxu	if (ret == 0) {
413170294Sjeff		if (!(td->td_flags & TDF_INTERRUPT)) {
414170294Sjeff			sleepq_switch(wchan);
415155932Sdavidxu			return (0);
416170294Sjeff		}
417155932Sdavidxu		/* KSE threads tried unblocking us. */
418155932Sdavidxu		ret = td->td_intrval;
419170294Sjeff		MPASS(ret == EINTR || ret == ERESTART || ret == EWOULDBLOCK);
420155741Sdavidxu	}
421155936Sdavidxu	/*
422155936Sdavidxu	 * There were pending signals and this thread is still
423155936Sdavidxu	 * on the sleep queue, remove it from the sleep queue.
424155936Sdavidxu	 */
425170294Sjeff	if (TD_ON_SLEEPQ(td)) {
426170294Sjeff		sq = sleepq_lookup(wchan);
427155932Sdavidxu		sleepq_resume_thread(sq, td, -1);
428170294Sjeff	}
429170294Sjeff	mtx_unlock_spin(&sc->sc_lock);
430170294Sjeff	MPASS(td->td_lock != &sc->sc_lock);
431155741Sdavidxu	return (ret);
432126324Sjhb}
433126324Sjhb
434126324Sjhb/*
435170294Sjeff * Switches to another thread if we are still asleep on a sleep queue.
436170294Sjeff * Returns with thread lock.
437126324Sjhb */
438126324Sjhbstatic void
439126324Sjhbsleepq_switch(void *wchan)
440126324Sjhb{
441126324Sjhb	struct sleepqueue_chain *sc;
442126324Sjhb	struct thread *td;
443126324Sjhb
444126324Sjhb	td = curthread;
445126324Sjhb	sc = SC_LOOKUP(wchan);
446126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
447170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
448170294Sjeff	/* We were removed */
449126324Sjhb	if (td->td_sleepqueue != NULL) {
450126324Sjhb		mtx_unlock_spin(&sc->sc_lock);
451126324Sjhb		return;
452126324Sjhb	}
453170294Sjeff	thread_lock_set(td, &sc->sc_lock);
454126324Sjhb
455170294Sjeff	MPASS(td->td_sleepqueue == NULL);
456126324Sjhb	sched_sleep(td);
457126324Sjhb	TD_SET_SLEEPING(td);
458170294Sjeff	SCHED_STAT_INC(switch_sleepq);
459131473Sjhb	mi_switch(SW_VOL, NULL);
460126324Sjhb	KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING"));
461129241Sbde	CTR3(KTR_PROC, "sleepq resume: thread %p (pid %ld, %s)",
462173600Sjulian	    (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
463126324Sjhb}
464126324Sjhb
465126324Sjhb/*
466126324Sjhb * Check to see if we timed out.
467126324Sjhb */
468126324Sjhbstatic int
469126324Sjhbsleepq_check_timeout(void)
470126324Sjhb{
471126324Sjhb	struct thread *td;
472126324Sjhb
473126324Sjhb	td = curthread;
474170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
475126324Sjhb
476126324Sjhb	/*
477126324Sjhb	 * If TDF_TIMEOUT is set, we timed out.
478126324Sjhb	 */
479126324Sjhb	if (td->td_flags & TDF_TIMEOUT) {
480126324Sjhb		td->td_flags &= ~TDF_TIMEOUT;
481126324Sjhb		return (EWOULDBLOCK);
482126324Sjhb	}
483126324Sjhb
484126324Sjhb	/*
485126324Sjhb	 * If TDF_TIMOFAIL is set, the timeout ran after we had
486126324Sjhb	 * already been woken up.
487126324Sjhb	 */
488126324Sjhb	if (td->td_flags & TDF_TIMOFAIL)
489126324Sjhb		td->td_flags &= ~TDF_TIMOFAIL;
490126324Sjhb
491126324Sjhb	/*
492126324Sjhb	 * If callout_stop() fails, then the timeout is running on
493126324Sjhb	 * another CPU, so synchronize with it to avoid having it
494126324Sjhb	 * accidentally wake up a subsequent sleep.
495126324Sjhb	 */
496126324Sjhb	else if (callout_stop(&td->td_slpcallout) == 0) {
497126324Sjhb		td->td_flags |= TDF_TIMEOUT;
498126324Sjhb		TD_SET_SLEEPING(td);
499170294Sjeff		SCHED_STAT_INC(switch_sleepqtimo);
500131473Sjhb		mi_switch(SW_INVOL, NULL);
501126324Sjhb	}
502126324Sjhb	return (0);
503126324Sjhb}
504126324Sjhb
505126324Sjhb/*
506126324Sjhb * Check to see if we were awoken by a signal.
507126324Sjhb */
508126324Sjhbstatic int
509126324Sjhbsleepq_check_signals(void)
510126324Sjhb{
511126324Sjhb	struct thread *td;
512126324Sjhb
513126324Sjhb	td = curthread;
514170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
515126324Sjhb
516126324Sjhb	/* We are no longer in an interruptible sleep. */
517155741Sdavidxu	if (td->td_flags & TDF_SINTR)
518155741Sdavidxu		td->td_flags &= ~TDF_SINTR;
519126324Sjhb
520155741Sdavidxu	if (td->td_flags & TDF_SLEEPABORT) {
521155741Sdavidxu		td->td_flags &= ~TDF_SLEEPABORT;
522155741Sdavidxu		return (td->td_intrval);
523155741Sdavidxu	}
524155741Sdavidxu
525126324Sjhb	if (td->td_flags & TDF_INTERRUPT)
526126324Sjhb		return (td->td_intrval);
527155741Sdavidxu
528126324Sjhb	return (0);
529126324Sjhb}
530126324Sjhb
531126324Sjhb/*
532126324Sjhb * Block the current thread until it is awakened from its sleep queue.
533126324Sjhb */
534126324Sjhbvoid
535126324Sjhbsleepq_wait(void *wchan)
536126324Sjhb{
537170294Sjeff	struct thread *td;
538126324Sjhb
539170294Sjeff	td = curthread;
540170294Sjeff	MPASS(!(td->td_flags & TDF_SINTR));
541170294Sjeff	thread_lock(td);
542126324Sjhb	sleepq_switch(wchan);
543170294Sjeff	thread_unlock(td);
544126324Sjhb}
545126324Sjhb
546126324Sjhb/*
547126324Sjhb * Block the current thread until it is awakened from its sleep queue
548126324Sjhb * or it is interrupted by a signal.
549126324Sjhb */
550126324Sjhbint
551126324Sjhbsleepq_wait_sig(void *wchan)
552126324Sjhb{
553155741Sdavidxu	int rcatch;
554126324Sjhb	int rval;
555126324Sjhb
556155741Sdavidxu	rcatch = sleepq_catch_signals(wchan);
557126324Sjhb	rval = sleepq_check_signals();
558170294Sjeff	thread_unlock(curthread);
559155741Sdavidxu	if (rcatch)
560155741Sdavidxu		return (rcatch);
561126324Sjhb	return (rval);
562126324Sjhb}
563126324Sjhb
564126324Sjhb/*
565126324Sjhb * Block the current thread until it is awakened from its sleep queue
566126324Sjhb * or it times out while waiting.
567126324Sjhb */
568126324Sjhbint
569131249Sjhbsleepq_timedwait(void *wchan)
570126324Sjhb{
571170294Sjeff	struct thread *td;
572126324Sjhb	int rval;
573126324Sjhb
574170294Sjeff	td = curthread;
575170294Sjeff	MPASS(!(td->td_flags & TDF_SINTR));
576170294Sjeff	thread_lock(td);
577126324Sjhb	sleepq_switch(wchan);
578126324Sjhb	rval = sleepq_check_timeout();
579170294Sjeff	thread_unlock(td);
580170294Sjeff
581131249Sjhb	return (rval);
582126324Sjhb}
583126324Sjhb
584126324Sjhb/*
585126324Sjhb * Block the current thread until it is awakened from its sleep queue,
586126324Sjhb * it is interrupted by a signal, or it times out waiting to be awakened.
587126324Sjhb */
588126324Sjhbint
589155741Sdavidxusleepq_timedwait_sig(void *wchan)
590126324Sjhb{
591155741Sdavidxu	int rcatch, rvalt, rvals;
592126324Sjhb
593155741Sdavidxu	rcatch = sleepq_catch_signals(wchan);
594126324Sjhb	rvalt = sleepq_check_timeout();
595126324Sjhb	rvals = sleepq_check_signals();
596170294Sjeff	thread_unlock(curthread);
597155741Sdavidxu	if (rcatch)
598155741Sdavidxu		return (rcatch);
599155741Sdavidxu	if (rvals)
600126324Sjhb		return (rvals);
601155741Sdavidxu	return (rvalt);
602126324Sjhb}
603126324Sjhb
604126324Sjhb/*
605145056Sjhb * Removes a thread from a sleep queue and makes it
606145056Sjhb * runnable.
607126324Sjhb */
608126324Sjhbstatic void
609145056Sjhbsleepq_resume_thread(struct sleepqueue *sq, struct thread *td, int pri)
610126324Sjhb{
611126324Sjhb	struct sleepqueue_chain *sc;
612126324Sjhb
613126324Sjhb	MPASS(td != NULL);
614126324Sjhb	MPASS(sq->sq_wchan != NULL);
615126324Sjhb	MPASS(td->td_wchan == sq->sq_wchan);
616165272Skmacy	MPASS(td->td_sqqueue < NR_SLEEPQS && td->td_sqqueue >= 0);
617170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
618126324Sjhb	sc = SC_LOOKUP(sq->sq_wchan);
619126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
620126324Sjhb
621126324Sjhb	/* Remove the thread from the queue. */
622165272Skmacy	TAILQ_REMOVE(&sq->sq_blocked[td->td_sqqueue], td, td_slpq);
623126324Sjhb
624126324Sjhb	/*
625126324Sjhb	 * Get a sleep queue for this thread.  If this is the last waiter,
626126324Sjhb	 * use the queue itself and take it out of the chain, otherwise,
627126324Sjhb	 * remove a queue from the free list.
628126324Sjhb	 */
629126324Sjhb	if (LIST_EMPTY(&sq->sq_free)) {
630126324Sjhb		td->td_sleepqueue = sq;
631126324Sjhb#ifdef INVARIANTS
632126324Sjhb		sq->sq_wchan = NULL;
633126324Sjhb#endif
634131259Sjhb#ifdef SLEEPQUEUE_PROFILING
635131259Sjhb		sc->sc_depth--;
636131259Sjhb#endif
637126324Sjhb	} else
638126324Sjhb		td->td_sleepqueue = LIST_FIRST(&sq->sq_free);
639126324Sjhb	LIST_REMOVE(td->td_sleepqueue, sq_hash);
640126324Sjhb
641129188Sjhb	td->td_wmesg = NULL;
642129188Sjhb	td->td_wchan = NULL;
643157743Sdavidxu	td->td_flags &= ~TDF_SINTR;
644129188Sjhb
645126324Sjhb	/*
646129188Sjhb	 * Note that thread td might not be sleeping if it is running
647129188Sjhb	 * sleepq_catch_signals() on another CPU or is blocked on
648129188Sjhb	 * its proc lock to check signals.  It doesn't hurt to clear
649129188Sjhb	 * the sleeping flag if it isn't set though, so we just always
650129188Sjhb	 * do it.  However, we can't assert that it is set.
651126324Sjhb	 */
652129241Sbde	CTR3(KTR_PROC, "sleepq_wakeup: thread %p (pid %ld, %s)",
653173600Sjulian	    (void *)td, (long)td->td_proc->p_pid, td->td_name);
654126324Sjhb	TD_CLR_SLEEPING(td);
655126324Sjhb
656126324Sjhb	/* Adjust priority if requested. */
657126324Sjhb	MPASS(pri == -1 || (pri >= PRI_MIN && pri <= PRI_MAX));
658126324Sjhb	if (pri != -1 && td->td_priority > pri)
659136439Sups		sched_prio(td, pri);
660126324Sjhb	setrunnable(td);
661126324Sjhb}
662126324Sjhb
663169666Sjeff#ifdef INVARIANTS
664126324Sjhb/*
665169666Sjeff * UMA zone item deallocator.
666169666Sjeff */
667169666Sjeffstatic void
668169666Sjeffsleepq_dtor(void *mem, int size, void *arg)
669169666Sjeff{
670169666Sjeff	struct sleepqueue *sq;
671169666Sjeff	int i;
672169666Sjeff
673169666Sjeff	sq = mem;
674169666Sjeff	for (i = 0; i < NR_SLEEPQS; i++)
675169666Sjeff		MPASS(TAILQ_EMPTY(&sq->sq_blocked[i]));
676169666Sjeff}
677169666Sjeff#endif
678169666Sjeff
679169666Sjeff/*
680169666Sjeff * UMA zone item initializer.
681169666Sjeff */
682169666Sjeffstatic int
683169666Sjeffsleepq_init(void *mem, int size, int flags)
684169666Sjeff{
685169666Sjeff	struct sleepqueue *sq;
686169666Sjeff	int i;
687169666Sjeff
688169666Sjeff	bzero(mem, size);
689169666Sjeff	sq = mem;
690169666Sjeff	for (i = 0; i < NR_SLEEPQS; i++)
691169666Sjeff		TAILQ_INIT(&sq->sq_blocked[i]);
692169666Sjeff	LIST_INIT(&sq->sq_free);
693169666Sjeff	return (0);
694169666Sjeff}
695169666Sjeff
696169666Sjeff/*
697126324Sjhb * Find the highest priority thread sleeping on a wait channel and resume it.
698126324Sjhb */
699126324Sjhbvoid
700165272Skmacysleepq_signal(void *wchan, int flags, int pri, int queue)
701126324Sjhb{
702126324Sjhb	struct sleepqueue *sq;
703137277Sjhb	struct thread *td, *besttd;
704126324Sjhb
705126324Sjhb	CTR2(KTR_PROC, "sleepq_signal(%p, %d)", wchan, flags);
706126324Sjhb	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
707165272Skmacy	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
708126324Sjhb	sq = sleepq_lookup(wchan);
709170294Sjeff	if (sq == NULL)
710126324Sjhb		return;
711134013Sjhb	KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
712126324Sjhb	    ("%s: mismatch between sleep/wakeup and cv_*", __func__));
713129188Sjhb
714137277Sjhb	/*
715137277Sjhb	 * Find the highest priority thread on the queue.  If there is a
716137277Sjhb	 * tie, use the thread that first appears in the queue as it has
717137277Sjhb	 * been sleeping the longest since threads are always added to
718137277Sjhb	 * the tail of sleep queues.
719137277Sjhb	 */
720137277Sjhb	besttd = NULL;
721165272Skmacy	TAILQ_FOREACH(td, &sq->sq_blocked[queue], td_slpq) {
722137277Sjhb		if (besttd == NULL || td->td_priority < besttd->td_priority)
723137277Sjhb			besttd = td;
724137277Sjhb	}
725137277Sjhb	MPASS(besttd != NULL);
726170294Sjeff	thread_lock(besttd);
727145056Sjhb	sleepq_resume_thread(sq, besttd, pri);
728170294Sjeff	thread_unlock(besttd);
729126324Sjhb}
730126324Sjhb
731126324Sjhb/*
732126324Sjhb * Resume all threads sleeping on a specified wait channel.
733126324Sjhb */
734126324Sjhbvoid
735165272Skmacysleepq_broadcast(void *wchan, int flags, int pri, int queue)
736126324Sjhb{
737126324Sjhb	struct sleepqueue *sq;
738170294Sjeff	struct thread *td;
739126324Sjhb
740126324Sjhb	CTR2(KTR_PROC, "sleepq_broadcast(%p, %d)", wchan, flags);
741126324Sjhb	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
742165272Skmacy	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
743126324Sjhb	sq = sleepq_lookup(wchan);
744126324Sjhb	if (sq == NULL) {
745126324Sjhb		sleepq_release(wchan);
746126324Sjhb		return;
747126324Sjhb	}
748134013Sjhb	KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
749126324Sjhb	    ("%s: mismatch between sleep/wakeup and cv_*", __func__));
750129188Sjhb
751145056Sjhb	/* Resume all blocked threads on the sleep queue. */
752170294Sjeff	while (!TAILQ_EMPTY(&sq->sq_blocked[queue])) {
753170294Sjeff		td = TAILQ_FIRST(&sq->sq_blocked[queue]);
754170294Sjeff		thread_lock(td);
755170294Sjeff		sleepq_resume_thread(sq, td, pri);
756170294Sjeff		thread_unlock(td);
757170294Sjeff	}
758126324Sjhb	sleepq_release(wchan);
759126324Sjhb}
760126324Sjhb
761126324Sjhb/*
762126324Sjhb * Time sleeping threads out.  When the timeout expires, the thread is
763126324Sjhb * removed from the sleep queue and made runnable if it is still asleep.
764126324Sjhb */
765126324Sjhbstatic void
766126324Sjhbsleepq_timeout(void *arg)
767126324Sjhb{
768170294Sjeff	struct sleepqueue_chain *sc;
769126324Sjhb	struct sleepqueue *sq;
770126324Sjhb	struct thread *td;
771126324Sjhb	void *wchan;
772126324Sjhb
773129241Sbde	td = arg;
774129241Sbde	CTR3(KTR_PROC, "sleepq_timeout: thread %p (pid %ld, %s)",
775173600Sjulian	    (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
776126324Sjhb
777126324Sjhb	/*
778126324Sjhb	 * First, see if the thread is asleep and get the wait channel if
779126324Sjhb	 * it is.
780126324Sjhb	 */
781170294Sjeff	thread_lock(td);
782170294Sjeff	if (TD_IS_SLEEPING(td) && TD_ON_SLEEPQ(td)) {
783126324Sjhb		wchan = td->td_wchan;
784170294Sjeff		sc = SC_LOOKUP(wchan);
785170294Sjeff		MPASS(td->td_lock == &sc->sc_lock);
786126324Sjhb		sq = sleepq_lookup(wchan);
787170294Sjeff		MPASS(sq != NULL);
788170294Sjeff		td->td_flags |= TDF_TIMEOUT;
789170294Sjeff		sleepq_resume_thread(sq, td, -1);
790170294Sjeff		thread_unlock(td);
791170294Sjeff		return;
792126324Sjhb	}
793126324Sjhb	/*
794170294Sjeff	 * If the thread is on the SLEEPQ but not sleeping and we have it
795170294Sjeff	 * locked it must be in sleepq_catch_signals().  Let it know we've
796170294Sjeff 	 * timedout here so it can remove itself.
797126324Sjhb	 */
798126324Sjhb	if (TD_ON_SLEEPQ(td)) {
799170294Sjeff		td->td_flags |= TDF_TIMEOUT | TDF_INTERRUPT;
800170294Sjeff		td->td_intrval = EWOULDBLOCK;
801170294Sjeff		thread_unlock(td);
802126324Sjhb		return;
803170294Sjeff	}
804126324Sjhb
805126324Sjhb	/*
806126324Sjhb	 * Now check for the edge cases.  First, if TDF_TIMEOUT is set,
807126324Sjhb	 * then the other thread has already yielded to us, so clear
808126324Sjhb	 * the flag and resume it.  If TDF_TIMEOUT is not set, then the
809126324Sjhb	 * we know that the other thread is not on a sleep queue, but it
810126324Sjhb	 * hasn't resumed execution yet.  In that case, set TDF_TIMOFAIL
811126324Sjhb	 * to let it know that the timeout has already run and doesn't
812126324Sjhb	 * need to be canceled.
813126324Sjhb	 */
814126324Sjhb	if (td->td_flags & TDF_TIMEOUT) {
815127085Sjhb		MPASS(TD_IS_SLEEPING(td));
816126324Sjhb		td->td_flags &= ~TDF_TIMEOUT;
817126324Sjhb		TD_CLR_SLEEPING(td);
818126324Sjhb		setrunnable(td);
819126324Sjhb	} else
820126324Sjhb		td->td_flags |= TDF_TIMOFAIL;
821170294Sjeff	thread_unlock(td);
822126324Sjhb}
823126324Sjhb
824126324Sjhb/*
825126324Sjhb * Resumes a specific thread from the sleep queue associated with a specific
826126324Sjhb * wait channel if it is on that queue.
827126324Sjhb */
828126324Sjhbvoid
829126324Sjhbsleepq_remove(struct thread *td, void *wchan)
830126324Sjhb{
831126324Sjhb	struct sleepqueue *sq;
832126324Sjhb
833126324Sjhb	/*
834126324Sjhb	 * Look up the sleep queue for this wait channel, then re-check
835126324Sjhb	 * that the thread is asleep on that channel, if it is not, then
836126324Sjhb	 * bail.
837126324Sjhb	 */
838126324Sjhb	MPASS(wchan != NULL);
839136445Sjhb	sleepq_lock(wchan);
840126324Sjhb	sq = sleepq_lookup(wchan);
841170294Sjeff	/*
842170294Sjeff	 * We can not lock the thread here as it may be sleeping on a
843170294Sjeff	 * different sleepq.  However, holding the sleepq lock for this
844170294Sjeff	 * wchan can guarantee that we do not miss a wakeup for this
845170294Sjeff	 * channel.  The asserts below will catch any false positives.
846170294Sjeff	 */
847126324Sjhb	if (!TD_ON_SLEEPQ(td) || td->td_wchan != wchan) {
848126324Sjhb		sleepq_release(wchan);
849126324Sjhb		return;
850126324Sjhb	}
851170294Sjeff	/* Thread is asleep on sleep queue sq, so wake it up. */
852170294Sjeff	thread_lock(td);
853126324Sjhb	MPASS(sq != NULL);
854170294Sjeff	MPASS(td->td_wchan == wchan);
855145056Sjhb	sleepq_resume_thread(sq, td, -1);
856170294Sjeff	thread_unlock(td);
857126324Sjhb	sleepq_release(wchan);
858126324Sjhb}
859126324Sjhb
860126324Sjhb/*
861129241Sbde * Abort a thread as if an interrupt had occurred.  Only abort
862129241Sbde * interruptible waits (unfortunately it isn't safe to abort others).
863126324Sjhb */
864126324Sjhbvoid
865155741Sdavidxusleepq_abort(struct thread *td, int intrval)
866126324Sjhb{
867170294Sjeff	struct sleepqueue *sq;
868126324Sjhb	void *wchan;
869126324Sjhb
870170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
871126324Sjhb	MPASS(TD_ON_SLEEPQ(td));
872126324Sjhb	MPASS(td->td_flags & TDF_SINTR);
873155741Sdavidxu	MPASS(intrval == EINTR || intrval == ERESTART);
874126324Sjhb
875126324Sjhb	/*
876126324Sjhb	 * If the TDF_TIMEOUT flag is set, just leave. A
877126324Sjhb	 * timeout is scheduled anyhow.
878126324Sjhb	 */
879126324Sjhb	if (td->td_flags & TDF_TIMEOUT)
880126324Sjhb		return;
881126324Sjhb
882129241Sbde	CTR3(KTR_PROC, "sleepq_abort: thread %p (pid %ld, %s)",
883173600Sjulian	    (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
884170294Sjeff	td->td_intrval = intrval;
885170294Sjeff	td->td_flags |= TDF_SLEEPABORT;
886170294Sjeff	/*
887170294Sjeff	 * If the thread has not slept yet it will find the signal in
888170294Sjeff	 * sleepq_catch_signals() and call sleepq_resume_thread.  Otherwise
889170294Sjeff	 * we have to do it here.
890170294Sjeff	 */
891170294Sjeff	if (!TD_IS_SLEEPING(td))
892170294Sjeff		return;
893126324Sjhb	wchan = td->td_wchan;
894170294Sjeff	MPASS(wchan != NULL);
895170294Sjeff	sq = sleepq_lookup(wchan);
896170294Sjeff	MPASS(sq != NULL);
897170294Sjeff
898170294Sjeff	/* Thread is asleep on sleep queue sq, so wake it up. */
899170294Sjeff	sleepq_resume_thread(sq, td, -1);
900126324Sjhb}
901154936Sjhb
902154936Sjhb#ifdef DDB
903154936SjhbDB_SHOW_COMMAND(sleepq, db_show_sleepqueue)
904154936Sjhb{
905154936Sjhb	struct sleepqueue_chain *sc;
906154936Sjhb	struct sleepqueue *sq;
907154944Simp#ifdef INVARIANTS
908154936Sjhb	struct lock_object *lock;
909154944Simp#endif
910154936Sjhb	struct thread *td;
911154936Sjhb	void *wchan;
912154936Sjhb	int i;
913154936Sjhb
914154936Sjhb	if (!have_addr)
915154936Sjhb		return;
916154936Sjhb
917154936Sjhb	/*
918154936Sjhb	 * First, see if there is an active sleep queue for the wait channel
919154936Sjhb	 * indicated by the address.
920154936Sjhb	 */
921154936Sjhb	wchan = (void *)addr;
922154936Sjhb	sc = SC_LOOKUP(wchan);
923154936Sjhb	LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
924154936Sjhb		if (sq->sq_wchan == wchan)
925154936Sjhb			goto found;
926154936Sjhb
927154936Sjhb	/*
928154936Sjhb	 * Second, see if there is an active sleep queue at the address
929154936Sjhb	 * indicated.
930154936Sjhb	 */
931154936Sjhb	for (i = 0; i < SC_TABLESIZE; i++)
932154936Sjhb		LIST_FOREACH(sq, &sleepq_chains[i].sc_queues, sq_hash) {
933154936Sjhb			if (sq == (struct sleepqueue *)addr)
934154936Sjhb				goto found;
935154936Sjhb		}
936154936Sjhb
937154936Sjhb	db_printf("Unable to locate a sleep queue via %p\n", (void *)addr);
938154936Sjhb	return;
939154936Sjhbfound:
940154936Sjhb	db_printf("Wait channel: %p\n", sq->sq_wchan);
941154936Sjhb#ifdef INVARIANTS
942154936Sjhb	db_printf("Queue type: %d\n", sq->sq_type);
943154936Sjhb	if (sq->sq_lock) {
944164325Spjd		lock = sq->sq_lock;
945154936Sjhb		db_printf("Associated Interlock: %p - (%s) %s\n", lock,
946154936Sjhb		    LOCK_CLASS(lock)->lc_name, lock->lo_name);
947154936Sjhb	}
948154936Sjhb#endif
949154936Sjhb	db_printf("Blocked threads:\n");
950165272Skmacy	for (i = 0; i < NR_SLEEPQS; i++) {
951165272Skmacy		db_printf("\nQueue[%d]:\n", i);
952165272Skmacy		if (TAILQ_EMPTY(&sq->sq_blocked[i]))
953165272Skmacy			db_printf("\tempty\n");
954165272Skmacy		else
955165272Skmacy			TAILQ_FOREACH(td, &sq->sq_blocked[0],
956165272Skmacy				      td_slpq) {
957165272Skmacy				db_printf("\t%p (tid %d, pid %d, \"%s\")\n", td,
958165272Skmacy					  td->td_tid, td->td_proc->p_pid,
959165272Skmacy					  td->td_name[i] != '\0' ? td->td_name :
960173600Sjulian					  td->td_name);
961165272Skmacy			}
962165272Skmacy	}
963154936Sjhb}
964157823Sjhb
965157823Sjhb/* Alias 'show sleepqueue' to 'show sleepq'. */
966157823SjhbDB_SET(sleepqueue, db_show_sleepqueue, db_show_cmd_set, 0, NULL);
967154936Sjhb#endif
968