]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/wait.h
Merge tag 'powerpc-4.13-8' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[mirror_ubuntu-artful-kernel.git] / include / linux / wait.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_WAIT_H
2#define _LINUX_WAIT_H
fb869b6e
IM
3/*
4 * Linux wait queue related types and methods
5 */
1da177e4
LT
6#include <linux/list.h>
7#include <linux/stddef.h>
8#include <linux/spinlock.h>
5b825c3a 9
1da177e4 10#include <asm/current.h>
607ca46e 11#include <uapi/linux/wait.h>
1da177e4 12
ac6424b9 13typedef struct wait_queue_entry wait_queue_entry_t;
50816c48
IM
14
15typedef int (*wait_queue_func_t)(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
16int default_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
1da177e4 17
ac6424b9 18/* wait_queue_entry::flags */
61ada528
PZ
19#define WQ_FLAG_EXCLUSIVE 0x01
20#define WQ_FLAG_WOKEN 0x02
21
ac6424b9
IM
22/*
23 * A single wait-queue entry structure:
24 */
25struct wait_queue_entry {
fb869b6e 26 unsigned int flags;
fb869b6e
IM
27 void *private;
28 wait_queue_func_t func;
2055da97 29 struct list_head entry;
1da177e4
LT
30};
31
9d9d676f 32struct wait_queue_head {
fb869b6e 33 spinlock_t lock;
2055da97 34 struct list_head head;
1da177e4 35};
9d9d676f 36typedef struct wait_queue_head wait_queue_head_t;
1da177e4 37
8c65b4a6 38struct task_struct;
1da177e4
LT
39
40/*
41 * Macros for declaration and initialisaton of the datatypes
42 */
43
4b1c480b
IM
44#define __WAITQUEUE_INITIALIZER(name, tsk) { \
45 .private = tsk, \
46 .func = default_wake_function, \
2055da97 47 .entry = { NULL, NULL } }
1da177e4 48
4b1c480b 49#define DECLARE_WAITQUEUE(name, tsk) \
50816c48 50 struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
1da177e4 51
4b1c480b
IM
52#define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
53 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
2055da97 54 .head = { &(name).head, &(name).head } }
1da177e4
LT
55
56#define DECLARE_WAIT_QUEUE_HEAD(name) \
9d9d676f 57 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
1da177e4 58
9d9d676f 59extern void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *);
2fc39111 60
4b1c480b
IM
61#define init_waitqueue_head(wq_head) \
62 do { \
63 static struct lock_class_key __key; \
64 \
65 __init_waitqueue_head((wq_head), #wq_head, &__key); \
2fc39111 66 } while (0)
1da177e4 67
7259f0d0
PZ
68#ifdef CONFIG_LOCKDEP
69# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
70 ({ init_waitqueue_head(&name); name; })
71# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
9d9d676f 72 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
7259f0d0
PZ
73#else
74# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
75#endif
76
50816c48 77static inline void init_waitqueue_entry(struct wait_queue_entry *wq_entry, struct task_struct *p)
1da177e4 78{
50816c48
IM
79 wq_entry->flags = 0;
80 wq_entry->private = p;
81 wq_entry->func = default_wake_function;
1da177e4
LT
82}
83
fb869b6e 84static inline void
50816c48 85init_waitqueue_func_entry(struct wait_queue_entry *wq_entry, wait_queue_func_t func)
1da177e4 86{
50816c48
IM
87 wq_entry->flags = 0;
88 wq_entry->private = NULL;
89 wq_entry->func = func;
1da177e4
LT
90}
91
69e51e92
PZ
92/**
93 * waitqueue_active -- locklessly test for waiters on the queue
9d9d676f 94 * @wq_head: the waitqueue to test for waiters
69e51e92
PZ
95 *
96 * returns true if the wait list is not empty
97 *
98 * NOTE: this function is lockless and requires care, incorrect usage _will_
99 * lead to sporadic and non-obvious failure.
100 *
9d9d676f 101 * Use either while holding wait_queue_head::lock or when used for wakeups
69e51e92
PZ
102 * with an extra smp_mb() like:
103 *
104 * CPU0 - waker CPU1 - waiter
105 *
106 * for (;;) {
4b1c480b 107 * @cond = true; prepare_to_wait(&wq_head, &wait, state);
69e51e92 108 * smp_mb(); // smp_mb() from set_current_state()
4b1c480b
IM
109 * if (waitqueue_active(wq_head)) if (@cond)
110 * wake_up(wq_head); break;
69e51e92
PZ
111 * schedule();
112 * }
4b1c480b 113 * finish_wait(&wq_head, &wait);
69e51e92
PZ
114 *
115 * Because without the explicit smp_mb() it's possible for the
116 * waitqueue_active() load to get hoisted over the @cond store such that we'll
117 * observe an empty wait list while the waiter might not observe @cond.
118 *
119 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
120 * which (when the lock is uncontended) are of roughly equal cost.
121 */
9d9d676f 122static inline int waitqueue_active(struct wait_queue_head *wq_head)
1da177e4 123{
2055da97 124 return !list_empty(&wq_head->head);
1da177e4
LT
125}
126
1ce0bf50
HX
127/**
128 * wq_has_sleeper - check if there are any waiting processes
4b1c480b 129 * @wq_head: wait queue head
1ce0bf50 130 *
4b1c480b 131 * Returns true if wq_head has waiting processes
1ce0bf50
HX
132 *
133 * Please refer to the comment for waitqueue_active.
134 */
9d9d676f 135static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
1ce0bf50
HX
136{
137 /*
138 * We need to be sure we are in sync with the
139 * add_wait_queue modifications to the wait queue.
140 *
141 * This memory barrier should be paired with one on the
142 * waiting side.
143 */
144 smp_mb();
9d9d676f 145 return waitqueue_active(wq_head);
1ce0bf50
HX
146}
147
9d9d676f
IM
148extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
149extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
150extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1da177e4 151
9d9d676f 152static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
1da177e4 153{
2055da97 154 list_add(&wq_entry->entry, &wq_head->head);
1da177e4
LT
155}
156
157/*
158 * Used for wake-one threads:
159 */
fb869b6e 160static inline void
9d9d676f 161__add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
a93d2f17 162{
50816c48 163 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
9d9d676f 164 __add_wait_queue(wq_head, wq_entry);
a93d2f17
CG
165}
166
9d9d676f 167static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
1da177e4 168{
2055da97 169 list_add_tail(&wq_entry->entry, &wq_head->head);
1da177e4
LT
170}
171
fb869b6e 172static inline void
9d9d676f 173__add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
a93d2f17 174{
50816c48 175 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
9d9d676f 176 __add_wait_queue_entry_tail(wq_head, wq_entry);
a93d2f17
CG
177}
178
fb869b6e 179static inline void
9d9d676f 180__remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
1da177e4 181{
2055da97 182 list_del(&wq_entry->entry);
1da177e4
LT
183}
184
9d9d676f
IM
185void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
186void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
187void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
188void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
189void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr);
1da177e4 190
e64d66c8
MW
191#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
192#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
193#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
63b20011
TG
194#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
195#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
e64d66c8 196
1da177e4
LT
197#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
198#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
199#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
e64d66c8 200#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
1da177e4 201
0ccf831c 202/*
c0da3775 203 * Wakeup macros to be used to report events to the targets.
0ccf831c 204 */
4b1c480b 205#define wake_up_poll(x, m) \
c0da3775 206 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
4b1c480b 207#define wake_up_locked_poll(x, m) \
ac5be6b4 208 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
4b1c480b 209#define wake_up_interruptible_poll(x, m) \
c0da3775 210 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
4b1c480b 211#define wake_up_interruptible_sync_poll(x, m) \
c0da3775 212 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
0ccf831c 213
4b1c480b
IM
214#define ___wait_cond_timeout(condition) \
215({ \
216 bool __cond = (condition); \
217 if (__cond && !__ret) \
218 __ret = 1; \
219 __cond || !__ret; \
2953ef24
PZ
220})
221
4b1c480b
IM
222#define ___wait_is_interruptible(state) \
223 (!__builtin_constant_p(state) || \
224 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
41a1431b 225
50816c48 226extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
0176beaf 227
8b32201d
PZ
228/*
229 * The below macro ___wait_event() has an explicit shadow of the __ret
230 * variable when used from the wait_event_*() macros.
231 *
232 * This is so that both can use the ___wait_cond_timeout() construct
233 * to wrap the condition.
234 *
235 * The type inconsistency of the wait_event_*() __ret variable is also
236 * on purpose; we use long where we can return timeout values and int
237 * otherwise.
238 */
239
4b1c480b
IM
240#define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
241({ \
242 __label__ __out; \
243 struct wait_queue_entry __wq_entry; \
244 long __ret = ret; /* explicit shadow */ \
245 \
246 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
247 for (;;) { \
248 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
249 \
250 if (condition) \
251 break; \
252 \
253 if (___wait_is_interruptible(state) && __int) { \
254 __ret = __int; \
255 goto __out; \
256 } \
257 \
258 cmd; \
259 } \
260 finish_wait(&wq_head, &__wq_entry); \
261__out: __ret; \
35a2af94 262})
41a1431b 263
4b1c480b
IM
264#define __wait_event(wq_head, condition) \
265 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
35a2af94 266 schedule())
1da177e4
LT
267
268/**
269 * wait_event - sleep until a condition gets true
4b1c480b 270 * @wq_head: the waitqueue to wait on
1da177e4
LT
271 * @condition: a C expression for the event to wait for
272 *
273 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
274 * @condition evaluates to true. The @condition is checked each time
4b1c480b 275 * the waitqueue @wq_head is woken up.
1da177e4
LT
276 *
277 * wake_up() has to be called after changing any variable that could
278 * change the result of the wait condition.
279 */
4b1c480b
IM
280#define wait_event(wq_head, condition) \
281do { \
282 might_sleep(); \
283 if (condition) \
284 break; \
285 __wait_event(wq_head, condition); \
1da177e4
LT
286} while (0)
287
4b1c480b
IM
288#define __io_wait_event(wq_head, condition) \
289 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
2c561246
PZ
290 io_schedule())
291
292/*
293 * io_wait_event() -- like wait_event() but with io_schedule()
294 */
4b1c480b
IM
295#define io_wait_event(wq_head, condition) \
296do { \
297 might_sleep(); \
298 if (condition) \
299 break; \
300 __io_wait_event(wq_head, condition); \
2c561246
PZ
301} while (0)
302
4b1c480b
IM
303#define __wait_event_freezable(wq_head, condition) \
304 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
36df04bc
PZ
305 schedule(); try_to_freeze())
306
307/**
f4bcfa1d 308 * wait_event_freezable - sleep (or freeze) until a condition gets true
4b1c480b 309 * @wq_head: the waitqueue to wait on
36df04bc
PZ
310 * @condition: a C expression for the event to wait for
311 *
312 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
313 * to system load) until the @condition evaluates to true. The
4b1c480b 314 * @condition is checked each time the waitqueue @wq_head is woken up.
36df04bc
PZ
315 *
316 * wake_up() has to be called after changing any variable that could
317 * change the result of the wait condition.
318 */
4b1c480b
IM
319#define wait_event_freezable(wq_head, condition) \
320({ \
321 int __ret = 0; \
322 might_sleep(); \
323 if (!(condition)) \
324 __ret = __wait_event_freezable(wq_head, condition); \
325 __ret; \
36df04bc
PZ
326})
327
4b1c480b
IM
328#define __wait_event_timeout(wq_head, condition, timeout) \
329 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
330 TASK_UNINTERRUPTIBLE, 0, timeout, \
35a2af94 331 __ret = schedule_timeout(__ret))
1da177e4
LT
332
333/**
334 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
4b1c480b 335 * @wq_head: the waitqueue to wait on
1da177e4
LT
336 * @condition: a C expression for the event to wait for
337 * @timeout: timeout, in jiffies
338 *
339 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
340 * @condition evaluates to true. The @condition is checked each time
4b1c480b 341 * the waitqueue @wq_head is woken up.
1da177e4
LT
342 *
343 * wake_up() has to be called after changing any variable that could
344 * change the result of the wait condition.
345 *
6b44f519
SD
346 * Returns:
347 * 0 if the @condition evaluated to %false after the @timeout elapsed,
348 * 1 if the @condition evaluated to %true after the @timeout elapsed,
349 * or the remaining jiffies (at least 1) if the @condition evaluated
350 * to %true before the @timeout elapsed.
1da177e4 351 */
4b1c480b
IM
352#define wait_event_timeout(wq_head, condition, timeout) \
353({ \
354 long __ret = timeout; \
355 might_sleep(); \
356 if (!___wait_cond_timeout(condition)) \
357 __ret = __wait_event_timeout(wq_head, condition, timeout); \
358 __ret; \
1da177e4
LT
359})
360
4b1c480b
IM
361#define __wait_event_freezable_timeout(wq_head, condition, timeout) \
362 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
363 TASK_INTERRUPTIBLE, 0, timeout, \
36df04bc
PZ
364 __ret = schedule_timeout(__ret); try_to_freeze())
365
366/*
367 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
368 * increasing load and is freezable.
369 */
4b1c480b
IM
370#define wait_event_freezable_timeout(wq_head, condition, timeout) \
371({ \
372 long __ret = timeout; \
373 might_sleep(); \
374 if (!___wait_cond_timeout(condition)) \
375 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
376 __ret; \
36df04bc
PZ
377})
378
4b1c480b
IM
379#define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
380 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
9f3520c3
YL
381 cmd1; schedule(); cmd2)
382/*
383 * Just like wait_event_cmd(), except it sets exclusive flag
384 */
4b1c480b
IM
385#define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
386do { \
387 if (condition) \
388 break; \
389 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
9f3520c3
YL
390} while (0)
391
4b1c480b
IM
392#define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
393 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
82e06c81
SL
394 cmd1; schedule(); cmd2)
395
396/**
397 * wait_event_cmd - sleep until a condition gets true
4b1c480b 398 * @wq_head: the waitqueue to wait on
82e06c81 399 * @condition: a C expression for the event to wait for
f434f7af
MI
400 * @cmd1: the command will be executed before sleep
401 * @cmd2: the command will be executed after sleep
82e06c81
SL
402 *
403 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
404 * @condition evaluates to true. The @condition is checked each time
4b1c480b 405 * the waitqueue @wq_head is woken up.
82e06c81
SL
406 *
407 * wake_up() has to be called after changing any variable that could
408 * change the result of the wait condition.
409 */
4b1c480b
IM
410#define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
411do { \
412 if (condition) \
413 break; \
414 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
82e06c81
SL
415} while (0)
416
4b1c480b
IM
417#define __wait_event_interruptible(wq_head, condition) \
418 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
f13f4c41 419 schedule())
1da177e4
LT
420
421/**
422 * wait_event_interruptible - sleep until a condition gets true
4b1c480b 423 * @wq_head: the waitqueue to wait on
1da177e4
LT
424 * @condition: a C expression for the event to wait for
425 *
426 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
427 * @condition evaluates to true or a signal is received.
4b1c480b 428 * The @condition is checked each time the waitqueue @wq_head is woken up.
1da177e4
LT
429 *
430 * wake_up() has to be called after changing any variable that could
431 * change the result of the wait condition.
432 *
433 * The function will return -ERESTARTSYS if it was interrupted by a
434 * signal and 0 if @condition evaluated to true.
435 */
4b1c480b
IM
436#define wait_event_interruptible(wq_head, condition) \
437({ \
438 int __ret = 0; \
439 might_sleep(); \
440 if (!(condition)) \
441 __ret = __wait_event_interruptible(wq_head, condition); \
442 __ret; \
1da177e4
LT
443})
444
4b1c480b
IM
445#define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
446 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
447 TASK_INTERRUPTIBLE, 0, timeout, \
35a2af94 448 __ret = schedule_timeout(__ret))
1da177e4
LT
449
450/**
451 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
4b1c480b 452 * @wq_head: the waitqueue to wait on
1da177e4
LT
453 * @condition: a C expression for the event to wait for
454 * @timeout: timeout, in jiffies
455 *
456 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
457 * @condition evaluates to true or a signal is received.
4b1c480b 458 * The @condition is checked each time the waitqueue @wq_head is woken up.
1da177e4
LT
459 *
460 * wake_up() has to be called after changing any variable that could
461 * change the result of the wait condition.
462 *
4c663cfc 463 * Returns:
6b44f519
SD
464 * 0 if the @condition evaluated to %false after the @timeout elapsed,
465 * 1 if the @condition evaluated to %true after the @timeout elapsed,
466 * the remaining jiffies (at least 1) if the @condition evaluated
467 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
468 * interrupted by a signal.
1da177e4 469 */
4b1c480b
IM
470#define wait_event_interruptible_timeout(wq_head, condition, timeout) \
471({ \
472 long __ret = timeout; \
473 might_sleep(); \
474 if (!___wait_cond_timeout(condition)) \
475 __ret = __wait_event_interruptible_timeout(wq_head, \
476 condition, timeout); \
477 __ret; \
1da177e4
LT
478})
479
4b1c480b
IM
480#define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
481({ \
482 int __ret = 0; \
483 struct hrtimer_sleeper __t; \
484 \
485 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); \
486 hrtimer_init_sleeper(&__t, current); \
487 if ((timeout) != KTIME_MAX) \
488 hrtimer_start_range_ns(&__t.timer, timeout, \
489 current->timer_slack_ns, \
490 HRTIMER_MODE_REL); \
491 \
492 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
493 if (!__t.task) { \
494 __ret = -ETIME; \
495 break; \
496 } \
497 schedule()); \
498 \
499 hrtimer_cancel(&__t.timer); \
500 destroy_hrtimer_on_stack(&__t.timer); \
501 __ret; \
774a08b3
KO
502})
503
504/**
505 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
4b1c480b 506 * @wq_head: the waitqueue to wait on
774a08b3
KO
507 * @condition: a C expression for the event to wait for
508 * @timeout: timeout, as a ktime_t
509 *
510 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
511 * @condition evaluates to true or a signal is received.
4b1c480b 512 * The @condition is checked each time the waitqueue @wq_head is woken up.
774a08b3
KO
513 *
514 * wake_up() has to be called after changing any variable that could
515 * change the result of the wait condition.
516 *
517 * The function returns 0 if @condition became true, or -ETIME if the timeout
518 * elapsed.
519 */
4b1c480b
IM
520#define wait_event_hrtimeout(wq_head, condition, timeout) \
521({ \
522 int __ret = 0; \
523 might_sleep(); \
524 if (!(condition)) \
525 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
526 TASK_UNINTERRUPTIBLE); \
527 __ret; \
774a08b3
KO
528})
529
530/**
531 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
6c423f57 532 * @wq: the waitqueue to wait on
774a08b3
KO
533 * @condition: a C expression for the event to wait for
534 * @timeout: timeout, as a ktime_t
535 *
536 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
537 * @condition evaluates to true or a signal is received.
6c423f57 538 * The @condition is checked each time the waitqueue @wq is woken up.
774a08b3
KO
539 *
540 * wake_up() has to be called after changing any variable that could
541 * change the result of the wait condition.
542 *
543 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
544 * interrupted by a signal, or -ETIME if the timeout elapsed.
545 */
4b1c480b
IM
546#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
547({ \
548 long __ret = 0; \
549 might_sleep(); \
550 if (!(condition)) \
551 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
552 TASK_INTERRUPTIBLE); \
553 __ret; \
774a08b3
KO
554})
555
4b1c480b
IM
556#define __wait_event_interruptible_exclusive(wq, condition) \
557 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
48c25217 558 schedule())
1da177e4 559
4b1c480b
IM
560#define wait_event_interruptible_exclusive(wq, condition) \
561({ \
562 int __ret = 0; \
563 might_sleep(); \
564 if (!(condition)) \
565 __ret = __wait_event_interruptible_exclusive(wq, condition); \
566 __ret; \
1da177e4
LT
567})
568
4b1c480b
IM
569#define __wait_event_killable_exclusive(wq, condition) \
570 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
6a0fb306
AV
571 schedule())
572
4b1c480b
IM
573#define wait_event_killable_exclusive(wq, condition) \
574({ \
575 int __ret = 0; \
576 might_sleep(); \
577 if (!(condition)) \
578 __ret = __wait_event_killable_exclusive(wq, condition); \
579 __ret; \
6a0fb306
AV
580})
581
22c43c81 582
4b1c480b
IM
583#define __wait_event_freezable_exclusive(wq, condition) \
584 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
36df04bc
PZ
585 schedule(); try_to_freeze())
586
4b1c480b
IM
587#define wait_event_freezable_exclusive(wq, condition) \
588({ \
589 int __ret = 0; \
590 might_sleep(); \
591 if (!(condition)) \
592 __ret = __wait_event_freezable_exclusive(wq, condition); \
593 __ret; \
36df04bc
PZ
594})
595
ac6424b9
IM
596extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
597extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
36df04bc 598
4b1c480b
IM
599#define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
600({ \
601 int __ret; \
602 DEFINE_WAIT(__wait); \
603 if (exclusive) \
604 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
605 do { \
606 __ret = fn(&(wq), &__wait); \
607 if (__ret) \
608 break; \
609 } while (!(condition)); \
610 __remove_wait_queue(&(wq), &__wait); \
611 __set_current_state(TASK_RUNNING); \
612 __ret; \
22c43c81
MN
613})
614
615
616/**
617 * wait_event_interruptible_locked - sleep until a condition gets true
618 * @wq: the waitqueue to wait on
619 * @condition: a C expression for the event to wait for
620 *
621 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
622 * @condition evaluates to true or a signal is received.
623 * The @condition is checked each time the waitqueue @wq is woken up.
624 *
625 * It must be called with wq.lock being held. This spinlock is
626 * unlocked while sleeping but @condition testing is done while lock
627 * is held and when this macro exits the lock is held.
628 *
629 * The lock is locked/unlocked using spin_lock()/spin_unlock()
630 * functions which must match the way they are locked/unlocked outside
631 * of this macro.
632 *
633 * wake_up_locked() has to be called after changing any variable that could
634 * change the result of the wait condition.
635 *
636 * The function will return -ERESTARTSYS if it was interrupted by a
637 * signal and 0 if @condition evaluated to true.
638 */
4b1c480b
IM
639#define wait_event_interruptible_locked(wq, condition) \
640 ((condition) \
bd0f9b35 641 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
22c43c81
MN
642
643/**
644 * wait_event_interruptible_locked_irq - sleep until a condition gets true
645 * @wq: the waitqueue to wait on
646 * @condition: a C expression for the event to wait for
647 *
648 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
649 * @condition evaluates to true or a signal is received.
650 * The @condition is checked each time the waitqueue @wq is woken up.
651 *
652 * It must be called with wq.lock being held. This spinlock is
653 * unlocked while sleeping but @condition testing is done while lock
654 * is held and when this macro exits the lock is held.
655 *
656 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
657 * functions which must match the way they are locked/unlocked outside
658 * of this macro.
659 *
660 * wake_up_locked() has to be called after changing any variable that could
661 * change the result of the wait condition.
662 *
663 * The function will return -ERESTARTSYS if it was interrupted by a
664 * signal and 0 if @condition evaluated to true.
665 */
4b1c480b
IM
666#define wait_event_interruptible_locked_irq(wq, condition) \
667 ((condition) \
bd0f9b35 668 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
22c43c81
MN
669
670/**
671 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
672 * @wq: the waitqueue to wait on
673 * @condition: a C expression for the event to wait for
674 *
675 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
676 * @condition evaluates to true or a signal is received.
677 * The @condition is checked each time the waitqueue @wq is woken up.
678 *
679 * It must be called with wq.lock being held. This spinlock is
680 * unlocked while sleeping but @condition testing is done while lock
681 * is held and when this macro exits the lock is held.
682 *
683 * The lock is locked/unlocked using spin_lock()/spin_unlock()
684 * functions which must match the way they are locked/unlocked outside
685 * of this macro.
686 *
687 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
688 * set thus when other process waits process on the list if this
689 * process is awaken further processes are not considered.
690 *
691 * wake_up_locked() has to be called after changing any variable that could
692 * change the result of the wait condition.
693 *
694 * The function will return -ERESTARTSYS if it was interrupted by a
695 * signal and 0 if @condition evaluated to true.
696 */
4b1c480b
IM
697#define wait_event_interruptible_exclusive_locked(wq, condition) \
698 ((condition) \
bd0f9b35 699 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
22c43c81
MN
700
701/**
702 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
703 * @wq: the waitqueue to wait on
704 * @condition: a C expression for the event to wait for
705 *
706 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
707 * @condition evaluates to true or a signal is received.
708 * The @condition is checked each time the waitqueue @wq is woken up.
709 *
710 * It must be called with wq.lock being held. This spinlock is
711 * unlocked while sleeping but @condition testing is done while lock
712 * is held and when this macro exits the lock is held.
713 *
714 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
715 * functions which must match the way they are locked/unlocked outside
716 * of this macro.
717 *
718 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
719 * set thus when other process waits process on the list if this
720 * process is awaken further processes are not considered.
721 *
722 * wake_up_locked() has to be called after changing any variable that could
723 * change the result of the wait condition.
724 *
725 * The function will return -ERESTARTSYS if it was interrupted by a
726 * signal and 0 if @condition evaluated to true.
727 */
4b1c480b
IM
728#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
729 ((condition) \
bd0f9b35 730 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
22c43c81
MN
731
732
4b1c480b 733#define __wait_event_killable(wq, condition) \
35a2af94 734 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
1411d5a7
MW
735
736/**
737 * wait_event_killable - sleep until a condition gets true
6c423f57 738 * @wq_head: the waitqueue to wait on
1411d5a7
MW
739 * @condition: a C expression for the event to wait for
740 *
741 * The process is put to sleep (TASK_KILLABLE) until the
742 * @condition evaluates to true or a signal is received.
6c423f57 743 * The @condition is checked each time the waitqueue @wq_head is woken up.
1411d5a7
MW
744 *
745 * wake_up() has to be called after changing any variable that could
746 * change the result of the wait condition.
747 *
748 * The function will return -ERESTARTSYS if it was interrupted by a
749 * signal and 0 if @condition evaluated to true.
750 */
4b1c480b
IM
751#define wait_event_killable(wq_head, condition) \
752({ \
753 int __ret = 0; \
754 might_sleep(); \
755 if (!(condition)) \
756 __ret = __wait_event_killable(wq_head, condition); \
757 __ret; \
1411d5a7
MW
758})
759
8ada9279
LR
760#define __wait_event_killable_timeout(wq_head, condition, timeout) \
761 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
762 TASK_KILLABLE, 0, timeout, \
763 __ret = schedule_timeout(__ret))
764
765/**
766 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
767 * @wq_head: the waitqueue to wait on
768 * @condition: a C expression for the event to wait for
769 * @timeout: timeout, in jiffies
770 *
771 * The process is put to sleep (TASK_KILLABLE) until the
772 * @condition evaluates to true or a kill signal is received.
773 * The @condition is checked each time the waitqueue @wq_head is woken up.
774 *
775 * wake_up() has to be called after changing any variable that could
776 * change the result of the wait condition.
777 *
778 * Returns:
779 * 0 if the @condition evaluated to %false after the @timeout elapsed,
780 * 1 if the @condition evaluated to %true after the @timeout elapsed,
781 * the remaining jiffies (at least 1) if the @condition evaluated
782 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
783 * interrupted by a kill signal.
784 *
785 * Only kill signals interrupt this process.
786 */
787#define wait_event_killable_timeout(wq_head, condition, timeout) \
788({ \
789 long __ret = timeout; \
790 might_sleep(); \
791 if (!___wait_cond_timeout(condition)) \
792 __ret = __wait_event_killable_timeout(wq_head, \
793 condition, timeout); \
794 __ret; \
795})
796
eed8c02e 797
4b1c480b
IM
798#define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
799 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
800 spin_unlock_irq(&lock); \
801 cmd; \
802 schedule(); \
35a2af94 803 spin_lock_irq(&lock))
eed8c02e
LC
804
805/**
806 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
807 * condition is checked under the lock. This
808 * is expected to be called with the lock
809 * taken.
4b1c480b 810 * @wq_head: the waitqueue to wait on
eed8c02e
LC
811 * @condition: a C expression for the event to wait for
812 * @lock: a locked spinlock_t, which will be released before cmd
813 * and schedule() and reacquired afterwards.
814 * @cmd: a command which is invoked outside the critical section before
815 * sleep
816 *
817 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
818 * @condition evaluates to true. The @condition is checked each time
4b1c480b 819 * the waitqueue @wq_head is woken up.
eed8c02e
LC
820 *
821 * wake_up() has to be called after changing any variable that could
822 * change the result of the wait condition.
823 *
824 * This is supposed to be called while holding the lock. The lock is
825 * dropped before invoking the cmd and going to sleep and is reacquired
826 * afterwards.
827 */
4b1c480b
IM
828#define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
829do { \
830 if (condition) \
831 break; \
832 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
eed8c02e
LC
833} while (0)
834
835/**
836 * wait_event_lock_irq - sleep until a condition gets true. The
837 * condition is checked under the lock. This
838 * is expected to be called with the lock
839 * taken.
4b1c480b 840 * @wq_head: the waitqueue to wait on
eed8c02e
LC
841 * @condition: a C expression for the event to wait for
842 * @lock: a locked spinlock_t, which will be released before schedule()
843 * and reacquired afterwards.
844 *
845 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
846 * @condition evaluates to true. The @condition is checked each time
4b1c480b 847 * the waitqueue @wq_head is woken up.
eed8c02e
LC
848 *
849 * wake_up() has to be called after changing any variable that could
850 * change the result of the wait condition.
851 *
852 * This is supposed to be called while holding the lock. The lock is
853 * dropped before going to sleep and is reacquired afterwards.
854 */
4b1c480b
IM
855#define wait_event_lock_irq(wq_head, condition, lock) \
856do { \
857 if (condition) \
858 break; \
859 __wait_event_lock_irq(wq_head, condition, lock, ); \
eed8c02e
LC
860} while (0)
861
862
4b1c480b
IM
863#define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
864 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
865 spin_unlock_irq(&lock); \
866 cmd; \
867 schedule(); \
8fbd88fa 868 spin_lock_irq(&lock))
eed8c02e
LC
869
870/**
871 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
872 * The condition is checked under the lock. This is expected to
873 * be called with the lock taken.
4b1c480b 874 * @wq_head: the waitqueue to wait on
eed8c02e
LC
875 * @condition: a C expression for the event to wait for
876 * @lock: a locked spinlock_t, which will be released before cmd and
877 * schedule() and reacquired afterwards.
878 * @cmd: a command which is invoked outside the critical section before
879 * sleep
880 *
881 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
882 * @condition evaluates to true or a signal is received. The @condition is
4b1c480b 883 * checked each time the waitqueue @wq_head is woken up.
eed8c02e
LC
884 *
885 * wake_up() has to be called after changing any variable that could
886 * change the result of the wait condition.
887 *
888 * This is supposed to be called while holding the lock. The lock is
889 * dropped before invoking the cmd and going to sleep and is reacquired
890 * afterwards.
891 *
892 * The macro will return -ERESTARTSYS if it was interrupted by a signal
893 * and 0 if @condition evaluated to true.
894 */
4b1c480b
IM
895#define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
896({ \
897 int __ret = 0; \
898 if (!(condition)) \
899 __ret = __wait_event_interruptible_lock_irq(wq_head, \
900 condition, lock, cmd); \
901 __ret; \
eed8c02e
LC
902})
903
904/**
905 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
906 * The condition is checked under the lock. This is expected
907 * to be called with the lock taken.
4b1c480b 908 * @wq_head: the waitqueue to wait on
eed8c02e
LC
909 * @condition: a C expression for the event to wait for
910 * @lock: a locked spinlock_t, which will be released before schedule()
911 * and reacquired afterwards.
912 *
913 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
914 * @condition evaluates to true or signal is received. The @condition is
4b1c480b 915 * checked each time the waitqueue @wq_head is woken up.
eed8c02e
LC
916 *
917 * wake_up() has to be called after changing any variable that could
918 * change the result of the wait condition.
919 *
920 * This is supposed to be called while holding the lock. The lock is
921 * dropped before going to sleep and is reacquired afterwards.
922 *
923 * The macro will return -ERESTARTSYS if it was interrupted by a signal
924 * and 0 if @condition evaluated to true.
925 */
4b1c480b
IM
926#define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
927({ \
928 int __ret = 0; \
929 if (!(condition)) \
930 __ret = __wait_event_interruptible_lock_irq(wq_head, \
931 condition, lock,); \
932 __ret; \
eed8c02e
LC
933})
934
4b1c480b
IM
935#define __wait_event_interruptible_lock_irq_timeout(wq_head, condition, \
936 lock, timeout) \
937 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
938 TASK_INTERRUPTIBLE, 0, timeout, \
939 spin_unlock_irq(&lock); \
940 __ret = schedule_timeout(__ret); \
a1dc6852 941 spin_lock_irq(&lock));
d79ff142
MP
942
943/**
fb869b6e
IM
944 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
945 * true or a timeout elapses. The condition is checked under
946 * the lock. This is expected to be called with the lock taken.
4b1c480b 947 * @wq_head: the waitqueue to wait on
d79ff142
MP
948 * @condition: a C expression for the event to wait for
949 * @lock: a locked spinlock_t, which will be released before schedule()
950 * and reacquired afterwards.
951 * @timeout: timeout, in jiffies
952 *
953 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
954 * @condition evaluates to true or signal is received. The @condition is
4b1c480b 955 * checked each time the waitqueue @wq_head is woken up.
d79ff142
MP
956 *
957 * wake_up() has to be called after changing any variable that could
958 * change the result of the wait condition.
959 *
960 * This is supposed to be called while holding the lock. The lock is
961 * dropped before going to sleep and is reacquired afterwards.
962 *
963 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
964 * was interrupted by a signal, and the remaining jiffies otherwise
965 * if the condition evaluated to true before the timeout elapsed.
966 */
4b1c480b
IM
967#define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
968 timeout) \
969({ \
970 long __ret = timeout; \
971 if (!___wait_cond_timeout(condition)) \
972 __ret = __wait_event_interruptible_lock_irq_timeout( \
973 wq_head, condition, lock, timeout); \
974 __ret; \
d79ff142
MP
975})
976
1da177e4
LT
977/*
978 * Waitqueues which are removed from the waitqueue_head at wakeup time
979 */
9d9d676f
IM
980void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
981void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
982long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
983void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
50816c48
IM
984long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
985int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
986int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1da177e4 987
4b1c480b
IM
988#define DEFINE_WAIT_FUNC(name, function) \
989 struct wait_queue_entry name = { \
990 .private = current, \
991 .func = function, \
2055da97 992 .entry = LIST_HEAD_INIT((name).entry), \
1da177e4
LT
993 }
994
bf368e4e
ED
995#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
996
4b1c480b
IM
997#define init_wait(wait) \
998 do { \
999 (wait)->private = current; \
1000 (wait)->func = autoremove_wake_function; \
2055da97 1001 INIT_LIST_HEAD(&(wait)->entry); \
4b1c480b 1002 (wait)->flags = 0; \
1da177e4
LT
1003 } while (0)
1004
fb869b6e 1005#endif /* _LINUX_WAIT_H */