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>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
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.
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.
33 #ifndef _LINUX_WAIT_H_
34 #define _LINUX_WAIT_H_
36 #include <linux/compiler.h>
37 #include <linux/list.h>
38 #include <linux/spinlock.h>
40 #include <asm/atomic.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #define SKIP_SLEEP() (SCHEDULER_STOPPED() || kdb_active)
47 #define might_sleep() \
48 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "might_sleep()")
50 #define might_sleep_if(cond) do { \
51 if (cond) { might_sleep(); } \
55 struct wait_queue_head;
57 #define wait_queue_entry wait_queue
59 typedef struct wait_queue wait_queue_t;
60 typedef struct wait_queue_entry wait_queue_entry_t;
61 typedef struct wait_queue_head wait_queue_head_t;
63 typedef int wait_queue_func_t(wait_queue_t *, unsigned int, int, void *);
66 * Many API consumers directly reference these fields and those of
70 unsigned int flags; /* always 0 */
72 wait_queue_func_t *func;
74 struct list_head task_list; /* < v4.13 */
75 struct list_head entry; /* >= v4.13 */
79 struct wait_queue_head {
82 struct list_head task_list; /* < v4.13 */
83 struct list_head head; /* >= v4.13 */
88 * This function is referenced by at least one DRM driver, so it may not be
89 * renamed and furthermore must be the default wait queue callback.
91 extern wait_queue_func_t autoremove_wake_function;
92 extern wait_queue_func_t default_wake_function;
94 #define DEFINE_WAIT_FUNC(name, function) \
95 wait_queue_t name = { \
98 .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \
101 #define DEFINE_WAIT(name) \
102 DEFINE_WAIT_FUNC(name, autoremove_wake_function)
104 #define DECLARE_WAITQUEUE(name, task) \
105 wait_queue_t name = { \
107 .task_list = LINUX_LIST_HEAD_INIT(name.task_list) \
110 #define DECLARE_WAIT_QUEUE_HEAD(name) \
111 wait_queue_head_t name = { \
112 .task_list = LINUX_LIST_HEAD_INIT(name.task_list), \
114 MTX_SYSINIT(name, &(name).lock.m, spin_lock_name("wqhead"), MTX_DEF)
116 #define init_waitqueue_head(wqh) do { \
117 mtx_init(&(wqh)->lock.m, spin_lock_name("wqhead"), \
118 NULL, MTX_DEF | MTX_NEW | MTX_NOWITNESS); \
119 INIT_LIST_HEAD(&(wqh)->task_list); \
122 void linux_init_wait_entry(wait_queue_t *, int);
123 void linux_wake_up(wait_queue_head_t *, unsigned int, int, bool);
125 #define init_wait_entry(wq, flags) \
126 linux_init_wait_entry(wq, flags)
127 #define wake_up(wqh) \
128 linux_wake_up(wqh, TASK_NORMAL, 1, false)
129 #define wake_up_all(wqh) \
130 linux_wake_up(wqh, TASK_NORMAL, 0, false)
131 #define wake_up_locked(wqh) \
132 linux_wake_up(wqh, TASK_NORMAL, 1, true)
133 #define wake_up_all_locked(wqh) \
134 linux_wake_up(wqh, TASK_NORMAL, 0, true)
135 #define wake_up_interruptible(wqh) \
136 linux_wake_up(wqh, TASK_INTERRUPTIBLE, 1, false)
137 #define wake_up_interruptible_all(wqh) \
138 linux_wake_up(wqh, TASK_INTERRUPTIBLE, 0, false)
140 int linux_wait_event_common(wait_queue_head_t *, wait_queue_t *, int,
141 unsigned int, spinlock_t *);
144 * Returns -ERESTARTSYS for a signal, 0 if cond is false after timeout, 1 if
145 * cond is true after timeout, remaining jiffies (> 0) if cond is true before
148 #define __wait_event_common(wqh, cond, timeout, state, lock) ({ \
150 const int __timeout = ((int)(timeout)) < 1 ? 1 : (timeout); \
151 int __start = ticks; \
155 linux_prepare_to_wait(&(wqh), &__wq, state); \
158 __ret = linux_wait_event_common(&(wqh), &__wq, \
159 __timeout, state, lock); \
163 linux_finish_wait(&(wqh), &__wq); \
164 if (__timeout != MAX_SCHEDULE_TIMEOUT) { \
165 if (__ret == -EWOULDBLOCK) \
167 else if (__ret != -ERESTARTSYS) { \
168 __ret = __timeout + __start - ticks; \
169 /* range check return value */ \
172 else if (__ret > __timeout) \
179 #define wait_event(wqh, cond) do { \
180 (void) __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
181 TASK_UNINTERRUPTIBLE, NULL); \
184 #define wait_event_timeout(wqh, cond, timeout) ({ \
185 __wait_event_common(wqh, cond, timeout, TASK_UNINTERRUPTIBLE, \
189 #define wait_event_killable(wqh, cond) ({ \
190 __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
191 TASK_INTERRUPTIBLE, NULL); \
194 #define wait_event_interruptible(wqh, cond) ({ \
195 __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
196 TASK_INTERRUPTIBLE, NULL); \
199 #define wait_event_interruptible_timeout(wqh, cond, timeout) ({ \
200 __wait_event_common(wqh, cond, timeout, TASK_INTERRUPTIBLE, \
205 * Wait queue is already locked.
207 #define wait_event_interruptible_locked(wqh, cond) ({ \
210 spin_unlock(&(wqh).lock); \
211 __ret = __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
212 TASK_INTERRUPTIBLE, NULL); \
213 spin_lock(&(wqh).lock); \
218 * The passed spinlock is held when testing the condition.
220 #define wait_event_interruptible_lock_irq(wqh, cond, lock) ({ \
221 __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
222 TASK_INTERRUPTIBLE, &(lock)); \
226 * The passed spinlock is held when testing the condition.
228 #define wait_event_lock_irq(wqh, cond, lock) ({ \
229 __wait_event_common(wqh, cond, MAX_SCHEDULE_TIMEOUT, \
230 TASK_UNINTERRUPTIBLE, &(lock)); \
234 __add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
236 list_add(&wq->task_list, &wqh->task_list);
240 add_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
243 spin_lock(&wqh->lock);
244 __add_wait_queue(wqh, wq);
245 spin_unlock(&wqh->lock);
249 __add_wait_queue_tail(wait_queue_head_t *wqh, wait_queue_t *wq)
251 list_add_tail(&wq->task_list, &wqh->task_list);
255 __add_wait_queue_entry_tail(wait_queue_head_t *wqh, wait_queue_entry_t *wq)
257 list_add_tail(&wq->entry, &wqh->head);
261 __remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
263 list_del(&wq->task_list);
267 remove_wait_queue(wait_queue_head_t *wqh, wait_queue_t *wq)
270 spin_lock(&wqh->lock);
271 __remove_wait_queue(wqh, wq);
272 spin_unlock(&wqh->lock);
275 bool linux_waitqueue_active(wait_queue_head_t *);
277 #define waitqueue_active(wqh) linux_waitqueue_active(wqh)
279 void linux_prepare_to_wait(wait_queue_head_t *, wait_queue_t *, int);
280 void linux_finish_wait(wait_queue_head_t *, wait_queue_t *);
282 #define prepare_to_wait(wqh, wq, state) linux_prepare_to_wait(wqh, wq, state)
283 #define finish_wait(wqh, wq) linux_finish_wait(wqh, wq)
285 void linux_wake_up_bit(void *, int);
286 int linux_wait_on_bit_timeout(unsigned long *, int, unsigned int, int);
287 void linux_wake_up_atomic_t(atomic_t *);
288 int linux_wait_on_atomic_t(atomic_t *, unsigned int);
290 #define wake_up_bit(word, bit) linux_wake_up_bit(word, bit)
291 #define wait_on_bit(word, bit, state) \
292 linux_wait_on_bit_timeout(word, bit, state, MAX_SCHEDULE_TIMEOUT)
293 #define wait_on_bit_timeout(word, bit, state, timeout) \
294 linux_wait_on_bit_timeout(word, bit, state, timeout)
295 #define wake_up_atomic_t(a) linux_wake_up_atomic_t(a)
297 * All existing callers have a cb that just schedule()s. To avoid adding
298 * complexity, just emulate that internally. The prototype is different so that
299 * callers must be manually modified; a cb that does something other than call
300 * schedule() will require special treatment.
302 #define wait_on_atomic_t(a, state) linux_wait_on_atomic_t(a, state)
305 bool linux_wake_up_state(struct task_struct *, unsigned int);
307 #define wake_up_process(task) linux_wake_up_state(task, TASK_NORMAL)
308 #define wake_up_state(task, state) linux_wake_up_state(task, state)
310 #endif /* _LINUX_WAIT_H_ */