sleepqueue.h revision 177085
1139825Simp/*- 2126324Sjhb * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org> 3126324Sjhb * All rights reserved. 4126324Sjhb * 5126324Sjhb * Redistribution and use in source and binary forms, with or without 6126324Sjhb * modification, are permitted provided that the following conditions 7126324Sjhb * are met: 8126324Sjhb * 1. Redistributions of source code must retain the above copyright 9126324Sjhb * notice, this list of conditions and the following disclaimer. 10126324Sjhb * 2. Redistributions in binary form must reproduce the above copyright 11126324Sjhb * notice, this list of conditions and the following disclaimer in the 12126324Sjhb * documentation and/or other materials provided with the distribution. 13126324Sjhb * 3. Neither the name of the author nor the names of any co-contributors 14126324Sjhb * may be used to endorse or promote products derived from this software 15126324Sjhb * without specific prior written permission. 16126324Sjhb * 17126324Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18126324Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19126324Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20126324Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21126324Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22126324Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23126324Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24126324Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25126324Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26126324Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27126324Sjhb * SUCH DAMAGE. 28126324Sjhb * 29126324Sjhb * $FreeBSD: head/sys/sys/sleepqueue.h 177085 2008-03-12 06:31:06Z jeff $ 30126324Sjhb */ 31126324Sjhb 32126324Sjhb#ifndef _SYS_SLEEPQUEUE_H_ 33126324Sjhb#define _SYS_SLEEPQUEUE_H_ 34126324Sjhb 35126324Sjhb/* 36168191Sjhb * Sleep queue interface. Sleep/wakeup, condition variables, and sx 37168191Sjhb * locks use a sleep queue for the queue of threads blocked on a sleep 38168191Sjhb * channel. 39126324Sjhb * 40136445Sjhb * A thread calls sleepq_lock() to lock the sleep queue chain associated 41136445Sjhb * with a given wait channel. A thread can then call call sleepq_add() to 42136445Sjhb * add themself onto a sleep queue and call one of the sleepq_wait() 43136445Sjhb * functions to actually go to sleep. If a thread needs to abort a sleep 44136445Sjhb * operation it should call sleepq_release() to unlock the associated sleep 45136445Sjhb * queue chain lock. If the thread also needs to remove itself from a queue 46136445Sjhb * it just enqueued itself on, it can use sleepq_remove() instead. 47126324Sjhb * 48126324Sjhb * If the thread only wishes to sleep for a limited amount of time, it can 49126324Sjhb * call sleepq_set_timeout() after sleepq_add() to setup a timeout. It 50126324Sjhb * should then use one of the sleepq_timedwait() functions to block. 51126324Sjhb * 52126324Sjhb * If the thread wants to the sleep to be interruptible by signals, it can 53126324Sjhb * call sleepq_catch_signals() after sleepq_add(). It should then use 54126324Sjhb * one of the sleepq_wait_sig() functions to block. After the thread has 55126324Sjhb * been resumed, it should call sleepq_calc_signal_retval() to determine 56126324Sjhb * if it should return EINTR or ERESTART passing in the value returned from 57126324Sjhb * the earlier call to sleepq_catch_signals(). 58126324Sjhb * 59126324Sjhb * A thread is normally resumed from a sleep queue by either the 60126324Sjhb * sleepq_signal() or sleepq_broadcast() functions. Sleepq_signal() wakes 61126324Sjhb * the thread with the highest priority that is sleeping on the specified 62126324Sjhb * wait channel. Sleepq_broadcast() wakes all threads that are sleeping 63126324Sjhb * on the specified wait channel. A thread sleeping in an interruptible 64126324Sjhb * sleep can be interrupted by calling sleepq_abort(). A thread can also 65126324Sjhb * be removed from a specified sleep queue using the sleepq_remove() 66136445Sjhb * function. Note that the sleep queue chain must first be locked via 67136445Sjhb * sleepq_lock() when calling sleepq_signal() and sleepq_broadcast(). 68126324Sjhb * 69126324Sjhb * Each thread allocates a sleep queue at thread creation via sleepq_alloc() 70126324Sjhb * and releases it at thread destruction via sleepq_free(). Note that 71126324Sjhb * a sleep queue is not tied to a specific thread and that the sleep queue 72126324Sjhb * released at thread destruction may not be the same sleep queue that the 73126324Sjhb * thread allocated when it was created. 74126324Sjhb * 75126324Sjhb * XXX: Some other parts of the kernel such as ithread sleeping may end up 76126324Sjhb * using this interface as well (death to TDI_IWAIT!) 77126324Sjhb */ 78126324Sjhb 79164325Spjdstruct lock_object; 80126324Sjhbstruct sleepqueue; 81126324Sjhbstruct thread; 82126324Sjhb 83126324Sjhb#ifdef _KERNEL 84126324Sjhb 85134013Sjhb#define SLEEPQ_TYPE 0x0ff /* Mask of sleep queue types. */ 86167387Sjhb#define SLEEPQ_SLEEP 0x00 /* Used by sleep/wakeup. */ 87134013Sjhb#define SLEEPQ_CONDVAR 0x01 /* Used for a cv. */ 88166908Sjhb#define SLEEPQ_PAUSE 0x02 /* Used by pause. */ 89168191Sjhb#define SLEEPQ_SX 0x03 /* Used by an sx lock. */ 90134013Sjhb#define SLEEPQ_INTERRUPTIBLE 0x100 /* Sleep is interruptible. */ 91126324Sjhb 92126324Sjhbvoid init_sleepqueues(void); 93155741Sdavidxuvoid sleepq_abort(struct thread *td, int intrval); 94165272Skmacyvoid sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg, 95165272Skmacy int flags, int queue); 96126324Sjhbstruct sleepqueue *sleepq_alloc(void); 97165272Skmacyvoid sleepq_broadcast(void *wchan, int flags, int pri, int queue); 98165272Skmacyvoid sleepq_free(struct sleepqueue *sq); 99165272Skmacyvoid sleepq_lock(void *wchan); 100165272Skmacystruct sleepqueue *sleepq_lookup(void *wchan); 101165272Skmacyvoid sleepq_release(void *wchan); 102165272Skmacyvoid sleepq_remove(struct thread *td, void *wchan); 103165272Skmacyvoid sleepq_signal(void *wchan, int flags, int pri, int queue); 104126885Sjhbvoid sleepq_set_timeout(void *wchan, int timo); 105177085Sjeffint sleepq_timedwait(void *wchan, int pri); 106177085Sjeffint sleepq_timedwait_sig(void *wchan, int pri); 107177085Sjeffvoid sleepq_wait(void *wchan, int pri); 108177085Sjeffint sleepq_wait_sig(void *wchan, int pri); 109126324Sjhb 110126324Sjhb#endif /* _KERNEL */ 111126324Sjhb#endif /* !_SYS_SLEEPQUEUE_H_ */ 112