xref: /freebsd/sys/sys/sleepqueue.h (revision 7bd6fde3)
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  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _SYS_SLEEPQUEUE_H_
33 #define _SYS_SLEEPQUEUE_H_
34 
35 /*
36  * Sleep queue interface.  Sleep/wakeup and condition variables use a sleep
37  * queue for the queue of threads blocked on a sleep channel.
38  *
39  * A thread calls sleepq_lock() to lock the sleep queue chain associated
40  * with a given wait channel.  A thread can then call call sleepq_add() to
41  * add themself onto a sleep queue and call one of the sleepq_wait()
42  * functions to actually go to sleep.  If a thread needs to abort a sleep
43  * operation it should call sleepq_release() to unlock the associated sleep
44  * queue chain lock.  If the thread also needs to remove itself from a queue
45  * it just enqueued itself on, it can use sleepq_remove() instead.
46  *
47  * If the thread only wishes to sleep for a limited amount of time, it can
48  * call sleepq_set_timeout() after sleepq_add() to setup a timeout.  It
49  * should then use one of the sleepq_timedwait() functions to block.
50  *
51  * If the thread wants to the sleep to be interruptible by signals, it can
52  * call sleepq_catch_signals() after sleepq_add().  It should then use
53  * one of the sleepq_wait_sig() functions to block.  After the thread has
54  * been resumed, it should call sleepq_calc_signal_retval() to determine
55  * if it should return EINTR or ERESTART passing in the value returned from
56  * the earlier call to sleepq_catch_signals().
57  *
58  * A thread is normally resumed from a sleep queue by either the
59  * sleepq_signal() or sleepq_broadcast() functions.  Sleepq_signal() wakes
60  * the thread with the highest priority that is sleeping on the specified
61  * wait channel.  Sleepq_broadcast() wakes all threads that are sleeping
62  * on the specified wait channel.  A thread sleeping in an interruptible
63  * sleep can be interrupted by calling sleepq_abort().  A thread can also
64  * be removed from a specified sleep queue using the sleepq_remove()
65  * function.  Note that the sleep queue chain must first be locked via
66  * sleepq_lock() when calling sleepq_signal() and sleepq_broadcast().
67  *
68  * Each thread allocates a sleep queue at thread creation via sleepq_alloc()
69  * and releases it at thread destruction via sleepq_free().  Note that
70  * a sleep queue is not tied to a specific thread and that the sleep queue
71  * released at thread destruction may not be the same sleep queue that the
72  * thread allocated when it was created.
73  *
74  * XXX: Some other parts of the kernel such as ithread sleeping may end up
75  * using this interface as well (death to TDI_IWAIT!)
76  */
77 
78 struct lock_object;
79 struct sleepqueue;
80 struct thread;
81 
82 #ifdef _KERNEL
83 
84 #define	SLEEPQ_TYPE		0x0ff		/* Mask of sleep queue types. */
85 #define	SLEEPQ_MSLEEP		0x00		/* Used by msleep/wakeup. */
86 #define	SLEEPQ_CONDVAR		0x01		/* Used for a cv. */
87 #define	SLEEPQ_PAUSE		0x02		/* Used by pause. */
88 #define	SLEEPQ_INTERRUPTIBLE	0x100		/* Sleep is interruptible. */
89 
90 void	init_sleepqueues(void);
91 void	sleepq_abort(struct thread *td, int intrval);
92 void	sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg,
93 	    int flags, int queue);
94 struct sleepqueue *sleepq_alloc(void);
95 void	sleepq_broadcast(void *wchan, int flags, int pri, int queue);
96 void	sleepq_free(struct sleepqueue *sq);
97 void	sleepq_lock(void *wchan);
98 struct sleepqueue *sleepq_lookup(void *wchan);
99 void	sleepq_release(void *wchan);
100 void	sleepq_remove(struct thread *td, void *wchan);
101 void	sleepq_signal(void *wchan, int flags, int pri, int queue);
102 void	sleepq_set_timeout(void *wchan, int timo);
103 int	sleepq_timedwait(void *wchan);
104 int	sleepq_timedwait_sig(void *wchan);
105 void	sleepq_wait(void *wchan);
106 int	sleepq_wait_sig(void *wchan);
107 
108 #endif	/* _KERNEL */
109 #endif	/* !_SYS_SLEEPQUEUE_H_ */
110