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
23 #define WQ_FLAG_CUSTOM 0x08
24 #define WQ_FLAG_DONE 0x10
27 * A single wait-queue entry structure:
29 struct wait_queue_entry {
32 wait_queue_func_t func;
33 struct list_head entry;
36 struct wait_queue_head {
38 struct list_head head;
40 typedef struct wait_queue_head wait_queue_head_t;
45 * Macros for declaration and initialisaton of the datatypes
48 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
50 .func = default_wake_function, \
51 .entry = { NULL, NULL } }
53 #define DECLARE_WAITQUEUE(name, tsk) \
54 struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
56 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
57 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
58 .head = { &(name).head, &(name).head } }
60 #define DECLARE_WAIT_QUEUE_HEAD(name) \
61 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
63 extern void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *);
65 #define init_waitqueue_head(wq_head) \
67 static struct lock_class_key __key; \
69 __init_waitqueue_head((wq_head), #wq_head, &__key); \
73 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
74 ({ init_waitqueue_head(&name); name; })
75 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
76 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
78 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
81 static inline void init_waitqueue_entry(struct wait_queue_entry *wq_entry, struct task_struct *p)
84 wq_entry->private = p;
85 wq_entry->func = default_wake_function;
89 init_waitqueue_func_entry(struct wait_queue_entry *wq_entry, wait_queue_func_t func)
92 wq_entry->private = NULL;
93 wq_entry->func = func;
97 * waitqueue_active -- locklessly test for waiters on the queue
98 * @wq_head: the waitqueue to test for waiters
100 * returns true if the wait list is not empty
102 * NOTE: this function is lockless and requires care, incorrect usage _will_
103 * lead to sporadic and non-obvious failure.
105 * Use either while holding wait_queue_head::lock or when used for wakeups
106 * with an extra smp_mb() like::
108 * CPU0 - waker CPU1 - waiter
111 * @cond = true; prepare_to_wait(&wq_head, &wait, state);
112 * smp_mb(); // smp_mb() from set_current_state()
113 * if (waitqueue_active(wq_head)) if (@cond)
114 * wake_up(wq_head); break;
117 * finish_wait(&wq_head, &wait);
119 * Because without the explicit smp_mb() it's possible for the
120 * waitqueue_active() load to get hoisted over the @cond store such that we'll
121 * observe an empty wait list while the waiter might not observe @cond.
123 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
124 * which (when the lock is uncontended) are of roughly equal cost.
126 static inline int waitqueue_active(struct wait_queue_head *wq_head)
128 return !list_empty(&wq_head->head);
132 * wq_has_single_sleeper - check if there is only one sleeper
133 * @wq_head: wait queue head
135 * Returns true of wq_head has only one sleeper on the list.
137 * Please refer to the comment for waitqueue_active.
139 static inline bool wq_has_single_sleeper(struct wait_queue_head *wq_head)
141 return list_is_singular(&wq_head->head);
145 * wq_has_sleeper - check if there are any waiting processes
146 * @wq_head: wait queue head
148 * Returns true if wq_head has waiting processes
150 * Please refer to the comment for waitqueue_active.
152 static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
155 * We need to be sure we are in sync with the
156 * add_wait_queue modifications to the wait queue.
158 * This memory barrier should be paired with one on the
162 return waitqueue_active(wq_head);
165 extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
166 extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
167 extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
169 static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
171 list_add(&wq_entry->entry, &wq_head->head);
175 * Used for wake-one threads:
178 __add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
180 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
181 __add_wait_queue(wq_head, wq_entry);
184 static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
186 list_add_tail(&wq_entry->entry, &wq_head->head);
190 __add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
192 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
193 __add_wait_queue_entry_tail(wq_head, wq_entry);
197 __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
199 list_del(&wq_entry->entry);
202 void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
203 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
204 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
205 unsigned int mode, void *key, wait_queue_entry_t *bookmark);
206 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
207 void __wake_up_locked_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
208 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
209 void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode);
211 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
212 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
213 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
214 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
215 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
217 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
218 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
219 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
220 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE)
223 * Wakeup macros to be used to report events to the targets.
225 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
226 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
227 #define wake_up_poll(x, m) \
228 __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
229 #define wake_up_locked_poll(x, m) \
230 __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
231 #define wake_up_interruptible_poll(x, m) \
232 __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
233 #define wake_up_interruptible_sync_poll(x, m) \
234 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
235 #define wake_up_interruptible_sync_poll_locked(x, m) \
236 __wake_up_locked_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
238 #define ___wait_cond_timeout(condition) \
240 bool __cond = (condition); \
241 if (__cond && !__ret) \
246 #define ___wait_is_interruptible(state) \
247 (!__builtin_constant_p(state) || \
248 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
250 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
253 * The below macro ___wait_event() has an explicit shadow of the __ret
254 * variable when used from the wait_event_*() macros.
256 * This is so that both can use the ___wait_cond_timeout() construct
257 * to wrap the condition.
259 * The type inconsistency of the wait_event_*() __ret variable is also
260 * on purpose; we use long where we can return timeout values and int
264 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
267 struct wait_queue_entry __wq_entry; \
268 long __ret = ret; /* explicit shadow */ \
270 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
272 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
277 if (___wait_is_interruptible(state) && __int) { \
284 finish_wait(&wq_head, &__wq_entry); \
288 #define __wait_event(wq_head, condition) \
289 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
293 * wait_event - sleep until a condition gets true
294 * @wq_head: the waitqueue to wait on
295 * @condition: a C expression for the event to wait for
297 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
298 * @condition evaluates to true. The @condition is checked each time
299 * the waitqueue @wq_head is woken up.
301 * wake_up() has to be called after changing any variable that could
302 * change the result of the wait condition.
304 #define wait_event(wq_head, condition) \
309 __wait_event(wq_head, condition); \
312 #define __io_wait_event(wq_head, condition) \
313 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
317 * io_wait_event() -- like wait_event() but with io_schedule()
319 #define io_wait_event(wq_head, condition) \
324 __io_wait_event(wq_head, condition); \
327 #define __wait_event_freezable(wq_head, condition) \
328 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
329 freezable_schedule())
332 * wait_event_freezable - sleep (or freeze) until a condition gets true
333 * @wq_head: the waitqueue to wait on
334 * @condition: a C expression for the event to wait for
336 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
337 * to system load) until the @condition evaluates to true. The
338 * @condition is checked each time the waitqueue @wq_head is woken up.
340 * wake_up() has to be called after changing any variable that could
341 * change the result of the wait condition.
343 #define wait_event_freezable(wq_head, condition) \
348 __ret = __wait_event_freezable(wq_head, condition); \
352 #define __wait_event_timeout(wq_head, condition, timeout) \
353 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
354 TASK_UNINTERRUPTIBLE, 0, timeout, \
355 __ret = schedule_timeout(__ret))
358 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
359 * @wq_head: the waitqueue to wait on
360 * @condition: a C expression for the event to wait for
361 * @timeout: timeout, in jiffies
363 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
364 * @condition evaluates to true. The @condition is checked each time
365 * the waitqueue @wq_head is woken up.
367 * wake_up() has to be called after changing any variable that could
368 * change the result of the wait condition.
371 * 0 if the @condition evaluated to %false after the @timeout elapsed,
372 * 1 if the @condition evaluated to %true after the @timeout elapsed,
373 * or the remaining jiffies (at least 1) if the @condition evaluated
374 * to %true before the @timeout elapsed.
376 #define wait_event_timeout(wq_head, condition, timeout) \
378 long __ret = timeout; \
380 if (!___wait_cond_timeout(condition)) \
381 __ret = __wait_event_timeout(wq_head, condition, timeout); \
385 #define __wait_event_freezable_timeout(wq_head, condition, timeout) \
386 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
387 TASK_INTERRUPTIBLE, 0, timeout, \
388 __ret = freezable_schedule_timeout(__ret))
391 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
392 * increasing load and is freezable.
394 #define wait_event_freezable_timeout(wq_head, condition, timeout) \
396 long __ret = timeout; \
398 if (!___wait_cond_timeout(condition)) \
399 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
403 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
404 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
405 cmd1; schedule(); cmd2)
407 * Just like wait_event_cmd(), except it sets exclusive flag
409 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
413 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
416 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
417 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
418 cmd1; schedule(); cmd2)
421 * wait_event_cmd - sleep until a condition gets true
422 * @wq_head: the waitqueue to wait on
423 * @condition: a C expression for the event to wait for
424 * @cmd1: the command will be executed before sleep
425 * @cmd2: the command will be executed after sleep
427 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
428 * @condition evaluates to true. The @condition is checked each time
429 * the waitqueue @wq_head is woken up.
431 * wake_up() has to be called after changing any variable that could
432 * change the result of the wait condition.
434 #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
438 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
441 #define __wait_event_interruptible(wq_head, condition) \
442 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
446 * wait_event_interruptible - sleep until a condition gets true
447 * @wq_head: the waitqueue to wait on
448 * @condition: a C expression for the event to wait for
450 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
451 * @condition evaluates to true or a signal is received.
452 * The @condition is checked each time the waitqueue @wq_head is woken up.
454 * wake_up() has to be called after changing any variable that could
455 * change the result of the wait condition.
457 * The function will return -ERESTARTSYS if it was interrupted by a
458 * signal and 0 if @condition evaluated to true.
460 #define wait_event_interruptible(wq_head, condition) \
465 __ret = __wait_event_interruptible(wq_head, condition); \
469 #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
470 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
471 TASK_INTERRUPTIBLE, 0, timeout, \
472 __ret = schedule_timeout(__ret))
475 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
476 * @wq_head: the waitqueue to wait on
477 * @condition: a C expression for the event to wait for
478 * @timeout: timeout, in jiffies
480 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
481 * @condition evaluates to true or a signal is received.
482 * The @condition is checked each time the waitqueue @wq_head is woken up.
484 * wake_up() has to be called after changing any variable that could
485 * change the result of the wait condition.
488 * 0 if the @condition evaluated to %false after the @timeout elapsed,
489 * 1 if the @condition evaluated to %true after the @timeout elapsed,
490 * the remaining jiffies (at least 1) if the @condition evaluated
491 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
492 * interrupted by a signal.
494 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \
496 long __ret = timeout; \
498 if (!___wait_cond_timeout(condition)) \
499 __ret = __wait_event_interruptible_timeout(wq_head, \
500 condition, timeout); \
504 #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
507 struct hrtimer_sleeper __t; \
509 hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC, \
511 if ((timeout) != KTIME_MAX) \
512 hrtimer_start_range_ns(&__t.timer, timeout, \
513 current->timer_slack_ns, \
516 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
523 hrtimer_cancel(&__t.timer); \
524 destroy_hrtimer_on_stack(&__t.timer); \
529 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
530 * @wq_head: the waitqueue to wait on
531 * @condition: a C expression for the event to wait for
532 * @timeout: timeout, as a ktime_t
534 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
535 * @condition evaluates to true or a signal is received.
536 * The @condition is checked each time the waitqueue @wq_head is woken up.
538 * wake_up() has to be called after changing any variable that could
539 * change the result of the wait condition.
541 * The function returns 0 if @condition became true, or -ETIME if the timeout
544 #define wait_event_hrtimeout(wq_head, condition, timeout) \
549 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
550 TASK_UNINTERRUPTIBLE); \
555 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
556 * @wq: the waitqueue to wait on
557 * @condition: a C expression for the event to wait for
558 * @timeout: timeout, as a ktime_t
560 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
561 * @condition evaluates to true or a signal is received.
562 * The @condition is checked each time the waitqueue @wq is woken up.
564 * wake_up() has to be called after changing any variable that could
565 * change the result of the wait condition.
567 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
568 * interrupted by a signal, or -ETIME if the timeout elapsed.
570 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
575 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
576 TASK_INTERRUPTIBLE); \
580 #define __wait_event_interruptible_exclusive(wq, condition) \
581 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
584 #define wait_event_interruptible_exclusive(wq, condition) \
589 __ret = __wait_event_interruptible_exclusive(wq, condition); \
593 #define __wait_event_killable_exclusive(wq, condition) \
594 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
597 #define wait_event_killable_exclusive(wq, condition) \
602 __ret = __wait_event_killable_exclusive(wq, condition); \
607 #define __wait_event_freezable_exclusive(wq, condition) \
608 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
609 freezable_schedule())
611 #define wait_event_freezable_exclusive(wq, condition) \
616 __ret = __wait_event_freezable_exclusive(wq, condition); \
621 * wait_event_idle - wait for a condition without contributing to system load
622 * @wq_head: the waitqueue to wait on
623 * @condition: a C expression for the event to wait for
625 * The process is put to sleep (TASK_IDLE) until the
626 * @condition evaluates to true.
627 * The @condition is checked each time the waitqueue @wq_head is woken up.
629 * wake_up() has to be called after changing any variable that could
630 * change the result of the wait condition.
633 #define wait_event_idle(wq_head, condition) \
637 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
641 * wait_event_idle_exclusive - wait for a condition with contributing to system load
642 * @wq_head: the waitqueue to wait on
643 * @condition: a C expression for the event to wait for
645 * The process is put to sleep (TASK_IDLE) until the
646 * @condition evaluates to true.
647 * The @condition is checked each time the waitqueue @wq_head is woken up.
649 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
650 * set thus if other processes wait on the same list, when this
651 * process is woken further processes are not considered.
653 * wake_up() has to be called after changing any variable that could
654 * change the result of the wait condition.
657 #define wait_event_idle_exclusive(wq_head, condition) \
661 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
664 #define __wait_event_idle_timeout(wq_head, condition, timeout) \
665 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
666 TASK_IDLE, 0, timeout, \
667 __ret = schedule_timeout(__ret))
670 * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
671 * @wq_head: the waitqueue to wait on
672 * @condition: a C expression for the event to wait for
673 * @timeout: timeout, in jiffies
675 * The process is put to sleep (TASK_IDLE) until the
676 * @condition evaluates to true. The @condition is checked each time
677 * the waitqueue @wq_head is woken up.
679 * wake_up() has to be called after changing any variable that could
680 * change the result of the wait condition.
683 * 0 if the @condition evaluated to %false after the @timeout elapsed,
684 * 1 if the @condition evaluated to %true after the @timeout elapsed,
685 * or the remaining jiffies (at least 1) if the @condition evaluated
686 * to %true before the @timeout elapsed.
688 #define wait_event_idle_timeout(wq_head, condition, timeout) \
690 long __ret = timeout; \
692 if (!___wait_cond_timeout(condition)) \
693 __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
697 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
698 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
699 TASK_IDLE, 1, timeout, \
700 __ret = schedule_timeout(__ret))
703 * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
704 * @wq_head: the waitqueue to wait on
705 * @condition: a C expression for the event to wait for
706 * @timeout: timeout, in jiffies
708 * The process is put to sleep (TASK_IDLE) until the
709 * @condition evaluates to true. The @condition is checked each time
710 * the waitqueue @wq_head is woken up.
712 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
713 * set thus if other processes wait on the same list, when this
714 * process is woken further processes are not considered.
716 * wake_up() has to be called after changing any variable that could
717 * change the result of the wait condition.
720 * 0 if the @condition evaluated to %false after the @timeout elapsed,
721 * 1 if the @condition evaluated to %true after the @timeout elapsed,
722 * or the remaining jiffies (at least 1) if the @condition evaluated
723 * to %true before the @timeout elapsed.
725 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
727 long __ret = timeout; \
729 if (!___wait_cond_timeout(condition)) \
730 __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
734 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
735 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
737 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
740 DEFINE_WAIT(__wait); \
742 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
744 __ret = fn(&(wq), &__wait); \
747 } while (!(condition)); \
748 __remove_wait_queue(&(wq), &__wait); \
749 __set_current_state(TASK_RUNNING); \
755 * wait_event_interruptible_locked - sleep until a condition gets true
756 * @wq: the waitqueue to wait on
757 * @condition: a C expression for the event to wait for
759 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
760 * @condition evaluates to true or a signal is received.
761 * The @condition is checked each time the waitqueue @wq is woken up.
763 * It must be called with wq.lock being held. This spinlock is
764 * unlocked while sleeping but @condition testing is done while lock
765 * is held and when this macro exits the lock is held.
767 * The lock is locked/unlocked using spin_lock()/spin_unlock()
768 * functions which must match the way they are locked/unlocked outside
771 * wake_up_locked() has to be called after changing any variable that could
772 * change the result of the wait condition.
774 * The function will return -ERESTARTSYS if it was interrupted by a
775 * signal and 0 if @condition evaluated to true.
777 #define wait_event_interruptible_locked(wq, condition) \
779 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
782 * wait_event_interruptible_locked_irq - sleep until a condition gets true
783 * @wq: the waitqueue to wait on
784 * @condition: a C expression for the event to wait for
786 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
787 * @condition evaluates to true or a signal is received.
788 * The @condition is checked each time the waitqueue @wq is woken up.
790 * It must be called with wq.lock being held. This spinlock is
791 * unlocked while sleeping but @condition testing is done while lock
792 * is held and when this macro exits the lock is held.
794 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
795 * functions which must match the way they are locked/unlocked outside
798 * wake_up_locked() has to be called after changing any variable that could
799 * change the result of the wait condition.
801 * The function will return -ERESTARTSYS if it was interrupted by a
802 * signal and 0 if @condition evaluated to true.
804 #define wait_event_interruptible_locked_irq(wq, condition) \
806 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
809 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
810 * @wq: the waitqueue to wait on
811 * @condition: a C expression for the event to wait for
813 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
814 * @condition evaluates to true or a signal is received.
815 * The @condition is checked each time the waitqueue @wq is woken up.
817 * It must be called with wq.lock being held. This spinlock is
818 * unlocked while sleeping but @condition testing is done while lock
819 * is held and when this macro exits the lock is held.
821 * The lock is locked/unlocked using spin_lock()/spin_unlock()
822 * functions which must match the way they are locked/unlocked outside
825 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
826 * set thus when other process waits process on the list if this
827 * process is awaken further processes are not considered.
829 * wake_up_locked() has to be called after changing any variable that could
830 * change the result of the wait condition.
832 * The function will return -ERESTARTSYS if it was interrupted by a
833 * signal and 0 if @condition evaluated to true.
835 #define wait_event_interruptible_exclusive_locked(wq, condition) \
837 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
840 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
841 * @wq: the waitqueue to wait on
842 * @condition: a C expression for the event to wait for
844 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
845 * @condition evaluates to true or a signal is received.
846 * The @condition is checked each time the waitqueue @wq is woken up.
848 * It must be called with wq.lock being held. This spinlock is
849 * unlocked while sleeping but @condition testing is done while lock
850 * is held and when this macro exits the lock is held.
852 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
853 * functions which must match the way they are locked/unlocked outside
856 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
857 * set thus when other process waits process on the list if this
858 * process is awaken further processes are not considered.
860 * wake_up_locked() has to be called after changing any variable that could
861 * change the result of the wait condition.
863 * The function will return -ERESTARTSYS if it was interrupted by a
864 * signal and 0 if @condition evaluated to true.
866 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
868 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
871 #define __wait_event_killable(wq, condition) \
872 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
875 * wait_event_killable - sleep until a condition gets true
876 * @wq_head: the waitqueue to wait on
877 * @condition: a C expression for the event to wait for
879 * The process is put to sleep (TASK_KILLABLE) until the
880 * @condition evaluates to true or a signal is received.
881 * The @condition is checked each time the waitqueue @wq_head is woken up.
883 * wake_up() has to be called after changing any variable that could
884 * change the result of the wait condition.
886 * The function will return -ERESTARTSYS if it was interrupted by a
887 * signal and 0 if @condition evaluated to true.
889 #define wait_event_killable(wq_head, condition) \
894 __ret = __wait_event_killable(wq_head, condition); \
898 #define __wait_event_killable_timeout(wq_head, condition, timeout) \
899 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
900 TASK_KILLABLE, 0, timeout, \
901 __ret = schedule_timeout(__ret))
904 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
905 * @wq_head: the waitqueue to wait on
906 * @condition: a C expression for the event to wait for
907 * @timeout: timeout, in jiffies
909 * The process is put to sleep (TASK_KILLABLE) until the
910 * @condition evaluates to true or a kill signal is received.
911 * The @condition is checked each time the waitqueue @wq_head is woken up.
913 * wake_up() has to be called after changing any variable that could
914 * change the result of the wait condition.
917 * 0 if the @condition evaluated to %false after the @timeout elapsed,
918 * 1 if the @condition evaluated to %true after the @timeout elapsed,
919 * the remaining jiffies (at least 1) if the @condition evaluated
920 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
921 * interrupted by a kill signal.
923 * Only kill signals interrupt this process.
925 #define wait_event_killable_timeout(wq_head, condition, timeout) \
927 long __ret = timeout; \
929 if (!___wait_cond_timeout(condition)) \
930 __ret = __wait_event_killable_timeout(wq_head, \
931 condition, timeout); \
936 #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
937 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
938 spin_unlock_irq(&lock); \
941 spin_lock_irq(&lock))
944 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
945 * condition is checked under the lock. This
946 * is expected to be called with the lock
948 * @wq_head: the waitqueue to wait on
949 * @condition: a C expression for the event to wait for
950 * @lock: a locked spinlock_t, which will be released before cmd
951 * and schedule() and reacquired afterwards.
952 * @cmd: a command which is invoked outside the critical section before
955 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
956 * @condition evaluates to true. The @condition is checked each time
957 * the waitqueue @wq_head is woken up.
959 * wake_up() has to be called after changing any variable that could
960 * change the result of the wait condition.
962 * This is supposed to be called while holding the lock. The lock is
963 * dropped before invoking the cmd and going to sleep and is reacquired
966 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
970 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
974 * wait_event_lock_irq - sleep until a condition gets true. The
975 * condition is checked under the lock. This
976 * is expected to be called with the lock
978 * @wq_head: the waitqueue to wait on
979 * @condition: a C expression for the event to wait for
980 * @lock: a locked spinlock_t, which will be released before schedule()
981 * and reacquired afterwards.
983 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
984 * @condition evaluates to true. The @condition is checked each time
985 * 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 #define wait_event_lock_irq(wq_head, condition, lock) \
997 __wait_event_lock_irq(wq_head, condition, lock, ); \
1001 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
1002 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
1003 spin_unlock_irq(&lock); \
1006 spin_lock_irq(&lock))
1009 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
1010 * The condition is checked under the lock. This is expected to
1011 * be called with the lock taken.
1012 * @wq_head: the waitqueue to wait on
1013 * @condition: a C expression for the event to wait for
1014 * @lock: a locked spinlock_t, which will be released before cmd and
1015 * schedule() and reacquired afterwards.
1016 * @cmd: a command which is invoked outside the critical section before
1019 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1020 * @condition evaluates to true or a signal is received. The @condition is
1021 * checked each time the waitqueue @wq_head is woken up.
1023 * wake_up() has to be called after changing any variable that could
1024 * change the result of the wait condition.
1026 * This is supposed to be called while holding the lock. The lock is
1027 * dropped before invoking the cmd and going to sleep and is reacquired
1030 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1031 * and 0 if @condition evaluated to true.
1033 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
1037 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1038 condition, lock, cmd); \
1043 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1044 * The condition is checked under the lock. This is expected
1045 * to be called with the lock taken.
1046 * @wq_head: the waitqueue to wait on
1047 * @condition: a C expression for the event to wait for
1048 * @lock: a locked spinlock_t, which will be released before schedule()
1049 * and reacquired afterwards.
1051 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1052 * @condition evaluates to true or signal is received. The @condition is
1053 * checked each time the waitqueue @wq_head is woken up.
1055 * wake_up() has to be called after changing any variable that could
1056 * change the result of the wait condition.
1058 * This is supposed to be called while holding the lock. The lock is
1059 * dropped before going to sleep and is reacquired afterwards.
1061 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1062 * and 0 if @condition evaluated to true.
1064 #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
1068 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1069 condition, lock,); \
1073 #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
1074 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
1075 state, 0, timeout, \
1076 spin_unlock_irq(&lock); \
1077 __ret = schedule_timeout(__ret); \
1078 spin_lock_irq(&lock));
1081 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1082 * true or a timeout elapses. The condition is checked under
1083 * the lock. This is expected to be called with the lock taken.
1084 * @wq_head: the waitqueue to wait on
1085 * @condition: a C expression for the event to wait for
1086 * @lock: a locked spinlock_t, which will be released before schedule()
1087 * and reacquired afterwards.
1088 * @timeout: timeout, in jiffies
1090 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1091 * @condition evaluates to true or signal is received. The @condition is
1092 * checked each time the waitqueue @wq_head is woken up.
1094 * wake_up() has to be called after changing any variable that could
1095 * change the result of the wait condition.
1097 * This is supposed to be called while holding the lock. The lock is
1098 * dropped before going to sleep and is reacquired afterwards.
1100 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1101 * was interrupted by a signal, and the remaining jiffies otherwise
1102 * if the condition evaluated to true before the timeout elapsed.
1104 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1107 long __ret = timeout; \
1108 if (!___wait_cond_timeout(condition)) \
1109 __ret = __wait_event_lock_irq_timeout( \
1110 wq_head, condition, lock, timeout, \
1111 TASK_INTERRUPTIBLE); \
1115 #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout) \
1117 long __ret = timeout; \
1118 if (!___wait_cond_timeout(condition)) \
1119 __ret = __wait_event_lock_irq_timeout( \
1120 wq_head, condition, lock, timeout, \
1121 TASK_UNINTERRUPTIBLE); \
1126 * Waitqueues which are removed from the waitqueue_head at wakeup time
1128 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1129 void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1130 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1131 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1132 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1133 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1134 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1136 #define DEFINE_WAIT_FUNC(name, function) \
1137 struct wait_queue_entry name = { \
1138 .private = current, \
1140 .entry = LIST_HEAD_INIT((name).entry), \
1143 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1145 #define init_wait(wait) \
1147 (wait)->private = current; \
1148 (wait)->func = autoremove_wake_function; \
1149 INIT_LIST_HEAD(&(wait)->entry); \
1150 (wait)->flags = 0; \
1153 bool try_invoke_on_locked_down_task(struct task_struct *p, bool (*func)(struct task_struct *t, void *arg), void *arg);
1155 #endif /* _LINUX_WAIT_H */