blob: 19c75f9545cef5f2e56ce9fa8ebd9ac9ceeaffc4 [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; \
Oleg Nesterovb1ea06a2016-09-08 18:48:15 +0200283 goto __out; \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200284 } \
285 \
286 cmd; \
287 } \
288 finish_wait(&wq, &__wait); \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200289__out: __ret; \
290})
Peter Zijlstra41a14312013-10-02 11:22:21 +0200291
Ingo Molnarfb869b62013-10-04 10:24:49 +0200292#define __wait_event(wq, condition) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200293 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
294 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296/**
297 * wait_event - sleep until a condition gets true
298 * @wq: the waitqueue to wait on
299 * @condition: a C expression for the event to wait for
300 *
301 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
302 * @condition evaluates to true. The @condition is checked each time
303 * the waitqueue @wq is woken up.
304 *
305 * wake_up() has to be called after changing any variable that could
306 * change the result of the wait condition.
307 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200308#define wait_event(wq, condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309do { \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200310 might_sleep(); \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200311 if (condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 break; \
313 __wait_event(wq, condition); \
314} while (0)
315
Peter Zijlstra2c561242015-02-03 12:55:31 +0100316#define __io_wait_event(wq, condition) \
317 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
318 io_schedule())
319
320/*
321 * io_wait_event() -- like wait_event() but with io_schedule()
322 */
323#define io_wait_event(wq, condition) \
324do { \
325 might_sleep(); \
326 if (condition) \
327 break; \
328 __io_wait_event(wq, condition); \
329} while (0)
330
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100331#define __wait_event_freezable(wq, condition) \
332 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
333 schedule(); try_to_freeze())
334
335/**
Stafford Hornef4bcfa12016-02-23 22:39:28 +0900336 * wait_event_freezable - sleep (or freeze) until a condition gets true
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100337 * @wq: the waitqueue to wait on
338 * @condition: a C expression for the event to wait for
339 *
340 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
341 * to system load) until the @condition evaluates to true. The
342 * @condition is checked each time the waitqueue @wq is woken up.
343 *
344 * wake_up() has to be called after changing any variable that could
345 * change the result of the wait condition.
346 */
347#define wait_event_freezable(wq, condition) \
348({ \
349 int __ret = 0; \
350 might_sleep(); \
351 if (!(condition)) \
352 __ret = __wait_event_freezable(wq, condition); \
353 __ret; \
354})
355
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200356#define __wait_event_timeout(wq, condition, timeout) \
357 ___wait_event(wq, ___wait_cond_timeout(condition), \
358 TASK_UNINTERRUPTIBLE, 0, timeout, \
359 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361/**
362 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
363 * @wq: the waitqueue to wait on
364 * @condition: a C expression for the event to wait for
365 * @timeout: timeout, in jiffies
366 *
367 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
368 * @condition evaluates to true. The @condition is checked each time
369 * the waitqueue @wq is woken up.
370 *
371 * wake_up() has to be called after changing any variable that could
372 * change the result of the wait condition.
373 *
Scot Doyle6b44f512014-08-24 17:12:27 +0000374 * Returns:
375 * 0 if the @condition evaluated to %false after the @timeout elapsed,
376 * 1 if the @condition evaluated to %true after the @timeout elapsed,
377 * or the remaining jiffies (at least 1) if the @condition evaluated
378 * to %true before the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
380#define wait_event_timeout(wq, condition, timeout) \
381({ \
382 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200383 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200384 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200385 __ret = __wait_event_timeout(wq, condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 __ret; \
387})
388
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100389#define __wait_event_freezable_timeout(wq, condition, timeout) \
390 ___wait_event(wq, ___wait_cond_timeout(condition), \
391 TASK_INTERRUPTIBLE, 0, timeout, \
392 __ret = schedule_timeout(__ret); try_to_freeze())
393
394/*
395 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
396 * increasing load and is freezable.
397 */
398#define wait_event_freezable_timeout(wq, condition, timeout) \
399({ \
400 long __ret = timeout; \
401 might_sleep(); \
402 if (!___wait_cond_timeout(condition)) \
403 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
404 __ret; \
405})
406
Yuanhan Liu9f3520c2015-05-08 18:19:05 +1000407#define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
408 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
409 cmd1; schedule(); cmd2)
410/*
411 * Just like wait_event_cmd(), except it sets exclusive flag
412 */
413#define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
414do { \
415 if (condition) \
416 break; \
417 __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2); \
418} while (0)
419
Shaohua Li82e06c82013-11-14 15:16:16 +1100420#define __wait_event_cmd(wq, condition, cmd1, cmd2) \
421 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
422 cmd1; schedule(); cmd2)
423
424/**
425 * wait_event_cmd - sleep until a condition gets true
426 * @wq: the waitqueue to wait on
427 * @condition: a C expression for the event to wait for
Masanari Iidaf434f7a2014-01-22 01:22:06 +0900428 * @cmd1: the command will be executed before sleep
429 * @cmd2: the command will be executed after sleep
Shaohua Li82e06c82013-11-14 15:16:16 +1100430 *
431 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
432 * @condition evaluates to true. The @condition is checked each time
433 * the waitqueue @wq is woken up.
434 *
435 * wake_up() has to be called after changing any variable that could
436 * change the result of the wait condition.
437 */
438#define wait_event_cmd(wq, condition, cmd1, cmd2) \
439do { \
440 if (condition) \
441 break; \
442 __wait_event_cmd(wq, condition, cmd1, cmd2); \
443} while (0)
444
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200445#define __wait_event_interruptible(wq, condition) \
446 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstraf13f4c42013-10-02 11:22:24 +0200447 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449/**
450 * wait_event_interruptible - sleep until a condition gets true
451 * @wq: the waitqueue to wait on
452 * @condition: a C expression for the event to wait for
453 *
454 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
455 * @condition evaluates to true or a signal is received.
456 * The @condition is checked each time the waitqueue @wq is woken up.
457 *
458 * wake_up() has to be called after changing any variable that could
459 * change the result of the wait condition.
460 *
461 * The function will return -ERESTARTSYS if it was interrupted by a
462 * signal and 0 if @condition evaluated to true.
463 */
464#define wait_event_interruptible(wq, condition) \
465({ \
466 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200467 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200469 __ret = __wait_event_interruptible(wq, condition); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 __ret; \
471})
472
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200473#define __wait_event_interruptible_timeout(wq, condition, timeout) \
474 ___wait_event(wq, ___wait_cond_timeout(condition), \
475 TASK_INTERRUPTIBLE, 0, timeout, \
476 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478/**
479 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
480 * @wq: the waitqueue to wait on
481 * @condition: a C expression for the event to wait for
482 * @timeout: timeout, in jiffies
483 *
484 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
485 * @condition evaluates to true or a signal is received.
486 * The @condition is checked each time the waitqueue @wq is woken up.
487 *
488 * wake_up() has to be called after changing any variable that could
489 * change the result of the wait condition.
490 *
Imre Deak4c663cf2013-05-24 15:55:09 -0700491 * Returns:
Scot Doyle6b44f512014-08-24 17:12:27 +0000492 * 0 if the @condition evaluated to %false after the @timeout elapsed,
493 * 1 if the @condition evaluated to %true after the @timeout elapsed,
494 * the remaining jiffies (at least 1) if the @condition evaluated
495 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
496 * interrupted by a signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
498#define wait_event_interruptible_timeout(wq, condition, timeout) \
499({ \
500 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200501 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200502 if (!___wait_cond_timeout(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200503 __ret = __wait_event_interruptible_timeout(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200504 condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 __ret; \
506})
507
Kent Overstreet774a08b2013-05-07 16:18:43 -0700508#define __wait_event_hrtimeout(wq, condition, timeout, state) \
509({ \
510 int __ret = 0; \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700511 struct hrtimer_sleeper __t; \
512 \
513 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
514 HRTIMER_MODE_REL); \
515 hrtimer_init_sleeper(&__t, current); \
516 if ((timeout).tv64 != KTIME_MAX) \
517 hrtimer_start_range_ns(&__t.timer, timeout, \
518 current->timer_slack_ns, \
519 HRTIMER_MODE_REL); \
520 \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200521 __ret = ___wait_event(wq, condition, state, 0, 0, \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700522 if (!__t.task) { \
523 __ret = -ETIME; \
524 break; \
525 } \
Peter Zijlstraebdc1952013-10-02 11:22:32 +0200526 schedule()); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700527 \
528 hrtimer_cancel(&__t.timer); \
529 destroy_hrtimer_on_stack(&__t.timer); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700530 __ret; \
531})
532
533/**
534 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
535 * @wq: the waitqueue to wait on
536 * @condition: a C expression for the event to wait for
537 * @timeout: timeout, as a ktime_t
538 *
539 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
540 * @condition evaluates to true or a signal is received.
541 * The @condition is checked each time the waitqueue @wq is woken up.
542 *
543 * wake_up() has to be called after changing any variable that could
544 * change the result of the wait condition.
545 *
546 * The function returns 0 if @condition became true, or -ETIME if the timeout
547 * elapsed.
548 */
549#define wait_event_hrtimeout(wq, condition, timeout) \
550({ \
551 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200552 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700553 if (!(condition)) \
554 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
555 TASK_UNINTERRUPTIBLE); \
556 __ret; \
557})
558
559/**
560 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
561 * @wq: the waitqueue to wait on
562 * @condition: a C expression for the event to wait for
563 * @timeout: timeout, as a ktime_t
564 *
565 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
566 * @condition evaluates to true or a signal is received.
567 * The @condition is checked each time the waitqueue @wq is woken up.
568 *
569 * wake_up() has to be called after changing any variable that could
570 * change the result of the wait condition.
571 *
572 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
573 * interrupted by a signal, or -ETIME if the timeout elapsed.
574 */
575#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
576({ \
577 long __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200578 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700579 if (!(condition)) \
580 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
581 TASK_INTERRUPTIBLE); \
582 __ret; \
583})
584
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200585#define __wait_event_interruptible_exclusive(wq, condition) \
586 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
Peter Zijlstra48c25212013-10-02 11:22:26 +0200587 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589#define wait_event_interruptible_exclusive(wq, condition) \
590({ \
591 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200592 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200594 __ret = __wait_event_interruptible_exclusive(wq, condition);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 __ret; \
596})
597
Al Viro6a0fb302016-07-19 03:04:34 -0400598#define __wait_event_killable_exclusive(wq, condition) \
599 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
600 schedule())
601
602#define wait_event_killable_exclusive(wq, condition) \
603({ \
604 int __ret = 0; \
605 might_sleep(); \
606 if (!(condition)) \
607 __ret = __wait_event_killable_exclusive(wq, condition); \
608 __ret; \
609})
610
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200611
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100612#define __wait_event_freezable_exclusive(wq, condition) \
613 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
614 schedule(); try_to_freeze())
615
616#define wait_event_freezable_exclusive(wq, condition) \
617({ \
618 int __ret = 0; \
619 might_sleep(); \
620 if (!(condition)) \
621 __ret = __wait_event_freezable_exclusive(wq, condition);\
622 __ret; \
623})
624
625
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200626#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
627({ \
628 int __ret = 0; \
629 DEFINE_WAIT(__wait); \
630 if (exclusive) \
631 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
632 do { \
633 if (likely(list_empty(&__wait.task_list))) \
634 __add_wait_queue_tail(&(wq), &__wait); \
635 set_current_state(TASK_INTERRUPTIBLE); \
636 if (signal_pending(current)) { \
637 __ret = -ERESTARTSYS; \
638 break; \
639 } \
640 if (irq) \
641 spin_unlock_irq(&(wq).lock); \
642 else \
643 spin_unlock(&(wq).lock); \
644 schedule(); \
645 if (irq) \
646 spin_lock_irq(&(wq).lock); \
647 else \
648 spin_lock(&(wq).lock); \
649 } while (!(condition)); \
650 __remove_wait_queue(&(wq), &__wait); \
651 __set_current_state(TASK_RUNNING); \
652 __ret; \
653})
654
655
656/**
657 * wait_event_interruptible_locked - sleep until a condition gets true
658 * @wq: the waitqueue to wait on
659 * @condition: a C expression for the event to wait for
660 *
661 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
662 * @condition evaluates to true or a signal is received.
663 * The @condition is checked each time the waitqueue @wq is woken up.
664 *
665 * It must be called with wq.lock being held. This spinlock is
666 * unlocked while sleeping but @condition testing is done while lock
667 * is held and when this macro exits the lock is held.
668 *
669 * The lock is locked/unlocked using spin_lock()/spin_unlock()
670 * functions which must match the way they are locked/unlocked outside
671 * of this macro.
672 *
673 * wake_up_locked() has to be called after changing any variable that could
674 * change the result of the wait condition.
675 *
676 * The function will return -ERESTARTSYS if it was interrupted by a
677 * signal and 0 if @condition evaluated to true.
678 */
679#define wait_event_interruptible_locked(wq, condition) \
680 ((condition) \
681 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
682
683/**
684 * wait_event_interruptible_locked_irq - sleep until a condition gets true
685 * @wq: the waitqueue to wait on
686 * @condition: a C expression for the event to wait for
687 *
688 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
689 * @condition evaluates to true or a signal is received.
690 * The @condition is checked each time the waitqueue @wq is woken up.
691 *
692 * It must be called with wq.lock being held. This spinlock is
693 * unlocked while sleeping but @condition testing is done while lock
694 * is held and when this macro exits the lock is held.
695 *
696 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
697 * functions which must match the way they are locked/unlocked outside
698 * of this macro.
699 *
700 * wake_up_locked() has to be called after changing any variable that could
701 * change the result of the wait condition.
702 *
703 * The function will return -ERESTARTSYS if it was interrupted by a
704 * signal and 0 if @condition evaluated to true.
705 */
706#define wait_event_interruptible_locked_irq(wq, condition) \
707 ((condition) \
708 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
709
710/**
711 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
712 * @wq: the waitqueue to wait on
713 * @condition: a C expression for the event to wait for
714 *
715 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
716 * @condition evaluates to true or a signal is received.
717 * The @condition is checked each time the waitqueue @wq is woken up.
718 *
719 * It must be called with wq.lock being held. This spinlock is
720 * unlocked while sleeping but @condition testing is done while lock
721 * is held and when this macro exits the lock is held.
722 *
723 * The lock is locked/unlocked using spin_lock()/spin_unlock()
724 * functions which must match the way they are locked/unlocked outside
725 * of this macro.
726 *
727 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
728 * set thus when other process waits process on the list if this
729 * process is awaken further processes are not considered.
730 *
731 * wake_up_locked() has to be called after changing any variable that could
732 * change the result of the wait condition.
733 *
734 * The function will return -ERESTARTSYS if it was interrupted by a
735 * signal and 0 if @condition evaluated to true.
736 */
737#define wait_event_interruptible_exclusive_locked(wq, condition) \
738 ((condition) \
739 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
740
741/**
742 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
743 * @wq: the waitqueue to wait on
744 * @condition: a C expression for the event to wait for
745 *
746 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
747 * @condition evaluates to true or a signal is received.
748 * The @condition is checked each time the waitqueue @wq is woken up.
749 *
750 * It must be called with wq.lock being held. This spinlock is
751 * unlocked while sleeping but @condition testing is done while lock
752 * is held and when this macro exits the lock is held.
753 *
754 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
755 * functions which must match the way they are locked/unlocked outside
756 * of this macro.
757 *
758 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
759 * set thus when other process waits process on the list if this
760 * process is awaken further processes are not considered.
761 *
762 * wake_up_locked() has to be called after changing any variable that could
763 * change the result of the wait condition.
764 *
765 * The function will return -ERESTARTSYS if it was interrupted by a
766 * signal and 0 if @condition evaluated to true.
767 */
768#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
769 ((condition) \
770 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
771
772
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200773#define __wait_event_killable(wq, condition) \
774 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500775
776/**
777 * wait_event_killable - sleep until a condition gets true
778 * @wq: the waitqueue to wait on
779 * @condition: a C expression for the event to wait for
780 *
781 * The process is put to sleep (TASK_KILLABLE) until the
782 * @condition evaluates to true or a signal is received.
783 * The @condition is checked each time the waitqueue @wq is woken up.
784 *
785 * wake_up() has to be called after changing any variable that could
786 * change the result of the wait condition.
787 *
788 * The function will return -ERESTARTSYS if it was interrupted by a
789 * signal and 0 if @condition evaluated to true.
790 */
791#define wait_event_killable(wq, condition) \
792({ \
793 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200794 might_sleep(); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500795 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200796 __ret = __wait_event_killable(wq, condition); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500797 __ret; \
798})
799
Lukas Czernereed8c022012-11-30 11:42:40 +0100800
801#define __wait_event_lock_irq(wq, condition, lock, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200802 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
803 spin_unlock_irq(&lock); \
804 cmd; \
805 schedule(); \
806 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100807
808/**
809 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
810 * condition is checked under the lock. This
811 * is expected to be called with the lock
812 * taken.
813 * @wq: the waitqueue to wait on
814 * @condition: a C expression for the event to wait for
815 * @lock: a locked spinlock_t, which will be released before cmd
816 * and schedule() and reacquired afterwards.
817 * @cmd: a command which is invoked outside the critical section before
818 * sleep
819 *
820 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
821 * @condition evaluates to true. The @condition is checked each time
822 * the waitqueue @wq is woken up.
823 *
824 * wake_up() has to be called after changing any variable that could
825 * change the result of the wait condition.
826 *
827 * This is supposed to be called while holding the lock. The lock is
828 * dropped before invoking the cmd and going to sleep and is reacquired
829 * afterwards.
830 */
831#define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
832do { \
833 if (condition) \
834 break; \
835 __wait_event_lock_irq(wq, condition, lock, cmd); \
836} while (0)
837
838/**
839 * wait_event_lock_irq - sleep until a condition gets true. The
840 * condition is checked under the lock. This
841 * is expected to be called with the lock
842 * taken.
843 * @wq: the waitqueue to wait on
844 * @condition: a C expression for the event to wait for
845 * @lock: a locked spinlock_t, which will be released before schedule()
846 * and reacquired afterwards.
847 *
848 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
849 * @condition evaluates to true. The @condition is checked each time
850 * the waitqueue @wq is woken up.
851 *
852 * wake_up() has to be called after changing any variable that could
853 * change the result of the wait condition.
854 *
855 * This is supposed to be called while holding the lock. The lock is
856 * dropped before going to sleep and is reacquired afterwards.
857 */
858#define wait_event_lock_irq(wq, condition, lock) \
859do { \
860 if (condition) \
861 break; \
862 __wait_event_lock_irq(wq, condition, lock, ); \
863} while (0)
864
865
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200866#define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200867 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200868 spin_unlock_irq(&lock); \
869 cmd; \
870 schedule(); \
Peter Zijlstra8fbd88f2013-10-02 11:22:28 +0200871 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100872
873/**
874 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
875 * The condition is checked under the lock. This is expected to
876 * be called with the lock taken.
877 * @wq: the waitqueue to wait on
878 * @condition: a C expression for the event to wait for
879 * @lock: a locked spinlock_t, which will be released before cmd and
880 * schedule() and reacquired afterwards.
881 * @cmd: a command which is invoked outside the critical section before
882 * sleep
883 *
884 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
885 * @condition evaluates to true or a signal is received. The @condition is
886 * checked each time the waitqueue @wq is woken up.
887 *
888 * wake_up() has to be called after changing any variable that could
889 * change the result of the wait condition.
890 *
891 * This is supposed to be called while holding the lock. The lock is
892 * dropped before invoking the cmd and going to sleep and is reacquired
893 * afterwards.
894 *
895 * The macro will return -ERESTARTSYS if it was interrupted by a signal
896 * and 0 if @condition evaluated to true.
897 */
898#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
899({ \
900 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100901 if (!(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200902 __ret = __wait_event_interruptible_lock_irq(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200903 condition, lock, cmd); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100904 __ret; \
905})
906
907/**
908 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
909 * The condition is checked under the lock. This is expected
910 * to be called with the lock taken.
911 * @wq: the waitqueue to wait on
912 * @condition: a C expression for the event to wait for
913 * @lock: a locked spinlock_t, which will be released before schedule()
914 * and reacquired afterwards.
915 *
916 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
917 * @condition evaluates to true or signal is received. The @condition is
918 * checked each time the waitqueue @wq is woken up.
919 *
920 * wake_up() has to be called after changing any variable that could
921 * change the result of the wait condition.
922 *
923 * This is supposed to be called while holding the lock. The lock is
924 * dropped before going to sleep and is reacquired afterwards.
925 *
926 * The macro will return -ERESTARTSYS if it was interrupted by a signal
927 * and 0 if @condition evaluated to true.
928 */
929#define wait_event_interruptible_lock_irq(wq, condition, lock) \
930({ \
931 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100932 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200933 __ret = __wait_event_interruptible_lock_irq(wq, \
Thierry Reding92ec1182013-10-23 13:40:55 +0200934 condition, lock,); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100935 __ret; \
936})
937
Ingo Molnarfb869b62013-10-04 10:24:49 +0200938#define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
939 lock, timeout) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200940 ___wait_event(wq, ___wait_cond_timeout(condition), \
Heiko Carstens7d716452013-10-31 12:48:14 +0100941 TASK_INTERRUPTIBLE, 0, timeout, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200942 spin_unlock_irq(&lock); \
943 __ret = schedule_timeout(__ret); \
Peter Zijlstraa1dc6852013-10-02 11:22:29 +0200944 spin_lock_irq(&lock));
Martin Peschked79ff142013-08-22 17:45:36 +0200945
946/**
Ingo Molnarfb869b62013-10-04 10:24:49 +0200947 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
948 * true or a timeout elapses. The condition is checked under
949 * the lock. This is expected to be called with the lock taken.
Martin Peschked79ff142013-08-22 17:45:36 +0200950 * @wq: the waitqueue to wait on
951 * @condition: a C expression for the event to wait for
952 * @lock: a locked spinlock_t, which will be released before schedule()
953 * and reacquired afterwards.
954 * @timeout: timeout, in jiffies
955 *
956 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
957 * @condition evaluates to true or signal is received. The @condition is
958 * checked each time the waitqueue @wq is woken up.
959 *
960 * wake_up() has to be called after changing any variable that could
961 * change the result of the wait condition.
962 *
963 * This is supposed to be called while holding the lock. The lock is
964 * dropped before going to sleep and is reacquired afterwards.
965 *
966 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
967 * was interrupted by a signal, and the remaining jiffies otherwise
968 * if the condition evaluated to true before the timeout elapsed.
969 */
970#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
971 timeout) \
972({ \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200973 long __ret = timeout; \
Oleg Nesterov89229152013-10-07 20:31:06 +0200974 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200975 __ret = __wait_event_interruptible_lock_irq_timeout( \
976 wq, condition, lock, timeout); \
Martin Peschked79ff142013-08-22 17:45:36 +0200977 __ret; \
978})
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980/*
981 * Waitqueues which are removed from the waitqueue_head at wakeup time
982 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800983void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
984void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200985long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800986void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Peter Zijlstra61ada522014-09-24 10:18:47 +0200987long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
988int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
990int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
991
Eric Dumazetbf368e42009-04-28 02:24:21 -0700992#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700994 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -0700995 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200996 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998
Eric Dumazetbf368e42009-04-28 02:24:21 -0700999#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001#define DEFINE_WAIT_BIT(name, word, bit) \
1002 struct wait_bit_queue name = { \
1003 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
1004 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -07001005 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 .func = wake_bit_function, \
1007 .task_list = \
1008 LIST_HEAD_INIT((name).wait.task_list), \
1009 }, \
1010 }
1011
1012#define init_wait(wait) \
1013 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -07001014 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 (wait)->func = autoremove_wake_function; \
1016 INIT_LIST_HEAD(&(wait)->task_list); \
Evgeny Kuznetsov231d0ae2010-10-05 12:47:57 +04001017 (wait)->flags = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 } while (0)
1019
NeilBrown74316202014-07-07 15:16:04 +10001020
Peter Zijlstradfd01f02015-12-13 22:11:16 +01001021extern int bit_wait(struct wait_bit_key *, int);
1022extern int bit_wait_io(struct wait_bit_key *, int);
1023extern int bit_wait_timeout(struct wait_bit_key *, int);
1024extern int bit_wait_io_timeout(struct wait_bit_key *, int);
NeilBrown74316202014-07-07 15:16:04 +10001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026/**
1027 * wait_on_bit - wait for a bit to be cleared
1028 * @word: the word being waited on, a kernel virtual address
1029 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 * @mode: the task state to sleep in
1031 *
1032 * There is a standard hashed waitqueue table for generic use. This
1033 * is the part of the hashtable's accessor API that waits on a bit.
1034 * For instance, if one were to have waiters on a bitflag, one would
1035 * call wait_on_bit() in threads waiting for the bit to clear.
1036 * One uses wait_on_bit() where one is waiting for the bit to clear,
1037 * but has no intention of setting it.
NeilBrown74316202014-07-07 15:16:04 +10001038 * Returned value will be zero if the bit was cleared, or non-zero
1039 * if the process received a signal and the mode permitted wakeup
1040 * on that signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
Ingo Molnarfb869b62013-10-04 10:24:49 +02001042static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001043wait_on_bit(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001044{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001045 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001046 if (!test_bit(bit, word))
1047 return 0;
1048 return out_of_line_wait_on_bit(word, bit,
1049 bit_wait,
1050 mode);
1051}
1052
1053/**
1054 * wait_on_bit_io - wait for a bit to be cleared
1055 * @word: the word being waited on, a kernel virtual address
1056 * @bit: the bit of the word being waited on
1057 * @mode: the task state to sleep in
1058 *
1059 * Use the standard hashed waitqueue table to wait for a bit
1060 * to be cleared. This is similar to wait_on_bit(), but calls
1061 * io_schedule() instead of schedule() for the actual waiting.
1062 *
1063 * Returned value will be zero if the bit was cleared, or non-zero
1064 * if the process received a signal and the mode permitted wakeup
1065 * on that signal.
1066 */
1067static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001068wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001069{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001070 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001071 if (!test_bit(bit, word))
1072 return 0;
1073 return out_of_line_wait_on_bit(word, bit,
1074 bit_wait_io,
1075 mode);
1076}
1077
1078/**
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001079 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
1080 * @word: the word being waited on, a kernel virtual address
1081 * @bit: the bit of the word being waited on
1082 * @mode: the task state to sleep in
1083 * @timeout: timeout, in jiffies
1084 *
1085 * Use the standard hashed waitqueue table to wait for a bit
1086 * to be cleared. This is similar to wait_on_bit(), except also takes a
1087 * timeout parameter.
1088 *
1089 * Returned value will be zero if the bit was cleared before the
1090 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1091 * received a signal and the mode permitted wakeup on that signal.
1092 */
1093static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001094wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
1095 unsigned long timeout)
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001096{
1097 might_sleep();
1098 if (!test_bit(bit, word))
1099 return 0;
1100 return out_of_line_wait_on_bit_timeout(word, bit,
1101 bit_wait_timeout,
1102 mode, timeout);
1103}
1104
1105/**
NeilBrown74316202014-07-07 15:16:04 +10001106 * wait_on_bit_action - wait for a bit to be cleared
1107 * @word: the word being waited on, a kernel virtual address
1108 * @bit: the bit of the word being waited on
1109 * @action: the function used to sleep, which may take special actions
1110 * @mode: the task state to sleep in
1111 *
1112 * Use the standard hashed waitqueue table to wait for a bit
1113 * to be cleared, and allow the waiting action to be specified.
1114 * This is like wait_on_bit() but allows fine control of how the waiting
1115 * is done.
1116 *
1117 * Returned value will be zero if the bit was cleared, or non-zero
1118 * if the process received a signal and the mode permitted wakeup
1119 * on that signal.
1120 */
1121static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001122wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
1123 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001125 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!test_bit(bit, word))
1127 return 0;
1128 return out_of_line_wait_on_bit(word, bit, action, mode);
1129}
1130
1131/**
1132 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1133 * @word: the word being waited on, a kernel virtual address
1134 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 * @mode: the task state to sleep in
1136 *
1137 * There is a standard hashed waitqueue table for generic use. This
1138 * is the part of the hashtable's accessor API that waits on a bit
1139 * when one intends to set it, for instance, trying to lock bitflags.
1140 * For instance, if one were to have waiters trying to set bitflag
1141 * and waiting for it to clear before setting it, one would call
1142 * wait_on_bit() in threads waiting to be able to set the bit.
1143 * One uses wait_on_bit_lock() where one is waiting for the bit to
1144 * clear with the intention of setting it, and when done, clearing it.
NeilBrown74316202014-07-07 15:16:04 +10001145 *
1146 * Returns zero if the bit was (eventually) found to be clear and was
1147 * set. Returns non-zero if a signal was delivered to the process and
1148 * the @mode allows that signal to wake the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 */
Ingo Molnarfb869b62013-10-04 10:24:49 +02001150static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001151wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001152{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001153 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001154 if (!test_and_set_bit(bit, word))
1155 return 0;
1156 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1157}
1158
1159/**
1160 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1161 * @word: the word being waited on, a kernel virtual address
1162 * @bit: the bit of the word being waited on
1163 * @mode: the task state to sleep in
1164 *
1165 * Use the standard hashed waitqueue table to wait for a bit
1166 * to be cleared and then to atomically set it. This is similar
1167 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1168 * for the actual waiting.
1169 *
1170 * Returns zero if the bit was (eventually) found to be clear and was
1171 * set. Returns non-zero if a signal was delivered to the process and
1172 * the @mode allows that signal to wake the process.
1173 */
1174static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001175wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001176{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001177 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001178 if (!test_and_set_bit(bit, word))
1179 return 0;
1180 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1181}
1182
1183/**
1184 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1185 * @word: the word being waited on, a kernel virtual address
1186 * @bit: the bit of the word being waited on
1187 * @action: the function used to sleep, which may take special actions
1188 * @mode: the task state to sleep in
1189 *
1190 * Use the standard hashed waitqueue table to wait for a bit
1191 * to be cleared and then to set it, and allow the waiting action
1192 * to be specified.
1193 * This is like wait_on_bit() but allows fine control of how the waiting
1194 * is done.
1195 *
1196 * Returns zero if the bit was (eventually) found to be clear and was
1197 * set. Returns non-zero if a signal was delivered to the process and
1198 * the @mode allows that signal to wake the process.
1199 */
1200static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001201wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
1202 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001204 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (!test_and_set_bit(bit, word))
1206 return 0;
1207 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1208}
David Howellscb655372013-05-10 19:50:26 +01001209
1210/**
1211 * wait_on_atomic_t - Wait for an atomic_t to become 0
1212 * @val: The atomic value being waited on, a kernel virtual address
1213 * @action: the function used to sleep, which may take special actions
1214 * @mode: the task state to sleep in
1215 *
1216 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1217 * the purpose of getting a waitqueue, but we set the key to a bit number
1218 * outside of the target 'word'.
1219 */
1220static inline
1221int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1222{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001223 might_sleep();
David Howellscb655372013-05-10 19:50:26 +01001224 if (atomic_read(val) == 0)
1225 return 0;
1226 return out_of_line_wait_on_atomic_t(val, action, mode);
1227}
Ingo Molnarfb869b62013-10-04 10:24:49 +02001228
1229#endif /* _LINUX_WAIT_H */