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>
11 #include <linux/atomic.h>
13 typedef struct __wait_queue wait_queue_t;
14 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
15 int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
17 /* __wait_queue::flags */
18 #define WQ_FLAG_EXCLUSIVE 0x01
19 #define WQ_FLAG_WOKEN 0x02
24 wait_queue_func_t func;
25 struct list_head task_list;
31 #define WAIT_ATOMIC_T_BIT_NR -1
32 unsigned long timeout;
35 struct wait_bit_queue {
36 struct wait_bit_key key;
40 struct __wait_queue_head {
42 struct list_head task_list;
44 typedef struct __wait_queue_head wait_queue_head_t;
49 * Macros for declaration and initialisaton of the datatypes
52 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
54 .func = default_wake_function, \
55 .task_list = { NULL, NULL } }
57 #define DECLARE_WAITQUEUE(name, tsk) \
58 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
60 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
61 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
62 .task_list = { &(name).task_list, &(name).task_list } }
64 #define DECLARE_WAIT_QUEUE_HEAD(name) \
65 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
67 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
68 { .flags = word, .bit_nr = bit, }
70 #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
71 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
73 extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
75 #define init_waitqueue_head(q) \
77 static struct lock_class_key __key; \
79 __init_waitqueue_head((q), #q, &__key); \
83 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
84 ({ init_waitqueue_head(&name); name; })
85 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
86 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
88 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
91 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
95 q->func = default_wake_function;
99 init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
106 static inline int waitqueue_active(wait_queue_head_t *q)
108 return !list_empty(&q->task_list);
111 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
112 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
113 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
115 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
117 list_add(&new->task_list, &head->task_list);
121 * Used for wake-one threads:
124 __add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
126 wait->flags |= WQ_FLAG_EXCLUSIVE;
127 __add_wait_queue(q, wait);
130 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
133 list_add_tail(&new->task_list, &head->task_list);
137 __add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
139 wait->flags |= WQ_FLAG_EXCLUSIVE;
140 __add_wait_queue_tail(q, wait);
144 __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
146 list_del(&old->task_list);
149 typedef int wait_bit_action_f(struct wait_bit_key *);
150 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
151 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
152 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
153 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
154 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
155 void __wake_up_bit(wait_queue_head_t *, void *, int);
156 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
157 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned);
158 void wake_up_bit(void *, int);
159 void wake_up_atomic_t(atomic_t *);
160 int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned);
161 int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long);
162 int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned);
163 int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
164 wait_queue_head_t *bit_waitqueue(void *, int);
166 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
167 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
168 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
169 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
170 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
172 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
173 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
174 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
175 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
178 * Wakeup macros to be used to report events to the targets.
180 #define wake_up_poll(x, m) \
181 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
182 #define wake_up_locked_poll(x, m) \
183 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
184 #define wake_up_interruptible_poll(x, m) \
185 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
186 #define wake_up_interruptible_sync_poll(x, m) \
187 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
189 #define ___wait_cond_timeout(condition) \
191 bool __cond = (condition); \
192 if (__cond && !__ret) \
197 #define ___wait_is_interruptible(state) \
198 (!__builtin_constant_p(state) || \
199 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
202 * The below macro ___wait_event() has an explicit shadow of the __ret
203 * variable when used from the wait_event_*() macros.
205 * This is so that both can use the ___wait_cond_timeout() construct
206 * to wrap the condition.
208 * The type inconsistency of the wait_event_*() __ret variable is also
209 * on purpose; we use long where we can return timeout values and int
213 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
216 wait_queue_t __wait; \
217 long __ret = ret; /* explicit shadow */ \
219 INIT_LIST_HEAD(&__wait.task_list); \
221 __wait.flags = WQ_FLAG_EXCLUSIVE; \
226 long __int = prepare_to_wait_event(&wq, &__wait, state);\
231 if (___wait_is_interruptible(state) && __int) { \
234 abort_exclusive_wait(&wq, &__wait, \
243 finish_wait(&wq, &__wait); \
247 #define __wait_event(wq, condition) \
248 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
252 * wait_event - sleep until a condition gets true
253 * @wq: the waitqueue to wait on
254 * @condition: a C expression for the event to wait for
256 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
257 * @condition evaluates to true. The @condition is checked each time
258 * the waitqueue @wq is woken up.
260 * wake_up() has to be called after changing any variable that could
261 * change the result of the wait condition.
263 #define wait_event(wq, condition) \
268 __wait_event(wq, condition); \
271 #define __io_wait_event(wq, condition) \
272 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
276 * io_wait_event() -- like wait_event() but with io_schedule()
278 #define io_wait_event(wq, condition) \
283 __io_wait_event(wq, condition); \
286 #define __wait_event_freezable(wq, condition) \
287 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
288 schedule(); try_to_freeze())
291 * wait_event - sleep (or freeze) until a condition gets true
292 * @wq: the waitqueue to wait on
293 * @condition: a C expression for the event to wait for
295 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
296 * to system load) until the @condition evaluates to true. The
297 * @condition is checked each time the waitqueue @wq is woken up.
299 * wake_up() has to be called after changing any variable that could
300 * change the result of the wait condition.
302 #define wait_event_freezable(wq, condition) \
307 __ret = __wait_event_freezable(wq, condition); \
311 #define __wait_event_timeout(wq, condition, timeout) \
312 ___wait_event(wq, ___wait_cond_timeout(condition), \
313 TASK_UNINTERRUPTIBLE, 0, timeout, \
314 __ret = schedule_timeout(__ret))
317 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
318 * @wq: the waitqueue to wait on
319 * @condition: a C expression for the event to wait for
320 * @timeout: timeout, in jiffies
322 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
323 * @condition evaluates to true. The @condition is checked each time
324 * the waitqueue @wq is woken up.
326 * wake_up() has to be called after changing any variable that could
327 * change the result of the wait condition.
330 * 0 if the @condition evaluated to %false after the @timeout elapsed,
331 * 1 if the @condition evaluated to %true after the @timeout elapsed,
332 * or the remaining jiffies (at least 1) if the @condition evaluated
333 * to %true before the @timeout elapsed.
335 #define wait_event_timeout(wq, condition, timeout) \
337 long __ret = timeout; \
339 if (!___wait_cond_timeout(condition)) \
340 __ret = __wait_event_timeout(wq, condition, timeout); \
344 #define __wait_event_freezable_timeout(wq, condition, timeout) \
345 ___wait_event(wq, ___wait_cond_timeout(condition), \
346 TASK_INTERRUPTIBLE, 0, timeout, \
347 __ret = schedule_timeout(__ret); try_to_freeze())
350 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
351 * increasing load and is freezable.
353 #define wait_event_freezable_timeout(wq, condition, timeout) \
355 long __ret = timeout; \
357 if (!___wait_cond_timeout(condition)) \
358 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
362 #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
363 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
364 cmd1; schedule(); cmd2)
367 * wait_event_cmd - sleep until a condition gets true
368 * @wq: the waitqueue to wait on
369 * @condition: a C expression for the event to wait for
370 * @cmd1: the command will be executed before sleep
371 * @cmd2: the command will be executed after sleep
373 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
374 * @condition evaluates to true. The @condition is checked each time
375 * the waitqueue @wq is woken up.
377 * wake_up() has to be called after changing any variable that could
378 * change the result of the wait condition.
380 #define wait_event_cmd(wq, condition, cmd1, cmd2) \
384 __wait_event_cmd(wq, condition, cmd1, cmd2); \
387 #define __wait_event_interruptible(wq, condition) \
388 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
392 * wait_event_interruptible - sleep until a condition gets true
393 * @wq: the waitqueue to wait on
394 * @condition: a C expression for the event to wait for
396 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
397 * @condition evaluates to true or a signal is received.
398 * The @condition is checked each time the waitqueue @wq is woken up.
400 * wake_up() has to be called after changing any variable that could
401 * change the result of the wait condition.
403 * The function will return -ERESTARTSYS if it was interrupted by a
404 * signal and 0 if @condition evaluated to true.
406 #define wait_event_interruptible(wq, condition) \
411 __ret = __wait_event_interruptible(wq, condition); \
415 #define __wait_event_interruptible_timeout(wq, condition, timeout) \
416 ___wait_event(wq, ___wait_cond_timeout(condition), \
417 TASK_INTERRUPTIBLE, 0, timeout, \
418 __ret = schedule_timeout(__ret))
421 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
422 * @wq: the waitqueue to wait on
423 * @condition: a C expression for the event to wait for
424 * @timeout: timeout, in jiffies
426 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
427 * @condition evaluates to true or a signal is received.
428 * The @condition is checked each time the waitqueue @wq is woken up.
430 * wake_up() has to be called after changing any variable that could
431 * change the result of the wait condition.
434 * 0 if the @condition evaluated to %false after the @timeout elapsed,
435 * 1 if the @condition evaluated to %true after the @timeout elapsed,
436 * the remaining jiffies (at least 1) if the @condition evaluated
437 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
438 * interrupted by a signal.
440 #define wait_event_interruptible_timeout(wq, condition, timeout) \
442 long __ret = timeout; \
444 if (!___wait_cond_timeout(condition)) \
445 __ret = __wait_event_interruptible_timeout(wq, \
446 condition, timeout); \
450 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
453 struct hrtimer_sleeper __t; \
455 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
457 hrtimer_init_sleeper(&__t, current); \
458 if ((timeout).tv64 != KTIME_MAX) \
459 hrtimer_start_range_ns(&__t.timer, timeout, \
460 current->timer_slack_ns, \
463 __ret = ___wait_event(wq, condition, state, 0, 0, \
470 hrtimer_cancel(&__t.timer); \
471 destroy_hrtimer_on_stack(&__t.timer); \
476 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
477 * @wq: the waitqueue to wait on
478 * @condition: a C expression for the event to wait for
479 * @timeout: timeout, as a ktime_t
481 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
482 * @condition evaluates to true or a signal is received.
483 * The @condition is checked each time the waitqueue @wq is woken up.
485 * wake_up() has to be called after changing any variable that could
486 * change the result of the wait condition.
488 * The function returns 0 if @condition became true, or -ETIME if the timeout
491 #define wait_event_hrtimeout(wq, condition, timeout) \
496 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
497 TASK_UNINTERRUPTIBLE); \
502 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
503 * @wq: the waitqueue to wait on
504 * @condition: a C expression for the event to wait for
505 * @timeout: timeout, as a ktime_t
507 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
508 * @condition evaluates to true or a signal is received.
509 * The @condition is checked each time the waitqueue @wq is woken up.
511 * wake_up() has to be called after changing any variable that could
512 * change the result of the wait condition.
514 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
515 * interrupted by a signal, or -ETIME if the timeout elapsed.
517 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
522 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
523 TASK_INTERRUPTIBLE); \
527 #define __wait_event_interruptible_exclusive(wq, condition) \
528 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
531 #define wait_event_interruptible_exclusive(wq, condition) \
536 __ret = __wait_event_interruptible_exclusive(wq, condition);\
541 #define __wait_event_freezable_exclusive(wq, condition) \
542 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
543 schedule(); try_to_freeze())
545 #define wait_event_freezable_exclusive(wq, condition) \
550 __ret = __wait_event_freezable_exclusive(wq, condition);\
555 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
558 DEFINE_WAIT(__wait); \
560 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
562 if (likely(list_empty(&__wait.task_list))) \
563 __add_wait_queue_tail(&(wq), &__wait); \
564 set_current_state(TASK_INTERRUPTIBLE); \
565 if (signal_pending(current)) { \
566 __ret = -ERESTARTSYS; \
570 spin_unlock_irq(&(wq).lock); \
572 spin_unlock(&(wq).lock); \
575 spin_lock_irq(&(wq).lock); \
577 spin_lock(&(wq).lock); \
578 } while (!(condition)); \
579 __remove_wait_queue(&(wq), &__wait); \
580 __set_current_state(TASK_RUNNING); \
586 * wait_event_interruptible_locked - sleep until a condition gets true
587 * @wq: the waitqueue to wait on
588 * @condition: a C expression for the event to wait for
590 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
591 * @condition evaluates to true or a signal is received.
592 * The @condition is checked each time the waitqueue @wq is woken up.
594 * It must be called with wq.lock being held. This spinlock is
595 * unlocked while sleeping but @condition testing is done while lock
596 * is held and when this macro exits the lock is held.
598 * The lock is locked/unlocked using spin_lock()/spin_unlock()
599 * functions which must match the way they are locked/unlocked outside
602 * wake_up_locked() has to be called after changing any variable that could
603 * change the result of the wait condition.
605 * The function will return -ERESTARTSYS if it was interrupted by a
606 * signal and 0 if @condition evaluated to true.
608 #define wait_event_interruptible_locked(wq, condition) \
610 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
613 * wait_event_interruptible_locked_irq - sleep until a condition gets true
614 * @wq: the waitqueue to wait on
615 * @condition: a C expression for the event to wait for
617 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
618 * @condition evaluates to true or a signal is received.
619 * The @condition is checked each time the waitqueue @wq is woken up.
621 * It must be called with wq.lock being held. This spinlock is
622 * unlocked while sleeping but @condition testing is done while lock
623 * is held and when this macro exits the lock is held.
625 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
626 * functions which must match the way they are locked/unlocked outside
629 * wake_up_locked() has to be called after changing any variable that could
630 * change the result of the wait condition.
632 * The function will return -ERESTARTSYS if it was interrupted by a
633 * signal and 0 if @condition evaluated to true.
635 #define wait_event_interruptible_locked_irq(wq, condition) \
637 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
640 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
641 * @wq: the waitqueue to wait on
642 * @condition: a C expression for the event to wait for
644 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
645 * @condition evaluates to true or a signal is received.
646 * The @condition is checked each time the waitqueue @wq is woken up.
648 * It must be called with wq.lock being held. This spinlock is
649 * unlocked while sleeping but @condition testing is done while lock
650 * is held and when this macro exits the lock is held.
652 * The lock is locked/unlocked using spin_lock()/spin_unlock()
653 * functions which must match the way they are locked/unlocked outside
656 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
657 * set thus when other process waits process on the list if this
658 * process is awaken further processes are not considered.
660 * wake_up_locked() has to be called after changing any variable that could
661 * change the result of the wait condition.
663 * The function will return -ERESTARTSYS if it was interrupted by a
664 * signal and 0 if @condition evaluated to true.
666 #define wait_event_interruptible_exclusive_locked(wq, condition) \
668 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
671 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
672 * @wq: the waitqueue to wait on
673 * @condition: a C expression for the event to wait for
675 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
676 * @condition evaluates to true or a signal is received.
677 * The @condition is checked each time the waitqueue @wq is woken up.
679 * It must be called with wq.lock being held. This spinlock is
680 * unlocked while sleeping but @condition testing is done while lock
681 * is held and when this macro exits the lock is held.
683 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
684 * functions which must match the way they are locked/unlocked outside
687 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
688 * set thus when other process waits process on the list if this
689 * process is awaken further processes are not considered.
691 * wake_up_locked() has to be called after changing any variable that could
692 * change the result of the wait condition.
694 * The function will return -ERESTARTSYS if it was interrupted by a
695 * signal and 0 if @condition evaluated to true.
697 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
699 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
702 #define __wait_event_killable(wq, condition) \
703 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
706 * wait_event_killable - sleep until a condition gets true
707 * @wq: the waitqueue to wait on
708 * @condition: a C expression for the event to wait for
710 * The process is put to sleep (TASK_KILLABLE) until the
711 * @condition evaluates to true or a signal is received.
712 * The @condition is checked each time the waitqueue @wq is woken up.
714 * wake_up() has to be called after changing any variable that could
715 * change the result of the wait condition.
717 * The function will return -ERESTARTSYS if it was interrupted by a
718 * signal and 0 if @condition evaluated to true.
720 #define wait_event_killable(wq, condition) \
725 __ret = __wait_event_killable(wq, condition); \
730 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
731 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
732 spin_unlock_irq(&lock); \
735 spin_lock_irq(&lock))
738 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
739 * condition is checked under the lock. This
740 * is expected to be called with the lock
742 * @wq: the waitqueue to wait on
743 * @condition: a C expression for the event to wait for
744 * @lock: a locked spinlock_t, which will be released before cmd
745 * and schedule() and reacquired afterwards.
746 * @cmd: a command which is invoked outside the critical section before
749 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
750 * @condition evaluates to true. The @condition is checked each time
751 * the waitqueue @wq is woken up.
753 * wake_up() has to be called after changing any variable that could
754 * change the result of the wait condition.
756 * This is supposed to be called while holding the lock. The lock is
757 * dropped before invoking the cmd and going to sleep and is reacquired
760 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
764 __wait_event_lock_irq(wq, condition, lock, cmd); \
768 * wait_event_lock_irq - sleep until a condition gets true. The
769 * condition is checked under the lock. This
770 * is expected to be called with the lock
772 * @wq: the waitqueue to wait on
773 * @condition: a C expression for the event to wait for
774 * @lock: a locked spinlock_t, which will be released before schedule()
775 * and reacquired afterwards.
777 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
778 * @condition evaluates to true. The @condition is checked each time
779 * the waitqueue @wq is woken up.
781 * wake_up() has to be called after changing any variable that could
782 * change the result of the wait condition.
784 * This is supposed to be called while holding the lock. The lock is
785 * dropped before going to sleep and is reacquired afterwards.
787 #define wait_event_lock_irq(wq, condition, lock) \
791 __wait_event_lock_irq(wq, condition, lock, ); \
795 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
796 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
797 spin_unlock_irq(&lock); \
800 spin_lock_irq(&lock))
803 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
804 * The condition is checked under the lock. This is expected to
805 * be called with the lock taken.
806 * @wq: the waitqueue to wait on
807 * @condition: a C expression for the event to wait for
808 * @lock: a locked spinlock_t, which will be released before cmd and
809 * schedule() and reacquired afterwards.
810 * @cmd: a command which is invoked outside the critical section before
813 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
814 * @condition evaluates to true or a signal is received. The @condition is
815 * checked each time the waitqueue @wq is woken up.
817 * wake_up() has to be called after changing any variable that could
818 * change the result of the wait condition.
820 * This is supposed to be called while holding the lock. The lock is
821 * dropped before invoking the cmd and going to sleep and is reacquired
824 * The macro will return -ERESTARTSYS if it was interrupted by a signal
825 * and 0 if @condition evaluated to true.
827 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
831 __ret = __wait_event_interruptible_lock_irq(wq, \
832 condition, lock, cmd); \
837 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
838 * The condition is checked under the lock. This is expected
839 * to be called with the lock taken.
840 * @wq: the waitqueue to wait on
841 * @condition: a C expression for the event to wait for
842 * @lock: a locked spinlock_t, which will be released before schedule()
843 * and reacquired afterwards.
845 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
846 * @condition evaluates to true or signal is received. The @condition is
847 * checked each time the waitqueue @wq is woken up.
849 * wake_up() has to be called after changing any variable that could
850 * change the result of the wait condition.
852 * This is supposed to be called while holding the lock. The lock is
853 * dropped before going to sleep and is reacquired afterwards.
855 * The macro will return -ERESTARTSYS if it was interrupted by a signal
856 * and 0 if @condition evaluated to true.
858 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
862 __ret = __wait_event_interruptible_lock_irq(wq, \
867 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
869 ___wait_event(wq, ___wait_cond_timeout(condition), \
870 TASK_INTERRUPTIBLE, 0, timeout, \
871 spin_unlock_irq(&lock); \
872 __ret = schedule_timeout(__ret); \
873 spin_lock_irq(&lock));
876 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
877 * true or a timeout elapses. The condition is checked under
878 * the lock. This is expected to be called with the lock taken.
879 * @wq: the waitqueue to wait on
880 * @condition: a C expression for the event to wait for
881 * @lock: a locked spinlock_t, which will be released before schedule()
882 * and reacquired afterwards.
883 * @timeout: timeout, in jiffies
885 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
886 * @condition evaluates to true or signal is received. The @condition is
887 * checked each time the waitqueue @wq is woken up.
889 * wake_up() has to be called after changing any variable that could
890 * change the result of the wait condition.
892 * This is supposed to be called while holding the lock. The lock is
893 * dropped before going to sleep and is reacquired afterwards.
895 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
896 * was interrupted by a signal, and the remaining jiffies otherwise
897 * if the condition evaluated to true before the timeout elapsed.
899 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
902 long __ret = timeout; \
903 if (!___wait_cond_timeout(condition)) \
904 __ret = __wait_event_interruptible_lock_irq_timeout( \
905 wq, condition, lock, timeout); \
910 * Waitqueues which are removed from the waitqueue_head at wakeup time
912 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
913 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
914 long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
915 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
916 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
917 long wait_woken(wait_queue_t *wait, unsigned mode, long timeout);
918 int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
919 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
920 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
922 #define DEFINE_WAIT_FUNC(name, function) \
923 wait_queue_t name = { \
924 .private = current, \
926 .task_list = LIST_HEAD_INIT((name).task_list), \
929 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
931 #define DEFINE_WAIT_BIT(name, word, bit) \
932 struct wait_bit_queue name = { \
933 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
935 .private = current, \
936 .func = wake_bit_function, \
938 LIST_HEAD_INIT((name).wait.task_list), \
942 #define init_wait(wait) \
944 (wait)->private = current; \
945 (wait)->func = autoremove_wake_function; \
946 INIT_LIST_HEAD(&(wait)->task_list); \
951 extern int bit_wait(struct wait_bit_key *);
952 extern int bit_wait_io(struct wait_bit_key *);
953 extern int bit_wait_timeout(struct wait_bit_key *);
954 extern int bit_wait_io_timeout(struct wait_bit_key *);
957 * wait_on_bit - wait for a bit to be cleared
958 * @word: the word being waited on, a kernel virtual address
959 * @bit: the bit of the word being waited on
960 * @mode: the task state to sleep in
962 * There is a standard hashed waitqueue table for generic use. This
963 * is the part of the hashtable's accessor API that waits on a bit.
964 * For instance, if one were to have waiters on a bitflag, one would
965 * call wait_on_bit() in threads waiting for the bit to clear.
966 * One uses wait_on_bit() where one is waiting for the bit to clear,
967 * but has no intention of setting it.
968 * Returned value will be zero if the bit was cleared, or non-zero
969 * if the process received a signal and the mode permitted wakeup
973 wait_on_bit(void *word, int bit, unsigned mode)
976 if (!test_bit(bit, word))
978 return out_of_line_wait_on_bit(word, bit,
984 * wait_on_bit_io - wait for a bit to be cleared
985 * @word: the word being waited on, a kernel virtual address
986 * @bit: the bit of the word being waited on
987 * @mode: the task state to sleep in
989 * Use the standard hashed waitqueue table to wait for a bit
990 * to be cleared. This is similar to wait_on_bit(), but calls
991 * io_schedule() instead of schedule() for the actual waiting.
993 * Returned value will be zero if the bit was cleared, or non-zero
994 * if the process received a signal and the mode permitted wakeup
998 wait_on_bit_io(void *word, int bit, unsigned mode)
1001 if (!test_bit(bit, word))
1003 return out_of_line_wait_on_bit(word, bit,
1009 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
1010 * @word: the word being waited on, a kernel virtual address
1011 * @bit: the bit of the word being waited on
1012 * @mode: the task state to sleep in
1013 * @timeout: timeout, in jiffies
1015 * Use the standard hashed waitqueue table to wait for a bit
1016 * to be cleared. This is similar to wait_on_bit(), except also takes a
1017 * timeout parameter.
1019 * Returned value will be zero if the bit was cleared before the
1020 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1021 * received a signal and the mode permitted wakeup on that signal.
1024 wait_on_bit_timeout(void *word, int bit, unsigned mode, unsigned long timeout)
1027 if (!test_bit(bit, word))
1029 return out_of_line_wait_on_bit_timeout(word, bit,
1035 * wait_on_bit_action - wait for a bit to be cleared
1036 * @word: the word being waited on, a kernel virtual address
1037 * @bit: the bit of the word being waited on
1038 * @action: the function used to sleep, which may take special actions
1039 * @mode: the task state to sleep in
1041 * Use the standard hashed waitqueue table to wait for a bit
1042 * to be cleared, and allow the waiting action to be specified.
1043 * This is like wait_on_bit() but allows fine control of how the waiting
1046 * Returned value will be zero if the bit was cleared, or non-zero
1047 * if the process received a signal and the mode permitted wakeup
1051 wait_on_bit_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1054 if (!test_bit(bit, word))
1056 return out_of_line_wait_on_bit(word, bit, action, mode);
1060 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1061 * @word: the word being waited on, a kernel virtual address
1062 * @bit: the bit of the word being waited on
1063 * @mode: the task state to sleep in
1065 * There is a standard hashed waitqueue table for generic use. This
1066 * is the part of the hashtable's accessor API that waits on a bit
1067 * when one intends to set it, for instance, trying to lock bitflags.
1068 * For instance, if one were to have waiters trying to set bitflag
1069 * and waiting for it to clear before setting it, one would call
1070 * wait_on_bit() in threads waiting to be able to set the bit.
1071 * One uses wait_on_bit_lock() where one is waiting for the bit to
1072 * clear with the intention of setting it, and when done, clearing it.
1074 * Returns zero if the bit was (eventually) found to be clear and was
1075 * set. Returns non-zero if a signal was delivered to the process and
1076 * the @mode allows that signal to wake the process.
1079 wait_on_bit_lock(void *word, int bit, unsigned mode)
1082 if (!test_and_set_bit(bit, word))
1084 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
1088 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1089 * @word: the word being waited on, a kernel virtual address
1090 * @bit: the bit of the word being waited on
1091 * @mode: the task state to sleep in
1093 * Use the standard hashed waitqueue table to wait for a bit
1094 * to be cleared and then to atomically set it. This is similar
1095 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1096 * for the actual waiting.
1098 * Returns zero if the bit was (eventually) found to be clear and was
1099 * set. Returns non-zero if a signal was delivered to the process and
1100 * the @mode allows that signal to wake the process.
1103 wait_on_bit_lock_io(void *word, int bit, unsigned mode)
1106 if (!test_and_set_bit(bit, word))
1108 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
1112 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1113 * @word: the word being waited on, a kernel virtual address
1114 * @bit: the bit of the word being waited on
1115 * @action: the function used to sleep, which may take special actions
1116 * @mode: the task state to sleep in
1118 * Use the standard hashed waitqueue table to wait for a bit
1119 * to be cleared and then to set it, and allow the waiting action
1121 * This is like wait_on_bit() but allows fine control of how the waiting
1124 * Returns zero if the bit was (eventually) found to be clear and was
1125 * set. Returns non-zero if a signal was delivered to the process and
1126 * the @mode allows that signal to wake the process.
1129 wait_on_bit_lock_action(void *word, int bit, wait_bit_action_f *action, unsigned mode)
1132 if (!test_and_set_bit(bit, word))
1134 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
1138 * wait_on_atomic_t - Wait for an atomic_t to become 0
1139 * @val: The atomic value being waited on, a kernel virtual address
1140 * @action: the function used to sleep, which may take special actions
1141 * @mode: the task state to sleep in
1143 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1144 * the purpose of getting a waitqueue, but we set the key to a bit number
1145 * outside of the target 'word'.
1148 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1151 if (atomic_read(val) == 0)
1153 return out_of_line_wait_on_atomic_t(val, action, mode);
1156 #endif /* _LINUX_WAIT_H */