blob: d3d077228d4c155ad4b08a6668dc725679996372 [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
105static inline int waitqueue_active(wait_queue_head_t *q)
106{
107 return !list_empty(&q->task_list);
108}
109
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800110extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
111extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
112extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
115{
116 list_add(&new->task_list, &head->task_list);
117}
118
119/*
120 * Used for wake-one threads:
121 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200122static inline void
123__add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
Changli Gaoa93d2f12010-05-07 14:33:26 +0800124{
125 wait->flags |= WQ_FLAG_EXCLUSIVE;
126 __add_wait_queue(q, wait);
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static inline void __add_wait_queue_tail(wait_queue_head_t *head,
Changli Gaoa93d2f12010-05-07 14:33:26 +0800130 wait_queue_t *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 list_add_tail(&new->task_list, &head->task_list);
133}
134
Ingo Molnarfb869b62013-10-04 10:24:49 +0200135static inline void
136__add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
Changli Gaoa93d2f12010-05-07 14:33:26 +0800137{
138 wait->flags |= WQ_FLAG_EXCLUSIVE;
139 __add_wait_queue_tail(q, wait);
140}
141
Ingo Molnarfb869b62013-10-04 10:24:49 +0200142static inline void
143__remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 list_del(&old->task_list);
146}
147
NeilBrownc1221322014-07-07 15:16:04 +1000148typedef int wait_bit_action_f(struct wait_bit_key *);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800149void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Andrea Arcangeli51360152015-09-04 15:46:04 -0700150void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, int nr,
151 void *key);
Ingo Molnarfb869b62013-10-04 10:24:49 +0200152void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Thomas Gleixner63b20012011-12-01 00:04:00 +0100153void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700154void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800155void __wake_up_bit(wait_queue_head_t *, void *, int);
NeilBrownc1221322014-07-07 15:16:04 +1000156int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
157int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800158void wake_up_bit(void *, int);
David Howellscb655372013-05-10 19:50:26 +0100159void wake_up_atomic_t(atomic_t *);
NeilBrownc1221322014-07-07 15:16:04 +1000160int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
NeilBrowncbbce822014-09-25 13:55:19 +1000161int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
NeilBrownc1221322014-07-07 15:16:04 +1000162int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
David Howellscb655372013-05-10 19:50:26 +0100163int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800164wait_queue_head_t *bit_waitqueue(void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500166#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
167#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
168#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
Thomas Gleixner63b20012011-12-01 00:04:00 +0100169#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
170#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
173#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
174#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500175#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800177/*
Davide Libenzic0da3772009-03-31 15:24:20 -0700178 * Wakeup macros to be used to report events to the targets.
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800179 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200180#define wake_up_poll(x, m) \
Davide Libenzic0da3772009-03-31 15:24:20 -0700181 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
Ingo Molnarfb869b62013-10-04 10:24:49 +0200182#define wake_up_locked_poll(x, m) \
Andrea Arcangeli51360152015-09-04 15:46:04 -0700183 __wake_up_locked_key((x), TASK_NORMAL, 1, (void *) (m))
Ingo Molnarfb869b62013-10-04 10:24:49 +0200184#define wake_up_interruptible_poll(x, m) \
Davide Libenzic0da3772009-03-31 15:24:20 -0700185 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
186#define wake_up_interruptible_sync_poll(x, m) \
187 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800188
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200189#define ___wait_cond_timeout(condition) \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200190({ \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200191 bool __cond = (condition); \
192 if (__cond && !__ret) \
193 __ret = 1; \
194 __cond || !__ret; \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200195})
196
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200197#define ___wait_is_interruptible(state) \
198 (!__builtin_constant_p(state) || \
199 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200200
Peter Zijlstra8b322012014-04-18 15:07:17 -0700201/*
202 * The below macro ___wait_event() has an explicit shadow of the __ret
203 * variable when used from the wait_event_*() macros.
204 *
205 * This is so that both can use the ___wait_cond_timeout() construct
206 * to wrap the condition.
207 *
208 * The type inconsistency of the wait_event_*() __ret variable is also
209 * on purpose; we use long where we can return timeout values and int
210 * otherwise.
211 */
212
Peter Zijlstra41a14312013-10-02 11:22:21 +0200213#define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200214({ \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200215 __label__ __out; \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200216 wait_queue_t __wait; \
Peter Zijlstra8b322012014-04-18 15:07:17 -0700217 long __ret = ret; /* explicit shadow */ \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200218 \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200219 INIT_LIST_HEAD(&__wait.task_list); \
220 if (exclusive) \
221 __wait.flags = WQ_FLAG_EXCLUSIVE; \
222 else \
223 __wait.flags = 0; \
224 \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200225 for (;;) { \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200226 long __int = prepare_to_wait_event(&wq, &__wait, state);\
Peter Zijlstra41a14312013-10-02 11:22:21 +0200227 \
228 if (condition) \
229 break; \
230 \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200231 if (___wait_is_interruptible(state) && __int) { \
232 __ret = __int; \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200233 if (exclusive) { \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200234 abort_exclusive_wait(&wq, &__wait, \
235 state, NULL); \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200236 goto __out; \
237 } \
238 break; \
239 } \
240 \
241 cmd; \
242 } \
243 finish_wait(&wq, &__wait); \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200244__out: __ret; \
245})
Peter Zijlstra41a14312013-10-02 11:22:21 +0200246
Ingo Molnarfb869b62013-10-04 10:24:49 +0200247#define __wait_event(wq, condition) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200248 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
249 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251/**
252 * wait_event - sleep until a condition gets true
253 * @wq: the waitqueue to wait on
254 * @condition: a C expression for the event to wait for
255 *
256 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
257 * @condition evaluates to true. The @condition is checked each time
258 * the waitqueue @wq is woken up.
259 *
260 * wake_up() has to be called after changing any variable that could
261 * change the result of the wait condition.
262 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200263#define wait_event(wq, condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264do { \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200265 might_sleep(); \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200266 if (condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 break; \
268 __wait_event(wq, condition); \
269} while (0)
270
Peter Zijlstra2c561242015-02-03 12:55:31 +0100271#define __io_wait_event(wq, condition) \
272 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
273 io_schedule())
274
275/*
276 * io_wait_event() -- like wait_event() but with io_schedule()
277 */
278#define io_wait_event(wq, condition) \
279do { \
280 might_sleep(); \
281 if (condition) \
282 break; \
283 __io_wait_event(wq, condition); \
284} while (0)
285
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100286#define __wait_event_freezable(wq, condition) \
287 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
288 schedule(); try_to_freeze())
289
290/**
291 * wait_event - sleep (or freeze) until a condition gets true
292 * @wq: the waitqueue to wait on
293 * @condition: a C expression for the event to wait for
294 *
295 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
296 * to system load) until the @condition evaluates to true. The
297 * @condition is checked each time the waitqueue @wq is woken up.
298 *
299 * wake_up() has to be called after changing any variable that could
300 * change the result of the wait condition.
301 */
302#define wait_event_freezable(wq, condition) \
303({ \
304 int __ret = 0; \
305 might_sleep(); \
306 if (!(condition)) \
307 __ret = __wait_event_freezable(wq, condition); \
308 __ret; \
309})
310
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200311#define __wait_event_timeout(wq, condition, timeout) \
312 ___wait_event(wq, ___wait_cond_timeout(condition), \
313 TASK_UNINTERRUPTIBLE, 0, timeout, \
314 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316/**
317 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
318 * @wq: the waitqueue to wait on
319 * @condition: a C expression for the event to wait for
320 * @timeout: timeout, in jiffies
321 *
322 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
323 * @condition evaluates to true. The @condition is checked each time
324 * the waitqueue @wq is woken up.
325 *
326 * wake_up() has to be called after changing any variable that could
327 * change the result of the wait condition.
328 *
Scot Doyle6b44f512014-08-24 17:12:27 +0000329 * Returns:
330 * 0 if the @condition evaluated to %false after the @timeout elapsed,
331 * 1 if the @condition evaluated to %true after the @timeout elapsed,
332 * or the remaining jiffies (at least 1) if the @condition evaluated
333 * to %true before the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 */
335#define wait_event_timeout(wq, condition, timeout) \
336({ \
337 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200338 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200339 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200340 __ret = __wait_event_timeout(wq, condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 __ret; \
342})
343
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100344#define __wait_event_freezable_timeout(wq, condition, timeout) \
345 ___wait_event(wq, ___wait_cond_timeout(condition), \
346 TASK_INTERRUPTIBLE, 0, timeout, \
347 __ret = schedule_timeout(__ret); try_to_freeze())
348
349/*
350 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
351 * increasing load and is freezable.
352 */
353#define wait_event_freezable_timeout(wq, condition, timeout) \
354({ \
355 long __ret = timeout; \
356 might_sleep(); \
357 if (!___wait_cond_timeout(condition)) \
358 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
359 __ret; \
360})
361
Yuanhan Liu9f3520c2015-05-08 18:19:05 +1000362#define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
363 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
364 cmd1; schedule(); cmd2)
365/*
366 * Just like wait_event_cmd(), except it sets exclusive flag
367 */
368#define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
369do { \
370 if (condition) \
371 break; \
372 __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2); \
373} while (0)
374
Shaohua Li82e06c82013-11-14 15:16:16 +1100375#define __wait_event_cmd(wq, condition, cmd1, cmd2) \
376 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
377 cmd1; schedule(); cmd2)
378
379/**
380 * wait_event_cmd - sleep until a condition gets true
381 * @wq: the waitqueue to wait on
382 * @condition: a C expression for the event to wait for
Masanari Iidaf434f7a2014-01-22 01:22:06 +0900383 * @cmd1: the command will be executed before sleep
384 * @cmd2: the command will be executed after sleep
Shaohua Li82e06c82013-11-14 15:16:16 +1100385 *
386 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
387 * @condition evaluates to true. The @condition is checked each time
388 * the waitqueue @wq is woken up.
389 *
390 * wake_up() has to be called after changing any variable that could
391 * change the result of the wait condition.
392 */
393#define wait_event_cmd(wq, condition, cmd1, cmd2) \
394do { \
395 if (condition) \
396 break; \
397 __wait_event_cmd(wq, condition, cmd1, cmd2); \
398} while (0)
399
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200400#define __wait_event_interruptible(wq, condition) \
401 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstraf13f4c42013-10-02 11:22:24 +0200402 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404/**
405 * wait_event_interruptible - sleep until a condition gets true
406 * @wq: the waitqueue to wait on
407 * @condition: a C expression for the event to wait for
408 *
409 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
410 * @condition evaluates to true or a signal is received.
411 * The @condition is checked each time the waitqueue @wq is woken up.
412 *
413 * wake_up() has to be called after changing any variable that could
414 * change the result of the wait condition.
415 *
416 * The function will return -ERESTARTSYS if it was interrupted by a
417 * signal and 0 if @condition evaluated to true.
418 */
419#define wait_event_interruptible(wq, condition) \
420({ \
421 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200422 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200424 __ret = __wait_event_interruptible(wq, condition); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 __ret; \
426})
427
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200428#define __wait_event_interruptible_timeout(wq, condition, timeout) \
429 ___wait_event(wq, ___wait_cond_timeout(condition), \
430 TASK_INTERRUPTIBLE, 0, timeout, \
431 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433/**
434 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
435 * @wq: the waitqueue to wait on
436 * @condition: a C expression for the event to wait for
437 * @timeout: timeout, in jiffies
438 *
439 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
440 * @condition evaluates to true or a signal is received.
441 * The @condition is checked each time the waitqueue @wq is woken up.
442 *
443 * wake_up() has to be called after changing any variable that could
444 * change the result of the wait condition.
445 *
Imre Deak4c663cf2013-05-24 15:55:09 -0700446 * Returns:
Scot Doyle6b44f512014-08-24 17:12:27 +0000447 * 0 if the @condition evaluated to %false after the @timeout elapsed,
448 * 1 if the @condition evaluated to %true after the @timeout elapsed,
449 * the remaining jiffies (at least 1) if the @condition evaluated
450 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
451 * interrupted by a signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
453#define wait_event_interruptible_timeout(wq, condition, timeout) \
454({ \
455 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200456 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200457 if (!___wait_cond_timeout(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200458 __ret = __wait_event_interruptible_timeout(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200459 condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 __ret; \
461})
462
Kent Overstreet774a08b2013-05-07 16:18:43 -0700463#define __wait_event_hrtimeout(wq, condition, timeout, state) \
464({ \
465 int __ret = 0; \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700466 struct hrtimer_sleeper __t; \
467 \
468 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
469 HRTIMER_MODE_REL); \
470 hrtimer_init_sleeper(&__t, current); \
471 if ((timeout).tv64 != KTIME_MAX) \
472 hrtimer_start_range_ns(&__t.timer, timeout, \
473 current->timer_slack_ns, \
474 HRTIMER_MODE_REL); \
475 \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200476 __ret = ___wait_event(wq, condition, state, 0, 0, \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700477 if (!__t.task) { \
478 __ret = -ETIME; \
479 break; \
480 } \
Peter Zijlstraebdc1952013-10-02 11:22:32 +0200481 schedule()); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700482 \
483 hrtimer_cancel(&__t.timer); \
484 destroy_hrtimer_on_stack(&__t.timer); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700485 __ret; \
486})
487
488/**
489 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
490 * @wq: the waitqueue to wait on
491 * @condition: a C expression for the event to wait for
492 * @timeout: timeout, as a ktime_t
493 *
494 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
495 * @condition evaluates to true or a signal is received.
496 * The @condition is checked each time the waitqueue @wq is woken up.
497 *
498 * wake_up() has to be called after changing any variable that could
499 * change the result of the wait condition.
500 *
501 * The function returns 0 if @condition became true, or -ETIME if the timeout
502 * elapsed.
503 */
504#define wait_event_hrtimeout(wq, condition, timeout) \
505({ \
506 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200507 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700508 if (!(condition)) \
509 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
510 TASK_UNINTERRUPTIBLE); \
511 __ret; \
512})
513
514/**
515 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
516 * @wq: the waitqueue to wait on
517 * @condition: a C expression for the event to wait for
518 * @timeout: timeout, as a ktime_t
519 *
520 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
521 * @condition evaluates to true or a signal is received.
522 * The @condition is checked each time the waitqueue @wq is woken up.
523 *
524 * wake_up() has to be called after changing any variable that could
525 * change the result of the wait condition.
526 *
527 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
528 * interrupted by a signal, or -ETIME if the timeout elapsed.
529 */
530#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
531({ \
532 long __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200533 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700534 if (!(condition)) \
535 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
536 TASK_INTERRUPTIBLE); \
537 __ret; \
538})
539
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200540#define __wait_event_interruptible_exclusive(wq, condition) \
541 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
Peter Zijlstra48c25212013-10-02 11:22:26 +0200542 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544#define wait_event_interruptible_exclusive(wq, condition) \
545({ \
546 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200547 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200549 __ret = __wait_event_interruptible_exclusive(wq, condition);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 __ret; \
551})
552
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200553
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100554#define __wait_event_freezable_exclusive(wq, condition) \
555 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
556 schedule(); try_to_freeze())
557
558#define wait_event_freezable_exclusive(wq, condition) \
559({ \
560 int __ret = 0; \
561 might_sleep(); \
562 if (!(condition)) \
563 __ret = __wait_event_freezable_exclusive(wq, condition);\
564 __ret; \
565})
566
567
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200568#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
569({ \
570 int __ret = 0; \
571 DEFINE_WAIT(__wait); \
572 if (exclusive) \
573 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
574 do { \
575 if (likely(list_empty(&__wait.task_list))) \
576 __add_wait_queue_tail(&(wq), &__wait); \
577 set_current_state(TASK_INTERRUPTIBLE); \
578 if (signal_pending(current)) { \
579 __ret = -ERESTARTSYS; \
580 break; \
581 } \
582 if (irq) \
583 spin_unlock_irq(&(wq).lock); \
584 else \
585 spin_unlock(&(wq).lock); \
586 schedule(); \
587 if (irq) \
588 spin_lock_irq(&(wq).lock); \
589 else \
590 spin_lock(&(wq).lock); \
591 } while (!(condition)); \
592 __remove_wait_queue(&(wq), &__wait); \
593 __set_current_state(TASK_RUNNING); \
594 __ret; \
595})
596
597
598/**
599 * wait_event_interruptible_locked - sleep until a condition gets true
600 * @wq: the waitqueue to wait on
601 * @condition: a C expression for the event to wait for
602 *
603 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
604 * @condition evaluates to true or a signal is received.
605 * The @condition is checked each time the waitqueue @wq is woken up.
606 *
607 * It must be called with wq.lock being held. This spinlock is
608 * unlocked while sleeping but @condition testing is done while lock
609 * is held and when this macro exits the lock is held.
610 *
611 * The lock is locked/unlocked using spin_lock()/spin_unlock()
612 * functions which must match the way they are locked/unlocked outside
613 * of this macro.
614 *
615 * wake_up_locked() has to be called after changing any variable that could
616 * change the result of the wait condition.
617 *
618 * The function will return -ERESTARTSYS if it was interrupted by a
619 * signal and 0 if @condition evaluated to true.
620 */
621#define wait_event_interruptible_locked(wq, condition) \
622 ((condition) \
623 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
624
625/**
626 * wait_event_interruptible_locked_irq - sleep until a condition gets true
627 * @wq: the waitqueue to wait on
628 * @condition: a C expression for the event to wait for
629 *
630 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
631 * @condition evaluates to true or a signal is received.
632 * The @condition is checked each time the waitqueue @wq is woken up.
633 *
634 * It must be called with wq.lock being held. This spinlock is
635 * unlocked while sleeping but @condition testing is done while lock
636 * is held and when this macro exits the lock is held.
637 *
638 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
639 * functions which must match the way they are locked/unlocked outside
640 * of this macro.
641 *
642 * wake_up_locked() has to be called after changing any variable that could
643 * change the result of the wait condition.
644 *
645 * The function will return -ERESTARTSYS if it was interrupted by a
646 * signal and 0 if @condition evaluated to true.
647 */
648#define wait_event_interruptible_locked_irq(wq, condition) \
649 ((condition) \
650 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
651
652/**
653 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
654 * @wq: the waitqueue to wait on
655 * @condition: a C expression for the event to wait for
656 *
657 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
658 * @condition evaluates to true or a signal is received.
659 * The @condition is checked each time the waitqueue @wq is woken up.
660 *
661 * It must be called with wq.lock being held. This spinlock is
662 * unlocked while sleeping but @condition testing is done while lock
663 * is held and when this macro exits the lock is held.
664 *
665 * The lock is locked/unlocked using spin_lock()/spin_unlock()
666 * functions which must match the way they are locked/unlocked outside
667 * of this macro.
668 *
669 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
670 * set thus when other process waits process on the list if this
671 * process is awaken further processes are not considered.
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_exclusive_locked(wq, condition) \
680 ((condition) \
681 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
682
683/**
684 * wait_event_interruptible_exclusive_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 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
701 * set thus when other process waits process on the list if this
702 * process is awaken further processes are not considered.
703 *
704 * wake_up_locked() has to be called after changing any variable that could
705 * change the result of the wait condition.
706 *
707 * The function will return -ERESTARTSYS if it was interrupted by a
708 * signal and 0 if @condition evaluated to true.
709 */
710#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
711 ((condition) \
712 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
713
714
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200715#define __wait_event_killable(wq, condition) \
716 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500717
718/**
719 * wait_event_killable - sleep until a condition gets true
720 * @wq: the waitqueue to wait on
721 * @condition: a C expression for the event to wait for
722 *
723 * The process is put to sleep (TASK_KILLABLE) until the
724 * @condition evaluates to true or a signal is received.
725 * The @condition is checked each time the waitqueue @wq is woken up.
726 *
727 * wake_up() has to be called after changing any variable that could
728 * change the result of the wait condition.
729 *
730 * The function will return -ERESTARTSYS if it was interrupted by a
731 * signal and 0 if @condition evaluated to true.
732 */
733#define wait_event_killable(wq, condition) \
734({ \
735 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200736 might_sleep(); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500737 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200738 __ret = __wait_event_killable(wq, condition); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500739 __ret; \
740})
741
Lukas Czernereed8c022012-11-30 11:42:40 +0100742
743#define __wait_event_lock_irq(wq, condition, lock, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200744 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
745 spin_unlock_irq(&lock); \
746 cmd; \
747 schedule(); \
748 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100749
750/**
751 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
752 * condition is checked under the lock. This
753 * is expected to be called with the lock
754 * taken.
755 * @wq: the waitqueue to wait on
756 * @condition: a C expression for the event to wait for
757 * @lock: a locked spinlock_t, which will be released before cmd
758 * and schedule() and reacquired afterwards.
759 * @cmd: a command which is invoked outside the critical section before
760 * sleep
761 *
762 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
763 * @condition evaluates to true. The @condition is checked each time
764 * the waitqueue @wq is woken up.
765 *
766 * wake_up() has to be called after changing any variable that could
767 * change the result of the wait condition.
768 *
769 * This is supposed to be called while holding the lock. The lock is
770 * dropped before invoking the cmd and going to sleep and is reacquired
771 * afterwards.
772 */
773#define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
774do { \
775 if (condition) \
776 break; \
777 __wait_event_lock_irq(wq, condition, lock, cmd); \
778} while (0)
779
780/**
781 * wait_event_lock_irq - sleep until a condition gets true. The
782 * condition is checked under the lock. This
783 * is expected to be called with the lock
784 * taken.
785 * @wq: the waitqueue to wait on
786 * @condition: a C expression for the event to wait for
787 * @lock: a locked spinlock_t, which will be released before schedule()
788 * and reacquired afterwards.
789 *
790 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
791 * @condition evaluates to true. The @condition is checked each time
792 * the waitqueue @wq is woken up.
793 *
794 * wake_up() has to be called after changing any variable that could
795 * change the result of the wait condition.
796 *
797 * This is supposed to be called while holding the lock. The lock is
798 * dropped before going to sleep and is reacquired afterwards.
799 */
800#define wait_event_lock_irq(wq, condition, lock) \
801do { \
802 if (condition) \
803 break; \
804 __wait_event_lock_irq(wq, condition, lock, ); \
805} while (0)
806
807
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200808#define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200809 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200810 spin_unlock_irq(&lock); \
811 cmd; \
812 schedule(); \
Peter Zijlstra8fbd88f2013-10-02 11:22:28 +0200813 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100814
815/**
816 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
817 * The condition is checked under the lock. This is expected to
818 * be called with the lock taken.
819 * @wq: the waitqueue to wait on
820 * @condition: a C expression for the event to wait for
821 * @lock: a locked spinlock_t, which will be released before cmd and
822 * schedule() and reacquired afterwards.
823 * @cmd: a command which is invoked outside the critical section before
824 * sleep
825 *
826 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
827 * @condition evaluates to true or a signal is received. The @condition is
828 * checked each time the waitqueue @wq is woken up.
829 *
830 * wake_up() has to be called after changing any variable that could
831 * change the result of the wait condition.
832 *
833 * This is supposed to be called while holding the lock. The lock is
834 * dropped before invoking the cmd and going to sleep and is reacquired
835 * afterwards.
836 *
837 * The macro will return -ERESTARTSYS if it was interrupted by a signal
838 * and 0 if @condition evaluated to true.
839 */
840#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
841({ \
842 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100843 if (!(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200844 __ret = __wait_event_interruptible_lock_irq(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200845 condition, lock, cmd); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100846 __ret; \
847})
848
849/**
850 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
851 * The condition is checked under the lock. This is expected
852 * to be called with the lock taken.
853 * @wq: the waitqueue to wait on
854 * @condition: a C expression for the event to wait for
855 * @lock: a locked spinlock_t, which will be released before schedule()
856 * and reacquired afterwards.
857 *
858 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
859 * @condition evaluates to true or signal is received. The @condition is
860 * checked each time the waitqueue @wq is woken up.
861 *
862 * wake_up() has to be called after changing any variable that could
863 * change the result of the wait condition.
864 *
865 * This is supposed to be called while holding the lock. The lock is
866 * dropped before going to sleep and is reacquired afterwards.
867 *
868 * The macro will return -ERESTARTSYS if it was interrupted by a signal
869 * and 0 if @condition evaluated to true.
870 */
871#define wait_event_interruptible_lock_irq(wq, condition, lock) \
872({ \
873 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100874 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200875 __ret = __wait_event_interruptible_lock_irq(wq, \
Thierry Reding92ec1182013-10-23 13:40:55 +0200876 condition, lock,); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100877 __ret; \
878})
879
Ingo Molnarfb869b62013-10-04 10:24:49 +0200880#define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
881 lock, timeout) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200882 ___wait_event(wq, ___wait_cond_timeout(condition), \
Heiko Carstens7d716452013-10-31 12:48:14 +0100883 TASK_INTERRUPTIBLE, 0, timeout, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200884 spin_unlock_irq(&lock); \
885 __ret = schedule_timeout(__ret); \
Peter Zijlstraa1dc6852013-10-02 11:22:29 +0200886 spin_lock_irq(&lock));
Martin Peschked79ff142013-08-22 17:45:36 +0200887
888/**
Ingo Molnarfb869b62013-10-04 10:24:49 +0200889 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
890 * true or a timeout elapses. The condition is checked under
891 * the lock. This is expected to be called with the lock taken.
Martin Peschked79ff142013-08-22 17:45:36 +0200892 * @wq: the waitqueue to wait on
893 * @condition: a C expression for the event to wait for
894 * @lock: a locked spinlock_t, which will be released before schedule()
895 * and reacquired afterwards.
896 * @timeout: timeout, in jiffies
897 *
898 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
899 * @condition evaluates to true or signal is received. The @condition is
900 * checked each time the waitqueue @wq is woken up.
901 *
902 * wake_up() has to be called after changing any variable that could
903 * change the result of the wait condition.
904 *
905 * This is supposed to be called while holding the lock. The lock is
906 * dropped before going to sleep and is reacquired afterwards.
907 *
908 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
909 * was interrupted by a signal, and the remaining jiffies otherwise
910 * if the condition evaluated to true before the timeout elapsed.
911 */
912#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
913 timeout) \
914({ \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200915 long __ret = timeout; \
Oleg Nesterov89229152013-10-07 20:31:06 +0200916 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200917 __ret = __wait_event_interruptible_lock_irq_timeout( \
918 wq, condition, lock, timeout); \
Martin Peschked79ff142013-08-22 17:45:36 +0200919 __ret; \
920})
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922/*
923 * Waitqueues which are removed from the waitqueue_head at wakeup time
924 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800925void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
926void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200927long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800928void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Ingo Molnarfb869b62013-10-04 10:24:49 +0200929void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
Peter Zijlstra61ada522014-09-24 10:18:47 +0200930long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
931int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
933int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
934
Eric Dumazetbf368e42009-04-28 02:24:21 -0700935#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700937 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -0700938 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200939 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
Eric Dumazetbf368e42009-04-28 02:24:21 -0700942#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944#define DEFINE_WAIT_BIT(name, word, bit) \
945 struct wait_bit_queue name = { \
946 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
947 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700948 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 .func = wake_bit_function, \
950 .task_list = \
951 LIST_HEAD_INIT((name).wait.task_list), \
952 }, \
953 }
954
955#define init_wait(wait) \
956 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700957 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 (wait)->func = autoremove_wake_function; \
959 INIT_LIST_HEAD(&(wait)->task_list); \
Evgeny Kuznetsov231d0ae2010-10-05 12:47:57 +0400960 (wait)->flags = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 } while (0)
962
NeilBrown74316202014-07-07 15:16:04 +1000963
NeilBrownc1221322014-07-07 15:16:04 +1000964extern int bit_wait(struct wait_bit_key *);
965extern int bit_wait_io(struct wait_bit_key *);
NeilBrowncbbce822014-09-25 13:55:19 +1000966extern int bit_wait_timeout(struct wait_bit_key *);
967extern int bit_wait_io_timeout(struct wait_bit_key *);
NeilBrown74316202014-07-07 15:16:04 +1000968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/**
970 * wait_on_bit - wait for a bit to be cleared
971 * @word: the word being waited on, a kernel virtual address
972 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * @mode: the task state to sleep in
974 *
975 * There is a standard hashed waitqueue table for generic use. This
976 * is the part of the hashtable's accessor API that waits on a bit.
977 * For instance, if one were to have waiters on a bitflag, one would
978 * call wait_on_bit() in threads waiting for the bit to clear.
979 * One uses wait_on_bit() where one is waiting for the bit to clear,
980 * but has no intention of setting it.
NeilBrown74316202014-07-07 15:16:04 +1000981 * Returned value will be zero if the bit was cleared, or non-zero
982 * if the process received a signal and the mode permitted wakeup
983 * on that signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200985static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -0700986wait_on_bit(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +1000987{
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200988 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000989 if (!test_bit(bit, word))
990 return 0;
991 return out_of_line_wait_on_bit(word, bit,
992 bit_wait,
993 mode);
994}
995
996/**
997 * wait_on_bit_io - wait for a bit to be cleared
998 * @word: the word being waited on, a kernel virtual address
999 * @bit: the bit of the word being waited on
1000 * @mode: the task state to sleep in
1001 *
1002 * Use the standard hashed waitqueue table to wait for a bit
1003 * to be cleared. This is similar to wait_on_bit(), but calls
1004 * io_schedule() instead of schedule() for the actual waiting.
1005 *
1006 * Returned value will be zero if the bit was cleared, or non-zero
1007 * if the process received a signal and the mode permitted wakeup
1008 * on that signal.
1009 */
1010static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001011wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001012{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001013 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001014 if (!test_bit(bit, word))
1015 return 0;
1016 return out_of_line_wait_on_bit(word, bit,
1017 bit_wait_io,
1018 mode);
1019}
1020
1021/**
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001022 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
1023 * @word: the word being waited on, a kernel virtual address
1024 * @bit: the bit of the word being waited on
1025 * @mode: the task state to sleep in
1026 * @timeout: timeout, in jiffies
1027 *
1028 * Use the standard hashed waitqueue table to wait for a bit
1029 * to be cleared. This is similar to wait_on_bit(), except also takes a
1030 * timeout parameter.
1031 *
1032 * Returned value will be zero if the bit was cleared before the
1033 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1034 * received a signal and the mode permitted wakeup on that signal.
1035 */
1036static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001037wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
1038 unsigned long timeout)
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001039{
1040 might_sleep();
1041 if (!test_bit(bit, word))
1042 return 0;
1043 return out_of_line_wait_on_bit_timeout(word, bit,
1044 bit_wait_timeout,
1045 mode, timeout);
1046}
1047
1048/**
NeilBrown74316202014-07-07 15:16:04 +10001049 * wait_on_bit_action - wait for a bit to be cleared
1050 * @word: the word being waited on, a kernel virtual address
1051 * @bit: the bit of the word being waited on
1052 * @action: the function used to sleep, which may take special actions
1053 * @mode: the task state to sleep in
1054 *
1055 * Use the standard hashed waitqueue table to wait for a bit
1056 * to be cleared, and allow the waiting action to be specified.
1057 * This is like wait_on_bit() but allows fine control of how the waiting
1058 * is done.
1059 *
1060 * Returned value will be zero if the bit was cleared, or non-zero
1061 * if the process received a signal and the mode permitted wakeup
1062 * on that signal.
1063 */
1064static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001065wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
1066 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001068 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (!test_bit(bit, word))
1070 return 0;
1071 return out_of_line_wait_on_bit(word, bit, action, mode);
1072}
1073
1074/**
1075 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1076 * @word: the word being waited on, a kernel virtual address
1077 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 * @mode: the task state to sleep in
1079 *
1080 * There is a standard hashed waitqueue table for generic use. This
1081 * is the part of the hashtable's accessor API that waits on a bit
1082 * when one intends to set it, for instance, trying to lock bitflags.
1083 * For instance, if one were to have waiters trying to set bitflag
1084 * and waiting for it to clear before setting it, one would call
1085 * wait_on_bit() in threads waiting to be able to set the bit.
1086 * One uses wait_on_bit_lock() where one is waiting for the bit to
1087 * clear with the intention of setting it, and when done, clearing it.
NeilBrown74316202014-07-07 15:16:04 +10001088 *
1089 * Returns zero if the bit was (eventually) found to be clear and was
1090 * set. Returns non-zero if a signal was delivered to the process and
1091 * the @mode allows that signal to wake the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 */
Ingo Molnarfb869b62013-10-04 10:24:49 +02001093static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001094wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001095{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001096 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001097 if (!test_and_set_bit(bit, word))
1098 return 0;
1099 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1100}
1101
1102/**
1103 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1104 * @word: the word being waited on, a kernel virtual address
1105 * @bit: the bit of the word being waited on
1106 * @mode: the task state to sleep in
1107 *
1108 * Use the standard hashed waitqueue table to wait for a bit
1109 * to be cleared and then to atomically set it. This is similar
1110 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1111 * for the actual waiting.
1112 *
1113 * Returns zero if the bit was (eventually) found to be clear and was
1114 * set. Returns non-zero if a signal was delivered to the process and
1115 * the @mode allows that signal to wake the process.
1116 */
1117static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001118wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001119{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001120 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001121 if (!test_and_set_bit(bit, word))
1122 return 0;
1123 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1124}
1125
1126/**
1127 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1128 * @word: the word being waited on, a kernel virtual address
1129 * @bit: the bit of the word being waited on
1130 * @action: the function used to sleep, which may take special actions
1131 * @mode: the task state to sleep in
1132 *
1133 * Use the standard hashed waitqueue table to wait for a bit
1134 * to be cleared and then to set it, and allow the waiting action
1135 * to be specified.
1136 * This is like wait_on_bit() but allows fine control of how the waiting
1137 * is done.
1138 *
1139 * Returns zero if the bit was (eventually) found to be clear and was
1140 * set. Returns non-zero if a signal was delivered to the process and
1141 * the @mode allows that signal to wake the process.
1142 */
1143static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001144wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
1145 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001147 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (!test_and_set_bit(bit, word))
1149 return 0;
1150 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1151}
David Howellscb655372013-05-10 19:50:26 +01001152
1153/**
1154 * wait_on_atomic_t - Wait for an atomic_t to become 0
1155 * @val: The atomic value being waited on, a kernel virtual address
1156 * @action: the function used to sleep, which may take special actions
1157 * @mode: the task state to sleep in
1158 *
1159 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1160 * the purpose of getting a waitqueue, but we set the key to a bit number
1161 * outside of the target 'word'.
1162 */
1163static inline
1164int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1165{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001166 might_sleep();
David Howellscb655372013-05-10 19:50:26 +01001167 if (atomic_read(val) == 0)
1168 return 0;
1169 return out_of_line_wait_on_atomic_t(val, action, mode);
1170}
Ingo Molnarfb869b62013-10-04 10:24:49 +02001171
1172#endif /* _LINUX_WAIT_H */