sleepqueue.h revision 262192
1/*-
2 * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/10/sys/sys/sleepqueue.h 262192 2014-02-18 20:27:17Z jhb $
27 */
28
29#ifndef _SYS_SLEEPQUEUE_H_
30#define _SYS_SLEEPQUEUE_H_
31
32/*
33 * Sleep queue interface.  Sleep/wakeup, condition variables, and sx
34 * locks use a sleep queue for the queue of threads blocked on a sleep
35 * channel.
36 *
37 * A thread calls sleepq_lock() to lock the sleep queue chain associated
38 * with a given wait channel.  A thread can then call call sleepq_add() to
39 * add themself onto a sleep queue and call one of the sleepq_wait()
40 * functions to actually go to sleep.  If a thread needs to abort a sleep
41 * operation it should call sleepq_release() to unlock the associated sleep
42 * queue chain lock.  If the thread also needs to remove itself from a queue
43 * it just enqueued itself on, it can use sleepq_remove() instead.
44 *
45 * If the thread only wishes to sleep for a limited amount of time, it can
46 * call sleepq_set_timeout() after sleepq_add() to setup a timeout.  It
47 * should then use one of the sleepq_timedwait() functions to block.
48 *
49 * If the thread wants the sleep to be interruptible by signals, it can
50 * call sleepq_catch_signals() after sleepq_add().  It should then use
51 * one of the sleepq_wait_sig() functions to block.  After the thread has
52 * been resumed, it should call sleepq_calc_signal_retval() to determine
53 * if it should return EINTR or ERESTART passing in the value returned from
54 * the earlier call to sleepq_catch_signals().
55 *
56 * A thread is normally resumed from a sleep queue by either the
57 * sleepq_signal() or sleepq_broadcast() functions.  Sleepq_signal() wakes
58 * the thread with the highest priority that is sleeping on the specified
59 * wait channel.  Sleepq_broadcast() wakes all threads that are sleeping
60 * on the specified wait channel.  A thread sleeping in an interruptible
61 * sleep can be interrupted by calling sleepq_abort().  A thread can also
62 * be removed from a specified sleep queue using the sleepq_remove()
63 * function.  Note that the sleep queue chain must first be locked via
64 * sleepq_lock() before calling sleepq_abort(), sleepq_broadcast(), or
65 * sleepq_signal().  These routines each return a boolean that will be true
66 * if at least one swapped-out thread was resumed.  In that case, the caller
67 * is responsible for waking up the swapper by calling kick_proc0() after
68 * releasing the sleep queue chain lock.
69 *
70 * Each thread allocates a sleep queue at thread creation via sleepq_alloc()
71 * and releases it at thread destruction via sleepq_free().  Note that
72 * a sleep queue is not tied to a specific thread and that the sleep queue
73 * released at thread destruction may not be the same sleep queue that the
74 * thread allocated when it was created.
75 *
76 * XXX: Some other parts of the kernel such as ithread sleeping may end up
77 * using this interface as well (death to TDI_IWAIT!)
78 */
79
80struct lock_object;
81struct sleepqueue;
82struct thread;
83
84#ifdef _KERNEL
85
86#define	SLEEPQ_TYPE		0x0ff		/* Mask of sleep queue types. */
87#define	SLEEPQ_SLEEP		0x00		/* Used by sleep/wakeup. */
88#define	SLEEPQ_CONDVAR		0x01		/* Used for a cv. */
89#define	SLEEPQ_PAUSE		0x02		/* Used by pause. */
90#define	SLEEPQ_SX		0x03		/* Used by an sx lock. */
91#define	SLEEPQ_LK		0x04		/* Used by a lockmgr. */
92#define	SLEEPQ_INTERRUPTIBLE	0x100		/* Sleep is interruptible. */
93
94void	init_sleepqueues(void);
95int	sleepq_abort(struct thread *td, int intrval);
96void	sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg,
97	    int flags, int queue);
98struct sleepqueue *sleepq_alloc(void);
99int	sleepq_broadcast(void *wchan, int flags, int pri, int queue);
100void	sleepq_free(struct sleepqueue *sq);
101void	sleepq_lock(void *wchan);
102struct sleepqueue *sleepq_lookup(void *wchan);
103void	sleepq_release(void *wchan);
104void	sleepq_remove(struct thread *td, void *wchan);
105int	sleepq_signal(void *wchan, int flags, int pri, int queue);
106void	sleepq_set_timeout_sbt(void *wchan, sbintime_t sbt,
107	    sbintime_t pr, int flags);
108#define	sleepq_set_timeout(wchan, timo)					\
109    sleepq_set_timeout_sbt((wchan), tick_sbt * (timo), 0, C_HARDCLOCK)
110u_int	sleepq_sleepcnt(void *wchan, int queue);
111int	sleepq_timedwait(void *wchan, int pri);
112int	sleepq_timedwait_sig(void *wchan, int pri);
113int	sleepq_type(void *wchan);
114void	sleepq_wait(void *wchan, int pri);
115int	sleepq_wait_sig(void *wchan, int pri);
116
117#endif	/* _KERNEL */
118#endif	/* !_SYS_SLEEPQUEUE_H_ */
119