5 #include <linux/list.h>
6 #include <linux/stddef.h>
7 #include <linux/spinlock.h>
8 #include <asm/current.h>
9 #include <uapi/linux/wait.h>
11 typedef struct __wait_queue wait_queue_t;
12 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
13 int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
17 #define WQ_FLAG_EXCLUSIVE 0x01
19 wait_queue_func_t func;
20 struct list_head task_list;
26 #define WAIT_ATOMIC_T_BIT_NR -1
29 struct wait_bit_queue {
30 struct wait_bit_key key;
34 struct __wait_queue_head {
36 struct list_head task_list;
38 typedef struct __wait_queue_head wait_queue_head_t;
43 * Macros for declaration and initialisaton of the datatypes
46 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
48 .func = default_wake_function, \
49 .task_list = { NULL, NULL } }
51 #define DECLARE_WAITQUEUE(name, tsk) \
52 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
54 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
55 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
56 .task_list = { &(name).task_list, &(name).task_list } }
58 #define DECLARE_WAIT_QUEUE_HEAD(name) \
59 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
61 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
62 { .flags = word, .bit_nr = bit, }
64 #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
65 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
67 extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
69 #define init_waitqueue_head(q) \
71 static struct lock_class_key __key; \
73 __init_waitqueue_head((q), #q, &__key); \
77 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
78 ({ init_waitqueue_head(&name); name; })
79 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
80 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
82 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
85 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
89 q->func = default_wake_function;
92 static inline void init_waitqueue_func_entry(wait_queue_t *q,
93 wait_queue_func_t func)
100 static inline int waitqueue_active(wait_queue_head_t *q)
102 return !list_empty(&q->task_list);
105 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
106 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
107 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
109 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
111 list_add(&new->task_list, &head->task_list);
115 * Used for wake-one threads:
117 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
120 wait->flags |= WQ_FLAG_EXCLUSIVE;
121 __add_wait_queue(q, wait);
124 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
127 list_add_tail(&new->task_list, &head->task_list);
130 static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q,
133 wait->flags |= WQ_FLAG_EXCLUSIVE;
134 __add_wait_queue_tail(q, wait);
137 static inline void __remove_wait_queue(wait_queue_head_t *head,
140 list_del(&old->task_list);
143 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
144 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
145 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
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_signal_pending(state) \
191 ((state == TASK_INTERRUPTIBLE && signal_pending(current)) || \
192 (state == TASK_KILLABLE && fatal_signal_pending(current)))
194 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
197 DEFINE_WAIT(__wait); \
202 prepare_to_wait_exclusive(&wq, &__wait, state); \
204 prepare_to_wait(&wq, &__wait, state); \
209 if (___wait_signal_pending(state)) { \
210 __ret = -ERESTARTSYS; \
212 abort_exclusive_wait(&wq, &__wait, \
221 finish_wait(&wq, &__wait); \
225 #define __wait_event(wq, condition) \
226 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
230 * wait_event - sleep until a condition gets true
231 * @wq: the waitqueue to wait on
232 * @condition: a C expression for the event to wait for
234 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
235 * @condition evaluates to true. The @condition is checked each time
236 * the waitqueue @wq is woken up.
238 * wake_up() has to be called after changing any variable that could
239 * change the result of the wait condition.
241 #define wait_event(wq, condition) \
245 __wait_event(wq, condition); \
248 #define __wait_event_timeout(wq, condition, timeout) \
249 ___wait_event(wq, ___wait_cond_timeout(condition), \
250 TASK_UNINTERRUPTIBLE, 0, timeout, \
251 __ret = schedule_timeout(__ret))
254 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
255 * @wq: the waitqueue to wait on
256 * @condition: a C expression for the event to wait for
257 * @timeout: timeout, in jiffies
259 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
260 * @condition evaluates to true. The @condition is checked each time
261 * the waitqueue @wq is woken up.
263 * wake_up() has to be called after changing any variable that could
264 * change the result of the wait condition.
266 * The function returns 0 if the @timeout elapsed, or the remaining
267 * jiffies (at least 1) if the @condition evaluated to %true before
268 * the @timeout elapsed.
270 #define wait_event_timeout(wq, condition, timeout) \
272 long __ret = timeout; \
274 __ret = __wait_event_timeout(wq, condition, timeout); \
278 #define __wait_event_interruptible(wq, condition) \
279 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
283 * wait_event_interruptible - sleep until a condition gets true
284 * @wq: the waitqueue to wait on
285 * @condition: a C expression for the event to wait for
287 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
288 * @condition evaluates to true or a signal is received.
289 * The @condition is checked each time the waitqueue @wq is woken up.
291 * wake_up() has to be called after changing any variable that could
292 * change the result of the wait condition.
294 * The function will return -ERESTARTSYS if it was interrupted by a
295 * signal and 0 if @condition evaluated to true.
297 #define wait_event_interruptible(wq, condition) \
301 __ret = __wait_event_interruptible(wq, condition); \
305 #define __wait_event_interruptible_timeout(wq, condition, timeout) \
306 ___wait_event(wq, ___wait_cond_timeout(condition), \
307 TASK_INTERRUPTIBLE, 0, timeout, \
308 __ret = schedule_timeout(__ret))
311 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
312 * @wq: the waitqueue to wait on
313 * @condition: a C expression for the event to wait for
314 * @timeout: timeout, in jiffies
316 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
317 * @condition evaluates to true or a signal is received.
318 * The @condition is checked each time the waitqueue @wq is woken up.
320 * wake_up() has to be called after changing any variable that could
321 * change the result of the wait condition.
324 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
325 * a signal, or the remaining jiffies (at least 1) if the @condition
326 * evaluated to %true before the @timeout elapsed.
328 #define wait_event_interruptible_timeout(wq, condition, timeout) \
330 long __ret = timeout; \
332 __ret = __wait_event_interruptible_timeout(wq, \
333 condition, timeout); \
337 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
340 struct hrtimer_sleeper __t; \
342 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
344 hrtimer_init_sleeper(&__t, current); \
345 if ((timeout).tv64 != KTIME_MAX) \
346 hrtimer_start_range_ns(&__t.timer, timeout, \
347 current->timer_slack_ns, \
350 __ret = ___wait_event(wq, condition, state, 0, 0, \
357 hrtimer_cancel(&__t.timer); \
358 destroy_hrtimer_on_stack(&__t.timer); \
363 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
364 * @wq: the waitqueue to wait on
365 * @condition: a C expression for the event to wait for
366 * @timeout: timeout, as a ktime_t
368 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
369 * @condition evaluates to true or a signal is received.
370 * The @condition is checked each time the waitqueue @wq is woken up.
372 * wake_up() has to be called after changing any variable that could
373 * change the result of the wait condition.
375 * The function returns 0 if @condition became true, or -ETIME if the timeout
378 #define wait_event_hrtimeout(wq, condition, timeout) \
382 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
383 TASK_UNINTERRUPTIBLE); \
388 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
389 * @wq: the waitqueue to wait on
390 * @condition: a C expression for the event to wait for
391 * @timeout: timeout, as a ktime_t
393 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
394 * @condition evaluates to true or a signal is received.
395 * The @condition is checked each time the waitqueue @wq is woken up.
397 * wake_up() has to be called after changing any variable that could
398 * change the result of the wait condition.
400 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
401 * interrupted by a signal, or -ETIME if the timeout elapsed.
403 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
407 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
408 TASK_INTERRUPTIBLE); \
412 #define __wait_event_interruptible_exclusive(wq, condition) \
413 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
416 #define wait_event_interruptible_exclusive(wq, condition) \
420 __ret = __wait_event_interruptible_exclusive(wq, condition);\
425 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
428 DEFINE_WAIT(__wait); \
430 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
432 if (likely(list_empty(&__wait.task_list))) \
433 __add_wait_queue_tail(&(wq), &__wait); \
434 set_current_state(TASK_INTERRUPTIBLE); \
435 if (signal_pending(current)) { \
436 __ret = -ERESTARTSYS; \
440 spin_unlock_irq(&(wq).lock); \
442 spin_unlock(&(wq).lock); \
445 spin_lock_irq(&(wq).lock); \
447 spin_lock(&(wq).lock); \
448 } while (!(condition)); \
449 __remove_wait_queue(&(wq), &__wait); \
450 __set_current_state(TASK_RUNNING); \
456 * wait_event_interruptible_locked - sleep until a condition gets true
457 * @wq: the waitqueue to wait on
458 * @condition: a C expression for the event to wait for
460 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
461 * @condition evaluates to true or a signal is received.
462 * The @condition is checked each time the waitqueue @wq is woken up.
464 * It must be called with wq.lock being held. This spinlock is
465 * unlocked while sleeping but @condition testing is done while lock
466 * is held and when this macro exits the lock is held.
468 * The lock is locked/unlocked using spin_lock()/spin_unlock()
469 * functions which must match the way they are locked/unlocked outside
472 * wake_up_locked() has to be called after changing any variable that could
473 * change the result of the wait condition.
475 * The function will return -ERESTARTSYS if it was interrupted by a
476 * signal and 0 if @condition evaluated to true.
478 #define wait_event_interruptible_locked(wq, condition) \
480 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
483 * wait_event_interruptible_locked_irq - sleep until a condition gets true
484 * @wq: the waitqueue to wait on
485 * @condition: a C expression for the event to wait for
487 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
488 * @condition evaluates to true or a signal is received.
489 * The @condition is checked each time the waitqueue @wq is woken up.
491 * It must be called with wq.lock being held. This spinlock is
492 * unlocked while sleeping but @condition testing is done while lock
493 * is held and when this macro exits the lock is held.
495 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
496 * functions which must match the way they are locked/unlocked outside
499 * wake_up_locked() has to be called after changing any variable that could
500 * change the result of the wait condition.
502 * The function will return -ERESTARTSYS if it was interrupted by a
503 * signal and 0 if @condition evaluated to true.
505 #define wait_event_interruptible_locked_irq(wq, condition) \
507 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
510 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
511 * @wq: the waitqueue to wait on
512 * @condition: a C expression for the event to wait for
514 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
515 * @condition evaluates to true or a signal is received.
516 * The @condition is checked each time the waitqueue @wq is woken up.
518 * It must be called with wq.lock being held. This spinlock is
519 * unlocked while sleeping but @condition testing is done while lock
520 * is held and when this macro exits the lock is held.
522 * The lock is locked/unlocked using spin_lock()/spin_unlock()
523 * functions which must match the way they are locked/unlocked outside
526 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
527 * set thus when other process waits process on the list if this
528 * process is awaken further processes are not considered.
530 * wake_up_locked() has to be called after changing any variable that could
531 * change the result of the wait condition.
533 * The function will return -ERESTARTSYS if it was interrupted by a
534 * signal and 0 if @condition evaluated to true.
536 #define wait_event_interruptible_exclusive_locked(wq, condition) \
538 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
541 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
542 * @wq: the waitqueue to wait on
543 * @condition: a C expression for the event to wait for
545 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
546 * @condition evaluates to true or a signal is received.
547 * The @condition is checked each time the waitqueue @wq is woken up.
549 * It must be called with wq.lock being held. This spinlock is
550 * unlocked while sleeping but @condition testing is done while lock
551 * is held and when this macro exits the lock is held.
553 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
554 * functions which must match the way they are locked/unlocked outside
557 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
558 * set thus when other process waits process on the list if this
559 * process is awaken further processes are not considered.
561 * wake_up_locked() has to be called after changing any variable that could
562 * change the result of the wait condition.
564 * The function will return -ERESTARTSYS if it was interrupted by a
565 * signal and 0 if @condition evaluated to true.
567 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
569 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
573 #define __wait_event_killable(wq, condition) \
574 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
577 * wait_event_killable - sleep until a condition gets true
578 * @wq: the waitqueue to wait on
579 * @condition: a C expression for the event to wait for
581 * The process is put to sleep (TASK_KILLABLE) until the
582 * @condition evaluates to true or a signal is received.
583 * The @condition is checked each time the waitqueue @wq is woken up.
585 * wake_up() has to be called after changing any variable that could
586 * change the result of the wait condition.
588 * The function will return -ERESTARTSYS if it was interrupted by a
589 * signal and 0 if @condition evaluated to true.
591 #define wait_event_killable(wq, condition) \
595 __ret = __wait_event_killable(wq, condition); \
600 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
601 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
602 spin_unlock_irq(&lock); \
605 spin_lock_irq(&lock))
608 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
609 * condition is checked under the lock. This
610 * is expected to be called with the lock
612 * @wq: the waitqueue to wait on
613 * @condition: a C expression for the event to wait for
614 * @lock: a locked spinlock_t, which will be released before cmd
615 * and schedule() and reacquired afterwards.
616 * @cmd: a command which is invoked outside the critical section before
619 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
620 * @condition evaluates to true. The @condition is checked each time
621 * the waitqueue @wq is woken up.
623 * wake_up() has to be called after changing any variable that could
624 * change the result of the wait condition.
626 * This is supposed to be called while holding the lock. The lock is
627 * dropped before invoking the cmd and going to sleep and is reacquired
630 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
634 __wait_event_lock_irq(wq, condition, lock, cmd); \
638 * wait_event_lock_irq - sleep until a condition gets true. The
639 * condition is checked under the lock. This
640 * is expected to be called with the lock
642 * @wq: the waitqueue to wait on
643 * @condition: a C expression for the event to wait for
644 * @lock: a locked spinlock_t, which will be released before schedule()
645 * and reacquired afterwards.
647 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
648 * @condition evaluates to true. The @condition is checked each time
649 * the waitqueue @wq is woken up.
651 * wake_up() has to be called after changing any variable that could
652 * change the result of the wait condition.
654 * This is supposed to be called while holding the lock. The lock is
655 * dropped before going to sleep and is reacquired afterwards.
657 #define wait_event_lock_irq(wq, condition, lock) \
661 __wait_event_lock_irq(wq, condition, lock, ); \
665 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
666 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
667 spin_unlock_irq(&lock); \
670 spin_lock_irq(&lock))
673 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
674 * The condition is checked under the lock. This is expected to
675 * be called with the lock taken.
676 * @wq: the waitqueue to wait on
677 * @condition: a C expression for the event to wait for
678 * @lock: a locked spinlock_t, which will be released before cmd and
679 * schedule() and reacquired afterwards.
680 * @cmd: a command which is invoked outside the critical section before
683 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
684 * @condition evaluates to true or a signal is received. The @condition is
685 * checked each time the waitqueue @wq is woken up.
687 * wake_up() has to be called after changing any variable that could
688 * change the result of the wait condition.
690 * This is supposed to be called while holding the lock. The lock is
691 * dropped before invoking the cmd and going to sleep and is reacquired
694 * The macro will return -ERESTARTSYS if it was interrupted by a signal
695 * and 0 if @condition evaluated to true.
697 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
701 __ret = __wait_event_interruptible_lock_irq(wq, \
702 condition, lock, cmd); \
707 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
708 * The condition is checked under the lock. This is expected
709 * to be called with the lock taken.
710 * @wq: the waitqueue to wait on
711 * @condition: a C expression for the event to wait for
712 * @lock: a locked spinlock_t, which will be released before schedule()
713 * and reacquired afterwards.
715 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
716 * @condition evaluates to true or signal is received. The @condition is
717 * checked each time the waitqueue @wq is woken up.
719 * wake_up() has to be called after changing any variable that could
720 * change the result of the wait condition.
722 * This is supposed to be called while holding the lock. The lock is
723 * dropped before going to sleep and is reacquired afterwards.
725 * The macro will return -ERESTARTSYS if it was interrupted by a signal
726 * and 0 if @condition evaluated to true.
728 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
732 __ret = __wait_event_interruptible_lock_irq(wq, \
737 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
739 ___wait_event(wq, ___wait_cond_timeout(condition), \
740 TASK_INTERRUPTIBLE, 0, ret, \
741 spin_unlock_irq(&lock); \
742 __ret = schedule_timeout(__ret); \
743 spin_lock_irq(&lock));
746 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
747 * The condition is checked under the lock. This is expected
748 * to be called with the lock taken.
749 * @wq: the waitqueue to wait on
750 * @condition: a C expression for the event to wait for
751 * @lock: a locked spinlock_t, which will be released before schedule()
752 * and reacquired afterwards.
753 * @timeout: timeout, in jiffies
755 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
756 * @condition evaluates to true or signal is received. The @condition is
757 * checked each time the waitqueue @wq is woken up.
759 * wake_up() has to be called after changing any variable that could
760 * change the result of the wait condition.
762 * This is supposed to be called while holding the lock. The lock is
763 * dropped before going to sleep and is reacquired afterwards.
765 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
766 * was interrupted by a signal, and the remaining jiffies otherwise
767 * if the condition evaluated to true before the timeout elapsed.
769 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
772 long __ret = timeout; \
774 __ret = __wait_event_interruptible_lock_irq_timeout( \
775 wq, condition, lock, timeout); \
781 * These are the old interfaces to sleep waiting for an event.
782 * They are racy. DO NOT use them, use the wait_event* interfaces above.
783 * We plan to remove these interfaces.
785 extern void sleep_on(wait_queue_head_t *q);
786 extern long sleep_on_timeout(wait_queue_head_t *q,
787 signed long timeout);
788 extern void interruptible_sleep_on(wait_queue_head_t *q);
789 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
790 signed long timeout);
793 * Waitqueues which are removed from the waitqueue_head at wakeup time
795 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
796 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
797 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
798 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
799 unsigned int mode, void *key);
800 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
801 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
803 #define DEFINE_WAIT_FUNC(name, function) \
804 wait_queue_t name = { \
805 .private = current, \
807 .task_list = LIST_HEAD_INIT((name).task_list), \
810 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
812 #define DEFINE_WAIT_BIT(name, word, bit) \
813 struct wait_bit_queue name = { \
814 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
816 .private = current, \
817 .func = wake_bit_function, \
819 LIST_HEAD_INIT((name).wait.task_list), \
823 #define init_wait(wait) \
825 (wait)->private = current; \
826 (wait)->func = autoremove_wake_function; \
827 INIT_LIST_HEAD(&(wait)->task_list); \
832 * wait_on_bit - wait for a bit to be cleared
833 * @word: the word being waited on, a kernel virtual address
834 * @bit: the bit of the word being waited on
835 * @action: the function used to sleep, which may take special actions
836 * @mode: the task state to sleep in
838 * There is a standard hashed waitqueue table for generic use. This
839 * is the part of the hashtable's accessor API that waits on a bit.
840 * For instance, if one were to have waiters on a bitflag, one would
841 * call wait_on_bit() in threads waiting for the bit to clear.
842 * One uses wait_on_bit() where one is waiting for the bit to clear,
843 * but has no intention of setting it.
845 static inline int wait_on_bit(void *word, int bit,
846 int (*action)(void *), unsigned mode)
848 if (!test_bit(bit, word))
850 return out_of_line_wait_on_bit(word, bit, action, mode);
854 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
855 * @word: the word being waited on, a kernel virtual address
856 * @bit: the bit of the word being waited on
857 * @action: the function used to sleep, which may take special actions
858 * @mode: the task state to sleep in
860 * There is a standard hashed waitqueue table for generic use. This
861 * is the part of the hashtable's accessor API that waits on a bit
862 * when one intends to set it, for instance, trying to lock bitflags.
863 * For instance, if one were to have waiters trying to set bitflag
864 * and waiting for it to clear before setting it, one would call
865 * wait_on_bit() in threads waiting to be able to set the bit.
866 * One uses wait_on_bit_lock() where one is waiting for the bit to
867 * clear with the intention of setting it, and when done, clearing it.
869 static inline int wait_on_bit_lock(void *word, int bit,
870 int (*action)(void *), unsigned mode)
872 if (!test_and_set_bit(bit, word))
874 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
878 * wait_on_atomic_t - Wait for an atomic_t to become 0
879 * @val: The atomic value being waited on, a kernel virtual address
880 * @action: the function used to sleep, which may take special actions
881 * @mode: the task state to sleep in
883 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
884 * the purpose of getting a waitqueue, but we set the key to a bit number
885 * outside of the target 'word'.
888 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
890 if (atomic_read(val) == 0)
892 return out_of_line_wait_on_atomic_t(val, action, mode);