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);
18 #define WQ_FLAG_EXCLUSIVE 0x01
20 wait_queue_func_t func;
21 struct list_head task_list;
27 #define WAIT_ATOMIC_T_BIT_NR -1
28 unsigned long private;
31 struct wait_bit_queue {
32 struct wait_bit_key key;
36 struct __wait_queue_head {
38 struct list_head task_list;
40 typedef struct __wait_queue_head wait_queue_head_t;
45 * Macros for declaration and initialisaton of the datatypes
48 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
50 .func = default_wake_function, \
51 .task_list = { NULL, NULL } }
53 #define DECLARE_WAITQUEUE(name, tsk) \
54 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
56 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
57 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
58 .task_list = { &(name).task_list, &(name).task_list } }
60 #define DECLARE_WAIT_QUEUE_HEAD(name) \
61 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
63 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
64 { .flags = word, .bit_nr = bit, }
66 #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
67 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
69 extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
71 #define init_waitqueue_head(q) \
73 static struct lock_class_key __key; \
75 __init_waitqueue_head((q), #q, &__key); \
79 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
80 ({ init_waitqueue_head(&name); name; })
81 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
82 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
84 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
87 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
91 q->func = default_wake_function;
95 init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
102 static inline int waitqueue_active(wait_queue_head_t *q)
104 return !list_empty(&q->task_list);
107 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
108 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
109 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
111 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
113 list_add(&new->task_list, &head->task_list);
117 * Used for wake-one threads:
120 __add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
122 wait->flags |= WQ_FLAG_EXCLUSIVE;
123 __add_wait_queue(q, wait);
126 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
129 list_add_tail(&new->task_list, &head->task_list);
133 __add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
135 wait->flags |= WQ_FLAG_EXCLUSIVE;
136 __add_wait_queue_tail(q, wait);
140 __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
142 list_del(&old->task_list);
145 typedef int wait_bit_action_f(struct wait_bit_key *);
146 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
147 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
148 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
149 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
150 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
151 void __wake_up_bit(wait_queue_head_t *, void *, int);
152 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
153 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
154 void wake_up_bit(void *, int);
155 void wake_up_atomic_t(atomic_t *);
156 int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
157 int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
158 int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
159 wait_queue_head_t *bit_waitqueue(void *, int);
161 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
162 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
163 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
164 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
165 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
167 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
168 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
169 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
170 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
173 * Wakeup macros to be used to report events to the targets.
175 #define wake_up_poll(x, m) \
176 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
177 #define wake_up_locked_poll(x, m) \
178 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
179 #define wake_up_interruptible_poll(x, m) \
180 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
181 #define wake_up_interruptible_sync_poll(x, m) \
182 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
184 #define ___wait_cond_timeout(condition) \
186 bool __cond = (condition); \
187 if (__cond && !__ret) \
192 #define ___wait_is_interruptible(state) \
193 (!__builtin_constant_p(state) || \
194 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
197 * The below macro ___wait_event() has an explicit shadow of the __ret
198 * variable when used from the wait_event_*() macros.
200 * This is so that both can use the ___wait_cond_timeout() construct
201 * to wrap the condition.
203 * The type inconsistency of the wait_event_*() __ret variable is also
204 * on purpose; we use long where we can return timeout values and int
208 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
211 wait_queue_t __wait; \
212 long __ret = ret; /* explicit shadow */ \
214 INIT_LIST_HEAD(&__wait.task_list); \
216 __wait.flags = WQ_FLAG_EXCLUSIVE; \
221 long __int = prepare_to_wait_event(&wq, &__wait, state);\
226 if (___wait_is_interruptible(state) && __int) { \
229 abort_exclusive_wait(&wq, &__wait, \
238 finish_wait(&wq, &__wait); \
242 #define __wait_event(wq, condition) \
243 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
247 * wait_event - sleep until a condition gets true
248 * @wq: the waitqueue to wait on
249 * @condition: a C expression for the event to wait for
251 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
252 * @condition evaluates to true. The @condition is checked each time
253 * the waitqueue @wq is woken up.
255 * wake_up() has to be called after changing any variable that could
256 * change the result of the wait condition.
258 #define wait_event(wq, condition) \
262 __wait_event(wq, condition); \
265 #define __wait_event_timeout(wq, condition, timeout) \
266 ___wait_event(wq, ___wait_cond_timeout(condition), \
267 TASK_UNINTERRUPTIBLE, 0, timeout, \
268 __ret = schedule_timeout(__ret))
271 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
272 * @wq: the waitqueue to wait on
273 * @condition: a C expression for the event to wait for
274 * @timeout: timeout, in jiffies
276 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
277 * @condition evaluates to true. The @condition is checked each time
278 * the waitqueue @wq is woken up.
280 * wake_up() has to be called after changing any variable that could
281 * change the result of the wait condition.
284 * 0 if the @condition evaluated to %false after the @timeout elapsed,
285 * 1 if the @condition evaluated to %true after the @timeout elapsed,
286 * or the remaining jiffies (at least 1) if the @condition evaluated
287 * to %true before the @timeout elapsed.
289 #define wait_event_timeout(wq, condition, timeout) \
291 long __ret = timeout; \
292 if (!___wait_cond_timeout(condition)) \
293 __ret = __wait_event_timeout(wq, condition, timeout); \
297 #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
298 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
299 cmd1; schedule(); cmd2)
302 * wait_event_cmd - sleep until a condition gets true
303 * @wq: the waitqueue to wait on
304 * @condition: a C expression for the event to wait for
305 * @cmd1: the command will be executed before sleep
306 * @cmd2: the command will be executed after sleep
308 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
309 * @condition evaluates to true. The @condition is checked each time
310 * the waitqueue @wq is woken up.
312 * wake_up() has to be called after changing any variable that could
313 * change the result of the wait condition.
315 #define wait_event_cmd(wq, condition, cmd1, cmd2) \
319 __wait_event_cmd(wq, condition, cmd1, cmd2); \
322 #define __wait_event_interruptible(wq, condition) \
323 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
327 * wait_event_interruptible - sleep until a condition gets true
328 * @wq: the waitqueue to wait on
329 * @condition: a C expression for the event to wait for
331 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
332 * @condition evaluates to true or a signal is received.
333 * The @condition is checked each time the waitqueue @wq is woken up.
335 * wake_up() has to be called after changing any variable that could
336 * change the result of the wait condition.
338 * The function will return -ERESTARTSYS if it was interrupted by a
339 * signal and 0 if @condition evaluated to true.
341 #define wait_event_interruptible(wq, condition) \
345 __ret = __wait_event_interruptible(wq, condition); \
349 #define __wait_event_interruptible_timeout(wq, condition, timeout) \
350 ___wait_event(wq, ___wait_cond_timeout(condition), \
351 TASK_INTERRUPTIBLE, 0, timeout, \
352 __ret = schedule_timeout(__ret))
355 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
356 * @wq: the waitqueue to wait on
357 * @condition: a C expression for the event to wait for
358 * @timeout: timeout, in jiffies
360 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
361 * @condition evaluates to true or a signal is received.
362 * The @condition is checked each time the waitqueue @wq is woken up.
364 * wake_up() has to be called after changing any variable that could
365 * change the result of the wait condition.
368 * 0 if the @condition evaluated to %false after the @timeout elapsed,
369 * 1 if the @condition evaluated to %true after the @timeout elapsed,
370 * the remaining jiffies (at least 1) if the @condition evaluated
371 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
372 * interrupted by a signal.
374 #define wait_event_interruptible_timeout(wq, condition, timeout) \
376 long __ret = timeout; \
377 if (!___wait_cond_timeout(condition)) \
378 __ret = __wait_event_interruptible_timeout(wq, \
379 condition, timeout); \
383 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
386 struct hrtimer_sleeper __t; \
388 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
390 hrtimer_init_sleeper(&__t, current); \
391 if ((timeout).tv64 != KTIME_MAX) \
392 hrtimer_start_range_ns(&__t.timer, timeout, \
393 current->timer_slack_ns, \
396 __ret = ___wait_event(wq, condition, state, 0, 0, \
403 hrtimer_cancel(&__t.timer); \
404 destroy_hrtimer_on_stack(&__t.timer); \
409 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
410 * @wq: the waitqueue to wait on
411 * @condition: a C expression for the event to wait for
412 * @timeout: timeout, as a ktime_t
414 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
415 * @condition evaluates to true or a signal is received.
416 * The @condition is checked each time the waitqueue @wq is woken up.
418 * wake_up() has to be called after changing any variable that could
419 * change the result of the wait condition.
421 * The function returns 0 if @condition became true, or -ETIME if the timeout
424 #define wait_event_hrtimeout(wq, condition, timeout) \
428 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
429 TASK_UNINTERRUPTIBLE); \
434 * wait_event_interruptible_hrtimeout - 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, as a ktime_t
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.
446 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
447 * interrupted by a signal, or -ETIME if the timeout elapsed.
449 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
453 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
454 TASK_INTERRUPTIBLE); \
458 #define __wait_event_interruptible_exclusive(wq, condition) \
459 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
462 #define wait_event_interruptible_exclusive(wq, condition) \
466 __ret = __wait_event_interruptible_exclusive(wq, condition);\
471 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
474 DEFINE_WAIT(__wait); \
476 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
478 if (likely(list_empty(&__wait.task_list))) \
479 __add_wait_queue_tail(&(wq), &__wait); \
480 set_current_state(TASK_INTERRUPTIBLE); \
481 if (signal_pending(current)) { \
482 __ret = -ERESTARTSYS; \
486 spin_unlock_irq(&(wq).lock); \
488 spin_unlock(&(wq).lock); \
491 spin_lock_irq(&(wq).lock); \
493 spin_lock(&(wq).lock); \
494 } while (!(condition)); \
495 __remove_wait_queue(&(wq), &__wait); \
496 __set_current_state(TASK_RUNNING); \
502 * wait_event_interruptible_locked - sleep until a condition gets true
503 * @wq: the waitqueue to wait on
504 * @condition: a C expression for the event to wait for
506 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
507 * @condition evaluates to true or a signal is received.
508 * The @condition is checked each time the waitqueue @wq is woken up.
510 * It must be called with wq.lock being held. This spinlock is
511 * unlocked while sleeping but @condition testing is done while lock
512 * is held and when this macro exits the lock is held.
514 * The lock is locked/unlocked using spin_lock()/spin_unlock()
515 * functions which must match the way they are locked/unlocked outside
518 * wake_up_locked() has to be called after changing any variable that could
519 * change the result of the wait condition.
521 * The function will return -ERESTARTSYS if it was interrupted by a
522 * signal and 0 if @condition evaluated to true.
524 #define wait_event_interruptible_locked(wq, condition) \
526 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
529 * wait_event_interruptible_locked_irq - sleep until a condition gets true
530 * @wq: the waitqueue to wait on
531 * @condition: a C expression for the event to wait for
533 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
534 * @condition evaluates to true or a signal is received.
535 * The @condition is checked each time the waitqueue @wq is woken up.
537 * It must be called with wq.lock being held. This spinlock is
538 * unlocked while sleeping but @condition testing is done while lock
539 * is held and when this macro exits the lock is held.
541 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
542 * functions which must match the way they are locked/unlocked outside
545 * wake_up_locked() has to be called after changing any variable that could
546 * change the result of the wait condition.
548 * The function will return -ERESTARTSYS if it was interrupted by a
549 * signal and 0 if @condition evaluated to true.
551 #define wait_event_interruptible_locked_irq(wq, condition) \
553 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
556 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
557 * @wq: the waitqueue to wait on
558 * @condition: a C expression for the event to wait for
560 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
561 * @condition evaluates to true or a signal is received.
562 * The @condition is checked each time the waitqueue @wq is woken up.
564 * It must be called with wq.lock being held. This spinlock is
565 * unlocked while sleeping but @condition testing is done while lock
566 * is held and when this macro exits the lock is held.
568 * The lock is locked/unlocked using spin_lock()/spin_unlock()
569 * functions which must match the way they are locked/unlocked outside
572 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
573 * set thus when other process waits process on the list if this
574 * process is awaken further processes are not considered.
576 * wake_up_locked() has to be called after changing any variable that could
577 * change the result of the wait condition.
579 * The function will return -ERESTARTSYS if it was interrupted by a
580 * signal and 0 if @condition evaluated to true.
582 #define wait_event_interruptible_exclusive_locked(wq, condition) \
584 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
587 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
588 * @wq: the waitqueue to wait on
589 * @condition: a C expression for the event to wait for
591 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
592 * @condition evaluates to true or a signal is received.
593 * The @condition is checked each time the waitqueue @wq is woken up.
595 * It must be called with wq.lock being held. This spinlock is
596 * unlocked while sleeping but @condition testing is done while lock
597 * is held and when this macro exits the lock is held.
599 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
600 * functions which must match the way they are locked/unlocked outside
603 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
604 * set thus when other process waits process on the list if this
605 * process is awaken further processes are not considered.
607 * wake_up_locked() has to be called after changing any variable that could
608 * change the result of the wait condition.
610 * The function will return -ERESTARTSYS if it was interrupted by a
611 * signal and 0 if @condition evaluated to true.
613 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
615 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
618 #define __wait_event_killable(wq, condition) \
619 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
622 * wait_event_killable - sleep until a condition gets true
623 * @wq: the waitqueue to wait on
624 * @condition: a C expression for the event to wait for
626 * The process is put to sleep (TASK_KILLABLE) until the
627 * @condition evaluates to true or a signal is received.
628 * The @condition is checked each time the waitqueue @wq is woken up.
630 * wake_up() has to be called after changing any variable that could
631 * change the result of the wait condition.
633 * The function will return -ERESTARTSYS if it was interrupted by a
634 * signal and 0 if @condition evaluated to true.
636 #define wait_event_killable(wq, condition) \
640 __ret = __wait_event_killable(wq, condition); \
645 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
646 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
647 spin_unlock_irq(&lock); \
650 spin_lock_irq(&lock))
653 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
654 * condition is checked under the lock. This
655 * is expected to be called with the lock
657 * @wq: the waitqueue to wait on
658 * @condition: a C expression for the event to wait for
659 * @lock: a locked spinlock_t, which will be released before cmd
660 * and schedule() and reacquired afterwards.
661 * @cmd: a command which is invoked outside the critical section before
664 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
665 * @condition evaluates to true. The @condition is checked each time
666 * the waitqueue @wq is woken up.
668 * wake_up() has to be called after changing any variable that could
669 * change the result of the wait condition.
671 * This is supposed to be called while holding the lock. The lock is
672 * dropped before invoking the cmd and going to sleep and is reacquired
675 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
679 __wait_event_lock_irq(wq, condition, lock, cmd); \
683 * wait_event_lock_irq - sleep until a condition gets true. The
684 * condition is checked under the lock. This
685 * is expected to be called with the lock
687 * @wq: the waitqueue to wait on
688 * @condition: a C expression for the event to wait for
689 * @lock: a locked spinlock_t, which will be released before schedule()
690 * and reacquired afterwards.
692 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
693 * @condition evaluates to true. The @condition is checked each time
694 * the waitqueue @wq is woken up.
696 * wake_up() has to be called after changing any variable that could
697 * change the result of the wait condition.
699 * This is supposed to be called while holding the lock. The lock is
700 * dropped before going to sleep and is reacquired afterwards.
702 #define wait_event_lock_irq(wq, condition, lock) \
706 __wait_event_lock_irq(wq, condition, lock, ); \
710 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
711 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
712 spin_unlock_irq(&lock); \
715 spin_lock_irq(&lock))
718 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
719 * The condition is checked under the lock. This is expected to
720 * be called with the lock taken.
721 * @wq: the waitqueue to wait on
722 * @condition: a C expression for the event to wait for
723 * @lock: a locked spinlock_t, which will be released before cmd and
724 * schedule() and reacquired afterwards.
725 * @cmd: a command which is invoked outside the critical section before
728 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
729 * @condition evaluates to true or a signal is received. The @condition is
730 * checked each time the waitqueue @wq is woken up.
732 * wake_up() has to be called after changing any variable that could
733 * change the result of the wait condition.
735 * This is supposed to be called while holding the lock. The lock is
736 * dropped before invoking the cmd and going to sleep and is reacquired
739 * The macro will return -ERESTARTSYS if it was interrupted by a signal
740 * and 0 if @condition evaluated to true.
742 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
746 __ret = __wait_event_interruptible_lock_irq(wq, \
747 condition, lock, cmd); \
752 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
753 * The condition is checked under the lock. This is expected
754 * to be called with the lock taken.
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 schedule()
758 * and reacquired afterwards.
760 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
761 * @condition evaluates to true or signal is received. The @condition is
762 * checked each time the waitqueue @wq is woken up.
764 * wake_up() has to be called after changing any variable that could
765 * change the result of the wait condition.
767 * This is supposed to be called while holding the lock. The lock is
768 * dropped before going to sleep and is reacquired afterwards.
770 * The macro will return -ERESTARTSYS if it was interrupted by a signal
771 * and 0 if @condition evaluated to true.
773 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
777 __ret = __wait_event_interruptible_lock_irq(wq, \
782 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
784 ___wait_event(wq, ___wait_cond_timeout(condition), \
785 TASK_INTERRUPTIBLE, 0, timeout, \
786 spin_unlock_irq(&lock); \
787 __ret = schedule_timeout(__ret); \
788 spin_lock_irq(&lock));
791 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
792 * true or a timeout elapses. The condition is checked under
793 * the lock. This is expected to be called with the lock taken.
794 * @wq: the waitqueue to wait on
795 * @condition: a C expression for the event to wait for
796 * @lock: a locked spinlock_t, which will be released before schedule()
797 * and reacquired afterwards.
798 * @timeout: timeout, in jiffies
800 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
801 * @condition evaluates to true or signal is received. The @condition is
802 * checked each time the waitqueue @wq is woken up.
804 * wake_up() has to be called after changing any variable that could
805 * change the result of the wait condition.
807 * This is supposed to be called while holding the lock. The lock is
808 * dropped before going to sleep and is reacquired afterwards.
810 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
811 * was interrupted by a signal, and the remaining jiffies otherwise
812 * if the condition evaluated to true before the timeout elapsed.
814 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
817 long __ret = timeout; \
818 if (!___wait_cond_timeout(condition)) \
819 __ret = __wait_event_interruptible_lock_irq_timeout( \
820 wq, condition, lock, timeout); \
825 * Waitqueues which are removed from the waitqueue_head at wakeup time
827 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
828 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
829 long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
830 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
831 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
832 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
833 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
835 #define DEFINE_WAIT_FUNC(name, function) \
836 wait_queue_t name = { \
837 .private = current, \
839 .task_list = LIST_HEAD_INIT((name).task_list), \
842 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
844 #define DEFINE_WAIT_BIT(name, word, bit) \
845 struct wait_bit_queue name = { \
846 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
848 .private = current, \
849 .func = wake_bit_function, \
851 LIST_HEAD_INIT((name).wait.task_list), \
855 #define init_wait(wait) \
857 (wait)->private = current; \
858 (wait)->func = autoremove_wake_function; \
859 INIT_LIST_HEAD(&(wait)->task_list); \
864 extern int bit_wait(struct wait_bit_key *);
865 extern int bit_wait_io(struct wait_bit_key *);
868 * wait_on_bit - wait for a bit to be cleared
869 * @word: the word being waited on, a kernel virtual address
870 * @bit: the bit of the word being waited on
871 * @mode: the task state to sleep in
873 * There is a standard hashed waitqueue table for generic use. This
874 * is the part of the hashtable's accessor API that waits on a bit.
875 * For instance, if one were to have waiters on a bitflag, one would
876 * call wait_on_bit() in threads waiting for the bit to clear.
877 * One uses wait_on_bit() where one is waiting for the bit to clear,
878 * but has no intention of setting it.
879 * Returned value will be zero if the bit was cleared, or non-zero
880 * if the process received a signal and the mode permitted wakeup
884 wait_on_bit(void *word, int bit, unsigned mode)
886 if (!test_bit(bit, word))
888 return out_of_line_wait_on_bit(word, bit,
894 * wait_on_bit_io - wait for a bit to be cleared
895 * @word: the word being waited on, a kernel virtual address
896 * @bit: the bit of the word being waited on
897 * @mode: the task state to sleep in
899 * Use the standard hashed waitqueue table to wait for a bit
900 * to be cleared. This is similar to wait_on_bit(), but calls
901 * io_schedule() instead of schedule() for the actual waiting.
903 * Returned value will be zero if the bit was cleared, or non-zero
904 * if the process received a signal and the mode permitted wakeup
908 wait_on_bit_io(void *word, int bit, unsigned mode)
910 if (!test_bit(bit, word))
912 return out_of_line_wait_on_bit(word, bit,
918 * wait_on_bit_action - wait for a bit to be cleared
919 * @word: the word being waited on, a kernel virtual address
920 * @bit: the bit of the word being waited on
921 * @action: the function used to sleep, which may take special actions
922 * @mode: the task state to sleep in
924 * Use the standard hashed waitqueue table to wait for a bit
925 * to be cleared, and allow the waiting action to be specified.
926 * This is like wait_on_bit() but allows fine control of how the waiting
929 * Returned value will be zero if the bit was cleared, or non-zero
930 * if the process received a signal and the mode permitted wakeup
934 wait_on_bit_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
936 if (!test_bit(bit, word))
938 return out_of_line_wait_on_bit(word, bit, action, mode);
942 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
943 * @word: the word being waited on, a kernel virtual address
944 * @bit: the bit of the word being waited on
945 * @mode: the task state to sleep in
947 * There is a standard hashed waitqueue table for generic use. This
948 * is the part of the hashtable's accessor API that waits on a bit
949 * when one intends to set it, for instance, trying to lock bitflags.
950 * For instance, if one were to have waiters trying to set bitflag
951 * and waiting for it to clear before setting it, one would call
952 * wait_on_bit() in threads waiting to be able to set the bit.
953 * One uses wait_on_bit_lock() where one is waiting for the bit to
954 * clear with the intention of setting it, and when done, clearing it.
956 * Returns zero if the bit was (eventually) found to be clear and was
957 * set. Returns non-zero if a signal was delivered to the process and
958 * the @mode allows that signal to wake the process.
961 wait_on_bit_lock(void *word, int bit, unsigned mode)
963 if (!test_and_set_bit(bit, word))
965 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
969 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
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 * Use the standard hashed waitqueue table to wait for a bit
975 * to be cleared and then to atomically set it. This is similar
976 * to wait_on_bit(), but calls io_schedule() instead of schedule()
977 * for the actual waiting.
979 * Returns zero if the bit was (eventually) found to be clear and was
980 * set. Returns non-zero if a signal was delivered to the process and
981 * the @mode allows that signal to wake the process.
984 wait_on_bit_lock_io(void *word, int bit, unsigned mode)
986 if (!test_and_set_bit(bit, word))
988 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
992 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
993 * @word: the word being waited on, a kernel virtual address
994 * @bit: the bit of the word being waited on
995 * @action: the function used to sleep, which may take special actions
996 * @mode: the task state to sleep in
998 * Use the standard hashed waitqueue table to wait for a bit
999 * to be cleared and then to set it, and allow the waiting action
1001 * This is like wait_on_bit() but allows fine control of how the waiting
1004 * Returns zero if the bit was (eventually) found to be clear and was
1005 * set. Returns non-zero if a signal was delivered to the process and
1006 * the @mode allows that signal to wake the process.
1009 wait_on_bit_lock_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1011 if (!test_and_set_bit(bit, word))
1013 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1017 * wait_on_atomic_t - Wait for an atomic_t to become 0
1018 * @val: The atomic value being waited on, a kernel virtual address
1019 * @action: the function used to sleep, which may take special actions
1020 * @mode: the task state to sleep in
1022 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1023 * the purpose of getting a waitqueue, but we set the key to a bit number
1024 * outside of the target 'word'.
1027 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1029 if (atomic_read(val) == 0)
1031 return out_of_line_wait_on_atomic_t(val, action, mode);
1034 #endif /* _LINUX_WAIT_H */