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_event(wq, condition) \
184 DEFINE_WAIT(__wait); \
187 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
192 finish_wait(&wq, &__wait); \
196 * wait_event - sleep until a condition gets true
197 * @wq: the waitqueue to wait on
198 * @condition: a C expression for the event to wait for
200 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
201 * @condition evaluates to true. The @condition is checked each time
202 * the waitqueue @wq is woken up.
204 * wake_up() has to be called after changing any variable that could
205 * change the result of the wait condition.
207 #define wait_event(wq, condition) \
211 __wait_event(wq, condition); \
214 #define __wait_event_timeout(wq, condition, ret) \
216 DEFINE_WAIT(__wait); \
219 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
222 ret = schedule_timeout(ret); \
226 if (!ret && (condition)) \
228 finish_wait(&wq, &__wait); \
232 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
233 * @wq: the waitqueue to wait on
234 * @condition: a C expression for the event to wait for
235 * @timeout: timeout, in jiffies
237 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
238 * @condition evaluates to true. The @condition is checked each time
239 * the waitqueue @wq is woken up.
241 * wake_up() has to be called after changing any variable that could
242 * change the result of the wait condition.
244 * The function returns 0 if the @timeout elapsed, or the remaining
245 * jiffies (at least 1) if the @condition evaluated to %true before
246 * the @timeout elapsed.
248 #define wait_event_timeout(wq, condition, timeout) \
250 long __ret = timeout; \
252 __wait_event_timeout(wq, condition, __ret); \
256 #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
258 DEFINE_WAIT(__wait); \
261 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
268 finish_wait(&wq, &__wait); \
272 * wait_event_cmd - sleep until a condition gets true
273 * @wq: the waitqueue to wait on
274 * @condition: a C expression for the event to wait for
275 * cmd1: the command will be executed before sleep
276 * cmd2: the command will be executed after sleep
278 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
279 * @condition evaluates to true. The @condition is checked each time
280 * the waitqueue @wq is woken up.
282 * wake_up() has to be called after changing any variable that could
283 * change the result of the wait condition.
285 #define wait_event_cmd(wq, condition, cmd1, cmd2) \
289 __wait_event_cmd(wq, condition, cmd1, cmd2); \
292 #define __wait_event_interruptible(wq, condition, ret) \
294 DEFINE_WAIT(__wait); \
297 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
300 if (!signal_pending(current)) { \
304 ret = -ERESTARTSYS; \
307 finish_wait(&wq, &__wait); \
311 * wait_event_interruptible - sleep until a condition gets true
312 * @wq: the waitqueue to wait on
313 * @condition: a C expression for the event to wait for
315 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
316 * @condition evaluates to true or a signal is received.
317 * The @condition is checked each time the waitqueue @wq is woken up.
319 * wake_up() has to be called after changing any variable that could
320 * change the result of the wait condition.
322 * The function will return -ERESTARTSYS if it was interrupted by a
323 * signal and 0 if @condition evaluated to true.
325 #define wait_event_interruptible(wq, condition) \
329 __wait_event_interruptible(wq, condition, __ret); \
333 #define __wait_event_interruptible_timeout(wq, condition, ret) \
335 DEFINE_WAIT(__wait); \
338 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
341 if (!signal_pending(current)) { \
342 ret = schedule_timeout(ret); \
347 ret = -ERESTARTSYS; \
350 if (!ret && (condition)) \
352 finish_wait(&wq, &__wait); \
356 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
357 * @wq: the waitqueue to wait on
358 * @condition: a C expression for the event to wait for
359 * @timeout: timeout, in jiffies
361 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
362 * @condition evaluates to true or a signal is received.
363 * The @condition is checked each time the waitqueue @wq is woken up.
365 * wake_up() has to be called after changing any variable that could
366 * change the result of the wait condition.
369 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
370 * a signal, or the remaining jiffies (at least 1) if the @condition
371 * evaluated to %true before the @timeout elapsed.
373 #define wait_event_interruptible_timeout(wq, condition, timeout) \
375 long __ret = timeout; \
377 __wait_event_interruptible_timeout(wq, condition, __ret); \
381 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
384 DEFINE_WAIT(__wait); \
385 struct hrtimer_sleeper __t; \
387 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
389 hrtimer_init_sleeper(&__t, current); \
390 if ((timeout).tv64 != KTIME_MAX) \
391 hrtimer_start_range_ns(&__t.timer, timeout, \
392 current->timer_slack_ns, \
396 prepare_to_wait(&wq, &__wait, state); \
399 if (state == TASK_INTERRUPTIBLE && \
400 signal_pending(current)) { \
401 __ret = -ERESTARTSYS; \
411 hrtimer_cancel(&__t.timer); \
412 destroy_hrtimer_on_stack(&__t.timer); \
413 finish_wait(&wq, &__wait); \
418 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
419 * @wq: the waitqueue to wait on
420 * @condition: a C expression for the event to wait for
421 * @timeout: timeout, as a ktime_t
423 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
424 * @condition evaluates to true or a signal is received.
425 * The @condition is checked each time the waitqueue @wq is woken up.
427 * wake_up() has to be called after changing any variable that could
428 * change the result of the wait condition.
430 * The function returns 0 if @condition became true, or -ETIME if the timeout
433 #define wait_event_hrtimeout(wq, condition, timeout) \
437 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
438 TASK_UNINTERRUPTIBLE); \
443 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
444 * @wq: the waitqueue to wait on
445 * @condition: a C expression for the event to wait for
446 * @timeout: timeout, as a ktime_t
448 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
449 * @condition evaluates to true or a signal is received.
450 * The @condition is checked each time the waitqueue @wq is woken up.
452 * wake_up() has to be called after changing any variable that could
453 * change the result of the wait condition.
455 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
456 * interrupted by a signal, or -ETIME if the timeout elapsed.
458 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
462 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
463 TASK_INTERRUPTIBLE); \
467 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
469 DEFINE_WAIT(__wait); \
472 prepare_to_wait_exclusive(&wq, &__wait, \
473 TASK_INTERRUPTIBLE); \
475 finish_wait(&wq, &__wait); \
478 if (!signal_pending(current)) { \
482 ret = -ERESTARTSYS; \
483 abort_exclusive_wait(&wq, &__wait, \
484 TASK_INTERRUPTIBLE, NULL); \
489 #define wait_event_interruptible_exclusive(wq, condition) \
493 __wait_event_interruptible_exclusive(wq, condition, __ret);\
498 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
501 DEFINE_WAIT(__wait); \
503 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
505 if (likely(list_empty(&__wait.task_list))) \
506 __add_wait_queue_tail(&(wq), &__wait); \
507 set_current_state(TASK_INTERRUPTIBLE); \
508 if (signal_pending(current)) { \
509 __ret = -ERESTARTSYS; \
513 spin_unlock_irq(&(wq).lock); \
515 spin_unlock(&(wq).lock); \
518 spin_lock_irq(&(wq).lock); \
520 spin_lock(&(wq).lock); \
521 } while (!(condition)); \
522 __remove_wait_queue(&(wq), &__wait); \
523 __set_current_state(TASK_RUNNING); \
529 * wait_event_interruptible_locked - sleep until a condition gets true
530 * @wq: the waitqueue to wait on
531 * @condition: a C expression for the event to wait for
533 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
534 * @condition evaluates to true or a signal is received.
535 * The @condition is checked each time the waitqueue @wq is woken up.
537 * It must be called with wq.lock being held. This spinlock is
538 * unlocked while sleeping but @condition testing is done while lock
539 * is held and when this macro exits the lock is held.
541 * The lock is locked/unlocked using spin_lock()/spin_unlock()
542 * functions which must match the way they are locked/unlocked outside
545 * wake_up_locked() has to be called after changing any variable that could
546 * change the result of the wait condition.
548 * The function will return -ERESTARTSYS if it was interrupted by a
549 * signal and 0 if @condition evaluated to true.
551 #define wait_event_interruptible_locked(wq, condition) \
553 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
556 * wait_event_interruptible_locked_irq - sleep until a condition gets true
557 * @wq: the waitqueue to wait on
558 * @condition: a C expression for the event to wait for
560 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
561 * @condition evaluates to true or a signal is received.
562 * The @condition is checked each time the waitqueue @wq is woken up.
564 * It must be called with wq.lock being held. This spinlock is
565 * unlocked while sleeping but @condition testing is done while lock
566 * is held and when this macro exits the lock is held.
568 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
569 * functions which must match the way they are locked/unlocked outside
572 * wake_up_locked() has to be called after changing any variable that could
573 * change the result of the wait condition.
575 * The function will return -ERESTARTSYS if it was interrupted by a
576 * signal and 0 if @condition evaluated to true.
578 #define wait_event_interruptible_locked_irq(wq, condition) \
580 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
583 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
584 * @wq: the waitqueue to wait on
585 * @condition: a C expression for the event to wait for
587 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
588 * @condition evaluates to true or a signal is received.
589 * The @condition is checked each time the waitqueue @wq is woken up.
591 * It must be called with wq.lock being held. This spinlock is
592 * unlocked while sleeping but @condition testing is done while lock
593 * is held and when this macro exits the lock is held.
595 * The lock is locked/unlocked using spin_lock()/spin_unlock()
596 * functions which must match the way they are locked/unlocked outside
599 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
600 * set thus when other process waits process on the list if this
601 * process is awaken further processes are not considered.
603 * wake_up_locked() has to be called after changing any variable that could
604 * change the result of the wait condition.
606 * The function will return -ERESTARTSYS if it was interrupted by a
607 * signal and 0 if @condition evaluated to true.
609 #define wait_event_interruptible_exclusive_locked(wq, condition) \
611 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
614 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
615 * @wq: the waitqueue to wait on
616 * @condition: a C expression for the event to wait for
618 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
619 * @condition evaluates to true or a signal is received.
620 * The @condition is checked each time the waitqueue @wq is woken up.
622 * It must be called with wq.lock being held. This spinlock is
623 * unlocked while sleeping but @condition testing is done while lock
624 * is held and when this macro exits the lock is held.
626 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
627 * functions which must match the way they are locked/unlocked outside
630 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
631 * set thus when other process waits process on the list if this
632 * process is awaken further processes are not considered.
634 * wake_up_locked() has to be called after changing any variable that could
635 * change the result of the wait condition.
637 * The function will return -ERESTARTSYS if it was interrupted by a
638 * signal and 0 if @condition evaluated to true.
640 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
642 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
646 #define __wait_event_killable(wq, condition, ret) \
648 DEFINE_WAIT(__wait); \
651 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
654 if (!fatal_signal_pending(current)) { \
658 ret = -ERESTARTSYS; \
661 finish_wait(&wq, &__wait); \
665 * wait_event_killable - sleep until a condition gets true
666 * @wq: the waitqueue to wait on
667 * @condition: a C expression for the event to wait for
669 * The process is put to sleep (TASK_KILLABLE) until the
670 * @condition evaluates to true or a signal is received.
671 * The @condition is checked each time the waitqueue @wq is woken up.
673 * wake_up() has to be called after changing any variable that could
674 * change the result of the wait condition.
676 * The function will return -ERESTARTSYS if it was interrupted by a
677 * signal and 0 if @condition evaluated to true.
679 #define wait_event_killable(wq, condition) \
683 __wait_event_killable(wq, condition, __ret); \
688 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
690 DEFINE_WAIT(__wait); \
693 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
696 spin_unlock_irq(&lock); \
699 spin_lock_irq(&lock); \
701 finish_wait(&wq, &__wait); \
705 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
706 * condition is checked under the lock. This
707 * is expected to be called with the lock
709 * @wq: the waitqueue to wait on
710 * @condition: a C expression for the event to wait for
711 * @lock: a locked spinlock_t, which will be released before cmd
712 * and schedule() and reacquired afterwards.
713 * @cmd: a command which is invoked outside the critical section before
716 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
717 * @condition evaluates to true. The @condition is checked each time
718 * the waitqueue @wq is woken up.
720 * wake_up() has to be called after changing any variable that could
721 * change the result of the wait condition.
723 * This is supposed to be called while holding the lock. The lock is
724 * dropped before invoking the cmd and going to sleep and is reacquired
727 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
731 __wait_event_lock_irq(wq, condition, lock, cmd); \
735 * wait_event_lock_irq - sleep until a condition gets true. The
736 * condition is checked under the lock. This
737 * is expected to be called with the lock
739 * @wq: the waitqueue to wait on
740 * @condition: a C expression for the event to wait for
741 * @lock: a locked spinlock_t, which will be released before schedule()
742 * and reacquired afterwards.
744 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
745 * @condition evaluates to true. The @condition is checked each time
746 * the waitqueue @wq is woken up.
748 * wake_up() has to be called after changing any variable that could
749 * change the result of the wait condition.
751 * This is supposed to be called while holding the lock. The lock is
752 * dropped before going to sleep and is reacquired afterwards.
754 #define wait_event_lock_irq(wq, condition, lock) \
758 __wait_event_lock_irq(wq, condition, lock, ); \
762 #define __wait_event_interruptible_lock_irq(wq, condition, \
765 DEFINE_WAIT(__wait); \
768 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
771 if (signal_pending(current)) { \
772 ret = -ERESTARTSYS; \
775 spin_unlock_irq(&lock); \
778 spin_lock_irq(&lock); \
780 finish_wait(&wq, &__wait); \
784 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
785 * The condition is checked under the lock. This is expected to
786 * be called with the lock taken.
787 * @wq: the waitqueue to wait on
788 * @condition: a C expression for the event to wait for
789 * @lock: a locked spinlock_t, which will be released before cmd and
790 * schedule() and reacquired afterwards.
791 * @cmd: a command which is invoked outside the critical section before
794 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
795 * @condition evaluates to true or a signal is received. The @condition is
796 * checked each time the waitqueue @wq is woken up.
798 * wake_up() has to be called after changing any variable that could
799 * change the result of the wait condition.
801 * This is supposed to be called while holding the lock. The lock is
802 * dropped before invoking the cmd and going to sleep and is reacquired
805 * The macro will return -ERESTARTSYS if it was interrupted by a signal
806 * and 0 if @condition evaluated to true.
808 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
813 __wait_event_interruptible_lock_irq(wq, condition, \
819 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
820 * The condition is checked under the lock. This is expected
821 * to be called with the lock taken.
822 * @wq: the waitqueue to wait on
823 * @condition: a C expression for the event to wait for
824 * @lock: a locked spinlock_t, which will be released before schedule()
825 * and reacquired afterwards.
827 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
828 * @condition evaluates to true or signal is received. The @condition is
829 * checked each time the waitqueue @wq is woken up.
831 * wake_up() has to be called after changing any variable that could
832 * change the result of the wait condition.
834 * This is supposed to be called while holding the lock. The lock is
835 * dropped before going to sleep and is reacquired afterwards.
837 * The macro will return -ERESTARTSYS if it was interrupted by a signal
838 * and 0 if @condition evaluated to true.
840 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
845 __wait_event_interruptible_lock_irq(wq, condition, \
850 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
853 DEFINE_WAIT(__wait); \
856 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
859 if (signal_pending(current)) { \
860 ret = -ERESTARTSYS; \
863 spin_unlock_irq(&lock); \
864 ret = schedule_timeout(ret); \
865 spin_lock_irq(&lock); \
869 finish_wait(&wq, &__wait); \
873 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
874 * The condition is checked under the lock. This is expected
875 * to be called with the lock taken.
876 * @wq: the waitqueue to wait on
877 * @condition: a C expression for the event to wait for
878 * @lock: a locked spinlock_t, which will be released before schedule()
879 * and reacquired afterwards.
880 * @timeout: timeout, in jiffies
882 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
883 * @condition evaluates to true or signal is received. The @condition is
884 * checked each time the waitqueue @wq is woken up.
886 * wake_up() has to be called after changing any variable that could
887 * change the result of the wait condition.
889 * This is supposed to be called while holding the lock. The lock is
890 * dropped before going to sleep and is reacquired afterwards.
892 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
893 * was interrupted by a signal, and the remaining jiffies otherwise
894 * if the condition evaluated to true before the timeout elapsed.
896 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
899 int __ret = timeout; \
902 __wait_event_interruptible_lock_irq_timeout( \
903 wq, condition, lock, __ret); \
909 * These are the old interfaces to sleep waiting for an event.
910 * They are racy. DO NOT use them, use the wait_event* interfaces above.
911 * We plan to remove these interfaces.
913 extern void sleep_on(wait_queue_head_t *q);
914 extern long sleep_on_timeout(wait_queue_head_t *q,
915 signed long timeout);
916 extern void interruptible_sleep_on(wait_queue_head_t *q);
917 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
918 signed long timeout);
921 * Waitqueues which are removed from the waitqueue_head at wakeup time
923 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
924 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
925 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
926 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
927 unsigned int mode, void *key);
928 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
929 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
931 #define DEFINE_WAIT_FUNC(name, function) \
932 wait_queue_t name = { \
933 .private = current, \
935 .task_list = LIST_HEAD_INIT((name).task_list), \
938 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
940 #define DEFINE_WAIT_BIT(name, word, bit) \
941 struct wait_bit_queue name = { \
942 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
944 .private = current, \
945 .func = wake_bit_function, \
947 LIST_HEAD_INIT((name).wait.task_list), \
951 #define init_wait(wait) \
953 (wait)->private = current; \
954 (wait)->func = autoremove_wake_function; \
955 INIT_LIST_HEAD(&(wait)->task_list); \
960 * wait_on_bit - wait for a bit to be cleared
961 * @word: the word being waited on, a kernel virtual address
962 * @bit: the bit of the word being waited on
963 * @action: the function used to sleep, which may take special actions
964 * @mode: the task state to sleep in
966 * There is a standard hashed waitqueue table for generic use. This
967 * is the part of the hashtable's accessor API that waits on a bit.
968 * For instance, if one were to have waiters on a bitflag, one would
969 * call wait_on_bit() in threads waiting for the bit to clear.
970 * One uses wait_on_bit() where one is waiting for the bit to clear,
971 * but has no intention of setting it.
973 static inline int wait_on_bit(void *word, int bit,
974 int (*action)(void *), unsigned mode)
976 if (!test_bit(bit, word))
978 return out_of_line_wait_on_bit(word, bit, action, mode);
982 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
983 * @word: the word being waited on, a kernel virtual address
984 * @bit: the bit of the word being waited on
985 * @action: the function used to sleep, which may take special actions
986 * @mode: the task state to sleep in
988 * There is a standard hashed waitqueue table for generic use. This
989 * is the part of the hashtable's accessor API that waits on a bit
990 * when one intends to set it, for instance, trying to lock bitflags.
991 * For instance, if one were to have waiters trying to set bitflag
992 * and waiting for it to clear before setting it, one would call
993 * wait_on_bit() in threads waiting to be able to set the bit.
994 * One uses wait_on_bit_lock() where one is waiting for the bit to
995 * clear with the intention of setting it, and when done, clearing it.
997 static inline int wait_on_bit_lock(void *word, int bit,
998 int (*action)(void *), unsigned mode)
1000 if (!test_and_set_bit(bit, word))
1002 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1006 * wait_on_atomic_t - Wait for an atomic_t to become 0
1007 * @val: The atomic value being waited on, a kernel virtual address
1008 * @action: the function used to sleep, which may take special actions
1009 * @mode: the task state to sleep in
1011 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1012 * the purpose of getting a waitqueue, but we set the key to a bit number
1013 * outside of the target 'word'.
1016 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1018 if (atomic_read(val) == 0)
1020 return out_of_line_wait_on_atomic_t(val, action, mode);