xref: /freebsd/sys/sys/sleepqueue.h (revision 74bf4e164ba5851606a27d4feff27717452583e5)
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_lookup() to look up the proper sleep queue in the
40  * hash table that is associated with a specified wait channel.  This
41  * function returns a pointer to the queue and locks the associated sleep
42  * queue chain.  A thread calls sleepq_add() to add themself onto a sleep
43  * queue and calls one of the sleepq_wait() functions to actually go to
44  * sleep.  If a thread needs to abort a sleep operation it should call
45  * sleepq_release() to unlock the associated sleep queue chain lock.  If
46  * the thread also needs to remove itself from a queue it just enqueued
47  * itself on, it can use sleepq_remove().
48  *
49  * If the thread only wishes to sleep for a limited amount of time, it can
50  * call sleepq_set_timeout() after sleepq_add() to setup a timeout.  It
51  * should then use one of the sleepq_timedwait() functions to block.
52  *
53  * If the thread wants to the sleep to be interruptible by signals, it can
54  * call sleepq_catch_signals() after sleepq_add().  It should then use
55  * one of the sleepq_wait_sig() functions to block.  After the thread has
56  * been resumed, it should call sleepq_calc_signal_retval() to determine
57  * if it should return EINTR or ERESTART passing in the value returned from
58  * the earlier call to sleepq_catch_signals().
59  *
60  * A thread is normally resumed from a sleep queue by either the
61  * sleepq_signal() or sleepq_broadcast() functions.  Sleepq_signal() wakes
62  * the thread with the highest priority that is sleeping on the specified
63  * wait channel.  Sleepq_broadcast() wakes all threads that are sleeping
64  * on the specified wait channel.  A thread sleeping in an interruptible
65  * sleep can be interrupted by calling sleepq_abort().  A thread can also
66  * be removed from a specified sleep queue using the sleepq_remove()
67  * function.
68  *
69  * Each thread allocates a sleep queue at thread creation via sleepq_alloc()
70  * and releases it at thread destruction via sleepq_free().  Note that
71  * a sleep queue is not tied to a specific thread and that the sleep queue
72  * released at thread destruction may not be the same sleep queue that the
73  * thread allocated when it was created.
74  *
75  * XXX: Some other parts of the kernel such as ithread sleeping may end up
76  * using this interface as well (death to TDI_IWAIT!)
77  */
78 
79 struct mtx;
80 struct sleepqueue;
81 struct thread;
82 
83 #ifdef _KERNEL
84 
85 #define	SLEEPQ_TYPE		0x0ff		/* Mask of sleep queue types. */
86 #define	SLEEPQ_MSLEEP		0x00		/* Used by msleep/wakeup. */
87 #define	SLEEPQ_CONDVAR		0x01		/* Used for a cv. */
88 #define	SLEEPQ_INTERRUPTIBLE	0x100		/* Sleep is interruptible. */
89 
90 void	init_sleepqueues(void);
91 void	sleepq_abort(struct thread *td);
92 void	sleepq_add(struct sleepqueue *, void *, struct mtx *, const char *,
93 	    int);
94 struct sleepqueue *sleepq_alloc(void);
95 void	sleepq_broadcast(void *, int, int);
96 int	sleepq_calc_signal_retval(int sig);
97 int	sleepq_catch_signals(void *wchan);
98 void	sleepq_free(struct sleepqueue *);
99 struct sleepqueue *sleepq_lookup(void *);
100 void	sleepq_release(void *);
101 void	sleepq_remove(struct thread *, void *);
102 void	sleepq_signal(void *, int, int);
103 void	sleepq_set_timeout(void *wchan, int timo);
104 int	sleepq_timedwait(void *wchan);
105 int	sleepq_timedwait_sig(void *wchan, int signal_caught);
106 void	sleepq_wait(void *);
107 int	sleepq_wait_sig(void *wchan);
108 
109 #endif	/* _KERNEL */
110 #endif	/* !_SYS_SLEEPQUEUE_H_ */
111