blob: 1e1bf9f963a947fc686125d0a2809ad63b8a13ed [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);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700150void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
Ingo Molnarfb869b62013-10-04 10:24:49 +0200151void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Thomas Gleixner63b20012011-12-01 00:04:00 +0100152void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700153void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800154void __wake_up_bit(wait_queue_head_t *, void *, int);
NeilBrownc1221322014-07-07 15:16:04 +1000155int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
156int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800157void wake_up_bit(void *, int);
David Howellscb655372013-05-10 19:50:26 +0100158void wake_up_atomic_t(atomic_t *);
NeilBrownc1221322014-07-07 15:16:04 +1000159int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
NeilBrowncbbce822014-09-25 13:55:19 +1000160int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
NeilBrownc1221322014-07-07 15:16:04 +1000161int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
David Howellscb655372013-05-10 19:50:26 +0100162int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800163wait_queue_head_t *bit_waitqueue(void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500165#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
166#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
167#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
Thomas Gleixner63b20012011-12-01 00:04:00 +0100168#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
169#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
172#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
173#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500174#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800176/*
Davide Libenzic0da3772009-03-31 15:24:20 -0700177 * Wakeup macros to be used to report events to the targets.
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800178 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200179#define wake_up_poll(x, m) \
Davide Libenzic0da3772009-03-31 15:24:20 -0700180 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
Ingo Molnarfb869b62013-10-04 10:24:49 +0200181#define wake_up_locked_poll(x, m) \
Davide Libenzic0da3772009-03-31 15:24:20 -0700182 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
Ingo Molnarfb869b62013-10-04 10:24:49 +0200183#define wake_up_interruptible_poll(x, m) \
Davide Libenzic0da3772009-03-31 15:24:20 -0700184 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
185#define wake_up_interruptible_sync_poll(x, m) \
186 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800187
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200188#define ___wait_cond_timeout(condition) \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200189({ \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200190 bool __cond = (condition); \
191 if (__cond && !__ret) \
192 __ret = 1; \
193 __cond || !__ret; \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200194})
195
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200196#define ___wait_is_interruptible(state) \
197 (!__builtin_constant_p(state) || \
198 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200199
Peter Zijlstra8b322012014-04-18 15:07:17 -0700200/*
201 * The below macro ___wait_event() has an explicit shadow of the __ret
202 * variable when used from the wait_event_*() macros.
203 *
204 * This is so that both can use the ___wait_cond_timeout() construct
205 * to wrap the condition.
206 *
207 * The type inconsistency of the wait_event_*() __ret variable is also
208 * on purpose; we use long where we can return timeout values and int
209 * otherwise.
210 */
211
Peter Zijlstra41a14312013-10-02 11:22:21 +0200212#define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200213({ \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200214 __label__ __out; \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200215 wait_queue_t __wait; \
Peter Zijlstra8b322012014-04-18 15:07:17 -0700216 long __ret = ret; /* explicit shadow */ \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200217 \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200218 INIT_LIST_HEAD(&__wait.task_list); \
219 if (exclusive) \
220 __wait.flags = WQ_FLAG_EXCLUSIVE; \
221 else \
222 __wait.flags = 0; \
223 \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200224 for (;;) { \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200225 long __int = prepare_to_wait_event(&wq, &__wait, state);\
Peter Zijlstra41a14312013-10-02 11:22:21 +0200226 \
227 if (condition) \
228 break; \
229 \
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200230 if (___wait_is_interruptible(state) && __int) { \
231 __ret = __int; \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200232 if (exclusive) { \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200233 abort_exclusive_wait(&wq, &__wait, \
234 state, NULL); \
Peter Zijlstra41a14312013-10-02 11:22:21 +0200235 goto __out; \
236 } \
237 break; \
238 } \
239 \
240 cmd; \
241 } \
242 finish_wait(&wq, &__wait); \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200243__out: __ret; \
244})
Peter Zijlstra41a14312013-10-02 11:22:21 +0200245
Ingo Molnarfb869b62013-10-04 10:24:49 +0200246#define __wait_event(wq, condition) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200247 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
248 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/**
251 * wait_event - sleep until a condition gets true
252 * @wq: the waitqueue to wait on
253 * @condition: a C expression for the event to wait for
254 *
255 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
256 * @condition evaluates to true. The @condition is checked each time
257 * the waitqueue @wq is woken up.
258 *
259 * wake_up() has to be called after changing any variable that could
260 * change the result of the wait condition.
261 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200262#define wait_event(wq, condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263do { \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200264 might_sleep(); \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200265 if (condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 break; \
267 __wait_event(wq, condition); \
268} while (0)
269
Peter Zijlstra2c561242015-02-03 12:55:31 +0100270#define __io_wait_event(wq, condition) \
271 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
272 io_schedule())
273
274/*
275 * io_wait_event() -- like wait_event() but with io_schedule()
276 */
277#define io_wait_event(wq, condition) \
278do { \
279 might_sleep(); \
280 if (condition) \
281 break; \
282 __io_wait_event(wq, condition); \
283} while (0)
284
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100285#define __wait_event_freezable(wq, condition) \
286 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
287 schedule(); try_to_freeze())
288
289/**
290 * wait_event - sleep (or freeze) until a condition gets true
291 * @wq: the waitqueue to wait on
292 * @condition: a C expression for the event to wait for
293 *
294 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
295 * to system load) until the @condition evaluates to true. The
296 * @condition is checked each time the waitqueue @wq is woken up.
297 *
298 * wake_up() has to be called after changing any variable that could
299 * change the result of the wait condition.
300 */
301#define wait_event_freezable(wq, condition) \
302({ \
303 int __ret = 0; \
304 might_sleep(); \
305 if (!(condition)) \
306 __ret = __wait_event_freezable(wq, condition); \
307 __ret; \
308})
309
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200310#define __wait_event_timeout(wq, condition, timeout) \
311 ___wait_event(wq, ___wait_cond_timeout(condition), \
312 TASK_UNINTERRUPTIBLE, 0, timeout, \
313 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/**
316 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
317 * @wq: the waitqueue to wait on
318 * @condition: a C expression for the event to wait for
319 * @timeout: timeout, in jiffies
320 *
321 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
322 * @condition evaluates to true. The @condition is checked each time
323 * the waitqueue @wq is woken up.
324 *
325 * wake_up() has to be called after changing any variable that could
326 * change the result of the wait condition.
327 *
Scot Doyle6b44f512014-08-24 17:12:27 +0000328 * Returns:
329 * 0 if the @condition evaluated to %false after the @timeout elapsed,
330 * 1 if the @condition evaluated to %true after the @timeout elapsed,
331 * or the remaining jiffies (at least 1) if the @condition evaluated
332 * to %true before the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
334#define wait_event_timeout(wq, condition, timeout) \
335({ \
336 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200337 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200338 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200339 __ret = __wait_event_timeout(wq, condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 __ret; \
341})
342
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100343#define __wait_event_freezable_timeout(wq, condition, timeout) \
344 ___wait_event(wq, ___wait_cond_timeout(condition), \
345 TASK_INTERRUPTIBLE, 0, timeout, \
346 __ret = schedule_timeout(__ret); try_to_freeze())
347
348/*
349 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
350 * increasing load and is freezable.
351 */
352#define wait_event_freezable_timeout(wq, condition, timeout) \
353({ \
354 long __ret = timeout; \
355 might_sleep(); \
356 if (!___wait_cond_timeout(condition)) \
357 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
358 __ret; \
359})
360
Yuanhan Liu9f3520c2015-05-08 18:19:05 +1000361#define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
362 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
363 cmd1; schedule(); cmd2)
364/*
365 * Just like wait_event_cmd(), except it sets exclusive flag
366 */
367#define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
368do { \
369 if (condition) \
370 break; \
371 __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2); \
372} while (0)
373
Shaohua Li82e06c82013-11-14 15:16:16 +1100374#define __wait_event_cmd(wq, condition, cmd1, cmd2) \
375 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
376 cmd1; schedule(); cmd2)
377
378/**
379 * wait_event_cmd - sleep until a condition gets true
380 * @wq: the waitqueue to wait on
381 * @condition: a C expression for the event to wait for
Masanari Iidaf434f7a2014-01-22 01:22:06 +0900382 * @cmd1: the command will be executed before sleep
383 * @cmd2: the command will be executed after sleep
Shaohua Li82e06c82013-11-14 15:16:16 +1100384 *
385 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
386 * @condition evaluates to true. The @condition is checked each time
387 * the waitqueue @wq is woken up.
388 *
389 * wake_up() has to be called after changing any variable that could
390 * change the result of the wait condition.
391 */
392#define wait_event_cmd(wq, condition, cmd1, cmd2) \
393do { \
394 if (condition) \
395 break; \
396 __wait_event_cmd(wq, condition, cmd1, cmd2); \
397} while (0)
398
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200399#define __wait_event_interruptible(wq, condition) \
400 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstraf13f4c42013-10-02 11:22:24 +0200401 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403/**
404 * wait_event_interruptible - sleep until a condition gets true
405 * @wq: the waitqueue to wait on
406 * @condition: a C expression for the event to wait for
407 *
408 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
409 * @condition evaluates to true or a signal is received.
410 * The @condition is checked each time the waitqueue @wq is woken up.
411 *
412 * wake_up() has to be called after changing any variable that could
413 * change the result of the wait condition.
414 *
415 * The function will return -ERESTARTSYS if it was interrupted by a
416 * signal and 0 if @condition evaluated to true.
417 */
418#define wait_event_interruptible(wq, condition) \
419({ \
420 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200421 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200423 __ret = __wait_event_interruptible(wq, condition); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 __ret; \
425})
426
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200427#define __wait_event_interruptible_timeout(wq, condition, timeout) \
428 ___wait_event(wq, ___wait_cond_timeout(condition), \
429 TASK_INTERRUPTIBLE, 0, timeout, \
430 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/**
433 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
434 * @wq: the waitqueue to wait on
435 * @condition: a C expression for the event to wait for
436 * @timeout: timeout, in jiffies
437 *
438 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
439 * @condition evaluates to true or a signal is received.
440 * The @condition is checked each time the waitqueue @wq is woken up.
441 *
442 * wake_up() has to be called after changing any variable that could
443 * change the result of the wait condition.
444 *
Imre Deak4c663cf2013-05-24 15:55:09 -0700445 * Returns:
Scot Doyle6b44f512014-08-24 17:12:27 +0000446 * 0 if the @condition evaluated to %false after the @timeout elapsed,
447 * 1 if the @condition evaluated to %true after the @timeout elapsed,
448 * the remaining jiffies (at least 1) if the @condition evaluated
449 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
450 * interrupted by a signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 */
452#define wait_event_interruptible_timeout(wq, condition, timeout) \
453({ \
454 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200455 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200456 if (!___wait_cond_timeout(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200457 __ret = __wait_event_interruptible_timeout(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200458 condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 __ret; \
460})
461
Kent Overstreet774a08b2013-05-07 16:18:43 -0700462#define __wait_event_hrtimeout(wq, condition, timeout, state) \
463({ \
464 int __ret = 0; \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700465 struct hrtimer_sleeper __t; \
466 \
467 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
468 HRTIMER_MODE_REL); \
469 hrtimer_init_sleeper(&__t, current); \
470 if ((timeout).tv64 != KTIME_MAX) \
471 hrtimer_start_range_ns(&__t.timer, timeout, \
472 current->timer_slack_ns, \
473 HRTIMER_MODE_REL); \
474 \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200475 __ret = ___wait_event(wq, condition, state, 0, 0, \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700476 if (!__t.task) { \
477 __ret = -ETIME; \
478 break; \
479 } \
Peter Zijlstraebdc1952013-10-02 11:22:32 +0200480 schedule()); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700481 \
482 hrtimer_cancel(&__t.timer); \
483 destroy_hrtimer_on_stack(&__t.timer); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700484 __ret; \
485})
486
487/**
488 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
489 * @wq: the waitqueue to wait on
490 * @condition: a C expression for the event to wait for
491 * @timeout: timeout, as a ktime_t
492 *
493 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
494 * @condition evaluates to true or a signal is received.
495 * The @condition is checked each time the waitqueue @wq is woken up.
496 *
497 * wake_up() has to be called after changing any variable that could
498 * change the result of the wait condition.
499 *
500 * The function returns 0 if @condition became true, or -ETIME if the timeout
501 * elapsed.
502 */
503#define wait_event_hrtimeout(wq, condition, timeout) \
504({ \
505 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200506 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700507 if (!(condition)) \
508 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
509 TASK_UNINTERRUPTIBLE); \
510 __ret; \
511})
512
513/**
514 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
515 * @wq: the waitqueue to wait on
516 * @condition: a C expression for the event to wait for
517 * @timeout: timeout, as a ktime_t
518 *
519 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
520 * @condition evaluates to true or a signal is received.
521 * The @condition is checked each time the waitqueue @wq is woken up.
522 *
523 * wake_up() has to be called after changing any variable that could
524 * change the result of the wait condition.
525 *
526 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
527 * interrupted by a signal, or -ETIME if the timeout elapsed.
528 */
529#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
530({ \
531 long __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200532 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700533 if (!(condition)) \
534 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
535 TASK_INTERRUPTIBLE); \
536 __ret; \
537})
538
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200539#define __wait_event_interruptible_exclusive(wq, condition) \
540 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
Peter Zijlstra48c25212013-10-02 11:22:26 +0200541 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543#define wait_event_interruptible_exclusive(wq, condition) \
544({ \
545 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200546 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200548 __ret = __wait_event_interruptible_exclusive(wq, condition);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 __ret; \
550})
551
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200552
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100553#define __wait_event_freezable_exclusive(wq, condition) \
554 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
555 schedule(); try_to_freeze())
556
557#define wait_event_freezable_exclusive(wq, condition) \
558({ \
559 int __ret = 0; \
560 might_sleep(); \
561 if (!(condition)) \
562 __ret = __wait_event_freezable_exclusive(wq, condition);\
563 __ret; \
564})
565
566
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200567#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
568({ \
569 int __ret = 0; \
570 DEFINE_WAIT(__wait); \
571 if (exclusive) \
572 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
573 do { \
574 if (likely(list_empty(&__wait.task_list))) \
575 __add_wait_queue_tail(&(wq), &__wait); \
576 set_current_state(TASK_INTERRUPTIBLE); \
577 if (signal_pending(current)) { \
578 __ret = -ERESTARTSYS; \
579 break; \
580 } \
581 if (irq) \
582 spin_unlock_irq(&(wq).lock); \
583 else \
584 spin_unlock(&(wq).lock); \
585 schedule(); \
586 if (irq) \
587 spin_lock_irq(&(wq).lock); \
588 else \
589 spin_lock(&(wq).lock); \
590 } while (!(condition)); \
591 __remove_wait_queue(&(wq), &__wait); \
592 __set_current_state(TASK_RUNNING); \
593 __ret; \
594})
595
596
597/**
598 * wait_event_interruptible_locked - sleep until a condition gets true
599 * @wq: the waitqueue to wait on
600 * @condition: a C expression for the event to wait for
601 *
602 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
603 * @condition evaluates to true or a signal is received.
604 * The @condition is checked each time the waitqueue @wq is woken up.
605 *
606 * It must be called with wq.lock being held. This spinlock is
607 * unlocked while sleeping but @condition testing is done while lock
608 * is held and when this macro exits the lock is held.
609 *
610 * The lock is locked/unlocked using spin_lock()/spin_unlock()
611 * functions which must match the way they are locked/unlocked outside
612 * of this macro.
613 *
614 * wake_up_locked() has to be called after changing any variable that could
615 * change the result of the wait condition.
616 *
617 * The function will return -ERESTARTSYS if it was interrupted by a
618 * signal and 0 if @condition evaluated to true.
619 */
620#define wait_event_interruptible_locked(wq, condition) \
621 ((condition) \
622 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
623
624/**
625 * wait_event_interruptible_locked_irq - sleep until a condition gets true
626 * @wq: the waitqueue to wait on
627 * @condition: a C expression for the event to wait for
628 *
629 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
630 * @condition evaluates to true or a signal is received.
631 * The @condition is checked each time the waitqueue @wq is woken up.
632 *
633 * It must be called with wq.lock being held. This spinlock is
634 * unlocked while sleeping but @condition testing is done while lock
635 * is held and when this macro exits the lock is held.
636 *
637 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
638 * functions which must match the way they are locked/unlocked outside
639 * of this macro.
640 *
641 * wake_up_locked() has to be called after changing any variable that could
642 * change the result of the wait condition.
643 *
644 * The function will return -ERESTARTSYS if it was interrupted by a
645 * signal and 0 if @condition evaluated to true.
646 */
647#define wait_event_interruptible_locked_irq(wq, condition) \
648 ((condition) \
649 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
650
651/**
652 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
653 * @wq: the waitqueue to wait on
654 * @condition: a C expression for the event to wait for
655 *
656 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
657 * @condition evaluates to true or a signal is received.
658 * The @condition is checked each time the waitqueue @wq is woken up.
659 *
660 * It must be called with wq.lock being held. This spinlock is
661 * unlocked while sleeping but @condition testing is done while lock
662 * is held and when this macro exits the lock is held.
663 *
664 * The lock is locked/unlocked using spin_lock()/spin_unlock()
665 * functions which must match the way they are locked/unlocked outside
666 * of this macro.
667 *
668 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
669 * set thus when other process waits process on the list if this
670 * process is awaken further processes are not considered.
671 *
672 * wake_up_locked() has to be called after changing any variable that could
673 * change the result of the wait condition.
674 *
675 * The function will return -ERESTARTSYS if it was interrupted by a
676 * signal and 0 if @condition evaluated to true.
677 */
678#define wait_event_interruptible_exclusive_locked(wq, condition) \
679 ((condition) \
680 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
681
682/**
683 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
684 * @wq: the waitqueue to wait on
685 * @condition: a C expression for the event to wait for
686 *
687 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
688 * @condition evaluates to true or a signal is received.
689 * The @condition is checked each time the waitqueue @wq is woken up.
690 *
691 * It must be called with wq.lock being held. This spinlock is
692 * unlocked while sleeping but @condition testing is done while lock
693 * is held and when this macro exits the lock is held.
694 *
695 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
696 * functions which must match the way they are locked/unlocked outside
697 * of this macro.
698 *
699 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
700 * set thus when other process waits process on the list if this
701 * process is awaken further processes are not considered.
702 *
703 * wake_up_locked() has to be called after changing any variable that could
704 * change the result of the wait condition.
705 *
706 * The function will return -ERESTARTSYS if it was interrupted by a
707 * signal and 0 if @condition evaluated to true.
708 */
709#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
710 ((condition) \
711 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
712
713
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200714#define __wait_event_killable(wq, condition) \
715 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500716
717/**
718 * wait_event_killable - sleep until a condition gets true
719 * @wq: the waitqueue to wait on
720 * @condition: a C expression for the event to wait for
721 *
722 * The process is put to sleep (TASK_KILLABLE) until the
723 * @condition evaluates to true or a signal is received.
724 * The @condition is checked each time the waitqueue @wq is woken up.
725 *
726 * wake_up() has to be called after changing any variable that could
727 * change the result of the wait condition.
728 *
729 * The function will return -ERESTARTSYS if it was interrupted by a
730 * signal and 0 if @condition evaluated to true.
731 */
732#define wait_event_killable(wq, condition) \
733({ \
734 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200735 might_sleep(); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500736 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200737 __ret = __wait_event_killable(wq, condition); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500738 __ret; \
739})
740
Lukas Czernereed8c022012-11-30 11:42:40 +0100741
742#define __wait_event_lock_irq(wq, condition, lock, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200743 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
744 spin_unlock_irq(&lock); \
745 cmd; \
746 schedule(); \
747 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100748
749/**
750 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
751 * condition is checked under the lock. This
752 * is expected to be called with the lock
753 * taken.
754 * @wq: the waitqueue to wait on
755 * @condition: a C expression for the event to wait for
756 * @lock: a locked spinlock_t, which will be released before cmd
757 * and schedule() and reacquired afterwards.
758 * @cmd: a command which is invoked outside the critical section before
759 * sleep
760 *
761 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
762 * @condition evaluates to true. The @condition is checked each time
763 * the waitqueue @wq is woken up.
764 *
765 * wake_up() has to be called after changing any variable that could
766 * change the result of the wait condition.
767 *
768 * This is supposed to be called while holding the lock. The lock is
769 * dropped before invoking the cmd and going to sleep and is reacquired
770 * afterwards.
771 */
772#define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
773do { \
774 if (condition) \
775 break; \
776 __wait_event_lock_irq(wq, condition, lock, cmd); \
777} while (0)
778
779/**
780 * wait_event_lock_irq - sleep until a condition gets true. The
781 * condition is checked under the lock. This
782 * is expected to be called with the lock
783 * taken.
784 * @wq: the waitqueue to wait on
785 * @condition: a C expression for the event to wait for
786 * @lock: a locked spinlock_t, which will be released before schedule()
787 * and reacquired afterwards.
788 *
789 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
790 * @condition evaluates to true. The @condition is checked each time
791 * the waitqueue @wq is woken up.
792 *
793 * wake_up() has to be called after changing any variable that could
794 * change the result of the wait condition.
795 *
796 * This is supposed to be called while holding the lock. The lock is
797 * dropped before going to sleep and is reacquired afterwards.
798 */
799#define wait_event_lock_irq(wq, condition, lock) \
800do { \
801 if (condition) \
802 break; \
803 __wait_event_lock_irq(wq, condition, lock, ); \
804} while (0)
805
806
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200807#define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200808 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200809 spin_unlock_irq(&lock); \
810 cmd; \
811 schedule(); \
Peter Zijlstra8fbd88f2013-10-02 11:22:28 +0200812 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100813
814/**
815 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
816 * The condition is checked under the lock. This is expected to
817 * be called with the lock taken.
818 * @wq: the waitqueue to wait on
819 * @condition: a C expression for the event to wait for
820 * @lock: a locked spinlock_t, which will be released before cmd and
821 * schedule() and reacquired afterwards.
822 * @cmd: a command which is invoked outside the critical section before
823 * sleep
824 *
825 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
826 * @condition evaluates to true or a signal is received. The @condition is
827 * checked each time the waitqueue @wq is woken up.
828 *
829 * wake_up() has to be called after changing any variable that could
830 * change the result of the wait condition.
831 *
832 * This is supposed to be called while holding the lock. The lock is
833 * dropped before invoking the cmd and going to sleep and is reacquired
834 * afterwards.
835 *
836 * The macro will return -ERESTARTSYS if it was interrupted by a signal
837 * and 0 if @condition evaluated to true.
838 */
839#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
840({ \
841 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100842 if (!(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200843 __ret = __wait_event_interruptible_lock_irq(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200844 condition, lock, cmd); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100845 __ret; \
846})
847
848/**
849 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
850 * The condition is checked under the lock. This is expected
851 * to be called with the lock taken.
852 * @wq: the waitqueue to wait on
853 * @condition: a C expression for the event to wait for
854 * @lock: a locked spinlock_t, which will be released before schedule()
855 * and reacquired afterwards.
856 *
857 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
858 * @condition evaluates to true or signal is received. The @condition is
859 * checked each time the waitqueue @wq is woken up.
860 *
861 * wake_up() has to be called after changing any variable that could
862 * change the result of the wait condition.
863 *
864 * This is supposed to be called while holding the lock. The lock is
865 * dropped before going to sleep and is reacquired afterwards.
866 *
867 * The macro will return -ERESTARTSYS if it was interrupted by a signal
868 * and 0 if @condition evaluated to true.
869 */
870#define wait_event_interruptible_lock_irq(wq, condition, lock) \
871({ \
872 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100873 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200874 __ret = __wait_event_interruptible_lock_irq(wq, \
Thierry Reding92ec1182013-10-23 13:40:55 +0200875 condition, lock,); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100876 __ret; \
877})
878
Ingo Molnarfb869b62013-10-04 10:24:49 +0200879#define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
880 lock, timeout) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200881 ___wait_event(wq, ___wait_cond_timeout(condition), \
Heiko Carstens7d716452013-10-31 12:48:14 +0100882 TASK_INTERRUPTIBLE, 0, timeout, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200883 spin_unlock_irq(&lock); \
884 __ret = schedule_timeout(__ret); \
Peter Zijlstraa1dc68522013-10-02 11:22:29 +0200885 spin_lock_irq(&lock));
Martin Peschked79ff142013-08-22 17:45:36 +0200886
887/**
Ingo Molnarfb869b62013-10-04 10:24:49 +0200888 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
889 * true or a timeout elapses. The condition is checked under
890 * the lock. This is expected to be called with the lock taken.
Martin Peschked79ff142013-08-22 17:45:36 +0200891 * @wq: the waitqueue to wait on
892 * @condition: a C expression for the event to wait for
893 * @lock: a locked spinlock_t, which will be released before schedule()
894 * and reacquired afterwards.
895 * @timeout: timeout, in jiffies
896 *
897 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
898 * @condition evaluates to true or signal is received. The @condition is
899 * checked each time the waitqueue @wq is woken up.
900 *
901 * wake_up() has to be called after changing any variable that could
902 * change the result of the wait condition.
903 *
904 * This is supposed to be called while holding the lock. The lock is
905 * dropped before going to sleep and is reacquired afterwards.
906 *
907 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
908 * was interrupted by a signal, and the remaining jiffies otherwise
909 * if the condition evaluated to true before the timeout elapsed.
910 */
911#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
912 timeout) \
913({ \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200914 long __ret = timeout; \
Oleg Nesterov89229152013-10-07 20:31:06 +0200915 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200916 __ret = __wait_event_interruptible_lock_irq_timeout( \
917 wq, condition, lock, timeout); \
Martin Peschked79ff142013-08-22 17:45:36 +0200918 __ret; \
919})
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/*
922 * Waitqueues which are removed from the waitqueue_head at wakeup time
923 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800924void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
925void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200926long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800927void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Ingo Molnarfb869b62013-10-04 10:24:49 +0200928void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
Peter Zijlstra61ada522014-09-24 10:18:47 +0200929long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
930int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
932int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
933
Eric Dumazetbf368e42009-04-28 02:24:21 -0700934#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700936 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -0700937 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200938 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
940
Eric Dumazetbf368e42009-04-28 02:24:21 -0700941#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943#define DEFINE_WAIT_BIT(name, word, bit) \
944 struct wait_bit_queue name = { \
945 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
946 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700947 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 .func = wake_bit_function, \
949 .task_list = \
950 LIST_HEAD_INIT((name).wait.task_list), \
951 }, \
952 }
953
954#define init_wait(wait) \
955 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700956 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 (wait)->func = autoremove_wake_function; \
958 INIT_LIST_HEAD(&(wait)->task_list); \
Evgeny Kuznetsov231d0ae2010-10-05 12:47:57 +0400959 (wait)->flags = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 } while (0)
961
NeilBrown74316202014-07-07 15:16:04 +1000962
NeilBrownc1221322014-07-07 15:16:04 +1000963extern int bit_wait(struct wait_bit_key *);
964extern int bit_wait_io(struct wait_bit_key *);
NeilBrowncbbce822014-09-25 13:55:19 +1000965extern int bit_wait_timeout(struct wait_bit_key *);
966extern int bit_wait_io_timeout(struct wait_bit_key *);
NeilBrown74316202014-07-07 15:16:04 +1000967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968/**
969 * wait_on_bit - wait for a bit to be cleared
970 * @word: the word being waited on, a kernel virtual address
971 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * @mode: the task state to sleep in
973 *
974 * There is a standard hashed waitqueue table for generic use. This
975 * is the part of the hashtable's accessor API that waits on a bit.
976 * For instance, if one were to have waiters on a bitflag, one would
977 * call wait_on_bit() in threads waiting for the bit to clear.
978 * One uses wait_on_bit() where one is waiting for the bit to clear,
979 * but has no intention of setting it.
NeilBrown74316202014-07-07 15:16:04 +1000980 * Returned value will be zero if the bit was cleared, or non-zero
981 * if the process received a signal and the mode permitted wakeup
982 * on that signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200984static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -0700985wait_on_bit(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +1000986{
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200987 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000988 if (!test_bit(bit, word))
989 return 0;
990 return out_of_line_wait_on_bit(word, bit,
991 bit_wait,
992 mode);
993}
994
995/**
996 * wait_on_bit_io - wait for a bit to be cleared
997 * @word: the word being waited on, a kernel virtual address
998 * @bit: the bit of the word being waited on
999 * @mode: the task state to sleep in
1000 *
1001 * Use the standard hashed waitqueue table to wait for a bit
1002 * to be cleared. This is similar to wait_on_bit(), but calls
1003 * io_schedule() instead of schedule() for the actual waiting.
1004 *
1005 * Returned value will be zero if the bit was cleared, or non-zero
1006 * if the process received a signal and the mode permitted wakeup
1007 * on that signal.
1008 */
1009static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001010wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001011{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001012 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001013 if (!test_bit(bit, word))
1014 return 0;
1015 return out_of_line_wait_on_bit(word, bit,
1016 bit_wait_io,
1017 mode);
1018}
1019
1020/**
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001021 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
1022 * @word: the word being waited on, a kernel virtual address
1023 * @bit: the bit of the word being waited on
1024 * @mode: the task state to sleep in
1025 * @timeout: timeout, in jiffies
1026 *
1027 * Use the standard hashed waitqueue table to wait for a bit
1028 * to be cleared. This is similar to wait_on_bit(), except also takes a
1029 * timeout parameter.
1030 *
1031 * Returned value will be zero if the bit was cleared before the
1032 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1033 * received a signal and the mode permitted wakeup on that signal.
1034 */
1035static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001036wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
1037 unsigned long timeout)
Johan Hedberg44fc0e52015-01-30 13:14:36 +02001038{
1039 might_sleep();
1040 if (!test_bit(bit, word))
1041 return 0;
1042 return out_of_line_wait_on_bit_timeout(word, bit,
1043 bit_wait_timeout,
1044 mode, timeout);
1045}
1046
1047/**
NeilBrown74316202014-07-07 15:16:04 +10001048 * wait_on_bit_action - wait for a bit to be cleared
1049 * @word: the word being waited on, a kernel virtual address
1050 * @bit: the bit of the word being waited on
1051 * @action: the function used to sleep, which may take special actions
1052 * @mode: the task state to sleep in
1053 *
1054 * Use the standard hashed waitqueue table to wait for a bit
1055 * to be cleared, and allow the waiting action to be specified.
1056 * This is like wait_on_bit() but allows fine control of how the waiting
1057 * is done.
1058 *
1059 * Returned value will be zero if the bit was cleared, or non-zero
1060 * if the process received a signal and the mode permitted wakeup
1061 * on that signal.
1062 */
1063static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001064wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
1065 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001067 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!test_bit(bit, word))
1069 return 0;
1070 return out_of_line_wait_on_bit(word, bit, action, mode);
1071}
1072
1073/**
1074 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1075 * @word: the word being waited on, a kernel virtual address
1076 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * @mode: the task state to sleep in
1078 *
1079 * There is a standard hashed waitqueue table for generic use. This
1080 * is the part of the hashtable's accessor API that waits on a bit
1081 * when one intends to set it, for instance, trying to lock bitflags.
1082 * For instance, if one were to have waiters trying to set bitflag
1083 * and waiting for it to clear before setting it, one would call
1084 * wait_on_bit() in threads waiting to be able to set the bit.
1085 * One uses wait_on_bit_lock() where one is waiting for the bit to
1086 * clear with the intention of setting it, and when done, clearing it.
NeilBrown74316202014-07-07 15:16:04 +10001087 *
1088 * Returns zero if the bit was (eventually) found to be clear and was
1089 * set. Returns non-zero if a signal was delivered to the process and
1090 * the @mode allows that signal to wake the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Ingo Molnarfb869b62013-10-04 10:24:49 +02001092static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001093wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001094{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001095 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001096 if (!test_and_set_bit(bit, word))
1097 return 0;
1098 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1099}
1100
1101/**
1102 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1103 * @word: the word being waited on, a kernel virtual address
1104 * @bit: the bit of the word being waited on
1105 * @mode: the task state to sleep in
1106 *
1107 * Use the standard hashed waitqueue table to wait for a bit
1108 * to be cleared and then to atomically set it. This is similar
1109 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1110 * for the actual waiting.
1111 *
1112 * Returns zero if the bit was (eventually) found to be clear and was
1113 * set. Returns non-zero if a signal was delivered to the process and
1114 * the @mode allows that signal to wake the process.
1115 */
1116static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001117wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
NeilBrown74316202014-07-07 15:16:04 +10001118{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001119 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001120 if (!test_and_set_bit(bit, word))
1121 return 0;
1122 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1123}
1124
1125/**
1126 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1127 * @word: the word being waited on, a kernel virtual address
1128 * @bit: the bit of the word being waited on
1129 * @action: the function used to sleep, which may take special actions
1130 * @mode: the task state to sleep in
1131 *
1132 * Use the standard hashed waitqueue table to wait for a bit
1133 * to be cleared and then to set it, and allow the waiting action
1134 * to be specified.
1135 * This is like wait_on_bit() but allows fine control of how the waiting
1136 * is done.
1137 *
1138 * Returns zero if the bit was (eventually) found to be clear and was
1139 * set. Returns non-zero if a signal was delivered to the process and
1140 * the @mode allows that signal to wake the process.
1141 */
1142static inline int
Palmer Dabbelt7e605982015-04-30 21:19:56 -07001143wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
1144 unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001146 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!test_and_set_bit(bit, word))
1148 return 0;
1149 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1150}
David Howellscb655372013-05-10 19:50:26 +01001151
1152/**
1153 * wait_on_atomic_t - Wait for an atomic_t to become 0
1154 * @val: The atomic value being waited on, a kernel virtual address
1155 * @action: the function used to sleep, which may take special actions
1156 * @mode: the task state to sleep in
1157 *
1158 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1159 * the purpose of getting a waitqueue, but we set the key to a bit number
1160 * outside of the target 'word'.
1161 */
1162static inline
1163int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1164{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001165 might_sleep();
David Howellscb655372013-05-10 19:50:26 +01001166 if (atomic_read(val) == 0)
1167 return 0;
1168 return out_of_line_wait_on_atomic_t(val, action, mode);
1169}
Ingo Molnarfb869b62013-10-04 10:24:49 +02001170
1171#endif /* _LINUX_WAIT_H */