5 #include <linux/list.h>
6 #include <linux/stddef.h>
7 #include <linux/spinlock.h>
8 #include <asm/current.h>
9 #include <uapi/linux/wait.h>
11 typedef struct __wait_queue wait_queue_t;
12 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
13 int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
17 #define WQ_FLAG_EXCLUSIVE 0x01
19 wait_queue_func_t func;
20 struct list_head task_list;
26 #define WAIT_ATOMIC_T_BIT_NR -1
29 struct wait_bit_queue {
30 struct wait_bit_key key;
34 struct __wait_queue_head {
36 struct list_head task_list;
38 typedef struct __wait_queue_head wait_queue_head_t;
43 * Macros for declaration and initialisaton of the datatypes
46 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
48 .func = default_wake_function, \
49 .task_list = { NULL, NULL } }
51 #define DECLARE_WAITQUEUE(name, tsk) \
52 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
54 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
55 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
56 .task_list = { &(name).task_list, &(name).task_list } }
58 #define DECLARE_WAIT_QUEUE_HEAD(name) \
59 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
61 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
62 { .flags = word, .bit_nr = bit, }
64 #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
65 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
67 extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
69 #define init_waitqueue_head(q) \
71 static struct lock_class_key __key; \
73 __init_waitqueue_head((q), #q, &__key); \
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)
82 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
85 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
89 q->func = default_wake_function;
92 static inline void init_waitqueue_func_entry(wait_queue_t *q,
93 wait_queue_func_t func)
100 static inline int waitqueue_active(wait_queue_head_t *q)
102 return !list_empty(&q->task_list);
105 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
106 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
107 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
109 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
111 list_add(&new->task_list, &head->task_list);
115 * Used for wake-one threads:
117 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
120 wait->flags |= WQ_FLAG_EXCLUSIVE;
121 __add_wait_queue(q, wait);
124 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
127 list_add_tail(&new->task_list, &head->task_list);
130 static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q,
133 wait->flags |= WQ_FLAG_EXCLUSIVE;
134 __add_wait_queue_tail(q, wait);
137 static inline void __remove_wait_queue(wait_queue_head_t *head,
140 list_del(&old->task_list);
143 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
144 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
145 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
147 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
148 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
149 void __wake_up_bit(wait_queue_head_t *, void *, int);
150 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
151 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
152 void wake_up_bit(void *, int);
153 void wake_up_atomic_t(atomic_t *);
154 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
155 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
156 int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
157 wait_queue_head_t *bit_waitqueue(void *, int);
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)
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)
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)
168 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
171 * Wakeup macros to be used to report events to the targets.
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))
182 #define ___wait_cond_timeout(condition, ret) \
184 bool __cond = (condition); \
185 if (__cond && !ret) \
190 #define ___wait_signal_pending(state) \
191 ((state == TASK_INTERRUPTIBLE && signal_pending(current)) || \
192 (state == TASK_KILLABLE && fatal_signal_pending(current)))
194 #define ___wait_nop_ret int ret __always_unused
196 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
199 DEFINE_WAIT(__wait); \
203 prepare_to_wait_exclusive(&wq, &__wait, state); \
205 prepare_to_wait(&wq, &__wait, state); \
210 if (___wait_signal_pending(state)) { \
211 ret = -ERESTARTSYS; \
213 abort_exclusive_wait(&wq, &__wait, \
222 finish_wait(&wq, &__wait); \
226 #define __wait_event(wq, condition) \
227 ___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
228 ___wait_nop_ret, schedule())
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
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.
239 * wake_up() has to be called after changing any variable that could
240 * change the result of the wait condition.
242 #define wait_event(wq, condition) \
246 __wait_event(wq, condition); \
249 #define __wait_event_timeout(wq, condition, ret) \
251 DEFINE_WAIT(__wait); \
254 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
255 if (___wait_cond_timeout(condition, ret)) \
257 ret = schedule_timeout(ret); \
259 finish_wait(&wq, &__wait); \
263 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
264 * @wq: the waitqueue to wait on
265 * @condition: a C expression for the event to wait for
266 * @timeout: timeout, in jiffies
268 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
269 * @condition evaluates to true. The @condition is checked each time
270 * the waitqueue @wq is woken up.
272 * wake_up() has to be called after changing any variable that could
273 * change the result of the wait condition.
275 * The function returns 0 if the @timeout elapsed, or the remaining
276 * jiffies (at least 1) if the @condition evaluated to %true before
277 * the @timeout elapsed.
279 #define wait_event_timeout(wq, condition, timeout) \
281 long __ret = timeout; \
283 __wait_event_timeout(wq, condition, __ret); \
287 #define __wait_event_interruptible(wq, condition, ret) \
289 DEFINE_WAIT(__wait); \
292 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
295 if (signal_pending(current)) { \
296 ret = -ERESTARTSYS; \
301 finish_wait(&wq, &__wait); \
305 * wait_event_interruptible - sleep until a condition gets true
306 * @wq: the waitqueue to wait on
307 * @condition: a C expression for the event to wait for
309 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
310 * @condition evaluates to true or a signal is received.
311 * The @condition is checked each time the waitqueue @wq is woken up.
313 * wake_up() has to be called after changing any variable that could
314 * change the result of the wait condition.
316 * The function will return -ERESTARTSYS if it was interrupted by a
317 * signal and 0 if @condition evaluated to true.
319 #define wait_event_interruptible(wq, condition) \
323 __wait_event_interruptible(wq, condition, __ret); \
327 #define __wait_event_interruptible_timeout(wq, condition, ret) \
329 DEFINE_WAIT(__wait); \
332 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
333 if (___wait_cond_timeout(condition, ret)) \
335 if (signal_pending(current)) { \
336 ret = -ERESTARTSYS; \
339 ret = schedule_timeout(ret); \
341 finish_wait(&wq, &__wait); \
345 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
346 * @wq: the waitqueue to wait on
347 * @condition: a C expression for the event to wait for
348 * @timeout: timeout, in jiffies
350 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
351 * @condition evaluates to true or a signal is received.
352 * The @condition is checked each time the waitqueue @wq is woken up.
354 * wake_up() has to be called after changing any variable that could
355 * change the result of the wait condition.
358 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
359 * a signal, or the remaining jiffies (at least 1) if the @condition
360 * evaluated to %true before the @timeout elapsed.
362 #define wait_event_interruptible_timeout(wq, condition, timeout) \
364 long __ret = timeout; \
366 __wait_event_interruptible_timeout(wq, condition, __ret); \
370 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
373 DEFINE_WAIT(__wait); \
374 struct hrtimer_sleeper __t; \
376 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
378 hrtimer_init_sleeper(&__t, current); \
379 if ((timeout).tv64 != KTIME_MAX) \
380 hrtimer_start_range_ns(&__t.timer, timeout, \
381 current->timer_slack_ns, \
385 prepare_to_wait(&wq, &__wait, state); \
388 if (state == TASK_INTERRUPTIBLE && \
389 signal_pending(current)) { \
390 __ret = -ERESTARTSYS; \
400 hrtimer_cancel(&__t.timer); \
401 destroy_hrtimer_on_stack(&__t.timer); \
402 finish_wait(&wq, &__wait); \
407 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
408 * @wq: the waitqueue to wait on
409 * @condition: a C expression for the event to wait for
410 * @timeout: timeout, as a ktime_t
412 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
413 * @condition evaluates to true or a signal is received.
414 * The @condition is checked each time the waitqueue @wq is woken up.
416 * wake_up() has to be called after changing any variable that could
417 * change the result of the wait condition.
419 * The function returns 0 if @condition became true, or -ETIME if the timeout
422 #define wait_event_hrtimeout(wq, condition, timeout) \
426 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
427 TASK_UNINTERRUPTIBLE); \
432 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
433 * @wq: the waitqueue to wait on
434 * @condition: a C expression for the event to wait for
435 * @timeout: timeout, as a ktime_t
437 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
438 * @condition evaluates to true or a signal is received.
439 * The @condition is checked each time the waitqueue @wq is woken up.
441 * wake_up() has to be called after changing any variable that could
442 * change the result of the wait condition.
444 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
445 * interrupted by a signal, or -ETIME if the timeout elapsed.
447 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
451 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
452 TASK_INTERRUPTIBLE); \
456 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
459 DEFINE_WAIT(__wait); \
462 prepare_to_wait_exclusive(&wq, &__wait, \
463 TASK_INTERRUPTIBLE); \
466 if (signal_pending(current)) { \
467 ret = -ERESTARTSYS; \
468 abort_exclusive_wait(&wq, &__wait, \
469 TASK_INTERRUPTIBLE, NULL); \
474 finish_wait(&wq, &__wait); \
478 #define wait_event_interruptible_exclusive(wq, condition) \
482 __wait_event_interruptible_exclusive(wq, condition, __ret);\
487 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
490 DEFINE_WAIT(__wait); \
492 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
494 if (likely(list_empty(&__wait.task_list))) \
495 __add_wait_queue_tail(&(wq), &__wait); \
496 set_current_state(TASK_INTERRUPTIBLE); \
497 if (signal_pending(current)) { \
498 __ret = -ERESTARTSYS; \
502 spin_unlock_irq(&(wq).lock); \
504 spin_unlock(&(wq).lock); \
507 spin_lock_irq(&(wq).lock); \
509 spin_lock(&(wq).lock); \
510 } while (!(condition)); \
511 __remove_wait_queue(&(wq), &__wait); \
512 __set_current_state(TASK_RUNNING); \
518 * wait_event_interruptible_locked - sleep until a condition gets true
519 * @wq: the waitqueue to wait on
520 * @condition: a C expression for the event to wait for
522 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
523 * @condition evaluates to true or a signal is received.
524 * The @condition is checked each time the waitqueue @wq is woken up.
526 * It must be called with wq.lock being held. This spinlock is
527 * unlocked while sleeping but @condition testing is done while lock
528 * is held and when this macro exits the lock is held.
530 * The lock is locked/unlocked using spin_lock()/spin_unlock()
531 * functions which must match the way they are locked/unlocked outside
534 * wake_up_locked() has to be called after changing any variable that could
535 * change the result of the wait condition.
537 * The function will return -ERESTARTSYS if it was interrupted by a
538 * signal and 0 if @condition evaluated to true.
540 #define wait_event_interruptible_locked(wq, condition) \
542 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
545 * wait_event_interruptible_locked_irq - sleep until a condition gets true
546 * @wq: the waitqueue to wait on
547 * @condition: a C expression for the event to wait for
549 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
550 * @condition evaluates to true or a signal is received.
551 * The @condition is checked each time the waitqueue @wq is woken up.
553 * It must be called with wq.lock being held. This spinlock is
554 * unlocked while sleeping but @condition testing is done while lock
555 * is held and when this macro exits the lock is held.
557 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
558 * functions which must match the way they are locked/unlocked outside
561 * wake_up_locked() has to be called after changing any variable that could
562 * change the result of the wait condition.
564 * The function will return -ERESTARTSYS if it was interrupted by a
565 * signal and 0 if @condition evaluated to true.
567 #define wait_event_interruptible_locked_irq(wq, condition) \
569 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
572 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
573 * @wq: the waitqueue to wait on
574 * @condition: a C expression for the event to wait for
576 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
577 * @condition evaluates to true or a signal is received.
578 * The @condition is checked each time the waitqueue @wq is woken up.
580 * It must be called with wq.lock being held. This spinlock is
581 * unlocked while sleeping but @condition testing is done while lock
582 * is held and when this macro exits the lock is held.
584 * The lock is locked/unlocked using spin_lock()/spin_unlock()
585 * functions which must match the way they are locked/unlocked outside
588 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
589 * set thus when other process waits process on the list if this
590 * process is awaken further processes are not considered.
592 * wake_up_locked() has to be called after changing any variable that could
593 * change the result of the wait condition.
595 * The function will return -ERESTARTSYS if it was interrupted by a
596 * signal and 0 if @condition evaluated to true.
598 #define wait_event_interruptible_exclusive_locked(wq, condition) \
600 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
603 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
604 * @wq: the waitqueue to wait on
605 * @condition: a C expression for the event to wait for
607 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
608 * @condition evaluates to true or a signal is received.
609 * The @condition is checked each time the waitqueue @wq is woken up.
611 * It must be called with wq.lock being held. This spinlock is
612 * unlocked while sleeping but @condition testing is done while lock
613 * is held and when this macro exits the lock is held.
615 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
616 * functions which must match the way they are locked/unlocked outside
619 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
620 * set thus when other process waits process on the list if this
621 * process is awaken further processes are not considered.
623 * wake_up_locked() has to be called after changing any variable that could
624 * change the result of the wait condition.
626 * The function will return -ERESTARTSYS if it was interrupted by a
627 * signal and 0 if @condition evaluated to true.
629 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
631 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
635 #define __wait_event_killable(wq, condition, ret) \
637 DEFINE_WAIT(__wait); \
640 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
643 if (!fatal_signal_pending(current)) { \
647 ret = -ERESTARTSYS; \
650 finish_wait(&wq, &__wait); \
654 * wait_event_killable - sleep until a condition gets true
655 * @wq: the waitqueue to wait on
656 * @condition: a C expression for the event to wait for
658 * The process is put to sleep (TASK_KILLABLE) until the
659 * @condition evaluates to true or a signal is received.
660 * The @condition is checked each time the waitqueue @wq is woken up.
662 * wake_up() has to be called after changing any variable that could
663 * change the result of the wait condition.
665 * The function will return -ERESTARTSYS if it was interrupted by a
666 * signal and 0 if @condition evaluated to true.
668 #define wait_event_killable(wq, condition) \
672 __wait_event_killable(wq, condition, __ret); \
677 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
679 DEFINE_WAIT(__wait); \
682 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
685 spin_unlock_irq(&lock); \
688 spin_lock_irq(&lock); \
690 finish_wait(&wq, &__wait); \
694 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
695 * condition is checked under the lock. This
696 * is expected to be called with the lock
698 * @wq: the waitqueue to wait on
699 * @condition: a C expression for the event to wait for
700 * @lock: a locked spinlock_t, which will be released before cmd
701 * and schedule() and reacquired afterwards.
702 * @cmd: a command which is invoked outside the critical section before
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.
709 * wake_up() has to be called after changing any variable that could
710 * change the result of the wait condition.
712 * This is supposed to be called while holding the lock. The lock is
713 * dropped before invoking the cmd and going to sleep and is reacquired
716 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
720 __wait_event_lock_irq(wq, condition, lock, cmd); \
724 * wait_event_lock_irq - sleep until a condition gets true. The
725 * condition is checked under the lock. This
726 * is expected to be called with the lock
728 * @wq: the waitqueue to wait on
729 * @condition: a C expression for the event to wait for
730 * @lock: a locked spinlock_t, which will be released before schedule()
731 * and reacquired afterwards.
733 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
734 * @condition evaluates to true. The @condition is checked each time
735 * the waitqueue @wq is woken up.
737 * wake_up() has to be called after changing any variable that could
738 * change the result of the wait condition.
740 * This is supposed to be called while holding the lock. The lock is
741 * dropped before going to sleep and is reacquired afterwards.
743 #define wait_event_lock_irq(wq, condition, lock) \
747 __wait_event_lock_irq(wq, condition, lock, ); \
751 #define __wait_event_interruptible_lock_irq(wq, condition, \
754 DEFINE_WAIT(__wait); \
757 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
760 if (signal_pending(current)) { \
761 ret = -ERESTARTSYS; \
764 spin_unlock_irq(&lock); \
767 spin_lock_irq(&lock); \
769 finish_wait(&wq, &__wait); \
773 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
774 * The condition is checked under the lock. This is expected to
775 * be called with the lock taken.
776 * @wq: the waitqueue to wait on
777 * @condition: a C expression for the event to wait for
778 * @lock: a locked spinlock_t, which will be released before cmd and
779 * schedule() and reacquired afterwards.
780 * @cmd: a command which is invoked outside the critical section before
783 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
784 * @condition evaluates to true or a signal is received. The @condition is
785 * checked each time the waitqueue @wq is woken up.
787 * wake_up() has to be called after changing any variable that could
788 * change the result of the wait condition.
790 * This is supposed to be called while holding the lock. The lock is
791 * dropped before invoking the cmd and going to sleep and is reacquired
794 * The macro will return -ERESTARTSYS if it was interrupted by a signal
795 * and 0 if @condition evaluated to true.
797 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
802 __wait_event_interruptible_lock_irq(wq, condition, \
808 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
809 * The condition is checked under the lock. This is expected
810 * to be called with the lock taken.
811 * @wq: the waitqueue to wait on
812 * @condition: a C expression for the event to wait for
813 * @lock: a locked spinlock_t, which will be released before schedule()
814 * and reacquired afterwards.
816 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
817 * @condition evaluates to true or signal is received. The @condition is
818 * checked each time the waitqueue @wq is woken up.
820 * wake_up() has to be called after changing any variable that could
821 * change the result of the wait condition.
823 * This is supposed to be called while holding the lock. The lock is
824 * dropped before going to sleep and is reacquired afterwards.
826 * The macro will return -ERESTARTSYS if it was interrupted by a signal
827 * and 0 if @condition evaluated to true.
829 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
834 __wait_event_interruptible_lock_irq(wq, condition, \
839 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
842 DEFINE_WAIT(__wait); \
845 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
846 if (___wait_cond_timeout(condition, ret)) \
848 if (signal_pending(current)) { \
849 ret = -ERESTARTSYS; \
852 spin_unlock_irq(&lock); \
853 ret = schedule_timeout(ret); \
854 spin_lock_irq(&lock); \
856 finish_wait(&wq, &__wait); \
860 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
861 * The condition is checked under the lock. This is expected
862 * to be called with the lock taken.
863 * @wq: the waitqueue to wait on
864 * @condition: a C expression for the event to wait for
865 * @lock: a locked spinlock_t, which will be released before schedule()
866 * and reacquired afterwards.
867 * @timeout: timeout, in jiffies
869 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
870 * @condition evaluates to true or signal is received. The @condition is
871 * checked each time the waitqueue @wq is woken up.
873 * wake_up() has to be called after changing any variable that could
874 * change the result of the wait condition.
876 * This is supposed to be called while holding the lock. The lock is
877 * dropped before going to sleep and is reacquired afterwards.
879 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
880 * was interrupted by a signal, and the remaining jiffies otherwise
881 * if the condition evaluated to true before the timeout elapsed.
883 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
886 int __ret = timeout; \
889 __wait_event_interruptible_lock_irq_timeout( \
890 wq, condition, lock, __ret); \
896 * These are the old interfaces to sleep waiting for an event.
897 * They are racy. DO NOT use them, use the wait_event* interfaces above.
898 * We plan to remove these interfaces.
900 extern void sleep_on(wait_queue_head_t *q);
901 extern long sleep_on_timeout(wait_queue_head_t *q,
902 signed long timeout);
903 extern void interruptible_sleep_on(wait_queue_head_t *q);
904 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
905 signed long timeout);
908 * Waitqueues which are removed from the waitqueue_head at wakeup time
910 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
911 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
912 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
913 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
914 unsigned int mode, void *key);
915 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
916 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
918 #define DEFINE_WAIT_FUNC(name, function) \
919 wait_queue_t name = { \
920 .private = current, \
922 .task_list = LIST_HEAD_INIT((name).task_list), \
925 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
927 #define DEFINE_WAIT_BIT(name, word, bit) \
928 struct wait_bit_queue name = { \
929 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
931 .private = current, \
932 .func = wake_bit_function, \
934 LIST_HEAD_INIT((name).wait.task_list), \
938 #define init_wait(wait) \
940 (wait)->private = current; \
941 (wait)->func = autoremove_wake_function; \
942 INIT_LIST_HEAD(&(wait)->task_list); \
947 * wait_on_bit - wait for a bit to be cleared
948 * @word: the word being waited on, a kernel virtual address
949 * @bit: the bit of the word being waited on
950 * @action: the function used to sleep, which may take special actions
951 * @mode: the task state to sleep in
953 * There is a standard hashed waitqueue table for generic use. This
954 * is the part of the hashtable's accessor API that waits on a bit.
955 * For instance, if one were to have waiters on a bitflag, one would
956 * call wait_on_bit() in threads waiting for the bit to clear.
957 * One uses wait_on_bit() where one is waiting for the bit to clear,
958 * but has no intention of setting it.
960 static inline int wait_on_bit(void *word, int bit,
961 int (*action)(void *), unsigned mode)
963 if (!test_bit(bit, word))
965 return out_of_line_wait_on_bit(word, bit, action, mode);
969 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
970 * @word: the word being waited on, a kernel virtual address
971 * @bit: the bit of the word being waited on
972 * @action: the function used to sleep, which may take special actions
973 * @mode: the task state to sleep in
975 * There is a standard hashed waitqueue table for generic use. This
976 * is the part of the hashtable's accessor API that waits on a bit
977 * when one intends to set it, for instance, trying to lock bitflags.
978 * For instance, if one were to have waiters trying to set bitflag
979 * and waiting for it to clear before setting it, one would call
980 * wait_on_bit() in threads waiting to be able to set the bit.
981 * One uses wait_on_bit_lock() where one is waiting for the bit to
982 * clear with the intention of setting it, and when done, clearing it.
984 static inline int wait_on_bit_lock(void *word, int bit,
985 int (*action)(void *), unsigned mode)
987 if (!test_and_set_bit(bit, word))
989 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
993 * wait_on_atomic_t - Wait for an atomic_t to become 0
994 * @val: The atomic value being waited on, a kernel virtual address
995 * @action: the function used to sleep, which may take special actions
996 * @mode: the task state to sleep in
998 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
999 * the purpose of getting a waitqueue, but we set the key to a bit number
1000 * outside of the target 'word'.
1003 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1005 if (atomic_read(val) == 0)
1007 return out_of_line_wait_on_atomic_t(val, action, mode);