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 * 14126324Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15126324Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16126324Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17126324Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18126324Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19126324Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20126324Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21126324Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22126324Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23126324Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24126324Sjhb * SUCH DAMAGE. 25126324Sjhb * 26126324Sjhb * $FreeBSD$ 27126324Sjhb */ 28126324Sjhb 29126324Sjhb#ifndef _SYS_SLEEPQUEUE_H_ 30126324Sjhb#define _SYS_SLEEPQUEUE_H_ 31126324Sjhb 32126324Sjhb/* 33168191Sjhb * Sleep queue interface. Sleep/wakeup, condition variables, and sx 34168191Sjhb * locks use a sleep queue for the queue of threads blocked on a sleep 35168191Sjhb * channel. 36126324Sjhb * 37136445Sjhb * A thread calls sleepq_lock() to lock the sleep queue chain associated 38136445Sjhb * with a given wait channel. A thread can then call call sleepq_add() to 39136445Sjhb * add themself onto a sleep queue and call one of the sleepq_wait() 40136445Sjhb * functions to actually go to sleep. If a thread needs to abort a sleep 41136445Sjhb * operation it should call sleepq_release() to unlock the associated sleep 42136445Sjhb * queue chain lock. If the thread also needs to remove itself from a queue 43136445Sjhb * it just enqueued itself on, it can use sleepq_remove() instead. 44126324Sjhb * 45126324Sjhb * If the thread only wishes to sleep for a limited amount of time, it can 46126324Sjhb * call sleepq_set_timeout() after sleepq_add() to setup a timeout. It 47126324Sjhb * should then use one of the sleepq_timedwait() functions to block. 48126324Sjhb * 49216421Smckusick * If the thread wants the sleep to be interruptible by signals, it can 50126324Sjhb * call sleepq_catch_signals() after sleepq_add(). It should then use 51126324Sjhb * one of the sleepq_wait_sig() functions to block. After the thread has 52126324Sjhb * been resumed, it should call sleepq_calc_signal_retval() to determine 53126324Sjhb * if it should return EINTR or ERESTART passing in the value returned from 54126324Sjhb * the earlier call to sleepq_catch_signals(). 55126324Sjhb * 56126324Sjhb * A thread is normally resumed from a sleep queue by either the 57126324Sjhb * sleepq_signal() or sleepq_broadcast() functions. Sleepq_signal() wakes 58126324Sjhb * the thread with the highest priority that is sleeping on the specified 59126324Sjhb * wait channel. Sleepq_broadcast() wakes all threads that are sleeping 60126324Sjhb * on the specified wait channel. A thread sleeping in an interruptible 61126324Sjhb * sleep can be interrupted by calling sleepq_abort(). A thread can also 62126324Sjhb * be removed from a specified sleep queue using the sleepq_remove() 63136445Sjhb * function. Note that the sleep queue chain must first be locked via 64181390Sjhb * sleepq_lock() before calling sleepq_abort(), sleepq_broadcast(), or 65181390Sjhb * sleepq_signal(). These routines each return a boolean that will be true 66181390Sjhb * if at least one swapped-out thread was resumed. In that case, the caller 67181390Sjhb * is responsible for waking up the swapper by calling kick_proc0() after 68216421Smckusick * releasing the sleep queue chain lock. 69126324Sjhb * 70126324Sjhb * Each thread allocates a sleep queue at thread creation via sleepq_alloc() 71126324Sjhb * and releases it at thread destruction via sleepq_free(). Note that 72126324Sjhb * a sleep queue is not tied to a specific thread and that the sleep queue 73126324Sjhb * released at thread destruction may not be the same sleep queue that the 74126324Sjhb * thread allocated when it was created. 75126324Sjhb * 76126324Sjhb * XXX: Some other parts of the kernel such as ithread sleeping may end up 77126324Sjhb * using this interface as well (death to TDI_IWAIT!) 78126324Sjhb */ 79126324Sjhb 80164325Spjdstruct lock_object; 81126324Sjhbstruct sleepqueue; 82126324Sjhbstruct thread; 83126324Sjhb 84126324Sjhb#ifdef _KERNEL 85126324Sjhb 86134013Sjhb#define SLEEPQ_TYPE 0x0ff /* Mask of sleep queue types. */ 87167387Sjhb#define SLEEPQ_SLEEP 0x00 /* Used by sleep/wakeup. */ 88134013Sjhb#define SLEEPQ_CONDVAR 0x01 /* Used for a cv. */ 89166908Sjhb#define SLEEPQ_PAUSE 0x02 /* Used by pause. */ 90168191Sjhb#define SLEEPQ_SX 0x03 /* Used by an sx lock. */ 91177957Sattilio#define SLEEPQ_LK 0x04 /* Used by a lockmgr. */ 92134013Sjhb#define SLEEPQ_INTERRUPTIBLE 0x100 /* Sleep is interruptible. */ 93195702Skib#define SLEEPQ_STOP_ON_BDRY 0x200 /* Stop sleeping thread on 94195702Skib user mode boundary */ 95126324Sjhb 96126324Sjhbvoid init_sleepqueues(void); 97181334Sjhbint sleepq_abort(struct thread *td, int intrval); 98165272Skmacyvoid sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg, 99165272Skmacy int flags, int queue); 100126324Sjhbstruct sleepqueue *sleepq_alloc(void); 101181334Sjhbint sleepq_broadcast(void *wchan, int flags, int pri, int queue); 102165272Skmacyvoid sleepq_free(struct sleepqueue *sq); 103165272Skmacyvoid sleepq_lock(void *wchan); 104165272Skmacystruct sleepqueue *sleepq_lookup(void *wchan); 105165272Skmacyvoid sleepq_release(void *wchan); 106165272Skmacyvoid sleepq_remove(struct thread *td, void *wchan); 107181334Sjhbint sleepq_signal(void *wchan, int flags, int pri, int queue); 108126885Sjhbvoid sleepq_set_timeout(void *wchan, int timo); 109200447Sattiliou_int sleepq_sleepcnt(void *wchan, int queue); 110177085Sjeffint sleepq_timedwait(void *wchan, int pri); 111177085Sjeffint sleepq_timedwait_sig(void *wchan, int pri); 112201879Sattilioint sleepq_type(void *wchan); 113177085Sjeffvoid sleepq_wait(void *wchan, int pri); 114177085Sjeffint sleepq_wait_sig(void *wchan, int pri); 115126324Sjhb 116126324Sjhb#endif /* _KERNEL */ 117126324Sjhb#endif /* !_SYS_SLEEPQUEUE_H_ */ 118