blob: aacb1282d19a38d7b633bf7aedcc40fa258584f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_WAIT_H
2#define _LINUX_WAIT_H
Ingo Molnarfb869b62013-10-04 10:24:49 +02003/*
4 * Linux wait queue related types and methods
5 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/list.h>
7#include <linux/stddef.h>
8#include <linux/spinlock.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +01009
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <asm/current.h>
David Howells607ca462012-10-13 10:46:48 +010011#include <uapi/linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13typedef struct __wait_queue wait_queue_t;
Peter Zijlstra7d478722009-09-14 19:55:44 +020014typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
15int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Peter Zijlstra61ada522014-09-24 10:18:47 +020017/* __wait_queue::flags */
18#define WQ_FLAG_EXCLUSIVE 0x01
19#define WQ_FLAG_WOKEN 0x02
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021struct __wait_queue {
Ingo Molnarfb869b62013-10-04 10:24:49 +020022 unsigned int flags;
Ingo Molnarfb869b62013-10-04 10:24:49 +020023 void *private;
24 wait_queue_func_t func;
25 struct list_head task_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070026};
27
28struct wait_bit_key {
Ingo Molnarfb869b62013-10-04 10:24:49 +020029 void *flags;
30 int bit_nr;
31#define WAIT_ATOMIC_T_BIT_NR -1
NeilBrowncbbce822014-09-25 13:55:19 +100032 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
35struct wait_bit_queue {
Ingo Molnarfb869b62013-10-04 10:24:49 +020036 struct wait_bit_key key;
37 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40struct __wait_queue_head {
Ingo Molnarfb869b62013-10-04 10:24:49 +020041 spinlock_t lock;
42 struct list_head task_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44typedef struct __wait_queue_head wait_queue_head_t;
45
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080046struct task_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/*
49 * Macros for declaration and initialisaton of the datatypes
50 */
51
52#define __WAITQUEUE_INITIALIZER(name, tsk) { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070053 .private = tsk, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .func = default_wake_function, \
55 .task_list = { NULL, NULL } }
56
57#define DECLARE_WAITQUEUE(name, tsk) \
58 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
59
60#define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
Ingo Molnare4d91912006-07-03 00:24:34 -070061 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .task_list = { &(name).task_list, &(name).task_list } }
63
64#define DECLARE_WAIT_QUEUE_HEAD(name) \
65 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
66
67#define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
68 { .flags = word, .bit_nr = bit, }
69
David Howellscb655372013-05-10 19:50:26 +010070#define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
71 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
72
Peter Zijlstraf07fdec2011-12-13 13:20:54 +010073extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
Peter Zijlstra2fc39112009-08-10 12:33:05 +010074
75#define init_waitqueue_head(q) \
76 do { \
77 static struct lock_class_key __key; \
78 \
Peter Zijlstraf07fdec2011-12-13 13:20:54 +010079 __init_waitqueue_head((q), #q, &__key); \
Peter Zijlstra2fc39112009-08-10 12:33:05 +010080 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080082#ifdef CONFIG_LOCKDEP
83# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
84 ({ init_waitqueue_head(&name); name; })
85# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
86 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
87#else
88# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
89#endif
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
92{
Ingo Molnarfb869b62013-10-04 10:24:49 +020093 q->flags = 0;
94 q->private = p;
95 q->func = default_wake_function;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Ingo Molnarfb869b62013-10-04 10:24:49 +020098static inline void
99init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Ingo Molnarfb869b62013-10-04 10:24:49 +0200101 q->flags = 0;
102 q->private = NULL;
103 q->func = func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Peter Zijlstra69e51e922015-10-23 14:32:34 +0200106/**
107 * waitqueue_active -- locklessly test for waiters on the queue
108 * @q: the waitqueue to test for waiters
109 *
110 * returns true if the wait list is not empty
111 *
112 * NOTE: this function is lockless and requires care, incorrect usage _will_
113 * lead to sporadic and non-obvious failure.
114 *
115 * Use either while holding wait_queue_head_t::lock or when used for wakeups
116 * with an extra smp_mb() like:
117 *
118 * CPU0 - waker CPU1 - waiter
119 *
120 * for (;;) {
121 * @cond = true; prepare_to_wait(&wq, &wait, state);
122 * smp_mb(); // smp_mb() from set_current_state()
123 * if (waitqueue_active(wq)) if (@cond)
124 * wake_up(wq); break;
125 * schedule();
126 * }
127 * finish_wait(&wq, &wait);
128 *
129 * Because without the explicit smp_mb() it's possible for the
130 * waitqueue_active() load to get hoisted over the @cond store such that we'll
131 * observe an empty wait list while the waiter might not observe @cond.
132 *
133 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
134 * which (when the lock is uncontended) are of roughly equal cost.
135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static inline int waitqueue_active(wait_queue_head_t *q)
137{
138 return !list_empty(&q->task_list);
139}
140
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800141/**
142 * wq_has_sleeper - check if there are any waiting processes
143 * @wq: wait queue head
144 *
145 * Returns true if wq has waiting processes
146 *
147 * Please refer to the comment for waitqueue_active.
148 */
149static inline bool wq_has_sleeper(wait_queue_head_t *wq)
150{
151 /*
152 * We need to be sure we are in sync with the
153 * add_wait_queue modifications to the wait queue.
154 *
155 * This memory barrier should be paired with one on the
156 * waiting side.
157 */
158 smp_mb();
159 return waitqueue_active(wq);
160}
161
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800162extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
163extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
164extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
167{
168 list_add(&new->task_list, &head->task_list);
169}
170
171/*
172 * Used for wake-one threads:
173 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200174static inline void
175__add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
Changli Gaoa93d2f12010-05-07 14:33:26 +0800176{
177 wait->flags |= WQ_FLAG_EXCLUSIVE;
178 __add_wait_queue(q, wait);
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static inline void __add_wait_queue_tail(wait_queue_head_t *head,
Changli Gaoa93d2f12010-05-07 14:33:26 +0800182 wait_queue_t *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 list_add_tail(&new->task_list, &head->task_list);
185}
186
Ingo Molnarfb869b62013-10-04 10:24:49 +0200187static inline void
188__add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
Changli Gaoa93d2f12010-05-07 14:33:26 +0800189{
190 wait->flags |= WQ_FLAG_EXCLUSIVE;
191 __add_wait_queue_tail(q, wait);
192}
193
Ingo Molnarfb869b62013-10-04 10:24:49 +0200194static inline void
195__remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 list_del(&old->task_list);
198}
199
Peter Zijlstradfd01f02015-12-13 22:11:16 +0100200typedef int wait_bit_action_f(struct wait_bit_key *, int mode);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800201void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700202void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
Ingo Molnarfb869b62013-10-04 10:24:49 +0200203void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Thomas Gleixner63b20012011-12-01 00:04:00 +0100204void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700205void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800206void __wake_up_bit(wait_queue_head_t *, void *, int);
NeilBrownc1221322014-07-07 15:16:04 +1000207int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
208int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800209void wake_up_bit(void *, int);
David Howellscb655372013-05-10 19:50:26 +0100210void wake_up_atomic_t(atomic_t *);
NeilBrownc1221322014-07-07 15:16:04 +1000211int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
NeilBrowncbbce822014-09-25 13:55:19 +1000212int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
NeilBrownc1221322014-07-07 15:16:04 +1000213int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
David Howellscb655372013-05-10 19:50:26 +0100214int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800215wait_queue_head_t *bit_waitqueue(void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500217#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
218#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
219#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
Thomas Gleixner63b20012011-12-01 00:04:00 +0100220#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
221#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
224#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
225#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500226#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800228/*
Davide Libenzic0da3772009-03-31 15:24:20 -0700229 * Wakeup macros to be used to report events to the targets.
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800230 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200231#define wake_up_poll(x, m) \
Davide Libenzic0da3772009-03-31 15:24:20 -0700232 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
Ingo Molnarfb869b62013-10-04 10:24:49 +0200233#define wake_up_locked_poll(x, m) \
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700234 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
Ingo Molnarfb869b62013-10-04 10:24:49 +0200235#define wake_up_interruptible_poll(x, m) \
Davide Libenzic0da3772009-03-31 15:24:20 -0700236 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
237#define wake_up_interruptible_sync_poll(x, m) \
238 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800239
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200240#define ___wait_cond_timeout(condition) \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200241({ \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200242 bool __cond = (condition); \
243 if (__cond && !__ret) \
244 __ret = 1; \
245 __cond || !__ret; \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200246})
247
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200248#define ___wait_is_interruptible(state) \
249 (!__builtin_constant_p(state) || \
250 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200251
Oleg Nesterov0176bea2016-09-06 16:00:55 +0200252extern void init_wait_entry(wait_queue_t *__wait, int flags);
253
Peter Zijlstra8b322012014-04-18 15:07:17 -0700254/*
255 * The below macro ___wait_event() has an explicit shadow of the __ret
256 * variable when used from the wait_event_*() macros.
257 *
258 * This is so that both can use the ___wait_cond_timeout() construct
259 * to wrap the condition.
260 *
261 * The type inconsistency of the wait_event_*() __ret variable is also
262 * on purpose; we use long where we can return timeout values and int
263 * otherwise.
264 */
265
Peter Zijlstra41a14312013-10-02 11:22:21 +0200266#define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200267({ \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200268 __label__ __out; \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200269 wait_queue_t __wait; \
Peter Zijlstra8b322012014-04-18 15:07:17 -0700270 long __ret = ret; /* explicit shadow */ \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200271 \
Oleg Nesterov0176bea2016-09-06 16:00:55 +0200272 init_wait_entry(&__wait, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200273 for (;;) { \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200274 long __int = prepare_to_wait_event(&wq, &__wait, state);\
Peter Zijlstra41a14312013-10-02 11:22:21 +0200275 \
276 if (condition) \
277 break; \
278 \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200279 if (___wait_is_interruptible(state) && __int) { \
280 __ret = __int; \
Oleg Nesterovb1ea06a2016-09-08 18:48:15 +0200281 goto __out; \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200282 } \
283 \
284 cmd; \
285 } \
286 finish_wait(&wq, &__wait); \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200287__out: __ret; \
288})
Peter Zijlstra41a14312013-10-02 11:22:21 +0200289
Ingo Molnarfb869b62013-10-04 10:24:49 +0200290#define __wait_event(wq, condition) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200291 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
292 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294/**
295 * wait_event - sleep until a condition gets true
296 * @wq: the waitqueue to wait on
297 * @condition: a C expression for the event to wait for
298 *
299 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
300 * @condition evaluates to true. The @condition is checked each time
301 * the waitqueue @wq is woken up.
302 *
303 * wake_up() has to be called after changing any variable that could
304 * change the result of the wait condition.
305 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200306#define wait_event(wq, condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307do { \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200308 might_sleep(); \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200309 if (condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 break; \
311 __wait_event(wq, condition); \
312} while (0)
313
Peter Zijlstra2c561242015-02-03 12:55:31 +0100314#define __io_wait_event(wq, condition) \
315 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
316 io_schedule())
317
318/*
319 * io_wait_event() -- like wait_event() but with io_schedule()
320 */
321#define io_wait_event(wq, condition) \
322do { \
323 might_sleep(); \
324 if (condition) \
325 break; \
326 __io_wait_event(wq, condition); \
327} while (0)
328
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100329#define __wait_event_freezable(wq, condition) \
330 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
331 schedule(); try_to_freeze())
332
333/**
Stafford Hornef4bcfa12016-02-23 22:39:28 +0900334 * wait_event_freezable - sleep (or freeze) until a condition gets true
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100335 * @wq: the waitqueue to wait on
336 * @condition: a C expression for the event to wait for
337 *
338 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
339 * to system load) until the @condition evaluates to true. The
340 * @condition is checked each time the waitqueue @wq is woken up.
341 *
342 * wake_up() has to be called after changing any variable that could
343 * change the result of the wait condition.
344 */
345#define wait_event_freezable(wq, condition) \
346({ \
347 int __ret = 0; \
348 might_sleep(); \
349 if (!(condition)) \
350 __ret = __wait_event_freezable(wq, condition); \
351 __ret; \
352})
353
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200354#define __wait_event_timeout(wq, condition, timeout) \
355 ___wait_event(wq, ___wait_cond_timeout(condition), \
356 TASK_UNINTERRUPTIBLE, 0, timeout, \
357 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359/**
360 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
361 * @wq: the waitqueue to wait on
362 * @condition: a C expression for the event to wait for
363 * @timeout: timeout, in jiffies
364 *
365 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
366 * @condition evaluates to true. The @condition is checked each time
367 * the waitqueue @wq is woken up.
368 *
369 * wake_up() has to be called after changing any variable that could
370 * change the result of the wait condition.
371 *
Scot Doyle6b44f512014-08-24 17:12:27 +0000372 * Returns:
373 * 0 if the @condition evaluated to %false after the @timeout elapsed,
374 * 1 if the @condition evaluated to %true after the @timeout elapsed,
375 * or the remaining jiffies (at least 1) if the @condition evaluated
376 * to %true before the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
378#define wait_event_timeout(wq, condition, timeout) \
379({ \
380 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200381 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200382 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200383 __ret = __wait_event_timeout(wq, condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 __ret; \
385})
386
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100387#define __wait_event_freezable_timeout(wq, condition, timeout) \
388 ___wait_event(wq, ___wait_cond_timeout(condition), \
389 TASK_INTERRUPTIBLE, 0, timeout, \
390 __ret = schedule_timeout(__ret); try_to_freeze())
391
392/*
393 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
394 * increasing load and is freezable.
395 */
396#define wait_event_freezable_timeout(wq, condition, timeout) \
397({ \
398 long __ret = timeout; \
399 might_sleep(); \
400 if (!___wait_cond_timeout(condition)) \
401 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
402 __ret; \
403})
404
Yuanhan Liu9f3520c2015-05-08 18:19:05 +1000405#define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
406 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
407 cmd1; schedule(); cmd2)
408/*
409 * Just like wait_event_cmd(), except it sets exclusive flag
410 */
411#define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
412do { \
413 if (condition) \
414 break; \
415 __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2); \
416} while (0)
417
Shaohua Li82e06c82013-11-14 15:16:16 +1100418#define __wait_event_cmd(wq, condition, cmd1, cmd2) \
419 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
420 cmd1; schedule(); cmd2)
421
422/**
423 * wait_event_cmd - sleep until a condition gets true
424 * @wq: the waitqueue to wait on
425 * @condition: a C expression for the event to wait for
Masanari Iidaf434f7a2014-01-22 01:22:06 +0900426 * @cmd1: the command will be executed before sleep
427 * @cmd2: the command will be executed after sleep
Shaohua Li82e06c82013-11-14 15:16:16 +1100428 *
429 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
430 * @condition evaluates to true. The @condition is checked each time
431 * the waitqueue @wq is woken up.
432 *
433 * wake_up() has to be called after changing any variable that could
434 * change the result of the wait condition.
435 */
436#define wait_event_cmd(wq, condition, cmd1, cmd2) \
437do { \
438 if (condition) \
439 break; \
440 __wait_event_cmd(wq, condition, cmd1, cmd2); \
441} while (0)
442
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200443#define __wait_event_interruptible(wq, condition) \
444 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstraf13f4c42013-10-02 11:22:24 +0200445 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447/**
448 * wait_event_interruptible - sleep until a condition gets true
449 * @wq: the waitqueue to wait on
450 * @condition: a C expression for the event to wait for
451 *
452 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
453 * @condition evaluates to true or a signal is received.
454 * The @condition is checked each time the waitqueue @wq is woken up.
455 *
456 * wake_up() has to be called after changing any variable that could
457 * change the result of the wait condition.
458 *
459 * The function will return -ERESTARTSYS if it was interrupted by a
460 * signal and 0 if @condition evaluated to true.
461 */
462#define wait_event_interruptible(wq, condition) \
463({ \
464 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200465 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200467 __ret = __wait_event_interruptible(wq, condition); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 __ret; \
469})
470
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200471#define __wait_event_interruptible_timeout(wq, condition, timeout) \
472 ___wait_event(wq, ___wait_cond_timeout(condition), \
473 TASK_INTERRUPTIBLE, 0, timeout, \
474 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476/**
477 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
478 * @wq: the waitqueue to wait on
479 * @condition: a C expression for the event to wait for
480 * @timeout: timeout, in jiffies
481 *
482 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
483 * @condition evaluates to true or a signal is received.
484 * The @condition is checked each time the waitqueue @wq is woken up.
485 *
486 * wake_up() has to be called after changing any variable that could
487 * change the result of the wait condition.
488 *
Imre Deak4c663cf2013-05-24 15:55:09 -0700489 * Returns:
Scot Doyle6b44f512014-08-24 17:12:27 +0000490 * 0 if the @condition evaluated to %false after the @timeout elapsed,
491 * 1 if the @condition evaluated to %true after the @timeout elapsed,
492 * the remaining jiffies (at least 1) if the @condition evaluated
493 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
494 * interrupted by a signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
496#define wait_event_interruptible_timeout(wq, condition, timeout) \
497({ \
498 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200499 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200500 if (!___wait_cond_timeout(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200501 __ret = __wait_event_interruptible_timeout(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200502 condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 __ret; \
504})
505
Kent Overstreet774a08b2013-05-07 16:18:43 -0700506#define __wait_event_hrtimeout(wq, condition, timeout, state) \
507({ \
508 int __ret = 0; \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700509 struct hrtimer_sleeper __t; \
510 \
511 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
512 HRTIMER_MODE_REL); \
513 hrtimer_init_sleeper(&__t, current); \
Thomas Gleixner2456e852016-12-25 11:38:40 +0100514 if ((timeout) != KTIME_MAX) \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700515 hrtimer_start_range_ns(&__t.timer, timeout, \
516 current->timer_slack_ns, \
517 HRTIMER_MODE_REL); \
518 \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200519 __ret = ___wait_event(wq, condition, state, 0, 0, \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700520 if (!__t.task) { \
521 __ret = -ETIME; \
522 break; \
523 } \
Peter Zijlstraebdc1952013-10-02 11:22:32 +0200524 schedule()); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700525 \
526 hrtimer_cancel(&__t.timer); \
527 destroy_hrtimer_on_stack(&__t.timer); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700528 __ret; \
529})
530
531/**
532 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
533 * @wq: the waitqueue to wait on
534 * @condition: a C expression for the event to wait for
535 * @timeout: timeout, as a ktime_t
536 *
537 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
538 * @condition evaluates to true or a signal is received.
539 * The @condition is checked each time the waitqueue @wq is woken up.
540 *
541 * wake_up() has to be called after changing any variable that could
542 * change the result of the wait condition.
543 *
544 * The function returns 0 if @condition became true, or -ETIME if the timeout
545 * elapsed.
546 */
547#define wait_event_hrtimeout(wq, condition, timeout) \
548({ \
549 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200550 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700551 if (!(condition)) \
552 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
553 TASK_UNINTERRUPTIBLE); \
554 __ret; \
555})
556
557/**
558 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
559 * @wq: the waitqueue to wait on
560 * @condition: a C expression for the event to wait for
561 * @timeout: timeout, as a ktime_t
562 *
563 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
564 * @condition evaluates to true or a signal is received.
565 * The @condition is checked each time the waitqueue @wq is woken up.
566 *
567 * wake_up() has to be called after changing any variable that could
568 * change the result of the wait condition.
569 *
570 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
571 * interrupted by a signal, or -ETIME if the timeout elapsed.
572 */
573#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
574({ \
575 long __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200576 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700577 if (!(condition)) \
578 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
579 TASK_INTERRUPTIBLE); \
580 __ret; \
581})
582
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200583#define __wait_event_interruptible_exclusive(wq, condition) \
584 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
Peter Zijlstra48c25212013-10-02 11:22:26 +0200585 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587#define wait_event_interruptible_exclusive(wq, condition) \
588({ \
589 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200590 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200592 __ret = __wait_event_interruptible_exclusive(wq, condition);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 __ret; \
594})
595
Al Viro6a0fb302016-07-19 03:04:34 -0400596#define __wait_event_killable_exclusive(wq, condition) \
597 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
598 schedule())
599
600#define wait_event_killable_exclusive(wq, condition) \
601({ \
602 int __ret = 0; \
603 might_sleep(); \
604 if (!(condition)) \
605 __ret = __wait_event_killable_exclusive(wq, condition); \
606 __ret; \
607})
608
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200609
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100610#define __wait_event_freezable_exclusive(wq, condition) \
611 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
612 schedule(); try_to_freeze())
613
614#define wait_event_freezable_exclusive(wq, condition) \
615({ \
616 int __ret = 0; \
617 might_sleep(); \
618 if (!(condition)) \
619 __ret = __wait_event_freezable_exclusive(wq, condition);\
620 __ret; \
621})
622
623
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200624#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
625({ \
626 int __ret = 0; \
627 DEFINE_WAIT(__wait); \
628 if (exclusive) \
629 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
630 do { \
631 if (likely(list_empty(&__wait.task_list))) \
632 __add_wait_queue_tail(&(wq), &__wait); \
633 set_current_state(TASK_INTERRUPTIBLE); \
634 if (signal_pending(current)) { \
635 __ret = -ERESTARTSYS; \
636 break; \
637 } \
638 if (irq) \
639 spin_unlock_irq(&(wq).lock); \
640 else \
641 spin_unlock(&(wq).lock); \
642 schedule(); \
643 if (irq) \
644 spin_lock_irq(&(wq).lock); \
645 else \
646 spin_lock(&(wq).lock); \
647 } while (!(condition)); \
648 __remove_wait_queue(&(wq), &__wait); \
649 __set_current_state(TASK_RUNNING); \
650 __ret; \
651})
652
653
654/**
655 * wait_event_interruptible_locked - sleep until a condition gets true
656 * @wq: the waitqueue to wait on
657 * @condition: a C expression for the event to wait for
658 *
659 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
660 * @condition evaluates to true or a signal is received.
661 * The @condition is checked each time the waitqueue @wq is woken up.
662 *
663 * It must be called with wq.lock being held. This spinlock is
664 * unlocked while sleeping but @condition testing is done while lock
665 * is held and when this macro exits the lock is held.
666 *
667 * The lock is locked/unlocked using spin_lock()/spin_unlock()
668 * functions which must match the way they are locked/unlocked outside
669 * of this macro.
670 *
671 * wake_up_locked() has to be called after changing any variable that could
672 * change the result of the wait condition.
673 *
674 * The function will return -ERESTARTSYS if it was interrupted by a
675 * signal and 0 if @condition evaluated to true.
676 */
677#define wait_event_interruptible_locked(wq, condition) \
678 ((condition) \
679 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
680
681/**
682 * wait_event_interruptible_locked_irq - sleep until a condition gets true
683 * @wq: the waitqueue to wait on
684 * @condition: a C expression for the event to wait for
685 *
686 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
687 * @condition evaluates to true or a signal is received.
688 * The @condition is checked each time the waitqueue @wq is woken up.
689 *
690 * It must be called with wq.lock being held. This spinlock is
691 * unlocked while sleeping but @condition testing is done while lock
692 * is held and when this macro exits the lock is held.
693 *
694 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
695 * functions which must match the way they are locked/unlocked outside
696 * of this macro.
697 *
698 * wake_up_locked() has to be called after changing any variable that could
699 * change the result of the wait condition.
700 *
701 * The function will return -ERESTARTSYS if it was interrupted by a
702 * signal and 0 if @condition evaluated to true.
703 */
704#define wait_event_interruptible_locked_irq(wq, condition) \
705 ((condition) \
706 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
707
708/**
709 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
710 * @wq: the waitqueue to wait on
711 * @condition: a C expression for the event to wait for
712 *
713 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
714 * @condition evaluates to true or a signal is received.
715 * The @condition is checked each time the waitqueue @wq is woken up.
716 *
717 * It must be called with wq.lock being held. This spinlock is
718 * unlocked while sleeping but @condition testing is done while lock
719 * is held and when this macro exits the lock is held.
720 *
721 * The lock is locked/unlocked using spin_lock()/spin_unlock()
722 * functions which must match the way they are locked/unlocked outside
723 * of this macro.
724 *
725 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
726 * set thus when other process waits process on the list if this
727 * process is awaken further processes are not considered.
728 *
729 * wake_up_locked() has to be called after changing any variable that could
730 * change the result of the wait condition.
731 *
732 * The function will return -ERESTARTSYS if it was interrupted by a
733 * signal and 0 if @condition evaluated to true.
734 */
735#define wait_event_interruptible_exclusive_locked(wq, condition) \
736 ((condition) \
737 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
738
739/**
740 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
741 * @wq: the waitqueue to wait on
742 * @condition: a C expression for the event to wait for
743 *
744 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
745 * @condition evaluates to true or a signal is received.
746 * The @condition is checked each time the waitqueue @wq is woken up.
747 *
748 * It must be called with wq.lock being held. This spinlock is
749 * unlocked while sleeping but @condition testing is done while lock
750 * is held and when this macro exits the lock is held.
751 *
752 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
753 * functions which must match the way they are locked/unlocked outside
754 * of this macro.
755 *
756 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
757 * set thus when other process waits process on the list if this
758 * process is awaken further processes are not considered.
759 *
760 * wake_up_locked() has to be called after changing any variable that could
761 * change the result of the wait condition.
762 *
763 * The function will return -ERESTARTSYS if it was interrupted by a
764 * signal and 0 if @condition evaluated to true.
765 */
766#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
767 ((condition) \
768 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
769
770
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200771#define __wait_event_killable(wq, condition) \
772 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500773
774/**
775 * wait_event_killable - sleep until a condition gets true
776 * @wq: the waitqueue to wait on
777 * @condition: a C expression for the event to wait for
778 *
779 * The process is put to sleep (TASK_KILLABLE) until the
780 * @condition evaluates to true or a signal is received.
781 * The @condition is checked each time the waitqueue @wq is woken up.
782 *
783 * wake_up() has to be called after changing any variable that could
784 * change the result of the wait condition.
785 *
786 * The function will return -ERESTARTSYS if it was interrupted by a
787 * signal and 0 if @condition evaluated to true.
788 */
789#define wait_event_killable(wq, condition) \
790({ \
791 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200792 might_sleep(); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500793 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200794 __ret = __wait_event_killable(wq, condition); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500795 __ret; \
796})
797
Lukas Czernereed8c022012-11-30 11:42:40 +0100798
799#define __wait_event_lock_irq(wq, condition, lock, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200800 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
801 spin_unlock_irq(&lock); \
802 cmd; \
803 schedule(); \
804 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100805
806/**
807 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
808 * condition is checked under the lock. This
809 * is expected to be called with the lock
810 * taken.
811 * @wq: the waitqueue to wait on
812 * @condition: a C expression for the event to wait for
813 * @lock: a locked spinlock_t, which will be released before cmd
814 * and schedule() and reacquired afterwards.
815 * @cmd: a command which is invoked outside the critical section before
816 * sleep
817 *
818 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
819 * @condition evaluates to true. The @condition is checked each time
820 * the waitqueue @wq is woken up.
821 *
822 * wake_up() has to be called after changing any variable that could
823 * change the result of the wait condition.
824 *
825 * This is supposed to be called while holding the lock. The lock is
826 * dropped before invoking the cmd and going to sleep and is reacquired
827 * afterwards.
828 */
829#define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
830do { \
831 if (condition) \
832 break; \
833 __wait_event_lock_irq(wq, condition, lock, cmd); \
834} while (0)
835
836/**
837 * wait_event_lock_irq - sleep until a condition gets true. The
838 * condition is checked under the lock. This
839 * is expected to be called with the lock
840 * taken.
841 * @wq: the waitqueue to wait on
842 * @condition: a C expression for the event to wait for
843 * @lock: a locked spinlock_t, which will be released before schedule()
844 * and reacquired afterwards.
845 *
846 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
847 * @condition evaluates to true. The @condition is checked each time
848 * the waitqueue @wq is woken up.
849 *
850 * wake_up() has to be called after changing any variable that could
851 * change the result of the wait condition.
852 *
853 * This is supposed to be called while holding the lock. The lock is
854 * dropped before going to sleep and is reacquired afterwards.
855 */
856#define wait_event_lock_irq(wq, condition, lock) \
857do { \
858 if (condition) \
859 break; \
860 __wait_event_lock_irq(wq, condition, lock, ); \
861} while (0)
862
863
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200864#define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200865 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200866 spin_unlock_irq(&lock); \
867 cmd; \
868 schedule(); \
Peter Zijlstra8fbd88f2013-10-02 11:22:28 +0200869 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100870
871/**
872 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
873 * The condition is checked under the lock. This is expected to
874 * be called with the lock taken.
875 * @wq: the waitqueue to wait on
876 * @condition: a C expression for the event to wait for
877 * @lock: a locked spinlock_t, which will be released before cmd and
878 * schedule() and reacquired afterwards.
879 * @cmd: a command which is invoked outside the critical section before
880 * sleep
881 *
882 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
883 * @condition evaluates to true or a signal is received. The @condition is
884 * checked each time the waitqueue @wq is woken up.
885 *
886 * wake_up() has to be called after changing any variable that could
887 * change the result of the wait condition.
888 *
889 * This is supposed to be called while holding the lock. The lock is
890 * dropped before invoking the cmd and going to sleep and is reacquired
891 * afterwards.
892 *
893 * The macro will return -ERESTARTSYS if it was interrupted by a signal
894 * and 0 if @condition evaluated to true.
895 */
896#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
897({ \
898 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100899 if (!(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200900 __ret = __wait_event_interruptible_lock_irq(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200901 condition, lock, cmd); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100902 __ret; \
903})
904
905/**
906 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
907 * The condition is checked under the lock. This is expected
908 * to be called with the lock taken.
909 * @wq: the waitqueue to wait on
910 * @condition: a C expression for the event to wait for
911 * @lock: a locked spinlock_t, which will be released before schedule()
912 * and reacquired afterwards.
913 *
914 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
915 * @condition evaluates to true or signal is received. The @condition is
916 * checked each time the waitqueue @wq is woken up.
917 *
918 * wake_up() has to be called after changing any variable that could
919 * change the result of the wait condition.
920 *
921 * This is supposed to be called while holding the lock. The lock is
922 * dropped before going to sleep and is reacquired afterwards.
923 *
924 * The macro will return -ERESTARTSYS if it was interrupted by a signal
925 * and 0 if @condition evaluated to true.
926 */
927#define wait_event_interruptible_lock_irq(wq, condition, lock) \
928({ \
929 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100930 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200931 __ret = __wait_event_interruptible_lock_irq(wq, \
Thierry Reding92ec1182013-10-23 13:40:55 +0200932 condition, lock,); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100933 __ret; \
934})
935
Ingo Molnarfb869b62013-10-04 10:24:49 +0200936#define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
937 lock, timeout) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200938 ___wait_event(wq, ___wait_cond_timeout(condition), \
Heiko Carstens7d716452013-10-31 12:48:14 +0100939 TASK_INTERRUPTIBLE, 0, timeout, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200940 spin_unlock_irq(&lock); \
941 __ret = schedule_timeout(__ret); \
Peter Zijlstraa1dc6852013-10-02 11:22:29 +0200942 spin_lock_irq(&lock));
Martin Peschked79ff142013-08-22 17:45:36 +0200943
944/**
Ingo Molnarfb869b62013-10-04 10:24:49 +0200945 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
946 * true or a timeout elapses. The condition is checked under
947 * the lock. This is expected to be called with the lock taken.
Martin Peschked79ff142013-08-22 17:45:36 +0200948 * @wq: the waitqueue to wait on
949 * @condition: a C expression for the event to wait for
950 * @lock: a locked spinlock_t, which will be released before schedule()
951 * and reacquired afterwards.
952 * @timeout: timeout, in jiffies
953 *
954 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
955 * @condition evaluates to true or signal is received. The @condition is
956 * checked each time the waitqueue @wq is woken up.
957 *
958 * wake_up() has to be called after changing any variable that could
959 * change the result of the wait condition.
960 *
961 * This is supposed to be called while holding the lock. The lock is
962 * dropped before going to sleep and is reacquired afterwards.
963 *
964 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
965 * was interrupted by a signal, and the remaining jiffies otherwise
966 * if the condition evaluated to true before the timeout elapsed.
967 */
968#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
969 timeout) \
970({ \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200971 long __ret = timeout; \
Oleg Nesterov89229152013-10-07 20:31:06 +0200972 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200973 __ret = __wait_event_interruptible_lock_irq_timeout( \
974 wq, condition, lock, timeout); \
Martin Peschked79ff142013-08-22 17:45:36 +0200975 __ret; \
976})
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978/*
979 * Waitqueues which are removed from the waitqueue_head at wakeup time
980 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800981void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
982void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200983long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800984void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Peter Zijlstra61ada522014-09-24 10:18:47 +0200985long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
986int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
988int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
989
Eric Dumazetbf368e42009-04-28 02:24:21 -0700990#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700992 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -0700993 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200994 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
Eric Dumazetbf368e42009-04-28 02:24:21 -0700997#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999#define DEFINE_WAIT_BIT(name, word, bit) \
1000 struct wait_bit_queue name = { \
1001 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
1002 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -07001003 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 .func = wake_bit_function, \
1005 .task_list = \
1006 LIST_HEAD_INIT((name).wait.task_list), \
1007 }, \
1008 }
1009
1010#define init_wait(wait) \
1011 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -07001012 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 (wait)->func = autoremove_wake_function; \
1014 INIT_LIST_HEAD(&(wait)->task_list); \
Evgeny Kuznetsov231d0ae2010-10-05 12:47:57 +04001015 (wait)->flags = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 } while (0)
1017
NeilBrown74316202014-07-07 15:16:04 +10001018
Peter Zijlstradfd01f02015-12-13 22:11:16 +01001019extern int bit_wait(struct wait_bit_key *, int);
1020extern int bit_wait_io(struct wait_bit_key *, int);
1021extern int bit_wait_timeout(struct wait_bit_key *, int);
1022extern int bit_wait_io_timeout(struct wait_bit_key *, int);
NeilBrown74316202014-07-07 15:16:04 +10001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/**
1025 * wait_on_bit - wait for a bit to be cleared
1026 * @word: the word being waited on, a kernel virtual address
1027 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 * @mode: the task state to sleep in
1029 *
1030 * There is a standard hashed waitqueue table for generic use. This
1031 * is the part of the hashtable's accessor API that waits on a bit.
1032 * For instance, if one were to have waiters on a bitflag, one would
1033 * call wait_on_bit() in threads waiting for the bit to clear.
1034 * One uses wait_on_bit() where one is waiting for the bit to clear,
1035 * but has no intention of setting it.
NeilBrown74316202014-07-07 15:16:04 +10001036 * Returned value will be zero if the bit was cleared, or non-zero
1037 * if the process received a signal and the mode permitted wakeup
1038 * on that signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
Ingo Molnarfb869b62013-10-04 10:24:49 +02001040static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001041wait_on_bit(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001042{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001043 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001044 if (!test_bit(bit, word))
1045 return 0;
1046 return out_of_line_wait_on_bit(word, bit,
1047 bit_wait,
1048 mode);
1049}
1050
1051/**
1052 * wait_on_bit_io - wait for a bit to be cleared
1053 * @word: the word being waited on, a kernel virtual address
1054 * @bit: the bit of the word being waited on
1055 * @mode: the task state to sleep in
1056 *
1057 * Use the standard hashed waitqueue table to wait for a bit
1058 * to be cleared. This is similar to wait_on_bit(), but calls
1059 * io_schedule() instead of schedule() for the actual waiting.
1060 *
1061 * Returned value will be zero if the bit was cleared, or non-zero
1062 * if the process received a signal and the mode permitted wakeup
1063 * on that signal.
1064 */
1065static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001066wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001067{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001068 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001069 if (!test_bit(bit, word))
1070 return 0;
1071 return out_of_line_wait_on_bit(word, bit,
1072 bit_wait_io,
1073 mode);
1074}
1075
1076/**
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001077 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
1078 * @word: the word being waited on, a kernel virtual address
1079 * @bit: the bit of the word being waited on
1080 * @mode: the task state to sleep in
1081 * @timeout: timeout, in jiffies
1082 *
1083 * Use the standard hashed waitqueue table to wait for a bit
1084 * to be cleared. This is similar to wait_on_bit(), except also takes a
1085 * timeout parameter.
1086 *
1087 * Returned value will be zero if the bit was cleared before the
1088 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1089 * received a signal and the mode permitted wakeup on that signal.
1090 */
1091static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001092wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
1093 unsigned long timeout)
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001094{
1095 might_sleep();
1096 if (!test_bit(bit, word))
1097 return 0;
1098 return out_of_line_wait_on_bit_timeout(word, bit,
1099 bit_wait_timeout,
1100 mode, timeout);
1101}
1102
1103/**
NeilBrown74316202014-07-07 15:16:04 +10001104 * wait_on_bit_action - wait for a bit to be cleared
1105 * @word: the word being waited on, a kernel virtual address
1106 * @bit: the bit of the word being waited on
1107 * @action: the function used to sleep, which may take special actions
1108 * @mode: the task state to sleep in
1109 *
1110 * Use the standard hashed waitqueue table to wait for a bit
1111 * to be cleared, and allow the waiting action to be specified.
1112 * This is like wait_on_bit() but allows fine control of how the waiting
1113 * is done.
1114 *
1115 * Returned value will be zero if the bit was cleared, or non-zero
1116 * if the process received a signal and the mode permitted wakeup
1117 * on that signal.
1118 */
1119static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001120wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
1121 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001123 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (!test_bit(bit, word))
1125 return 0;
1126 return out_of_line_wait_on_bit(word, bit, action, mode);
1127}
1128
1129/**
1130 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1131 * @word: the word being waited on, a kernel virtual address
1132 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * @mode: the task state to sleep in
1134 *
1135 * There is a standard hashed waitqueue table for generic use. This
1136 * is the part of the hashtable's accessor API that waits on a bit
1137 * when one intends to set it, for instance, trying to lock bitflags.
1138 * For instance, if one were to have waiters trying to set bitflag
1139 * and waiting for it to clear before setting it, one would call
1140 * wait_on_bit() in threads waiting to be able to set the bit.
1141 * One uses wait_on_bit_lock() where one is waiting for the bit to
1142 * clear with the intention of setting it, and when done, clearing it.
NeilBrown74316202014-07-07 15:16:04 +10001143 *
1144 * Returns zero if the bit was (eventually) found to be clear and was
1145 * set. Returns non-zero if a signal was delivered to the process and
1146 * the @mode allows that signal to wake the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 */
Ingo Molnarfb869b62013-10-04 10:24:49 +02001148static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001149wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001150{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001151 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001152 if (!test_and_set_bit(bit, word))
1153 return 0;
1154 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1155}
1156
1157/**
1158 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1159 * @word: the word being waited on, a kernel virtual address
1160 * @bit: the bit of the word being waited on
1161 * @mode: the task state to sleep in
1162 *
1163 * Use the standard hashed waitqueue table to wait for a bit
1164 * to be cleared and then to atomically set it. This is similar
1165 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1166 * for the actual waiting.
1167 *
1168 * Returns zero if the bit was (eventually) found to be clear and was
1169 * set. Returns non-zero if a signal was delivered to the process and
1170 * the @mode allows that signal to wake the process.
1171 */
1172static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001173wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001174{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001175 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001176 if (!test_and_set_bit(bit, word))
1177 return 0;
1178 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1179}
1180
1181/**
1182 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1183 * @word: the word being waited on, a kernel virtual address
1184 * @bit: the bit of the word being waited on
1185 * @action: the function used to sleep, which may take special actions
1186 * @mode: the task state to sleep in
1187 *
1188 * Use the standard hashed waitqueue table to wait for a bit
1189 * to be cleared and then to set it, and allow the waiting action
1190 * to be specified.
1191 * This is like wait_on_bit() but allows fine control of how the waiting
1192 * is done.
1193 *
1194 * Returns zero if the bit was (eventually) found to be clear and was
1195 * set. Returns non-zero if a signal was delivered to the process and
1196 * the @mode allows that signal to wake the process.
1197 */
1198static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001199wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
1200 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001202 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (!test_and_set_bit(bit, word))
1204 return 0;
1205 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1206}
David Howellscb655372013-05-10 19:50:26 +01001207
1208/**
1209 * wait_on_atomic_t - Wait for an atomic_t to become 0
1210 * @val: The atomic value being waited on, a kernel virtual address
1211 * @action: the function used to sleep, which may take special actions
1212 * @mode: the task state to sleep in
1213 *
1214 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1215 * the purpose of getting a waitqueue, but we set the key to a bit number
1216 * outside of the target 'word'.
1217 */
1218static inline
1219int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1220{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001221 might_sleep();
David Howellscb655372013-05-10 19:50:26 +01001222 if (atomic_read(val) == 0)
1223 return 0;
1224 return out_of_line_wait_on_atomic_t(val, action, mode);
1225}
Ingo Molnarfb869b62013-10-04 10:24:49 +02001226
1227#endif /* _LINUX_WAIT_H */