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