1139825Simp/*-
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 *
14126324Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15126324Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16126324Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17126324Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18126324Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19126324Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20126324Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21126324Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22126324Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23126324Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24126324Sjhb * SUCH DAMAGE.
25126324Sjhb *
26126324Sjhb * $FreeBSD$
27126324Sjhb */
28126324Sjhb
29126324Sjhb#ifndef _SYS_SLEEPQUEUE_H_
30126324Sjhb#define _SYS_SLEEPQUEUE_H_
31126324Sjhb
32126324Sjhb/*
33168191Sjhb * Sleep queue interface.  Sleep/wakeup, condition variables, and sx
34168191Sjhb * locks use a sleep queue for the queue of threads blocked on a sleep
35168191Sjhb * channel.
36126324Sjhb *
37136445Sjhb * A thread calls sleepq_lock() to lock the sleep queue chain associated
38136445Sjhb * with a given wait channel.  A thread can then call call sleepq_add() to
39136445Sjhb * add themself onto a sleep queue and call one of the sleepq_wait()
40136445Sjhb * functions to actually go to sleep.  If a thread needs to abort a sleep
41136445Sjhb * operation it should call sleepq_release() to unlock the associated sleep
42136445Sjhb * queue chain lock.  If the thread also needs to remove itself from a queue
43136445Sjhb * it just enqueued itself on, it can use sleepq_remove() instead.
44126324Sjhb *
45126324Sjhb * If the thread only wishes to sleep for a limited amount of time, it can
46126324Sjhb * call sleepq_set_timeout() after sleepq_add() to setup a timeout.  It
47126324Sjhb * should then use one of the sleepq_timedwait() functions to block.
48126324Sjhb *
49126324Sjhb * A thread is normally resumed from a sleep queue by either the
50126324Sjhb * sleepq_signal() or sleepq_broadcast() functions.  Sleepq_signal() wakes
51126324Sjhb * the thread with the highest priority that is sleeping on the specified
52126324Sjhb * wait channel.  Sleepq_broadcast() wakes all threads that are sleeping
53126324Sjhb * on the specified wait channel.  A thread sleeping in an interruptible
54126324Sjhb * sleep can be interrupted by calling sleepq_abort().  A thread can also
55126324Sjhb * be removed from a specified sleep queue using the sleepq_remove()
56136445Sjhb * function.  Note that the sleep queue chain must first be locked via
57181390Sjhb * sleepq_lock() before calling sleepq_abort(), sleepq_broadcast(), or
58181390Sjhb * sleepq_signal().  These routines each return a boolean that will be true
59181390Sjhb * if at least one swapped-out thread was resumed.  In that case, the caller
60181390Sjhb * is responsible for waking up the swapper by calling kick_proc0() after
61216421Smckusick * releasing the sleep queue chain lock.
62126324Sjhb *
63126324Sjhb * Each thread allocates a sleep queue at thread creation via sleepq_alloc()
64126324Sjhb * and releases it at thread destruction via sleepq_free().  Note that
65126324Sjhb * a sleep queue is not tied to a specific thread and that the sleep queue
66126324Sjhb * released at thread destruction may not be the same sleep queue that the
67126324Sjhb * thread allocated when it was created.
68126324Sjhb *
69126324Sjhb * XXX: Some other parts of the kernel such as ithread sleeping may end up
70126324Sjhb * using this interface as well (death to TDI_IWAIT!)
71126324Sjhb */
72126324Sjhb
73164325Spjdstruct lock_object;
74126324Sjhbstruct sleepqueue;
75126324Sjhbstruct thread;
76126324Sjhb
77126324Sjhb#ifdef _KERNEL
78126324Sjhb
79134013Sjhb#define	SLEEPQ_TYPE		0x0ff		/* Mask of sleep queue types. */
80167387Sjhb#define	SLEEPQ_SLEEP		0x00		/* Used by sleep/wakeup. */
81134013Sjhb#define	SLEEPQ_CONDVAR		0x01		/* Used for a cv. */
82166908Sjhb#define	SLEEPQ_PAUSE		0x02		/* Used by pause. */
83168191Sjhb#define	SLEEPQ_SX		0x03		/* Used by an sx lock. */
84177957Sattilio#define	SLEEPQ_LK		0x04		/* Used by a lockmgr. */
85134013Sjhb#define	SLEEPQ_INTERRUPTIBLE	0x100		/* Sleep is interruptible. */
86126324Sjhb
87126324Sjhbvoid	init_sleepqueues(void);
88181334Sjhbint	sleepq_abort(struct thread *td, int intrval);
89165272Skmacyvoid	sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg,
90165272Skmacy	    int flags, int queue);
91126324Sjhbstruct sleepqueue *sleepq_alloc(void);
92181334Sjhbint	sleepq_broadcast(void *wchan, int flags, int pri, int queue);
93165272Skmacyvoid	sleepq_free(struct sleepqueue *sq);
94165272Skmacyvoid	sleepq_lock(void *wchan);
95165272Skmacystruct sleepqueue *sleepq_lookup(void *wchan);
96165272Skmacyvoid	sleepq_release(void *wchan);
97165272Skmacyvoid	sleepq_remove(struct thread *td, void *wchan);
98181334Sjhbint	sleepq_signal(void *wchan, int flags, int pri, int queue);
99247783Sdavidevoid	sleepq_set_timeout_sbt(void *wchan, sbintime_t sbt,
100247783Sdavide	    sbintime_t pr, int flags);
101247783Sdavide#define	sleepq_set_timeout(wchan, timo)					\
102247784Sdavide    sleepq_set_timeout_sbt((wchan), tick_sbt * (timo), 0, C_HARDCLOCK)
103200447Sattiliou_int	sleepq_sleepcnt(void *wchan, int queue);
104177085Sjeffint	sleepq_timedwait(void *wchan, int pri);
105177085Sjeffint	sleepq_timedwait_sig(void *wchan, int pri);
106201879Sattilioint	sleepq_type(void *wchan);
107177085Sjeffvoid	sleepq_wait(void *wchan, int pri);
108177085Sjeffint	sleepq_wait_sig(void *wchan, int pri);
109126324Sjhb
110126324Sjhb#endif	/* _KERNEL */
111126324Sjhb#endif	/* !_SYS_SLEEPQUEUE_H_ */
112