blob: c3ff74d764faad094e7f00dcd025ddb568a6382d [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/current.h>
David Howells607ca462012-10-13 10:46:48 +010010#include <uapi/linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12typedef struct __wait_queue wait_queue_t;
Peter Zijlstra7d478722009-09-14 19:55:44 +020013typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
14int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Peter Zijlstra61ada522014-09-24 10:18:47 +020016/* __wait_queue::flags */
17#define WQ_FLAG_EXCLUSIVE 0x01
18#define WQ_FLAG_WOKEN 0x02
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020struct __wait_queue {
Ingo Molnarfb869b62013-10-04 10:24:49 +020021 unsigned int flags;
Ingo Molnarfb869b62013-10-04 10:24:49 +020022 void *private;
23 wait_queue_func_t func;
24 struct list_head task_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025};
26
27struct wait_bit_key {
Ingo Molnarfb869b62013-10-04 10:24:49 +020028 void *flags;
29 int bit_nr;
30#define WAIT_ATOMIC_T_BIT_NR -1
NeilBrowncbbce822014-09-25 13:55:19 +100031 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
34struct wait_bit_queue {
Ingo Molnarfb869b62013-10-04 10:24:49 +020035 struct wait_bit_key key;
36 wait_queue_t wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
39struct __wait_queue_head {
Ingo Molnarfb869b62013-10-04 10:24:49 +020040 spinlock_t lock;
41 struct list_head task_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43typedef struct __wait_queue_head wait_queue_head_t;
44
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080045struct task_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
48 * Macros for declaration and initialisaton of the datatypes
49 */
50
51#define __WAITQUEUE_INITIALIZER(name, tsk) { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070052 .private = tsk, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .func = default_wake_function, \
54 .task_list = { NULL, NULL } }
55
56#define DECLARE_WAITQUEUE(name, tsk) \
57 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
58
59#define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
Ingo Molnare4d91912006-07-03 00:24:34 -070060 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 .task_list = { &(name).task_list, &(name).task_list } }
62
63#define DECLARE_WAIT_QUEUE_HEAD(name) \
64 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
65
66#define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
67 { .flags = word, .bit_nr = bit, }
68
David Howellscb655372013-05-10 19:50:26 +010069#define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
70 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
71
Peter Zijlstraf07fdec2011-12-13 13:20:54 +010072extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
Peter Zijlstra2fc39112009-08-10 12:33:05 +010073
74#define init_waitqueue_head(q) \
75 do { \
76 static struct lock_class_key __key; \
77 \
Peter Zijlstraf07fdec2011-12-13 13:20:54 +010078 __init_waitqueue_head((q), #q, &__key); \
Peter Zijlstra2fc39112009-08-10 12:33:05 +010079 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080081#ifdef CONFIG_LOCKDEP
82# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
83 ({ init_waitqueue_head(&name); name; })
84# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
85 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
86#else
87# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
88#endif
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
91{
Ingo Molnarfb869b62013-10-04 10:24:49 +020092 q->flags = 0;
93 q->private = p;
94 q->func = default_wake_function;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Ingo Molnarfb869b62013-10-04 10:24:49 +020097static inline void
98init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Ingo Molnarfb869b62013-10-04 10:24:49 +0200100 q->flags = 0;
101 q->private = NULL;
102 q->func = func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Peter Zijlstra69e51e922015-10-23 14:32:34 +0200105/**
106 * waitqueue_active -- locklessly test for waiters on the queue
107 * @q: the waitqueue to test for waiters
108 *
109 * returns true if the wait list is not empty
110 *
111 * NOTE: this function is lockless and requires care, incorrect usage _will_
112 * lead to sporadic and non-obvious failure.
113 *
114 * Use either while holding wait_queue_head_t::lock or when used for wakeups
115 * with an extra smp_mb() like:
116 *
117 * CPU0 - waker CPU1 - waiter
118 *
119 * for (;;) {
120 * @cond = true; prepare_to_wait(&wq, &wait, state);
121 * smp_mb(); // smp_mb() from set_current_state()
122 * if (waitqueue_active(wq)) if (@cond)
123 * wake_up(wq); break;
124 * schedule();
125 * }
126 * finish_wait(&wq, &wait);
127 *
128 * Because without the explicit smp_mb() it's possible for the
129 * waitqueue_active() load to get hoisted over the @cond store such that we'll
130 * observe an empty wait list while the waiter might not observe @cond.
131 *
132 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
133 * which (when the lock is uncontended) are of roughly equal cost.
134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static inline int waitqueue_active(wait_queue_head_t *q)
136{
137 return !list_empty(&q->task_list);
138}
139
Herbert Xu1ce0bf52015-11-26 13:55:39 +0800140/**
141 * wq_has_sleeper - check if there are any waiting processes
142 * @wq: wait queue head
143 *
144 * Returns true if wq has waiting processes
145 *
146 * Please refer to the comment for waitqueue_active.
147 */
148static inline bool wq_has_sleeper(wait_queue_head_t *wq)
149{
150 /*
151 * We need to be sure we are in sync with the
152 * add_wait_queue modifications to the wait queue.
153 *
154 * This memory barrier should be paired with one on the
155 * waiting side.
156 */
157 smp_mb();
158 return waitqueue_active(wq);
159}
160
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800161extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
162extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
163extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
166{
167 list_add(&new->task_list, &head->task_list);
168}
169
170/*
171 * Used for wake-one threads:
172 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200173static inline void
174__add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
Changli Gaoa93d2f12010-05-07 14:33:26 +0800175{
176 wait->flags |= WQ_FLAG_EXCLUSIVE;
177 __add_wait_queue(q, wait);
178}
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static inline void __add_wait_queue_tail(wait_queue_head_t *head,
Changli Gaoa93d2f12010-05-07 14:33:26 +0800181 wait_queue_t *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 list_add_tail(&new->task_list, &head->task_list);
184}
185
Ingo Molnarfb869b62013-10-04 10:24:49 +0200186static inline void
187__add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
Changli Gaoa93d2f12010-05-07 14:33:26 +0800188{
189 wait->flags |= WQ_FLAG_EXCLUSIVE;
190 __add_wait_queue_tail(q, wait);
191}
192
Ingo Molnarfb869b62013-10-04 10:24:49 +0200193static inline void
194__remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 list_del(&old->task_list);
197}
198
Peter Zijlstradfd01f02015-12-13 22:11:16 +0100199typedef int wait_bit_action_f(struct wait_bit_key *, int mode);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800200void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700201void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
Ingo Molnarfb869b62013-10-04 10:24:49 +0200202void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Thomas Gleixner63b20012011-12-01 00:04:00 +0100203void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700204void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800205void __wake_up_bit(wait_queue_head_t *, void *, int);
NeilBrownc1221322014-07-07 15:16:04 +1000206int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
207int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800208void wake_up_bit(void *, int);
David Howellscb655372013-05-10 19:50:26 +0100209void wake_up_atomic_t(atomic_t *);
NeilBrownc1221322014-07-07 15:16:04 +1000210int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
NeilBrowncbbce822014-09-25 13:55:19 +1000211int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
NeilBrownc1221322014-07-07 15:16:04 +1000212int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
David Howellscb655372013-05-10 19:50:26 +0100213int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800214wait_queue_head_t *bit_waitqueue(void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500216#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
217#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
218#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
Thomas Gleixner63b20012011-12-01 00:04:00 +0100219#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
220#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
223#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
224#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500225#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800227/*
Davide Libenzic0da3772009-03-31 15:24:20 -0700228 * Wakeup macros to be used to report events to the targets.
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800229 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200230#define wake_up_poll(x, m) \
Davide Libenzic0da3772009-03-31 15:24:20 -0700231 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
Ingo Molnarfb869b62013-10-04 10:24:49 +0200232#define wake_up_locked_poll(x, m) \
Andrea Arcangeliac5be6b2015-09-22 14:58:49 -0700233 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
Ingo Molnarfb869b62013-10-04 10:24:49 +0200234#define wake_up_interruptible_poll(x, m) \
Davide Libenzic0da3772009-03-31 15:24:20 -0700235 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
236#define wake_up_interruptible_sync_poll(x, m) \
237 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800238
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200239#define ___wait_cond_timeout(condition) \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200240({ \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200241 bool __cond = (condition); \
242 if (__cond && !__ret) \
243 __ret = 1; \
244 __cond || !__ret; \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200245})
246
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200247#define ___wait_is_interruptible(state) \
248 (!__builtin_constant_p(state) || \
249 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200250
Peter Zijlstra8b322012014-04-18 15:07:17 -0700251/*
252 * The below macro ___wait_event() has an explicit shadow of the __ret
253 * variable when used from the wait_event_*() macros.
254 *
255 * This is so that both can use the ___wait_cond_timeout() construct
256 * to wrap the condition.
257 *
258 * The type inconsistency of the wait_event_*() __ret variable is also
259 * on purpose; we use long where we can return timeout values and int
260 * otherwise.
261 */
262
Peter Zijlstra41a14312013-10-02 11:22:21 +0200263#define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200264({ \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200265 __label__ __out; \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200266 wait_queue_t __wait; \
Peter Zijlstra8b322012014-04-18 15:07:17 -0700267 long __ret = ret; /* explicit shadow */ \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200268 \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200269 INIT_LIST_HEAD(&__wait.task_list); \
270 if (exclusive) \
271 __wait.flags = WQ_FLAG_EXCLUSIVE; \
272 else \
273 __wait.flags = 0; \
274 \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200275 for (;;) { \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200276 long __int = prepare_to_wait_event(&wq, &__wait, state);\
Peter Zijlstra41a14312013-10-02 11:22:21 +0200277 \
278 if (condition) \
279 break; \
280 \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200281 if (___wait_is_interruptible(state) && __int) { \
282 __ret = __int; \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200283 if (exclusive) { \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200284 abort_exclusive_wait(&wq, &__wait, \
285 state, NULL); \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200286 goto __out; \
287 } \
288 break; \
289 } \
290 \
291 cmd; \
292 } \
293 finish_wait(&wq, &__wait); \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200294__out: __ret; \
295})
Peter Zijlstra41a14312013-10-02 11:22:21 +0200296
Ingo Molnarfb869b62013-10-04 10:24:49 +0200297#define __wait_event(wq, condition) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200298 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
299 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301/**
302 * wait_event - sleep until a condition gets true
303 * @wq: the waitqueue to wait on
304 * @condition: a C expression for the event to wait for
305 *
306 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
307 * @condition evaluates to true. The @condition is checked each time
308 * the waitqueue @wq is woken up.
309 *
310 * wake_up() has to be called after changing any variable that could
311 * change the result of the wait condition.
312 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200313#define wait_event(wq, condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314do { \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200315 might_sleep(); \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200316 if (condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 break; \
318 __wait_event(wq, condition); \
319} while (0)
320
Peter Zijlstra2c561242015-02-03 12:55:31 +0100321#define __io_wait_event(wq, condition) \
322 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
323 io_schedule())
324
325/*
326 * io_wait_event() -- like wait_event() but with io_schedule()
327 */
328#define io_wait_event(wq, condition) \
329do { \
330 might_sleep(); \
331 if (condition) \
332 break; \
333 __io_wait_event(wq, condition); \
334} while (0)
335
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100336#define __wait_event_freezable(wq, condition) \
337 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
338 schedule(); try_to_freeze())
339
340/**
Stafford Hornef4bcfa12016-02-23 22:39:28 +0900341 * wait_event_freezable - sleep (or freeze) until a condition gets true
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100342 * @wq: the waitqueue to wait on
343 * @condition: a C expression for the event to wait for
344 *
345 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
346 * to system load) until the @condition evaluates to true. The
347 * @condition is checked each time the waitqueue @wq is woken up.
348 *
349 * wake_up() has to be called after changing any variable that could
350 * change the result of the wait condition.
351 */
352#define wait_event_freezable(wq, condition) \
353({ \
354 int __ret = 0; \
355 might_sleep(); \
356 if (!(condition)) \
357 __ret = __wait_event_freezable(wq, condition); \
358 __ret; \
359})
360
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200361#define __wait_event_timeout(wq, condition, timeout) \
362 ___wait_event(wq, ___wait_cond_timeout(condition), \
363 TASK_UNINTERRUPTIBLE, 0, timeout, \
364 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366/**
367 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
368 * @wq: the waitqueue to wait on
369 * @condition: a C expression for the event to wait for
370 * @timeout: timeout, in jiffies
371 *
372 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
373 * @condition evaluates to true. The @condition is checked each time
374 * the waitqueue @wq is woken up.
375 *
376 * wake_up() has to be called after changing any variable that could
377 * change the result of the wait condition.
378 *
Scot Doyle6b44f512014-08-24 17:12:27 +0000379 * Returns:
380 * 0 if the @condition evaluated to %false after the @timeout elapsed,
381 * 1 if the @condition evaluated to %true after the @timeout elapsed,
382 * or the remaining jiffies (at least 1) if the @condition evaluated
383 * to %true before the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 */
385#define wait_event_timeout(wq, condition, timeout) \
386({ \
387 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200388 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200389 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200390 __ret = __wait_event_timeout(wq, condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 __ret; \
392})
393
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100394#define __wait_event_freezable_timeout(wq, condition, timeout) \
395 ___wait_event(wq, ___wait_cond_timeout(condition), \
396 TASK_INTERRUPTIBLE, 0, timeout, \
397 __ret = schedule_timeout(__ret); try_to_freeze())
398
399/*
400 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
401 * increasing load and is freezable.
402 */
403#define wait_event_freezable_timeout(wq, condition, timeout) \
404({ \
405 long __ret = timeout; \
406 might_sleep(); \
407 if (!___wait_cond_timeout(condition)) \
408 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
409 __ret; \
410})
411
Yuanhan Liu9f3520c2015-05-08 18:19:05 +1000412#define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
413 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
414 cmd1; schedule(); cmd2)
415/*
416 * Just like wait_event_cmd(), except it sets exclusive flag
417 */
418#define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
419do { \
420 if (condition) \
421 break; \
422 __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2); \
423} while (0)
424
Shaohua Li82e06c82013-11-14 15:16:16 +1100425#define __wait_event_cmd(wq, condition, cmd1, cmd2) \
426 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
427 cmd1; schedule(); cmd2)
428
429/**
430 * wait_event_cmd - sleep until a condition gets true
431 * @wq: the waitqueue to wait on
432 * @condition: a C expression for the event to wait for
Masanari Iidaf434f7a2014-01-22 01:22:06 +0900433 * @cmd1: the command will be executed before sleep
434 * @cmd2: the command will be executed after sleep
Shaohua Li82e06c82013-11-14 15:16:16 +1100435 *
436 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
437 * @condition evaluates to true. The @condition is checked each time
438 * the waitqueue @wq is woken up.
439 *
440 * wake_up() has to be called after changing any variable that could
441 * change the result of the wait condition.
442 */
443#define wait_event_cmd(wq, condition, cmd1, cmd2) \
444do { \
445 if (condition) \
446 break; \
447 __wait_event_cmd(wq, condition, cmd1, cmd2); \
448} while (0)
449
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200450#define __wait_event_interruptible(wq, condition) \
451 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstraf13f4c42013-10-02 11:22:24 +0200452 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454/**
455 * wait_event_interruptible - sleep until a condition gets true
456 * @wq: the waitqueue to wait on
457 * @condition: a C expression for the event to wait for
458 *
459 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
460 * @condition evaluates to true or a signal is received.
461 * The @condition is checked each time the waitqueue @wq is woken up.
462 *
463 * wake_up() has to be called after changing any variable that could
464 * change the result of the wait condition.
465 *
466 * The function will return -ERESTARTSYS if it was interrupted by a
467 * signal and 0 if @condition evaluated to true.
468 */
469#define wait_event_interruptible(wq, condition) \
470({ \
471 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200472 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200474 __ret = __wait_event_interruptible(wq, condition); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 __ret; \
476})
477
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200478#define __wait_event_interruptible_timeout(wq, condition, timeout) \
479 ___wait_event(wq, ___wait_cond_timeout(condition), \
480 TASK_INTERRUPTIBLE, 0, timeout, \
481 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483/**
484 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
485 * @wq: the waitqueue to wait on
486 * @condition: a C expression for the event to wait for
487 * @timeout: timeout, in jiffies
488 *
489 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
490 * @condition evaluates to true or a signal is received.
491 * The @condition is checked each time the waitqueue @wq is woken up.
492 *
493 * wake_up() has to be called after changing any variable that could
494 * change the result of the wait condition.
495 *
Imre Deak4c663cf2013-05-24 15:55:09 -0700496 * Returns:
Scot Doyle6b44f512014-08-24 17:12:27 +0000497 * 0 if the @condition evaluated to %false after the @timeout elapsed,
498 * 1 if the @condition evaluated to %true after the @timeout elapsed,
499 * the remaining jiffies (at least 1) if the @condition evaluated
500 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
501 * interrupted by a signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
503#define wait_event_interruptible_timeout(wq, condition, timeout) \
504({ \
505 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200506 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200507 if (!___wait_cond_timeout(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200508 __ret = __wait_event_interruptible_timeout(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200509 condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 __ret; \
511})
512
Kent Overstreet774a08b2013-05-07 16:18:43 -0700513#define __wait_event_hrtimeout(wq, condition, timeout, state) \
514({ \
515 int __ret = 0; \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700516 struct hrtimer_sleeper __t; \
517 \
518 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
519 HRTIMER_MODE_REL); \
520 hrtimer_init_sleeper(&__t, current); \
521 if ((timeout).tv64 != KTIME_MAX) \
522 hrtimer_start_range_ns(&__t.timer, timeout, \
523 current->timer_slack_ns, \
524 HRTIMER_MODE_REL); \
525 \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200526 __ret = ___wait_event(wq, condition, state, 0, 0, \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700527 if (!__t.task) { \
528 __ret = -ETIME; \
529 break; \
530 } \
Peter Zijlstraebdc1952013-10-02 11:22:32 +0200531 schedule()); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700532 \
533 hrtimer_cancel(&__t.timer); \
534 destroy_hrtimer_on_stack(&__t.timer); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700535 __ret; \
536})
537
538/**
539 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
540 * @wq: the waitqueue to wait on
541 * @condition: a C expression for the event to wait for
542 * @timeout: timeout, as a ktime_t
543 *
544 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
545 * @condition evaluates to true or a signal is received.
546 * The @condition is checked each time the waitqueue @wq is woken up.
547 *
548 * wake_up() has to be called after changing any variable that could
549 * change the result of the wait condition.
550 *
551 * The function returns 0 if @condition became true, or -ETIME if the timeout
552 * elapsed.
553 */
554#define wait_event_hrtimeout(wq, condition, timeout) \
555({ \
556 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200557 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700558 if (!(condition)) \
559 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
560 TASK_UNINTERRUPTIBLE); \
561 __ret; \
562})
563
564/**
565 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
566 * @wq: the waitqueue to wait on
567 * @condition: a C expression for the event to wait for
568 * @timeout: timeout, as a ktime_t
569 *
570 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
571 * @condition evaluates to true or a signal is received.
572 * The @condition is checked each time the waitqueue @wq is woken up.
573 *
574 * wake_up() has to be called after changing any variable that could
575 * change the result of the wait condition.
576 *
577 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
578 * interrupted by a signal, or -ETIME if the timeout elapsed.
579 */
580#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
581({ \
582 long __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200583 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700584 if (!(condition)) \
585 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
586 TASK_INTERRUPTIBLE); \
587 __ret; \
588})
589
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200590#define __wait_event_interruptible_exclusive(wq, condition) \
591 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
Peter Zijlstra48c25212013-10-02 11:22:26 +0200592 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594#define wait_event_interruptible_exclusive(wq, condition) \
595({ \
596 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200597 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200599 __ret = __wait_event_interruptible_exclusive(wq, condition);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 __ret; \
601})
602
Al Viro6a0fb302016-07-19 03:04:34 -0400603#define __wait_event_killable_exclusive(wq, condition) \
604 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
605 schedule())
606
607#define wait_event_killable_exclusive(wq, condition) \
608({ \
609 int __ret = 0; \
610 might_sleep(); \
611 if (!(condition)) \
612 __ret = __wait_event_killable_exclusive(wq, condition); \
613 __ret; \
614})
615
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200616
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100617#define __wait_event_freezable_exclusive(wq, condition) \
618 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
619 schedule(); try_to_freeze())
620
621#define wait_event_freezable_exclusive(wq, condition) \
622({ \
623 int __ret = 0; \
624 might_sleep(); \
625 if (!(condition)) \
626 __ret = __wait_event_freezable_exclusive(wq, condition);\
627 __ret; \
628})
629
630
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200631#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
632({ \
633 int __ret = 0; \
634 DEFINE_WAIT(__wait); \
635 if (exclusive) \
636 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
637 do { \
638 if (likely(list_empty(&__wait.task_list))) \
639 __add_wait_queue_tail(&(wq), &__wait); \
640 set_current_state(TASK_INTERRUPTIBLE); \
641 if (signal_pending(current)) { \
642 __ret = -ERESTARTSYS; \
643 break; \
644 } \
645 if (irq) \
646 spin_unlock_irq(&(wq).lock); \
647 else \
648 spin_unlock(&(wq).lock); \
649 schedule(); \
650 if (irq) \
651 spin_lock_irq(&(wq).lock); \
652 else \
653 spin_lock(&(wq).lock); \
654 } while (!(condition)); \
655 __remove_wait_queue(&(wq), &__wait); \
656 __set_current_state(TASK_RUNNING); \
657 __ret; \
658})
659
660
661/**
662 * wait_event_interruptible_locked - sleep until a condition gets true
663 * @wq: the waitqueue to wait on
664 * @condition: a C expression for the event to wait for
665 *
666 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
667 * @condition evaluates to true or a signal is received.
668 * The @condition is checked each time the waitqueue @wq is woken up.
669 *
670 * It must be called with wq.lock being held. This spinlock is
671 * unlocked while sleeping but @condition testing is done while lock
672 * is held and when this macro exits the lock is held.
673 *
674 * The lock is locked/unlocked using spin_lock()/spin_unlock()
675 * functions which must match the way they are locked/unlocked outside
676 * of this macro.
677 *
678 * wake_up_locked() has to be called after changing any variable that could
679 * change the result of the wait condition.
680 *
681 * The function will return -ERESTARTSYS if it was interrupted by a
682 * signal and 0 if @condition evaluated to true.
683 */
684#define wait_event_interruptible_locked(wq, condition) \
685 ((condition) \
686 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
687
688/**
689 * wait_event_interruptible_locked_irq - sleep until a condition gets true
690 * @wq: the waitqueue to wait on
691 * @condition: a C expression for the event to wait for
692 *
693 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
694 * @condition evaluates to true or a signal is received.
695 * The @condition is checked each time the waitqueue @wq is woken up.
696 *
697 * It must be called with wq.lock being held. This spinlock is
698 * unlocked while sleeping but @condition testing is done while lock
699 * is held and when this macro exits the lock is held.
700 *
701 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
702 * functions which must match the way they are locked/unlocked outside
703 * of this macro.
704 *
705 * wake_up_locked() has to be called after changing any variable that could
706 * change the result of the wait condition.
707 *
708 * The function will return -ERESTARTSYS if it was interrupted by a
709 * signal and 0 if @condition evaluated to true.
710 */
711#define wait_event_interruptible_locked_irq(wq, condition) \
712 ((condition) \
713 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
714
715/**
716 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
717 * @wq: the waitqueue to wait on
718 * @condition: a C expression for the event to wait for
719 *
720 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
721 * @condition evaluates to true or a signal is received.
722 * The @condition is checked each time the waitqueue @wq is woken up.
723 *
724 * It must be called with wq.lock being held. This spinlock is
725 * unlocked while sleeping but @condition testing is done while lock
726 * is held and when this macro exits the lock is held.
727 *
728 * The lock is locked/unlocked using spin_lock()/spin_unlock()
729 * functions which must match the way they are locked/unlocked outside
730 * of this macro.
731 *
732 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
733 * set thus when other process waits process on the list if this
734 * process is awaken further processes are not considered.
735 *
736 * wake_up_locked() has to be called after changing any variable that could
737 * change the result of the wait condition.
738 *
739 * The function will return -ERESTARTSYS if it was interrupted by a
740 * signal and 0 if @condition evaluated to true.
741 */
742#define wait_event_interruptible_exclusive_locked(wq, condition) \
743 ((condition) \
744 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
745
746/**
747 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
748 * @wq: the waitqueue to wait on
749 * @condition: a C expression for the event to wait for
750 *
751 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
752 * @condition evaluates to true or a signal is received.
753 * The @condition is checked each time the waitqueue @wq is woken up.
754 *
755 * It must be called with wq.lock being held. This spinlock is
756 * unlocked while sleeping but @condition testing is done while lock
757 * is held and when this macro exits the lock is held.
758 *
759 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
760 * functions which must match the way they are locked/unlocked outside
761 * of this macro.
762 *
763 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
764 * set thus when other process waits process on the list if this
765 * process is awaken further processes are not considered.
766 *
767 * wake_up_locked() has to be called after changing any variable that could
768 * change the result of the wait condition.
769 *
770 * The function will return -ERESTARTSYS if it was interrupted by a
771 * signal and 0 if @condition evaluated to true.
772 */
773#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
774 ((condition) \
775 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
776
777
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200778#define __wait_event_killable(wq, condition) \
779 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500780
781/**
782 * wait_event_killable - sleep until a condition gets true
783 * @wq: the waitqueue to wait on
784 * @condition: a C expression for the event to wait for
785 *
786 * The process is put to sleep (TASK_KILLABLE) until the
787 * @condition evaluates to true or a signal is received.
788 * The @condition is checked each time the waitqueue @wq is woken up.
789 *
790 * wake_up() has to be called after changing any variable that could
791 * change the result of the wait condition.
792 *
793 * The function will return -ERESTARTSYS if it was interrupted by a
794 * signal and 0 if @condition evaluated to true.
795 */
796#define wait_event_killable(wq, condition) \
797({ \
798 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200799 might_sleep(); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500800 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200801 __ret = __wait_event_killable(wq, condition); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500802 __ret; \
803})
804
Lukas Czernereed8c022012-11-30 11:42:40 +0100805
806#define __wait_event_lock_irq(wq, condition, lock, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200807 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
808 spin_unlock_irq(&lock); \
809 cmd; \
810 schedule(); \
811 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100812
813/**
814 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
815 * condition is checked under the lock. This
816 * is expected to be called with the lock
817 * taken.
818 * @wq: the waitqueue to wait on
819 * @condition: a C expression for the event to wait for
820 * @lock: a locked spinlock_t, which will be released before cmd
821 * and schedule() and reacquired afterwards.
822 * @cmd: a command which is invoked outside the critical section before
823 * sleep
824 *
825 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
826 * @condition evaluates to true. The @condition is checked each time
827 * the waitqueue @wq is woken up.
828 *
829 * wake_up() has to be called after changing any variable that could
830 * change the result of the wait condition.
831 *
832 * This is supposed to be called while holding the lock. The lock is
833 * dropped before invoking the cmd and going to sleep and is reacquired
834 * afterwards.
835 */
836#define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
837do { \
838 if (condition) \
839 break; \
840 __wait_event_lock_irq(wq, condition, lock, cmd); \
841} while (0)
842
843/**
844 * wait_event_lock_irq - sleep until a condition gets true. The
845 * condition is checked under the lock. This
846 * is expected to be called with the lock
847 * taken.
848 * @wq: the waitqueue to wait on
849 * @condition: a C expression for the event to wait for
850 * @lock: a locked spinlock_t, which will be released before schedule()
851 * and reacquired afterwards.
852 *
853 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
854 * @condition evaluates to true. The @condition is checked each time
855 * the waitqueue @wq is woken up.
856 *
857 * wake_up() has to be called after changing any variable that could
858 * change the result of the wait condition.
859 *
860 * This is supposed to be called while holding the lock. The lock is
861 * dropped before going to sleep and is reacquired afterwards.
862 */
863#define wait_event_lock_irq(wq, condition, lock) \
864do { \
865 if (condition) \
866 break; \
867 __wait_event_lock_irq(wq, condition, lock, ); \
868} while (0)
869
870
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200871#define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200872 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200873 spin_unlock_irq(&lock); \
874 cmd; \
875 schedule(); \
Peter Zijlstra8fbd88f2013-10-02 11:22:28 +0200876 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100877
878/**
879 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
880 * The condition is checked under the lock. This is expected to
881 * be called with the lock taken.
882 * @wq: the waitqueue to wait on
883 * @condition: a C expression for the event to wait for
884 * @lock: a locked spinlock_t, which will be released before cmd and
885 * schedule() and reacquired afterwards.
886 * @cmd: a command which is invoked outside the critical section before
887 * sleep
888 *
889 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
890 * @condition evaluates to true or a signal is received. The @condition is
891 * checked each time the waitqueue @wq is woken up.
892 *
893 * wake_up() has to be called after changing any variable that could
894 * change the result of the wait condition.
895 *
896 * This is supposed to be called while holding the lock. The lock is
897 * dropped before invoking the cmd and going to sleep and is reacquired
898 * afterwards.
899 *
900 * The macro will return -ERESTARTSYS if it was interrupted by a signal
901 * and 0 if @condition evaluated to true.
902 */
903#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
904({ \
905 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100906 if (!(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200907 __ret = __wait_event_interruptible_lock_irq(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200908 condition, lock, cmd); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100909 __ret; \
910})
911
912/**
913 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
914 * The condition is checked under the lock. This is expected
915 * to be called with the lock taken.
916 * @wq: the waitqueue to wait on
917 * @condition: a C expression for the event to wait for
918 * @lock: a locked spinlock_t, which will be released before schedule()
919 * and reacquired afterwards.
920 *
921 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
922 * @condition evaluates to true or signal is received. The @condition is
923 * checked each time the waitqueue @wq is woken up.
924 *
925 * wake_up() has to be called after changing any variable that could
926 * change the result of the wait condition.
927 *
928 * This is supposed to be called while holding the lock. The lock is
929 * dropped before going to sleep and is reacquired afterwards.
930 *
931 * The macro will return -ERESTARTSYS if it was interrupted by a signal
932 * and 0 if @condition evaluated to true.
933 */
934#define wait_event_interruptible_lock_irq(wq, condition, lock) \
935({ \
936 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100937 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200938 __ret = __wait_event_interruptible_lock_irq(wq, \
Thierry Reding92ec1182013-10-23 13:40:55 +0200939 condition, lock,); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100940 __ret; \
941})
942
Ingo Molnarfb869b62013-10-04 10:24:49 +0200943#define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
944 lock, timeout) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200945 ___wait_event(wq, ___wait_cond_timeout(condition), \
Heiko Carstens7d716452013-10-31 12:48:14 +0100946 TASK_INTERRUPTIBLE, 0, timeout, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200947 spin_unlock_irq(&lock); \
948 __ret = schedule_timeout(__ret); \
Peter Zijlstraa1dc68522013-10-02 11:22:29 +0200949 spin_lock_irq(&lock));
Martin Peschked79ff142013-08-22 17:45:36 +0200950
951/**
Ingo Molnarfb869b62013-10-04 10:24:49 +0200952 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
953 * true or a timeout elapses. The condition is checked under
954 * the lock. This is expected to be called with the lock taken.
Martin Peschked79ff142013-08-22 17:45:36 +0200955 * @wq: the waitqueue to wait on
956 * @condition: a C expression for the event to wait for
957 * @lock: a locked spinlock_t, which will be released before schedule()
958 * and reacquired afterwards.
959 * @timeout: timeout, in jiffies
960 *
961 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
962 * @condition evaluates to true or signal is received. The @condition is
963 * checked each time the waitqueue @wq is woken up.
964 *
965 * wake_up() has to be called after changing any variable that could
966 * change the result of the wait condition.
967 *
968 * This is supposed to be called while holding the lock. The lock is
969 * dropped before going to sleep and is reacquired afterwards.
970 *
971 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
972 * was interrupted by a signal, and the remaining jiffies otherwise
973 * if the condition evaluated to true before the timeout elapsed.
974 */
975#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
976 timeout) \
977({ \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200978 long __ret = timeout; \
Oleg Nesterov89229152013-10-07 20:31:06 +0200979 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200980 __ret = __wait_event_interruptible_lock_irq_timeout( \
981 wq, condition, lock, timeout); \
Martin Peschked79ff142013-08-22 17:45:36 +0200982 __ret; \
983})
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/*
986 * Waitqueues which are removed from the waitqueue_head at wakeup time
987 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800988void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
989void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200990long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800991void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Ingo Molnarfb869b62013-10-04 10:24:49 +0200992void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
Peter Zijlstra61ada522014-09-24 10:18:47 +0200993long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
994int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
996int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
997
Eric Dumazetbf368e42009-04-28 02:24:21 -0700998#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -07001000 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -07001001 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +02001002 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
Eric Dumazetbf368e42009-04-28 02:24:21 -07001005#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007#define DEFINE_WAIT_BIT(name, word, bit) \
1008 struct wait_bit_queue name = { \
1009 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
1010 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -07001011 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 .func = wake_bit_function, \
1013 .task_list = \
1014 LIST_HEAD_INIT((name).wait.task_list), \
1015 }, \
1016 }
1017
1018#define init_wait(wait) \
1019 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -07001020 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 (wait)->func = autoremove_wake_function; \
1022 INIT_LIST_HEAD(&(wait)->task_list); \
Evgeny Kuznetsov231d0ae2010-10-05 12:47:57 +04001023 (wait)->flags = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 } while (0)
1025
NeilBrown74316202014-07-07 15:16:04 +10001026
Peter Zijlstradfd01f02015-12-13 22:11:16 +01001027extern int bit_wait(struct wait_bit_key *, int);
1028extern int bit_wait_io(struct wait_bit_key *, int);
1029extern int bit_wait_timeout(struct wait_bit_key *, int);
1030extern int bit_wait_io_timeout(struct wait_bit_key *, int);
NeilBrown74316202014-07-07 15:16:04 +10001031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032/**
1033 * wait_on_bit - wait for a bit to be cleared
1034 * @word: the word being waited on, a kernel virtual address
1035 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 * @mode: the task state to sleep in
1037 *
1038 * There is a standard hashed waitqueue table for generic use. This
1039 * is the part of the hashtable's accessor API that waits on a bit.
1040 * For instance, if one were to have waiters on a bitflag, one would
1041 * call wait_on_bit() in threads waiting for the bit to clear.
1042 * One uses wait_on_bit() where one is waiting for the bit to clear,
1043 * but has no intention of setting it.
NeilBrown74316202014-07-07 15:16:04 +10001044 * Returned value will be zero if the bit was cleared, or non-zero
1045 * if the process received a signal and the mode permitted wakeup
1046 * on that signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
Ingo Molnarfb869b62013-10-04 10:24:49 +02001048static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001049wait_on_bit(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001050{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001051 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001052 if (!test_bit(bit, word))
1053 return 0;
1054 return out_of_line_wait_on_bit(word, bit,
1055 bit_wait,
1056 mode);
1057}
1058
1059/**
1060 * wait_on_bit_io - wait for a bit to be cleared
1061 * @word: the word being waited on, a kernel virtual address
1062 * @bit: the bit of the word being waited on
1063 * @mode: the task state to sleep in
1064 *
1065 * Use the standard hashed waitqueue table to wait for a bit
1066 * to be cleared. This is similar to wait_on_bit(), but calls
1067 * io_schedule() instead of schedule() for the actual waiting.
1068 *
1069 * Returned value will be zero if the bit was cleared, or non-zero
1070 * if the process received a signal and the mode permitted wakeup
1071 * on that signal.
1072 */
1073static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001074wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001075{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001076 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001077 if (!test_bit(bit, word))
1078 return 0;
1079 return out_of_line_wait_on_bit(word, bit,
1080 bit_wait_io,
1081 mode);
1082}
1083
1084/**
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001085 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
1086 * @word: the word being waited on, a kernel virtual address
1087 * @bit: the bit of the word being waited on
1088 * @mode: the task state to sleep in
1089 * @timeout: timeout, in jiffies
1090 *
1091 * Use the standard hashed waitqueue table to wait for a bit
1092 * to be cleared. This is similar to wait_on_bit(), except also takes a
1093 * timeout parameter.
1094 *
1095 * Returned value will be zero if the bit was cleared before the
1096 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1097 * received a signal and the mode permitted wakeup on that signal.
1098 */
1099static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001100wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
1101 unsigned long timeout)
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001102{
1103 might_sleep();
1104 if (!test_bit(bit, word))
1105 return 0;
1106 return out_of_line_wait_on_bit_timeout(word, bit,
1107 bit_wait_timeout,
1108 mode, timeout);
1109}
1110
1111/**
NeilBrown74316202014-07-07 15:16:04 +10001112 * wait_on_bit_action - wait for a bit to be cleared
1113 * @word: the word being waited on, a kernel virtual address
1114 * @bit: the bit of the word being waited on
1115 * @action: the function used to sleep, which may take special actions
1116 * @mode: the task state to sleep in
1117 *
1118 * Use the standard hashed waitqueue table to wait for a bit
1119 * to be cleared, and allow the waiting action to be specified.
1120 * This is like wait_on_bit() but allows fine control of how the waiting
1121 * is done.
1122 *
1123 * Returned value will be zero if the bit was cleared, or non-zero
1124 * if the process received a signal and the mode permitted wakeup
1125 * on that signal.
1126 */
1127static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001128wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
1129 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001131 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (!test_bit(bit, word))
1133 return 0;
1134 return out_of_line_wait_on_bit(word, bit, action, mode);
1135}
1136
1137/**
1138 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1139 * @word: the word being waited on, a kernel virtual address
1140 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 * @mode: the task state to sleep in
1142 *
1143 * There is a standard hashed waitqueue table for generic use. This
1144 * is the part of the hashtable's accessor API that waits on a bit
1145 * when one intends to set it, for instance, trying to lock bitflags.
1146 * For instance, if one were to have waiters trying to set bitflag
1147 * and waiting for it to clear before setting it, one would call
1148 * wait_on_bit() in threads waiting to be able to set the bit.
1149 * One uses wait_on_bit_lock() where one is waiting for the bit to
1150 * clear with the intention of setting it, and when done, clearing it.
NeilBrown74316202014-07-07 15:16:04 +10001151 *
1152 * Returns zero if the bit was (eventually) found to be clear and was
1153 * set. Returns non-zero if a signal was delivered to the process and
1154 * the @mode allows that signal to wake the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
Ingo Molnarfb869b62013-10-04 10:24:49 +02001156static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001157wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001158{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001159 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001160 if (!test_and_set_bit(bit, word))
1161 return 0;
1162 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1163}
1164
1165/**
1166 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1167 * @word: the word being waited on, a kernel virtual address
1168 * @bit: the bit of the word being waited on
1169 * @mode: the task state to sleep in
1170 *
1171 * Use the standard hashed waitqueue table to wait for a bit
1172 * to be cleared and then to atomically set it. This is similar
1173 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1174 * for the actual waiting.
1175 *
1176 * Returns zero if the bit was (eventually) found to be clear and was
1177 * set. Returns non-zero if a signal was delivered to the process and
1178 * the @mode allows that signal to wake the process.
1179 */
1180static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001181wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001182{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001183 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001184 if (!test_and_set_bit(bit, word))
1185 return 0;
1186 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1187}
1188
1189/**
1190 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1191 * @word: the word being waited on, a kernel virtual address
1192 * @bit: the bit of the word being waited on
1193 * @action: the function used to sleep, which may take special actions
1194 * @mode: the task state to sleep in
1195 *
1196 * Use the standard hashed waitqueue table to wait for a bit
1197 * to be cleared and then to set it, and allow the waiting action
1198 * to be specified.
1199 * This is like wait_on_bit() but allows fine control of how the waiting
1200 * is done.
1201 *
1202 * Returns zero if the bit was (eventually) found to be clear and was
1203 * set. Returns non-zero if a signal was delivered to the process and
1204 * the @mode allows that signal to wake the process.
1205 */
1206static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001207wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
1208 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001210 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (!test_and_set_bit(bit, word))
1212 return 0;
1213 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1214}
David Howellscb655372013-05-10 19:50:26 +01001215
1216/**
1217 * wait_on_atomic_t - Wait for an atomic_t to become 0
1218 * @val: The atomic value being waited on, a kernel virtual address
1219 * @action: the function used to sleep, which may take special actions
1220 * @mode: the task state to sleep in
1221 *
1222 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1223 * the purpose of getting a waitqueue, but we set the key to a bit number
1224 * outside of the target 'word'.
1225 */
1226static inline
1227int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1228{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001229 might_sleep();
David Howellscb655372013-05-10 19:50:26 +01001230 if (atomic_read(val) == 0)
1231 return 0;
1232 return out_of_line_wait_on_atomic_t(val, action, mode);
1233}
Ingo Molnarfb869b62013-10-04 10:24:49 +02001234
1235#endif /* _LINUX_WAIT_H */