wait.h revision 335423
1219820Sjeff/*- 2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc. 3219820Sjeff * Copyright (c) 2010 iX Systems, Inc. 4219820Sjeff * Copyright (c) 2010 Panasas, Inc. 5270710Shselasky * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd. 6328653Shselasky * Copyright (c) 2017 Mark Johnston <markj@FreeBSD.org> 7219820Sjeff * All rights reserved. 8219820Sjeff * 9219820Sjeff * Redistribution and use in source and binary forms, with or without 10219820Sjeff * modification, are permitted provided that the following conditions 11219820Sjeff * are met: 12219820Sjeff * 1. Redistributions of source code must retain the above copyright 13219820Sjeff * notice unmodified, this list of conditions, and the following 14219820Sjeff * disclaimer. 15219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright 16219820Sjeff * notice, this list of conditions and the following disclaimer in the 17219820Sjeff * documentation and/or other materials provided with the distribution. 18219820Sjeff * 19219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29289644Shselasky * 30289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/wait.h 335423 2018-06-20 06:47:49Z hselasky $ 31219820Sjeff */ 32328653Shselasky 33328653Shselasky#ifndef _LINUX_WAIT_H_ 34219820Sjeff#define _LINUX_WAIT_H_ 35219820Sjeff 36300671Shselasky#include <linux/compiler.h> 37328653Shselasky#include <linux/list.h> 38219820Sjeff#include <linux/spinlock.h> 39219820Sjeff 40328653Shselasky#include <asm/atomic.h> 41328653Shselasky 42219820Sjeff#include <sys/param.h> 43219820Sjeff#include <sys/systm.h> 44219820Sjeff 45328653Shselasky#define SKIP_SLEEP() (SCHEDULER_STOPPED() || kdb_active) 46289564Shselasky 47328653Shselasky#define might_sleep() \ 48328653Shselasky WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "might_sleep()") 49219820Sjeff 50328653Shselaskystruct wait_queue; 51328653Shselaskystruct wait_queue_head; 52219820Sjeff 53328653Shselaskytypedef struct wait_queue wait_queue_t; 54328653Shselaskytypedef struct wait_queue_head wait_queue_head_t; 55219820Sjeff 56328653Shselaskytypedef int wait_queue_func_t(wait_queue_t *, unsigned int, int, void *); 57219820Sjeff 58328653Shselasky/* 59328653Shselasky * Many API consumers directly reference these fields and those of 60328653Shselasky * wait_queue_head. 61328653Shselasky */ 62328653Shselaskystruct wait_queue { 63328653Shselasky unsigned int flags; /* always 0 */ 64328653Shselasky void *private; 65328653Shselasky wait_queue_func_t *func; 66328653Shselasky struct list_head task_list; 67328653Shselasky}; 68219820Sjeff 69328653Shselaskystruct wait_queue_head { 70328653Shselasky spinlock_t lock; 71328653Shselasky struct list_head task_list; 72328653Shselasky}; 73328653Shselasky 74328653Shselasky/* 75328653Shselasky * This function is referenced by at least one DRM driver, so it may not be 76328653Shselasky * renamed and furthermore must be the default wait queue callback. 77328653Shselasky */ 78328653Shselaskyextern wait_queue_func_t autoremove_wake_function; 79330861Shselaskyextern wait_queue_func_t default_wake_function; 80328653Shselasky 81330861Shselasky#define DEFINE_WAIT_FUNC(name, function) \ 82328653Shselasky wait_queue_t name = { \ 83328653Shselasky .private = current, \ 84330861Shselasky .func = function, \ 85328653Shselasky .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \ 86328653Shselasky } 87328653Shselasky 88330861Shselasky#define DEFINE_WAIT(name) \ 89330861Shselasky DEFINE_WAIT_FUNC(name, autoremove_wake_function) 90330861Shselasky 91328653Shselasky#define DECLARE_WAITQUEUE(name, task) \ 92328653Shselasky wait_queue_t name = { \ 93328653Shselasky .private = task, \ 94328653Shselasky .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \ 95328653Shselasky } 96328653Shselasky 97328653Shselasky#define DECLARE_WAIT_QUEUE_HEAD(name) \ 98328653Shselasky wait_queue_head_t name = { \ 99328653Shselasky .task_list = LINUX_LIST_HEAD_INIT(name.task_list), \ 100328653Shselasky }; \ 101328653Shselasky MTX_SYSINIT(name, &(name).lock.m, spin_lock_name("wqhead"), MTX_DEF) 102328653Shselasky 103328653Shselasky#define init_waitqueue_head(wqh) do { \ 104328653Shselasky mtx_init(&(wqh)->lock.m, spin_lock_name("wqhead"), \ 105328653Shselasky NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS); \ 106328653Shselasky INIT_LIST_HEAD(&(wqh)->task_list); \ 107219820Sjeff} while (0) 108219820Sjeff 109335423Shselaskyvoid linux_init_wait_entry(wait_queue_t *, int); 110328653Shselaskyvoid linux_wake_up(wait_queue_head_t *, unsigned int, int, bool); 111328653Shselasky 112335423Shselasky#define init_wait_entry(wq, flags) \ 113335423Shselasky linux_init_wait_entry(wq, flags) 114328653Shselasky#define wake_up(wqh) \ 115328653Shselasky linux_wake_up(wqh, TASK_NORMAL, 1, false) 116328653Shselasky#define wake_up_all(wqh) \ 117328653Shselasky linux_wake_up(wqh, TASK_NORMAL, 0, false) 118328653Shselasky#define wake_up_locked(wqh) \ 119328653Shselasky linux_wake_up(wqh, TASK_NORMAL, 1, true) 120328653Shselasky#define wake_up_all_locked(wqh) \ 121328653Shselasky linux_wake_up(wqh, TASK_NORMAL, 0, true) 122328653Shselasky#define wake_up_interruptible(wqh) \ 123328653Shselasky linux_wake_up(wqh, TASK_INTERRUPTIBLE, 1, false) 124328653Shselasky#define wake_up_interruptible_all(wqh) \ 125328653Shselasky linux_wake_up(wqh, TASK_INTERRUPTIBLE, 0, false) 126328653Shselasky 127328653Shselaskyint linux_wait_event_common(wait_queue_head_t *, wait_queue_t *, int, 128328653Shselasky unsigned int, spinlock_t *); 129328653Shselasky 130328653Shselasky/* 131328653Shselasky * Returns -ERESTARTSYS for a signal, 0 if cond is false after timeout, 1 if 132328653Shselasky * cond is true after timeout, remaining jiffies (> 0) if cond is true before 133328653Shselasky * timeout. 134328653Shselasky */ 135328653Shselasky#define __wait_event_common(wqh, cond, timeout, state, lock) ({ \ 136328653Shselasky DEFINE_WAIT(__wq); \ 137328653Shselasky const int __timeout = ((int)(timeout)) < 1 ? 1 : (timeout); \ 138328653Shselasky int __start = ticks; \ 139328653Shselasky int __ret = 0; \ 140328653Shselasky \ 141328653Shselasky for (;;) { \ 142328653Shselasky linux_prepare_to_wait(&(wqh), &__wq, state); \ 143328653Shselasky if (cond) \ 144328653Shselasky break; \ 145328653Shselasky __ret = linux_wait_event_common(&(wqh), &__wq, \ 146328653Shselasky __timeout, state, lock); \ 147328653Shselasky if (__ret != 0) \ 148328653Shselasky break; \ 149328653Shselasky } \ 150328653Shselasky linux_finish_wait(&(wqh), &__wq); \ 151328653Shselasky if (__timeout != MAX_SCHEDULE_TIMEOUT) { \ 152328653Shselasky if (__ret == -EWOULDBLOCK) \ 153328653Shselasky __ret = !!(cond); \ 154328653Shselasky else if (__ret != -ERESTARTSYS) { \ 155328653Shselasky __ret = __timeout + __start - ticks; \ 156328653Shselasky /* range check return value */ \ 157328653Shselasky if (__ret < 1) \ 158328653Shselasky __ret = 1; \ 159328653Shselasky else if (__ret > __timeout) \ 160328653Shselasky __ret = __timeout; \ 161328653Shselasky } \ 162328653Shselasky } \ 163328653Shselasky __ret; \ 164219820Sjeff}) 165219820Sjeff 166328653Shselasky#define wait_event(wqh, cond) do { \ 167328653Shselasky (void) __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 168328653Shselasky TASK_UNINTERRUPTIBLE, NULL); \ 169328653Shselasky} while (0) 170328653Shselasky 171328653Shselasky#define wait_event_timeout(wqh, cond, timeout) ({ \ 172328653Shselasky __wait_event_common(wqh, cond, timeout, TASK_UNINTERRUPTIBLE, \ 173328653Shselasky NULL); \ 174328653Shselasky}) 175328653Shselasky 176334760Shselasky#define wait_event_killable(wqh, cond) ({ \ 177334760Shselasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 178334760Shselasky TASK_INTERRUPTIBLE, NULL); \ 179334760Shselasky}) 180334760Shselasky 181328653Shselasky#define wait_event_interruptible(wqh, cond) ({ \ 182328653Shselasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 183328653Shselasky TASK_INTERRUPTIBLE, NULL); \ 184328653Shselasky}) 185328653Shselasky 186328653Shselasky#define wait_event_interruptible_timeout(wqh, cond, timeout) ({ \ 187328653Shselasky __wait_event_common(wqh, cond, timeout, TASK_INTERRUPTIBLE, \ 188328653Shselasky NULL); \ 189328653Shselasky}) 190328653Shselasky 191328653Shselasky/* 192328653Shselasky * Wait queue is already locked. 193328653Shselasky */ 194328653Shselasky#define wait_event_interruptible_locked(wqh, cond) ({ \ 195328653Shselasky int __ret; \ 196297459Snp \ 197328653Shselasky spin_unlock(&(wqh).lock); \ 198328653Shselasky __ret = __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 199328653Shselasky TASK_INTERRUPTIBLE, NULL); \ 200328653Shselasky spin_lock(&(wqh).lock); \ 201297459Snp __ret; \ 202297459Snp}) 203297459Snp 204328653Shselasky/* 205330862Shselasky * The passed spinlock is held when testing the condition. 206328653Shselasky */ 207328653Shselasky#define wait_event_interruptible_lock_irq(wqh, cond, lock) ({ \ 208328653Shselasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 209328653Shselasky TASK_INTERRUPTIBLE, &(lock)); \ 210328653Shselasky}) 211297459Snp 212330862Shselasky/* 213330862Shselasky * The passed spinlock is held when testing the condition. 214330862Shselasky */ 215330862Shselasky#define wait_event_lock_irq(wqh, cond, lock) ({ \ 216330862Shselasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 217330862Shselasky TASK_UNINTERRUPTIBLE, &(lock)); \ 218330862Shselasky}) 219330862Shselasky 220328653Shselaskystatic inline void 221328653Shselasky__add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 222289564Shselasky{ 223328653Shselasky list_add(&wq->task_list, &wqh->task_list); 224289564Shselasky} 225219820Sjeff 226328653Shselaskystatic inline void 227328653Shselaskyadd_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 228328653Shselasky{ 229289564Shselasky 230328653Shselasky spin_lock(&wqh->lock); 231328653Shselasky __add_wait_queue(wqh, wq); 232328653Shselasky spin_unlock(&wqh->lock); 233328653Shselasky} 234328653Shselasky 235289564Shselaskystatic inline void 236328653Shselasky__add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq) 237289564Shselasky{ 238328653Shselasky list_add_tail(&wq->task_list, &wqh->task_list); 239289564Shselasky} 240289564Shselasky 241289564Shselaskystatic inline void 242328653Shselasky__remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 243289564Shselasky{ 244328653Shselasky list_del(&wq->task_list); 245289564Shselasky} 246289564Shselasky 247328653Shselaskystatic inline void 248328653Shselaskyremove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 249328653Shselasky{ 250328653Shselasky 251328653Shselasky spin_lock(&wqh->lock); 252328653Shselasky __remove_wait_queue(wqh, wq); 253328653Shselasky spin_unlock(&wqh->lock); 254328653Shselasky} 255328653Shselasky 256328653Shselaskybool linux_waitqueue_active(wait_queue_head_t *); 257328653Shselasky 258328653Shselasky#define waitqueue_active(wqh) linux_waitqueue_active(wqh) 259328653Shselasky 260328653Shselaskyvoid linux_prepare_to_wait(wait_queue_head_t *, wait_queue_t *, int); 261328653Shselaskyvoid linux_finish_wait(wait_queue_head_t *, wait_queue_t *); 262328653Shselasky 263328653Shselasky#define prepare_to_wait(wqh, wq, state) linux_prepare_to_wait(wqh, wq, state) 264328653Shselasky#define finish_wait(wqh, wq) linux_finish_wait(wqh, wq) 265328653Shselasky 266328653Shselaskyvoid linux_wake_up_bit(void *, int); 267328653Shselaskyint linux_wait_on_bit_timeout(unsigned long *, int, unsigned int, int); 268328653Shselaskyvoid linux_wake_up_atomic_t(atomic_t *); 269328653Shselaskyint linux_wait_on_atomic_t(atomic_t *, unsigned int); 270328653Shselasky 271328653Shselasky#define wake_up_bit(word, bit) linux_wake_up_bit(word, bit) 272330847Shselasky#define wait_on_bit(word, bit, state) \ 273330847Shselasky linux_wait_on_bit_timeout(word, bit, state, MAX_SCHEDULE_TIMEOUT) 274328653Shselasky#define wait_on_bit_timeout(word, bit, state, timeout) \ 275328653Shselasky linux_wait_on_bit_timeout(word, bit, state, timeout) 276328653Shselasky#define wake_up_atomic_t(a) linux_wake_up_atomic_t(a) 277328653Shselasky/* 278328653Shselasky * All existing callers have a cb that just schedule()s. To avoid adding 279328653Shselasky * complexity, just emulate that internally. The prototype is different so that 280328653Shselasky * callers must be manually modified; a cb that does something other than call 281328653Shselasky * schedule() will require special treatment. 282328653Shselasky */ 283328653Shselasky#define wait_on_atomic_t(a, state) linux_wait_on_atomic_t(a, state) 284328653Shselasky 285328653Shselaskystruct task_struct; 286328653Shselaskybool linux_wake_up_state(struct task_struct *, unsigned int); 287328653Shselasky 288328653Shselasky#define wake_up_process(task) linux_wake_up_state(task, TASK_NORMAL) 289328653Shselasky#define wake_up_state(task, state) linux_wake_up_state(task, state) 290328653Shselasky 291328653Shselasky#endif /* _LINUX_WAIT_H_ */ 292