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, ret) \
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_nop_ret int ret __always_unused
196 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
199 DEFINE_WAIT(__wait); \
203 prepare_to_wait_exclusive(&wq, &__wait, state); \
205 prepare_to_wait(&wq, &__wait, state); \
210 if (___wait_signal_pending(state)) { \
211 ret = -ERESTARTSYS; \
213 abort_exclusive_wait(&wq, &__wait, \
222 finish_wait(&wq, &__wait); \
226 #define __wait_event(wq, condition) \
227 ___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
228 ___wait_nop_ret, schedule())
231 * wait_event - sleep until a condition gets true
232 * @wq: the waitqueue to wait on
233 * @condition: a C expression for the event to wait for
235 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
236 * @condition evaluates to true. The @condition is checked each time
237 * the waitqueue @wq is woken up.
239 * wake_up() has to be called after changing any variable that could
240 * change the result of the wait condition.
242 #define wait_event(wq, condition) \
246 __wait_event(wq, condition); \
249 #define __wait_event_timeout(wq, condition, ret) \
250 ___wait_event(wq, ___wait_cond_timeout(condition, ret), \
251 TASK_UNINTERRUPTIBLE, 0, ret, \
252 ret = schedule_timeout(ret))
255 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
256 * @wq: the waitqueue to wait on
257 * @condition: a C expression for the event to wait for
258 * @timeout: timeout, in jiffies
260 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
261 * @condition evaluates to true. The @condition is checked each time
262 * the waitqueue @wq is woken up.
264 * wake_up() has to be called after changing any variable that could
265 * change the result of the wait condition.
267 * The function returns 0 if the @timeout elapsed, or the remaining
268 * jiffies (at least 1) if the @condition evaluated to %true before
269 * the @timeout elapsed.
271 #define wait_event_timeout(wq, condition, timeout) \
273 long __ret = timeout; \
275 __wait_event_timeout(wq, condition, __ret); \
279 #define __wait_event_interruptible(wq, condition, ret) \
280 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, ret, \
284 * wait_event_interruptible - sleep until a condition gets true
285 * @wq: the waitqueue to wait on
286 * @condition: a C expression for the event to wait for
288 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
289 * @condition evaluates to true or a signal is received.
290 * The @condition is checked each time the waitqueue @wq is woken up.
292 * wake_up() has to be called after changing any variable that could
293 * change the result of the wait condition.
295 * The function will return -ERESTARTSYS if it was interrupted by a
296 * signal and 0 if @condition evaluated to true.
298 #define wait_event_interruptible(wq, condition) \
302 __wait_event_interruptible(wq, condition, __ret); \
306 #define __wait_event_interruptible_timeout(wq, condition, ret) \
307 ___wait_event(wq, ___wait_cond_timeout(condition, ret), \
308 TASK_INTERRUPTIBLE, 0, ret, \
309 ret = schedule_timeout(ret))
312 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
313 * @wq: the waitqueue to wait on
314 * @condition: a C expression for the event to wait for
315 * @timeout: timeout, in jiffies
317 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
318 * @condition evaluates to true or a signal is received.
319 * The @condition is checked each time the waitqueue @wq is woken up.
321 * wake_up() has to be called after changing any variable that could
322 * change the result of the wait condition.
325 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
326 * a signal, or the remaining jiffies (at least 1) if the @condition
327 * evaluated to %true before the @timeout elapsed.
329 #define wait_event_interruptible_timeout(wq, condition, timeout) \
331 long __ret = timeout; \
333 __wait_event_interruptible_timeout(wq, condition, __ret); \
337 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
340 DEFINE_WAIT(__wait); \
341 struct hrtimer_sleeper __t; \
343 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
345 hrtimer_init_sleeper(&__t, current); \
346 if ((timeout).tv64 != KTIME_MAX) \
347 hrtimer_start_range_ns(&__t.timer, timeout, \
348 current->timer_slack_ns, \
352 prepare_to_wait(&wq, &__wait, state); \
355 if (state == TASK_INTERRUPTIBLE && \
356 signal_pending(current)) { \
357 __ret = -ERESTARTSYS; \
367 hrtimer_cancel(&__t.timer); \
368 destroy_hrtimer_on_stack(&__t.timer); \
369 finish_wait(&wq, &__wait); \
374 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
375 * @wq: the waitqueue to wait on
376 * @condition: a C expression for the event to wait for
377 * @timeout: timeout, as a ktime_t
379 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
380 * @condition evaluates to true or a signal is received.
381 * The @condition is checked each time the waitqueue @wq is woken up.
383 * wake_up() has to be called after changing any variable that could
384 * change the result of the wait condition.
386 * The function returns 0 if @condition became true, or -ETIME if the timeout
389 #define wait_event_hrtimeout(wq, condition, timeout) \
393 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
394 TASK_UNINTERRUPTIBLE); \
399 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
400 * @wq: the waitqueue to wait on
401 * @condition: a C expression for the event to wait for
402 * @timeout: timeout, as a ktime_t
404 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
405 * @condition evaluates to true or a signal is received.
406 * The @condition is checked each time the waitqueue @wq is woken up.
408 * wake_up() has to be called after changing any variable that could
409 * change the result of the wait condition.
411 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
412 * interrupted by a signal, or -ETIME if the timeout elapsed.
414 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
418 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
419 TASK_INTERRUPTIBLE); \
423 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
424 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, ret, \
427 #define wait_event_interruptible_exclusive(wq, condition) \
431 __wait_event_interruptible_exclusive(wq, condition, __ret);\
436 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
439 DEFINE_WAIT(__wait); \
441 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
443 if (likely(list_empty(&__wait.task_list))) \
444 __add_wait_queue_tail(&(wq), &__wait); \
445 set_current_state(TASK_INTERRUPTIBLE); \
446 if (signal_pending(current)) { \
447 __ret = -ERESTARTSYS; \
451 spin_unlock_irq(&(wq).lock); \
453 spin_unlock(&(wq).lock); \
456 spin_lock_irq(&(wq).lock); \
458 spin_lock(&(wq).lock); \
459 } while (!(condition)); \
460 __remove_wait_queue(&(wq), &__wait); \
461 __set_current_state(TASK_RUNNING); \
467 * wait_event_interruptible_locked - sleep until a condition gets true
468 * @wq: the waitqueue to wait on
469 * @condition: a C expression for the event to wait for
471 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
472 * @condition evaluates to true or a signal is received.
473 * The @condition is checked each time the waitqueue @wq is woken up.
475 * It must be called with wq.lock being held. This spinlock is
476 * unlocked while sleeping but @condition testing is done while lock
477 * is held and when this macro exits the lock is held.
479 * The lock is locked/unlocked using spin_lock()/spin_unlock()
480 * functions which must match the way they are locked/unlocked outside
483 * wake_up_locked() has to be called after changing any variable that could
484 * change the result of the wait condition.
486 * The function will return -ERESTARTSYS if it was interrupted by a
487 * signal and 0 if @condition evaluated to true.
489 #define wait_event_interruptible_locked(wq, condition) \
491 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
494 * wait_event_interruptible_locked_irq - sleep until a condition gets true
495 * @wq: the waitqueue to wait on
496 * @condition: a C expression for the event to wait for
498 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
499 * @condition evaluates to true or a signal is received.
500 * The @condition is checked each time the waitqueue @wq is woken up.
502 * It must be called with wq.lock being held. This spinlock is
503 * unlocked while sleeping but @condition testing is done while lock
504 * is held and when this macro exits the lock is held.
506 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
507 * functions which must match the way they are locked/unlocked outside
510 * wake_up_locked() has to be called after changing any variable that could
511 * change the result of the wait condition.
513 * The function will return -ERESTARTSYS if it was interrupted by a
514 * signal and 0 if @condition evaluated to true.
516 #define wait_event_interruptible_locked_irq(wq, condition) \
518 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
521 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
522 * @wq: the waitqueue to wait on
523 * @condition: a C expression for the event to wait for
525 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
526 * @condition evaluates to true or a signal is received.
527 * The @condition is checked each time the waitqueue @wq is woken up.
529 * It must be called with wq.lock being held. This spinlock is
530 * unlocked while sleeping but @condition testing is done while lock
531 * is held and when this macro exits the lock is held.
533 * The lock is locked/unlocked using spin_lock()/spin_unlock()
534 * functions which must match the way they are locked/unlocked outside
537 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
538 * set thus when other process waits process on the list if this
539 * process is awaken further processes are not considered.
541 * wake_up_locked() has to be called after changing any variable that could
542 * change the result of the wait condition.
544 * The function will return -ERESTARTSYS if it was interrupted by a
545 * signal and 0 if @condition evaluated to true.
547 #define wait_event_interruptible_exclusive_locked(wq, condition) \
549 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
552 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
553 * @wq: the waitqueue to wait on
554 * @condition: a C expression for the event to wait for
556 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
557 * @condition evaluates to true or a signal is received.
558 * The @condition is checked each time the waitqueue @wq is woken up.
560 * It must be called with wq.lock being held. This spinlock is
561 * unlocked while sleeping but @condition testing is done while lock
562 * is held and when this macro exits the lock is held.
564 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
565 * functions which must match the way they are locked/unlocked outside
568 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
569 * set thus when other process waits process on the list if this
570 * process is awaken further processes are not considered.
572 * wake_up_locked() has to be called after changing any variable that could
573 * change the result of the wait condition.
575 * The function will return -ERESTARTSYS if it was interrupted by a
576 * signal and 0 if @condition evaluated to true.
578 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
580 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
584 #define __wait_event_killable(wq, condition, ret) \
585 ___wait_event(wq, condition, TASK_KILLABLE, 0, ret, schedule())
588 * wait_event_killable - sleep until a condition gets true
589 * @wq: the waitqueue to wait on
590 * @condition: a C expression for the event to wait for
592 * The process is put to sleep (TASK_KILLABLE) until the
593 * @condition evaluates to true or a signal is received.
594 * The @condition is checked each time the waitqueue @wq is woken up.
596 * wake_up() has to be called after changing any variable that could
597 * change the result of the wait condition.
599 * The function will return -ERESTARTSYS if it was interrupted by a
600 * signal and 0 if @condition evaluated to true.
602 #define wait_event_killable(wq, condition) \
606 __wait_event_killable(wq, condition, __ret); \
611 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
612 ___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
614 spin_unlock_irq(&lock); \
617 spin_lock_irq(&lock))
620 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
621 * condition is checked under the lock. This
622 * is expected to be called with the lock
624 * @wq: the waitqueue to wait on
625 * @condition: a C expression for the event to wait for
626 * @lock: a locked spinlock_t, which will be released before cmd
627 * and schedule() and reacquired afterwards.
628 * @cmd: a command which is invoked outside the critical section before
631 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
632 * @condition evaluates to true. The @condition is checked each time
633 * the waitqueue @wq is woken up.
635 * wake_up() has to be called after changing any variable that could
636 * change the result of the wait condition.
638 * This is supposed to be called while holding the lock. The lock is
639 * dropped before invoking the cmd and going to sleep and is reacquired
642 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
646 __wait_event_lock_irq(wq, condition, lock, cmd); \
650 * wait_event_lock_irq - sleep until a condition gets true. The
651 * condition is checked under the lock. This
652 * is expected to be called with the lock
654 * @wq: the waitqueue to wait on
655 * @condition: a C expression for the event to wait for
656 * @lock: a locked spinlock_t, which will be released before schedule()
657 * and reacquired afterwards.
659 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
660 * @condition evaluates to true. The @condition is checked each time
661 * the waitqueue @wq is woken up.
663 * wake_up() has to be called after changing any variable that could
664 * change the result of the wait condition.
666 * This is supposed to be called while holding the lock. The lock is
667 * dropped before going to sleep and is reacquired afterwards.
669 #define wait_event_lock_irq(wq, condition, lock) \
673 __wait_event_lock_irq(wq, condition, lock, ); \
677 #define __wait_event_interruptible_lock_irq(wq, condition, lock, ret, cmd) \
678 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, ret, \
679 spin_unlock_irq(&lock); \
682 spin_lock_irq(&lock))
685 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
686 * The condition is checked under the lock. This is expected to
687 * be called with the lock taken.
688 * @wq: the waitqueue to wait on
689 * @condition: a C expression for the event to wait for
690 * @lock: a locked spinlock_t, which will be released before cmd and
691 * schedule() and reacquired afterwards.
692 * @cmd: a command which is invoked outside the critical section before
695 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
696 * @condition evaluates to true or a signal is received. The @condition is
697 * checked each time the waitqueue @wq is woken up.
699 * wake_up() has to be called after changing any variable that could
700 * change the result of the wait condition.
702 * This is supposed to be called while holding the lock. The lock is
703 * dropped before invoking the cmd and going to sleep and is reacquired
706 * The macro will return -ERESTARTSYS if it was interrupted by a signal
707 * and 0 if @condition evaluated to true.
709 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
714 __wait_event_interruptible_lock_irq(wq, condition, \
720 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
721 * The condition is checked under the lock. This is expected
722 * to be called with the lock taken.
723 * @wq: the waitqueue to wait on
724 * @condition: a C expression for the event to wait for
725 * @lock: a locked spinlock_t, which will be released before schedule()
726 * and reacquired afterwards.
728 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
729 * @condition evaluates to true or signal is received. The @condition is
730 * checked each time the waitqueue @wq is woken up.
732 * wake_up() has to be called after changing any variable that could
733 * change the result of the wait condition.
735 * This is supposed to be called while holding the lock. The lock is
736 * dropped before going to sleep and is reacquired afterwards.
738 * The macro will return -ERESTARTSYS if it was interrupted by a signal
739 * and 0 if @condition evaluated to true.
741 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
746 __wait_event_interruptible_lock_irq(wq, condition, \
751 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, lock, ret) \
752 ___wait_event(wq, ___wait_cond_timeout(condition, ret), \
753 TASK_INTERRUPTIBLE, 0, ret, \
754 spin_unlock_irq(&lock); \
755 ret = schedule_timeout(ret); \
756 spin_lock_irq(&lock));
759 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
760 * The condition is checked under the lock. This is expected
761 * to be called with the lock taken.
762 * @wq: the waitqueue to wait on
763 * @condition: a C expression for the event to wait for
764 * @lock: a locked spinlock_t, which will be released before schedule()
765 * and reacquired afterwards.
766 * @timeout: timeout, in jiffies
768 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
769 * @condition evaluates to true or signal is received. The @condition is
770 * checked each time the waitqueue @wq is woken up.
772 * wake_up() has to be called after changing any variable that could
773 * change the result of the wait condition.
775 * This is supposed to be called while holding the lock. The lock is
776 * dropped before going to sleep and is reacquired afterwards.
778 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
779 * was interrupted by a signal, and the remaining jiffies otherwise
780 * if the condition evaluated to true before the timeout elapsed.
782 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
785 int __ret = timeout; \
788 __wait_event_interruptible_lock_irq_timeout( \
789 wq, condition, lock, __ret); \
795 * These are the old interfaces to sleep waiting for an event.
796 * They are racy. DO NOT use them, use the wait_event* interfaces above.
797 * We plan to remove these interfaces.
799 extern void sleep_on(wait_queue_head_t *q);
800 extern long sleep_on_timeout(wait_queue_head_t *q,
801 signed long timeout);
802 extern void interruptible_sleep_on(wait_queue_head_t *q);
803 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
804 signed long timeout);
807 * Waitqueues which are removed from the waitqueue_head at wakeup time
809 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
810 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
811 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
812 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
813 unsigned int mode, void *key);
814 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
815 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
817 #define DEFINE_WAIT_FUNC(name, function) \
818 wait_queue_t name = { \
819 .private = current, \
821 .task_list = LIST_HEAD_INIT((name).task_list), \
824 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
826 #define DEFINE_WAIT_BIT(name, word, bit) \
827 struct wait_bit_queue name = { \
828 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
830 .private = current, \
831 .func = wake_bit_function, \
833 LIST_HEAD_INIT((name).wait.task_list), \
837 #define init_wait(wait) \
839 (wait)->private = current; \
840 (wait)->func = autoremove_wake_function; \
841 INIT_LIST_HEAD(&(wait)->task_list); \
846 * wait_on_bit - wait for a bit to be cleared
847 * @word: the word being waited on, a kernel virtual address
848 * @bit: the bit of the word being waited on
849 * @action: the function used to sleep, which may take special actions
850 * @mode: the task state to sleep in
852 * There is a standard hashed waitqueue table for generic use. This
853 * is the part of the hashtable's accessor API that waits on a bit.
854 * For instance, if one were to have waiters on a bitflag, one would
855 * call wait_on_bit() in threads waiting for the bit to clear.
856 * One uses wait_on_bit() where one is waiting for the bit to clear,
857 * but has no intention of setting it.
859 static inline int wait_on_bit(void *word, int bit,
860 int (*action)(void *), unsigned mode)
862 if (!test_bit(bit, word))
864 return out_of_line_wait_on_bit(word, bit, action, mode);
868 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
869 * @word: the word being waited on, a kernel virtual address
870 * @bit: the bit of the word being waited on
871 * @action: the function used to sleep, which may take special actions
872 * @mode: the task state to sleep in
874 * There is a standard hashed waitqueue table for generic use. This
875 * is the part of the hashtable's accessor API that waits on a bit
876 * when one intends to set it, for instance, trying to lock bitflags.
877 * For instance, if one were to have waiters trying to set bitflag
878 * and waiting for it to clear before setting it, one would call
879 * wait_on_bit() in threads waiting to be able to set the bit.
880 * One uses wait_on_bit_lock() where one is waiting for the bit to
881 * clear with the intention of setting it, and when done, clearing it.
883 static inline int wait_on_bit_lock(void *word, int bit,
884 int (*action)(void *), unsigned mode)
886 if (!test_and_set_bit(bit, word))
888 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
892 * wait_on_atomic_t - Wait for an atomic_t to become 0
893 * @val: The atomic value being waited on, a kernel virtual address
894 * @action: the function used to sleep, which may take special actions
895 * @mode: the task state to sleep in
897 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
898 * the purpose of getting a waitqueue, but we set the key to a bit number
899 * outside of the target 'word'.
902 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
904 if (atomic_read(val) == 0)
906 return out_of_line_wait_on_atomic_t(val, action, mode);