4 * Linux wait queue related types and methods
6 #include <linux/list.h>
7 #include <linux/stddef.h>
8 #include <linux/spinlock.h>
9 #include <asm/current.h>
10 #include <uapi/linux/wait.h>
12 typedef struct __wait_queue wait_queue_t;
13 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
14 int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
16 /* __wait_queue::flags */
17 #define WQ_FLAG_EXCLUSIVE 0x01
18 #define WQ_FLAG_WOKEN 0x02
23 wait_queue_func_t func;
24 struct list_head task_list;
30 #define WAIT_ATOMIC_T_BIT_NR -1
31 unsigned long timeout;
34 struct wait_bit_queue {
35 struct wait_bit_key key;
39 struct __wait_queue_head {
41 struct list_head task_list;
43 typedef struct __wait_queue_head wait_queue_head_t;
48 * Macros for declaration and initialisaton of the datatypes
51 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
53 .func = default_wake_function, \
54 .task_list = { NULL, NULL } }
56 #define DECLARE_WAITQUEUE(name, tsk) \
57 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
59 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
60 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
61 .task_list = { &(name).task_list, &(name).task_list } }
63 #define DECLARE_WAIT_QUEUE_HEAD(name) \
64 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
66 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
67 { .flags = word, .bit_nr = bit, }
69 #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
70 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
72 extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
74 #define init_waitqueue_head(q) \
76 static struct lock_class_key __key; \
78 __init_waitqueue_head((q), #q, &__key); \
82 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
83 ({ init_waitqueue_head(&name); name; })
84 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
85 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
87 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
90 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
94 q->func = default_wake_function;
98 init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
105 static inline int waitqueue_active(wait_queue_head_t *q)
107 return !list_empty(&q->task_list);
110 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
111 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
112 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
114 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
116 list_add(&new->task_list, &head->task_list);
120 * Used for wake-one threads:
123 __add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
125 wait->flags |= WQ_FLAG_EXCLUSIVE;
126 __add_wait_queue(q, wait);
129 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
132 list_add_tail(&new->task_list, &head->task_list);
136 __add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
138 wait->flags |= WQ_FLAG_EXCLUSIVE;
139 __add_wait_queue_tail(q, wait);
143 __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
145 list_del(&old->task_list);
148 typedef int wait_bit_action_f(struct wait_bit_key *);
149 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
150 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
151 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
152 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
153 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
154 void __wake_up_bit(wait_queue_head_t *, void *, int);
155 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
156 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
157 void wake_up_bit(void *, int);
158 void wake_up_atomic_t(atomic_t *);
159 int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
160 int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
161 int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
162 int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
163 wait_queue_head_t *bit_waitqueue(void *, int);
165 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
166 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
167 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
168 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
169 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
171 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
172 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
173 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
174 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
177 * Wakeup macros to be used to report events to the targets.
179 #define wake_up_poll(x, m) \
180 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
181 #define wake_up_locked_poll(x, m) \
182 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
183 #define wake_up_interruptible_poll(x, m) \
184 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
185 #define wake_up_interruptible_sync_poll(x, m) \
186 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
188 #define ___wait_cond_timeout(condition) \
190 bool __cond = (condition); \
191 if (__cond && !__ret) \
196 #define ___wait_is_interruptible(state) \
197 (!__builtin_constant_p(state) || \
198 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
201 * The below macro ___wait_event() has an explicit shadow of the __ret
202 * variable when used from the wait_event_*() macros.
204 * This is so that both can use the ___wait_cond_timeout() construct
205 * to wrap the condition.
207 * The type inconsistency of the wait_event_*() __ret variable is also
208 * on purpose; we use long where we can return timeout values and int
212 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
215 wait_queue_t __wait; \
216 long __ret = ret; /* explicit shadow */ \
218 INIT_LIST_HEAD(&__wait.task_list); \
220 __wait.flags = WQ_FLAG_EXCLUSIVE; \
225 long __int = prepare_to_wait_event(&wq, &__wait, state);\
230 if (___wait_is_interruptible(state) && __int) { \
233 abort_exclusive_wait(&wq, &__wait, \
242 finish_wait(&wq, &__wait); \
246 #define __wait_event(wq, condition) \
247 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
251 * wait_event - sleep until a condition gets true
252 * @wq: the waitqueue to wait on
253 * @condition: a C expression for the event to wait for
255 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
256 * @condition evaluates to true. The @condition is checked each time
257 * the waitqueue @wq is woken up.
259 * wake_up() has to be called after changing any variable that could
260 * change the result of the wait condition.
262 #define wait_event(wq, condition) \
267 __wait_event(wq, condition); \
270 #define __wait_event_freezable(wq, condition) \
271 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
272 schedule(); try_to_freeze())
275 * wait_event - sleep (or freeze) until a condition gets true
276 * @wq: the waitqueue to wait on
277 * @condition: a C expression for the event to wait for
279 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
280 * to system load) until the @condition evaluates to true. The
281 * @condition is checked each time the waitqueue @wq is woken up.
283 * wake_up() has to be called after changing any variable that could
284 * change the result of the wait condition.
286 #define wait_event_freezable(wq, condition) \
291 __ret = __wait_event_freezable(wq, condition); \
295 #define __wait_event_timeout(wq, condition, timeout) \
296 ___wait_event(wq, ___wait_cond_timeout(condition), \
297 TASK_UNINTERRUPTIBLE, 0, timeout, \
298 __ret = schedule_timeout(__ret))
301 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
302 * @wq: the waitqueue to wait on
303 * @condition: a C expression for the event to wait for
304 * @timeout: timeout, in jiffies
306 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
307 * @condition evaluates to true. The @condition is checked each time
308 * the waitqueue @wq is woken up.
310 * wake_up() has to be called after changing any variable that could
311 * change the result of the wait condition.
314 * 0 if the @condition evaluated to %false after the @timeout elapsed,
315 * 1 if the @condition evaluated to %true after the @timeout elapsed,
316 * or the remaining jiffies (at least 1) if the @condition evaluated
317 * to %true before the @timeout elapsed.
319 #define wait_event_timeout(wq, condition, timeout) \
321 long __ret = timeout; \
323 if (!___wait_cond_timeout(condition)) \
324 __ret = __wait_event_timeout(wq, condition, timeout); \
328 #define __wait_event_freezable_timeout(wq, condition, timeout) \
329 ___wait_event(wq, ___wait_cond_timeout(condition), \
330 TASK_INTERRUPTIBLE, 0, timeout, \
331 __ret = schedule_timeout(__ret); try_to_freeze())
334 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
335 * increasing load and is freezable.
337 #define wait_event_freezable_timeout(wq, condition, timeout) \
339 long __ret = timeout; \
341 if (!___wait_cond_timeout(condition)) \
342 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
346 #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
347 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
348 cmd1; schedule(); cmd2)
351 * wait_event_cmd - sleep until a condition gets true
352 * @wq: the waitqueue to wait on
353 * @condition: a C expression for the event to wait for
354 * @cmd1: the command will be executed before sleep
355 * @cmd2: the command will be executed after sleep
357 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
358 * @condition evaluates to true. The @condition is checked each time
359 * the waitqueue @wq is woken up.
361 * wake_up() has to be called after changing any variable that could
362 * change the result of the wait condition.
364 #define wait_event_cmd(wq, condition, cmd1, cmd2) \
368 __wait_event_cmd(wq, condition, cmd1, cmd2); \
371 #define __wait_event_interruptible(wq, condition) \
372 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
376 * wait_event_interruptible - sleep until a condition gets true
377 * @wq: the waitqueue to wait on
378 * @condition: a C expression for the event to wait for
380 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
381 * @condition evaluates to true or a signal is received.
382 * The @condition is checked each time the waitqueue @wq is woken up.
384 * wake_up() has to be called after changing any variable that could
385 * change the result of the wait condition.
387 * The function will return -ERESTARTSYS if it was interrupted by a
388 * signal and 0 if @condition evaluated to true.
390 #define wait_event_interruptible(wq, condition) \
395 __ret = __wait_event_interruptible(wq, condition); \
399 #define __wait_event_interruptible_timeout(wq, condition, timeout) \
400 ___wait_event(wq, ___wait_cond_timeout(condition), \
401 TASK_INTERRUPTIBLE, 0, timeout, \
402 __ret = schedule_timeout(__ret))
405 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
406 * @wq: the waitqueue to wait on
407 * @condition: a C expression for the event to wait for
408 * @timeout: timeout, in jiffies
410 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
411 * @condition evaluates to true or a signal is received.
412 * The @condition is checked each time the waitqueue @wq is woken up.
414 * wake_up() has to be called after changing any variable that could
415 * change the result of the wait condition.
418 * 0 if the @condition evaluated to %false after the @timeout elapsed,
419 * 1 if the @condition evaluated to %true after the @timeout elapsed,
420 * the remaining jiffies (at least 1) if the @condition evaluated
421 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
422 * interrupted by a signal.
424 #define wait_event_interruptible_timeout(wq, condition, timeout) \
426 long __ret = timeout; \
428 if (!___wait_cond_timeout(condition)) \
429 __ret = __wait_event_interruptible_timeout(wq, \
430 condition, timeout); \
434 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
437 struct hrtimer_sleeper __t; \
439 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
441 hrtimer_init_sleeper(&__t, current); \
442 if ((timeout).tv64 != KTIME_MAX) \
443 hrtimer_start_range_ns(&__t.timer, timeout, \
444 current->timer_slack_ns, \
447 __ret = ___wait_event(wq, condition, state, 0, 0, \
454 hrtimer_cancel(&__t.timer); \
455 destroy_hrtimer_on_stack(&__t.timer); \
460 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
461 * @wq: the waitqueue to wait on
462 * @condition: a C expression for the event to wait for
463 * @timeout: timeout, as a ktime_t
465 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
466 * @condition evaluates to true or a signal is received.
467 * The @condition is checked each time the waitqueue @wq is woken up.
469 * wake_up() has to be called after changing any variable that could
470 * change the result of the wait condition.
472 * The function returns 0 if @condition became true, or -ETIME if the timeout
475 #define wait_event_hrtimeout(wq, condition, timeout) \
480 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
481 TASK_UNINTERRUPTIBLE); \
486 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
487 * @wq: the waitqueue to wait on
488 * @condition: a C expression for the event to wait for
489 * @timeout: timeout, as a ktime_t
491 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
492 * @condition evaluates to true or a signal is received.
493 * The @condition is checked each time the waitqueue @wq is woken up.
495 * wake_up() has to be called after changing any variable that could
496 * change the result of the wait condition.
498 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
499 * interrupted by a signal, or -ETIME if the timeout elapsed.
501 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
506 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
507 TASK_INTERRUPTIBLE); \
511 #define __wait_event_interruptible_exclusive(wq, condition) \
512 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
515 #define wait_event_interruptible_exclusive(wq, condition) \
520 __ret = __wait_event_interruptible_exclusive(wq, condition);\
525 #define __wait_event_freezable_exclusive(wq, condition) \
526 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
527 schedule(); try_to_freeze())
529 #define wait_event_freezable_exclusive(wq, condition) \
534 __ret = __wait_event_freezable_exclusive(wq, condition);\
539 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
542 DEFINE_WAIT(__wait); \
544 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
546 if (likely(list_empty(&__wait.task_list))) \
547 __add_wait_queue_tail(&(wq), &__wait); \
548 set_current_state(TASK_INTERRUPTIBLE); \
549 if (signal_pending(current)) { \
550 __ret = -ERESTARTSYS; \
554 spin_unlock_irq(&(wq).lock); \
556 spin_unlock(&(wq).lock); \
559 spin_lock_irq(&(wq).lock); \
561 spin_lock(&(wq).lock); \
562 } while (!(condition)); \
563 __remove_wait_queue(&(wq), &__wait); \
564 __set_current_state(TASK_RUNNING); \
570 * wait_event_interruptible_locked - sleep until a condition gets true
571 * @wq: the waitqueue to wait on
572 * @condition: a C expression for the event to wait for
574 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
575 * @condition evaluates to true or a signal is received.
576 * The @condition is checked each time the waitqueue @wq is woken up.
578 * It must be called with wq.lock being held. This spinlock is
579 * unlocked while sleeping but @condition testing is done while lock
580 * is held and when this macro exits the lock is held.
582 * The lock is locked/unlocked using spin_lock()/spin_unlock()
583 * functions which must match the way they are locked/unlocked outside
586 * wake_up_locked() has to be called after changing any variable that could
587 * change the result of the wait condition.
589 * The function will return -ERESTARTSYS if it was interrupted by a
590 * signal and 0 if @condition evaluated to true.
592 #define wait_event_interruptible_locked(wq, condition) \
594 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
597 * wait_event_interruptible_locked_irq - sleep until a condition gets true
598 * @wq: the waitqueue to wait on
599 * @condition: a C expression for the event to wait for
601 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
602 * @condition evaluates to true or a signal is received.
603 * The @condition is checked each time the waitqueue @wq is woken up.
605 * It must be called with wq.lock being held. This spinlock is
606 * unlocked while sleeping but @condition testing is done while lock
607 * is held and when this macro exits the lock is held.
609 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
610 * functions which must match the way they are locked/unlocked outside
613 * wake_up_locked() has to be called after changing any variable that could
614 * change the result of the wait condition.
616 * The function will return -ERESTARTSYS if it was interrupted by a
617 * signal and 0 if @condition evaluated to true.
619 #define wait_event_interruptible_locked_irq(wq, condition) \
621 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
624 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
625 * @wq: the waitqueue to wait on
626 * @condition: a C expression for the event to wait for
628 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
629 * @condition evaluates to true or a signal is received.
630 * The @condition is checked each time the waitqueue @wq is woken up.
632 * It must be called with wq.lock being held. This spinlock is
633 * unlocked while sleeping but @condition testing is done while lock
634 * is held and when this macro exits the lock is held.
636 * The lock is locked/unlocked using spin_lock()/spin_unlock()
637 * functions which must match the way they are locked/unlocked outside
640 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
641 * set thus when other process waits process on the list if this
642 * process is awaken further processes are not considered.
644 * wake_up_locked() has to be called after changing any variable that could
645 * change the result of the wait condition.
647 * The function will return -ERESTARTSYS if it was interrupted by a
648 * signal and 0 if @condition evaluated to true.
650 #define wait_event_interruptible_exclusive_locked(wq, condition) \
652 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
655 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
656 * @wq: the waitqueue to wait on
657 * @condition: a C expression for the event to wait for
659 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
660 * @condition evaluates to true or a signal is received.
661 * The @condition is checked each time the waitqueue @wq is woken up.
663 * It must be called with wq.lock being held. This spinlock is
664 * unlocked while sleeping but @condition testing is done while lock
665 * is held and when this macro exits the lock is held.
667 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
668 * functions which must match the way they are locked/unlocked outside
671 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
672 * set thus when other process waits process on the list if this
673 * process is awaken further processes are not considered.
675 * wake_up_locked() has to be called after changing any variable that could
676 * change the result of the wait condition.
678 * The function will return -ERESTARTSYS if it was interrupted by a
679 * signal and 0 if @condition evaluated to true.
681 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
683 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
686 #define __wait_event_killable(wq, condition) \
687 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
690 * wait_event_killable - sleep until a condition gets true
691 * @wq: the waitqueue to wait on
692 * @condition: a C expression for the event to wait for
694 * The process is put to sleep (TASK_KILLABLE) until the
695 * @condition evaluates to true or a signal is received.
696 * The @condition is checked each time the waitqueue @wq is woken up.
698 * wake_up() has to be called after changing any variable that could
699 * change the result of the wait condition.
701 * The function will return -ERESTARTSYS if it was interrupted by a
702 * signal and 0 if @condition evaluated to true.
704 #define wait_event_killable(wq, condition) \
709 __ret = __wait_event_killable(wq, condition); \
714 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
715 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
716 spin_unlock_irq(&lock); \
719 spin_lock_irq(&lock))
722 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
723 * condition is checked under the lock. This
724 * is expected to be called with the lock
726 * @wq: the waitqueue to wait on
727 * @condition: a C expression for the event to wait for
728 * @lock: a locked spinlock_t, which will be released before cmd
729 * and schedule() and reacquired afterwards.
730 * @cmd: a command which is invoked outside the critical section before
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 invoking the cmd and going to sleep and is reacquired
744 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
748 __wait_event_lock_irq(wq, condition, lock, cmd); \
752 * wait_event_lock_irq - sleep until a condition gets true. The
753 * condition is checked under the lock. This
754 * is expected to be called with the lock
756 * @wq: the waitqueue to wait on
757 * @condition: a C expression for the event to wait for
758 * @lock: a locked spinlock_t, which will be released before schedule()
759 * and reacquired afterwards.
761 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
762 * @condition evaluates to true. The @condition is checked each time
763 * the waitqueue @wq is woken up.
765 * wake_up() has to be called after changing any variable that could
766 * change the result of the wait condition.
768 * This is supposed to be called while holding the lock. The lock is
769 * dropped before going to sleep and is reacquired afterwards.
771 #define wait_event_lock_irq(wq, condition, lock) \
775 __wait_event_lock_irq(wq, condition, lock, ); \
779 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
780 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
781 spin_unlock_irq(&lock); \
784 spin_lock_irq(&lock))
787 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
788 * The condition is checked under the lock. This is expected to
789 * be called with the lock taken.
790 * @wq: the waitqueue to wait on
791 * @condition: a C expression for the event to wait for
792 * @lock: a locked spinlock_t, which will be released before cmd and
793 * schedule() and reacquired afterwards.
794 * @cmd: a command which is invoked outside the critical section before
797 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
798 * @condition evaluates to true or a signal is received. The @condition is
799 * checked each time the waitqueue @wq is woken up.
801 * wake_up() has to be called after changing any variable that could
802 * change the result of the wait condition.
804 * This is supposed to be called while holding the lock. The lock is
805 * dropped before invoking the cmd and going to sleep and is reacquired
808 * The macro will return -ERESTARTSYS if it was interrupted by a signal
809 * and 0 if @condition evaluated to true.
811 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
815 __ret = __wait_event_interruptible_lock_irq(wq, \
816 condition, lock, cmd); \
821 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
822 * The condition is checked under the lock. This is expected
823 * to be called with the lock taken.
824 * @wq: the waitqueue to wait on
825 * @condition: a C expression for the event to wait for
826 * @lock: a locked spinlock_t, which will be released before schedule()
827 * and reacquired afterwards.
829 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
830 * @condition evaluates to true or signal is received. The @condition is
831 * checked each time the waitqueue @wq is woken up.
833 * wake_up() has to be called after changing any variable that could
834 * change the result of the wait condition.
836 * This is supposed to be called while holding the lock. The lock is
837 * dropped before going to sleep and is reacquired afterwards.
839 * The macro will return -ERESTARTSYS if it was interrupted by a signal
840 * and 0 if @condition evaluated to true.
842 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
846 __ret = __wait_event_interruptible_lock_irq(wq, \
851 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
853 ___wait_event(wq, ___wait_cond_timeout(condition), \
854 TASK_INTERRUPTIBLE, 0, timeout, \
855 spin_unlock_irq(&lock); \
856 __ret = schedule_timeout(__ret); \
857 spin_lock_irq(&lock));
860 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
861 * true or a timeout elapses. The condition is checked under
862 * the lock. This is expected 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 long __ret = timeout; \
887 if (!___wait_cond_timeout(condition)) \
888 __ret = __wait_event_interruptible_lock_irq_timeout( \
889 wq, condition, lock, timeout); \
894 * Waitqueues which are removed from the waitqueue_head at wakeup time
896 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
897 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
898 long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
899 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
900 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
901 long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
902 int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
903 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
904 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
906 #define DEFINE_WAIT_FUNC(name, function) \
907 wait_queue_t name = { \
908 .private = current, \
910 .task_list = LIST_HEAD_INIT((name).task_list), \
913 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
915 #define DEFINE_WAIT_BIT(name, word, bit) \
916 struct wait_bit_queue name = { \
917 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
919 .private = current, \
920 .func = wake_bit_function, \
922 LIST_HEAD_INIT((name).wait.task_list), \
926 #define init_wait(wait) \
928 (wait)->private = current; \
929 (wait)->func = autoremove_wake_function; \
930 INIT_LIST_HEAD(&(wait)->task_list); \
935 extern int bit_wait(struct wait_bit_key *);
936 extern int bit_wait_io(struct wait_bit_key *);
937 extern int bit_wait_timeout(struct wait_bit_key *);
938 extern int bit_wait_io_timeout(struct wait_bit_key *);
941 * wait_on_bit - wait for a bit to be cleared
942 * @word: the word being waited on, a kernel virtual address
943 * @bit: the bit of the word being waited on
944 * @mode: the task state to sleep in
946 * There is a standard hashed waitqueue table for generic use. This
947 * is the part of the hashtable's accessor API that waits on a bit.
948 * For instance, if one were to have waiters on a bitflag, one would
949 * call wait_on_bit() in threads waiting for the bit to clear.
950 * One uses wait_on_bit() where one is waiting for the bit to clear,
951 * but has no intention of setting it.
952 * Returned value will be zero if the bit was cleared, or non-zero
953 * if the process received a signal and the mode permitted wakeup
957 wait_on_bit(void *word, int bit, unsigned mode)
960 if (!test_bit(bit, word))
962 return out_of_line_wait_on_bit(word, bit,
968 * wait_on_bit_io - wait for a bit to be cleared
969 * @word: the word being waited on, a kernel virtual address
970 * @bit: the bit of the word being waited on
971 * @mode: the task state to sleep in
973 * Use the standard hashed waitqueue table to wait for a bit
974 * to be cleared. This is similar to wait_on_bit(), but calls
975 * io_schedule() instead of schedule() for the actual waiting.
977 * Returned value will be zero if the bit was cleared, or non-zero
978 * if the process received a signal and the mode permitted wakeup
982 wait_on_bit_io(void *word, int bit, unsigned mode)
985 if (!test_bit(bit, word))
987 return out_of_line_wait_on_bit(word, bit,
993 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
994 * @word: the word being waited on, a kernel virtual address
995 * @bit: the bit of the word being waited on
996 * @mode: the task state to sleep in
997 * @timeout: timeout, in jiffies
999 * Use the standard hashed waitqueue table to wait for a bit
1000 * to be cleared. This is similar to wait_on_bit(), except also takes a
1001 * timeout parameter.
1003 * Returned value will be zero if the bit was cleared before the
1004 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1005 * received a signal and the mode permitted wakeup on that signal.
1008 wait_on_bit_timeout(void *word, int bit, unsigned mode, unsigned long timeout)
1011 if (!test_bit(bit, word))
1013 return out_of_line_wait_on_bit_timeout(word, bit,
1019 * wait_on_bit_action - wait for a bit to be cleared
1020 * @word: the word being waited on, a kernel virtual address
1021 * @bit: the bit of the word being waited on
1022 * @action: the function used to sleep, which may take special actions
1023 * @mode: the task state to sleep in
1025 * Use the standard hashed waitqueue table to wait for a bit
1026 * to be cleared, and allow the waiting action to be specified.
1027 * This is like wait_on_bit() but allows fine control of how the waiting
1030 * Returned value will be zero if the bit was cleared, or non-zero
1031 * if the process received a signal and the mode permitted wakeup
1035 wait_on_bit_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1038 if (!test_bit(bit, word))
1040 return out_of_line_wait_on_bit(word, bit, action, mode);
1044 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1045 * @word: the word being waited on, a kernel virtual address
1046 * @bit: the bit of the word being waited on
1047 * @mode: the task state to sleep in
1049 * There is a standard hashed waitqueue table for generic use. This
1050 * is the part of the hashtable's accessor API that waits on a bit
1051 * when one intends to set it, for instance, trying to lock bitflags.
1052 * For instance, if one were to have waiters trying to set bitflag
1053 * and waiting for it to clear before setting it, one would call
1054 * wait_on_bit() in threads waiting to be able to set the bit.
1055 * One uses wait_on_bit_lock() where one is waiting for the bit to
1056 * clear with the intention of setting it, and when done, clearing it.
1058 * Returns zero if the bit was (eventually) found to be clear and was
1059 * set. Returns non-zero if a signal was delivered to the process and
1060 * the @mode allows that signal to wake the process.
1063 wait_on_bit_lock(void *word, int bit, unsigned mode)
1066 if (!test_and_set_bit(bit, word))
1068 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1072 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1073 * @word: the word being waited on, a kernel virtual address
1074 * @bit: the bit of the word being waited on
1075 * @mode: the task state to sleep in
1077 * Use the standard hashed waitqueue table to wait for a bit
1078 * to be cleared and then to atomically set it. This is similar
1079 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1080 * for the actual waiting.
1082 * Returns zero if the bit was (eventually) found to be clear and was
1083 * set. Returns non-zero if a signal was delivered to the process and
1084 * the @mode allows that signal to wake the process.
1087 wait_on_bit_lock_io(void *word, int bit, unsigned mode)
1090 if (!test_and_set_bit(bit, word))
1092 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1096 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1097 * @word: the word being waited on, a kernel virtual address
1098 * @bit: the bit of the word being waited on
1099 * @action: the function used to sleep, which may take special actions
1100 * @mode: the task state to sleep in
1102 * Use the standard hashed waitqueue table to wait for a bit
1103 * to be cleared and then to set it, and allow the waiting action
1105 * This is like wait_on_bit() but allows fine control of how the waiting
1108 * Returns zero if the bit was (eventually) found to be clear and was
1109 * set. Returns non-zero if a signal was delivered to the process and
1110 * the @mode allows that signal to wake the process.
1113 wait_on_bit_lock_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1116 if (!test_and_set_bit(bit, word))
1118 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1122 * wait_on_atomic_t - Wait for an atomic_t to become 0
1123 * @val: The atomic value being waited on, a kernel virtual address
1124 * @action: the function used to sleep, which may take special actions
1125 * @mode: the task state to sleep in
1127 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1128 * the purpose of getting a waitqueue, but we set the key to a bit number
1129 * outside of the target 'word'.
1132 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1135 if (atomic_read(val) == 0)
1137 return out_of_line_wait_on_atomic_t(val, action, mode);
1140 #endif /* _LINUX_WAIT_H */