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 timeout;
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_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
158 int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
159 int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
160 wait_queue_head_t *bit_waitqueue(void *, int);
162 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
163 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
164 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
165 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
166 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
168 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
169 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
170 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
171 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
174 * Wakeup macros to be used to report events to the targets.
176 #define wake_up_poll(x, m) \
177 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
178 #define wake_up_locked_poll(x, m) \
179 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
180 #define wake_up_interruptible_poll(x, m) \
181 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
182 #define wake_up_interruptible_sync_poll(x, m) \
183 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
185 #define ___wait_cond_timeout(condition) \
187 bool __cond = (condition); \
188 if (__cond && !__ret) \
193 #define ___wait_is_interruptible(state) \
194 (!__builtin_constant_p(state) || \
195 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
198 * The below macro ___wait_event() has an explicit shadow of the __ret
199 * variable when used from the wait_event_*() macros.
201 * This is so that both can use the ___wait_cond_timeout() construct
202 * to wrap the condition.
204 * The type inconsistency of the wait_event_*() __ret variable is also
205 * on purpose; we use long where we can return timeout values and int
209 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
212 wait_queue_t __wait; \
213 long __ret = ret; /* explicit shadow */ \
215 INIT_LIST_HEAD(&__wait.task_list); \
217 __wait.flags = WQ_FLAG_EXCLUSIVE; \
222 long __int = prepare_to_wait_event(&wq, &__wait, state);\
227 if (___wait_is_interruptible(state) && __int) { \
230 abort_exclusive_wait(&wq, &__wait, \
239 finish_wait(&wq, &__wait); \
243 #define __wait_event(wq, condition) \
244 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
248 * wait_event - sleep until a condition gets true
249 * @wq: the waitqueue to wait on
250 * @condition: a C expression for the event to wait for
252 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
253 * @condition evaluates to true. The @condition is checked each time
254 * the waitqueue @wq is woken up.
256 * wake_up() has to be called after changing any variable that could
257 * change the result of the wait condition.
259 #define wait_event(wq, condition) \
263 __wait_event(wq, condition); \
266 #define __wait_event_timeout(wq, condition, timeout) \
267 ___wait_event(wq, ___wait_cond_timeout(condition), \
268 TASK_UNINTERRUPTIBLE, 0, timeout, \
269 __ret = schedule_timeout(__ret))
272 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
273 * @wq: the waitqueue to wait on
274 * @condition: a C expression for the event to wait for
275 * @timeout: timeout, in jiffies
277 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
278 * @condition evaluates to true. The @condition is checked each time
279 * the waitqueue @wq is woken up.
281 * wake_up() has to be called after changing any variable that could
282 * change the result of the wait condition.
284 * The function returns 0 if the @timeout elapsed, or the remaining
285 * jiffies (at least 1) if the @condition evaluated to %true before
286 * the @timeout elapsed.
288 #define wait_event_timeout(wq, condition, timeout) \
290 long __ret = timeout; \
291 if (!___wait_cond_timeout(condition)) \
292 __ret = __wait_event_timeout(wq, condition, timeout); \
296 #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
297 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
298 cmd1; schedule(); cmd2)
301 * wait_event_cmd - sleep until a condition gets true
302 * @wq: the waitqueue to wait on
303 * @condition: a C expression for the event to wait for
304 * @cmd1: the command will be executed before sleep
305 * @cmd2: the command will be executed after sleep
307 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
308 * @condition evaluates to true. The @condition is checked each time
309 * the waitqueue @wq is woken up.
311 * wake_up() has to be called after changing any variable that could
312 * change the result of the wait condition.
314 #define wait_event_cmd(wq, condition, cmd1, cmd2) \
318 __wait_event_cmd(wq, condition, cmd1, cmd2); \
321 #define __wait_event_interruptible(wq, condition) \
322 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
326 * wait_event_interruptible - sleep until a condition gets true
327 * @wq: the waitqueue to wait on
328 * @condition: a C expression for the event to wait for
330 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
331 * @condition evaluates to true or a signal is received.
332 * The @condition is checked each time the waitqueue @wq is woken up.
334 * wake_up() has to be called after changing any variable that could
335 * change the result of the wait condition.
337 * The function will return -ERESTARTSYS if it was interrupted by a
338 * signal and 0 if @condition evaluated to true.
340 #define wait_event_interruptible(wq, condition) \
344 __ret = __wait_event_interruptible(wq, condition); \
348 #define __wait_event_interruptible_timeout(wq, condition, timeout) \
349 ___wait_event(wq, ___wait_cond_timeout(condition), \
350 TASK_INTERRUPTIBLE, 0, timeout, \
351 __ret = schedule_timeout(__ret))
354 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
355 * @wq: the waitqueue to wait on
356 * @condition: a C expression for the event to wait for
357 * @timeout: timeout, in jiffies
359 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
360 * @condition evaluates to true or a signal is received.
361 * The @condition is checked each time the waitqueue @wq is woken up.
363 * wake_up() has to be called after changing any variable that could
364 * change the result of the wait condition.
367 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
368 * a signal, or the remaining jiffies (at least 1) if the @condition
369 * evaluated to %true before the @timeout elapsed.
371 #define wait_event_interruptible_timeout(wq, condition, timeout) \
373 long __ret = timeout; \
374 if (!___wait_cond_timeout(condition)) \
375 __ret = __wait_event_interruptible_timeout(wq, \
376 condition, timeout); \
380 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
383 struct hrtimer_sleeper __t; \
385 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
387 hrtimer_init_sleeper(&__t, current); \
388 if ((timeout).tv64 != KTIME_MAX) \
389 hrtimer_start_range_ns(&__t.timer, timeout, \
390 current->timer_slack_ns, \
393 __ret = ___wait_event(wq, condition, state, 0, 0, \
400 hrtimer_cancel(&__t.timer); \
401 destroy_hrtimer_on_stack(&__t.timer); \
406 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
407 * @wq: the waitqueue to wait on
408 * @condition: a C expression for the event to wait for
409 * @timeout: timeout, as a ktime_t
411 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
412 * @condition evaluates to true or a signal is received.
413 * The @condition is checked each time the waitqueue @wq is woken up.
415 * wake_up() has to be called after changing any variable that could
416 * change the result of the wait condition.
418 * The function returns 0 if @condition became true, or -ETIME if the timeout
421 #define wait_event_hrtimeout(wq, condition, timeout) \
425 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
426 TASK_UNINTERRUPTIBLE); \
431 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
432 * @wq: the waitqueue to wait on
433 * @condition: a C expression for the event to wait for
434 * @timeout: timeout, as a ktime_t
436 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
437 * @condition evaluates to true or a signal is received.
438 * The @condition is checked each time the waitqueue @wq is woken up.
440 * wake_up() has to be called after changing any variable that could
441 * change the result of the wait condition.
443 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
444 * interrupted by a signal, or -ETIME if the timeout elapsed.
446 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
450 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
451 TASK_INTERRUPTIBLE); \
455 #define __wait_event_interruptible_exclusive(wq, condition) \
456 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
459 #define wait_event_interruptible_exclusive(wq, condition) \
463 __ret = __wait_event_interruptible_exclusive(wq, condition);\
468 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
471 DEFINE_WAIT(__wait); \
473 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
475 if (likely(list_empty(&__wait.task_list))) \
476 __add_wait_queue_tail(&(wq), &__wait); \
477 set_current_state(TASK_INTERRUPTIBLE); \
478 if (signal_pending(current)) { \
479 __ret = -ERESTARTSYS; \
483 spin_unlock_irq(&(wq).lock); \
485 spin_unlock(&(wq).lock); \
488 spin_lock_irq(&(wq).lock); \
490 spin_lock(&(wq).lock); \
491 } while (!(condition)); \
492 __remove_wait_queue(&(wq), &__wait); \
493 __set_current_state(TASK_RUNNING); \
499 * wait_event_interruptible_locked - sleep until a condition gets true
500 * @wq: the waitqueue to wait on
501 * @condition: a C expression for the event to wait for
503 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
504 * @condition evaluates to true or a signal is received.
505 * The @condition is checked each time the waitqueue @wq is woken up.
507 * It must be called with wq.lock being held. This spinlock is
508 * unlocked while sleeping but @condition testing is done while lock
509 * is held and when this macro exits the lock is held.
511 * The lock is locked/unlocked using spin_lock()/spin_unlock()
512 * functions which must match the way they are locked/unlocked outside
515 * wake_up_locked() has to be called after changing any variable that could
516 * change the result of the wait condition.
518 * The function will return -ERESTARTSYS if it was interrupted by a
519 * signal and 0 if @condition evaluated to true.
521 #define wait_event_interruptible_locked(wq, condition) \
523 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
526 * wait_event_interruptible_locked_irq - sleep until a condition gets true
527 * @wq: the waitqueue to wait on
528 * @condition: a C expression for the event to wait for
530 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
531 * @condition evaluates to true or a signal is received.
532 * The @condition is checked each time the waitqueue @wq is woken up.
534 * It must be called with wq.lock being held. This spinlock is
535 * unlocked while sleeping but @condition testing is done while lock
536 * is held and when this macro exits the lock is held.
538 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
539 * functions which must match the way they are locked/unlocked outside
542 * wake_up_locked() has to be called after changing any variable that could
543 * change the result of the wait condition.
545 * The function will return -ERESTARTSYS if it was interrupted by a
546 * signal and 0 if @condition evaluated to true.
548 #define wait_event_interruptible_locked_irq(wq, condition) \
550 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
553 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
554 * @wq: the waitqueue to wait on
555 * @condition: a C expression for the event to wait for
557 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
558 * @condition evaluates to true or a signal is received.
559 * The @condition is checked each time the waitqueue @wq is woken up.
561 * It must be called with wq.lock being held. This spinlock is
562 * unlocked while sleeping but @condition testing is done while lock
563 * is held and when this macro exits the lock is held.
565 * The lock is locked/unlocked using spin_lock()/spin_unlock()
566 * functions which must match the way they are locked/unlocked outside
569 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
570 * set thus when other process waits process on the list if this
571 * process is awaken further processes are not considered.
573 * wake_up_locked() has to be called after changing any variable that could
574 * change the result of the wait condition.
576 * The function will return -ERESTARTSYS if it was interrupted by a
577 * signal and 0 if @condition evaluated to true.
579 #define wait_event_interruptible_exclusive_locked(wq, condition) \
581 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
584 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
585 * @wq: the waitqueue to wait on
586 * @condition: a C expression for the event to wait for
588 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
589 * @condition evaluates to true or a signal is received.
590 * The @condition is checked each time the waitqueue @wq is woken up.
592 * It must be called with wq.lock being held. This spinlock is
593 * unlocked while sleeping but @condition testing is done while lock
594 * is held and when this macro exits the lock is held.
596 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
597 * functions which must match the way they are locked/unlocked outside
600 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
601 * set thus when other process waits process on the list if this
602 * process is awaken further processes are not considered.
604 * wake_up_locked() has to be called after changing any variable that could
605 * change the result of the wait condition.
607 * The function will return -ERESTARTSYS if it was interrupted by a
608 * signal and 0 if @condition evaluated to true.
610 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
612 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
615 #define __wait_event_killable(wq, condition) \
616 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
619 * wait_event_killable - sleep until a condition gets true
620 * @wq: the waitqueue to wait on
621 * @condition: a C expression for the event to wait for
623 * The process is put to sleep (TASK_KILLABLE) until the
624 * @condition evaluates to true or a signal is received.
625 * The @condition is checked each time the waitqueue @wq is woken up.
627 * wake_up() has to be called after changing any variable that could
628 * change the result of the wait condition.
630 * The function will return -ERESTARTSYS if it was interrupted by a
631 * signal and 0 if @condition evaluated to true.
633 #define wait_event_killable(wq, condition) \
637 __ret = __wait_event_killable(wq, condition); \
642 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
643 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
644 spin_unlock_irq(&lock); \
647 spin_lock_irq(&lock))
650 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
651 * condition is checked under the lock. This
652 * is expected to be called with the lock
654 * @wq: the waitqueue to wait on
655 * @condition: a C expression for the event to wait for
656 * @lock: a locked spinlock_t, which will be released before cmd
657 * and schedule() and reacquired afterwards.
658 * @cmd: a command which is invoked outside the critical section before
661 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
662 * @condition evaluates to true. The @condition is checked each time
663 * the waitqueue @wq is woken up.
665 * wake_up() has to be called after changing any variable that could
666 * change the result of the wait condition.
668 * This is supposed to be called while holding the lock. The lock is
669 * dropped before invoking the cmd and going to sleep and is reacquired
672 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
676 __wait_event_lock_irq(wq, condition, lock, cmd); \
680 * wait_event_lock_irq - sleep until a condition gets true. The
681 * condition is checked under the lock. This
682 * is expected to be called with the lock
684 * @wq: the waitqueue to wait on
685 * @condition: a C expression for the event to wait for
686 * @lock: a locked spinlock_t, which will be released before schedule()
687 * and reacquired afterwards.
689 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
690 * @condition evaluates to true. The @condition is checked each time
691 * the waitqueue @wq is woken up.
693 * wake_up() has to be called after changing any variable that could
694 * change the result of the wait condition.
696 * This is supposed to be called while holding the lock. The lock is
697 * dropped before going to sleep and is reacquired afterwards.
699 #define wait_event_lock_irq(wq, condition, lock) \
703 __wait_event_lock_irq(wq, condition, lock, ); \
707 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
708 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
709 spin_unlock_irq(&lock); \
712 spin_lock_irq(&lock))
715 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
716 * The condition is checked under the lock. This is expected to
717 * be called with the lock taken.
718 * @wq: the waitqueue to wait on
719 * @condition: a C expression for the event to wait for
720 * @lock: a locked spinlock_t, which will be released before cmd and
721 * schedule() and reacquired afterwards.
722 * @cmd: a command which is invoked outside the critical section before
725 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
726 * @condition evaluates to true or a signal is received. The @condition is
727 * checked each time the waitqueue @wq is woken up.
729 * wake_up() has to be called after changing any variable that could
730 * change the result of the wait condition.
732 * This is supposed to be called while holding the lock. The lock is
733 * dropped before invoking the cmd and going to sleep and is reacquired
736 * The macro will return -ERESTARTSYS if it was interrupted by a signal
737 * and 0 if @condition evaluated to true.
739 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
743 __ret = __wait_event_interruptible_lock_irq(wq, \
744 condition, lock, cmd); \
749 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
750 * The condition is checked under the lock. This is expected
751 * to be called with the lock taken.
752 * @wq: the waitqueue to wait on
753 * @condition: a C expression for the event to wait for
754 * @lock: a locked spinlock_t, which will be released before schedule()
755 * and reacquired afterwards.
757 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
758 * @condition evaluates to true or signal is received. The @condition is
759 * checked each time the waitqueue @wq is woken up.
761 * wake_up() has to be called after changing any variable that could
762 * change the result of the wait condition.
764 * This is supposed to be called while holding the lock. The lock is
765 * dropped before going to sleep and is reacquired afterwards.
767 * The macro will return -ERESTARTSYS if it was interrupted by a signal
768 * and 0 if @condition evaluated to true.
770 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
774 __ret = __wait_event_interruptible_lock_irq(wq, \
779 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
781 ___wait_event(wq, ___wait_cond_timeout(condition), \
782 TASK_INTERRUPTIBLE, 0, timeout, \
783 spin_unlock_irq(&lock); \
784 __ret = schedule_timeout(__ret); \
785 spin_lock_irq(&lock));
788 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
789 * true or a timeout elapses. The condition is checked under
790 * the lock. This is expected to be called with the lock taken.
791 * @wq: the waitqueue to wait on
792 * @condition: a C expression for the event to wait for
793 * @lock: a locked spinlock_t, which will be released before schedule()
794 * and reacquired afterwards.
795 * @timeout: timeout, in jiffies
797 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
798 * @condition evaluates to true or signal is received. The @condition is
799 * checked each time the waitqueue @wq is woken up.
801 * wake_up() has to be called after changing any variable that could
802 * change the result of the wait condition.
804 * This is supposed to be called while holding the lock. The lock is
805 * dropped before going to sleep and is reacquired afterwards.
807 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
808 * was interrupted by a signal, and the remaining jiffies otherwise
809 * if the condition evaluated to true before the timeout elapsed.
811 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
814 long __ret = timeout; \
815 if (!___wait_cond_timeout(condition)) \
816 __ret = __wait_event_interruptible_lock_irq_timeout( \
817 wq, condition, lock, timeout); \
822 * Waitqueues which are removed from the waitqueue_head at wakeup time
824 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
825 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
826 long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
827 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
828 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
829 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
830 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
832 #define DEFINE_WAIT_FUNC(name, function) \
833 wait_queue_t name = { \
834 .private = current, \
836 .task_list = LIST_HEAD_INIT((name).task_list), \
839 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
841 #define DEFINE_WAIT_BIT(name, word, bit) \
842 struct wait_bit_queue name = { \
843 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
845 .private = current, \
846 .func = wake_bit_function, \
848 LIST_HEAD_INIT((name).wait.task_list), \
852 #define init_wait(wait) \
854 (wait)->private = current; \
855 (wait)->func = autoremove_wake_function; \
856 INIT_LIST_HEAD(&(wait)->task_list); \
861 extern int bit_wait(struct wait_bit_key *);
862 extern int bit_wait_io(struct wait_bit_key *);
863 extern int bit_wait_timeout(struct wait_bit_key *);
864 extern int bit_wait_io_timeout(struct wait_bit_key *);
867 * wait_on_bit - wait for a bit to be cleared
868 * @word: the word being waited on, a kernel virtual address
869 * @bit: the bit of the word being waited on
870 * @mode: the task state to sleep in
872 * There is a standard hashed waitqueue table for generic use. This
873 * is the part of the hashtable's accessor API that waits on a bit.
874 * For instance, if one were to have waiters on a bitflag, one would
875 * call wait_on_bit() in threads waiting for the bit to clear.
876 * One uses wait_on_bit() where one is waiting for the bit to clear,
877 * but has no intention of setting it.
878 * Returned value will be zero if the bit was cleared, or non-zero
879 * if the process received a signal and the mode permitted wakeup
883 wait_on_bit(void *word, int bit, unsigned mode)
885 if (!test_bit(bit, word))
887 return out_of_line_wait_on_bit(word, bit,
893 * wait_on_bit_io - wait for a bit to be cleared
894 * @word: the word being waited on, a kernel virtual address
895 * @bit: the bit of the word being waited on
896 * @mode: the task state to sleep in
898 * Use the standard hashed waitqueue table to wait for a bit
899 * to be cleared. This is similar to wait_on_bit(), but calls
900 * io_schedule() instead of schedule() for the actual waiting.
902 * Returned value will be zero if the bit was cleared, or non-zero
903 * if the process received a signal and the mode permitted wakeup
907 wait_on_bit_io(void *word, int bit, unsigned mode)
909 if (!test_bit(bit, word))
911 return out_of_line_wait_on_bit(word, bit,
917 * wait_on_bit_action - wait for a bit to be cleared
918 * @word: the word being waited on, a kernel virtual address
919 * @bit: the bit of the word being waited on
920 * @action: the function used to sleep, which may take special actions
921 * @mode: the task state to sleep in
923 * Use the standard hashed waitqueue table to wait for a bit
924 * to be cleared, and allow the waiting action to be specified.
925 * This is like wait_on_bit() but allows fine control of how the waiting
928 * Returned value will be zero if the bit was cleared, or non-zero
929 * if the process received a signal and the mode permitted wakeup
933 wait_on_bit_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
935 if (!test_bit(bit, word))
937 return out_of_line_wait_on_bit(word, bit, action, mode);
941 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
942 * @word: the word being waited on, a kernel virtual address
943 * @bit: the bit of the word being waited on
944 * @mode: the task state to sleep in
946 * There is a standard hashed waitqueue table for generic use. This
947 * is the part of the hashtable's accessor API that waits on a bit
948 * when one intends to set it, for instance, trying to lock bitflags.
949 * For instance, if one were to have waiters trying to set bitflag
950 * and waiting for it to clear before setting it, one would call
951 * wait_on_bit() in threads waiting to be able to set the bit.
952 * One uses wait_on_bit_lock() where one is waiting for the bit to
953 * clear with the intention of setting it, and when done, clearing it.
955 * Returns zero if the bit was (eventually) found to be clear and was
956 * set. Returns non-zero if a signal was delivered to the process and
957 * the @mode allows that signal to wake the process.
960 wait_on_bit_lock(void *word, int bit, unsigned mode)
962 if (!test_and_set_bit(bit, word))
964 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
968 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
969 * @word: the word being waited on, a kernel virtual address
970 * @bit: the bit of the word being waited on
971 * @mode: the task state to sleep in
973 * Use the standard hashed waitqueue table to wait for a bit
974 * to be cleared and then to atomically set it. This is similar
975 * to wait_on_bit(), but calls io_schedule() instead of schedule()
976 * for the actual waiting.
978 * Returns zero if the bit was (eventually) found to be clear and was
979 * set. Returns non-zero if a signal was delivered to the process and
980 * the @mode allows that signal to wake the process.
983 wait_on_bit_lock_io(void *word, int bit, unsigned mode)
985 if (!test_and_set_bit(bit, word))
987 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
991 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
992 * @word: the word being waited on, a kernel virtual address
993 * @bit: the bit of the word being waited on
994 * @action: the function used to sleep, which may take special actions
995 * @mode: the task state to sleep in
997 * Use the standard hashed waitqueue table to wait for a bit
998 * to be cleared and then to set it, and allow the waiting action
1000 * This is like wait_on_bit() but allows fine control of how the waiting
1003 * Returns zero if the bit was (eventually) found to be clear and was
1004 * set. Returns non-zero if a signal was delivered to the process and
1005 * the @mode allows that signal to wake the process.
1008 wait_on_bit_lock_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1010 if (!test_and_set_bit(bit, word))
1012 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1016 * wait_on_atomic_t - Wait for an atomic_t to become 0
1017 * @val: The atomic value being waited on, a kernel virtual address
1018 * @action: the function used to sleep, which may take special actions
1019 * @mode: the task state to sleep in
1021 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1022 * the purpose of getting a waitqueue, but we set the key to a bit number
1023 * outside of the target 'word'.
1026 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1028 if (atomic_read(val) == 0)
1030 return out_of_line_wait_on_atomic_t(val, action, mode);
1033 #endif /* _LINUX_WAIT_H */