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
30 struct wait_bit_queue {
31 struct wait_bit_key key;
35 struct __wait_queue_head {
37 struct list_head task_list;
39 typedef struct __wait_queue_head wait_queue_head_t;
44 * Macros for declaration and initialisaton of the datatypes
47 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
49 .func = default_wake_function, \
50 .task_list = { NULL, NULL } }
52 #define DECLARE_WAITQUEUE(name, tsk) \
53 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
55 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
56 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
57 .task_list = { &(name).task_list, &(name).task_list } }
59 #define DECLARE_WAIT_QUEUE_HEAD(name) \
60 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
62 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
63 { .flags = word, .bit_nr = bit, }
65 #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
66 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
68 extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
70 #define init_waitqueue_head(q) \
72 static struct lock_class_key __key; \
74 __init_waitqueue_head((q), #q, &__key); \
78 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
79 ({ init_waitqueue_head(&name); name; })
80 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
81 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
83 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
86 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
90 q->func = default_wake_function;
94 init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func)
101 static inline int waitqueue_active(wait_queue_head_t *q)
103 return !list_empty(&q->task_list);
106 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
107 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
108 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
110 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
112 list_add(&new->task_list, &head->task_list);
116 * Used for wake-one threads:
119 __add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
121 wait->flags |= WQ_FLAG_EXCLUSIVE;
122 __add_wait_queue(q, wait);
125 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
128 list_add_tail(&new->task_list, &head->task_list);
132 __add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
134 wait->flags |= WQ_FLAG_EXCLUSIVE;
135 __add_wait_queue_tail(q, wait);
139 __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old)
141 list_del(&old->task_list);
144 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
145 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
146 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
147 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
148 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
149 void __wake_up_bit(wait_queue_head_t *, void *, int);
150 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
151 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
152 void wake_up_bit(void *, int);
153 void wake_up_atomic_t(atomic_t *);
154 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
155 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
156 int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
157 wait_queue_head_t *bit_waitqueue(void *, int);
159 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
160 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
161 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
162 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
163 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
165 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
166 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
167 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
168 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
171 * Wakeup macros to be used to report events to the targets.
173 #define wake_up_poll(x, m) \
174 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
175 #define wake_up_locked_poll(x, m) \
176 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
177 #define wake_up_interruptible_poll(x, m) \
178 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
179 #define wake_up_interruptible_sync_poll(x, m) \
180 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
182 #define ___wait_cond_timeout(condition) \
184 bool __cond = (condition); \
185 if (__cond && !__ret) \
190 #define ___wait_is_interruptible(state) \
191 (!__builtin_constant_p(state) || \
192 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
194 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
197 wait_queue_t __wait; \
200 INIT_LIST_HEAD(&__wait.task_list); \
202 __wait.flags = WQ_FLAG_EXCLUSIVE; \
207 long __int = prepare_to_wait_event(&wq, &__wait, state);\
212 if (___wait_is_interruptible(state) && __int) { \
215 abort_exclusive_wait(&wq, &__wait, \
224 finish_wait(&wq, &__wait); \
228 #define __wait_event(wq, condition) \
229 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
233 * wait_event - sleep until a condition gets true
234 * @wq: the waitqueue to wait on
235 * @condition: a C expression for the event to wait for
237 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
238 * @condition evaluates to true. The @condition is checked each time
239 * the waitqueue @wq is woken up.
241 * wake_up() has to be called after changing any variable that could
242 * change the result of the wait condition.
244 #define wait_event(wq, condition) \
248 __wait_event(wq, condition); \
251 #define __wait_event_timeout(wq, condition, timeout) \
252 ___wait_event(wq, ___wait_cond_timeout(condition), \
253 TASK_UNINTERRUPTIBLE, 0, timeout, \
254 __ret = schedule_timeout(__ret))
257 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
258 * @wq: the waitqueue to wait on
259 * @condition: a C expression for the event to wait for
260 * @timeout: timeout, in jiffies
262 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
263 * @condition evaluates to true. The @condition is checked each time
264 * the waitqueue @wq is woken up.
266 * wake_up() has to be called after changing any variable that could
267 * change the result of the wait condition.
269 * The function returns 0 if the @timeout elapsed, or the remaining
270 * jiffies (at least 1) if the @condition evaluated to %true before
271 * the @timeout elapsed.
273 #define wait_event_timeout(wq, condition, timeout) \
275 long __ret = timeout; \
276 if (!___wait_cond_timeout(condition)) \
277 __ret = __wait_event_timeout(wq, condition, timeout); \
281 #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
282 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
283 cmd1; schedule(); cmd2)
286 * wait_event_cmd - sleep until a condition gets true
287 * @wq: the waitqueue to wait on
288 * @condition: a C expression for the event to wait for
289 * cmd1: the command will be executed before sleep
290 * cmd2: the command will be executed after sleep
292 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
293 * @condition evaluates to true. The @condition is checked each time
294 * the waitqueue @wq is woken up.
296 * wake_up() has to be called after changing any variable that could
297 * change the result of the wait condition.
299 #define wait_event_cmd(wq, condition, cmd1, cmd2) \
303 __wait_event_cmd(wq, condition, cmd1, cmd2); \
306 #define __wait_event_interruptible(wq, condition) \
307 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
311 * wait_event_interruptible - sleep until a condition gets true
312 * @wq: the waitqueue to wait on
313 * @condition: a C expression for the event to wait for
315 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
316 * @condition evaluates to true or a signal is received.
317 * The @condition is checked each time the waitqueue @wq is woken up.
319 * wake_up() has to be called after changing any variable that could
320 * change the result of the wait condition.
322 * The function will return -ERESTARTSYS if it was interrupted by a
323 * signal and 0 if @condition evaluated to true.
325 #define wait_event_interruptible(wq, condition) \
329 __ret = __wait_event_interruptible(wq, condition); \
333 #define __wait_event_interruptible_timeout(wq, condition, timeout) \
334 ___wait_event(wq, ___wait_cond_timeout(condition), \
335 TASK_INTERRUPTIBLE, 0, timeout, \
336 __ret = schedule_timeout(__ret))
339 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
340 * @wq: the waitqueue to wait on
341 * @condition: a C expression for the event to wait for
342 * @timeout: timeout, in jiffies
344 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
345 * @condition evaluates to true or a signal is received.
346 * The @condition is checked each time the waitqueue @wq is woken up.
348 * wake_up() has to be called after changing any variable that could
349 * change the result of the wait condition.
352 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
353 * a signal, or the remaining jiffies (at least 1) if the @condition
354 * evaluated to %true before the @timeout elapsed.
356 #define wait_event_interruptible_timeout(wq, condition, timeout) \
358 long __ret = timeout; \
359 if (!___wait_cond_timeout(condition)) \
360 __ret = __wait_event_interruptible_timeout(wq, \
361 condition, timeout); \
365 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
368 struct hrtimer_sleeper __t; \
370 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
372 hrtimer_init_sleeper(&__t, current); \
373 if ((timeout).tv64 != KTIME_MAX) \
374 hrtimer_start_range_ns(&__t.timer, timeout, \
375 current->timer_slack_ns, \
378 __ret = ___wait_event(wq, condition, state, 0, 0, \
385 hrtimer_cancel(&__t.timer); \
386 destroy_hrtimer_on_stack(&__t.timer); \
391 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
392 * @wq: the waitqueue to wait on
393 * @condition: a C expression for the event to wait for
394 * @timeout: timeout, as a ktime_t
396 * The process is put to sleep (TASK_UNINTERRUPTIBLE) 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 returns 0 if @condition became true, or -ETIME if the timeout
406 #define wait_event_hrtimeout(wq, condition, timeout) \
410 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
411 TASK_UNINTERRUPTIBLE); \
416 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
417 * @wq: the waitqueue to wait on
418 * @condition: a C expression for the event to wait for
419 * @timeout: timeout, as a ktime_t
421 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
422 * @condition evaluates to true or a signal is received.
423 * The @condition is checked each time the waitqueue @wq is woken up.
425 * wake_up() has to be called after changing any variable that could
426 * change the result of the wait condition.
428 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
429 * interrupted by a signal, or -ETIME if the timeout elapsed.
431 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
435 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
436 TASK_INTERRUPTIBLE); \
440 #define __wait_event_interruptible_exclusive(wq, condition) \
441 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
444 #define wait_event_interruptible_exclusive(wq, condition) \
448 __ret = __wait_event_interruptible_exclusive(wq, condition);\
453 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
456 DEFINE_WAIT(__wait); \
458 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
460 if (likely(list_empty(&__wait.task_list))) \
461 __add_wait_queue_tail(&(wq), &__wait); \
462 set_current_state(TASK_INTERRUPTIBLE); \
463 if (signal_pending(current)) { \
464 __ret = -ERESTARTSYS; \
468 spin_unlock_irq(&(wq).lock); \
470 spin_unlock(&(wq).lock); \
473 spin_lock_irq(&(wq).lock); \
475 spin_lock(&(wq).lock); \
476 } while (!(condition)); \
477 __remove_wait_queue(&(wq), &__wait); \
478 __set_current_state(TASK_RUNNING); \
484 * wait_event_interruptible_locked - sleep until a condition gets true
485 * @wq: the waitqueue to wait on
486 * @condition: a C expression for the event to wait for
488 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
489 * @condition evaluates to true or a signal is received.
490 * The @condition is checked each time the waitqueue @wq is woken up.
492 * It must be called with wq.lock being held. This spinlock is
493 * unlocked while sleeping but @condition testing is done while lock
494 * is held and when this macro exits the lock is held.
496 * The lock is locked/unlocked using spin_lock()/spin_unlock()
497 * functions which must match the way they are locked/unlocked outside
500 * wake_up_locked() has to be called after changing any variable that could
501 * change the result of the wait condition.
503 * The function will return -ERESTARTSYS if it was interrupted by a
504 * signal and 0 if @condition evaluated to true.
506 #define wait_event_interruptible_locked(wq, condition) \
508 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
511 * wait_event_interruptible_locked_irq - sleep until a condition gets true
512 * @wq: the waitqueue to wait on
513 * @condition: a C expression for the event to wait for
515 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
516 * @condition evaluates to true or a signal is received.
517 * The @condition is checked each time the waitqueue @wq is woken up.
519 * It must be called with wq.lock being held. This spinlock is
520 * unlocked while sleeping but @condition testing is done while lock
521 * is held and when this macro exits the lock is held.
523 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
524 * functions which must match the way they are locked/unlocked outside
527 * wake_up_locked() has to be called after changing any variable that could
528 * change the result of the wait condition.
530 * The function will return -ERESTARTSYS if it was interrupted by a
531 * signal and 0 if @condition evaluated to true.
533 #define wait_event_interruptible_locked_irq(wq, condition) \
535 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
538 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
539 * @wq: the waitqueue to wait on
540 * @condition: a C expression for the event to wait for
542 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
543 * @condition evaluates to true or a signal is received.
544 * The @condition is checked each time the waitqueue @wq is woken up.
546 * It must be called with wq.lock being held. This spinlock is
547 * unlocked while sleeping but @condition testing is done while lock
548 * is held and when this macro exits the lock is held.
550 * The lock is locked/unlocked using spin_lock()/spin_unlock()
551 * functions which must match the way they are locked/unlocked outside
554 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
555 * set thus when other process waits process on the list if this
556 * process is awaken further processes are not considered.
558 * wake_up_locked() has to be called after changing any variable that could
559 * change the result of the wait condition.
561 * The function will return -ERESTARTSYS if it was interrupted by a
562 * signal and 0 if @condition evaluated to true.
564 #define wait_event_interruptible_exclusive_locked(wq, condition) \
566 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
569 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
570 * @wq: the waitqueue to wait on
571 * @condition: a C expression for the event to wait for
573 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
574 * @condition evaluates to true or a signal is received.
575 * The @condition is checked each time the waitqueue @wq is woken up.
577 * It must be called with wq.lock being held. This spinlock is
578 * unlocked while sleeping but @condition testing is done while lock
579 * is held and when this macro exits the lock is held.
581 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
582 * functions which must match the way they are locked/unlocked outside
585 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
586 * set thus when other process waits process on the list if this
587 * process is awaken further processes are not considered.
589 * wake_up_locked() has to be called after changing any variable that could
590 * change the result of the wait condition.
592 * The function will return -ERESTARTSYS if it was interrupted by a
593 * signal and 0 if @condition evaluated to true.
595 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
597 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
600 #define __wait_event_killable(wq, condition) \
601 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
604 * wait_event_killable - sleep until a condition gets true
605 * @wq: the waitqueue to wait on
606 * @condition: a C expression for the event to wait for
608 * The process is put to sleep (TASK_KILLABLE) until the
609 * @condition evaluates to true or a signal is received.
610 * The @condition is checked each time the waitqueue @wq is woken up.
612 * wake_up() has to be called after changing any variable that could
613 * change the result of the wait condition.
615 * The function will return -ERESTARTSYS if it was interrupted by a
616 * signal and 0 if @condition evaluated to true.
618 #define wait_event_killable(wq, condition) \
622 __ret = __wait_event_killable(wq, condition); \
627 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
628 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
629 spin_unlock_irq(&lock); \
632 spin_lock_irq(&lock))
635 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
636 * condition is checked under the lock. This
637 * is expected to be called with the lock
639 * @wq: the waitqueue to wait on
640 * @condition: a C expression for the event to wait for
641 * @lock: a locked spinlock_t, which will be released before cmd
642 * and schedule() and reacquired afterwards.
643 * @cmd: a command which is invoked outside the critical section before
646 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
647 * @condition evaluates to true. The @condition is checked each time
648 * the waitqueue @wq is woken up.
650 * wake_up() has to be called after changing any variable that could
651 * change the result of the wait condition.
653 * This is supposed to be called while holding the lock. The lock is
654 * dropped before invoking the cmd and going to sleep and is reacquired
657 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
661 __wait_event_lock_irq(wq, condition, lock, cmd); \
665 * wait_event_lock_irq - sleep until a condition gets true. The
666 * condition is checked under the lock. This
667 * is expected to be called with the lock
669 * @wq: the waitqueue to wait on
670 * @condition: a C expression for the event to wait for
671 * @lock: a locked spinlock_t, which will be released before schedule()
672 * and reacquired afterwards.
674 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
675 * @condition evaluates to true. The @condition is checked each time
676 * the waitqueue @wq is woken up.
678 * wake_up() has to be called after changing any variable that could
679 * change the result of the wait condition.
681 * This is supposed to be called while holding the lock. The lock is
682 * dropped before going to sleep and is reacquired afterwards.
684 #define wait_event_lock_irq(wq, condition, lock) \
688 __wait_event_lock_irq(wq, condition, lock, ); \
692 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
693 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
694 spin_unlock_irq(&lock); \
697 spin_lock_irq(&lock))
700 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
701 * The condition is checked under the lock. This is expected to
702 * be called with the lock taken.
703 * @wq: the waitqueue to wait on
704 * @condition: a C expression for the event to wait for
705 * @lock: a locked spinlock_t, which will be released before cmd and
706 * schedule() and reacquired afterwards.
707 * @cmd: a command which is invoked outside the critical section before
710 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
711 * @condition evaluates to true or a signal is received. The @condition is
712 * 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 * This is supposed to be called while holding the lock. The lock is
718 * dropped before invoking the cmd and going to sleep and is reacquired
721 * The macro will return -ERESTARTSYS if it was interrupted by a signal
722 * and 0 if @condition evaluated to true.
724 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
728 __ret = __wait_event_interruptible_lock_irq(wq, \
729 condition, lock, cmd); \
734 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
735 * The condition is checked under the lock. This is expected
736 * to be called with the lock taken.
737 * @wq: the waitqueue to wait on
738 * @condition: a C expression for the event to wait for
739 * @lock: a locked spinlock_t, which will be released before schedule()
740 * and reacquired afterwards.
742 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
743 * @condition evaluates to true or signal is received. The @condition is
744 * checked each time the waitqueue @wq is woken up.
746 * wake_up() has to be called after changing any variable that could
747 * change the result of the wait condition.
749 * This is supposed to be called while holding the lock. The lock is
750 * dropped before going to sleep and is reacquired afterwards.
752 * The macro will return -ERESTARTSYS if it was interrupted by a signal
753 * and 0 if @condition evaluated to true.
755 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
759 __ret = __wait_event_interruptible_lock_irq(wq, \
764 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
766 ___wait_event(wq, ___wait_cond_timeout(condition), \
767 TASK_INTERRUPTIBLE, 0, timeout, \
768 spin_unlock_irq(&lock); \
769 __ret = schedule_timeout(__ret); \
770 spin_lock_irq(&lock));
773 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
774 * true or a timeout elapses. The condition is checked under
775 * the lock. This is expected to be called with the lock taken.
776 * @wq: the waitqueue to wait on
777 * @condition: a C expression for the event to wait for
778 * @lock: a locked spinlock_t, which will be released before schedule()
779 * and reacquired afterwards.
780 * @timeout: timeout, in jiffies
782 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
783 * @condition evaluates to true or signal is received. The @condition is
784 * checked each time the waitqueue @wq is woken up.
786 * wake_up() has to be called after changing any variable that could
787 * change the result of the wait condition.
789 * This is supposed to be called while holding the lock. The lock is
790 * dropped before going to sleep and is reacquired afterwards.
792 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
793 * was interrupted by a signal, and the remaining jiffies otherwise
794 * if the condition evaluated to true before the timeout elapsed.
796 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
799 long __ret = timeout; \
800 if (!___wait_cond_timeout(condition)) \
801 __ret = __wait_event_interruptible_lock_irq_timeout( \
802 wq, condition, lock, timeout); \
808 * These are the old interfaces to sleep waiting for an event.
809 * They are racy. DO NOT use them, use the wait_event* interfaces above.
810 * We plan to remove these interfaces.
812 extern void sleep_on(wait_queue_head_t *q);
813 extern long sleep_on_timeout(wait_queue_head_t *q, signed long timeout);
814 extern void interruptible_sleep_on(wait_queue_head_t *q);
815 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q, signed long timeout);
818 * Waitqueues which are removed from the waitqueue_head at wakeup time
820 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
821 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
822 long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state);
823 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
824 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key);
825 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
826 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
828 #define DEFINE_WAIT_FUNC(name, function) \
829 wait_queue_t name = { \
830 .private = current, \
832 .task_list = LIST_HEAD_INIT((name).task_list), \
835 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
837 #define DEFINE_WAIT_BIT(name, word, bit) \
838 struct wait_bit_queue name = { \
839 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
841 .private = current, \
842 .func = wake_bit_function, \
844 LIST_HEAD_INIT((name).wait.task_list), \
848 #define init_wait(wait) \
850 (wait)->private = current; \
851 (wait)->func = autoremove_wake_function; \
852 INIT_LIST_HEAD(&(wait)->task_list); \
857 * wait_on_bit - wait for a bit to be cleared
858 * @word: the word being waited on, a kernel virtual address
859 * @bit: the bit of the word being waited on
860 * @action: the function used to sleep, which may take special actions
861 * @mode: the task state to sleep in
863 * There is a standard hashed waitqueue table for generic use. This
864 * is the part of the hashtable's accessor API that waits on a bit.
865 * For instance, if one were to have waiters on a bitflag, one would
866 * call wait_on_bit() in threads waiting for the bit to clear.
867 * One uses wait_on_bit() where one is waiting for the bit to clear,
868 * but has no intention of setting it.
871 wait_on_bit(void *word, int bit, int (*action)(void *), unsigned mode)
873 if (!test_bit(bit, word))
875 return out_of_line_wait_on_bit(word, bit, action, mode);
879 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
880 * @word: the word being waited on, a kernel virtual address
881 * @bit: the bit of the word being waited on
882 * @action: the function used to sleep, which may take special actions
883 * @mode: the task state to sleep in
885 * There is a standard hashed waitqueue table for generic use. This
886 * is the part of the hashtable's accessor API that waits on a bit
887 * when one intends to set it, for instance, trying to lock bitflags.
888 * For instance, if one were to have waiters trying to set bitflag
889 * and waiting for it to clear before setting it, one would call
890 * wait_on_bit() in threads waiting to be able to set the bit.
891 * One uses wait_on_bit_lock() where one is waiting for the bit to
892 * clear with the intention of setting it, and when done, clearing it.
895 wait_on_bit_lock(void *word, int bit, int (*action)(void *), unsigned mode)
897 if (!test_and_set_bit(bit, word))
899 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
903 * wait_on_atomic_t - Wait for an atomic_t to become 0
904 * @val: The atomic value being waited on, a kernel virtual address
905 * @action: the function used to sleep, which may take special actions
906 * @mode: the task state to sleep in
908 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
909 * the purpose of getting a waitqueue, but we set the key to a bit number
910 * outside of the target 'word'.
913 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
915 if (atomic_read(val) == 0)
917 return out_of_line_wait_on_atomic_t(val, action, mode);
920 #endif /* _LINUX_WAIT_H */