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