Linux 6.16-rc6
[linux-2.6-block.git] / include / linux / wait.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
2#ifndef _LINUX_WAIT_H
3#define _LINUX_WAIT_H
fb869b6e
IM
4/*
5 * Linux wait queue related types and methods
6 */
1da177e4
LT
7#include <linux/list.h>
8#include <linux/stddef.h>
9#include <linux/spinlock.h>
5b825c3a 10
1da177e4
LT
11#include <asm/current.h>
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
37acade0
MWO
21#define WQ_FLAG_CUSTOM 0x04
22#define WQ_FLAG_DONE 0x08
23#define WQ_FLAG_PRIORITY 0x10
61ada528 24
ac6424b9
IM
25/*
26 * A single wait-queue entry structure:
27 */
28struct wait_queue_entry {
fb869b6e 29 unsigned int flags;
fb869b6e
IM
30 void *private;
31 wait_queue_func_t func;
2055da97 32 struct list_head entry;
1da177e4
LT
33};
34
9d9d676f 35struct wait_queue_head {
fb869b6e 36 spinlock_t lock;
2055da97 37 struct list_head head;
1da177e4 38};
9d9d676f 39typedef struct wait_queue_head wait_queue_head_t;
1da177e4 40
8c65b4a6 41struct task_struct;
1da177e4
LT
42
43/*
44 * Macros for declaration and initialisaton of the datatypes
45 */
46
4b1c480b
IM
47#define __WAITQUEUE_INITIALIZER(name, tsk) { \
48 .private = tsk, \
49 .func = default_wake_function, \
2055da97 50 .entry = { NULL, NULL } }
1da177e4 51
4b1c480b 52#define DECLARE_WAITQUEUE(name, tsk) \
50816c48 53 struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
1da177e4 54
4b1c480b
IM
55#define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
56 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
77eccd0d 57 .head = LIST_HEAD_INIT(name.head) }
1da177e4
LT
58
59#define DECLARE_WAIT_QUEUE_HEAD(name) \
9d9d676f 60 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
1da177e4 61
9d9d676f 62extern void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *);
2fc39111 63
4b1c480b
IM
64#define init_waitqueue_head(wq_head) \
65 do { \
66 static struct lock_class_key __key; \
67 \
68 __init_waitqueue_head((wq_head), #wq_head, &__key); \
2fc39111 69 } while (0)
1da177e4 70
7259f0d0
PZ
71#ifdef CONFIG_LOCKDEP
72# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
73 ({ init_waitqueue_head(&name); name; })
74# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
9d9d676f 75 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
7259f0d0
PZ
76#else
77# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
78#endif
79
50816c48 80static inline void init_waitqueue_entry(struct wait_queue_entry *wq_entry, struct task_struct *p)
1da177e4 81{
50816c48
IM
82 wq_entry->flags = 0;
83 wq_entry->private = p;
84 wq_entry->func = default_wake_function;
1da177e4
LT
85}
86
fb869b6e 87static inline void
50816c48 88init_waitqueue_func_entry(struct wait_queue_entry *wq_entry, wait_queue_func_t func)
1da177e4 89{
50816c48
IM
90 wq_entry->flags = 0;
91 wq_entry->private = NULL;
92 wq_entry->func = func;
1da177e4
LT
93}
94
69e51e92
PZ
95/**
96 * waitqueue_active -- locklessly test for waiters on the queue
9d9d676f 97 * @wq_head: the waitqueue to test for waiters
69e51e92
PZ
98 *
99 * returns true if the wait list is not empty
100 *
101 * NOTE: this function is lockless and requires care, incorrect usage _will_
102 * lead to sporadic and non-obvious failure.
103 *
9d9d676f 104 * Use either while holding wait_queue_head::lock or when used for wakeups
8c1007fd 105 * with an extra smp_mb() like::
69e51e92
PZ
106 *
107 * CPU0 - waker CPU1 - waiter
108 *
109 * for (;;) {
4b1c480b 110 * @cond = true; prepare_to_wait(&wq_head, &wait, state);
69e51e92 111 * smp_mb(); // smp_mb() from set_current_state()
4b1c480b
IM
112 * if (waitqueue_active(wq_head)) if (@cond)
113 * wake_up(wq_head); break;
69e51e92
PZ
114 * schedule();
115 * }
4b1c480b 116 * finish_wait(&wq_head, &wait);
69e51e92
PZ
117 *
118 * Because without the explicit smp_mb() it's possible for the
119 * waitqueue_active() load to get hoisted over the @cond store such that we'll
120 * observe an empty wait list while the waiter might not observe @cond.
121 *
122 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
123 * which (when the lock is uncontended) are of roughly equal cost.
124 */
9d9d676f 125static inline int waitqueue_active(struct wait_queue_head *wq_head)
1da177e4 126{
2055da97 127 return !list_empty(&wq_head->head);
1da177e4
LT
128}
129
a6d81d30
JB
130/**
131 * wq_has_single_sleeper - check if there is only one sleeper
132 * @wq_head: wait queue head
133 *
134 * Returns true of wq_head has only one sleeper on the list.
135 *
136 * Please refer to the comment for waitqueue_active.
137 */
138static inline bool wq_has_single_sleeper(struct wait_queue_head *wq_head)
139{
140 return list_is_singular(&wq_head->head);
141}
142
1ce0bf50
HX
143/**
144 * wq_has_sleeper - check if there are any waiting processes
4b1c480b 145 * @wq_head: wait queue head
1ce0bf50 146 *
4b1c480b 147 * Returns true if wq_head has waiting processes
1ce0bf50
HX
148 *
149 * Please refer to the comment for waitqueue_active.
150 */
9d9d676f 151static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
1ce0bf50
HX
152{
153 /*
154 * We need to be sure we are in sync with the
155 * add_wait_queue modifications to the wait queue.
156 *
157 * This memory barrier should be paired with one on the
158 * waiting side.
159 */
160 smp_mb();
9d9d676f 161 return waitqueue_active(wq_head);
1ce0bf50
HX
162}
163
9d9d676f
IM
164extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
165extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
c4d51a52 166extern void add_wait_queue_priority(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
9d9d676f 167extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1da177e4 168
9d9d676f 169static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
1da177e4 170{
c4d51a52
DW
171 struct list_head *head = &wq_head->head;
172 struct wait_queue_entry *wq;
173
174 list_for_each_entry(wq, &wq_head->head, entry) {
175 if (!(wq->flags & WQ_FLAG_PRIORITY))
176 break;
177 head = &wq->entry;
178 }
179 list_add(&wq_entry->entry, head);
1da177e4
LT
180}
181
182/*
183 * Used for wake-one threads:
184 */
fb869b6e 185static inline void
9d9d676f 186__add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
a93d2f17 187{
50816c48 188 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
9d9d676f 189 __add_wait_queue(wq_head, wq_entry);
a93d2f17
CG
190}
191
9d9d676f 192static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
1da177e4 193{
2055da97 194 list_add_tail(&wq_entry->entry, &wq_head->head);
1da177e4
LT
195}
196
fb869b6e 197static inline void
9d9d676f 198__add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
a93d2f17 199{
50816c48 200 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
9d9d676f 201 __add_wait_queue_entry_tail(wq_head, wq_entry);
a93d2f17
CG
202}
203
fb869b6e 204static inline void
9d9d676f 205__remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
1da177e4 206{
2055da97 207 list_del(&wq_entry->entry);
1da177e4
LT
208}
209
ee7dc86b 210int __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
6f63904c 211void __wake_up_on_current_cpu(struct wait_queue_head *wq_head, unsigned int mode, void *key);
9d9d676f 212void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
ce4dd442 213void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
f94df989 214void __wake_up_locked_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
9d9d676f 215void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
ce4dd442 216void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode);
42288cb4 217void __wake_up_pollfree(struct wait_queue_head *wq_head);
1da177e4 218
e64d66c8
MW
219#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
220#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
221#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
63b20011
TG
222#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
223#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
900bbaae 224#define wake_up_sync(x) __wake_up_sync(x, TASK_NORMAL)
e64d66c8 225
1da177e4
LT
226#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
227#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
228#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
ce4dd442 229#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE)
1da177e4 230
0ccf831c 231/*
c0da3775 232 * Wakeup macros to be used to report events to the targets.
0ccf831c 233 */
3ad6f93e
AV
234#define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
235#define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
4b1c480b 236#define wake_up_poll(x, m) \
3ad6f93e 237 __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
6f63904c
AV
238#define wake_up_poll_on_current_cpu(x, m) \
239 __wake_up_on_current_cpu(x, TASK_NORMAL, poll_to_key(m))
4b1c480b 240#define wake_up_locked_poll(x, m) \
3ad6f93e 241 __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
4b1c480b 242#define wake_up_interruptible_poll(x, m) \
3ad6f93e 243 __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
4b1c480b 244#define wake_up_interruptible_sync_poll(x, m) \
ce4dd442 245 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
f94df989
DH
246#define wake_up_interruptible_sync_poll_locked(x, m) \
247 __wake_up_locked_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
0ccf831c 248
42288cb4
EB
249/**
250 * wake_up_pollfree - signal that a polled waitqueue is going away
251 * @wq_head: the wait queue head
252 *
253 * In the very rare cases where a ->poll() implementation uses a waitqueue whose
254 * lifetime is tied to a task rather than to the 'struct file' being polled,
255 * this function must be called before the waitqueue is freed so that
256 * non-blocking polls (e.g. epoll) are notified that the queue is going away.
257 *
258 * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via
259 * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU.
260 */
261static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
262{
263 /*
264 * For performance reasons, we don't always take the queue lock here.
265 * Therefore, we might race with someone removing the last entry from
266 * the queue, and proceed while they still hold the queue lock.
267 * However, rcu_read_lock() is required to be held in such cases, so we
268 * can safely proceed with an RCU-delayed free.
269 */
270 if (waitqueue_active(wq_head))
271 __wake_up_pollfree(wq_head);
272}
273
4b1c480b
IM
274#define ___wait_cond_timeout(condition) \
275({ \
276 bool __cond = (condition); \
277 if (__cond && !__ret) \
278 __ret = 1; \
279 __cond || !__ret; \
2953ef24
PZ
280})
281
4b1c480b
IM
282#define ___wait_is_interruptible(state) \
283 (!__builtin_constant_p(state) || \
5aec788a 284 (state & (TASK_INTERRUPTIBLE | TASK_WAKEKILL)))
41a1431b 285
50816c48 286extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
0176beaf 287
8b32201d
PZ
288/*
289 * The below macro ___wait_event() has an explicit shadow of the __ret
290 * variable when used from the wait_event_*() macros.
291 *
292 * This is so that both can use the ___wait_cond_timeout() construct
293 * to wrap the condition.
294 *
295 * The type inconsistency of the wait_event_*() __ret variable is also
296 * on purpose; we use long where we can return timeout values and int
297 * otherwise.
298 */
299
4b1c480b
IM
300#define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
301({ \
302 __label__ __out; \
303 struct wait_queue_entry __wq_entry; \
304 long __ret = ret; /* explicit shadow */ \
305 \
306 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
307 for (;;) { \
308 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
309 \
310 if (condition) \
311 break; \
312 \
313 if (___wait_is_interruptible(state) && __int) { \
314 __ret = __int; \
315 goto __out; \
316 } \
317 \
318 cmd; \
84654c7f
MG
319 \
320 if (condition) \
321 break; \
4b1c480b
IM
322 } \
323 finish_wait(&wq_head, &__wq_entry); \
324__out: __ret; \
35a2af94 325})
41a1431b 326
4b1c480b
IM
327#define __wait_event(wq_head, condition) \
328 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
35a2af94 329 schedule())
1da177e4
LT
330
331/**
332 * wait_event - sleep until a condition gets true
4b1c480b 333 * @wq_head: the waitqueue to wait on
1da177e4
LT
334 * @condition: a C expression for the event to wait for
335 *
336 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
337 * @condition evaluates to true. The @condition is checked each time
4b1c480b 338 * the waitqueue @wq_head is woken up.
1da177e4
LT
339 *
340 * wake_up() has to be called after changing any variable that could
341 * change the result of the wait condition.
342 */
4b1c480b
IM
343#define wait_event(wq_head, condition) \
344do { \
345 might_sleep(); \
346 if (condition) \
347 break; \
348 __wait_event(wq_head, condition); \
1da177e4
LT
349} while (0)
350
4b1c480b
IM
351#define __io_wait_event(wq_head, condition) \
352 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
2c561246
PZ
353 io_schedule())
354
355/*
356 * io_wait_event() -- like wait_event() but with io_schedule()
357 */
4b1c480b
IM
358#define io_wait_event(wq_head, condition) \
359do { \
360 might_sleep(); \
361 if (condition) \
362 break; \
363 __io_wait_event(wq_head, condition); \
2c561246
PZ
364} while (0)
365
4b1c480b 366#define __wait_event_freezable(wq_head, condition) \
f5d39b02
PZ
367 ___wait_event(wq_head, condition, (TASK_INTERRUPTIBLE|TASK_FREEZABLE), \
368 0, 0, schedule())
36df04bc
PZ
369
370/**
f4bcfa1d 371 * wait_event_freezable - sleep (or freeze) until a condition gets true
4b1c480b 372 * @wq_head: the waitqueue to wait on
36df04bc
PZ
373 * @condition: a C expression for the event to wait for
374 *
375 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
376 * to system load) until the @condition evaluates to true. The
4b1c480b 377 * @condition is checked each time the waitqueue @wq_head is woken up.
36df04bc
PZ
378 *
379 * wake_up() has to be called after changing any variable that could
380 * change the result of the wait condition.
381 */
4b1c480b
IM
382#define wait_event_freezable(wq_head, condition) \
383({ \
384 int __ret = 0; \
385 might_sleep(); \
386 if (!(condition)) \
387 __ret = __wait_event_freezable(wq_head, condition); \
388 __ret; \
36df04bc
PZ
389})
390
4b1c480b
IM
391#define __wait_event_timeout(wq_head, condition, timeout) \
392 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
393 TASK_UNINTERRUPTIBLE, 0, timeout, \
35a2af94 394 __ret = schedule_timeout(__ret))
1da177e4
LT
395
396/**
397 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
4b1c480b 398 * @wq_head: the waitqueue to wait on
1da177e4
LT
399 * @condition: a C expression for the event to wait for
400 * @timeout: timeout, in jiffies
401 *
402 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
403 * @condition evaluates to true. The @condition is checked each time
4b1c480b 404 * the waitqueue @wq_head is woken up.
1da177e4
LT
405 *
406 * wake_up() has to be called after changing any variable that could
407 * change the result of the wait condition.
408 *
6b44f519
SD
409 * Returns:
410 * 0 if the @condition evaluated to %false after the @timeout elapsed,
411 * 1 if the @condition evaluated to %true after the @timeout elapsed,
412 * or the remaining jiffies (at least 1) if the @condition evaluated
413 * to %true before the @timeout elapsed.
1da177e4 414 */
4b1c480b
IM
415#define wait_event_timeout(wq_head, condition, timeout) \
416({ \
417 long __ret = timeout; \
418 might_sleep(); \
419 if (!___wait_cond_timeout(condition)) \
420 __ret = __wait_event_timeout(wq_head, condition, timeout); \
421 __ret; \
1da177e4
LT
422})
423
4b1c480b
IM
424#define __wait_event_freezable_timeout(wq_head, condition, timeout) \
425 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
f5d39b02
PZ
426 (TASK_INTERRUPTIBLE|TASK_FREEZABLE), 0, timeout, \
427 __ret = schedule_timeout(__ret))
36df04bc
PZ
428
429/*
430 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
431 * increasing load and is freezable.
432 */
4b1c480b
IM
433#define wait_event_freezable_timeout(wq_head, condition, timeout) \
434({ \
435 long __ret = timeout; \
436 might_sleep(); \
437 if (!___wait_cond_timeout(condition)) \
438 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
439 __ret; \
36df04bc
PZ
440})
441
4b1c480b
IM
442#define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
443 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
9f3520c3
YL
444 cmd1; schedule(); cmd2)
445/*
446 * Just like wait_event_cmd(), except it sets exclusive flag
447 */
4b1c480b
IM
448#define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
449do { \
450 if (condition) \
451 break; \
452 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
9f3520c3
YL
453} while (0)
454
4b1c480b
IM
455#define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
456 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
82e06c81
SL
457 cmd1; schedule(); cmd2)
458
459/**
460 * wait_event_cmd - sleep until a condition gets true
4b1c480b 461 * @wq_head: the waitqueue to wait on
82e06c81 462 * @condition: a C expression for the event to wait for
f434f7af
MI
463 * @cmd1: the command will be executed before sleep
464 * @cmd2: the command will be executed after sleep
82e06c81
SL
465 *
466 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
467 * @condition evaluates to true. The @condition is checked each time
4b1c480b 468 * the waitqueue @wq_head is woken up.
82e06c81
SL
469 *
470 * wake_up() has to be called after changing any variable that could
471 * change the result of the wait condition.
472 */
4b1c480b
IM
473#define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
474do { \
475 if (condition) \
476 break; \
477 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
82e06c81
SL
478} while (0)
479
4b1c480b
IM
480#define __wait_event_interruptible(wq_head, condition) \
481 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
f13f4c41 482 schedule())
1da177e4
LT
483
484/**
485 * wait_event_interruptible - sleep until a condition gets true
4b1c480b 486 * @wq_head: the waitqueue to wait on
1da177e4
LT
487 * @condition: a C expression for the event to wait for
488 *
489 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
490 * @condition evaluates to true or a signal is received.
4b1c480b 491 * The @condition is checked each time the waitqueue @wq_head is woken up.
1da177e4
LT
492 *
493 * wake_up() has to be called after changing any variable that could
494 * change the result of the wait condition.
495 *
496 * The function will return -ERESTARTSYS if it was interrupted by a
497 * signal and 0 if @condition evaluated to true.
498 */
4b1c480b
IM
499#define wait_event_interruptible(wq_head, condition) \
500({ \
501 int __ret = 0; \
502 might_sleep(); \
503 if (!(condition)) \
504 __ret = __wait_event_interruptible(wq_head, condition); \
505 __ret; \
1da177e4
LT
506})
507
4b1c480b
IM
508#define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
509 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
510 TASK_INTERRUPTIBLE, 0, timeout, \
35a2af94 511 __ret = schedule_timeout(__ret))
1da177e4
LT
512
513/**
514 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
4b1c480b 515 * @wq_head: the waitqueue to wait on
1da177e4
LT
516 * @condition: a C expression for the event to wait for
517 * @timeout: timeout, in jiffies
518 *
519 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
520 * @condition evaluates to true or a signal is received.
4b1c480b 521 * The @condition is checked each time the waitqueue @wq_head is woken up.
1da177e4
LT
522 *
523 * wake_up() has to be called after changing any variable that could
524 * change the result of the wait condition.
525 *
4c663cfc 526 * Returns:
6b44f519
SD
527 * 0 if the @condition evaluated to %false after the @timeout elapsed,
528 * 1 if the @condition evaluated to %true after the @timeout elapsed,
529 * the remaining jiffies (at least 1) if the @condition evaluated
530 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
531 * interrupted by a signal.
1da177e4 532 */
4b1c480b
IM
533#define wait_event_interruptible_timeout(wq_head, condition, timeout) \
534({ \
535 long __ret = timeout; \
536 might_sleep(); \
537 if (!___wait_cond_timeout(condition)) \
538 __ret = __wait_event_interruptible_timeout(wq_head, \
539 condition, timeout); \
540 __ret; \
1da177e4
LT
541})
542
4b1c480b
IM
543#define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
544({ \
545 int __ret = 0; \
546 struct hrtimer_sleeper __t; \
547 \
211647e5
NC
548 hrtimer_setup_sleeper_on_stack(&__t, CLOCK_MONOTONIC, \
549 HRTIMER_MODE_REL); \
cceeeb6a
JL
550 if ((timeout) != KTIME_MAX) { \
551 hrtimer_set_expires_range_ns(&__t.timer, timeout, \
552 current->timer_slack_ns); \
553 hrtimer_sleeper_start_expires(&__t, HRTIMER_MODE_REL); \
554 } \
4b1c480b
IM
555 \
556 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
557 if (!__t.task) { \
558 __ret = -ETIME; \
559 break; \
560 } \
561 schedule()); \
562 \
563 hrtimer_cancel(&__t.timer); \
564 destroy_hrtimer_on_stack(&__t.timer); \
565 __ret; \
774a08b3
KO
566})
567
568/**
569 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
4b1c480b 570 * @wq_head: the waitqueue to wait on
774a08b3
KO
571 * @condition: a C expression for the event to wait for
572 * @timeout: timeout, as a ktime_t
573 *
574 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
575 * @condition evaluates to true or a signal is received.
4b1c480b 576 * The @condition is checked each time the waitqueue @wq_head is woken up.
774a08b3
KO
577 *
578 * wake_up() has to be called after changing any variable that could
579 * change the result of the wait condition.
580 *
581 * The function returns 0 if @condition became true, or -ETIME if the timeout
582 * elapsed.
583 */
4b1c480b
IM
584#define wait_event_hrtimeout(wq_head, condition, timeout) \
585({ \
586 int __ret = 0; \
587 might_sleep(); \
588 if (!(condition)) \
589 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
590 TASK_UNINTERRUPTIBLE); \
591 __ret; \
774a08b3
KO
592})
593
594/**
595 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
6c423f57 596 * @wq: the waitqueue to wait on
774a08b3
KO
597 * @condition: a C expression for the event to wait for
598 * @timeout: timeout, as a ktime_t
599 *
600 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
601 * @condition evaluates to true or a signal is received.
6c423f57 602 * The @condition is checked each time the waitqueue @wq is woken up.
774a08b3
KO
603 *
604 * wake_up() has to be called after changing any variable that could
605 * change the result of the wait condition.
606 *
607 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
608 * interrupted by a signal, or -ETIME if the timeout elapsed.
609 */
4b1c480b
IM
610#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
611({ \
612 long __ret = 0; \
613 might_sleep(); \
614 if (!(condition)) \
615 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
616 TASK_INTERRUPTIBLE); \
617 __ret; \
774a08b3
KO
618})
619
4b1c480b
IM
620#define __wait_event_interruptible_exclusive(wq, condition) \
621 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
48c25217 622 schedule())
1da177e4 623
4b1c480b
IM
624#define wait_event_interruptible_exclusive(wq, condition) \
625({ \
626 int __ret = 0; \
627 might_sleep(); \
628 if (!(condition)) \
629 __ret = __wait_event_interruptible_exclusive(wq, condition); \
630 __ret; \
1da177e4
LT
631})
632
4b1c480b
IM
633#define __wait_event_killable_exclusive(wq, condition) \
634 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
6a0fb306
AV
635 schedule())
636
4b1c480b
IM
637#define wait_event_killable_exclusive(wq, condition) \
638({ \
639 int __ret = 0; \
640 might_sleep(); \
641 if (!(condition)) \
642 __ret = __wait_event_killable_exclusive(wq, condition); \
643 __ret; \
6a0fb306
AV
644})
645
22c43c81 646
4b1c480b 647#define __wait_event_freezable_exclusive(wq, condition) \
f5d39b02
PZ
648 ___wait_event(wq, condition, (TASK_INTERRUPTIBLE|TASK_FREEZABLE), 1, 0,\
649 schedule())
36df04bc 650
4b1c480b
IM
651#define wait_event_freezable_exclusive(wq, condition) \
652({ \
653 int __ret = 0; \
654 might_sleep(); \
655 if (!(condition)) \
656 __ret = __wait_event_freezable_exclusive(wq, condition); \
657 __ret; \
36df04bc
PZ
658})
659
0957a2c1
N
660/**
661 * wait_event_idle - wait for a condition without contributing to system load
662 * @wq_head: the waitqueue to wait on
663 * @condition: a C expression for the event to wait for
664 *
665 * The process is put to sleep (TASK_IDLE) until the
666 * @condition evaluates to true.
667 * The @condition is checked each time the waitqueue @wq_head is woken up.
668 *
669 * wake_up() has to be called after changing any variable that could
670 * change the result of the wait condition.
671 *
672 */
673#define wait_event_idle(wq_head, condition) \
674do { \
675 might_sleep(); \
676 if (!(condition)) \
677 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
678} while (0)
679
680/**
681 * wait_event_idle_exclusive - wait for a condition with contributing to system load
682 * @wq_head: the waitqueue to wait on
683 * @condition: a C expression for the event to wait for
684 *
685 * The process is put to sleep (TASK_IDLE) until the
686 * @condition evaluates to true.
687 * The @condition is checked each time the waitqueue @wq_head is woken up.
688 *
689 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
690 * set thus if other processes wait on the same list, when this
691 * process is woken further processes are not considered.
692 *
693 * wake_up() has to be called after changing any variable that could
694 * change the result of the wait condition.
695 *
696 */
697#define wait_event_idle_exclusive(wq_head, condition) \
698do { \
699 might_sleep(); \
700 if (!(condition)) \
701 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
702} while (0)
703
704#define __wait_event_idle_timeout(wq_head, condition, timeout) \
705 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
706 TASK_IDLE, 0, timeout, \
707 __ret = schedule_timeout(__ret))
708
709/**
710 * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
711 * @wq_head: the waitqueue to wait on
712 * @condition: a C expression for the event to wait for
713 * @timeout: timeout, in jiffies
714 *
715 * The process is put to sleep (TASK_IDLE) until the
716 * @condition evaluates to true. The @condition is checked each time
717 * the waitqueue @wq_head is woken up.
718 *
719 * wake_up() has to be called after changing any variable that could
720 * change the result of the wait condition.
721 *
722 * Returns:
723 * 0 if the @condition evaluated to %false after the @timeout elapsed,
724 * 1 if the @condition evaluated to %true after the @timeout elapsed,
725 * or the remaining jiffies (at least 1) if the @condition evaluated
726 * to %true before the @timeout elapsed.
727 */
728#define wait_event_idle_timeout(wq_head, condition, timeout) \
729({ \
730 long __ret = timeout; \
731 might_sleep(); \
732 if (!___wait_cond_timeout(condition)) \
733 __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
734 __ret; \
735})
736
737#define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
738 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
739 TASK_IDLE, 1, timeout, \
740 __ret = schedule_timeout(__ret))
741
742/**
743 * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
744 * @wq_head: the waitqueue to wait on
745 * @condition: a C expression for the event to wait for
746 * @timeout: timeout, in jiffies
747 *
748 * The process is put to sleep (TASK_IDLE) until the
749 * @condition evaluates to true. The @condition is checked each time
750 * the waitqueue @wq_head is woken up.
751 *
752 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
753 * set thus if other processes wait on the same list, when this
754 * process is woken further processes are not considered.
755 *
756 * wake_up() has to be called after changing any variable that could
757 * change the result of the wait condition.
758 *
759 * Returns:
760 * 0 if the @condition evaluated to %false after the @timeout elapsed,
761 * 1 if the @condition evaluated to %true after the @timeout elapsed,
762 * or the remaining jiffies (at least 1) if the @condition evaluated
763 * to %true before the @timeout elapsed.
764 */
765#define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
766({ \
767 long __ret = timeout; \
768 might_sleep(); \
769 if (!___wait_cond_timeout(condition)) \
770 __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
771 __ret; \
772})
773
ac6424b9
IM
774extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
775extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
36df04bc 776
4b1c480b
IM
777#define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
778({ \
779 int __ret; \
780 DEFINE_WAIT(__wait); \
781 if (exclusive) \
782 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
783 do { \
784 __ret = fn(&(wq), &__wait); \
785 if (__ret) \
786 break; \
787 } while (!(condition)); \
788 __remove_wait_queue(&(wq), &__wait); \
789 __set_current_state(TASK_RUNNING); \
790 __ret; \
22c43c81
MN
791})
792
793
794/**
795 * wait_event_interruptible_locked - sleep until a condition gets true
796 * @wq: the waitqueue to wait on
797 * @condition: a C expression for the event to wait for
798 *
799 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
800 * @condition evaluates to true or a signal is received.
801 * The @condition is checked each time the waitqueue @wq is woken up.
802 *
803 * It must be called with wq.lock being held. This spinlock is
804 * unlocked while sleeping but @condition testing is done while lock
805 * is held and when this macro exits the lock is held.
806 *
807 * The lock is locked/unlocked using spin_lock()/spin_unlock()
808 * functions which must match the way they are locked/unlocked outside
809 * of this macro.
810 *
811 * wake_up_locked() has to be called after changing any variable that could
812 * change the result of the wait condition.
813 *
814 * The function will return -ERESTARTSYS if it was interrupted by a
815 * signal and 0 if @condition evaluated to true.
816 */
4b1c480b
IM
817#define wait_event_interruptible_locked(wq, condition) \
818 ((condition) \
bd0f9b35 819 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
22c43c81
MN
820
821/**
822 * wait_event_interruptible_locked_irq - sleep until a condition gets true
823 * @wq: the waitqueue to wait on
824 * @condition: a C expression for the event to wait for
825 *
826 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
827 * @condition evaluates to true or a signal is received.
828 * The @condition is checked each time the waitqueue @wq is woken up.
829 *
830 * It must be called with wq.lock being held. This spinlock is
831 * unlocked while sleeping but @condition testing is done while lock
832 * is held and when this macro exits the lock is held.
833 *
834 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
835 * functions which must match the way they are locked/unlocked outside
836 * of this macro.
837 *
838 * wake_up_locked() has to be called after changing any variable that could
839 * change the result of the wait condition.
840 *
841 * The function will return -ERESTARTSYS if it was interrupted by a
842 * signal and 0 if @condition evaluated to true.
843 */
4b1c480b
IM
844#define wait_event_interruptible_locked_irq(wq, condition) \
845 ((condition) \
bd0f9b35 846 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
22c43c81
MN
847
848/**
849 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
850 * @wq: the waitqueue to wait on
851 * @condition: a C expression for the event to wait for
852 *
853 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
854 * @condition evaluates to true or a signal is received.
855 * The @condition is checked each time the waitqueue @wq is woken up.
856 *
857 * It must be called with wq.lock being held. This spinlock is
858 * unlocked while sleeping but @condition testing is done while lock
859 * is held and when this macro exits the lock is held.
860 *
861 * The lock is locked/unlocked using spin_lock()/spin_unlock()
862 * functions which must match the way they are locked/unlocked outside
863 * of this macro.
864 *
865 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
866 * set thus when other process waits process on the list if this
867 * process is awaken further processes are not considered.
868 *
869 * wake_up_locked() has to be called after changing any variable that could
870 * change the result of the wait condition.
871 *
872 * The function will return -ERESTARTSYS if it was interrupted by a
873 * signal and 0 if @condition evaluated to true.
874 */
4b1c480b
IM
875#define wait_event_interruptible_exclusive_locked(wq, condition) \
876 ((condition) \
bd0f9b35 877 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
22c43c81
MN
878
879/**
880 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
881 * @wq: the waitqueue to wait on
882 * @condition: a C expression for the event to wait for
883 *
884 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
885 * @condition evaluates to true or a signal is received.
886 * The @condition is checked each time the waitqueue @wq is woken up.
887 *
888 * It must be called with wq.lock being held. This spinlock is
889 * unlocked while sleeping but @condition testing is done while lock
890 * is held and when this macro exits the lock is held.
891 *
892 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
893 * functions which must match the way they are locked/unlocked outside
894 * of this macro.
895 *
896 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
897 * set thus when other process waits process on the list if this
898 * process is awaken further processes are not considered.
899 *
900 * wake_up_locked() has to be called after changing any variable that could
901 * change the result of the wait condition.
902 *
903 * The function will return -ERESTARTSYS if it was interrupted by a
904 * signal and 0 if @condition evaluated to true.
905 */
4b1c480b
IM
906#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
907 ((condition) \
bd0f9b35 908 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
22c43c81
MN
909
910
4b1c480b 911#define __wait_event_killable(wq, condition) \
35a2af94 912 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
1411d5a7
MW
913
914/**
915 * wait_event_killable - sleep until a condition gets true
6c423f57 916 * @wq_head: the waitqueue to wait on
1411d5a7
MW
917 * @condition: a C expression for the event to wait for
918 *
919 * The process is put to sleep (TASK_KILLABLE) until the
920 * @condition evaluates to true or a signal is received.
6c423f57 921 * The @condition is checked each time the waitqueue @wq_head is woken up.
1411d5a7
MW
922 *
923 * wake_up() has to be called after changing any variable that could
924 * change the result of the wait condition.
925 *
926 * The function will return -ERESTARTSYS if it was interrupted by a
927 * signal and 0 if @condition evaluated to true.
928 */
4b1c480b
IM
929#define wait_event_killable(wq_head, condition) \
930({ \
931 int __ret = 0; \
932 might_sleep(); \
933 if (!(condition)) \
934 __ret = __wait_event_killable(wq_head, condition); \
935 __ret; \
1411d5a7
MW
936})
937
3f884a10
PZ
938#define __wait_event_state(wq, condition, state) \
939 ___wait_event(wq, condition, state, 0, 0, schedule())
940
941/**
942 * wait_event_state - sleep until a condition gets true
943 * @wq_head: the waitqueue to wait on
944 * @condition: a C expression for the event to wait for
945 * @state: state to sleep in
946 *
947 * The process is put to sleep (@state) until the @condition evaluates to true
948 * or a signal is received (when allowed by @state). The @condition is checked
949 * each time the waitqueue @wq_head is woken up.
950 *
951 * wake_up() has to be called after changing any variable that could
952 * change the result of the wait condition.
953 *
954 * The function will return -ERESTARTSYS if it was interrupted by a signal
955 * (when allowed by @state) and 0 if @condition evaluated to true.
956 */
957#define wait_event_state(wq_head, condition, state) \
958({ \
959 int __ret = 0; \
960 might_sleep(); \
961 if (!(condition)) \
962 __ret = __wait_event_state(wq_head, condition, state); \
963 __ret; \
964})
965
8ada9279
LR
966#define __wait_event_killable_timeout(wq_head, condition, timeout) \
967 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
968 TASK_KILLABLE, 0, timeout, \
969 __ret = schedule_timeout(__ret))
970
971/**
972 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
973 * @wq_head: the waitqueue to wait on
974 * @condition: a C expression for the event to wait for
975 * @timeout: timeout, in jiffies
976 *
977 * The process is put to sleep (TASK_KILLABLE) until the
978 * @condition evaluates to true or a kill signal is received.
979 * The @condition is checked each time the waitqueue @wq_head is woken up.
980 *
981 * wake_up() has to be called after changing any variable that could
982 * change the result of the wait condition.
983 *
984 * Returns:
985 * 0 if the @condition evaluated to %false after the @timeout elapsed,
986 * 1 if the @condition evaluated to %true after the @timeout elapsed,
987 * the remaining jiffies (at least 1) if the @condition evaluated
988 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
989 * interrupted by a kill signal.
990 *
991 * Only kill signals interrupt this process.
992 */
993#define wait_event_killable_timeout(wq_head, condition, timeout) \
994({ \
995 long __ret = timeout; \
996 might_sleep(); \
997 if (!___wait_cond_timeout(condition)) \
998 __ret = __wait_event_killable_timeout(wq_head, \
999 condition, timeout); \
1000 __ret; \
1001})
1002
eed8c02e 1003
4b1c480b
IM
1004#define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
1005 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
1006 spin_unlock_irq(&lock); \
1007 cmd; \
1008 schedule(); \
35a2af94 1009 spin_lock_irq(&lock))
eed8c02e
LC
1010
1011/**
1012 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
1013 * condition is checked under the lock. This
1014 * is expected to be called with the lock
1015 * taken.
4b1c480b 1016 * @wq_head: the waitqueue to wait on
eed8c02e
LC
1017 * @condition: a C expression for the event to wait for
1018 * @lock: a locked spinlock_t, which will be released before cmd
1019 * and schedule() and reacquired afterwards.
1020 * @cmd: a command which is invoked outside the critical section before
1021 * sleep
1022 *
1023 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
1024 * @condition evaluates to true. The @condition is checked each time
4b1c480b 1025 * the waitqueue @wq_head is woken up.
eed8c02e
LC
1026 *
1027 * wake_up() has to be called after changing any variable that could
1028 * change the result of the wait condition.
1029 *
1030 * This is supposed to be called while holding the lock. The lock is
1031 * dropped before invoking the cmd and going to sleep and is reacquired
1032 * afterwards.
1033 */
4b1c480b
IM
1034#define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
1035do { \
1036 if (condition) \
1037 break; \
1038 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
eed8c02e
LC
1039} while (0)
1040
1041/**
1042 * wait_event_lock_irq - sleep until a condition gets true. The
1043 * condition is checked under the lock. This
1044 * is expected to be called with the lock
1045 * taken.
4b1c480b 1046 * @wq_head: the waitqueue to wait on
eed8c02e
LC
1047 * @condition: a C expression for the event to wait for
1048 * @lock: a locked spinlock_t, which will be released before schedule()
1049 * and reacquired afterwards.
1050 *
1051 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
1052 * @condition evaluates to true. The @condition is checked each time
4b1c480b 1053 * the waitqueue @wq_head is woken up.
eed8c02e
LC
1054 *
1055 * wake_up() has to be called after changing any variable that could
1056 * change the result of the wait condition.
1057 *
1058 * This is supposed to be called while holding the lock. The lock is
1059 * dropped before going to sleep and is reacquired afterwards.
1060 */
4b1c480b
IM
1061#define wait_event_lock_irq(wq_head, condition, lock) \
1062do { \
1063 if (condition) \
1064 break; \
1065 __wait_event_lock_irq(wq_head, condition, lock, ); \
eed8c02e
LC
1066} while (0)
1067
1068
4b1c480b
IM
1069#define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
1070 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
1071 spin_unlock_irq(&lock); \
1072 cmd; \
1073 schedule(); \
8fbd88fa 1074 spin_lock_irq(&lock))
eed8c02e
LC
1075
1076/**
1077 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
1078 * The condition is checked under the lock. This is expected to
1079 * be called with the lock taken.
4b1c480b 1080 * @wq_head: the waitqueue to wait on
eed8c02e
LC
1081 * @condition: a C expression for the event to wait for
1082 * @lock: a locked spinlock_t, which will be released before cmd and
1083 * schedule() and reacquired afterwards.
1084 * @cmd: a command which is invoked outside the critical section before
1085 * sleep
1086 *
1087 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1088 * @condition evaluates to true or a signal is received. The @condition is
4b1c480b 1089 * checked each time the waitqueue @wq_head is woken up.
eed8c02e
LC
1090 *
1091 * wake_up() has to be called after changing any variable that could
1092 * change the result of the wait condition.
1093 *
1094 * This is supposed to be called while holding the lock. The lock is
1095 * dropped before invoking the cmd and going to sleep and is reacquired
1096 * afterwards.
1097 *
1098 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1099 * and 0 if @condition evaluated to true.
1100 */
4b1c480b
IM
1101#define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
1102({ \
1103 int __ret = 0; \
1104 if (!(condition)) \
1105 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1106 condition, lock, cmd); \
1107 __ret; \
eed8c02e
LC
1108})
1109
1110/**
1111 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1112 * The condition is checked under the lock. This is expected
1113 * to be called with the lock taken.
4b1c480b 1114 * @wq_head: the waitqueue to wait on
eed8c02e
LC
1115 * @condition: a C expression for the event to wait for
1116 * @lock: a locked spinlock_t, which will be released before schedule()
1117 * and reacquired afterwards.
1118 *
1119 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1120 * @condition evaluates to true or signal is received. The @condition is
4b1c480b 1121 * checked each time the waitqueue @wq_head is woken up.
eed8c02e
LC
1122 *
1123 * wake_up() has to be called after changing any variable that could
1124 * change the result of the wait condition.
1125 *
1126 * This is supposed to be called while holding the lock. The lock is
1127 * dropped before going to sleep and is reacquired afterwards.
1128 *
1129 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1130 * and 0 if @condition evaluated to true.
1131 */
4b1c480b
IM
1132#define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
1133({ \
1134 int __ret = 0; \
1135 if (!(condition)) \
1136 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1137 condition, lock,); \
1138 __ret; \
eed8c02e
LC
1139})
1140
25ab0bc3 1141#define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
4b1c480b 1142 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
25ab0bc3 1143 state, 0, timeout, \
4b1c480b
IM
1144 spin_unlock_irq(&lock); \
1145 __ret = schedule_timeout(__ret); \
a1dc6852 1146 spin_lock_irq(&lock));
d79ff142
MP
1147
1148/**
fb869b6e
IM
1149 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1150 * true or a timeout elapses. The condition is checked under
1151 * the lock. This is expected to be called with the lock taken.
4b1c480b 1152 * @wq_head: the waitqueue to wait on
d79ff142
MP
1153 * @condition: a C expression for the event to wait for
1154 * @lock: a locked spinlock_t, which will be released before schedule()
1155 * and reacquired afterwards.
1156 * @timeout: timeout, in jiffies
1157 *
1158 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1159 * @condition evaluates to true or signal is received. The @condition is
4b1c480b 1160 * checked each time the waitqueue @wq_head is woken up.
d79ff142
MP
1161 *
1162 * wake_up() has to be called after changing any variable that could
1163 * change the result of the wait condition.
1164 *
1165 * This is supposed to be called while holding the lock. The lock is
1166 * dropped before going to sleep and is reacquired afterwards.
1167 *
1168 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1169 * was interrupted by a signal, and the remaining jiffies otherwise
1170 * if the condition evaluated to true before the timeout elapsed.
1171 */
4b1c480b
IM
1172#define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1173 timeout) \
1174({ \
1175 long __ret = timeout; \
1176 if (!___wait_cond_timeout(condition)) \
25ab0bc3
NB
1177 __ret = __wait_event_lock_irq_timeout( \
1178 wq_head, condition, lock, timeout, \
1179 TASK_INTERRUPTIBLE); \
1180 __ret; \
1181})
1182
1183#define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout) \
1184({ \
1185 long __ret = timeout; \
1186 if (!___wait_cond_timeout(condition)) \
1187 __ret = __wait_event_lock_irq_timeout( \
1188 wq_head, condition, lock, timeout, \
1189 TASK_UNINTERRUPTIBLE); \
4b1c480b 1190 __ret; \
d79ff142
MP
1191})
1192
1da177e4
LT
1193/*
1194 * Waitqueues which are removed from the waitqueue_head at wakeup time
1195 */
9d9d676f 1196void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
11c7aa0d 1197bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
9d9d676f
IM
1198long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1199void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
50816c48
IM
1200long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1201int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1202int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1da177e4 1203
4b1c480b
IM
1204#define DEFINE_WAIT_FUNC(name, function) \
1205 struct wait_queue_entry name = { \
1206 .private = current, \
1207 .func = function, \
2055da97 1208 .entry = LIST_HEAD_INIT((name).entry), \
1da177e4
LT
1209 }
1210
bf368e4e
ED
1211#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1212
36d03cb3 1213#define init_wait_func(wait, function) \
4b1c480b
IM
1214 do { \
1215 (wait)->private = current; \
36d03cb3 1216 (wait)->func = function; \
2055da97 1217 INIT_LIST_HEAD(&(wait)->entry); \
4b1c480b 1218 (wait)->flags = 0; \
1da177e4
LT
1219 } while (0)
1220
36d03cb3
MS
1221#define init_wait(wait) init_wait_func(wait, autoremove_wake_function)
1222
9b3c4ab3
PZ
1223typedef int (*task_call_f)(struct task_struct *p, void *arg);
1224extern int task_call_func(struct task_struct *p, task_call_f func, void *arg);
2beaf328 1225
fb869b6e 1226#endif /* _LINUX_WAIT_H */