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, int nr,
152 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
153 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
154 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
155 void __wake_up_bit(wait_queue_head_t *, void *, int);
156 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
157 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
158 void wake_up_bit(void *, int);
159 void wake_up_atomic_t(atomic_t *);
160 int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
161 int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
162 int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
163 int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
164 wait_queue_head_t *bit_waitqueue(void *, int);
166 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
167 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
168 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
169 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
170 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
172 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
173 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
174 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
175 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
178 * Wakeup macros to be used to report events to the targets.
180 #define wake_up_poll(x, m) \
181 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
182 #define wake_up_locked_poll(x, m) \
183 __wake_up_locked_key((x), TASK_NORMAL, 1, (void *) (m))
184 #define wake_up_interruptible_poll(x, m) \
185 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
186 #define wake_up_interruptible_sync_poll(x, m) \
187 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
189 #define ___wait_cond_timeout(condition) \
191 bool __cond = (condition); \
192 if (__cond && !__ret) \
197 #define ___wait_is_interruptible(state) \
198 (!__builtin_constant_p(state) || \
199 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
202 * The below macro ___wait_event() has an explicit shadow of the __ret
203 * variable when used from the wait_event_*() macros.
205 * This is so that both can use the ___wait_cond_timeout() construct
206 * to wrap the condition.
208 * The type inconsistency of the wait_event_*() __ret variable is also
209 * on purpose; we use long where we can return timeout values and int
213 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
216 wait_queue_t __wait; \
217 long __ret = ret; /* explicit shadow */ \
219 INIT_LIST_HEAD(&__wait.task_list); \
221 __wait.flags = WQ_FLAG_EXCLUSIVE; \
226 long __int = prepare_to_wait_event(&wq, &__wait, state);\
231 if (___wait_is_interruptible(state) && __int) { \
234 abort_exclusive_wait(&wq, &__wait, \
243 finish_wait(&wq, &__wait); \
247 #define __wait_event(wq, condition) \
248 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
252 * wait_event - sleep until a condition gets true
253 * @wq: the waitqueue to wait on
254 * @condition: a C expression for the event to wait for
256 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
257 * @condition evaluates to true. The @condition is checked each time
258 * the waitqueue @wq is woken up.
260 * wake_up() has to be called after changing any variable that could
261 * change the result of the wait condition.
263 #define wait_event(wq, condition) \
268 __wait_event(wq, condition); \
271 #define __io_wait_event(wq, condition) \
272 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
276 * io_wait_event() -- like wait_event() but with io_schedule()
278 #define io_wait_event(wq, condition) \
283 __io_wait_event(wq, condition); \
286 #define __wait_event_freezable(wq, condition) \
287 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
288 schedule(); try_to_freeze())
291 * wait_event - sleep (or freeze) until a condition gets true
292 * @wq: the waitqueue to wait on
293 * @condition: a C expression for the event to wait for
295 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
296 * to system load) until the @condition evaluates to true. The
297 * @condition is checked each time the waitqueue @wq is woken up.
299 * wake_up() has to be called after changing any variable that could
300 * change the result of the wait condition.
302 #define wait_event_freezable(wq, condition) \
307 __ret = __wait_event_freezable(wq, condition); \
311 #define __wait_event_timeout(wq, condition, timeout) \
312 ___wait_event(wq, ___wait_cond_timeout(condition), \
313 TASK_UNINTERRUPTIBLE, 0, timeout, \
314 __ret = schedule_timeout(__ret))
317 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
318 * @wq: the waitqueue to wait on
319 * @condition: a C expression for the event to wait for
320 * @timeout: timeout, in jiffies
322 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
323 * @condition evaluates to true. The @condition is checked each time
324 * the waitqueue @wq is woken up.
326 * wake_up() has to be called after changing any variable that could
327 * change the result of the wait condition.
330 * 0 if the @condition evaluated to %false after the @timeout elapsed,
331 * 1 if the @condition evaluated to %true after the @timeout elapsed,
332 * or the remaining jiffies (at least 1) if the @condition evaluated
333 * to %true before the @timeout elapsed.
335 #define wait_event_timeout(wq, condition, timeout) \
337 long __ret = timeout; \
339 if (!___wait_cond_timeout(condition)) \
340 __ret = __wait_event_timeout(wq, condition, timeout); \
344 #define __wait_event_freezable_timeout(wq, condition, timeout) \
345 ___wait_event(wq, ___wait_cond_timeout(condition), \
346 TASK_INTERRUPTIBLE, 0, timeout, \
347 __ret = schedule_timeout(__ret); try_to_freeze())
350 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
351 * increasing load and is freezable.
353 #define wait_event_freezable_timeout(wq, condition, timeout) \
355 long __ret = timeout; \
357 if (!___wait_cond_timeout(condition)) \
358 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
362 #define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
363 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
364 cmd1; schedule(); cmd2)
366 * Just like wait_event_cmd(), except it sets exclusive flag
368 #define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
372 __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2); \
375 #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
376 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
377 cmd1; schedule(); cmd2)
380 * wait_event_cmd - sleep until a condition gets true
381 * @wq: the waitqueue to wait on
382 * @condition: a C expression for the event to wait for
383 * @cmd1: the command will be executed before sleep
384 * @cmd2: the command will be executed after sleep
386 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
387 * @condition evaluates to true. The @condition is checked each time
388 * the waitqueue @wq is woken up.
390 * wake_up() has to be called after changing any variable that could
391 * change the result of the wait condition.
393 #define wait_event_cmd(wq, condition, cmd1, cmd2) \
397 __wait_event_cmd(wq, condition, cmd1, cmd2); \
400 #define __wait_event_interruptible(wq, condition) \
401 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
405 * wait_event_interruptible - sleep until a condition gets true
406 * @wq: the waitqueue to wait on
407 * @condition: a C expression for the event to wait for
409 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
410 * @condition evaluates to true or a signal is received.
411 * The @condition is checked each time the waitqueue @wq is woken up.
413 * wake_up() has to be called after changing any variable that could
414 * change the result of the wait condition.
416 * The function will return -ERESTARTSYS if it was interrupted by a
417 * signal and 0 if @condition evaluated to true.
419 #define wait_event_interruptible(wq, condition) \
424 __ret = __wait_event_interruptible(wq, condition); \
428 #define __wait_event_interruptible_timeout(wq, condition, timeout) \
429 ___wait_event(wq, ___wait_cond_timeout(condition), \
430 TASK_INTERRUPTIBLE, 0, timeout, \
431 __ret = schedule_timeout(__ret))
434 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
435 * @wq: the waitqueue to wait on
436 * @condition: a C expression for the event to wait for
437 * @timeout: timeout, in jiffies
439 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
440 * @condition evaluates to true or a signal is received.
441 * The @condition is checked each time the waitqueue @wq is woken up.
443 * wake_up() has to be called after changing any variable that could
444 * change the result of the wait condition.
447 * 0 if the @condition evaluated to %false after the @timeout elapsed,
448 * 1 if the @condition evaluated to %true after the @timeout elapsed,
449 * the remaining jiffies (at least 1) if the @condition evaluated
450 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
451 * interrupted by a signal.
453 #define wait_event_interruptible_timeout(wq, condition, timeout) \
455 long __ret = timeout; \
457 if (!___wait_cond_timeout(condition)) \
458 __ret = __wait_event_interruptible_timeout(wq, \
459 condition, timeout); \
463 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
466 struct hrtimer_sleeper __t; \
468 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
470 hrtimer_init_sleeper(&__t, current); \
471 if ((timeout).tv64 != KTIME_MAX) \
472 hrtimer_start_range_ns(&__t.timer, timeout, \
473 current->timer_slack_ns, \
476 __ret = ___wait_event(wq, condition, state, 0, 0, \
483 hrtimer_cancel(&__t.timer); \
484 destroy_hrtimer_on_stack(&__t.timer); \
489 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
490 * @wq: the waitqueue to wait on
491 * @condition: a C expression for the event to wait for
492 * @timeout: timeout, as a ktime_t
494 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
495 * @condition evaluates to true or a signal is received.
496 * The @condition is checked each time the waitqueue @wq is woken up.
498 * wake_up() has to be called after changing any variable that could
499 * change the result of the wait condition.
501 * The function returns 0 if @condition became true, or -ETIME if the timeout
504 #define wait_event_hrtimeout(wq, condition, timeout) \
509 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
510 TASK_UNINTERRUPTIBLE); \
515 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
516 * @wq: the waitqueue to wait on
517 * @condition: a C expression for the event to wait for
518 * @timeout: timeout, as a ktime_t
520 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
521 * @condition evaluates to true or a signal is received.
522 * The @condition is checked each time the waitqueue @wq is woken up.
524 * wake_up() has to be called after changing any variable that could
525 * change the result of the wait condition.
527 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
528 * interrupted by a signal, or -ETIME if the timeout elapsed.
530 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
535 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
536 TASK_INTERRUPTIBLE); \
540 #define __wait_event_interruptible_exclusive(wq, condition) \
541 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
544 #define wait_event_interruptible_exclusive(wq, condition) \
549 __ret = __wait_event_interruptible_exclusive(wq, condition);\
554 #define __wait_event_freezable_exclusive(wq, condition) \
555 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
556 schedule(); try_to_freeze())
558 #define wait_event_freezable_exclusive(wq, condition) \
563 __ret = __wait_event_freezable_exclusive(wq, condition);\
568 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
571 DEFINE_WAIT(__wait); \
573 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
575 if (likely(list_empty(&__wait.task_list))) \
576 __add_wait_queue_tail(&(wq), &__wait); \
577 set_current_state(TASK_INTERRUPTIBLE); \
578 if (signal_pending(current)) { \
579 __ret = -ERESTARTSYS; \
583 spin_unlock_irq(&(wq).lock); \
585 spin_unlock(&(wq).lock); \
588 spin_lock_irq(&(wq).lock); \
590 spin_lock(&(wq).lock); \
591 } while (!(condition)); \
592 __remove_wait_queue(&(wq), &__wait); \
593 __set_current_state(TASK_RUNNING); \
599 * wait_event_interruptible_locked - sleep until a condition gets true
600 * @wq: the waitqueue to wait on
601 * @condition: a C expression for the event to wait for
603 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
604 * @condition evaluates to true or a signal is received.
605 * The @condition is checked each time the waitqueue @wq is woken up.
607 * It must be called with wq.lock being held. This spinlock is
608 * unlocked while sleeping but @condition testing is done while lock
609 * is held and when this macro exits the lock is held.
611 * The lock is locked/unlocked using spin_lock()/spin_unlock()
612 * functions which must match the way they are locked/unlocked outside
615 * wake_up_locked() has to be called after changing any variable that could
616 * change the result of the wait condition.
618 * The function will return -ERESTARTSYS if it was interrupted by a
619 * signal and 0 if @condition evaluated to true.
621 #define wait_event_interruptible_locked(wq, condition) \
623 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
626 * wait_event_interruptible_locked_irq - sleep until a condition gets true
627 * @wq: the waitqueue to wait on
628 * @condition: a C expression for the event to wait for
630 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
631 * @condition evaluates to true or a signal is received.
632 * The @condition is checked each time the waitqueue @wq is woken up.
634 * It must be called with wq.lock being held. This spinlock is
635 * unlocked while sleeping but @condition testing is done while lock
636 * is held and when this macro exits the lock is held.
638 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
639 * functions which must match the way they are locked/unlocked outside
642 * wake_up_locked() has to be called after changing any variable that could
643 * change the result of the wait condition.
645 * The function will return -ERESTARTSYS if it was interrupted by a
646 * signal and 0 if @condition evaluated to true.
648 #define wait_event_interruptible_locked_irq(wq, condition) \
650 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
653 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
654 * @wq: the waitqueue to wait on
655 * @condition: a C expression for the event to wait for
657 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
658 * @condition evaluates to true or a signal is received.
659 * The @condition is checked each time the waitqueue @wq is woken up.
661 * It must be called with wq.lock being held. This spinlock is
662 * unlocked while sleeping but @condition testing is done while lock
663 * is held and when this macro exits the lock is held.
665 * The lock is locked/unlocked using spin_lock()/spin_unlock()
666 * functions which must match the way they are locked/unlocked outside
669 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
670 * set thus when other process waits process on the list if this
671 * process is awaken further processes are not considered.
673 * wake_up_locked() 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_interruptible_exclusive_locked(wq, condition) \
681 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
684 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
685 * @wq: the waitqueue to wait on
686 * @condition: a C expression for the event to wait for
688 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
689 * @condition evaluates to true or a signal is received.
690 * The @condition is checked each time the waitqueue @wq is woken up.
692 * It must be called with wq.lock being held. This spinlock is
693 * unlocked while sleeping but @condition testing is done while lock
694 * is held and when this macro exits the lock is held.
696 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
697 * functions which must match the way they are locked/unlocked outside
700 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
701 * set thus when other process waits process on the list if this
702 * process is awaken further processes are not considered.
704 * wake_up_locked() has to be called after changing any variable that could
705 * change the result of the wait condition.
707 * The function will return -ERESTARTSYS if it was interrupted by a
708 * signal and 0 if @condition evaluated to true.
710 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
712 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
715 #define __wait_event_killable(wq, condition) \
716 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
719 * wait_event_killable - sleep until a condition gets true
720 * @wq: the waitqueue to wait on
721 * @condition: a C expression for the event to wait for
723 * The process is put to sleep (TASK_KILLABLE) until the
724 * @condition evaluates to true or a signal is received.
725 * The @condition is checked each time the waitqueue @wq is woken up.
727 * wake_up() has to be called after changing any variable that could
728 * change the result of the wait condition.
730 * The function will return -ERESTARTSYS if it was interrupted by a
731 * signal and 0 if @condition evaluated to true.
733 #define wait_event_killable(wq, condition) \
738 __ret = __wait_event_killable(wq, condition); \
743 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
744 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
745 spin_unlock_irq(&lock); \
748 spin_lock_irq(&lock))
751 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
752 * condition is checked under the lock. This
753 * is expected to be called with the lock
755 * @wq: the waitqueue to wait on
756 * @condition: a C expression for the event to wait for
757 * @lock: a locked spinlock_t, which will be released before cmd
758 * and schedule() and reacquired afterwards.
759 * @cmd: a command which is invoked outside the critical section before
762 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
763 * @condition evaluates to true. The @condition is checked each time
764 * the waitqueue @wq is woken up.
766 * wake_up() has to be called after changing any variable that could
767 * change the result of the wait condition.
769 * This is supposed to be called while holding the lock. The lock is
770 * dropped before invoking the cmd and going to sleep and is reacquired
773 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
777 __wait_event_lock_irq(wq, condition, lock, cmd); \
781 * wait_event_lock_irq - sleep until a condition gets true. The
782 * condition is checked under the lock. This
783 * is expected to be called with the lock
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 schedule()
788 * and reacquired afterwards.
790 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
791 * @condition evaluates to true. The @condition is checked each time
792 * the waitqueue @wq is woken up.
794 * wake_up() has to be called after changing any variable that could
795 * change the result of the wait condition.
797 * This is supposed to be called while holding the lock. The lock is
798 * dropped before going to sleep and is reacquired afterwards.
800 #define wait_event_lock_irq(wq, condition, lock) \
804 __wait_event_lock_irq(wq, condition, lock, ); \
808 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
809 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
810 spin_unlock_irq(&lock); \
813 spin_lock_irq(&lock))
816 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
817 * The condition is checked under the lock. This is expected to
818 * be called with the lock taken.
819 * @wq: the waitqueue to wait on
820 * @condition: a C expression for the event to wait for
821 * @lock: a locked spinlock_t, which will be released before cmd and
822 * schedule() and reacquired afterwards.
823 * @cmd: a command which is invoked outside the critical section before
826 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
827 * @condition evaluates to true or a signal is received. The @condition is
828 * checked each time the waitqueue @wq is woken up.
830 * wake_up() has to be called after changing any variable that could
831 * change the result of the wait condition.
833 * This is supposed to be called while holding the lock. The lock is
834 * dropped before invoking the cmd and going to sleep and is reacquired
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_cmd(wq, condition, lock, cmd) \
844 __ret = __wait_event_interruptible_lock_irq(wq, \
845 condition, lock, cmd); \
850 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
851 * The condition is checked under the lock. This is expected
852 * to be called with the lock taken.
853 * @wq: the waitqueue to wait on
854 * @condition: a C expression for the event to wait for
855 * @lock: a locked spinlock_t, which will be released before schedule()
856 * and reacquired afterwards.
858 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
859 * @condition evaluates to true or signal is received. The @condition is
860 * checked each time the waitqueue @wq is woken up.
862 * wake_up() has to be called after changing any variable that could
863 * change the result of the wait condition.
865 * This is supposed to be called while holding the lock. The lock is
866 * dropped before going to sleep and is reacquired afterwards.
868 * The macro will return -ERESTARTSYS if it was interrupted by a signal
869 * and 0 if @condition evaluated to true.
871 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
875 __ret = __wait_event_interruptible_lock_irq(wq, \
880 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
882 ___wait_event(wq, ___wait_cond_timeout(condition), \
883 TASK_INTERRUPTIBLE, 0, timeout, \
884 spin_unlock_irq(&lock); \
885 __ret = schedule_timeout(__ret); \
886 spin_lock_irq(&lock));
889 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
890 * true or a timeout elapses. The condition is checked under
891 * the lock. This is expected to be called with the lock taken.
892 * @wq: the waitqueue to wait on
893 * @condition: a C expression for the event to wait for
894 * @lock: a locked spinlock_t, which will be released before schedule()
895 * and reacquired afterwards.
896 * @timeout: timeout, in jiffies
898 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
899 * @condition evaluates to true or signal is received. The @condition is
900 * checked each time the waitqueue @wq is woken up.
902 * wake_up() has to be called after changing any variable that could
903 * change the result of the wait condition.
905 * This is supposed to be called while holding the lock. The lock is
906 * dropped before going to sleep and is reacquired afterwards.
908 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
909 * was interrupted by a signal, and the remaining jiffies otherwise
910 * if the condition evaluated to true before the timeout elapsed.
912 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
915 long __ret = timeout; \
916 if (!___wait_cond_timeout(condition)) \
917 __ret = __wait_event_interruptible_lock_irq_timeout( \
918 wq, condition, lock, timeout); \
923 * Waitqueues which are removed from the waitqueue_head at wakeup time
925 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
926 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
927 long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
928 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
929 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
930 long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
931 int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
932 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
933 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
935 #define DEFINE_WAIT_FUNC(name, function) \
936 wait_queue_t name = { \
937 .private = current, \
939 .task_list = LIST_HEAD_INIT((name).task_list), \
942 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
944 #define DEFINE_WAIT_BIT(name, word, bit) \
945 struct wait_bit_queue name = { \
946 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
948 .private = current, \
949 .func = wake_bit_function, \
951 LIST_HEAD_INIT((name).wait.task_list), \
955 #define init_wait(wait) \
957 (wait)->private = current; \
958 (wait)->func = autoremove_wake_function; \
959 INIT_LIST_HEAD(&(wait)->task_list); \
964 extern int bit_wait(struct wait_bit_key *);
965 extern int bit_wait_io(struct wait_bit_key *);
966 extern int bit_wait_timeout(struct wait_bit_key *);
967 extern int bit_wait_io_timeout(struct wait_bit_key *);
970 * wait_on_bit - wait for a bit to be cleared
971 * @word: the word being waited on, a kernel virtual address
972 * @bit: the bit of the word being waited on
973 * @mode: the task state to sleep in
975 * There is a standard hashed waitqueue table for generic use. This
976 * is the part of the hashtable's accessor API that waits on a bit.
977 * For instance, if one were to have waiters on a bitflag, one would
978 * call wait_on_bit() in threads waiting for the bit to clear.
979 * One uses wait_on_bit() where one is waiting for the bit to clear,
980 * but has no intention of setting it.
981 * Returned value will be zero if the bit was cleared, or non-zero
982 * if the process received a signal and the mode permitted wakeup
986 wait_on_bit(unsigned long *word, int bit, unsigned mode)
989 if (!test_bit(bit, word))
991 return out_of_line_wait_on_bit(word, bit,
997 * wait_on_bit_io - wait for a bit to be cleared
998 * @word: the word being waited on, a kernel virtual address
999 * @bit: the bit of the word being waited on
1000 * @mode: the task state to sleep in
1002 * Use the standard hashed waitqueue table to wait for a bit
1003 * to be cleared. This is similar to wait_on_bit(), but calls
1004 * io_schedule() instead of schedule() for the actual waiting.
1006 * Returned value will be zero if the bit was cleared, or non-zero
1007 * if the process received a signal and the mode permitted wakeup
1011 wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
1014 if (!test_bit(bit, word))
1016 return out_of_line_wait_on_bit(word, bit,
1022 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
1023 * @word: the word being waited on, a kernel virtual address
1024 * @bit: the bit of the word being waited on
1025 * @mode: the task state to sleep in
1026 * @timeout: timeout, in jiffies
1028 * Use the standard hashed waitqueue table to wait for a bit
1029 * to be cleared. This is similar to wait_on_bit(), except also takes a
1030 * timeout parameter.
1032 * Returned value will be zero if the bit was cleared before the
1033 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1034 * received a signal and the mode permitted wakeup on that signal.
1037 wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
1038 unsigned long timeout)
1041 if (!test_bit(bit, word))
1043 return out_of_line_wait_on_bit_timeout(word, bit,
1049 * wait_on_bit_action - wait for a bit to be cleared
1050 * @word: the word being waited on, a kernel virtual address
1051 * @bit: the bit of the word being waited on
1052 * @action: the function used to sleep, which may take special actions
1053 * @mode: the task state to sleep in
1055 * Use the standard hashed waitqueue table to wait for a bit
1056 * to be cleared, and allow the waiting action to be specified.
1057 * This is like wait_on_bit() but allows fine control of how the waiting
1060 * Returned value will be zero if the bit was cleared, or non-zero
1061 * if the process received a signal and the mode permitted wakeup
1065 wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
1069 if (!test_bit(bit, word))
1071 return out_of_line_wait_on_bit(word, bit, action, mode);
1075 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1076 * @word: the word being waited on, a kernel virtual address
1077 * @bit: the bit of the word being waited on
1078 * @mode: the task state to sleep in
1080 * There is a standard hashed waitqueue table for generic use. This
1081 * is the part of the hashtable's accessor API that waits on a bit
1082 * when one intends to set it, for instance, trying to lock bitflags.
1083 * For instance, if one were to have waiters trying to set bitflag
1084 * and waiting for it to clear before setting it, one would call
1085 * wait_on_bit() in threads waiting to be able to set the bit.
1086 * One uses wait_on_bit_lock() where one is waiting for the bit to
1087 * clear with the intention of setting it, and when done, clearing it.
1089 * Returns zero if the bit was (eventually) found to be clear and was
1090 * set. Returns non-zero if a signal was delivered to the process and
1091 * the @mode allows that signal to wake the process.
1094 wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
1097 if (!test_and_set_bit(bit, word))
1099 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1103 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1104 * @word: the word being waited on, a kernel virtual address
1105 * @bit: the bit of the word being waited on
1106 * @mode: the task state to sleep in
1108 * Use the standard hashed waitqueue table to wait for a bit
1109 * to be cleared and then to atomically set it. This is similar
1110 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1111 * for the actual waiting.
1113 * Returns zero if the bit was (eventually) found to be clear and was
1114 * set. Returns non-zero if a signal was delivered to the process and
1115 * the @mode allows that signal to wake the process.
1118 wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
1121 if (!test_and_set_bit(bit, word))
1123 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1127 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1128 * @word: the word being waited on, a kernel virtual address
1129 * @bit: the bit of the word being waited on
1130 * @action: the function used to sleep, which may take special actions
1131 * @mode: the task state to sleep in
1133 * Use the standard hashed waitqueue table to wait for a bit
1134 * to be cleared and then to set it, and allow the waiting action
1136 * This is like wait_on_bit() but allows fine control of how the waiting
1139 * Returns zero if the bit was (eventually) found to be clear and was
1140 * set. Returns non-zero if a signal was delivered to the process and
1141 * the @mode allows that signal to wake the process.
1144 wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
1148 if (!test_and_set_bit(bit, word))
1150 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1154 * wait_on_atomic_t - Wait for an atomic_t to become 0
1155 * @val: The atomic value being waited on, a kernel virtual address
1156 * @action: the function used to sleep, which may take special actions
1157 * @mode: the task state to sleep in
1159 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1160 * the purpose of getting a waitqueue, but we set the key to a bit number
1161 * outside of the target 'word'.
1164 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1167 if (atomic_read(val) == 0)
1169 return out_of_line_wait_on_atomic_t(val, action, mode);
1172 #endif /* _LINUX_WAIT_H */