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);
210 void __wake_up_pollfree(struct wait_queue_head *wq_head);
212 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
213 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
214 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
215 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
216 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
218 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
219 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
220 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
221 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE)
224 * Wakeup macros to be used to report events to the targets.
226 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
227 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
228 #define wake_up_poll(x, m) \
229 __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
230 #define wake_up_locked_poll(x, m) \
231 __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
232 #define wake_up_interruptible_poll(x, m) \
233 __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
234 #define wake_up_interruptible_sync_poll(x, m) \
235 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
236 #define wake_up_interruptible_sync_poll_locked(x, m) \
237 __wake_up_locked_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
240 * wake_up_pollfree - signal that a polled waitqueue is going away
241 * @wq_head: the wait queue head
243 * In the very rare cases where a ->poll() implementation uses a waitqueue whose
244 * lifetime is tied to a task rather than to the 'struct file' being polled,
245 * this function must be called before the waitqueue is freed so that
246 * non-blocking polls (e.g. epoll) are notified that the queue is going away.
248 * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via
249 * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU.
251 static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
254 * For performance reasons, we don't always take the queue lock here.
255 * Therefore, we might race with someone removing the last entry from
256 * the queue, and proceed while they still hold the queue lock.
257 * However, rcu_read_lock() is required to be held in such cases, so we
258 * can safely proceed with an RCU-delayed free.
260 if (waitqueue_active(wq_head))
261 __wake_up_pollfree(wq_head);
264 #define ___wait_cond_timeout(condition) \
266 bool __cond = (condition); \
267 if (__cond && !__ret) \
272 #define ___wait_is_interruptible(state) \
273 (!__builtin_constant_p(state) || \
274 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
276 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
279 * The below macro ___wait_event() has an explicit shadow of the __ret
280 * variable when used from the wait_event_*() macros.
282 * This is so that both can use the ___wait_cond_timeout() construct
283 * to wrap the condition.
285 * The type inconsistency of the wait_event_*() __ret variable is also
286 * on purpose; we use long where we can return timeout values and int
290 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
293 struct wait_queue_entry __wq_entry; \
294 long __ret = ret; /* explicit shadow */ \
296 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
298 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
303 if (___wait_is_interruptible(state) && __int) { \
310 finish_wait(&wq_head, &__wq_entry); \
314 #define __wait_event(wq_head, condition) \
315 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
319 * wait_event - sleep until a condition gets true
320 * @wq_head: the waitqueue to wait on
321 * @condition: a C expression for the event to wait for
323 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
324 * @condition evaluates to true. The @condition is checked each time
325 * the waitqueue @wq_head is woken up.
327 * wake_up() has to be called after changing any variable that could
328 * change the result of the wait condition.
330 #define wait_event(wq_head, condition) \
335 __wait_event(wq_head, condition); \
338 #define __io_wait_event(wq_head, condition) \
339 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
343 * io_wait_event() -- like wait_event() but with io_schedule()
345 #define io_wait_event(wq_head, condition) \
350 __io_wait_event(wq_head, condition); \
353 #define __wait_event_freezable(wq_head, condition) \
354 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
355 freezable_schedule())
358 * wait_event_freezable - sleep (or freeze) until a condition gets true
359 * @wq_head: the waitqueue to wait on
360 * @condition: a C expression for the event to wait for
362 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
363 * to system load) until the @condition evaluates to true. The
364 * @condition is checked each time the waitqueue @wq_head is woken up.
366 * wake_up() has to be called after changing any variable that could
367 * change the result of the wait condition.
369 #define wait_event_freezable(wq_head, condition) \
374 __ret = __wait_event_freezable(wq_head, condition); \
378 #define __wait_event_timeout(wq_head, condition, timeout) \
379 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
380 TASK_UNINTERRUPTIBLE, 0, timeout, \
381 __ret = schedule_timeout(__ret))
384 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
385 * @wq_head: the waitqueue to wait on
386 * @condition: a C expression for the event to wait for
387 * @timeout: timeout, in jiffies
389 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
390 * @condition evaluates to true. The @condition is checked each time
391 * the waitqueue @wq_head is woken up.
393 * wake_up() has to be called after changing any variable that could
394 * change the result of the wait condition.
397 * 0 if the @condition evaluated to %false after the @timeout elapsed,
398 * 1 if the @condition evaluated to %true after the @timeout elapsed,
399 * or the remaining jiffies (at least 1) if the @condition evaluated
400 * to %true before the @timeout elapsed.
402 #define wait_event_timeout(wq_head, condition, timeout) \
404 long __ret = timeout; \
406 if (!___wait_cond_timeout(condition)) \
407 __ret = __wait_event_timeout(wq_head, condition, timeout); \
411 #define __wait_event_freezable_timeout(wq_head, condition, timeout) \
412 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
413 TASK_INTERRUPTIBLE, 0, timeout, \
414 __ret = freezable_schedule_timeout(__ret))
417 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
418 * increasing load and is freezable.
420 #define wait_event_freezable_timeout(wq_head, condition, timeout) \
422 long __ret = timeout; \
424 if (!___wait_cond_timeout(condition)) \
425 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
429 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
430 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
431 cmd1; schedule(); cmd2)
433 * Just like wait_event_cmd(), except it sets exclusive flag
435 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
439 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
442 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
443 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
444 cmd1; schedule(); cmd2)
447 * wait_event_cmd - sleep until a condition gets true
448 * @wq_head: the waitqueue to wait on
449 * @condition: a C expression for the event to wait for
450 * @cmd1: the command will be executed before sleep
451 * @cmd2: the command will be executed after sleep
453 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
454 * @condition evaluates to true. The @condition is checked each time
455 * the waitqueue @wq_head is woken up.
457 * wake_up() has to be called after changing any variable that could
458 * change the result of the wait condition.
460 #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
464 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
467 #define __wait_event_interruptible(wq_head, condition) \
468 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
472 * wait_event_interruptible - sleep until a condition gets true
473 * @wq_head: the waitqueue to wait on
474 * @condition: a C expression for the event to wait for
476 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
477 * @condition evaluates to true or a signal is received.
478 * The @condition is checked each time the waitqueue @wq_head is woken up.
480 * wake_up() has to be called after changing any variable that could
481 * change the result of the wait condition.
483 * The function will return -ERESTARTSYS if it was interrupted by a
484 * signal and 0 if @condition evaluated to true.
486 #define wait_event_interruptible(wq_head, condition) \
491 __ret = __wait_event_interruptible(wq_head, condition); \
495 #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
496 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
497 TASK_INTERRUPTIBLE, 0, timeout, \
498 __ret = schedule_timeout(__ret))
501 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
502 * @wq_head: the waitqueue to wait on
503 * @condition: a C expression for the event to wait for
504 * @timeout: timeout, in jiffies
506 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
507 * @condition evaluates to true or a signal is received.
508 * The @condition is checked each time the waitqueue @wq_head is woken up.
510 * wake_up() has to be called after changing any variable that could
511 * change the result of the wait condition.
514 * 0 if the @condition evaluated to %false after the @timeout elapsed,
515 * 1 if the @condition evaluated to %true after the @timeout elapsed,
516 * the remaining jiffies (at least 1) if the @condition evaluated
517 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
518 * interrupted by a signal.
520 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \
522 long __ret = timeout; \
524 if (!___wait_cond_timeout(condition)) \
525 __ret = __wait_event_interruptible_timeout(wq_head, \
526 condition, timeout); \
530 #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
533 struct hrtimer_sleeper __t; \
535 hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC, \
537 if ((timeout) != KTIME_MAX) { \
538 hrtimer_set_expires_range_ns(&__t.timer, timeout, \
539 current->timer_slack_ns); \
540 hrtimer_sleeper_start_expires(&__t, HRTIMER_MODE_REL); \
543 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
550 hrtimer_cancel(&__t.timer); \
551 destroy_hrtimer_on_stack(&__t.timer); \
556 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
557 * @wq_head: the waitqueue to wait on
558 * @condition: a C expression for the event to wait for
559 * @timeout: timeout, as a ktime_t
561 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
562 * @condition evaluates to true or a signal is received.
563 * The @condition is checked each time the waitqueue @wq_head is woken up.
565 * wake_up() has to be called after changing any variable that could
566 * change the result of the wait condition.
568 * The function returns 0 if @condition became true, or -ETIME if the timeout
571 #define wait_event_hrtimeout(wq_head, condition, timeout) \
576 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
577 TASK_UNINTERRUPTIBLE); \
582 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
583 * @wq: the waitqueue to wait on
584 * @condition: a C expression for the event to wait for
585 * @timeout: timeout, as a ktime_t
587 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
588 * @condition evaluates to true or a signal is received.
589 * The @condition is checked each time the waitqueue @wq is woken up.
591 * wake_up() has to be called after changing any variable that could
592 * change the result of the wait condition.
594 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
595 * interrupted by a signal, or -ETIME if the timeout elapsed.
597 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
602 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
603 TASK_INTERRUPTIBLE); \
607 #define __wait_event_interruptible_exclusive(wq, condition) \
608 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
611 #define wait_event_interruptible_exclusive(wq, condition) \
616 __ret = __wait_event_interruptible_exclusive(wq, condition); \
620 #define __wait_event_killable_exclusive(wq, condition) \
621 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
624 #define wait_event_killable_exclusive(wq, condition) \
629 __ret = __wait_event_killable_exclusive(wq, condition); \
634 #define __wait_event_freezable_exclusive(wq, condition) \
635 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
636 freezable_schedule())
638 #define wait_event_freezable_exclusive(wq, condition) \
643 __ret = __wait_event_freezable_exclusive(wq, condition); \
648 * wait_event_idle - wait for a condition without contributing to system load
649 * @wq_head: the waitqueue to wait on
650 * @condition: a C expression for the event to wait for
652 * The process is put to sleep (TASK_IDLE) until the
653 * @condition evaluates to true.
654 * The @condition is checked each time the waitqueue @wq_head is woken up.
656 * wake_up() has to be called after changing any variable that could
657 * change the result of the wait condition.
660 #define wait_event_idle(wq_head, condition) \
664 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
668 * wait_event_idle_exclusive - wait for a condition with contributing to system load
669 * @wq_head: the waitqueue to wait on
670 * @condition: a C expression for the event to wait for
672 * The process is put to sleep (TASK_IDLE) until the
673 * @condition evaluates to true.
674 * The @condition is checked each time the waitqueue @wq_head is woken up.
676 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
677 * set thus if other processes wait on the same list, when this
678 * process is woken further processes are not considered.
680 * wake_up() has to be called after changing any variable that could
681 * change the result of the wait condition.
684 #define wait_event_idle_exclusive(wq_head, condition) \
688 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
691 #define __wait_event_idle_timeout(wq_head, condition, timeout) \
692 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
693 TASK_IDLE, 0, timeout, \
694 __ret = schedule_timeout(__ret))
697 * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
698 * @wq_head: the waitqueue to wait on
699 * @condition: a C expression for the event to wait for
700 * @timeout: timeout, in jiffies
702 * The process is put to sleep (TASK_IDLE) until the
703 * @condition evaluates to true. The @condition is checked each time
704 * the waitqueue @wq_head is woken up.
706 * wake_up() has to be called after changing any variable that could
707 * change the result of the wait condition.
710 * 0 if the @condition evaluated to %false after the @timeout elapsed,
711 * 1 if the @condition evaluated to %true after the @timeout elapsed,
712 * or the remaining jiffies (at least 1) if the @condition evaluated
713 * to %true before the @timeout elapsed.
715 #define wait_event_idle_timeout(wq_head, condition, timeout) \
717 long __ret = timeout; \
719 if (!___wait_cond_timeout(condition)) \
720 __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
724 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
725 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
726 TASK_IDLE, 1, timeout, \
727 __ret = schedule_timeout(__ret))
730 * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
731 * @wq_head: the waitqueue to wait on
732 * @condition: a C expression for the event to wait for
733 * @timeout: timeout, in jiffies
735 * The process is put to sleep (TASK_IDLE) until the
736 * @condition evaluates to true. The @condition is checked each time
737 * the waitqueue @wq_head is woken up.
739 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
740 * set thus if other processes wait on the same list, when this
741 * process is woken further processes are not considered.
743 * wake_up() has to be called after changing any variable that could
744 * change the result of the wait condition.
747 * 0 if the @condition evaluated to %false after the @timeout elapsed,
748 * 1 if the @condition evaluated to %true after the @timeout elapsed,
749 * or the remaining jiffies (at least 1) if the @condition evaluated
750 * to %true before the @timeout elapsed.
752 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
754 long __ret = timeout; \
756 if (!___wait_cond_timeout(condition)) \
757 __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
761 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
762 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
764 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
767 DEFINE_WAIT(__wait); \
769 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
771 __ret = fn(&(wq), &__wait); \
774 } while (!(condition)); \
775 __remove_wait_queue(&(wq), &__wait); \
776 __set_current_state(TASK_RUNNING); \
782 * wait_event_interruptible_locked - 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()/spin_unlock()
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(wq, condition) \
806 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
809 * wait_event_interruptible_locked_irq - sleep 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_irq()/spin_unlock_irq()
822 * functions which must match the way they are locked/unlocked outside
825 * wake_up_locked() has to be called after changing any variable that could
826 * change the result of the wait condition.
828 * The function will return -ERESTARTSYS if it was interrupted by a
829 * signal and 0 if @condition evaluated to true.
831 #define wait_event_interruptible_locked_irq(wq, condition) \
833 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
836 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
837 * @wq: the waitqueue to wait on
838 * @condition: a C expression for the event to wait for
840 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
841 * @condition evaluates to true or a signal is received.
842 * The @condition is checked each time the waitqueue @wq is woken up.
844 * It must be called with wq.lock being held. This spinlock is
845 * unlocked while sleeping but @condition testing is done while lock
846 * is held and when this macro exits the lock is held.
848 * The lock is locked/unlocked using spin_lock()/spin_unlock()
849 * functions which must match the way they are locked/unlocked outside
852 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
853 * set thus when other process waits process on the list if this
854 * process is awaken further processes are not considered.
856 * wake_up_locked() has to be called after changing any variable that could
857 * change the result of the wait condition.
859 * The function will return -ERESTARTSYS if it was interrupted by a
860 * signal and 0 if @condition evaluated to true.
862 #define wait_event_interruptible_exclusive_locked(wq, condition) \
864 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
867 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
868 * @wq: the waitqueue to wait on
869 * @condition: a C expression for the event to wait for
871 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
872 * @condition evaluates to true or a signal is received.
873 * The @condition is checked each time the waitqueue @wq is woken up.
875 * It must be called with wq.lock being held. This spinlock is
876 * unlocked while sleeping but @condition testing is done while lock
877 * is held and when this macro exits the lock is held.
879 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
880 * functions which must match the way they are locked/unlocked outside
883 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
884 * set thus when other process waits process on the list if this
885 * process is awaken further processes are not considered.
887 * wake_up_locked() has to be called after changing any variable that could
888 * change the result of the wait condition.
890 * The function will return -ERESTARTSYS if it was interrupted by a
891 * signal and 0 if @condition evaluated to true.
893 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
895 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
898 #define __wait_event_killable(wq, condition) \
899 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
902 * wait_event_killable - sleep until a condition gets true
903 * @wq_head: the waitqueue to wait on
904 * @condition: a C expression for the event to wait for
906 * The process is put to sleep (TASK_KILLABLE) until the
907 * @condition evaluates to true or a signal is received.
908 * The @condition is checked each time the waitqueue @wq_head is woken up.
910 * wake_up() has to be called after changing any variable that could
911 * change the result of the wait condition.
913 * The function will return -ERESTARTSYS if it was interrupted by a
914 * signal and 0 if @condition evaluated to true.
916 #define wait_event_killable(wq_head, condition) \
921 __ret = __wait_event_killable(wq_head, condition); \
925 #define __wait_event_killable_timeout(wq_head, condition, timeout) \
926 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
927 TASK_KILLABLE, 0, timeout, \
928 __ret = schedule_timeout(__ret))
931 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
932 * @wq_head: the waitqueue to wait on
933 * @condition: a C expression for the event to wait for
934 * @timeout: timeout, in jiffies
936 * The process is put to sleep (TASK_KILLABLE) until the
937 * @condition evaluates to true or a kill signal is received.
938 * The @condition is checked each time the waitqueue @wq_head is woken up.
940 * wake_up() has to be called after changing any variable that could
941 * change the result of the wait condition.
944 * 0 if the @condition evaluated to %false after the @timeout elapsed,
945 * 1 if the @condition evaluated to %true after the @timeout elapsed,
946 * the remaining jiffies (at least 1) if the @condition evaluated
947 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
948 * interrupted by a kill signal.
950 * Only kill signals interrupt this process.
952 #define wait_event_killable_timeout(wq_head, condition, timeout) \
954 long __ret = timeout; \
956 if (!___wait_cond_timeout(condition)) \
957 __ret = __wait_event_killable_timeout(wq_head, \
958 condition, timeout); \
963 #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
964 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
965 spin_unlock_irq(&lock); \
968 spin_lock_irq(&lock))
971 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
972 * condition is checked under the lock. This
973 * is expected to be called with the lock
975 * @wq_head: the waitqueue to wait on
976 * @condition: a C expression for the event to wait for
977 * @lock: a locked spinlock_t, which will be released before cmd
978 * and schedule() and reacquired afterwards.
979 * @cmd: a command which is invoked outside the critical section before
982 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
983 * @condition evaluates to true. The @condition is checked each time
984 * the waitqueue @wq_head is woken up.
986 * wake_up() has to be called after changing any variable that could
987 * change the result of the wait condition.
989 * This is supposed to be called while holding the lock. The lock is
990 * dropped before invoking the cmd and going to sleep and is reacquired
993 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
997 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
1001 * wait_event_lock_irq - sleep until a condition gets true. The
1002 * condition is checked under the lock. This
1003 * is expected to be called with the lock
1005 * @wq_head: the waitqueue to wait on
1006 * @condition: a C expression for the event to wait for
1007 * @lock: a locked spinlock_t, which will be released before schedule()
1008 * and reacquired afterwards.
1010 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
1011 * @condition evaluates to true. The @condition is checked each time
1012 * the waitqueue @wq_head is woken up.
1014 * wake_up() has to be called after changing any variable that could
1015 * change the result of the wait condition.
1017 * This is supposed to be called while holding the lock. The lock is
1018 * dropped before going to sleep and is reacquired afterwards.
1020 #define wait_event_lock_irq(wq_head, condition, lock) \
1024 __wait_event_lock_irq(wq_head, condition, lock, ); \
1028 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
1029 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
1030 spin_unlock_irq(&lock); \
1033 spin_lock_irq(&lock))
1036 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
1037 * The condition is checked under the lock. This is expected to
1038 * be called with the lock taken.
1039 * @wq_head: the waitqueue to wait on
1040 * @condition: a C expression for the event to wait for
1041 * @lock: a locked spinlock_t, which will be released before cmd and
1042 * schedule() and reacquired afterwards.
1043 * @cmd: a command which is invoked outside the critical section before
1046 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1047 * @condition evaluates to true or a signal is received. The @condition is
1048 * checked each time the waitqueue @wq_head is woken up.
1050 * wake_up() has to be called after changing any variable that could
1051 * change the result of the wait condition.
1053 * This is supposed to be called while holding the lock. The lock is
1054 * dropped before invoking the cmd and going to sleep and is reacquired
1057 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1058 * and 0 if @condition evaluated to true.
1060 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
1064 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1065 condition, lock, cmd); \
1070 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1071 * The condition is checked under the lock. This is expected
1072 * to be called with the lock taken.
1073 * @wq_head: the waitqueue to wait on
1074 * @condition: a C expression for the event to wait for
1075 * @lock: a locked spinlock_t, which will be released before schedule()
1076 * and reacquired afterwards.
1078 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1079 * @condition evaluates to true or signal is received. The @condition is
1080 * checked each time the waitqueue @wq_head is woken up.
1082 * wake_up() has to be called after changing any variable that could
1083 * change the result of the wait condition.
1085 * This is supposed to be called while holding the lock. The lock is
1086 * dropped before going to sleep and is reacquired afterwards.
1088 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1089 * and 0 if @condition evaluated to true.
1091 #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
1095 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1096 condition, lock,); \
1100 #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
1101 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
1102 state, 0, timeout, \
1103 spin_unlock_irq(&lock); \
1104 __ret = schedule_timeout(__ret); \
1105 spin_lock_irq(&lock));
1108 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1109 * true or a timeout elapses. The condition is checked under
1110 * the lock. This is expected to be called with the lock taken.
1111 * @wq_head: the waitqueue to wait on
1112 * @condition: a C expression for the event to wait for
1113 * @lock: a locked spinlock_t, which will be released before schedule()
1114 * and reacquired afterwards.
1115 * @timeout: timeout, in jiffies
1117 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1118 * @condition evaluates to true or signal is received. The @condition is
1119 * checked each time the waitqueue @wq_head is woken up.
1121 * wake_up() has to be called after changing any variable that could
1122 * change the result of the wait condition.
1124 * This is supposed to be called while holding the lock. The lock is
1125 * dropped before going to sleep and is reacquired afterwards.
1127 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1128 * was interrupted by a signal, and the remaining jiffies otherwise
1129 * if the condition evaluated to true before the timeout elapsed.
1131 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1134 long __ret = timeout; \
1135 if (!___wait_cond_timeout(condition)) \
1136 __ret = __wait_event_lock_irq_timeout( \
1137 wq_head, condition, lock, timeout, \
1138 TASK_INTERRUPTIBLE); \
1142 #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout) \
1144 long __ret = timeout; \
1145 if (!___wait_cond_timeout(condition)) \
1146 __ret = __wait_event_lock_irq_timeout( \
1147 wq_head, condition, lock, timeout, \
1148 TASK_UNINTERRUPTIBLE); \
1153 * Waitqueues which are removed from the waitqueue_head at wakeup time
1155 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1156 bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1157 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1158 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1159 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1160 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1161 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1163 #define DEFINE_WAIT_FUNC(name, function) \
1164 struct wait_queue_entry name = { \
1165 .private = current, \
1167 .entry = LIST_HEAD_INIT((name).entry), \
1170 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1172 #define init_wait(wait) \
1174 (wait)->private = current; \
1175 (wait)->func = autoremove_wake_function; \
1176 INIT_LIST_HEAD(&(wait)->entry); \
1177 (wait)->flags = 0; \
1180 bool try_invoke_on_locked_down_task(struct task_struct *p, bool (*func)(struct task_struct *t, void *arg), void *arg);
1182 #endif /* _LINUX_WAIT_H */