sleepqueue.h revision 247783
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org>
31541Srgrimes * All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. Neither the name of the author nor the names of any co-contributors
141541Srgrimes *    may be used to endorse or promote products derived from this software
151541Srgrimes *    without specific prior written permission.
161541Srgrimes *
171541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
181541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
211541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271541Srgrimes * SUCH DAMAGE.
281541Srgrimes *
291541Srgrimes * $FreeBSD: head/sys/sys/sleepqueue.h 247783 2013-03-04 11:51:46Z davide $
301541Srgrimes */
311541Srgrimes
321541Srgrimes#ifndef _SYS_SLEEPQUEUE_H_
331541Srgrimes#define _SYS_SLEEPQUEUE_H_
341541Srgrimes
351541Srgrimes/*
361541Srgrimes * Sleep queue interface.  Sleep/wakeup, condition variables, and sx
371541Srgrimes * locks use a sleep queue for the queue of threads blocked on a sleep
381541Srgrimes * channel.
391541Srgrimes *
401541Srgrimes * A thread calls sleepq_lock() to lock the sleep queue chain associated
411541Srgrimes * with a given wait channel.  A thread can then call call sleepq_add() to
421541Srgrimes * add themself onto a sleep queue and call one of the sleepq_wait()
431541Srgrimes * functions to actually go to sleep.  If a thread needs to abort a sleep
441541Srgrimes * operation it should call sleepq_release() to unlock the associated sleep
451541Srgrimes * queue chain lock.  If the thread also needs to remove itself from a queue
461541Srgrimes * it just enqueued itself on, it can use sleepq_remove() instead.
471541Srgrimes *
481541Srgrimes * If the thread only wishes to sleep for a limited amount of time, it can
491541Srgrimes * call sleepq_set_timeout() after sleepq_add() to setup a timeout.  It
501541Srgrimes * should then use one of the sleepq_timedwait() functions to block.
511541Srgrimes *
521541Srgrimes * If the thread wants the sleep to be interruptible by signals, it can
531541Srgrimes * call sleepq_catch_signals() after sleepq_add().  It should then use
541541Srgrimes * one of the sleepq_wait_sig() functions to block.  After the thread has
551541Srgrimes * been resumed, it should call sleepq_calc_signal_retval() to determine
561541Srgrimes * if it should return EINTR or ERESTART passing in the value returned from
571541Srgrimes * the earlier call to sleepq_catch_signals().
581541Srgrimes *
591541Srgrimes * A thread is normally resumed from a sleep queue by either the
601541Srgrimes * sleepq_signal() or sleepq_broadcast() functions.  Sleepq_signal() wakes
611541Srgrimes * the thread with the highest priority that is sleeping on the specified
621541Srgrimes * wait channel.  Sleepq_broadcast() wakes all threads that are sleeping
631541Srgrimes * on the specified wait channel.  A thread sleeping in an interruptible
641541Srgrimes * sleep can be interrupted by calling sleepq_abort().  A thread can also
651541Srgrimes * be removed from a specified sleep queue using the sleepq_remove()
661541Srgrimes * function.  Note that the sleep queue chain must first be locked via
671541Srgrimes * sleepq_lock() before calling sleepq_abort(), sleepq_broadcast(), or
681541Srgrimes * sleepq_signal().  These routines each return a boolean that will be true
691541Srgrimes * if at least one swapped-out thread was resumed.  In that case, the caller
701541Srgrimes * is responsible for waking up the swapper by calling kick_proc0() after
711541Srgrimes * releasing the sleep queue chain lock.
721541Srgrimes *
73 * Each thread allocates a sleep queue at thread creation via sleepq_alloc()
74 * and releases it at thread destruction via sleepq_free().  Note that
75 * a sleep queue is not tied to a specific thread and that the sleep queue
76 * released at thread destruction may not be the same sleep queue that the
77 * thread allocated when it was created.
78 *
79 * XXX: Some other parts of the kernel such as ithread sleeping may end up
80 * using this interface as well (death to TDI_IWAIT!)
81 */
82
83struct lock_object;
84struct sleepqueue;
85struct thread;
86
87#ifdef _KERNEL
88
89#define	SLEEPQ_TYPE		0x0ff		/* Mask of sleep queue types. */
90#define	SLEEPQ_SLEEP		0x00		/* Used by sleep/wakeup. */
91#define	SLEEPQ_CONDVAR		0x01		/* Used for a cv. */
92#define	SLEEPQ_PAUSE		0x02		/* Used by pause. */
93#define	SLEEPQ_SX		0x03		/* Used by an sx lock. */
94#define	SLEEPQ_LK		0x04		/* Used by a lockmgr. */
95#define	SLEEPQ_INTERRUPTIBLE	0x100		/* Sleep is interruptible. */
96#define	SLEEPQ_STOP_ON_BDRY	0x200		/* Stop sleeping thread on
97						   user mode boundary */
98
99void	init_sleepqueues(void);
100int	sleepq_abort(struct thread *td, int intrval);
101void	sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg,
102	    int flags, int queue);
103struct sleepqueue *sleepq_alloc(void);
104int	sleepq_broadcast(void *wchan, int flags, int pri, int queue);
105void	sleepq_free(struct sleepqueue *sq);
106void	sleepq_lock(void *wchan);
107struct sleepqueue *sleepq_lookup(void *wchan);
108void	sleepq_release(void *wchan);
109void	sleepq_remove(struct thread *td, void *wchan);
110int	sleepq_signal(void *wchan, int flags, int pri, int queue);
111void	sleepq_set_timeout_sbt(void *wchan, sbintime_t sbt,
112	    sbintime_t pr, int flags);
113#define	sleepq_set_timeout(wchan, timo)					\
114    sleepq_set_timeout_sbt((wchan), (tick_sbt * (timo)), 0, C_HARDCLOCK)
115u_int	sleepq_sleepcnt(void *wchan, int queue);
116int	sleepq_timedwait(void *wchan, int pri);
117int	sleepq_timedwait_sig(void *wchan, int pri);
118int	sleepq_type(void *wchan);
119void	sleepq_wait(void *wchan, int pri);
120int	sleepq_wait_sig(void *wchan, int pri);
121
122#endif	/* _KERNEL */
123#endif	/* !_SYS_SLEEPQUEUE_H_ */
124