xref: /freebsd/sys/sys/sleepqueue.h (revision 82e17adcb481ff13d2e088d8e66fc97d1b375021)
160727d8bSWarner Losh /*-
2dd75b0a9SJohn Baldwin  * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org>
3dd75b0a9SJohn Baldwin  * All rights reserved.
4dd75b0a9SJohn Baldwin  *
5dd75b0a9SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
6dd75b0a9SJohn Baldwin  * modification, are permitted provided that the following conditions
7dd75b0a9SJohn Baldwin  * are met:
8dd75b0a9SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
9dd75b0a9SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
10dd75b0a9SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
11dd75b0a9SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
12dd75b0a9SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
13dd75b0a9SJohn Baldwin  *
14dd75b0a9SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15dd75b0a9SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16dd75b0a9SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17dd75b0a9SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18dd75b0a9SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19dd75b0a9SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20dd75b0a9SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21dd75b0a9SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22dd75b0a9SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23dd75b0a9SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24dd75b0a9SJohn Baldwin  * SUCH DAMAGE.
25dd75b0a9SJohn Baldwin  *
26dd75b0a9SJohn Baldwin  * $FreeBSD$
27dd75b0a9SJohn Baldwin  */
28dd75b0a9SJohn Baldwin 
29dd75b0a9SJohn Baldwin #ifndef _SYS_SLEEPQUEUE_H_
30dd75b0a9SJohn Baldwin #define _SYS_SLEEPQUEUE_H_
31dd75b0a9SJohn Baldwin 
32dd75b0a9SJohn Baldwin /*
334e7f640dSJohn Baldwin  * Sleep queue interface.  Sleep/wakeup, condition variables, and sx
344e7f640dSJohn Baldwin  * locks use a sleep queue for the queue of threads blocked on a sleep
354e7f640dSJohn Baldwin  * channel.
36dd75b0a9SJohn Baldwin  *
372ff0e645SJohn Baldwin  * A thread calls sleepq_lock() to lock the sleep queue chain associated
382ff0e645SJohn Baldwin  * with a given wait channel.  A thread can then call call sleepq_add() to
392ff0e645SJohn Baldwin  * add themself onto a sleep queue and call one of the sleepq_wait()
402ff0e645SJohn Baldwin  * functions to actually go to sleep.  If a thread needs to abort a sleep
412ff0e645SJohn Baldwin  * operation it should call sleepq_release() to unlock the associated sleep
422ff0e645SJohn Baldwin  * queue chain lock.  If the thread also needs to remove itself from a queue
432ff0e645SJohn Baldwin  * it just enqueued itself on, it can use sleepq_remove() instead.
44dd75b0a9SJohn Baldwin  *
45dd75b0a9SJohn Baldwin  * If the thread only wishes to sleep for a limited amount of time, it can
46dd75b0a9SJohn Baldwin  * call sleepq_set_timeout() after sleepq_add() to setup a timeout.  It
47dd75b0a9SJohn Baldwin  * should then use one of the sleepq_timedwait() functions to block.
48dd75b0a9SJohn Baldwin  *
49dd75b0a9SJohn Baldwin  * A thread is normally resumed from a sleep queue by either the
50dd75b0a9SJohn Baldwin  * sleepq_signal() or sleepq_broadcast() functions.  Sleepq_signal() wakes
51dd75b0a9SJohn Baldwin  * the thread with the highest priority that is sleeping on the specified
52dd75b0a9SJohn Baldwin  * wait channel.  Sleepq_broadcast() wakes all threads that are sleeping
53dd75b0a9SJohn Baldwin  * on the specified wait channel.  A thread sleeping in an interruptible
54dd75b0a9SJohn Baldwin  * sleep can be interrupted by calling sleepq_abort().  A thread can also
55dd75b0a9SJohn Baldwin  * be removed from a specified sleep queue using the sleepq_remove()
562ff0e645SJohn Baldwin  * function.  Note that the sleep queue chain must first be locked via
572cdcea5eSJohn Baldwin  * sleepq_lock() before calling sleepq_abort(), sleepq_broadcast(), or
582cdcea5eSJohn Baldwin  * sleepq_signal().  These routines each return a boolean that will be true
592cdcea5eSJohn Baldwin  * if at least one swapped-out thread was resumed.  In that case, the caller
602cdcea5eSJohn Baldwin  * is responsible for waking up the swapper by calling kick_proc0() after
61b4dac21dSKirk McKusick  * releasing the sleep queue chain lock.
62dd75b0a9SJohn Baldwin  *
63dd75b0a9SJohn Baldwin  * Each thread allocates a sleep queue at thread creation via sleepq_alloc()
64dd75b0a9SJohn Baldwin  * and releases it at thread destruction via sleepq_free().  Note that
65dd75b0a9SJohn Baldwin  * a sleep queue is not tied to a specific thread and that the sleep queue
66dd75b0a9SJohn Baldwin  * released at thread destruction may not be the same sleep queue that the
67dd75b0a9SJohn Baldwin  * thread allocated when it was created.
68dd75b0a9SJohn Baldwin  *
69dd75b0a9SJohn Baldwin  * XXX: Some other parts of the kernel such as ithread sleeping may end up
70dd75b0a9SJohn Baldwin  * using this interface as well (death to TDI_IWAIT!)
71dd75b0a9SJohn Baldwin  */
72dd75b0a9SJohn Baldwin 
737ee07175SPawel Jakub Dawidek struct lock_object;
74dd75b0a9SJohn Baldwin struct sleepqueue;
75dd75b0a9SJohn Baldwin struct thread;
76dd75b0a9SJohn Baldwin 
77dd75b0a9SJohn Baldwin #ifdef _KERNEL
78dd75b0a9SJohn Baldwin 
79007ddf7eSJohn Baldwin #define	SLEEPQ_TYPE		0x0ff		/* Mask of sleep queue types. */
80e7573e7aSJohn Baldwin #define	SLEEPQ_SLEEP		0x00		/* Used by sleep/wakeup. */
81007ddf7eSJohn Baldwin #define	SLEEPQ_CONDVAR		0x01		/* Used for a cv. */
8237e80fcaSJohn Baldwin #define	SLEEPQ_PAUSE		0x02		/* Used by pause. */
834e7f640dSJohn Baldwin #define	SLEEPQ_SX		0x03		/* Used by an sx lock. */
84047dd67eSAttilio Rao #define	SLEEPQ_LK		0x04		/* Used by a lockmgr. */
85007ddf7eSJohn Baldwin #define	SLEEPQ_INTERRUPTIBLE	0x100		/* Sleep is interruptible. */
86dd75b0a9SJohn Baldwin 
87dd75b0a9SJohn Baldwin void	init_sleepqueues(void);
88da7bbd2cSJohn Baldwin int	sleepq_abort(struct thread *td, int intrval);
896cbb70e2SKip Macy void	sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg,
906cbb70e2SKip Macy 	    int flags, int queue);
91dd75b0a9SJohn Baldwin struct sleepqueue *sleepq_alloc(void);
92da7bbd2cSJohn Baldwin int	sleepq_broadcast(void *wchan, int flags, int pri, int queue);
936cbb70e2SKip Macy void	sleepq_free(struct sleepqueue *sq);
946cbb70e2SKip Macy void	sleepq_lock(void *wchan);
956cbb70e2SKip Macy struct sleepqueue *sleepq_lookup(void *wchan);
966cbb70e2SKip Macy void	sleepq_release(void *wchan);
976cbb70e2SKip Macy void	sleepq_remove(struct thread *td, void *wchan);
98da7bbd2cSJohn Baldwin int	sleepq_signal(void *wchan, int flags, int pri, int queue);
99965ac611SDavide Italiano void	sleepq_set_timeout_sbt(void *wchan, sbintime_t sbt,
100965ac611SDavide Italiano 	    sbintime_t pr, int flags);
101965ac611SDavide Italiano #define	sleepq_set_timeout(wchan, timo)					\
1027392d01cSDavide Italiano     sleepq_set_timeout_sbt((wchan), tick_sbt * (timo), 0, C_HARDCLOCK)
1032028867dSAttilio Rao u_int	sleepq_sleepcnt(void *wchan, int queue);
104c5aa6b58SJeff Roberson int	sleepq_timedwait(void *wchan, int pri);
105c5aa6b58SJeff Roberson int	sleepq_timedwait_sig(void *wchan, int pri);
106f7829d0dSAttilio Rao int	sleepq_type(void *wchan);
107c5aa6b58SJeff Roberson void	sleepq_wait(void *wchan, int pri);
108c5aa6b58SJeff Roberson int	sleepq_wait_sig(void *wchan, int pri);
109dd75b0a9SJohn Baldwin 
110*82e17adcSConrad Meyer #ifdef STACK
111*82e17adcSConrad Meyer struct sbuf;
11270e20d4eSConrad Meyer int sleepq_sbuf_print_stacks(struct sbuf *sb, void *wchan, int queue,
11370e20d4eSConrad Meyer     int *count_stacks_printed);
114*82e17adcSConrad Meyer #endif
11570e20d4eSConrad Meyer 
116dd75b0a9SJohn Baldwin #endif	/* _KERNEL */
117dd75b0a9SJohn Baldwin #endif	/* !_SYS_SLEEPQUEUE_H_ */
118