sleepqueue.h revision 367457
1/*- 2 * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD: stable/11/sys/sys/sleepqueue.h 367457 2020-11-07 18:10:59Z dim $ 26 */ 27 28#ifndef _SYS_SLEEPQUEUE_H_ 29#define _SYS_SLEEPQUEUE_H_ 30 31/* 32 * Sleep queue interface. Sleep/wakeup, condition variables, and sx 33 * locks use a sleep queue for the queue of threads blocked on a sleep 34 * channel. 35 * 36 * A thread calls sleepq_lock() to lock the sleep queue chain associated 37 * with a given wait channel. A thread can then call call sleepq_add() to 38 * add themself onto a sleep queue and call one of the sleepq_wait() 39 * functions to actually go to sleep. If a thread needs to abort a sleep 40 * operation it should call sleepq_release() to unlock the associated sleep 41 * queue chain lock. If the thread also needs to remove itself from a queue 42 * it just enqueued itself on, it can use sleepq_remove() instead. 43 * 44 * If the thread only wishes to sleep for a limited amount of time, it can 45 * call sleepq_set_timeout() after sleepq_add() to setup a timeout. It 46 * should then use one of the sleepq_timedwait() functions to block. 47 * 48 * A thread is normally resumed from a sleep queue by either the 49 * sleepq_signal() or sleepq_broadcast() functions. Sleepq_signal() wakes 50 * the thread with the highest priority that is sleeping on the specified 51 * wait channel. Sleepq_broadcast() wakes all threads that are sleeping 52 * on the specified wait channel. A thread sleeping in an interruptible 53 * sleep can be interrupted by calling sleepq_abort(). A thread can also 54 * be removed from a specified sleep queue using the sleepq_remove() 55 * function. Note that the sleep queue chain must first be locked via 56 * sleepq_lock() before calling sleepq_abort(), sleepq_broadcast(), or 57 * sleepq_signal(). These routines each return a boolean that will be true 58 * if at least one swapped-out thread was resumed. In that case, the caller 59 * is responsible for waking up the swapper by calling kick_proc0() after 60 * releasing the sleep queue chain lock. 61 * 62 * Each thread allocates a sleep queue at thread creation via sleepq_alloc() 63 * and releases it at thread destruction via sleepq_free(). Note that 64 * a sleep queue is not tied to a specific thread and that the sleep queue 65 * released at thread destruction may not be the same sleep queue that the 66 * thread allocated when it was created. 67 * 68 * XXX: Some other parts of the kernel such as ithread sleeping may end up 69 * using this interface as well (death to TDI_IWAIT!) 70 */ 71 72struct lock_object; 73struct sleepqueue; 74struct thread; 75 76#ifdef _KERNEL 77 78#define SLEEPQ_TYPE 0x0ff /* Mask of sleep queue types. */ 79#define SLEEPQ_SLEEP 0x00 /* Used by sleep/wakeup. */ 80#define SLEEPQ_CONDVAR 0x01 /* Used for a cv. */ 81#define SLEEPQ_PAUSE 0x02 /* Used by pause. */ 82#define SLEEPQ_SX 0x03 /* Used by an sx lock. */ 83#define SLEEPQ_LK 0x04 /* Used by a lockmgr. */ 84#define SLEEPQ_INTERRUPTIBLE 0x100 /* Sleep is interruptible. */ 85#define SLEEPQ_UNFAIR 0x200 /* Unfair wakeup order. */ 86 87void init_sleepqueues(void); 88int sleepq_abort(struct thread *td, int intrval); 89void sleepq_add(void *wchan, struct lock_object *lock, const char *wmesg, 90 int flags, int queue); 91struct sleepqueue *sleepq_alloc(void); 92int sleepq_broadcast(void *wchan, int flags, int pri, int queue); 93void sleepq_chains_remove_matching(bool (*matches)(struct thread *)); 94void sleepq_free(struct sleepqueue *sq); 95void sleepq_lock(void *wchan); 96struct sleepqueue *sleepq_lookup(void *wchan); 97void sleepq_release(void *wchan); 98void sleepq_remove(struct thread *td, void *wchan); 99int sleepq_remove_matching(struct sleepqueue *sq, int queue, 100 bool (*matches)(struct thread *), int pri); 101int sleepq_signal(void *wchan, int flags, int pri, int queue); 102void sleepq_set_timeout_sbt(void *wchan, sbintime_t sbt, 103 sbintime_t pr, int flags); 104#define sleepq_set_timeout(wchan, timo) \ 105 sleepq_set_timeout_sbt((wchan), tick_sbt * (timo), 0, C_HARDCLOCK) 106u_int sleepq_sleepcnt(void *wchan, int queue); 107int sleepq_timedwait(void *wchan, int pri); 108int sleepq_timedwait_sig(void *wchan, int pri); 109int sleepq_type(void *wchan); 110void sleepq_wait(void *wchan, int pri); 111int sleepq_wait_sig(void *wchan, int pri); 112 113#ifdef STACK 114struct sbuf; 115int sleepq_sbuf_print_stacks(struct sbuf *sb, void *wchan, int queue, 116 int *count_stacks_printed); 117#endif 118 119#endif /* _KERNEL */ 120#endif /* !_SYS_SLEEPQUEUE_H_ */ 121