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 poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
211 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
212 #define wake_up_poll(x, m) \
213 __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
214 #define wake_up_locked_poll(x, m) \
215 __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
216 #define wake_up_interruptible_poll(x, m) \
217 __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
218 #define wake_up_interruptible_sync_poll(x, m) \
219 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, poll_to_key(m))
222 * wake_up_pollfree - signal that a polled waitqueue is going away
223 * @wq_head: the wait queue head
225 * In the very rare cases where a ->poll() implementation uses a waitqueue whose
226 * lifetime is tied to a task rather than to the 'struct file' being polled,
227 * this function must be called before the waitqueue is freed so that
228 * non-blocking polls (e.g. epoll) are notified that the queue is going away.
230 * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via
231 * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU.
233 static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
236 * For performance reasons, we don't always take the queue lock here.
237 * Therefore, we might race with someone removing the last entry from
238 * the queue, and proceed while they still hold the queue lock.
239 * However, rcu_read_lock() is required to be held in such cases, so we
240 * can safely proceed with an RCU-delayed free.
242 if (waitqueue_active(wq_head))
243 __wake_up_pollfree(wq_head);
246 #define ___wait_cond_timeout(condition) \
248 bool __cond = (condition); \
249 if (__cond && !__ret) \
254 #define ___wait_is_interruptible(state) \
255 (!__builtin_constant_p(state) || \
256 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
258 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
261 * The below macro ___wait_event() has an explicit shadow of the __ret
262 * variable when used from the wait_event_*() macros.
264 * This is so that both can use the ___wait_cond_timeout() construct
265 * to wrap the condition.
267 * The type inconsistency of the wait_event_*() __ret variable is also
268 * on purpose; we use long where we can return timeout values and int
272 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
275 struct wait_queue_entry __wq_entry; \
276 long __ret = ret; /* explicit shadow */ \
278 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
280 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
285 if (___wait_is_interruptible(state) && __int) { \
292 finish_wait(&wq_head, &__wq_entry); \
296 #define __wait_event(wq_head, condition) \
297 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
301 * wait_event - sleep until a condition gets true
302 * @wq_head: the waitqueue to wait on
303 * @condition: a C expression for the event to wait for
305 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
306 * @condition evaluates to true. The @condition is checked each time
307 * the waitqueue @wq_head is woken up.
309 * wake_up() has to be called after changing any variable that could
310 * change the result of the wait condition.
312 #define wait_event(wq_head, condition) \
317 __wait_event(wq_head, condition); \
320 #define __io_wait_event(wq_head, condition) \
321 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
325 * io_wait_event() -- like wait_event() but with io_schedule()
327 #define io_wait_event(wq_head, condition) \
332 __io_wait_event(wq_head, condition); \
335 #define __wait_event_freezable(wq_head, condition) \
336 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
337 schedule(); try_to_freeze())
340 * wait_event_freezable - sleep (or freeze) until a condition gets true
341 * @wq_head: the waitqueue to wait on
342 * @condition: a C expression for the event to wait for
344 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
345 * to system load) until the @condition evaluates to true. The
346 * @condition is checked each time the waitqueue @wq_head is woken up.
348 * wake_up() has to be called after changing any variable that could
349 * change the result of the wait condition.
351 #define wait_event_freezable(wq_head, condition) \
356 __ret = __wait_event_freezable(wq_head, condition); \
360 #define __wait_event_timeout(wq_head, condition, timeout) \
361 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
362 TASK_UNINTERRUPTIBLE, 0, timeout, \
363 __ret = schedule_timeout(__ret))
366 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
367 * @wq_head: the waitqueue to wait on
368 * @condition: a C expression for the event to wait for
369 * @timeout: timeout, in jiffies
371 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
372 * @condition evaluates to true. The @condition is checked each time
373 * the waitqueue @wq_head is woken up.
375 * wake_up() has to be called after changing any variable that could
376 * change the result of the wait condition.
379 * 0 if the @condition evaluated to %false after the @timeout elapsed,
380 * 1 if the @condition evaluated to %true after the @timeout elapsed,
381 * or the remaining jiffies (at least 1) if the @condition evaluated
382 * to %true before the @timeout elapsed.
384 #define wait_event_timeout(wq_head, condition, timeout) \
386 long __ret = timeout; \
388 if (!___wait_cond_timeout(condition)) \
389 __ret = __wait_event_timeout(wq_head, condition, timeout); \
393 #define __wait_event_freezable_timeout(wq_head, condition, timeout) \
394 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
395 TASK_INTERRUPTIBLE, 0, timeout, \
396 __ret = schedule_timeout(__ret); try_to_freeze())
399 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
400 * increasing load and is freezable.
402 #define wait_event_freezable_timeout(wq_head, condition, timeout) \
404 long __ret = timeout; \
406 if (!___wait_cond_timeout(condition)) \
407 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
411 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
412 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
413 cmd1; schedule(); cmd2)
415 * Just like wait_event_cmd(), except it sets exclusive flag
417 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
421 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
424 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
425 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
426 cmd1; schedule(); cmd2)
429 * wait_event_cmd - sleep until a condition gets true
430 * @wq_head: the waitqueue to wait on
431 * @condition: a C expression for the event to wait for
432 * @cmd1: the command will be executed before sleep
433 * @cmd2: the command will be executed after sleep
435 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
436 * @condition evaluates to true. The @condition is checked each time
437 * the waitqueue @wq_head is woken up.
439 * wake_up() has to be called after changing any variable that could
440 * change the result of the wait condition.
442 #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
446 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
449 #define __wait_event_interruptible(wq_head, condition) \
450 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
454 * wait_event_interruptible - sleep until a condition gets true
455 * @wq_head: the waitqueue to wait on
456 * @condition: a C expression for the event to wait for
458 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
459 * @condition evaluates to true or a signal is received.
460 * The @condition is checked each time the waitqueue @wq_head is woken up.
462 * wake_up() has to be called after changing any variable that could
463 * change the result of the wait condition.
465 * The function will return -ERESTARTSYS if it was interrupted by a
466 * signal and 0 if @condition evaluated to true.
468 #define wait_event_interruptible(wq_head, condition) \
473 __ret = __wait_event_interruptible(wq_head, condition); \
477 #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
478 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
479 TASK_INTERRUPTIBLE, 0, timeout, \
480 __ret = schedule_timeout(__ret))
483 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
484 * @wq_head: the waitqueue to wait on
485 * @condition: a C expression for the event to wait for
486 * @timeout: timeout, in jiffies
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_head is woken up.
492 * wake_up() has to be called after changing any variable that could
493 * change the result of the wait condition.
496 * 0 if the @condition evaluated to %false after the @timeout elapsed,
497 * 1 if the @condition evaluated to %true after the @timeout elapsed,
498 * the remaining jiffies (at least 1) if the @condition evaluated
499 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
500 * interrupted by a signal.
502 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \
504 long __ret = timeout; \
506 if (!___wait_cond_timeout(condition)) \
507 __ret = __wait_event_interruptible_timeout(wq_head, \
508 condition, timeout); \
512 #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
515 struct hrtimer_sleeper __t; \
517 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); \
518 hrtimer_init_sleeper(&__t, current); \
519 if ((timeout) != KTIME_MAX) \
520 hrtimer_start_range_ns(&__t.timer, timeout, \
521 current->timer_slack_ns, \
524 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
531 hrtimer_cancel(&__t.timer); \
532 destroy_hrtimer_on_stack(&__t.timer); \
537 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
538 * @wq_head: the waitqueue to wait on
539 * @condition: a C expression for the event to wait for
540 * @timeout: timeout, as a ktime_t
542 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
543 * @condition evaluates to true or a signal is received.
544 * The @condition is checked each time the waitqueue @wq_head is woken up.
546 * wake_up() has to be called after changing any variable that could
547 * change the result of the wait condition.
549 * The function returns 0 if @condition became true, or -ETIME if the timeout
552 #define wait_event_hrtimeout(wq_head, condition, timeout) \
557 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
558 TASK_UNINTERRUPTIBLE); \
563 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
564 * @wq: the waitqueue to wait on
565 * @condition: a C expression for the event to wait for
566 * @timeout: timeout, as a ktime_t
568 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
569 * @condition evaluates to true or a signal is received.
570 * The @condition is checked each time the waitqueue @wq is woken up.
572 * wake_up() has to be called after changing any variable that could
573 * change the result of the wait condition.
575 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
576 * interrupted by a signal, or -ETIME if the timeout elapsed.
578 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
583 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
584 TASK_INTERRUPTIBLE); \
588 #define __wait_event_interruptible_exclusive(wq, condition) \
589 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
592 #define wait_event_interruptible_exclusive(wq, condition) \
597 __ret = __wait_event_interruptible_exclusive(wq, condition); \
601 #define __wait_event_killable_exclusive(wq, condition) \
602 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
605 #define wait_event_killable_exclusive(wq, condition) \
610 __ret = __wait_event_killable_exclusive(wq, condition); \
615 #define __wait_event_freezable_exclusive(wq, condition) \
616 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
617 schedule(); try_to_freeze())
619 #define wait_event_freezable_exclusive(wq, condition) \
624 __ret = __wait_event_freezable_exclusive(wq, condition); \
629 * wait_event_idle - wait for a condition without contributing to system load
630 * @wq_head: the waitqueue to wait on
631 * @condition: a C expression for the event to wait for
633 * The process is put to sleep (TASK_IDLE) until the
634 * @condition evaluates to true.
635 * The @condition is checked each time the waitqueue @wq_head is woken up.
637 * wake_up() has to be called after changing any variable that could
638 * change the result of the wait condition.
641 #define wait_event_idle(wq_head, condition) \
645 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
649 * wait_event_idle_exclusive - wait for a condition with contributing to system load
650 * @wq_head: the waitqueue to wait on
651 * @condition: a C expression for the event to wait for
653 * The process is put to sleep (TASK_IDLE) until the
654 * @condition evaluates to true.
655 * The @condition is checked each time the waitqueue @wq_head is woken up.
657 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
658 * set thus if other processes wait on the same list, when this
659 * process is woken further processes are not considered.
661 * wake_up() has to be called after changing any variable that could
662 * change the result of the wait condition.
665 #define wait_event_idle_exclusive(wq_head, condition) \
669 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
672 #define __wait_event_idle_timeout(wq_head, condition, timeout) \
673 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
674 TASK_IDLE, 0, timeout, \
675 __ret = schedule_timeout(__ret))
678 * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
679 * @wq_head: the waitqueue to wait on
680 * @condition: a C expression for the event to wait for
681 * @timeout: timeout, in jiffies
683 * The process is put to sleep (TASK_IDLE) until the
684 * @condition evaluates to true. The @condition is checked each time
685 * the waitqueue @wq_head is woken up.
687 * wake_up() has to be called after changing any variable that could
688 * change the result of the wait condition.
691 * 0 if the @condition evaluated to %false after the @timeout elapsed,
692 * 1 if the @condition evaluated to %true after the @timeout elapsed,
693 * or the remaining jiffies (at least 1) if the @condition evaluated
694 * to %true before the @timeout elapsed.
696 #define wait_event_idle_timeout(wq_head, condition, timeout) \
698 long __ret = timeout; \
700 if (!___wait_cond_timeout(condition)) \
701 __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
705 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
706 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
707 TASK_IDLE, 1, timeout, \
708 __ret = schedule_timeout(__ret))
711 * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
712 * @wq_head: the waitqueue to wait on
713 * @condition: a C expression for the event to wait for
714 * @timeout: timeout, in jiffies
716 * The process is put to sleep (TASK_IDLE) until the
717 * @condition evaluates to true. The @condition is checked each time
718 * the waitqueue @wq_head is woken up.
720 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
721 * set thus if other processes wait on the same list, when this
722 * process is woken further processes are not considered.
724 * wake_up() has to be called after changing any variable that could
725 * change the result of the wait condition.
728 * 0 if the @condition evaluated to %false after the @timeout elapsed,
729 * 1 if the @condition evaluated to %true after the @timeout elapsed,
730 * or the remaining jiffies (at least 1) if the @condition evaluated
731 * to %true before the @timeout elapsed.
733 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
735 long __ret = timeout; \
737 if (!___wait_cond_timeout(condition)) \
738 __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
742 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
743 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
745 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
748 DEFINE_WAIT(__wait); \
750 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
752 __ret = fn(&(wq), &__wait); \
755 } while (!(condition)); \
756 __remove_wait_queue(&(wq), &__wait); \
757 __set_current_state(TASK_RUNNING); \
763 * wait_event_interruptible_locked - sleep until a condition gets true
764 * @wq: the waitqueue to wait on
765 * @condition: a C expression for the event to wait for
767 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
768 * @condition evaluates to true or a signal is received.
769 * The @condition is checked each time the waitqueue @wq is woken up.
771 * It must be called with wq.lock being held. This spinlock is
772 * unlocked while sleeping but @condition testing is done while lock
773 * is held and when this macro exits the lock is held.
775 * The lock is locked/unlocked using spin_lock()/spin_unlock()
776 * functions which must match the way they are locked/unlocked outside
779 * wake_up_locked() has to be called after changing any variable that could
780 * change the result of the wait condition.
782 * The function will return -ERESTARTSYS if it was interrupted by a
783 * signal and 0 if @condition evaluated to true.
785 #define wait_event_interruptible_locked(wq, condition) \
787 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
790 * wait_event_interruptible_locked_irq - sleep until a condition gets true
791 * @wq: the waitqueue to wait on
792 * @condition: a C expression for the event to wait for
794 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
795 * @condition evaluates to true or a signal is received.
796 * The @condition is checked each time the waitqueue @wq is woken up.
798 * It must be called with wq.lock being held. This spinlock is
799 * unlocked while sleeping but @condition testing is done while lock
800 * is held and when this macro exits the lock is held.
802 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
803 * functions which must match the way they are locked/unlocked outside
806 * wake_up_locked() has to be called after changing any variable that could
807 * change the result of the wait condition.
809 * The function will return -ERESTARTSYS if it was interrupted by a
810 * signal and 0 if @condition evaluated to true.
812 #define wait_event_interruptible_locked_irq(wq, condition) \
814 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
817 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
818 * @wq: the waitqueue to wait on
819 * @condition: a C expression for the event to wait for
821 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
822 * @condition evaluates to true or a signal is received.
823 * The @condition is checked each time the waitqueue @wq is woken up.
825 * It must be called with wq.lock being held. This spinlock is
826 * unlocked while sleeping but @condition testing is done while lock
827 * is held and when this macro exits the lock is held.
829 * The lock is locked/unlocked using spin_lock()/spin_unlock()
830 * functions which must match the way they are locked/unlocked outside
833 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
834 * set thus when other process waits process on the list if this
835 * process is awaken further processes are not considered.
837 * wake_up_locked() has to be called after changing any variable that could
838 * change the result of the wait condition.
840 * The function will return -ERESTARTSYS if it was interrupted by a
841 * signal and 0 if @condition evaluated to true.
843 #define wait_event_interruptible_exclusive_locked(wq, condition) \
845 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
848 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
849 * @wq: the waitqueue to wait on
850 * @condition: a C expression for the event to wait for
852 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
853 * @condition evaluates to true or a signal is received.
854 * The @condition is checked each time the waitqueue @wq is woken up.
856 * It must be called with wq.lock being held. This spinlock is
857 * unlocked while sleeping but @condition testing is done while lock
858 * is held and when this macro exits the lock is held.
860 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
861 * functions which must match the way they are locked/unlocked outside
864 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
865 * set thus when other process waits process on the list if this
866 * process is awaken further processes are not considered.
868 * wake_up_locked() has to be called after changing any variable that could
869 * change the result of the wait condition.
871 * The function will return -ERESTARTSYS if it was interrupted by a
872 * signal and 0 if @condition evaluated to true.
874 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
876 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
879 #define __wait_event_killable(wq, condition) \
880 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
883 * wait_event_killable - sleep until a condition gets true
884 * @wq_head: the waitqueue to wait on
885 * @condition: a C expression for the event to wait for
887 * The process is put to sleep (TASK_KILLABLE) until the
888 * @condition evaluates to true or a signal is received.
889 * The @condition is checked each time the waitqueue @wq_head is woken up.
891 * wake_up() has to be called after changing any variable that could
892 * change the result of the wait condition.
894 * The function will return -ERESTARTSYS if it was interrupted by a
895 * signal and 0 if @condition evaluated to true.
897 #define wait_event_killable(wq_head, condition) \
902 __ret = __wait_event_killable(wq_head, condition); \
906 #define __wait_event_killable_timeout(wq_head, condition, timeout) \
907 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
908 TASK_KILLABLE, 0, timeout, \
909 __ret = schedule_timeout(__ret))
912 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
913 * @wq_head: the waitqueue to wait on
914 * @condition: a C expression for the event to wait for
915 * @timeout: timeout, in jiffies
917 * The process is put to sleep (TASK_KILLABLE) until the
918 * @condition evaluates to true or a kill signal is received.
919 * The @condition is checked each time the waitqueue @wq_head is woken up.
921 * wake_up() has to be called after changing any variable that could
922 * change the result of the wait condition.
925 * 0 if the @condition evaluated to %false after the @timeout elapsed,
926 * 1 if the @condition evaluated to %true after the @timeout elapsed,
927 * the remaining jiffies (at least 1) if the @condition evaluated
928 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
929 * interrupted by a kill signal.
931 * Only kill signals interrupt this process.
933 #define wait_event_killable_timeout(wq_head, condition, timeout) \
935 long __ret = timeout; \
937 if (!___wait_cond_timeout(condition)) \
938 __ret = __wait_event_killable_timeout(wq_head, \
939 condition, timeout); \
944 #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
945 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
946 spin_unlock_irq(&lock); \
949 spin_lock_irq(&lock))
952 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
953 * condition is checked under the lock. This
954 * is expected to be called with the lock
956 * @wq_head: the waitqueue to wait on
957 * @condition: a C expression for the event to wait for
958 * @lock: a locked spinlock_t, which will be released before cmd
959 * and schedule() and reacquired afterwards.
960 * @cmd: a command which is invoked outside the critical section before
963 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
964 * @condition evaluates to true. The @condition is checked each time
965 * the waitqueue @wq_head is woken up.
967 * wake_up() has to be called after changing any variable that could
968 * change the result of the wait condition.
970 * This is supposed to be called while holding the lock. The lock is
971 * dropped before invoking the cmd and going to sleep and is reacquired
974 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
978 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
982 * wait_event_lock_irq - sleep until a condition gets true. The
983 * condition is checked under the lock. This
984 * is expected to be called with the lock
986 * @wq_head: the waitqueue to wait on
987 * @condition: a C expression for the event to wait for
988 * @lock: a locked spinlock_t, which will be released before schedule()
989 * and reacquired afterwards.
991 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
992 * @condition evaluates to true. The @condition is checked each time
993 * the waitqueue @wq_head is woken up.
995 * wake_up() has to be called after changing any variable that could
996 * change the result of the wait condition.
998 * This is supposed to be called while holding the lock. The lock is
999 * dropped before going to sleep and is reacquired afterwards.
1001 #define wait_event_lock_irq(wq_head, condition, lock) \
1005 __wait_event_lock_irq(wq_head, condition, lock, ); \
1009 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
1010 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
1011 spin_unlock_irq(&lock); \
1014 spin_lock_irq(&lock))
1017 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
1018 * The condition is checked under the lock. This is expected to
1019 * be called with the lock taken.
1020 * @wq_head: the waitqueue to wait on
1021 * @condition: a C expression for the event to wait for
1022 * @lock: a locked spinlock_t, which will be released before cmd and
1023 * schedule() and reacquired afterwards.
1024 * @cmd: a command which is invoked outside the critical section before
1027 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1028 * @condition evaluates to true or a signal is received. The @condition is
1029 * checked each time the waitqueue @wq_head is woken up.
1031 * wake_up() has to be called after changing any variable that could
1032 * change the result of the wait condition.
1034 * This is supposed to be called while holding the lock. The lock is
1035 * dropped before invoking the cmd and going to sleep and is reacquired
1038 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1039 * and 0 if @condition evaluated to true.
1041 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
1045 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1046 condition, lock, cmd); \
1051 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1052 * The condition is checked under the lock. This is expected
1053 * to be called with the lock taken.
1054 * @wq_head: the waitqueue to wait on
1055 * @condition: a C expression for the event to wait for
1056 * @lock: a locked spinlock_t, which will be released before schedule()
1057 * and reacquired afterwards.
1059 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1060 * @condition evaluates to true or signal is received. The @condition is
1061 * checked each time the waitqueue @wq_head is woken up.
1063 * wake_up() has to be called after changing any variable that could
1064 * change the result of the wait condition.
1066 * This is supposed to be called while holding the lock. The lock is
1067 * dropped before going to sleep and is reacquired afterwards.
1069 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1070 * and 0 if @condition evaluated to true.
1072 #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
1076 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1077 condition, lock,); \
1081 #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
1082 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
1083 state, 0, timeout, \
1084 spin_unlock_irq(&lock); \
1085 __ret = schedule_timeout(__ret); \
1086 spin_lock_irq(&lock));
1089 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1090 * true or a timeout elapses. The condition is checked under
1091 * the lock. This is expected to be called with the lock taken.
1092 * @wq_head: the waitqueue to wait on
1093 * @condition: a C expression for the event to wait for
1094 * @lock: a locked spinlock_t, which will be released before schedule()
1095 * and reacquired afterwards.
1096 * @timeout: timeout, in jiffies
1098 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1099 * @condition evaluates to true or signal is received. The @condition is
1100 * checked each time the waitqueue @wq_head is woken up.
1102 * wake_up() has to be called after changing any variable that could
1103 * change the result of the wait condition.
1105 * This is supposed to be called while holding the lock. The lock is
1106 * dropped before going to sleep and is reacquired afterwards.
1108 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1109 * was interrupted by a signal, and the remaining jiffies otherwise
1110 * if the condition evaluated to true before the timeout elapsed.
1112 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1115 long __ret = timeout; \
1116 if (!___wait_cond_timeout(condition)) \
1117 __ret = __wait_event_lock_irq_timeout( \
1118 wq_head, condition, lock, timeout, \
1119 TASK_INTERRUPTIBLE); \
1123 #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout) \
1125 long __ret = timeout; \
1126 if (!___wait_cond_timeout(condition)) \
1127 __ret = __wait_event_lock_irq_timeout( \
1128 wq_head, condition, lock, timeout, \
1129 TASK_UNINTERRUPTIBLE); \
1134 * Waitqueues which are removed from the waitqueue_head at wakeup time
1136 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1137 void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1138 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1139 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1140 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1141 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1142 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1144 #define DEFINE_WAIT_FUNC(name, function) \
1145 struct wait_queue_entry name = { \
1146 .private = current, \
1148 .entry = LIST_HEAD_INIT((name).entry), \
1151 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1153 #define init_wait(wait) \
1155 (wait)->private = current; \
1156 (wait)->func = autoremove_wake_function; \
1157 INIT_LIST_HEAD(&(wait)->entry); \
1158 (wait)->flags = 0; \
1161 #endif /* _LINUX_WAIT_H */