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 * 3. Neither the name of the author nor the names of any co-contributors 14dd75b0a9SJohn Baldwin * may be used to endorse or promote products derived from this software 15dd75b0a9SJohn Baldwin * without specific prior written permission. 16dd75b0a9SJohn Baldwin * 17dd75b0a9SJohn Baldwin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18dd75b0a9SJohn Baldwin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19dd75b0a9SJohn Baldwin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20dd75b0a9SJohn Baldwin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21dd75b0a9SJohn Baldwin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22dd75b0a9SJohn Baldwin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23dd75b0a9SJohn Baldwin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24dd75b0a9SJohn Baldwin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25dd75b0a9SJohn Baldwin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26dd75b0a9SJohn Baldwin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27dd75b0a9SJohn Baldwin * SUCH DAMAGE. 28dd75b0a9SJohn Baldwin * 29dd75b0a9SJohn Baldwin * $FreeBSD$ 30dd75b0a9SJohn Baldwin */ 31dd75b0a9SJohn Baldwin 32dd75b0a9SJohn Baldwin #ifndef _SYS_SLEEPQUEUE_H_ 33dd75b0a9SJohn Baldwin #define _SYS_SLEEPQUEUE_H_ 34dd75b0a9SJohn Baldwin 35dd75b0a9SJohn Baldwin /* 364e7f640dSJohn Baldwin * Sleep queue interface. Sleep/wakeup, condition variables, and sx 374e7f640dSJohn Baldwin * locks use a sleep queue for the queue of threads blocked on a sleep 384e7f640dSJohn Baldwin * channel. 39dd75b0a9SJohn Baldwin * 402ff0e645SJohn Baldwin * A thread calls sleepq_lock() to lock the sleep queue chain associated 412ff0e645SJohn Baldwin * with a given wait channel. A thread can then call call sleepq_add() to 422ff0e645SJohn Baldwin * add themself onto a sleep queue and call one of the sleepq_wait() 432ff0e645SJohn Baldwin * functions to actually go to sleep. If a thread needs to abort a sleep 442ff0e645SJohn Baldwin * operation it should call sleepq_release() to unlock the associated sleep 452ff0e645SJohn Baldwin * queue chain lock. If the thread also needs to remove itself from a queue 462ff0e645SJohn Baldwin * it just enqueued itself on, it can use sleepq_remove() instead. 47dd75b0a9SJohn Baldwin * 48dd75b0a9SJohn Baldwin * If the thread only wishes to sleep for a limited amount of time, it can 49dd75b0a9SJohn Baldwin * call sleepq_set_timeout() after sleepq_add() to setup a timeout. It 50dd75b0a9SJohn Baldwin * should then use one of the sleepq_timedwait() functions to block. 51dd75b0a9SJohn Baldwin * 52dd75b0a9SJohn Baldwin * If the thread wants to the sleep to be interruptible by signals, it can 53dd75b0a9SJohn Baldwin * call sleepq_catch_signals() after sleepq_add(). It should then use 54dd75b0a9SJohn Baldwin * one of the sleepq_wait_sig() functions to block. After the thread has 55dd75b0a9SJohn Baldwin * been resumed, it should call sleepq_calc_signal_retval() to determine 56dd75b0a9SJohn Baldwin * if it should return EINTR or ERESTART passing in the value returned from 57dd75b0a9SJohn Baldwin * the earlier call to sleepq_catch_signals(). 58dd75b0a9SJohn Baldwin * 59dd75b0a9SJohn Baldwin * A thread is normally resumed from a sleep queue by either the 60dd75b0a9SJohn Baldwin * sleepq_signal() or sleepq_broadcast() functions. Sleepq_signal() wakes 61dd75b0a9SJohn Baldwin * the thread with the highest priority that is sleeping on the specified 62dd75b0a9SJohn Baldwin * wait channel. Sleepq_broadcast() wakes all threads that are sleeping 63dd75b0a9SJohn Baldwin * on the specified wait channel. A thread sleeping in an interruptible 64dd75b0a9SJohn Baldwin * sleep can be interrupted by calling sleepq_abort(). A thread can also 65dd75b0a9SJohn Baldwin * be removed from a specified sleep queue using the sleepq_remove() 662ff0e645SJohn Baldwin * function. Note that the sleep queue chain must first be locked via 672ff0e645SJohn Baldwin * sleepq_lock() when calling sleepq_signal() and sleepq_broadcast(). 68dd75b0a9SJohn Baldwin * 69dd75b0a9SJohn Baldwin * Each thread allocates a sleep queue at thread creation via sleepq_alloc() 70dd75b0a9SJohn Baldwin * and releases it at thread destruction via sleepq_free(). Note that 71dd75b0a9SJohn Baldwin * a sleep queue is not tied to a specific thread and that the sleep queue 72dd75b0a9SJohn Baldwin * released at thread destruction may not be the same sleep queue that the 73dd75b0a9SJohn Baldwin * thread allocated when it was created. 74dd75b0a9SJohn Baldwin * 75dd75b0a9SJohn Baldwin * XXX: Some other parts of the kernel such as ithread sleeping may end up 76dd75b0a9SJohn Baldwin * using this interface as well (death to TDI_IWAIT!) 77dd75b0a9SJohn Baldwin */ 78dd75b0a9SJohn Baldwin 797ee07175SPawel Jakub Dawidek struct lock_object; 80dd75b0a9SJohn Baldwin struct sleepqueue; 81dd75b0a9SJohn Baldwin struct thread; 82dd75b0a9SJohn Baldwin 83dd75b0a9SJohn Baldwin #ifdef _KERNEL 84dd75b0a9SJohn Baldwin 85007ddf7eSJohn Baldwin #define SLEEPQ_TYPE 0x0ff /* Mask of sleep queue types. */ 86e7573e7aSJohn Baldwin #define SLEEPQ_SLEEP 0x00 /* Used by sleep/wakeup. */ 87007ddf7eSJohn Baldwin #define SLEEPQ_CONDVAR 0x01 /* Used for a cv. */ 8837e80fcaSJohn Baldwin #define SLEEPQ_PAUSE 0x02 /* Used by pause. */ 894e7f640dSJohn Baldwin #define SLEEPQ_SX 0x03 /* Used by an sx lock. */ 90047dd67eSAttilio Rao #define SLEEPQ_LK 0x04 /* Used by a lockmgr. */ 91007ddf7eSJohn Baldwin #define SLEEPQ_INTERRUPTIBLE 0x100 /* Sleep is interruptible. */ 92dd75b0a9SJohn Baldwin 93dd75b0a9SJohn Baldwin void init_sleepqueues(void); 9494f0972bSDavid Xu void sleepq_abort(struct thread *td, int intrval); 956cbb70e2SKip Macy void sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg, 966cbb70e2SKip Macy int flags, int queue); 97dd75b0a9SJohn Baldwin struct sleepqueue *sleepq_alloc(void); 986cbb70e2SKip Macy void sleepq_broadcast(void *wchan, int flags, int pri, int queue); 996cbb70e2SKip Macy void sleepq_free(struct sleepqueue *sq); 1006cbb70e2SKip Macy void sleepq_lock(void *wchan); 1016cbb70e2SKip Macy struct sleepqueue *sleepq_lookup(void *wchan); 1026cbb70e2SKip Macy void sleepq_release(void *wchan); 1036cbb70e2SKip Macy void sleepq_remove(struct thread *td, void *wchan); 1046cbb70e2SKip Macy void sleepq_signal(void *wchan, int flags, int pri, int queue); 1051ed3e44fSJohn Baldwin void sleepq_set_timeout(void *wchan, int timo); 106c5aa6b58SJeff Roberson int sleepq_timedwait(void *wchan, int pri); 107c5aa6b58SJeff Roberson int sleepq_timedwait_sig(void *wchan, int pri); 108c5aa6b58SJeff Roberson void sleepq_wait(void *wchan, int pri); 109c5aa6b58SJeff Roberson int sleepq_wait_sig(void *wchan, int pri); 110dd75b0a9SJohn Baldwin 111dd75b0a9SJohn Baldwin #endif /* _KERNEL */ 112dd75b0a9SJohn Baldwin #endif /* !_SYS_SLEEPQUEUE_H_ */ 113