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 __io_wait_event(wq, condition) \
271 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
275 * io_wait_event() -- like wait_event() but with io_schedule()
277 #define io_wait_event(wq, condition) \
282 __io_wait_event(wq, condition); \
285 #define __wait_event_freezable(wq, condition) \
286 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
287 schedule(); try_to_freeze())
290 * wait_event - sleep (or freeze) until a condition gets true
291 * @wq: the waitqueue to wait on
292 * @condition: a C expression for the event to wait for
294 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
295 * to system load) until the @condition evaluates to true. The
296 * @condition is checked each time the waitqueue @wq is woken up.
298 * wake_up() has to be called after changing any variable that could
299 * change the result of the wait condition.
301 #define wait_event_freezable(wq, condition) \
306 __ret = __wait_event_freezable(wq, condition); \
310 #define __wait_event_timeout(wq, condition, timeout) \
311 ___wait_event(wq, ___wait_cond_timeout(condition), \
312 TASK_UNINTERRUPTIBLE, 0, timeout, \
313 __ret = schedule_timeout(__ret))
316 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
317 * @wq: the waitqueue to wait on
318 * @condition: a C expression for the event to wait for
319 * @timeout: timeout, in jiffies
321 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
322 * @condition evaluates to true. The @condition is checked each time
323 * the waitqueue @wq is woken up.
325 * wake_up() has to be called after changing any variable that could
326 * change the result of the wait condition.
329 * 0 if the @condition evaluated to %false after the @timeout elapsed,
330 * 1 if the @condition evaluated to %true after the @timeout elapsed,
331 * or the remaining jiffies (at least 1) if the @condition evaluated
332 * to %true before the @timeout elapsed.
334 #define wait_event_timeout(wq, condition, timeout) \
336 long __ret = timeout; \
338 if (!___wait_cond_timeout(condition)) \
339 __ret = __wait_event_timeout(wq, condition, timeout); \
343 #define __wait_event_freezable_timeout(wq, condition, timeout) \
344 ___wait_event(wq, ___wait_cond_timeout(condition), \
345 TASK_INTERRUPTIBLE, 0, timeout, \
346 __ret = schedule_timeout(__ret); try_to_freeze())
349 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
350 * increasing load and is freezable.
352 #define wait_event_freezable_timeout(wq, condition, timeout) \
354 long __ret = timeout; \
356 if (!___wait_cond_timeout(condition)) \
357 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
361 #define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
362 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
363 cmd1; schedule(); cmd2)
365 * Just like wait_event_cmd(), except it sets exclusive flag
367 #define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
371 __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2); \
374 #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
375 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
376 cmd1; schedule(); cmd2)
379 * wait_event_cmd - sleep until a condition gets true
380 * @wq: the waitqueue to wait on
381 * @condition: a C expression for the event to wait for
382 * @cmd1: the command will be executed before sleep
383 * @cmd2: the command will be executed after sleep
385 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
386 * @condition evaluates to true. The @condition is checked each time
387 * the waitqueue @wq is woken up.
389 * wake_up() has to be called after changing any variable that could
390 * change the result of the wait condition.
392 #define wait_event_cmd(wq, condition, cmd1, cmd2) \
396 __wait_event_cmd(wq, condition, cmd1, cmd2); \
399 #define __wait_event_interruptible(wq, condition) \
400 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
404 * wait_event_interruptible - sleep until a condition gets true
405 * @wq: the waitqueue to wait on
406 * @condition: a C expression for the event to wait for
408 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
409 * @condition evaluates to true or a signal is received.
410 * The @condition is checked each time the waitqueue @wq is woken up.
412 * wake_up() has to be called after changing any variable that could
413 * change the result of the wait condition.
415 * The function will return -ERESTARTSYS if it was interrupted by a
416 * signal and 0 if @condition evaluated to true.
418 #define wait_event_interruptible(wq, condition) \
423 __ret = __wait_event_interruptible(wq, condition); \
427 #define __wait_event_interruptible_timeout(wq, condition, timeout) \
428 ___wait_event(wq, ___wait_cond_timeout(condition), \
429 TASK_INTERRUPTIBLE, 0, timeout, \
430 __ret = schedule_timeout(__ret))
433 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
434 * @wq: the waitqueue to wait on
435 * @condition: a C expression for the event to wait for
436 * @timeout: timeout, in jiffies
438 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
439 * @condition evaluates to true or a signal is received.
440 * The @condition is checked each time the waitqueue @wq is woken up.
442 * wake_up() has to be called after changing any variable that could
443 * change the result of the wait condition.
446 * 0 if the @condition evaluated to %false after the @timeout elapsed,
447 * 1 if the @condition evaluated to %true after the @timeout elapsed,
448 * the remaining jiffies (at least 1) if the @condition evaluated
449 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
450 * interrupted by a signal.
452 #define wait_event_interruptible_timeout(wq, condition, timeout) \
454 long __ret = timeout; \
456 if (!___wait_cond_timeout(condition)) \
457 __ret = __wait_event_interruptible_timeout(wq, \
458 condition, timeout); \
462 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
465 struct hrtimer_sleeper __t; \
467 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
469 hrtimer_init_sleeper(&__t, current); \
470 if ((timeout).tv64 != KTIME_MAX) \
471 hrtimer_start_range_ns(&__t.timer, timeout, \
472 current->timer_slack_ns, \
475 __ret = ___wait_event(wq, condition, state, 0, 0, \
482 hrtimer_cancel(&__t.timer); \
483 destroy_hrtimer_on_stack(&__t.timer); \
488 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
489 * @wq: the waitqueue to wait on
490 * @condition: a C expression for the event to wait for
491 * @timeout: timeout, as a ktime_t
493 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
494 * @condition evaluates to true or a signal is received.
495 * The @condition is checked each time the waitqueue @wq is woken up.
497 * wake_up() has to be called after changing any variable that could
498 * change the result of the wait condition.
500 * The function returns 0 if @condition became true, or -ETIME if the timeout
503 #define wait_event_hrtimeout(wq, condition, timeout) \
508 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
509 TASK_UNINTERRUPTIBLE); \
514 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
515 * @wq: the waitqueue to wait on
516 * @condition: a C expression for the event to wait for
517 * @timeout: timeout, as a ktime_t
519 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
520 * @condition evaluates to true or a signal is received.
521 * The @condition is checked each time the waitqueue @wq is woken up.
523 * wake_up() has to be called after changing any variable that could
524 * change the result of the wait condition.
526 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
527 * interrupted by a signal, or -ETIME if the timeout elapsed.
529 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
534 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
535 TASK_INTERRUPTIBLE); \
539 #define __wait_event_interruptible_exclusive(wq, condition) \
540 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
543 #define wait_event_interruptible_exclusive(wq, condition) \
548 __ret = __wait_event_interruptible_exclusive(wq, condition);\
553 #define __wait_event_freezable_exclusive(wq, condition) \
554 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
555 schedule(); try_to_freeze())
557 #define wait_event_freezable_exclusive(wq, condition) \
562 __ret = __wait_event_freezable_exclusive(wq, condition);\
567 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
570 DEFINE_WAIT(__wait); \
572 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
574 if (likely(list_empty(&__wait.task_list))) \
575 __add_wait_queue_tail(&(wq), &__wait); \
576 set_current_state(TASK_INTERRUPTIBLE); \
577 if (signal_pending(current)) { \
578 __ret = -ERESTARTSYS; \
582 spin_unlock_irq(&(wq).lock); \
584 spin_unlock(&(wq).lock); \
587 spin_lock_irq(&(wq).lock); \
589 spin_lock(&(wq).lock); \
590 } while (!(condition)); \
591 __remove_wait_queue(&(wq), &__wait); \
592 __set_current_state(TASK_RUNNING); \
598 * wait_event_interruptible_locked - sleep until a condition gets true
599 * @wq: the waitqueue to wait on
600 * @condition: a C expression for the event to wait for
602 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
603 * @condition evaluates to true or a signal is received.
604 * The @condition is checked each time the waitqueue @wq is woken up.
606 * It must be called with wq.lock being held. This spinlock is
607 * unlocked while sleeping but @condition testing is done while lock
608 * is held and when this macro exits the lock is held.
610 * The lock is locked/unlocked using spin_lock()/spin_unlock()
611 * functions which must match the way they are locked/unlocked outside
614 * wake_up_locked() has to be called after changing any variable that could
615 * change the result of the wait condition.
617 * The function will return -ERESTARTSYS if it was interrupted by a
618 * signal and 0 if @condition evaluated to true.
620 #define wait_event_interruptible_locked(wq, condition) \
622 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
625 * wait_event_interruptible_locked_irq - sleep until a condition gets true
626 * @wq: the waitqueue to wait on
627 * @condition: a C expression for the event to wait for
629 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
630 * @condition evaluates to true or a signal is received.
631 * The @condition is checked each time the waitqueue @wq is woken up.
633 * It must be called with wq.lock being held. This spinlock is
634 * unlocked while sleeping but @condition testing is done while lock
635 * is held and when this macro exits the lock is held.
637 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
638 * functions which must match the way they are locked/unlocked outside
641 * wake_up_locked() has to be called after changing any variable that could
642 * change the result of the wait condition.
644 * The function will return -ERESTARTSYS if it was interrupted by a
645 * signal and 0 if @condition evaluated to true.
647 #define wait_event_interruptible_locked_irq(wq, condition) \
649 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
652 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
653 * @wq: the waitqueue to wait on
654 * @condition: a C expression for the event to wait for
656 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
657 * @condition evaluates to true or a signal is received.
658 * The @condition is checked each time the waitqueue @wq is woken up.
660 * It must be called with wq.lock being held. This spinlock is
661 * unlocked while sleeping but @condition testing is done while lock
662 * is held and when this macro exits the lock is held.
664 * The lock is locked/unlocked using spin_lock()/spin_unlock()
665 * functions which must match the way they are locked/unlocked outside
668 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
669 * set thus when other process waits process on the list if this
670 * process is awaken further processes are not considered.
672 * wake_up_locked() has to be called after changing any variable that could
673 * change the result of the wait condition.
675 * The function will return -ERESTARTSYS if it was interrupted by a
676 * signal and 0 if @condition evaluated to true.
678 #define wait_event_interruptible_exclusive_locked(wq, condition) \
680 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
683 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
684 * @wq: the waitqueue to wait on
685 * @condition: a C expression for the event to wait for
687 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
688 * @condition evaluates to true or a signal is received.
689 * The @condition is checked each time the waitqueue @wq is woken up.
691 * It must be called with wq.lock being held. This spinlock is
692 * unlocked while sleeping but @condition testing is done while lock
693 * is held and when this macro exits the lock is held.
695 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
696 * functions which must match the way they are locked/unlocked outside
699 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
700 * set thus when other process waits process on the list if this
701 * process is awaken further processes are not considered.
703 * wake_up_locked() has to be called after changing any variable that could
704 * change the result of the wait condition.
706 * The function will return -ERESTARTSYS if it was interrupted by a
707 * signal and 0 if @condition evaluated to true.
709 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
711 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
714 #define __wait_event_killable(wq, condition) \
715 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
718 * wait_event_killable - sleep until a condition gets true
719 * @wq: the waitqueue to wait on
720 * @condition: a C expression for the event to wait for
722 * The process is put to sleep (TASK_KILLABLE) until the
723 * @condition evaluates to true or a signal is received.
724 * The @condition is checked each time the waitqueue @wq is woken up.
726 * wake_up() has to be called after changing any variable that could
727 * change the result of the wait condition.
729 * The function will return -ERESTARTSYS if it was interrupted by a
730 * signal and 0 if @condition evaluated to true.
732 #define wait_event_killable(wq, condition) \
737 __ret = __wait_event_killable(wq, condition); \
742 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
743 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
744 spin_unlock_irq(&lock); \
747 spin_lock_irq(&lock))
750 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
751 * condition is checked under the lock. This
752 * is expected to be called with the lock
754 * @wq: the waitqueue to wait on
755 * @condition: a C expression for the event to wait for
756 * @lock: a locked spinlock_t, which will be released before cmd
757 * and schedule() and reacquired afterwards.
758 * @cmd: a command which is invoked outside the critical section before
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 invoking the cmd and going to sleep and is reacquired
772 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
776 __wait_event_lock_irq(wq, condition, lock, cmd); \
780 * wait_event_lock_irq - sleep until a condition gets true. The
781 * condition is checked under the lock. This
782 * is expected to be called with the lock
784 * @wq: the waitqueue to wait on
785 * @condition: a C expression for the event to wait for
786 * @lock: a locked spinlock_t, which will be released before schedule()
787 * and reacquired afterwards.
789 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
790 * @condition evaluates to true. The @condition is checked each time
791 * the waitqueue @wq is woken up.
793 * wake_up() has to be called after changing any variable that could
794 * change the result of the wait condition.
796 * This is supposed to be called while holding the lock. The lock is
797 * dropped before going to sleep and is reacquired afterwards.
799 #define wait_event_lock_irq(wq, condition, lock) \
803 __wait_event_lock_irq(wq, condition, lock, ); \
807 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
808 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
809 spin_unlock_irq(&lock); \
812 spin_lock_irq(&lock))
815 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
816 * The condition is checked under the lock. This is expected to
817 * be called with the lock taken.
818 * @wq: the waitqueue to wait on
819 * @condition: a C expression for the event to wait for
820 * @lock: a locked spinlock_t, which will be released before cmd and
821 * schedule() and reacquired afterwards.
822 * @cmd: a command which is invoked outside the critical section before
825 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
826 * @condition evaluates to true or a signal is received. The @condition is
827 * checked each time the waitqueue @wq is woken up.
829 * wake_up() has to be called after changing any variable that could
830 * change the result of the wait condition.
832 * This is supposed to be called while holding the lock. The lock is
833 * dropped before invoking the cmd and going to sleep and is reacquired
836 * The macro will return -ERESTARTSYS if it was interrupted by a signal
837 * and 0 if @condition evaluated to true.
839 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
843 __ret = __wait_event_interruptible_lock_irq(wq, \
844 condition, lock, cmd); \
849 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
850 * The condition is checked under the lock. This is expected
851 * to be called with the lock taken.
852 * @wq: the waitqueue to wait on
853 * @condition: a C expression for the event to wait for
854 * @lock: a locked spinlock_t, which will be released before schedule()
855 * and reacquired afterwards.
857 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
858 * @condition evaluates to true or signal is received. The @condition is
859 * checked each time the waitqueue @wq is woken up.
861 * wake_up() has to be called after changing any variable that could
862 * change the result of the wait condition.
864 * This is supposed to be called while holding the lock. The lock is
865 * dropped before going to sleep and is reacquired afterwards.
867 * The macro will return -ERESTARTSYS if it was interrupted by a signal
868 * and 0 if @condition evaluated to true.
870 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
874 __ret = __wait_event_interruptible_lock_irq(wq, \
879 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
881 ___wait_event(wq, ___wait_cond_timeout(condition), \
882 TASK_INTERRUPTIBLE, 0, timeout, \
883 spin_unlock_irq(&lock); \
884 __ret = schedule_timeout(__ret); \
885 spin_lock_irq(&lock));
888 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
889 * true or a timeout elapses. The condition is checked under
890 * the lock. This is expected to be called with the lock taken.
891 * @wq: the waitqueue to wait on
892 * @condition: a C expression for the event to wait for
893 * @lock: a locked spinlock_t, which will be released before schedule()
894 * and reacquired afterwards.
895 * @timeout: timeout, in jiffies
897 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
898 * @condition evaluates to true or signal is received. The @condition is
899 * checked each time the waitqueue @wq is woken up.
901 * wake_up() has to be called after changing any variable that could
902 * change the result of the wait condition.
904 * This is supposed to be called while holding the lock. The lock is
905 * dropped before going to sleep and is reacquired afterwards.
907 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
908 * was interrupted by a signal, and the remaining jiffies otherwise
909 * if the condition evaluated to true before the timeout elapsed.
911 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
914 long __ret = timeout; \
915 if (!___wait_cond_timeout(condition)) \
916 __ret = __wait_event_interruptible_lock_irq_timeout( \
917 wq, condition, lock, timeout); \
922 * Waitqueues which are removed from the waitqueue_head at wakeup time
924 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
925 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
926 long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
927 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
928 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
929 long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
930 int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
931 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
932 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
934 #define DEFINE_WAIT_FUNC(name, function) \
935 wait_queue_t name = { \
936 .private = current, \
938 .task_list = LIST_HEAD_INIT((name).task_list), \
941 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
943 #define DEFINE_WAIT_BIT(name, word, bit) \
944 struct wait_bit_queue name = { \
945 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
947 .private = current, \
948 .func = wake_bit_function, \
950 LIST_HEAD_INIT((name).wait.task_list), \
954 #define init_wait(wait) \
956 (wait)->private = current; \
957 (wait)->func = autoremove_wake_function; \
958 INIT_LIST_HEAD(&(wait)->task_list); \
963 extern int bit_wait(struct wait_bit_key *);
964 extern int bit_wait_io(struct wait_bit_key *);
965 extern int bit_wait_timeout(struct wait_bit_key *);
966 extern int bit_wait_io_timeout(struct wait_bit_key *);
969 * wait_on_bit - wait for a bit to be cleared
970 * @word: the word being waited on, a kernel virtual address
971 * @bit: the bit of the word being waited on
972 * @mode: the task state to sleep in
974 * There is a standard hashed waitqueue table for generic use. This
975 * is the part of the hashtable's accessor API that waits on a bit.
976 * For instance, if one were to have waiters on a bitflag, one would
977 * call wait_on_bit() in threads waiting for the bit to clear.
978 * One uses wait_on_bit() where one is waiting for the bit to clear,
979 * but has no intention of setting it.
980 * Returned value will be zero if the bit was cleared, or non-zero
981 * if the process received a signal and the mode permitted wakeup
985 wait_on_bit(unsigned long *word, int bit, unsigned mode)
988 if (!test_bit(bit, word))
990 return out_of_line_wait_on_bit(word, bit,
996 * wait_on_bit_io - wait for a bit to be cleared
997 * @word: the word being waited on, a kernel virtual address
998 * @bit: the bit of the word being waited on
999 * @mode: the task state to sleep in
1001 * Use the standard hashed waitqueue table to wait for a bit
1002 * to be cleared. This is similar to wait_on_bit(), but calls
1003 * io_schedule() instead of schedule() for the actual waiting.
1005 * Returned value will be zero if the bit was cleared, or non-zero
1006 * if the process received a signal and the mode permitted wakeup
1010 wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
1013 if (!test_bit(bit, word))
1015 return out_of_line_wait_on_bit(word, bit,
1021 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
1022 * @word: the word being waited on, a kernel virtual address
1023 * @bit: the bit of the word being waited on
1024 * @mode: the task state to sleep in
1025 * @timeout: timeout, in jiffies
1027 * Use the standard hashed waitqueue table to wait for a bit
1028 * to be cleared. This is similar to wait_on_bit(), except also takes a
1029 * timeout parameter.
1031 * Returned value will be zero if the bit was cleared before the
1032 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1033 * received a signal and the mode permitted wakeup on that signal.
1036 wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
1037 unsigned long timeout)
1040 if (!test_bit(bit, word))
1042 return out_of_line_wait_on_bit_timeout(word, bit,
1048 * wait_on_bit_action - wait for a bit to be cleared
1049 * @word: the word being waited on, a kernel virtual address
1050 * @bit: the bit of the word being waited on
1051 * @action: the function used to sleep, which may take special actions
1052 * @mode: the task state to sleep in
1054 * Use the standard hashed waitqueue table to wait for a bit
1055 * to be cleared, and allow the waiting action to be specified.
1056 * This is like wait_on_bit() but allows fine control of how the waiting
1059 * Returned value will be zero if the bit was cleared, or non-zero
1060 * if the process received a signal and the mode permitted wakeup
1064 wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
1068 if (!test_bit(bit, word))
1070 return out_of_line_wait_on_bit(word, bit, action, mode);
1074 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1075 * @word: the word being waited on, a kernel virtual address
1076 * @bit: the bit of the word being waited on
1077 * @mode: the task state to sleep in
1079 * There is a standard hashed waitqueue table for generic use. This
1080 * is the part of the hashtable's accessor API that waits on a bit
1081 * when one intends to set it, for instance, trying to lock bitflags.
1082 * For instance, if one were to have waiters trying to set bitflag
1083 * and waiting for it to clear before setting it, one would call
1084 * wait_on_bit() in threads waiting to be able to set the bit.
1085 * One uses wait_on_bit_lock() where one is waiting for the bit to
1086 * clear with the intention of setting it, and when done, clearing it.
1088 * Returns zero if the bit was (eventually) found to be clear and was
1089 * set. Returns non-zero if a signal was delivered to the process and
1090 * the @mode allows that signal to wake the process.
1093 wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
1096 if (!test_and_set_bit(bit, word))
1098 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1102 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1103 * @word: the word being waited on, a kernel virtual address
1104 * @bit: the bit of the word being waited on
1105 * @mode: the task state to sleep in
1107 * Use the standard hashed waitqueue table to wait for a bit
1108 * to be cleared and then to atomically set it. This is similar
1109 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1110 * for the actual waiting.
1112 * Returns zero if the bit was (eventually) found to be clear and was
1113 * set. Returns non-zero if a signal was delivered to the process and
1114 * the @mode allows that signal to wake the process.
1117 wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
1120 if (!test_and_set_bit(bit, word))
1122 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1126 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1127 * @word: the word being waited on, a kernel virtual address
1128 * @bit: the bit of the word being waited on
1129 * @action: the function used to sleep, which may take special actions
1130 * @mode: the task state to sleep in
1132 * Use the standard hashed waitqueue table to wait for a bit
1133 * to be cleared and then to set it, and allow the waiting action
1135 * This is like wait_on_bit() but allows fine control of how the waiting
1138 * Returns zero if the bit was (eventually) found to be clear and was
1139 * set. Returns non-zero if a signal was delivered to the process and
1140 * the @mode allows that signal to wake the process.
1143 wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
1147 if (!test_and_set_bit(bit, word))
1149 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1153 * wait_on_atomic_t - Wait for an atomic_t to become 0
1154 * @val: The atomic value being waited on, a kernel virtual address
1155 * @action: the function used to sleep, which may take special actions
1156 * @mode: the task state to sleep in
1158 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1159 * the purpose of getting a waitqueue, but we set the key to a bit number
1160 * outside of the target 'word'.
1163 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1166 if (atomic_read(val) == 0)
1168 return out_of_line_wait_on_atomic_t(val, action, mode);
1171 #endif /* _LINUX_WAIT_H */