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 335426 2018-06-20 06:50:11Z 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 50335425Shselasky#define might_sleep_if(cond) do { \ 51335425Shselasky if (cond) { might_sleep(); } \ 52335425Shselasky} while (0) 53335425Shselasky 54328653Shselaskystruct wait_queue; 55328653Shselaskystruct wait_queue_head; 56219820Sjeff 57335426Shselasky#define wait_queue_entry wait_queue 58335426Shselasky 59328653Shselaskytypedef struct wait_queue wait_queue_t; 60335426Shselaskytypedef struct wait_queue_entry wait_queue_entry_t; 61328653Shselaskytypedef struct wait_queue_head wait_queue_head_t; 62219820Sjeff 63328653Shselaskytypedef int wait_queue_func_t(wait_queue_t *, unsigned int, int, void *); 64219820Sjeff 65328653Shselasky/* 66328653Shselasky * Many API consumers directly reference these fields and those of 67328653Shselasky * wait_queue_head. 68328653Shselasky */ 69328653Shselaskystruct wait_queue { 70328653Shselasky unsigned int flags; /* always 0 */ 71328653Shselasky void *private; 72328653Shselasky wait_queue_func_t *func; 73335424Shselasky union { 74335424Shselasky struct list_head task_list; /* < v4.13 */ 75335424Shselasky struct list_head entry; /* >= v4.13 */ 76335424Shselasky }; 77328653Shselasky}; 78219820Sjeff 79328653Shselaskystruct wait_queue_head { 80328653Shselasky spinlock_t lock; 81335424Shselasky union { 82335424Shselasky struct list_head task_list; /* < v4.13 */ 83335424Shselasky struct list_head head; /* >= v4.13 */ 84335424Shselasky }; 85328653Shselasky}; 86328653Shselasky 87328653Shselasky/* 88328653Shselasky * This function is referenced by at least one DRM driver, so it may not be 89328653Shselasky * renamed and furthermore must be the default wait queue callback. 90328653Shselasky */ 91328653Shselaskyextern wait_queue_func_t autoremove_wake_function; 92330861Shselaskyextern wait_queue_func_t default_wake_function; 93328653Shselasky 94330861Shselasky#define DEFINE_WAIT_FUNC(name, function) \ 95328653Shselasky wait_queue_t name = { \ 96328653Shselasky .private = current, \ 97330861Shselasky .func = function, \ 98328653Shselasky .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \ 99328653Shselasky } 100328653Shselasky 101330861Shselasky#define DEFINE_WAIT(name) \ 102330861Shselasky DEFINE_WAIT_FUNC(name, autoremove_wake_function) 103330861Shselasky 104328653Shselasky#define DECLARE_WAITQUEUE(name, task) \ 105328653Shselasky wait_queue_t name = { \ 106328653Shselasky .private = task, \ 107328653Shselasky .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \ 108328653Shselasky } 109328653Shselasky 110328653Shselasky#define DECLARE_WAIT_QUEUE_HEAD(name) \ 111328653Shselasky wait_queue_head_t name = { \ 112328653Shselasky .task_list = LINUX_LIST_HEAD_INIT(name.task_list), \ 113328653Shselasky }; \ 114328653Shselasky MTX_SYSINIT(name, &(name).lock.m, spin_lock_name("wqhead"), MTX_DEF) 115328653Shselasky 116328653Shselasky#define init_waitqueue_head(wqh) do { \ 117328653Shselasky mtx_init(&(wqh)->lock.m, spin_lock_name("wqhead"), \ 118328653Shselasky NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS); \ 119328653Shselasky INIT_LIST_HEAD(&(wqh)->task_list); \ 120219820Sjeff} while (0) 121219820Sjeff 122335423Shselaskyvoid linux_init_wait_entry(wait_queue_t *, int); 123328653Shselaskyvoid linux_wake_up(wait_queue_head_t *, unsigned int, int, bool); 124328653Shselasky 125335423Shselasky#define init_wait_entry(wq, flags) \ 126335423Shselasky linux_init_wait_entry(wq, flags) 127328653Shselasky#define wake_up(wqh) \ 128328653Shselasky linux_wake_up(wqh, TASK_NORMAL, 1, false) 129328653Shselasky#define wake_up_all(wqh) \ 130328653Shselasky linux_wake_up(wqh, TASK_NORMAL, 0, false) 131328653Shselasky#define wake_up_locked(wqh) \ 132328653Shselasky linux_wake_up(wqh, TASK_NORMAL, 1, true) 133328653Shselasky#define wake_up_all_locked(wqh) \ 134328653Shselasky linux_wake_up(wqh, TASK_NORMAL, 0, true) 135328653Shselasky#define wake_up_interruptible(wqh) \ 136328653Shselasky linux_wake_up(wqh, TASK_INTERRUPTIBLE, 1, false) 137328653Shselasky#define wake_up_interruptible_all(wqh) \ 138328653Shselasky linux_wake_up(wqh, TASK_INTERRUPTIBLE, 0, false) 139328653Shselasky 140328653Shselaskyint linux_wait_event_common(wait_queue_head_t *, wait_queue_t *, int, 141328653Shselasky unsigned int, spinlock_t *); 142328653Shselasky 143328653Shselasky/* 144328653Shselasky * Returns -ERESTARTSYS for a signal, 0 if cond is false after timeout, 1 if 145328653Shselasky * cond is true after timeout, remaining jiffies (> 0) if cond is true before 146328653Shselasky * timeout. 147328653Shselasky */ 148328653Shselasky#define __wait_event_common(wqh, cond, timeout, state, lock) ({ \ 149328653Shselasky DEFINE_WAIT(__wq); \ 150328653Shselasky const int __timeout = ((int)(timeout)) < 1 ? 1 : (timeout); \ 151328653Shselasky int __start = ticks; \ 152328653Shselasky int __ret = 0; \ 153328653Shselasky \ 154328653Shselasky for (;;) { \ 155328653Shselasky linux_prepare_to_wait(&(wqh), &__wq, state); \ 156328653Shselasky if (cond) \ 157328653Shselasky break; \ 158328653Shselasky __ret = linux_wait_event_common(&(wqh), &__wq, \ 159328653Shselasky __timeout, state, lock); \ 160328653Shselasky if (__ret != 0) \ 161328653Shselasky break; \ 162328653Shselasky } \ 163328653Shselasky linux_finish_wait(&(wqh), &__wq); \ 164328653Shselasky if (__timeout != MAX_SCHEDULE_TIMEOUT) { \ 165328653Shselasky if (__ret == -EWOULDBLOCK) \ 166328653Shselasky __ret = !!(cond); \ 167328653Shselasky else if (__ret != -ERESTARTSYS) { \ 168328653Shselasky __ret = __timeout + __start - ticks; \ 169328653Shselasky /* range check return value */ \ 170328653Shselasky if (__ret < 1) \ 171328653Shselasky __ret = 1; \ 172328653Shselasky else if (__ret > __timeout) \ 173328653Shselasky __ret = __timeout; \ 174328653Shselasky } \ 175328653Shselasky } \ 176328653Shselasky __ret; \ 177219820Sjeff}) 178219820Sjeff 179328653Shselasky#define wait_event(wqh, cond) do { \ 180328653Shselasky (void) __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 181328653Shselasky TASK_UNINTERRUPTIBLE, NULL); \ 182328653Shselasky} while (0) 183328653Shselasky 184328653Shselasky#define wait_event_timeout(wqh, cond, timeout) ({ \ 185328653Shselasky __wait_event_common(wqh, cond, timeout, TASK_UNINTERRUPTIBLE, \ 186328653Shselasky NULL); \ 187328653Shselasky}) 188328653Shselasky 189334760Shselasky#define wait_event_killable(wqh, cond) ({ \ 190334760Shselasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 191334760Shselasky TASK_INTERRUPTIBLE, NULL); \ 192334760Shselasky}) 193334760Shselasky 194328653Shselasky#define wait_event_interruptible(wqh, cond) ({ \ 195328653Shselasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 196328653Shselasky TASK_INTERRUPTIBLE, NULL); \ 197328653Shselasky}) 198328653Shselasky 199328653Shselasky#define wait_event_interruptible_timeout(wqh, cond, timeout) ({ \ 200328653Shselasky __wait_event_common(wqh, cond, timeout, TASK_INTERRUPTIBLE, \ 201328653Shselasky NULL); \ 202328653Shselasky}) 203328653Shselasky 204328653Shselasky/* 205328653Shselasky * Wait queue is already locked. 206328653Shselasky */ 207328653Shselasky#define wait_event_interruptible_locked(wqh, cond) ({ \ 208328653Shselasky int __ret; \ 209297459Snp \ 210328653Shselasky spin_unlock(&(wqh).lock); \ 211328653Shselasky __ret = __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 212328653Shselasky TASK_INTERRUPTIBLE, NULL); \ 213328653Shselasky spin_lock(&(wqh).lock); \ 214297459Snp __ret; \ 215297459Snp}) 216297459Snp 217328653Shselasky/* 218330862Shselasky * The passed spinlock is held when testing the condition. 219328653Shselasky */ 220328653Shselasky#define wait_event_interruptible_lock_irq(wqh, cond, lock) ({ \ 221328653Shselasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 222328653Shselasky TASK_INTERRUPTIBLE, &(lock)); \ 223328653Shselasky}) 224297459Snp 225330862Shselasky/* 226330862Shselasky * The passed spinlock is held when testing the condition. 227330862Shselasky */ 228330862Shselasky#define wait_event_lock_irq(wqh, cond, lock) ({ \ 229330862Shselasky __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \ 230330862Shselasky TASK_UNINTERRUPTIBLE, &(lock)); \ 231330862Shselasky}) 232330862Shselasky 233328653Shselaskystatic inline void 234328653Shselasky__add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 235289564Shselasky{ 236328653Shselasky list_add(&wq->task_list, &wqh->task_list); 237289564Shselasky} 238219820Sjeff 239328653Shselaskystatic inline void 240328653Shselaskyadd_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 241328653Shselasky{ 242289564Shselasky 243328653Shselasky spin_lock(&wqh->lock); 244328653Shselasky __add_wait_queue(wqh, wq); 245328653Shselasky spin_unlock(&wqh->lock); 246328653Shselasky} 247328653Shselasky 248289564Shselaskystatic inline void 249328653Shselasky__add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq) 250289564Shselasky{ 251328653Shselasky list_add_tail(&wq->task_list, &wqh->task_list); 252289564Shselasky} 253289564Shselasky 254289564Shselaskystatic inline void 255335426Shselasky__add_wait_queue_entry_tail(wait_queue_head_t *wqh, wait_queue_entry_t *wq) 256335426Shselasky{ 257335426Shselasky list_add_tail(&wq->entry, &wqh->head); 258335426Shselasky} 259335426Shselasky 260335426Shselaskystatic inline void 261328653Shselasky__remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 262289564Shselasky{ 263328653Shselasky list_del(&wq->task_list); 264289564Shselasky} 265289564Shselasky 266328653Shselaskystatic inline void 267328653Shselaskyremove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq) 268328653Shselasky{ 269328653Shselasky 270328653Shselasky spin_lock(&wqh->lock); 271328653Shselasky __remove_wait_queue(wqh, wq); 272328653Shselasky spin_unlock(&wqh->lock); 273328653Shselasky} 274328653Shselasky 275328653Shselaskybool linux_waitqueue_active(wait_queue_head_t *); 276328653Shselasky 277328653Shselasky#define waitqueue_active(wqh) linux_waitqueue_active(wqh) 278328653Shselasky 279328653Shselaskyvoid linux_prepare_to_wait(wait_queue_head_t *, wait_queue_t *, int); 280328653Shselaskyvoid linux_finish_wait(wait_queue_head_t *, wait_queue_t *); 281328653Shselasky 282328653Shselasky#define prepare_to_wait(wqh, wq, state) linux_prepare_to_wait(wqh, wq, state) 283328653Shselasky#define finish_wait(wqh, wq) linux_finish_wait(wqh, wq) 284328653Shselasky 285328653Shselaskyvoid linux_wake_up_bit(void *, int); 286328653Shselaskyint linux_wait_on_bit_timeout(unsigned long *, int, unsigned int, int); 287328653Shselaskyvoid linux_wake_up_atomic_t(atomic_t *); 288328653Shselaskyint linux_wait_on_atomic_t(atomic_t *, unsigned int); 289328653Shselasky 290328653Shselasky#define wake_up_bit(word, bit) linux_wake_up_bit(word, bit) 291330847Shselasky#define wait_on_bit(word, bit, state) \ 292330847Shselasky linux_wait_on_bit_timeout(word, bit, state, MAX_SCHEDULE_TIMEOUT) 293328653Shselasky#define wait_on_bit_timeout(word, bit, state, timeout) \ 294328653Shselasky linux_wait_on_bit_timeout(word, bit, state, timeout) 295328653Shselasky#define wake_up_atomic_t(a) linux_wake_up_atomic_t(a) 296328653Shselasky/* 297328653Shselasky * All existing callers have a cb that just schedule()s. To avoid adding 298328653Shselasky * complexity, just emulate that internally. The prototype is different so that 299328653Shselasky * callers must be manually modified; a cb that does something other than call 300328653Shselasky * schedule() will require special treatment. 301328653Shselasky */ 302328653Shselasky#define wait_on_atomic_t(a, state) linux_wait_on_atomic_t(a, state) 303328653Shselasky 304328653Shselaskystruct task_struct; 305328653Shselaskybool linux_wake_up_state(struct task_struct *, unsigned int); 306328653Shselasky 307328653Shselasky#define wake_up_process(task) linux_wake_up_state(task, TASK_NORMAL) 308328653Shselasky#define wake_up_state(task, state) linux_wake_up_state(task, state) 309328653Shselasky 310328653Shselasky#endif /* _LINUX_WAIT_H_ */ 311