wait.h revision 335423
1/*- 2 * Copyright (c) 2010 Isilon Systems, Inc. 3 * Copyright (c) 2010 iX Systems, Inc. 4 * Copyright (c) 2010 Panasas, Inc. 5 * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 6 * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice unmodified, this list of conditions, and the following 14 * disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/wait.h 335423 2018-06-20 06:47:49Z hselasky $ 31 */ 32 33#ifndef _LINUX_WAIT_H_ 34#define _LINUX_WAIT_H_ 35 36#include <linux/compiler.h> 37#include <linux/list.h> 38#include <linux/spinlock.h> 39 40#include <asm/atomic.h> 41 42#include <sys/param.h> 43#include <sys/systm.h> 44 45#define SKIP_SLEEP() (SCHEDULER_STOPPED() || kdb_active) 46 47#define might_sleep() \ 48 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "might_sleep()") 49 50struct wait_queue; 51struct wait_queue_head; 52 53typedef struct wait_queue wait_queue_t; 54typedef struct wait_queue_head wait_queue_head_t; 55 56typedef int wait_queue_func_t(wait_queue_t *, unsigned int, int, void *); 57 58/* 59 * Many API consumers directly reference these fields and those of 60 * wait_queue_head. 61 */ 62struct wait_queue { 63 unsigned int flags; /* always 0 */ 64 void *private; 65 wait_queue_func_t *func; 66 struct list_head task_list; 67}; 68 69struct wait_queue_head { 70 spinlock_t lock; 71 struct list_head task_list; 72}; 73 74/* 75 * This function is referenced by at least one DRM driver, so it may not be 76 * renamed and furthermore must be the default wait queue callback. 77 */ 78extern wait_queue_func_t autoremove_wake_function; 79extern wait_queue_func_t default_wake_function; 80 81#define DEFINE_WAIT_FUNC(name, function) \ 82 wait_queue_t name = { \ 83 .private = current, \ 84 .func = function, \ 85 .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \ 86 } 87 88#define DEFINE_WAIT(name) \ 89 DEFINE_WAIT_FUNC(name, autoremove_wake_function) 90 91#define DECLARE_WAITQUEUE(name, task) \ 92 wait_queue_t name = { \ 93 .private = task, \ 94 .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \ 95 } 96 97#define DECLARE_WAIT_QUEUE_HEAD(name) \ 98 wait_queue_head_t name = { \ 99 .task_list = LINUX_LIST_HEAD_INIT(name.task_list), \ 100 }; \ 101 MTX_SYSINIT(name, &(name).lock.m, spin_lock_name("wqhead"), MTX_DEF) 102 103#define init_waitqueue_head(wqh) do { \ 104 mtx_init(&(wqh)->lock.m, spin_lock_name("wqhead"), \ 105 NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS); \ 106 INIT_LIST_HEAD(&(wqh)->task_list); \ 107} while (0) 108 109void linux_init_wait_entry(wait_queue_t *, int); 110void linux_wake_up(wait_queue_head_t *, unsigned int, int, bool); 111 112#define init_wait_entry(wq, flags) \ 113 linux_init_wait_entry(wq, flags) 114#define wake_up(wqh) \ 115 linux_wake_up(wqh, TASK_NORMAL, 1, false) 116#define wake_up_all(wqh) \ 117 linux_wake_up(wqh, TASK_NORMAL, 0, false) 118#define wake_up_locked(wqh) \ 119 linux_wake_up(wqh, TASK_NORMAL, 1, true) 120#define wake_up_all_locked(wqh) \ 121 linux_wake_up(wqh, TASK_NORMAL, 0, true) 122#define wake_up_interruptible(wqh) \ 123 linux_wake_up(wqh, TASK_INTERRUPTIBLE, 1, false) 124#define wake_up_interruptible_all(wqh) \ 125 linux_wake_up(wqh, TASK_INTERRUPTIBLE, 0, false) 126 127int linux_wait_event_common(wait_queue_head_t *, wait_queue_t *, int, 128 unsigned int, spinlock_t *); 129 130/* 131 * Returns -ERESTARTSYS for a signal, 0 if cond is false after timeout, 1 if 132 * cond is true after timeout, remaining jiffies (> 0) if cond is true before 133 * timeout. 134 */ 135#define __wait_event_common(wqh, cond, timeout, state, lock) ({ \ 136 DEFINE_WAIT(__wq); \ 137 const int __timeout = ((int)(timeout)) < 1 ? 1 : (timeout); \ 138 int __start = ticks; \ 139 int __ret = 0; \ 140 \ 141 for (;;) { \ 142 linux_prepare_to_wait(&(wqh), &__wq, state); \ 143 if (cond) \ 144 break; \ 145 __ret = linux_wait_event_common(&(wqh), &__wq, \ 146 __timeout, state, lock); \ 147 if (__ret != 0) \ 148 break; \ 149 } \ 150 linux_finish_wait(&(wqh), &__wq); \ 151 if (__timeout != MAX_SCHEDULE_TIMEOUT) { \ 152 if (__ret == -EWOULDBLOCK) \ 153 __ret = !!(cond); \ 154 else if (__ret != -ERESTARTSYS) { \ 155 __ret = __timeout + __start - ticks; \ 156 /* range check return value */ \ 157 if (__ret < 1) \ 158 __ret = 1; \ 159 else if (__ret > __timeout) \ 160 __ret = __timeout; \ 161 } \ 162 } \ 163 __ret; \ 164}) 165 166#define wait_event(wqh, cond) do { \ 167 (void) __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 168 TASK_UNINTERRUPTIBLE, NULL); \ 169} while (0) 170 171#define wait_event_timeout(wqh, cond, timeout) ({ \ 172 __wait_event_common(wqh, cond, timeout, TASK_UNINTERRUPTIBLE, \ 173 NULL); \ 174}) 175 176#define wait_event_killable(wqh, cond) ({ \ 177 __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 178 TASK_INTERRUPTIBLE, NULL); \ 179}) 180 181#define wait_event_interruptible(wqh, cond) ({ \ 182 __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 183 TASK_INTERRUPTIBLE, NULL); \ 184}) 185 186#define wait_event_interruptible_timeout(wqh, cond, timeout) ({ \ 187 __wait_event_common(wqh, cond, timeout, TASK_INTERRUPTIBLE, \ 188 NULL); \ 189}) 190 191/* 192 * Wait queue is already locked. 193 */ 194#define wait_event_interruptible_locked(wqh, cond) ({ \ 195 int __ret; \ 196 \ 197 spin_unlock(&(wqh).lock); \ 198 __ret = __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 199 TASK_INTERRUPTIBLE, NULL); \ 200 spin_lock(&(wqh).lock); \ 201 __ret; \ 202}) 203 204/* 205 * The passed spinlock is held when testing the condition. 206 */ 207#define wait_event_interruptible_lock_irq(wqh, cond, lock) ({ \ 208 __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 209 TASK_INTERRUPTIBLE, &(lock)); \ 210}) 211 212/* 213 * The passed spinlock is held when testing the condition. 214 */ 215#define wait_event_lock_irq(wqh, cond, lock) ({ \ 216 __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 217 TASK_UNINTERRUPTIBLE, &(lock)); \ 218}) 219 220static inline void 221__add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 222{ 223 list_add(&wq->task_list, &wqh->task_list); 224} 225 226static inline void 227add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 228{ 229 230 spin_lock(&wqh->lock); 231 __add_wait_queue(wqh, wq); 232 spin_unlock(&wqh->lock); 233} 234 235static inline void 236__add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq) 237{ 238 list_add_tail(&wq->task_list, &wqh->task_list); 239} 240 241static inline void 242__remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 243{ 244 list_del(&wq->task_list); 245} 246 247static inline void 248remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 249{ 250 251 spin_lock(&wqh->lock); 252 __remove_wait_queue(wqh, wq); 253 spin_unlock(&wqh->lock); 254} 255 256bool linux_waitqueue_active(wait_queue_head_t *); 257 258#define waitqueue_active(wqh) linux_waitqueue_active(wqh) 259 260void linux_prepare_to_wait(wait_queue_head_t *, wait_queue_t *, int); 261void linux_finish_wait(wait_queue_head_t *, wait_queue_t *); 262 263#define prepare_to_wait(wqh, wq, state) linux_prepare_to_wait(wqh, wq, state) 264#define finish_wait(wqh, wq) linux_finish_wait(wqh, wq) 265 266void linux_wake_up_bit(void *, int); 267int linux_wait_on_bit_timeout(unsigned long *, int, unsigned int, int); 268void linux_wake_up_atomic_t(atomic_t *); 269int linux_wait_on_atomic_t(atomic_t *, unsigned int); 270 271#define wake_up_bit(word, bit) linux_wake_up_bit(word, bit) 272#define wait_on_bit(word, bit, state) \ 273 linux_wait_on_bit_timeout(word, bit, state, MAX_SCHEDULE_TIMEOUT) 274#define wait_on_bit_timeout(word, bit, state, timeout) \ 275 linux_wait_on_bit_timeout(word, bit, state, timeout) 276#define wake_up_atomic_t(a) linux_wake_up_atomic_t(a) 277/* 278 * All existing callers have a cb that just schedule()s. To avoid adding 279 * complexity, just emulate that internally. The prototype is different so that 280 * callers must be manually modified; a cb that does something other than call 281 * schedule() will require special treatment. 282 */ 283#define wait_on_atomic_t(a, state) linux_wait_on_atomic_t(a, state) 284 285struct task_struct; 286bool linux_wake_up_state(struct task_struct *, unsigned int); 287 288#define wake_up_process(task) linux_wake_up_state(task, TASK_NORMAL) 289#define wake_up_state(task, state) linux_wake_up_state(task, state) 290 291#endif /* _LINUX_WAIT_H_ */ 292