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