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_single_sleeper - check if there is only one sleeper
131 * @wq_head: wait queue head
133 * Returns true of wq_head has only one sleeper on the list.
135 * Please refer to the comment for waitqueue_active.
137 static inline bool wq_has_single_sleeper(struct wait_queue_head *wq_head)
139 return list_is_singular(&wq_head->head);
143 * wq_has_sleeper - check if there are any waiting processes
144 * @wq_head: wait queue head
146 * Returns true if wq_head has waiting processes
148 * Please refer to the comment for waitqueue_active.
150 static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
153 * We need to be sure we are in sync with the
154 * add_wait_queue modifications to the wait queue.
156 * This memory barrier should be paired with one on the
160 return waitqueue_active(wq_head);
163 extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
164 extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
165 extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
167 static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
169 list_add(&wq_entry->entry, &wq_head->head);
173 * Used for wake-one threads:
176 __add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
178 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
179 __add_wait_queue(wq_head, wq_entry);
182 static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
184 list_add_tail(&wq_entry->entry, &wq_head->head);
188 __add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
190 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
191 __add_wait_queue_entry_tail(wq_head, wq_entry);
195 __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
197 list_del(&wq_entry->entry);
200 void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
201 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
202 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
203 unsigned int mode, void *key, wait_queue_entry_t *bookmark);
204 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
205 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
206 void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode, int nr);
207 void __wake_up_pollfree(struct wait_queue_head *wq_head);
209 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
210 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
211 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
212 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
213 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
215 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
216 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
217 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
218 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
221 * Wakeup macros to be used to report events to the targets.
223 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
224 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
225 #define wake_up_poll(x, m) \
226 __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
227 #define wake_up_locked_poll(x, m) \
228 __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
229 #define wake_up_interruptible_poll(x, m) \
230 __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
231 #define wake_up_interruptible_sync_poll(x, m) \
232 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, poll_to_key(m))
235 * wake_up_pollfree - signal that a polled waitqueue is going away
236 * @wq_head: the wait queue head
238 * In the very rare cases where a ->poll() implementation uses a waitqueue whose
239 * lifetime is tied to a task rather than to the 'struct file' being polled,
240 * this function must be called before the waitqueue is freed so that
241 * non-blocking polls (e.g. epoll) are notified that the queue is going away.
243 * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via
244 * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU.
246 static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
249 * For performance reasons, we don't always take the queue lock here.
250 * Therefore, we might race with someone removing the last entry from
251 * the queue, and proceed while they still hold the queue lock.
252 * However, rcu_read_lock() is required to be held in such cases, so we
253 * can safely proceed with an RCU-delayed free.
255 if (waitqueue_active(wq_head))
256 __wake_up_pollfree(wq_head);
259 #define ___wait_cond_timeout(condition) \
261 bool __cond = (condition); \
262 if (__cond && !__ret) \
267 #define ___wait_is_interruptible(state) \
268 (!__builtin_constant_p(state) || \
269 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
271 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
274 * The below macro ___wait_event() has an explicit shadow of the __ret
275 * variable when used from the wait_event_*() macros.
277 * This is so that both can use the ___wait_cond_timeout() construct
278 * to wrap the condition.
280 * The type inconsistency of the wait_event_*() __ret variable is also
281 * on purpose; we use long where we can return timeout values and int
285 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
288 struct wait_queue_entry __wq_entry; \
289 long __ret = ret; /* explicit shadow */ \
291 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
293 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
298 if (___wait_is_interruptible(state) && __int) { \
305 finish_wait(&wq_head, &__wq_entry); \
309 #define __wait_event(wq_head, condition) \
310 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
314 * wait_event - sleep until a condition gets true
315 * @wq_head: the waitqueue to wait on
316 * @condition: a C expression for the event to wait for
318 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
319 * @condition evaluates to true. The @condition is checked each time
320 * the waitqueue @wq_head is woken up.
322 * wake_up() has to be called after changing any variable that could
323 * change the result of the wait condition.
325 #define wait_event(wq_head, condition) \
330 __wait_event(wq_head, condition); \
333 #define __io_wait_event(wq_head, condition) \
334 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
338 * io_wait_event() -- like wait_event() but with io_schedule()
340 #define io_wait_event(wq_head, condition) \
345 __io_wait_event(wq_head, condition); \
348 #define __wait_event_freezable(wq_head, condition) \
349 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
350 freezable_schedule())
353 * wait_event_freezable - sleep (or freeze) until a condition gets true
354 * @wq_head: the waitqueue to wait on
355 * @condition: a C expression for the event to wait for
357 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
358 * to system load) until the @condition evaluates to true. The
359 * @condition is checked each time the waitqueue @wq_head is woken up.
361 * wake_up() has to be called after changing any variable that could
362 * change the result of the wait condition.
364 #define wait_event_freezable(wq_head, condition) \
369 __ret = __wait_event_freezable(wq_head, condition); \
373 #define __wait_event_timeout(wq_head, condition, timeout) \
374 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
375 TASK_UNINTERRUPTIBLE, 0, timeout, \
376 __ret = schedule_timeout(__ret))
379 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
380 * @wq_head: the waitqueue to wait on
381 * @condition: a C expression for the event to wait for
382 * @timeout: timeout, in jiffies
384 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
385 * @condition evaluates to true. The @condition is checked each time
386 * the waitqueue @wq_head is woken up.
388 * wake_up() has to be called after changing any variable that could
389 * change the result of the wait condition.
392 * 0 if the @condition evaluated to %false after the @timeout elapsed,
393 * 1 if the @condition evaluated to %true after the @timeout elapsed,
394 * or the remaining jiffies (at least 1) if the @condition evaluated
395 * to %true before the @timeout elapsed.
397 #define wait_event_timeout(wq_head, condition, timeout) \
399 long __ret = timeout; \
401 if (!___wait_cond_timeout(condition)) \
402 __ret = __wait_event_timeout(wq_head, condition, timeout); \
406 #define __wait_event_freezable_timeout(wq_head, condition, timeout) \
407 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
408 TASK_INTERRUPTIBLE, 0, timeout, \
409 __ret = freezable_schedule_timeout(__ret))
412 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
413 * increasing load and is freezable.
415 #define wait_event_freezable_timeout(wq_head, condition, timeout) \
417 long __ret = timeout; \
419 if (!___wait_cond_timeout(condition)) \
420 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
424 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
425 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
426 cmd1; schedule(); cmd2)
428 * Just like wait_event_cmd(), except it sets exclusive flag
430 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
434 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
437 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
438 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
439 cmd1; schedule(); cmd2)
442 * wait_event_cmd - sleep until a condition gets true
443 * @wq_head: the waitqueue to wait on
444 * @condition: a C expression for the event to wait for
445 * @cmd1: the command will be executed before sleep
446 * @cmd2: the command will be executed after sleep
448 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
449 * @condition evaluates to true. The @condition is checked each time
450 * the waitqueue @wq_head is woken up.
452 * wake_up() has to be called after changing any variable that could
453 * change the result of the wait condition.
455 #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
459 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
462 #define __wait_event_interruptible(wq_head, condition) \
463 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
467 * wait_event_interruptible - sleep until a condition gets true
468 * @wq_head: the waitqueue to wait on
469 * @condition: a C expression for the event to wait for
471 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
472 * @condition evaluates to true or a signal is received.
473 * The @condition is checked each time the waitqueue @wq_head is woken up.
475 * wake_up() has to be called after changing any variable that could
476 * change the result of the wait condition.
478 * The function will return -ERESTARTSYS if it was interrupted by a
479 * signal and 0 if @condition evaluated to true.
481 #define wait_event_interruptible(wq_head, condition) \
486 __ret = __wait_event_interruptible(wq_head, condition); \
490 #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
491 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
492 TASK_INTERRUPTIBLE, 0, timeout, \
493 __ret = schedule_timeout(__ret))
496 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
497 * @wq_head: the waitqueue to wait on
498 * @condition: a C expression for the event to wait for
499 * @timeout: timeout, in jiffies
501 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
502 * @condition evaluates to true or a signal is received.
503 * The @condition is checked each time the waitqueue @wq_head is woken up.
505 * wake_up() has to be called after changing any variable that could
506 * change the result of the wait condition.
509 * 0 if the @condition evaluated to %false after the @timeout elapsed,
510 * 1 if the @condition evaluated to %true after the @timeout elapsed,
511 * the remaining jiffies (at least 1) if the @condition evaluated
512 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
513 * interrupted by a signal.
515 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \
517 long __ret = timeout; \
519 if (!___wait_cond_timeout(condition)) \
520 __ret = __wait_event_interruptible_timeout(wq_head, \
521 condition, timeout); \
525 #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
528 struct hrtimer_sleeper __t; \
530 hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC, \
532 if ((timeout) != KTIME_MAX) { \
533 hrtimer_set_expires_range_ns(&__t.timer, timeout, \
534 current->timer_slack_ns); \
535 hrtimer_sleeper_start_expires(&__t, HRTIMER_MODE_REL); \
538 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
545 hrtimer_cancel(&__t.timer); \
546 destroy_hrtimer_on_stack(&__t.timer); \
551 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
552 * @wq_head: the waitqueue to wait on
553 * @condition: a C expression for the event to wait for
554 * @timeout: timeout, as a ktime_t
556 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
557 * @condition evaluates to true or a signal is received.
558 * The @condition is checked each time the waitqueue @wq_head is woken up.
560 * wake_up() has to be called after changing any variable that could
561 * change the result of the wait condition.
563 * The function returns 0 if @condition became true, or -ETIME if the timeout
566 #define wait_event_hrtimeout(wq_head, condition, timeout) \
571 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
572 TASK_UNINTERRUPTIBLE); \
577 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
578 * @wq: the waitqueue to wait on
579 * @condition: a C expression for the event to wait for
580 * @timeout: timeout, as a ktime_t
582 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
583 * @condition evaluates to true or a signal is received.
584 * The @condition is checked each time the waitqueue @wq is woken up.
586 * wake_up() has to be called after changing any variable that could
587 * change the result of the wait condition.
589 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
590 * interrupted by a signal, or -ETIME if the timeout elapsed.
592 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
597 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
598 TASK_INTERRUPTIBLE); \
602 #define __wait_event_interruptible_exclusive(wq, condition) \
603 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
606 #define wait_event_interruptible_exclusive(wq, condition) \
611 __ret = __wait_event_interruptible_exclusive(wq, condition); \
615 #define __wait_event_killable_exclusive(wq, condition) \
616 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
619 #define wait_event_killable_exclusive(wq, condition) \
624 __ret = __wait_event_killable_exclusive(wq, condition); \
629 #define __wait_event_freezable_exclusive(wq, condition) \
630 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
631 freezable_schedule())
633 #define wait_event_freezable_exclusive(wq, condition) \
638 __ret = __wait_event_freezable_exclusive(wq, condition); \
643 * wait_event_idle - wait for a condition without contributing to system load
644 * @wq_head: the waitqueue to wait on
645 * @condition: a C expression for the event to wait for
647 * The process is put to sleep (TASK_IDLE) until the
648 * @condition evaluates to true.
649 * The @condition is checked each time the waitqueue @wq_head is woken up.
651 * wake_up() has to be called after changing any variable that could
652 * change the result of the wait condition.
655 #define wait_event_idle(wq_head, condition) \
659 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
663 * wait_event_idle_exclusive - wait for a condition with contributing to system load
664 * @wq_head: the waitqueue to wait on
665 * @condition: a C expression for the event to wait for
667 * The process is put to sleep (TASK_IDLE) until the
668 * @condition evaluates to true.
669 * The @condition is checked each time the waitqueue @wq_head is woken up.
671 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
672 * set thus if other processes wait on the same list, when this
673 * process is woken further processes are not considered.
675 * wake_up() has to be called after changing any variable that could
676 * change the result of the wait condition.
679 #define wait_event_idle_exclusive(wq_head, condition) \
683 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
686 #define __wait_event_idle_timeout(wq_head, condition, timeout) \
687 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
688 TASK_IDLE, 0, timeout, \
689 __ret = schedule_timeout(__ret))
692 * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
693 * @wq_head: the waitqueue to wait on
694 * @condition: a C expression for the event to wait for
695 * @timeout: timeout, in jiffies
697 * The process is put to sleep (TASK_IDLE) until the
698 * @condition evaluates to true. The @condition is checked each time
699 * the waitqueue @wq_head is woken up.
701 * wake_up() has to be called after changing any variable that could
702 * change the result of the wait condition.
705 * 0 if the @condition evaluated to %false after the @timeout elapsed,
706 * 1 if the @condition evaluated to %true after the @timeout elapsed,
707 * or the remaining jiffies (at least 1) if the @condition evaluated
708 * to %true before the @timeout elapsed.
710 #define wait_event_idle_timeout(wq_head, condition, timeout) \
712 long __ret = timeout; \
714 if (!___wait_cond_timeout(condition)) \
715 __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
719 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
720 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
721 TASK_IDLE, 1, timeout, \
722 __ret = schedule_timeout(__ret))
725 * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
726 * @wq_head: the waitqueue to wait on
727 * @condition: a C expression for the event to wait for
728 * @timeout: timeout, in jiffies
730 * The process is put to sleep (TASK_IDLE) until the
731 * @condition evaluates to true. The @condition is checked each time
732 * the waitqueue @wq_head is woken up.
734 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
735 * set thus if other processes wait on the same list, when this
736 * process is woken further processes are not considered.
738 * wake_up() has to be called after changing any variable that could
739 * change the result of the wait condition.
742 * 0 if the @condition evaluated to %false after the @timeout elapsed,
743 * 1 if the @condition evaluated to %true after the @timeout elapsed,
744 * or the remaining jiffies (at least 1) if the @condition evaluated
745 * to %true before the @timeout elapsed.
747 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
749 long __ret = timeout; \
751 if (!___wait_cond_timeout(condition)) \
752 __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
756 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
757 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
759 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
762 DEFINE_WAIT(__wait); \
764 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
766 __ret = fn(&(wq), &__wait); \
769 } while (!(condition)); \
770 __remove_wait_queue(&(wq), &__wait); \
771 __set_current_state(TASK_RUNNING); \
777 * wait_event_interruptible_locked - sleep until a condition gets true
778 * @wq: the waitqueue to wait on
779 * @condition: a C expression for the event to wait for
781 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
782 * @condition evaluates to true or a signal is received.
783 * The @condition is checked each time the waitqueue @wq is woken up.
785 * It must be called with wq.lock being held. This spinlock is
786 * unlocked while sleeping but @condition testing is done while lock
787 * is held and when this macro exits the lock is held.
789 * The lock is locked/unlocked using spin_lock()/spin_unlock()
790 * functions which must match the way they are locked/unlocked outside
793 * wake_up_locked() has to be called after changing any variable that could
794 * change the result of the wait condition.
796 * The function will return -ERESTARTSYS if it was interrupted by a
797 * signal and 0 if @condition evaluated to true.
799 #define wait_event_interruptible_locked(wq, condition) \
801 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
804 * wait_event_interruptible_locked_irq - sleep until a condition gets true
805 * @wq: the waitqueue to wait on
806 * @condition: a C expression for the event to wait for
808 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
809 * @condition evaluates to true or a signal is received.
810 * The @condition is checked each time the waitqueue @wq is woken up.
812 * It must be called with wq.lock being held. This spinlock is
813 * unlocked while sleeping but @condition testing is done while lock
814 * is held and when this macro exits the lock is held.
816 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
817 * functions which must match the way they are locked/unlocked outside
820 * wake_up_locked() has to be called after changing any variable that could
821 * change the result of the wait condition.
823 * The function will return -ERESTARTSYS if it was interrupted by a
824 * signal and 0 if @condition evaluated to true.
826 #define wait_event_interruptible_locked_irq(wq, condition) \
828 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
831 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
832 * @wq: the waitqueue to wait on
833 * @condition: a C expression for the event to wait for
835 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
836 * @condition evaluates to true or a signal is received.
837 * The @condition is checked each time the waitqueue @wq is woken up.
839 * It must be called with wq.lock being held. This spinlock is
840 * unlocked while sleeping but @condition testing is done while lock
841 * is held and when this macro exits the lock is held.
843 * The lock is locked/unlocked using spin_lock()/spin_unlock()
844 * functions which must match the way they are locked/unlocked outside
847 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
848 * set thus when other process waits process on the list if this
849 * process is awaken further processes are not considered.
851 * wake_up_locked() has to be called after changing any variable that could
852 * change the result of the wait condition.
854 * The function will return -ERESTARTSYS if it was interrupted by a
855 * signal and 0 if @condition evaluated to true.
857 #define wait_event_interruptible_exclusive_locked(wq, condition) \
859 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
862 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
863 * @wq: the waitqueue to wait on
864 * @condition: a C expression for the event to wait for
866 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
867 * @condition evaluates to true or a signal is received.
868 * The @condition is checked each time the waitqueue @wq is woken up.
870 * It must be called with wq.lock being held. This spinlock is
871 * unlocked while sleeping but @condition testing is done while lock
872 * is held and when this macro exits the lock is held.
874 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
875 * functions which must match the way they are locked/unlocked outside
878 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
879 * set thus when other process waits process on the list if this
880 * process is awaken further processes are not considered.
882 * wake_up_locked() has to be called after changing any variable that could
883 * change the result of the wait condition.
885 * The function will return -ERESTARTSYS if it was interrupted by a
886 * signal and 0 if @condition evaluated to true.
888 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
890 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
893 #define __wait_event_killable(wq, condition) \
894 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
897 * wait_event_killable - sleep until a condition gets true
898 * @wq_head: the waitqueue to wait on
899 * @condition: a C expression for the event to wait for
901 * The process is put to sleep (TASK_KILLABLE) until the
902 * @condition evaluates to true or a signal is received.
903 * The @condition is checked each time the waitqueue @wq_head is woken up.
905 * wake_up() has to be called after changing any variable that could
906 * change the result of the wait condition.
908 * The function will return -ERESTARTSYS if it was interrupted by a
909 * signal and 0 if @condition evaluated to true.
911 #define wait_event_killable(wq_head, condition) \
916 __ret = __wait_event_killable(wq_head, condition); \
920 #define __wait_event_killable_timeout(wq_head, condition, timeout) \
921 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
922 TASK_KILLABLE, 0, timeout, \
923 __ret = schedule_timeout(__ret))
926 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
927 * @wq_head: the waitqueue to wait on
928 * @condition: a C expression for the event to wait for
929 * @timeout: timeout, in jiffies
931 * The process is put to sleep (TASK_KILLABLE) until the
932 * @condition evaluates to true or a kill signal is received.
933 * The @condition is checked each time the waitqueue @wq_head is woken up.
935 * wake_up() has to be called after changing any variable that could
936 * change the result of the wait condition.
939 * 0 if the @condition evaluated to %false after the @timeout elapsed,
940 * 1 if the @condition evaluated to %true after the @timeout elapsed,
941 * the remaining jiffies (at least 1) if the @condition evaluated
942 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
943 * interrupted by a kill signal.
945 * Only kill signals interrupt this process.
947 #define wait_event_killable_timeout(wq_head, condition, timeout) \
949 long __ret = timeout; \
951 if (!___wait_cond_timeout(condition)) \
952 __ret = __wait_event_killable_timeout(wq_head, \
953 condition, timeout); \
958 #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
959 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
960 spin_unlock_irq(&lock); \
963 spin_lock_irq(&lock))
966 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
967 * condition is checked under the lock. This
968 * is expected to be called with the lock
970 * @wq_head: the waitqueue to wait on
971 * @condition: a C expression for the event to wait for
972 * @lock: a locked spinlock_t, which will be released before cmd
973 * and schedule() and reacquired afterwards.
974 * @cmd: a command which is invoked outside the critical section before
977 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
978 * @condition evaluates to true. The @condition is checked each time
979 * the waitqueue @wq_head is woken up.
981 * wake_up() has to be called after changing any variable that could
982 * change the result of the wait condition.
984 * This is supposed to be called while holding the lock. The lock is
985 * dropped before invoking the cmd and going to sleep and is reacquired
988 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
992 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
996 * wait_event_lock_irq - sleep until a condition gets true. The
997 * condition is checked under the lock. This
998 * is expected to be called with the lock
1000 * @wq_head: the waitqueue to wait on
1001 * @condition: a C expression for the event to wait for
1002 * @lock: a locked spinlock_t, which will be released before schedule()
1003 * and reacquired afterwards.
1005 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
1006 * @condition evaluates to true. The @condition is checked each time
1007 * the waitqueue @wq_head is woken up.
1009 * wake_up() has to be called after changing any variable that could
1010 * change the result of the wait condition.
1012 * This is supposed to be called while holding the lock. The lock is
1013 * dropped before going to sleep and is reacquired afterwards.
1015 #define wait_event_lock_irq(wq_head, condition, lock) \
1019 __wait_event_lock_irq(wq_head, condition, lock, ); \
1023 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
1024 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
1025 spin_unlock_irq(&lock); \
1028 spin_lock_irq(&lock))
1031 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
1032 * The condition is checked under the lock. This is expected to
1033 * be called with the lock taken.
1034 * @wq_head: the waitqueue to wait on
1035 * @condition: a C expression for the event to wait for
1036 * @lock: a locked spinlock_t, which will be released before cmd and
1037 * schedule() and reacquired afterwards.
1038 * @cmd: a command which is invoked outside the critical section before
1041 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1042 * @condition evaluates to true or a signal is received. The @condition is
1043 * checked each time the waitqueue @wq_head is woken up.
1045 * wake_up() has to be called after changing any variable that could
1046 * change the result of the wait condition.
1048 * This is supposed to be called while holding the lock. The lock is
1049 * dropped before invoking the cmd and going to sleep and is reacquired
1052 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1053 * and 0 if @condition evaluated to true.
1055 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
1059 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1060 condition, lock, cmd); \
1065 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1066 * The condition is checked under the lock. This is expected
1067 * to be called with the lock taken.
1068 * @wq_head: the waitqueue to wait on
1069 * @condition: a C expression for the event to wait for
1070 * @lock: a locked spinlock_t, which will be released before schedule()
1071 * and reacquired afterwards.
1073 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1074 * @condition evaluates to true or signal is received. The @condition is
1075 * checked each time the waitqueue @wq_head is woken up.
1077 * wake_up() has to be called after changing any variable that could
1078 * change the result of the wait condition.
1080 * This is supposed to be called while holding the lock. The lock is
1081 * dropped before going to sleep and is reacquired afterwards.
1083 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1084 * and 0 if @condition evaluated to true.
1086 #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
1090 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1091 condition, lock,); \
1095 #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
1096 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
1097 state, 0, timeout, \
1098 spin_unlock_irq(&lock); \
1099 __ret = schedule_timeout(__ret); \
1100 spin_lock_irq(&lock));
1103 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1104 * true or a timeout elapses. The condition is checked under
1105 * the lock. This is expected to be called with the lock taken.
1106 * @wq_head: the waitqueue to wait on
1107 * @condition: a C expression for the event to wait for
1108 * @lock: a locked spinlock_t, which will be released before schedule()
1109 * and reacquired afterwards.
1110 * @timeout: timeout, in jiffies
1112 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1113 * @condition evaluates to true or signal is received. The @condition is
1114 * checked each time the waitqueue @wq_head is woken up.
1116 * wake_up() has to be called after changing any variable that could
1117 * change the result of the wait condition.
1119 * This is supposed to be called while holding the lock. The lock is
1120 * dropped before going to sleep and is reacquired afterwards.
1122 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1123 * was interrupted by a signal, and the remaining jiffies otherwise
1124 * if the condition evaluated to true before the timeout elapsed.
1126 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1129 long __ret = timeout; \
1130 if (!___wait_cond_timeout(condition)) \
1131 __ret = __wait_event_lock_irq_timeout( \
1132 wq_head, condition, lock, timeout, \
1133 TASK_INTERRUPTIBLE); \
1137 #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout) \
1139 long __ret = timeout; \
1140 if (!___wait_cond_timeout(condition)) \
1141 __ret = __wait_event_lock_irq_timeout( \
1142 wq_head, condition, lock, timeout, \
1143 TASK_UNINTERRUPTIBLE); \
1148 * Waitqueues which are removed from the waitqueue_head at wakeup time
1150 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1151 bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1152 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1153 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1154 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1155 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1156 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1158 #define DEFINE_WAIT_FUNC(name, function) \
1159 struct wait_queue_entry name = { \
1160 .private = current, \
1162 .entry = LIST_HEAD_INIT((name).entry), \
1165 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1167 #define init_wait(wait) \
1169 (wait)->private = current; \
1170 (wait)->func = autoremove_wake_function; \
1171 INIT_LIST_HEAD(&(wait)->entry); \
1172 (wait)->flags = 0; \
1175 #endif /* _LINUX_WAIT_H */