1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Linux wait queue related types and methods
7 #include <linux/list.h>
8 #include <linux/stddef.h>
9 #include <linux/spinlock.h>
11 #include <asm/current.h>
12 #include <uapi/linux/wait.h>
14 typedef struct wait_queue_entry wait_queue_entry_t;
16 typedef int (*wait_queue_func_t)(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
17 int default_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
19 /* wait_queue_entry::flags */
20 #define WQ_FLAG_EXCLUSIVE 0x01
21 #define WQ_FLAG_WOKEN 0x02
22 #define WQ_FLAG_BOOKMARK 0x04
25 * A single wait-queue entry structure:
27 struct wait_queue_entry {
30 wait_queue_func_t func;
31 struct list_head entry;
34 struct wait_queue_head {
36 struct list_head head;
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 .entry = { NULL, NULL } }
51 #define DECLARE_WAITQUEUE(name, tsk) \
52 struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
54 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
55 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
56 .head = { &(name).head, &(name).head } }
58 #define DECLARE_WAIT_QUEUE_HEAD(name) \
59 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
61 extern void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *);
63 #define init_waitqueue_head(wq_head) \
65 static struct lock_class_key __key; \
67 __init_waitqueue_head((wq_head), #wq_head, &__key); \
71 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
72 ({ init_waitqueue_head(&name); name; })
73 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
74 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
76 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
79 static inline void init_waitqueue_entry(struct wait_queue_entry *wq_entry, struct task_struct *p)
82 wq_entry->private = p;
83 wq_entry->func = default_wake_function;
87 init_waitqueue_func_entry(struct wait_queue_entry *wq_entry, wait_queue_func_t func)
90 wq_entry->private = NULL;
91 wq_entry->func = func;
95 * waitqueue_active -- locklessly test for waiters on the queue
96 * @wq_head: the waitqueue to test for waiters
98 * returns true if the wait list is not empty
100 * NOTE: this function is lockless and requires care, incorrect usage _will_
101 * lead to sporadic and non-obvious failure.
103 * Use either while holding wait_queue_head::lock or when used for wakeups
104 * with an extra smp_mb() like:
106 * CPU0 - waker CPU1 - waiter
109 * @cond = true; prepare_to_wait(&wq_head, &wait, state);
110 * smp_mb(); // smp_mb() from set_current_state()
111 * if (waitqueue_active(wq_head)) if (@cond)
112 * wake_up(wq_head); break;
115 * finish_wait(&wq_head, &wait);
117 * Because without the explicit smp_mb() it's possible for the
118 * waitqueue_active() load to get hoisted over the @cond store such that we'll
119 * observe an empty wait list while the waiter might not observe @cond.
121 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
122 * which (when the lock is uncontended) are of roughly equal cost.
124 static inline int waitqueue_active(struct wait_queue_head *wq_head)
126 return !list_empty(&wq_head->head);
130 * wq_has_sleeper - check if there are any waiting processes
131 * @wq_head: wait queue head
133 * Returns true if wq_head has waiting processes
135 * Please refer to the comment for waitqueue_active.
137 static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
140 * We need to be sure we are in sync with the
141 * add_wait_queue modifications to the wait queue.
143 * This memory barrier should be paired with one on the
147 return waitqueue_active(wq_head);
150 extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
151 extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
152 extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
154 static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
156 list_add(&wq_entry->entry, &wq_head->head);
160 * Used for wake-one threads:
163 __add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
165 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
166 __add_wait_queue(wq_head, wq_entry);
169 static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
171 list_add_tail(&wq_entry->entry, &wq_head->head);
175 __add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
177 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
178 __add_wait_queue_entry_tail(wq_head, wq_entry);
182 __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
184 list_del(&wq_entry->entry);
187 void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
188 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
189 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
190 unsigned int mode, void *key, wait_queue_entry_t *bookmark);
191 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
192 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
193 void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr);
194 void __wake_up_pollfree(struct wait_queue_head *wq_head);
196 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
197 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
198 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
199 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
200 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
202 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
203 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
204 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
205 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
208 * Wakeup macros to be used to report events to the targets.
210 #define wake_up_poll(x, m) \
211 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
212 #define wake_up_locked_poll(x, m) \
213 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
214 #define wake_up_interruptible_poll(x, m) \
215 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
216 #define wake_up_interruptible_sync_poll(x, m) \
217 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
220 * wake_up_pollfree - signal that a polled waitqueue is going away
221 * @wq_head: the wait queue head
223 * In the very rare cases where a ->poll() implementation uses a waitqueue whose
224 * lifetime is tied to a task rather than to the 'struct file' being polled,
225 * this function must be called before the waitqueue is freed so that
226 * non-blocking polls (e.g. epoll) are notified that the queue is going away.
228 * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via
229 * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU.
231 static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
234 * For performance reasons, we don't always take the queue lock here.
235 * Therefore, we might race with someone removing the last entry from
236 * the queue, and proceed while they still hold the queue lock.
237 * However, rcu_read_lock() is required to be held in such cases, so we
238 * can safely proceed with an RCU-delayed free.
240 if (waitqueue_active(wq_head))
241 __wake_up_pollfree(wq_head);
244 #define ___wait_cond_timeout(condition) \
246 bool __cond = (condition); \
247 if (__cond && !__ret) \
252 #define ___wait_is_interruptible(state) \
253 (!__builtin_constant_p(state) || \
254 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
256 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
259 * The below macro ___wait_event() has an explicit shadow of the __ret
260 * variable when used from the wait_event_*() macros.
262 * This is so that both can use the ___wait_cond_timeout() construct
263 * to wrap the condition.
265 * The type inconsistency of the wait_event_*() __ret variable is also
266 * on purpose; we use long where we can return timeout values and int
270 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
273 struct wait_queue_entry __wq_entry; \
274 long __ret = ret; /* explicit shadow */ \
276 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
278 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
283 if (___wait_is_interruptible(state) && __int) { \
290 finish_wait(&wq_head, &__wq_entry); \
294 #define __wait_event(wq_head, condition) \
295 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
299 * wait_event - sleep until a condition gets true
300 * @wq_head: the waitqueue to wait on
301 * @condition: a C expression for the event to wait for
303 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
304 * @condition evaluates to true. The @condition is checked each time
305 * the waitqueue @wq_head is woken up.
307 * wake_up() has to be called after changing any variable that could
308 * change the result of the wait condition.
310 #define wait_event(wq_head, condition) \
315 __wait_event(wq_head, condition); \
318 #define __io_wait_event(wq_head, condition) \
319 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
323 * io_wait_event() -- like wait_event() but with io_schedule()
325 #define io_wait_event(wq_head, condition) \
330 __io_wait_event(wq_head, condition); \
333 #define __wait_event_freezable(wq_head, condition) \
334 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
335 schedule(); try_to_freeze())
338 * wait_event_freezable - sleep (or freeze) until a condition gets true
339 * @wq_head: the waitqueue to wait on
340 * @condition: a C expression for the event to wait for
342 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
343 * to system load) until the @condition evaluates to true. The
344 * @condition is checked each time the waitqueue @wq_head is woken up.
346 * wake_up() has to be called after changing any variable that could
347 * change the result of the wait condition.
349 #define wait_event_freezable(wq_head, condition) \
354 __ret = __wait_event_freezable(wq_head, condition); \
358 #define __wait_event_timeout(wq_head, condition, timeout) \
359 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
360 TASK_UNINTERRUPTIBLE, 0, timeout, \
361 __ret = schedule_timeout(__ret))
364 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
365 * @wq_head: the waitqueue to wait on
366 * @condition: a C expression for the event to wait for
367 * @timeout: timeout, in jiffies
369 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
370 * @condition evaluates to true. The @condition is checked each time
371 * the waitqueue @wq_head is woken up.
373 * wake_up() has to be called after changing any variable that could
374 * change the result of the wait condition.
377 * 0 if the @condition evaluated to %false after the @timeout elapsed,
378 * 1 if the @condition evaluated to %true after the @timeout elapsed,
379 * or the remaining jiffies (at least 1) if the @condition evaluated
380 * to %true before the @timeout elapsed.
382 #define wait_event_timeout(wq_head, condition, timeout) \
384 long __ret = timeout; \
386 if (!___wait_cond_timeout(condition)) \
387 __ret = __wait_event_timeout(wq_head, condition, timeout); \
391 #define __wait_event_freezable_timeout(wq_head, condition, timeout) \
392 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
393 TASK_INTERRUPTIBLE, 0, timeout, \
394 __ret = schedule_timeout(__ret); try_to_freeze())
397 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
398 * increasing load and is freezable.
400 #define wait_event_freezable_timeout(wq_head, condition, timeout) \
402 long __ret = timeout; \
404 if (!___wait_cond_timeout(condition)) \
405 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
409 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
410 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
411 cmd1; schedule(); cmd2)
413 * Just like wait_event_cmd(), except it sets exclusive flag
415 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
419 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
422 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
423 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
424 cmd1; schedule(); cmd2)
427 * wait_event_cmd - sleep until a condition gets true
428 * @wq_head: the waitqueue to wait on
429 * @condition: a C expression for the event to wait for
430 * @cmd1: the command will be executed before sleep
431 * @cmd2: the command will be executed after sleep
433 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
434 * @condition evaluates to true. The @condition is checked each time
435 * the waitqueue @wq_head is woken up.
437 * wake_up() has to be called after changing any variable that could
438 * change the result of the wait condition.
440 #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
444 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
447 #define __wait_event_interruptible(wq_head, condition) \
448 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
452 * wait_event_interruptible - sleep until a condition gets true
453 * @wq_head: the waitqueue to wait on
454 * @condition: a C expression for the event to wait for
456 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
457 * @condition evaluates to true or a signal is received.
458 * The @condition is checked each time the waitqueue @wq_head is woken up.
460 * wake_up() has to be called after changing any variable that could
461 * change the result of the wait condition.
463 * The function will return -ERESTARTSYS if it was interrupted by a
464 * signal and 0 if @condition evaluated to true.
466 #define wait_event_interruptible(wq_head, condition) \
471 __ret = __wait_event_interruptible(wq_head, condition); \
475 #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
476 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
477 TASK_INTERRUPTIBLE, 0, timeout, \
478 __ret = schedule_timeout(__ret))
481 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
482 * @wq_head: the waitqueue to wait on
483 * @condition: a C expression for the event to wait for
484 * @timeout: timeout, in jiffies
486 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
487 * @condition evaluates to true or a signal is received.
488 * The @condition is checked each time the waitqueue @wq_head is woken up.
490 * wake_up() has to be called after changing any variable that could
491 * change the result of the wait condition.
494 * 0 if the @condition evaluated to %false after the @timeout elapsed,
495 * 1 if the @condition evaluated to %true after the @timeout elapsed,
496 * the remaining jiffies (at least 1) if the @condition evaluated
497 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
498 * interrupted by a signal.
500 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \
502 long __ret = timeout; \
504 if (!___wait_cond_timeout(condition)) \
505 __ret = __wait_event_interruptible_timeout(wq_head, \
506 condition, timeout); \
510 #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
513 struct hrtimer_sleeper __t; \
515 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); \
516 hrtimer_init_sleeper(&__t, current); \
517 if ((timeout) != KTIME_MAX) \
518 hrtimer_start_range_ns(&__t.timer, timeout, \
519 current->timer_slack_ns, \
522 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
529 hrtimer_cancel(&__t.timer); \
530 destroy_hrtimer_on_stack(&__t.timer); \
535 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
536 * @wq_head: the waitqueue to wait on
537 * @condition: a C expression for the event to wait for
538 * @timeout: timeout, as a ktime_t
540 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
541 * @condition evaluates to true or a signal is received.
542 * The @condition is checked each time the waitqueue @wq_head is woken up.
544 * wake_up() has to be called after changing any variable that could
545 * change the result of the wait condition.
547 * The function returns 0 if @condition became true, or -ETIME if the timeout
550 #define wait_event_hrtimeout(wq_head, condition, timeout) \
555 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
556 TASK_UNINTERRUPTIBLE); \
561 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
562 * @wq: the waitqueue to wait on
563 * @condition: a C expression for the event to wait for
564 * @timeout: timeout, as a ktime_t
566 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
567 * @condition evaluates to true or a signal is received.
568 * The @condition is checked each time the waitqueue @wq is woken up.
570 * wake_up() has to be called after changing any variable that could
571 * change the result of the wait condition.
573 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
574 * interrupted by a signal, or -ETIME if the timeout elapsed.
576 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
581 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
582 TASK_INTERRUPTIBLE); \
586 #define __wait_event_interruptible_exclusive(wq, condition) \
587 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
590 #define wait_event_interruptible_exclusive(wq, condition) \
595 __ret = __wait_event_interruptible_exclusive(wq, condition); \
599 #define __wait_event_killable_exclusive(wq, condition) \
600 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
603 #define wait_event_killable_exclusive(wq, condition) \
608 __ret = __wait_event_killable_exclusive(wq, condition); \
613 #define __wait_event_freezable_exclusive(wq, condition) \
614 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
615 schedule(); try_to_freeze())
617 #define wait_event_freezable_exclusive(wq, condition) \
622 __ret = __wait_event_freezable_exclusive(wq, condition); \
626 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
627 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
629 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
632 DEFINE_WAIT(__wait); \
634 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
636 __ret = fn(&(wq), &__wait); \
639 } while (!(condition)); \
640 __remove_wait_queue(&(wq), &__wait); \
641 __set_current_state(TASK_RUNNING); \
647 * wait_event_interruptible_locked - sleep until a condition gets true
648 * @wq: the waitqueue to wait on
649 * @condition: a C expression for the event to wait for
651 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
652 * @condition evaluates to true or a signal is received.
653 * The @condition is checked each time the waitqueue @wq is woken up.
655 * It must be called with wq.lock being held. This spinlock is
656 * unlocked while sleeping but @condition testing is done while lock
657 * is held and when this macro exits the lock is held.
659 * The lock is locked/unlocked using spin_lock()/spin_unlock()
660 * functions which must match the way they are locked/unlocked outside
663 * wake_up_locked() has to be called after changing any variable that could
664 * change the result of the wait condition.
666 * The function will return -ERESTARTSYS if it was interrupted by a
667 * signal and 0 if @condition evaluated to true.
669 #define wait_event_interruptible_locked(wq, condition) \
671 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
674 * wait_event_interruptible_locked_irq - sleep until a condition gets true
675 * @wq: the waitqueue to wait on
676 * @condition: a C expression for the event to wait for
678 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
679 * @condition evaluates to true or a signal is received.
680 * The @condition is checked each time the waitqueue @wq is woken up.
682 * It must be called with wq.lock being held. This spinlock is
683 * unlocked while sleeping but @condition testing is done while lock
684 * is held and when this macro exits the lock is held.
686 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
687 * functions which must match the way they are locked/unlocked outside
690 * wake_up_locked() has to be called after changing any variable that could
691 * change the result of the wait condition.
693 * The function will return -ERESTARTSYS if it was interrupted by a
694 * signal and 0 if @condition evaluated to true.
696 #define wait_event_interruptible_locked_irq(wq, condition) \
698 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
701 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
702 * @wq: the waitqueue to wait on
703 * @condition: a C expression for the event to wait for
705 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
706 * @condition evaluates to true or a signal is received.
707 * The @condition is checked each time the waitqueue @wq is woken up.
709 * It must be called with wq.lock being held. This spinlock is
710 * unlocked while sleeping but @condition testing is done while lock
711 * is held and when this macro exits the lock is held.
713 * The lock is locked/unlocked using spin_lock()/spin_unlock()
714 * functions which must match the way they are locked/unlocked outside
717 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
718 * set thus when other process waits process on the list if this
719 * process is awaken further processes are not considered.
721 * wake_up_locked() has to be called after changing any variable that could
722 * change the result of the wait condition.
724 * The function will return -ERESTARTSYS if it was interrupted by a
725 * signal and 0 if @condition evaluated to true.
727 #define wait_event_interruptible_exclusive_locked(wq, condition) \
729 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
732 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
733 * @wq: the waitqueue to wait on
734 * @condition: a C expression for the event to wait for
736 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
737 * @condition evaluates to true or a signal is received.
738 * The @condition is checked each time the waitqueue @wq is woken up.
740 * It must be called with wq.lock being held. This spinlock is
741 * unlocked while sleeping but @condition testing is done while lock
742 * is held and when this macro exits the lock is held.
744 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
745 * functions which must match the way they are locked/unlocked outside
748 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
749 * set thus when other process waits process on the list if this
750 * process is awaken further processes are not considered.
752 * wake_up_locked() has to be called after changing any variable that could
753 * change the result of the wait condition.
755 * The function will return -ERESTARTSYS if it was interrupted by a
756 * signal and 0 if @condition evaluated to true.
758 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
760 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
763 #define __wait_event_killable(wq, condition) \
764 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
767 * wait_event_killable - sleep until a condition gets true
768 * @wq_head: the waitqueue to wait on
769 * @condition: a C expression for the event to wait for
771 * The process is put to sleep (TASK_KILLABLE) until the
772 * @condition evaluates to true or a signal is received.
773 * The @condition is checked each time the waitqueue @wq_head is woken up.
775 * wake_up() has to be called after changing any variable that could
776 * change the result of the wait condition.
778 * The function will return -ERESTARTSYS if it was interrupted by a
779 * signal and 0 if @condition evaluated to true.
781 #define wait_event_killable(wq_head, condition) \
786 __ret = __wait_event_killable(wq_head, condition); \
790 #define __wait_event_killable_timeout(wq_head, condition, timeout) \
791 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
792 TASK_KILLABLE, 0, timeout, \
793 __ret = schedule_timeout(__ret))
796 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
797 * @wq_head: the waitqueue to wait on
798 * @condition: a C expression for the event to wait for
799 * @timeout: timeout, in jiffies
801 * The process is put to sleep (TASK_KILLABLE) until the
802 * @condition evaluates to true or a kill signal is received.
803 * The @condition is checked each time the waitqueue @wq_head is woken up.
805 * wake_up() has to be called after changing any variable that could
806 * change the result of the wait condition.
809 * 0 if the @condition evaluated to %false after the @timeout elapsed,
810 * 1 if the @condition evaluated to %true after the @timeout elapsed,
811 * the remaining jiffies (at least 1) if the @condition evaluated
812 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
813 * interrupted by a kill signal.
815 * Only kill signals interrupt this process.
817 #define wait_event_killable_timeout(wq_head, condition, timeout) \
819 long __ret = timeout; \
821 if (!___wait_cond_timeout(condition)) \
822 __ret = __wait_event_killable_timeout(wq_head, \
823 condition, timeout); \
828 #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
829 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
830 spin_unlock_irq(&lock); \
833 spin_lock_irq(&lock))
836 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
837 * condition is checked under the lock. This
838 * is expected to be called with the lock
840 * @wq_head: 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 cmd
843 * and schedule() and reacquired afterwards.
844 * @cmd: a command which is invoked outside the critical section before
847 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
848 * @condition evaluates to true. The @condition is checked each time
849 * the waitqueue @wq_head is woken up.
851 * wake_up() has to be called after changing any variable that could
852 * change the result of the wait condition.
854 * This is supposed to be called while holding the lock. The lock is
855 * dropped before invoking the cmd and going to sleep and is reacquired
858 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
862 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
866 * wait_event_lock_irq - sleep until a condition gets true. The
867 * condition is checked under the lock. This
868 * is expected to be called with the lock
870 * @wq_head: the waitqueue to wait on
871 * @condition: a C expression for the event to wait for
872 * @lock: a locked spinlock_t, which will be released before schedule()
873 * and reacquired afterwards.
875 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
876 * @condition evaluates to true. The @condition is checked each time
877 * the waitqueue @wq_head is woken up.
879 * wake_up() has to be called after changing any variable that could
880 * change the result of the wait condition.
882 * This is supposed to be called while holding the lock. The lock is
883 * dropped before going to sleep and is reacquired afterwards.
885 #define wait_event_lock_irq(wq_head, condition, lock) \
889 __wait_event_lock_irq(wq_head, condition, lock, ); \
893 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
894 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
895 spin_unlock_irq(&lock); \
898 spin_lock_irq(&lock))
901 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
902 * The condition is checked under the lock. This is expected to
903 * be called with the lock taken.
904 * @wq_head: the waitqueue to wait on
905 * @condition: a C expression for the event to wait for
906 * @lock: a locked spinlock_t, which will be released before cmd and
907 * schedule() and reacquired afterwards.
908 * @cmd: a command which is invoked outside the critical section before
911 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
912 * @condition evaluates to true or a signal is received. The @condition is
913 * checked each time the waitqueue @wq_head is woken up.
915 * wake_up() has to be called after changing any variable that could
916 * change the result of the wait condition.
918 * This is supposed to be called while holding the lock. The lock is
919 * dropped before invoking the cmd and going to sleep and is reacquired
922 * The macro will return -ERESTARTSYS if it was interrupted by a signal
923 * and 0 if @condition evaluated to true.
925 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
929 __ret = __wait_event_interruptible_lock_irq(wq_head, \
930 condition, lock, cmd); \
935 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
936 * The condition is checked under the lock. This is expected
937 * to be called with the lock taken.
938 * @wq_head: the waitqueue to wait on
939 * @condition: a C expression for the event to wait for
940 * @lock: a locked spinlock_t, which will be released before schedule()
941 * and reacquired afterwards.
943 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
944 * @condition evaluates to true or signal is received. The @condition is
945 * checked each time the waitqueue @wq_head is woken up.
947 * wake_up() has to be called after changing any variable that could
948 * change the result of the wait condition.
950 * This is supposed to be called while holding the lock. The lock is
951 * dropped before going to sleep and is reacquired afterwards.
953 * The macro will return -ERESTARTSYS if it was interrupted by a signal
954 * and 0 if @condition evaluated to true.
956 #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
960 __ret = __wait_event_interruptible_lock_irq(wq_head, \
965 #define __wait_event_interruptible_lock_irq_timeout(wq_head, condition, \
967 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
968 TASK_INTERRUPTIBLE, 0, timeout, \
969 spin_unlock_irq(&lock); \
970 __ret = schedule_timeout(__ret); \
971 spin_lock_irq(&lock));
974 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
975 * true or a timeout elapses. The condition is checked under
976 * the lock. This is expected to be called with the lock taken.
977 * @wq_head: the waitqueue to wait on
978 * @condition: a C expression for the event to wait for
979 * @lock: a locked spinlock_t, which will be released before schedule()
980 * and reacquired afterwards.
981 * @timeout: timeout, in jiffies
983 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
984 * @condition evaluates to true or signal is received. The @condition is
985 * checked each time the waitqueue @wq_head is woken up.
987 * wake_up() has to be called after changing any variable that could
988 * change the result of the wait condition.
990 * This is supposed to be called while holding the lock. The lock is
991 * dropped before going to sleep and is reacquired afterwards.
993 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
994 * was interrupted by a signal, and the remaining jiffies otherwise
995 * if the condition evaluated to true before the timeout elapsed.
997 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1000 long __ret = timeout; \
1001 if (!___wait_cond_timeout(condition)) \
1002 __ret = __wait_event_interruptible_lock_irq_timeout( \
1003 wq_head, condition, lock, timeout); \
1008 * Waitqueues which are removed from the waitqueue_head at wakeup time
1010 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1011 void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1012 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1013 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1014 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1015 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1016 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1018 #define DEFINE_WAIT_FUNC(name, function) \
1019 struct wait_queue_entry name = { \
1020 .private = current, \
1022 .entry = LIST_HEAD_INIT((name).entry), \
1025 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1027 #define init_wait(wait) \
1029 (wait)->private = current; \
1030 (wait)->func = autoremove_wake_function; \
1031 INIT_LIST_HEAD(&(wait)->entry); \
1032 (wait)->flags = 0; \
1035 #endif /* _LINUX_WAIT_H */