blob: 537d58eea8a084f1b51fdc3c702eb09758962406 [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 Zijlstra36df04b2014-10-29 12:21:57 +0100270#define __wait_event_freezable(wq, condition) \
271 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
272 schedule(); try_to_freeze())
273
274/**
275 * wait_event - sleep (or freeze) until a condition gets true
276 * @wq: the waitqueue to wait on
277 * @condition: a C expression for the event to wait for
278 *
279 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
280 * to system load) until the @condition evaluates to true. The
281 * @condition is checked each time the waitqueue @wq is woken up.
282 *
283 * wake_up() has to be called after changing any variable that could
284 * change the result of the wait condition.
285 */
286#define wait_event_freezable(wq, condition) \
287({ \
288 int __ret = 0; \
289 might_sleep(); \
290 if (!(condition)) \
291 __ret = __wait_event_freezable(wq, condition); \
292 __ret; \
293})
294
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200295#define __wait_event_timeout(wq, condition, timeout) \
296 ___wait_event(wq, ___wait_cond_timeout(condition), \
297 TASK_UNINTERRUPTIBLE, 0, timeout, \
298 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300/**
301 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
302 * @wq: the waitqueue to wait on
303 * @condition: a C expression for the event to wait for
304 * @timeout: timeout, in jiffies
305 *
306 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
307 * @condition evaluates to true. The @condition is checked each time
308 * the waitqueue @wq is woken up.
309 *
310 * wake_up() has to be called after changing any variable that could
311 * change the result of the wait condition.
312 *
Scot Doyle6b44f512014-08-24 17:12:27 +0000313 * Returns:
314 * 0 if the @condition evaluated to %false after the @timeout elapsed,
315 * 1 if the @condition evaluated to %true after the @timeout elapsed,
316 * or the remaining jiffies (at least 1) if the @condition evaluated
317 * to %true before the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
319#define wait_event_timeout(wq, condition, timeout) \
320({ \
321 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200322 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200323 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200324 __ret = __wait_event_timeout(wq, condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 __ret; \
326})
327
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100328#define __wait_event_freezable_timeout(wq, condition, timeout) \
329 ___wait_event(wq, ___wait_cond_timeout(condition), \
330 TASK_INTERRUPTIBLE, 0, timeout, \
331 __ret = schedule_timeout(__ret); try_to_freeze())
332
333/*
334 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
335 * increasing load and is freezable.
336 */
337#define wait_event_freezable_timeout(wq, condition, timeout) \
338({ \
339 long __ret = timeout; \
340 might_sleep(); \
341 if (!___wait_cond_timeout(condition)) \
342 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
343 __ret; \
344})
345
Shaohua Li82e06c82013-11-14 15:16:16 +1100346#define __wait_event_cmd(wq, condition, cmd1, cmd2) \
347 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
348 cmd1; schedule(); cmd2)
349
350/**
351 * wait_event_cmd - sleep until a condition gets true
352 * @wq: the waitqueue to wait on
353 * @condition: a C expression for the event to wait for
Masanari Iidaf434f7a2014-01-22 01:22:06 +0900354 * @cmd1: the command will be executed before sleep
355 * @cmd2: the command will be executed after sleep
Shaohua Li82e06c82013-11-14 15:16:16 +1100356 *
357 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
358 * @condition evaluates to true. The @condition is checked each time
359 * the waitqueue @wq is woken up.
360 *
361 * wake_up() has to be called after changing any variable that could
362 * change the result of the wait condition.
363 */
364#define wait_event_cmd(wq, condition, cmd1, cmd2) \
365do { \
366 if (condition) \
367 break; \
368 __wait_event_cmd(wq, condition, cmd1, cmd2); \
369} while (0)
370
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200371#define __wait_event_interruptible(wq, condition) \
372 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstraf13f4c42013-10-02 11:22:24 +0200373 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375/**
376 * wait_event_interruptible - sleep until a condition gets true
377 * @wq: the waitqueue to wait on
378 * @condition: a C expression for the event to wait for
379 *
380 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
381 * @condition evaluates to true or a signal is received.
382 * The @condition is checked each time the waitqueue @wq is woken up.
383 *
384 * wake_up() has to be called after changing any variable that could
385 * change the result of the wait condition.
386 *
387 * The function will return -ERESTARTSYS if it was interrupted by a
388 * signal and 0 if @condition evaluated to true.
389 */
390#define wait_event_interruptible(wq, condition) \
391({ \
392 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200393 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200395 __ret = __wait_event_interruptible(wq, condition); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 __ret; \
397})
398
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200399#define __wait_event_interruptible_timeout(wq, condition, timeout) \
400 ___wait_event(wq, ___wait_cond_timeout(condition), \
401 TASK_INTERRUPTIBLE, 0, timeout, \
402 __ret = schedule_timeout(__ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404/**
405 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
406 * @wq: the waitqueue to wait on
407 * @condition: a C expression for the event to wait for
408 * @timeout: timeout, in jiffies
409 *
410 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
411 * @condition evaluates to true or a signal is received.
412 * The @condition is checked each time the waitqueue @wq is woken up.
413 *
414 * wake_up() has to be called after changing any variable that could
415 * change the result of the wait condition.
416 *
Imre Deak4c663cf2013-05-24 15:55:09 -0700417 * Returns:
Scot Doyle6b44f512014-08-24 17:12:27 +0000418 * 0 if the @condition evaluated to %false after the @timeout elapsed,
419 * 1 if the @condition evaluated to %true after the @timeout elapsed,
420 * the remaining jiffies (at least 1) if the @condition evaluated
421 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
422 * interrupted by a signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
424#define wait_event_interruptible_timeout(wq, condition, timeout) \
425({ \
426 long __ret = timeout; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200427 might_sleep(); \
Oleg Nesterov89229152013-10-07 20:31:06 +0200428 if (!___wait_cond_timeout(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200429 __ret = __wait_event_interruptible_timeout(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200430 condition, timeout); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 __ret; \
432})
433
Kent Overstreet774a08b2013-05-07 16:18:43 -0700434#define __wait_event_hrtimeout(wq, condition, timeout, state) \
435({ \
436 int __ret = 0; \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700437 struct hrtimer_sleeper __t; \
438 \
439 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
440 HRTIMER_MODE_REL); \
441 hrtimer_init_sleeper(&__t, current); \
442 if ((timeout).tv64 != KTIME_MAX) \
443 hrtimer_start_range_ns(&__t.timer, timeout, \
444 current->timer_slack_ns, \
445 HRTIMER_MODE_REL); \
446 \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200447 __ret = ___wait_event(wq, condition, state, 0, 0, \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700448 if (!__t.task) { \
449 __ret = -ETIME; \
450 break; \
451 } \
Peter Zijlstraebdc1952013-10-02 11:22:32 +0200452 schedule()); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700453 \
454 hrtimer_cancel(&__t.timer); \
455 destroy_hrtimer_on_stack(&__t.timer); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700456 __ret; \
457})
458
459/**
460 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
461 * @wq: the waitqueue to wait on
462 * @condition: a C expression for the event to wait for
463 * @timeout: timeout, as a ktime_t
464 *
465 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
466 * @condition evaluates to true or a signal is received.
467 * The @condition is checked each time the waitqueue @wq is woken up.
468 *
469 * wake_up() has to be called after changing any variable that could
470 * change the result of the wait condition.
471 *
472 * The function returns 0 if @condition became true, or -ETIME if the timeout
473 * elapsed.
474 */
475#define wait_event_hrtimeout(wq, condition, timeout) \
476({ \
477 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200478 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700479 if (!(condition)) \
480 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
481 TASK_UNINTERRUPTIBLE); \
482 __ret; \
483})
484
485/**
486 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
487 * @wq: the waitqueue to wait on
488 * @condition: a C expression for the event to wait for
489 * @timeout: timeout, as a ktime_t
490 *
491 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
492 * @condition evaluates to true or a signal is received.
493 * The @condition is checked each time the waitqueue @wq is woken up.
494 *
495 * wake_up() has to be called after changing any variable that could
496 * change the result of the wait condition.
497 *
498 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
499 * interrupted by a signal, or -ETIME if the timeout elapsed.
500 */
501#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
502({ \
503 long __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200504 might_sleep(); \
Kent Overstreet774a08b2013-05-07 16:18:43 -0700505 if (!(condition)) \
506 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
507 TASK_INTERRUPTIBLE); \
508 __ret; \
509})
510
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200511#define __wait_event_interruptible_exclusive(wq, condition) \
512 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
Peter Zijlstra48c25212013-10-02 11:22:26 +0200513 schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515#define wait_event_interruptible_exclusive(wq, condition) \
516({ \
517 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200518 might_sleep(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200520 __ret = __wait_event_interruptible_exclusive(wq, condition);\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 __ret; \
522})
523
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200524
Peter Zijlstra36df04b2014-10-29 12:21:57 +0100525#define __wait_event_freezable_exclusive(wq, condition) \
526 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
527 schedule(); try_to_freeze())
528
529#define wait_event_freezable_exclusive(wq, condition) \
530({ \
531 int __ret = 0; \
532 might_sleep(); \
533 if (!(condition)) \
534 __ret = __wait_event_freezable_exclusive(wq, condition);\
535 __ret; \
536})
537
538
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200539#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
540({ \
541 int __ret = 0; \
542 DEFINE_WAIT(__wait); \
543 if (exclusive) \
544 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
545 do { \
546 if (likely(list_empty(&__wait.task_list))) \
547 __add_wait_queue_tail(&(wq), &__wait); \
548 set_current_state(TASK_INTERRUPTIBLE); \
549 if (signal_pending(current)) { \
550 __ret = -ERESTARTSYS; \
551 break; \
552 } \
553 if (irq) \
554 spin_unlock_irq(&(wq).lock); \
555 else \
556 spin_unlock(&(wq).lock); \
557 schedule(); \
558 if (irq) \
559 spin_lock_irq(&(wq).lock); \
560 else \
561 spin_lock(&(wq).lock); \
562 } while (!(condition)); \
563 __remove_wait_queue(&(wq), &__wait); \
564 __set_current_state(TASK_RUNNING); \
565 __ret; \
566})
567
568
569/**
570 * wait_event_interruptible_locked - sleep until a condition gets true
571 * @wq: the waitqueue to wait on
572 * @condition: a C expression for the event to wait for
573 *
574 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
575 * @condition evaluates to true or a signal is received.
576 * The @condition is checked each time the waitqueue @wq is woken up.
577 *
578 * It must be called with wq.lock being held. This spinlock is
579 * unlocked while sleeping but @condition testing is done while lock
580 * is held and when this macro exits the lock is held.
581 *
582 * The lock is locked/unlocked using spin_lock()/spin_unlock()
583 * functions which must match the way they are locked/unlocked outside
584 * of this macro.
585 *
586 * wake_up_locked() has to be called after changing any variable that could
587 * change the result of the wait condition.
588 *
589 * The function will return -ERESTARTSYS if it was interrupted by a
590 * signal and 0 if @condition evaluated to true.
591 */
592#define wait_event_interruptible_locked(wq, condition) \
593 ((condition) \
594 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
595
596/**
597 * wait_event_interruptible_locked_irq - sleep until a condition gets true
598 * @wq: the waitqueue to wait on
599 * @condition: a C expression for the event to wait for
600 *
601 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
602 * @condition evaluates to true or a signal is received.
603 * The @condition is checked each time the waitqueue @wq is woken up.
604 *
605 * It must be called with wq.lock being held. This spinlock is
606 * unlocked while sleeping but @condition testing is done while lock
607 * is held and when this macro exits the lock is held.
608 *
609 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
610 * functions which must match the way they are locked/unlocked outside
611 * of this macro.
612 *
613 * wake_up_locked() has to be called after changing any variable that could
614 * change the result of the wait condition.
615 *
616 * The function will return -ERESTARTSYS if it was interrupted by a
617 * signal and 0 if @condition evaluated to true.
618 */
619#define wait_event_interruptible_locked_irq(wq, condition) \
620 ((condition) \
621 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
622
623/**
624 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
625 * @wq: the waitqueue to wait on
626 * @condition: a C expression for the event to wait for
627 *
628 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
629 * @condition evaluates to true or a signal is received.
630 * The @condition is checked each time the waitqueue @wq is woken up.
631 *
632 * It must be called with wq.lock being held. This spinlock is
633 * unlocked while sleeping but @condition testing is done while lock
634 * is held and when this macro exits the lock is held.
635 *
636 * The lock is locked/unlocked using spin_lock()/spin_unlock()
637 * functions which must match the way they are locked/unlocked outside
638 * of this macro.
639 *
640 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
641 * set thus when other process waits process on the list if this
642 * process is awaken further processes are not considered.
643 *
644 * wake_up_locked() has to be called after changing any variable that could
645 * change the result of the wait condition.
646 *
647 * The function will return -ERESTARTSYS if it was interrupted by a
648 * signal and 0 if @condition evaluated to true.
649 */
650#define wait_event_interruptible_exclusive_locked(wq, condition) \
651 ((condition) \
652 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
653
654/**
655 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
656 * @wq: the waitqueue to wait on
657 * @condition: a C expression for the event to wait for
658 *
659 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
660 * @condition evaluates to true or a signal is received.
661 * The @condition is checked each time the waitqueue @wq is woken up.
662 *
663 * It must be called with wq.lock being held. This spinlock is
664 * unlocked while sleeping but @condition testing is done while lock
665 * is held and when this macro exits the lock is held.
666 *
667 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
668 * functions which must match the way they are locked/unlocked outside
669 * of this macro.
670 *
671 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
672 * set thus when other process waits process on the list if this
673 * process is awaken further processes are not considered.
674 *
675 * wake_up_locked() has to be called after changing any variable that could
676 * change the result of the wait condition.
677 *
678 * The function will return -ERESTARTSYS if it was interrupted by a
679 * signal and 0 if @condition evaluated to true.
680 */
681#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
682 ((condition) \
683 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
684
685
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200686#define __wait_event_killable(wq, condition) \
687 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500688
689/**
690 * wait_event_killable - sleep until a condition gets true
691 * @wq: the waitqueue to wait on
692 * @condition: a C expression for the event to wait for
693 *
694 * The process is put to sleep (TASK_KILLABLE) until the
695 * @condition evaluates to true or a signal is received.
696 * The @condition is checked each time the waitqueue @wq is woken up.
697 *
698 * wake_up() has to be called after changing any variable that could
699 * change the result of the wait condition.
700 *
701 * The function will return -ERESTARTSYS if it was interrupted by a
702 * signal and 0 if @condition evaluated to true.
703 */
704#define wait_event_killable(wq, condition) \
705({ \
706 int __ret = 0; \
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200707 might_sleep(); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500708 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200709 __ret = __wait_event_killable(wq, condition); \
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500710 __ret; \
711})
712
Lukas Czernereed8c022012-11-30 11:42:40 +0100713
714#define __wait_event_lock_irq(wq, condition, lock, cmd) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200715 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
716 spin_unlock_irq(&lock); \
717 cmd; \
718 schedule(); \
719 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100720
721/**
722 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
723 * condition is checked under the lock. This
724 * is expected to be called with the lock
725 * taken.
726 * @wq: the waitqueue to wait on
727 * @condition: a C expression for the event to wait for
728 * @lock: a locked spinlock_t, which will be released before cmd
729 * and schedule() and reacquired afterwards.
730 * @cmd: a command which is invoked outside the critical section before
731 * sleep
732 *
733 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
734 * @condition evaluates to true. The @condition is checked each time
735 * the waitqueue @wq is woken up.
736 *
737 * wake_up() has to be called after changing any variable that could
738 * change the result of the wait condition.
739 *
740 * This is supposed to be called while holding the lock. The lock is
741 * dropped before invoking the cmd and going to sleep and is reacquired
742 * afterwards.
743 */
744#define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
745do { \
746 if (condition) \
747 break; \
748 __wait_event_lock_irq(wq, condition, lock, cmd); \
749} while (0)
750
751/**
752 * wait_event_lock_irq - sleep until a condition gets true. The
753 * condition is checked under the lock. This
754 * is expected to be called with the lock
755 * taken.
756 * @wq: the waitqueue to wait on
757 * @condition: a C expression for the event to wait for
758 * @lock: a locked spinlock_t, which will be released before schedule()
759 * and reacquired afterwards.
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 going to sleep and is reacquired afterwards.
770 */
771#define wait_event_lock_irq(wq, condition, lock) \
772do { \
773 if (condition) \
774 break; \
775 __wait_event_lock_irq(wq, condition, lock, ); \
776} while (0)
777
778
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200779#define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200780 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200781 spin_unlock_irq(&lock); \
782 cmd; \
783 schedule(); \
Peter Zijlstra8fbd88f2013-10-02 11:22:28 +0200784 spin_lock_irq(&lock))
Lukas Czernereed8c022012-11-30 11:42:40 +0100785
786/**
787 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
788 * The condition is checked under the lock. This is expected to
789 * be called with the lock taken.
790 * @wq: the waitqueue to wait on
791 * @condition: a C expression for the event to wait for
792 * @lock: a locked spinlock_t, which will be released before cmd and
793 * schedule() and reacquired afterwards.
794 * @cmd: a command which is invoked outside the critical section before
795 * sleep
796 *
797 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
798 * @condition evaluates to true or a signal is received. The @condition is
799 * checked each time the waitqueue @wq is woken up.
800 *
801 * wake_up() has to be called after changing any variable that could
802 * change the result of the wait condition.
803 *
804 * This is supposed to be called while holding the lock. The lock is
805 * dropped before invoking the cmd and going to sleep and is reacquired
806 * afterwards.
807 *
808 * The macro will return -ERESTARTSYS if it was interrupted by a signal
809 * and 0 if @condition evaluated to true.
810 */
811#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
812({ \
813 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100814 if (!(condition)) \
Ingo Molnarfb869b62013-10-04 10:24:49 +0200815 __ret = __wait_event_interruptible_lock_irq(wq, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200816 condition, lock, cmd); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100817 __ret; \
818})
819
820/**
821 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
822 * The condition is checked under the lock. This is expected
823 * to be called with the lock taken.
824 * @wq: the waitqueue to wait on
825 * @condition: a C expression for the event to wait for
826 * @lock: a locked spinlock_t, which will be released before schedule()
827 * and reacquired afterwards.
828 *
829 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
830 * @condition evaluates to true or signal is received. The @condition is
831 * checked each time the waitqueue @wq is woken up.
832 *
833 * wake_up() has to be called after changing any variable that could
834 * change the result of the wait condition.
835 *
836 * This is supposed to be called while holding the lock. The lock is
837 * dropped before going to sleep and is reacquired afterwards.
838 *
839 * The macro will return -ERESTARTSYS if it was interrupted by a signal
840 * and 0 if @condition evaluated to true.
841 */
842#define wait_event_interruptible_lock_irq(wq, condition, lock) \
843({ \
844 int __ret = 0; \
Lukas Czernereed8c022012-11-30 11:42:40 +0100845 if (!(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200846 __ret = __wait_event_interruptible_lock_irq(wq, \
Thierry Reding92ec1182013-10-23 13:40:55 +0200847 condition, lock,); \
Lukas Czernereed8c022012-11-30 11:42:40 +0100848 __ret; \
849})
850
Ingo Molnarfb869b62013-10-04 10:24:49 +0200851#define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
852 lock, timeout) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200853 ___wait_event(wq, ___wait_cond_timeout(condition), \
Heiko Carstens7d716452013-10-31 12:48:14 +0100854 TASK_INTERRUPTIBLE, 0, timeout, \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200855 spin_unlock_irq(&lock); \
856 __ret = schedule_timeout(__ret); \
Peter Zijlstraa1dc68522013-10-02 11:22:29 +0200857 spin_lock_irq(&lock));
Martin Peschked79ff142013-08-22 17:45:36 +0200858
859/**
Ingo Molnarfb869b62013-10-04 10:24:49 +0200860 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
861 * true or a timeout elapses. The condition is checked under
862 * the lock. This is expected to be called with the lock taken.
Martin Peschked79ff142013-08-22 17:45:36 +0200863 * @wq: the waitqueue to wait on
864 * @condition: a C expression for the event to wait for
865 * @lock: a locked spinlock_t, which will be released before schedule()
866 * and reacquired afterwards.
867 * @timeout: timeout, in jiffies
868 *
869 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
870 * @condition evaluates to true or signal is received. The @condition is
871 * checked each time the waitqueue @wq is woken up.
872 *
873 * wake_up() has to be called after changing any variable that could
874 * change the result of the wait condition.
875 *
876 * This is supposed to be called while holding the lock. The lock is
877 * dropped before going to sleep and is reacquired afterwards.
878 *
879 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
880 * was interrupted by a signal, and the remaining jiffies otherwise
881 * if the condition evaluated to true before the timeout elapsed.
882 */
883#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
884 timeout) \
885({ \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200886 long __ret = timeout; \
Oleg Nesterov89229152013-10-07 20:31:06 +0200887 if (!___wait_cond_timeout(condition)) \
Peter Zijlstra35a2af92013-10-02 11:22:33 +0200888 __ret = __wait_event_interruptible_lock_irq_timeout( \
889 wq, condition, lock, timeout); \
Martin Peschked79ff142013-08-22 17:45:36 +0200890 __ret; \
891})
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/*
894 * Waitqueues which are removed from the waitqueue_head at wakeup time
895 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800896void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
897void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
Oleg Nesterovc2d81642013-10-07 18:18:24 +0200898long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800899void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Ingo Molnarfb869b62013-10-04 10:24:49 +0200900void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
Peter Zijlstra61ada522014-09-24 10:18:47 +0200901long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
902int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
904int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
905
Eric Dumazetbf368e42009-04-28 02:24:21 -0700906#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700908 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -0700909 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200910 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
912
Eric Dumazetbf368e42009-04-28 02:24:21 -0700913#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#define DEFINE_WAIT_BIT(name, word, bit) \
916 struct wait_bit_queue name = { \
917 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
918 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700919 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 .func = wake_bit_function, \
921 .task_list = \
922 LIST_HEAD_INIT((name).wait.task_list), \
923 }, \
924 }
925
926#define init_wait(wait) \
927 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700928 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 (wait)->func = autoremove_wake_function; \
930 INIT_LIST_HEAD(&(wait)->task_list); \
Evgeny Kuznetsov231d0ae2010-10-05 12:47:57 +0400931 (wait)->flags = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 } while (0)
933
NeilBrown74316202014-07-07 15:16:04 +1000934
NeilBrownc1221322014-07-07 15:16:04 +1000935extern int bit_wait(struct wait_bit_key *);
936extern int bit_wait_io(struct wait_bit_key *);
NeilBrowncbbce822014-09-25 13:55:19 +1000937extern int bit_wait_timeout(struct wait_bit_key *);
938extern int bit_wait_io_timeout(struct wait_bit_key *);
NeilBrown74316202014-07-07 15:16:04 +1000939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/**
941 * wait_on_bit - wait for a bit to be cleared
942 * @word: the word being waited on, a kernel virtual address
943 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 * @mode: the task state to sleep in
945 *
946 * There is a standard hashed waitqueue table for generic use. This
947 * is the part of the hashtable's accessor API that waits on a bit.
948 * For instance, if one were to have waiters on a bitflag, one would
949 * call wait_on_bit() in threads waiting for the bit to clear.
950 * One uses wait_on_bit() where one is waiting for the bit to clear,
951 * but has no intention of setting it.
NeilBrown74316202014-07-07 15:16:04 +1000952 * Returned value will be zero if the bit was cleared, or non-zero
953 * if the process received a signal and the mode permitted wakeup
954 * on that signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
Ingo Molnarfb869b62013-10-04 10:24:49 +0200956static inline int
NeilBrown74316202014-07-07 15:16:04 +1000957wait_on_bit(void *word, int bit, unsigned mode)
958{
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200959 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000960 if (!test_bit(bit, word))
961 return 0;
962 return out_of_line_wait_on_bit(word, bit,
963 bit_wait,
964 mode);
965}
966
967/**
968 * wait_on_bit_io - wait for a bit to be cleared
969 * @word: the word being waited on, a kernel virtual address
970 * @bit: the bit of the word being waited on
971 * @mode: the task state to sleep in
972 *
973 * Use the standard hashed waitqueue table to wait for a bit
974 * to be cleared. This is similar to wait_on_bit(), but calls
975 * io_schedule() instead of schedule() for the actual waiting.
976 *
977 * Returned value will be zero if the bit was cleared, or non-zero
978 * if the process received a signal and the mode permitted wakeup
979 * on that signal.
980 */
981static inline int
982wait_on_bit_io(void *word, int bit, unsigned mode)
983{
Peter Zijlstrae22b8862014-09-24 10:18:48 +0200984 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +1000985 if (!test_bit(bit, word))
986 return 0;
987 return out_of_line_wait_on_bit(word, bit,
988 bit_wait_io,
989 mode);
990}
991
992/**
Johan Hedberg44fc0e52015-01-30 13:14:36 +0200993 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
994 * @word: the word being waited on, a kernel virtual address
995 * @bit: the bit of the word being waited on
996 * @mode: the task state to sleep in
997 * @timeout: timeout, in jiffies
998 *
999 * Use the standard hashed waitqueue table to wait for a bit
1000 * to be cleared. This is similar to wait_on_bit(), except also takes a
1001 * timeout parameter.
1002 *
1003 * Returned value will be zero if the bit was cleared before the
1004 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1005 * received a signal and the mode permitted wakeup on that signal.
1006 */
1007static inline int
1008wait_on_bit_timeout(void *word, int bit, unsigned mode, unsigned long timeout)
1009{
1010 might_sleep();
1011 if (!test_bit(bit, word))
1012 return 0;
1013 return out_of_line_wait_on_bit_timeout(word, bit,
1014 bit_wait_timeout,
1015 mode, timeout);
1016}
1017
1018/**
NeilBrown74316202014-07-07 15:16:04 +10001019 * wait_on_bit_action - wait for a bit to be cleared
1020 * @word: the word being waited on, a kernel virtual address
1021 * @bit: the bit of the word being waited on
1022 * @action: the function used to sleep, which may take special actions
1023 * @mode: the task state to sleep in
1024 *
1025 * Use the standard hashed waitqueue table to wait for a bit
1026 * to be cleared, and allow the waiting action to be specified.
1027 * This is like wait_on_bit() but allows fine control of how the waiting
1028 * is done.
1029 *
1030 * Returned value will be zero if the bit was cleared, or non-zero
1031 * if the process received a signal and the mode permitted wakeup
1032 * on that signal.
1033 */
1034static inline int
NeilBrownc1221322014-07-07 15:16:04 +10001035wait_on_bit_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001037 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (!test_bit(bit, word))
1039 return 0;
1040 return out_of_line_wait_on_bit(word, bit, action, mode);
1041}
1042
1043/**
1044 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1045 * @word: the word being waited on, a kernel virtual address
1046 * @bit: the bit of the word being waited on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * @mode: the task state to sleep in
1048 *
1049 * There is a standard hashed waitqueue table for generic use. This
1050 * is the part of the hashtable's accessor API that waits on a bit
1051 * when one intends to set it, for instance, trying to lock bitflags.
1052 * For instance, if one were to have waiters trying to set bitflag
1053 * and waiting for it to clear before setting it, one would call
1054 * wait_on_bit() in threads waiting to be able to set the bit.
1055 * One uses wait_on_bit_lock() where one is waiting for the bit to
1056 * clear with the intention of setting it, and when done, clearing it.
NeilBrown74316202014-07-07 15:16:04 +10001057 *
1058 * Returns zero if the bit was (eventually) found to be clear and was
1059 * set. Returns non-zero if a signal was delivered to the process and
1060 * the @mode allows that signal to wake the process.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Ingo Molnarfb869b62013-10-04 10:24:49 +02001062static inline int
NeilBrown74316202014-07-07 15:16:04 +10001063wait_on_bit_lock(void *word, int bit, unsigned mode)
1064{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001065 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001066 if (!test_and_set_bit(bit, word))
1067 return 0;
1068 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1069}
1070
1071/**
1072 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1073 * @word: the word being waited on, a kernel virtual address
1074 * @bit: the bit of the word being waited on
1075 * @mode: the task state to sleep in
1076 *
1077 * Use the standard hashed waitqueue table to wait for a bit
1078 * to be cleared and then to atomically set it. This is similar
1079 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1080 * for the actual waiting.
1081 *
1082 * Returns zero if the bit was (eventually) found to be clear and was
1083 * set. Returns non-zero if a signal was delivered to the process and
1084 * the @mode allows that signal to wake the process.
1085 */
1086static inline int
1087wait_on_bit_lock_io(void *word, int bit, unsigned mode)
1088{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001089 might_sleep();
NeilBrown74316202014-07-07 15:16:04 +10001090 if (!test_and_set_bit(bit, word))
1091 return 0;
1092 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1093}
1094
1095/**
1096 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1097 * @word: the word being waited on, a kernel virtual address
1098 * @bit: the bit of the word being waited on
1099 * @action: the function used to sleep, which may take special actions
1100 * @mode: the task state to sleep in
1101 *
1102 * Use the standard hashed waitqueue table to wait for a bit
1103 * to be cleared and then to set it, and allow the waiting action
1104 * to be specified.
1105 * This is like wait_on_bit() but allows fine control of how the waiting
1106 * is done.
1107 *
1108 * Returns zero if the bit was (eventually) found to be clear and was
1109 * set. Returns non-zero if a signal was delivered to the process and
1110 * the @mode allows that signal to wake the process.
1111 */
1112static inline int
NeilBrownc1221322014-07-07 15:16:04 +10001113wait_on_bit_lock_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001115 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (!test_and_set_bit(bit, word))
1117 return 0;
1118 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1119}
David Howellscb655372013-05-10 19:50:26 +01001120
1121/**
1122 * wait_on_atomic_t - Wait for an atomic_t to become 0
1123 * @val: The atomic value being waited on, a kernel virtual address
1124 * @action: the function used to sleep, which may take special actions
1125 * @mode: the task state to sleep in
1126 *
1127 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1128 * the purpose of getting a waitqueue, but we set the key to a bit number
1129 * outside of the target 'word'.
1130 */
1131static inline
1132int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1133{
Peter Zijlstrae22b8862014-09-24 10:18:48 +02001134 might_sleep();
David Howellscb655372013-05-10 19:50:26 +01001135 if (atomic_read(val) == 0)
1136 return 0;
1137 return out_of_line_wait_on_atomic_t(val, action, mode);
1138}
Ingo Molnarfb869b62013-10-04 10:24:49 +02001139
1140#endif /* _LINUX_WAIT_H */