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) \
228 DEFINE_WAIT(__wait); \
231 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
236 finish_wait(&wq, &__wait); \
240 * wait_event - sleep until a condition gets true
241 * @wq: the waitqueue to wait on
242 * @condition: a C expression for the event to wait for
244 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
245 * @condition evaluates to true. The @condition is checked each time
246 * the waitqueue @wq is woken up.
248 * wake_up() has to be called after changing any variable that could
249 * change the result of the wait condition.
251 #define wait_event(wq, condition) \
255 __wait_event(wq, condition); \
258 #define __wait_event_timeout(wq, condition, ret) \
260 DEFINE_WAIT(__wait); \
263 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
264 if (___wait_cond_timeout(condition, ret)) \
266 ret = schedule_timeout(ret); \
268 finish_wait(&wq, &__wait); \
272 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
273 * @wq: the waitqueue to wait on
274 * @condition: a C expression for the event to wait for
275 * @timeout: timeout, in jiffies
277 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
278 * @condition evaluates to true. The @condition is checked each time
279 * the waitqueue @wq is woken up.
281 * wake_up() has to be called after changing any variable that could
282 * change the result of the wait condition.
284 * The function returns 0 if the @timeout elapsed, or the remaining
285 * jiffies (at least 1) if the @condition evaluated to %true before
286 * the @timeout elapsed.
288 #define wait_event_timeout(wq, condition, timeout) \
290 long __ret = timeout; \
292 __wait_event_timeout(wq, condition, __ret); \
296 #define __wait_event_interruptible(wq, condition, ret) \
298 DEFINE_WAIT(__wait); \
301 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
304 if (signal_pending(current)) { \
305 ret = -ERESTARTSYS; \
310 finish_wait(&wq, &__wait); \
314 * wait_event_interruptible - sleep until a condition gets true
315 * @wq: the waitqueue to wait on
316 * @condition: a C expression for the event to wait for
318 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
319 * @condition evaluates to true or a signal is received.
320 * The @condition is checked each time the waitqueue @wq is woken up.
322 * wake_up() has to be called after changing any variable that could
323 * change the result of the wait condition.
325 * The function will return -ERESTARTSYS if it was interrupted by a
326 * signal and 0 if @condition evaluated to true.
328 #define wait_event_interruptible(wq, condition) \
332 __wait_event_interruptible(wq, condition, __ret); \
336 #define __wait_event_interruptible_timeout(wq, condition, ret) \
338 DEFINE_WAIT(__wait); \
341 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
342 if (___wait_cond_timeout(condition, ret)) \
344 if (signal_pending(current)) { \
345 ret = -ERESTARTSYS; \
348 ret = schedule_timeout(ret); \
350 finish_wait(&wq, &__wait); \
354 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
355 * @wq: the waitqueue to wait on
356 * @condition: a C expression for the event to wait for
357 * @timeout: timeout, in jiffies
359 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
360 * @condition evaluates to true or a signal is received.
361 * The @condition is checked each time the waitqueue @wq is woken up.
363 * wake_up() has to be called after changing any variable that could
364 * change the result of the wait condition.
367 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
368 * a signal, or the remaining jiffies (at least 1) if the @condition
369 * evaluated to %true before the @timeout elapsed.
371 #define wait_event_interruptible_timeout(wq, condition, timeout) \
373 long __ret = timeout; \
375 __wait_event_interruptible_timeout(wq, condition, __ret); \
379 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
382 DEFINE_WAIT(__wait); \
383 struct hrtimer_sleeper __t; \
385 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
387 hrtimer_init_sleeper(&__t, current); \
388 if ((timeout).tv64 != KTIME_MAX) \
389 hrtimer_start_range_ns(&__t.timer, timeout, \
390 current->timer_slack_ns, \
394 prepare_to_wait(&wq, &__wait, state); \
397 if (state == TASK_INTERRUPTIBLE && \
398 signal_pending(current)) { \
399 __ret = -ERESTARTSYS; \
409 hrtimer_cancel(&__t.timer); \
410 destroy_hrtimer_on_stack(&__t.timer); \
411 finish_wait(&wq, &__wait); \
416 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
417 * @wq: the waitqueue to wait on
418 * @condition: a C expression for the event to wait for
419 * @timeout: timeout, as a ktime_t
421 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
422 * @condition evaluates to true or a signal is received.
423 * The @condition is checked each time the waitqueue @wq is woken up.
425 * wake_up() has to be called after changing any variable that could
426 * change the result of the wait condition.
428 * The function returns 0 if @condition became true, or -ETIME if the timeout
431 #define wait_event_hrtimeout(wq, condition, timeout) \
435 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
436 TASK_UNINTERRUPTIBLE); \
441 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
442 * @wq: the waitqueue to wait on
443 * @condition: a C expression for the event to wait for
444 * @timeout: timeout, as a ktime_t
446 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
447 * @condition evaluates to true or a signal is received.
448 * The @condition is checked each time the waitqueue @wq is woken up.
450 * wake_up() has to be called after changing any variable that could
451 * change the result of the wait condition.
453 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
454 * interrupted by a signal, or -ETIME if the timeout elapsed.
456 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
460 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
461 TASK_INTERRUPTIBLE); \
465 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
468 DEFINE_WAIT(__wait); \
471 prepare_to_wait_exclusive(&wq, &__wait, \
472 TASK_INTERRUPTIBLE); \
475 if (signal_pending(current)) { \
476 ret = -ERESTARTSYS; \
477 abort_exclusive_wait(&wq, &__wait, \
478 TASK_INTERRUPTIBLE, NULL); \
483 finish_wait(&wq, &__wait); \
487 #define wait_event_interruptible_exclusive(wq, condition) \
491 __wait_event_interruptible_exclusive(wq, condition, __ret);\
496 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
499 DEFINE_WAIT(__wait); \
501 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
503 if (likely(list_empty(&__wait.task_list))) \
504 __add_wait_queue_tail(&(wq), &__wait); \
505 set_current_state(TASK_INTERRUPTIBLE); \
506 if (signal_pending(current)) { \
507 __ret = -ERESTARTSYS; \
511 spin_unlock_irq(&(wq).lock); \
513 spin_unlock(&(wq).lock); \
516 spin_lock_irq(&(wq).lock); \
518 spin_lock(&(wq).lock); \
519 } while (!(condition)); \
520 __remove_wait_queue(&(wq), &__wait); \
521 __set_current_state(TASK_RUNNING); \
527 * wait_event_interruptible_locked - sleep until a condition gets true
528 * @wq: the waitqueue to wait on
529 * @condition: a C expression for the event to wait for
531 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
532 * @condition evaluates to true or a signal is received.
533 * The @condition is checked each time the waitqueue @wq is woken up.
535 * It must be called with wq.lock being held. This spinlock is
536 * unlocked while sleeping but @condition testing is done while lock
537 * is held and when this macro exits the lock is held.
539 * The lock is locked/unlocked using spin_lock()/spin_unlock()
540 * functions which must match the way they are locked/unlocked outside
543 * wake_up_locked() has to be called after changing any variable that could
544 * change the result of the wait condition.
546 * The function will return -ERESTARTSYS if it was interrupted by a
547 * signal and 0 if @condition evaluated to true.
549 #define wait_event_interruptible_locked(wq, condition) \
551 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
554 * wait_event_interruptible_locked_irq - sleep until a condition gets true
555 * @wq: the waitqueue to wait on
556 * @condition: a C expression for the event to wait for
558 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
559 * @condition evaluates to true or a signal is received.
560 * The @condition is checked each time the waitqueue @wq is woken up.
562 * It must be called with wq.lock being held. This spinlock is
563 * unlocked while sleeping but @condition testing is done while lock
564 * is held and when this macro exits the lock is held.
566 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
567 * functions which must match the way they are locked/unlocked outside
570 * wake_up_locked() has to be called after changing any variable that could
571 * change the result of the wait condition.
573 * The function will return -ERESTARTSYS if it was interrupted by a
574 * signal and 0 if @condition evaluated to true.
576 #define wait_event_interruptible_locked_irq(wq, condition) \
578 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
581 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
582 * @wq: the waitqueue to wait on
583 * @condition: a C expression for the event to wait for
585 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
586 * @condition evaluates to true or a signal is received.
587 * The @condition is checked each time the waitqueue @wq is woken up.
589 * It must be called with wq.lock being held. This spinlock is
590 * unlocked while sleeping but @condition testing is done while lock
591 * is held and when this macro exits the lock is held.
593 * The lock is locked/unlocked using spin_lock()/spin_unlock()
594 * functions which must match the way they are locked/unlocked outside
597 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
598 * set thus when other process waits process on the list if this
599 * process is awaken further processes are not considered.
601 * wake_up_locked() has to be called after changing any variable that could
602 * change the result of the wait condition.
604 * The function will return -ERESTARTSYS if it was interrupted by a
605 * signal and 0 if @condition evaluated to true.
607 #define wait_event_interruptible_exclusive_locked(wq, condition) \
609 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
612 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
613 * @wq: the waitqueue to wait on
614 * @condition: a C expression for the event to wait for
616 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
617 * @condition evaluates to true or a signal is received.
618 * The @condition is checked each time the waitqueue @wq is woken up.
620 * It must be called with wq.lock being held. This spinlock is
621 * unlocked while sleeping but @condition testing is done while lock
622 * is held and when this macro exits the lock is held.
624 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
625 * functions which must match the way they are locked/unlocked outside
628 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
629 * set thus when other process waits process on the list if this
630 * process is awaken further processes are not considered.
632 * wake_up_locked() has to be called after changing any variable that could
633 * change the result of the wait condition.
635 * The function will return -ERESTARTSYS if it was interrupted by a
636 * signal and 0 if @condition evaluated to true.
638 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
640 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
644 #define __wait_event_killable(wq, condition, ret) \
646 DEFINE_WAIT(__wait); \
649 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
652 if (!fatal_signal_pending(current)) { \
656 ret = -ERESTARTSYS; \
659 finish_wait(&wq, &__wait); \
663 * wait_event_killable - sleep until a condition gets true
664 * @wq: the waitqueue to wait on
665 * @condition: a C expression for the event to wait for
667 * The process is put to sleep (TASK_KILLABLE) until the
668 * @condition evaluates to true or a signal is received.
669 * The @condition is checked each time the waitqueue @wq is woken up.
671 * wake_up() has to be called after changing any variable that could
672 * change the result of the wait condition.
674 * The function will return -ERESTARTSYS if it was interrupted by a
675 * signal and 0 if @condition evaluated to true.
677 #define wait_event_killable(wq, condition) \
681 __wait_event_killable(wq, condition, __ret); \
686 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
688 DEFINE_WAIT(__wait); \
691 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
694 spin_unlock_irq(&lock); \
697 spin_lock_irq(&lock); \
699 finish_wait(&wq, &__wait); \
703 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
704 * condition is checked under the lock. This
705 * is expected to be called with the lock
707 * @wq: the waitqueue to wait on
708 * @condition: a C expression for the event to wait for
709 * @lock: a locked spinlock_t, which will be released before cmd
710 * and schedule() and reacquired afterwards.
711 * @cmd: a command which is invoked outside the critical section before
714 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
715 * @condition evaluates to true. The @condition is checked each time
716 * the waitqueue @wq is woken up.
718 * wake_up() has to be called after changing any variable that could
719 * change the result of the wait condition.
721 * This is supposed to be called while holding the lock. The lock is
722 * dropped before invoking the cmd and going to sleep and is reacquired
725 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
729 __wait_event_lock_irq(wq, condition, lock, cmd); \
733 * wait_event_lock_irq - sleep until a condition gets true. The
734 * condition is checked under the lock. This
735 * is expected to be called with the lock
737 * @wq: the waitqueue to wait on
738 * @condition: a C expression for the event to wait for
739 * @lock: a locked spinlock_t, which will be released before schedule()
740 * and reacquired afterwards.
742 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
743 * @condition evaluates to true. The @condition is checked each time
744 * the waitqueue @wq is woken up.
746 * wake_up() has to be called after changing any variable that could
747 * change the result of the wait condition.
749 * This is supposed to be called while holding the lock. The lock is
750 * dropped before going to sleep and is reacquired afterwards.
752 #define wait_event_lock_irq(wq, condition, lock) \
756 __wait_event_lock_irq(wq, condition, lock, ); \
760 #define __wait_event_interruptible_lock_irq(wq, condition, \
763 DEFINE_WAIT(__wait); \
766 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
769 if (signal_pending(current)) { \
770 ret = -ERESTARTSYS; \
773 spin_unlock_irq(&lock); \
776 spin_lock_irq(&lock); \
778 finish_wait(&wq, &__wait); \
782 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
783 * The condition is checked under the lock. This is expected to
784 * be called with the lock taken.
785 * @wq: the waitqueue to wait on
786 * @condition: a C expression for the event to wait for
787 * @lock: a locked spinlock_t, which will be released before cmd and
788 * schedule() and reacquired afterwards.
789 * @cmd: a command which is invoked outside the critical section before
792 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
793 * @condition evaluates to true or a signal is received. The @condition is
794 * checked each time the waitqueue @wq is woken up.
796 * wake_up() has to be called after changing any variable that could
797 * change the result of the wait condition.
799 * This is supposed to be called while holding the lock. The lock is
800 * dropped before invoking the cmd and going to sleep and is reacquired
803 * The macro will return -ERESTARTSYS if it was interrupted by a signal
804 * and 0 if @condition evaluated to true.
806 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
811 __wait_event_interruptible_lock_irq(wq, condition, \
817 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
818 * The condition is checked under the lock. This is expected
819 * to be called with the lock taken.
820 * @wq: the waitqueue to wait on
821 * @condition: a C expression for the event to wait for
822 * @lock: a locked spinlock_t, which will be released before schedule()
823 * and reacquired afterwards.
825 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
826 * @condition evaluates to true or signal is received. The @condition is
827 * checked each time the waitqueue @wq is woken up.
829 * wake_up() has to be called after changing any variable that could
830 * change the result of the wait condition.
832 * This is supposed to be called while holding the lock. The lock is
833 * dropped before going to sleep and is reacquired afterwards.
835 * The macro will return -ERESTARTSYS if it was interrupted by a signal
836 * and 0 if @condition evaluated to true.
838 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
843 __wait_event_interruptible_lock_irq(wq, condition, \
848 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
851 DEFINE_WAIT(__wait); \
854 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
855 if (___wait_cond_timeout(condition, ret)) \
857 if (signal_pending(current)) { \
858 ret = -ERESTARTSYS; \
861 spin_unlock_irq(&lock); \
862 ret = schedule_timeout(ret); \
863 spin_lock_irq(&lock); \
865 finish_wait(&wq, &__wait); \
869 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
870 * The condition is checked under the lock. This is expected
871 * to be called with the lock taken.
872 * @wq: the waitqueue to wait on
873 * @condition: a C expression for the event to wait for
874 * @lock: a locked spinlock_t, which will be released before schedule()
875 * and reacquired afterwards.
876 * @timeout: timeout, in jiffies
878 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
879 * @condition evaluates to true or signal is received. The @condition is
880 * checked each time the waitqueue @wq is woken up.
882 * wake_up() has to be called after changing any variable that could
883 * change the result of the wait condition.
885 * This is supposed to be called while holding the lock. The lock is
886 * dropped before going to sleep and is reacquired afterwards.
888 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
889 * was interrupted by a signal, and the remaining jiffies otherwise
890 * if the condition evaluated to true before the timeout elapsed.
892 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
895 int __ret = timeout; \
898 __wait_event_interruptible_lock_irq_timeout( \
899 wq, condition, lock, __ret); \
905 * These are the old interfaces to sleep waiting for an event.
906 * They are racy. DO NOT use them, use the wait_event* interfaces above.
907 * We plan to remove these interfaces.
909 extern void sleep_on(wait_queue_head_t *q);
910 extern long sleep_on_timeout(wait_queue_head_t *q,
911 signed long timeout);
912 extern void interruptible_sleep_on(wait_queue_head_t *q);
913 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
914 signed long timeout);
917 * Waitqueues which are removed from the waitqueue_head at wakeup time
919 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
920 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
921 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
922 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
923 unsigned int mode, void *key);
924 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
925 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
927 #define DEFINE_WAIT_FUNC(name, function) \
928 wait_queue_t name = { \
929 .private = current, \
931 .task_list = LIST_HEAD_INIT((name).task_list), \
934 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
936 #define DEFINE_WAIT_BIT(name, word, bit) \
937 struct wait_bit_queue name = { \
938 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
940 .private = current, \
941 .func = wake_bit_function, \
943 LIST_HEAD_INIT((name).wait.task_list), \
947 #define init_wait(wait) \
949 (wait)->private = current; \
950 (wait)->func = autoremove_wake_function; \
951 INIT_LIST_HEAD(&(wait)->task_list); \
956 * wait_on_bit - wait for a bit to be cleared
957 * @word: the word being waited on, a kernel virtual address
958 * @bit: the bit of the word being waited on
959 * @action: the function used to sleep, which may take special actions
960 * @mode: the task state to sleep in
962 * There is a standard hashed waitqueue table for generic use. This
963 * is the part of the hashtable's accessor API that waits on a bit.
964 * For instance, if one were to have waiters on a bitflag, one would
965 * call wait_on_bit() in threads waiting for the bit to clear.
966 * One uses wait_on_bit() where one is waiting for the bit to clear,
967 * but has no intention of setting it.
969 static inline int wait_on_bit(void *word, int bit,
970 int (*action)(void *), unsigned mode)
972 if (!test_bit(bit, word))
974 return out_of_line_wait_on_bit(word, bit, action, mode);
978 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
979 * @word: the word being waited on, a kernel virtual address
980 * @bit: the bit of the word being waited on
981 * @action: the function used to sleep, which may take special actions
982 * @mode: the task state to sleep in
984 * There is a standard hashed waitqueue table for generic use. This
985 * is the part of the hashtable's accessor API that waits on a bit
986 * when one intends to set it, for instance, trying to lock bitflags.
987 * For instance, if one were to have waiters trying to set bitflag
988 * and waiting for it to clear before setting it, one would call
989 * wait_on_bit() in threads waiting to be able to set the bit.
990 * One uses wait_on_bit_lock() where one is waiting for the bit to
991 * clear with the intention of setting it, and when done, clearing it.
993 static inline int wait_on_bit_lock(void *word, int bit,
994 int (*action)(void *), unsigned mode)
996 if (!test_and_set_bit(bit, word))
998 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1002 * wait_on_atomic_t - Wait for an atomic_t to become 0
1003 * @val: The atomic value being waited on, a kernel virtual address
1004 * @action: the function used to sleep, which may take special actions
1005 * @mode: the task state to sleep in
1007 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1008 * the purpose of getting a waitqueue, but we set the key to a bit number
1009 * outside of the target 'word'.
1012 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1014 if (atomic_read(val) == 0)
1016 return out_of_line_wait_on_atomic_t(val, action, mode);