1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Linux wait queue related types and methods
7 #include <linux/list.h>
8 #include <linux/stddef.h>
9 #include <linux/spinlock.h>
11 #include <asm/current.h>
12 #include <uapi/linux/wait.h>
14 typedef struct wait_queue_entry wait_queue_entry_t
;
16 typedef int (*wait_queue_func_t
)(struct wait_queue_entry
*wq_entry
, unsigned mode
, int flags
, void *key
);
17 int default_wake_function(struct wait_queue_entry
*wq_entry
, unsigned mode
, int flags
, void *key
);
19 /* wait_queue_entry::flags */
20 #define WQ_FLAG_EXCLUSIVE 0x01
21 #define WQ_FLAG_WOKEN 0x02
22 #define WQ_FLAG_BOOKMARK 0x04
25 * A single wait-queue entry structure:
27 struct wait_queue_entry
{
30 wait_queue_func_t func
;
31 struct list_head entry
;
34 struct wait_queue_head
{
36 struct list_head head
;
38 typedef struct wait_queue_head wait_queue_head_t
;
43 * Macros for declaration and initialisaton of the datatypes
46 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
48 .func = default_wake_function, \
49 .entry = { NULL, NULL } }
51 #define DECLARE_WAITQUEUE(name, tsk) \
52 struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
54 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
55 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
56 .head = { &(name).head, &(name).head } }
58 #define DECLARE_WAIT_QUEUE_HEAD(name) \
59 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
61 extern void __init_waitqueue_head(struct wait_queue_head
*wq_head
, const char *name
, struct lock_class_key
*);
63 #define init_waitqueue_head(wq_head) \
65 static struct lock_class_key __key; \
67 __init_waitqueue_head((wq_head), #wq_head, &__key); \
71 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
72 ({ init_waitqueue_head(&name); name; })
73 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
74 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
76 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
79 static inline void init_waitqueue_entry(struct wait_queue_entry
*wq_entry
, struct task_struct
*p
)
82 wq_entry
->private = p
;
83 wq_entry
->func
= default_wake_function
;
87 init_waitqueue_func_entry(struct wait_queue_entry
*wq_entry
, wait_queue_func_t func
)
90 wq_entry
->private = NULL
;
91 wq_entry
->func
= func
;
95 * waitqueue_active -- locklessly test for waiters on the queue
96 * @wq_head: the waitqueue to test for waiters
98 * returns true if the wait list is not empty
100 * NOTE: this function is lockless and requires care, incorrect usage _will_
101 * lead to sporadic and non-obvious failure.
103 * Use either while holding wait_queue_head::lock or when used for wakeups
104 * with an extra smp_mb() like:
106 * CPU0 - waker CPU1 - waiter
109 * @cond = true; prepare_to_wait(&wq_head, &wait, state);
110 * smp_mb(); // smp_mb() from set_current_state()
111 * if (waitqueue_active(wq_head)) if (@cond)
112 * wake_up(wq_head); break;
115 * finish_wait(&wq_head, &wait);
117 * Because without the explicit smp_mb() it's possible for the
118 * waitqueue_active() load to get hoisted over the @cond store such that we'll
119 * observe an empty wait list while the waiter might not observe @cond.
121 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
122 * which (when the lock is uncontended) are of roughly equal cost.
124 static inline int waitqueue_active(struct wait_queue_head
*wq_head
)
126 return !list_empty(&wq_head
->head
);
130 * wq_has_sleeper - check if there are any waiting processes
131 * @wq_head: wait queue head
133 * Returns true if wq_head has waiting processes
135 * Please refer to the comment for waitqueue_active.
137 static inline bool wq_has_sleeper(struct wait_queue_head
*wq_head
)
140 * We need to be sure we are in sync with the
141 * add_wait_queue modifications to the wait queue.
143 * This memory barrier should be paired with one on the
147 return waitqueue_active(wq_head
);
150 extern void add_wait_queue(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
);
151 extern void add_wait_queue_exclusive(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
);
152 extern void remove_wait_queue(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
);
154 static inline void __add_wait_queue(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
156 list_add(&wq_entry
->entry
, &wq_head
->head
);
160 * Used for wake-one threads:
163 __add_wait_queue_exclusive(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
165 wq_entry
->flags
|= WQ_FLAG_EXCLUSIVE
;
166 __add_wait_queue(wq_head
, wq_entry
);
169 static inline void __add_wait_queue_entry_tail(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
171 list_add_tail(&wq_entry
->entry
, &wq_head
->head
);
175 __add_wait_queue_entry_tail_exclusive(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
177 wq_entry
->flags
|= WQ_FLAG_EXCLUSIVE
;
178 __add_wait_queue_entry_tail(wq_head
, wq_entry
);
182 __remove_wait_queue(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
184 list_del(&wq_entry
->entry
);
187 void __wake_up(struct wait_queue_head
*wq_head
, unsigned int mode
, int nr
, void *key
);
188 void __wake_up_locked_key(struct wait_queue_head
*wq_head
, unsigned int mode
, void *key
);
189 void __wake_up_locked_key_bookmark(struct wait_queue_head
*wq_head
,
190 unsigned int mode
, void *key
, wait_queue_entry_t
*bookmark
);
191 void __wake_up_sync_key(struct wait_queue_head
*wq_head
, unsigned int mode
, int nr
, void *key
);
192 void __wake_up_locked(struct wait_queue_head
*wq_head
, unsigned int mode
, int nr
);
193 void __wake_up_sync(struct wait_queue_head
*wq_head
, unsigned int mode
, int nr
);
195 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
196 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
197 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
198 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
199 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
201 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
202 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
203 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
204 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
207 * Wakeup macros to be used to report events to the targets.
209 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
210 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
211 #define wake_up_poll(x, m) \
212 __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
213 #define wake_up_locked_poll(x, m) \
214 __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
215 #define wake_up_interruptible_poll(x, m) \
216 __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
217 #define wake_up_interruptible_sync_poll(x, m) \
218 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, poll_to_key(m))
220 #define ___wait_cond_timeout(condition) \
222 bool __cond = (condition); \
223 if (__cond && !__ret) \
228 #define ___wait_is_interruptible(state) \
229 (!__builtin_constant_p(state) || \
230 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
232 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
235 * The below macro ___wait_event() has an explicit shadow of the __ret
236 * variable when used from the wait_event_*() macros.
238 * This is so that both can use the ___wait_cond_timeout() construct
239 * to wrap the condition.
241 * The type inconsistency of the wait_event_*() __ret variable is also
242 * on purpose; we use long where we can return timeout values and int
246 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
249 struct wait_queue_entry __wq_entry; \
250 long __ret = ret; /* explicit shadow */ \
252 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
254 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
259 if (___wait_is_interruptible(state) && __int) { \
266 finish_wait(&wq_head, &__wq_entry); \
270 #define __wait_event(wq_head, condition) \
271 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
275 * wait_event - sleep until a condition gets true
276 * @wq_head: the waitqueue to wait on
277 * @condition: a C expression for the event to wait for
279 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
280 * @condition evaluates to true. The @condition is checked each time
281 * the waitqueue @wq_head is woken up.
283 * wake_up() has to be called after changing any variable that could
284 * change the result of the wait condition.
286 #define wait_event(wq_head, condition) \
291 __wait_event(wq_head, condition); \
294 #define __io_wait_event(wq_head, condition) \
295 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
299 * io_wait_event() -- like wait_event() but with io_schedule()
301 #define io_wait_event(wq_head, condition) \
306 __io_wait_event(wq_head, condition); \
309 #define __wait_event_freezable(wq_head, condition) \
310 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
311 schedule(); try_to_freeze())
314 * wait_event_freezable - sleep (or freeze) 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_INTERRUPTIBLE -- so as not to contribute
319 * to system load) until the @condition evaluates to true. The
320 * @condition is checked each time 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_freezable(wq_head, condition) \
330 __ret = __wait_event_freezable(wq_head, condition); \
334 #define __wait_event_timeout(wq_head, condition, timeout) \
335 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
336 TASK_UNINTERRUPTIBLE, 0, timeout, \
337 __ret = schedule_timeout(__ret))
340 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
341 * @wq_head: the waitqueue to wait on
342 * @condition: a C expression for the event to wait for
343 * @timeout: timeout, in jiffies
345 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
346 * @condition evaluates to true. The @condition is checked each time
347 * the waitqueue @wq_head is woken up.
349 * wake_up() has to be called after changing any variable that could
350 * change the result of the wait condition.
353 * 0 if the @condition evaluated to %false after the @timeout elapsed,
354 * 1 if the @condition evaluated to %true after the @timeout elapsed,
355 * or the remaining jiffies (at least 1) if the @condition evaluated
356 * to %true before the @timeout elapsed.
358 #define wait_event_timeout(wq_head, condition, timeout) \
360 long __ret = timeout; \
362 if (!___wait_cond_timeout(condition)) \
363 __ret = __wait_event_timeout(wq_head, condition, timeout); \
367 #define __wait_event_freezable_timeout(wq_head, condition, timeout) \
368 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
369 TASK_INTERRUPTIBLE, 0, timeout, \
370 __ret = schedule_timeout(__ret); try_to_freeze())
373 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
374 * increasing load and is freezable.
376 #define wait_event_freezable_timeout(wq_head, condition, timeout) \
378 long __ret = timeout; \
380 if (!___wait_cond_timeout(condition)) \
381 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
385 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
386 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
387 cmd1; schedule(); cmd2)
389 * Just like wait_event_cmd(), except it sets exclusive flag
391 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
395 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
398 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
399 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
400 cmd1; schedule(); cmd2)
403 * wait_event_cmd - sleep until a condition gets true
404 * @wq_head: the waitqueue to wait on
405 * @condition: a C expression for the event to wait for
406 * @cmd1: the command will be executed before sleep
407 * @cmd2: the command will be executed after sleep
409 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
410 * @condition evaluates to true. The @condition is checked each time
411 * the waitqueue @wq_head is woken up.
413 * wake_up() has to be called after changing any variable that could
414 * change the result of the wait condition.
416 #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
420 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
423 #define __wait_event_interruptible(wq_head, condition) \
424 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
428 * wait_event_interruptible - sleep until a condition gets true
429 * @wq_head: the waitqueue to wait on
430 * @condition: a C expression for the event to wait for
432 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
433 * @condition evaluates to true or a signal is received.
434 * The @condition is checked each time the waitqueue @wq_head is woken up.
436 * wake_up() has to be called after changing any variable that could
437 * change the result of the wait condition.
439 * The function will return -ERESTARTSYS if it was interrupted by a
440 * signal and 0 if @condition evaluated to true.
442 #define wait_event_interruptible(wq_head, condition) \
447 __ret = __wait_event_interruptible(wq_head, condition); \
451 #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
452 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
453 TASK_INTERRUPTIBLE, 0, timeout, \
454 __ret = schedule_timeout(__ret))
457 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
458 * @wq_head: the waitqueue to wait on
459 * @condition: a C expression for the event to wait for
460 * @timeout: timeout, in jiffies
462 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
463 * @condition evaluates to true or a signal is received.
464 * The @condition is checked each time the waitqueue @wq_head is woken up.
466 * wake_up() has to be called after changing any variable that could
467 * change the result of the wait condition.
470 * 0 if the @condition evaluated to %false after the @timeout elapsed,
471 * 1 if the @condition evaluated to %true after the @timeout elapsed,
472 * the remaining jiffies (at least 1) if the @condition evaluated
473 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
474 * interrupted by a signal.
476 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \
478 long __ret = timeout; \
480 if (!___wait_cond_timeout(condition)) \
481 __ret = __wait_event_interruptible_timeout(wq_head, \
482 condition, timeout); \
486 #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
489 struct hrtimer_sleeper __t; \
491 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); \
492 hrtimer_init_sleeper(&__t, current); \
493 if ((timeout) != KTIME_MAX) \
494 hrtimer_start_range_ns(&__t.timer, timeout, \
495 current->timer_slack_ns, \
498 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
505 hrtimer_cancel(&__t.timer); \
506 destroy_hrtimer_on_stack(&__t.timer); \
511 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
512 * @wq_head: the waitqueue to wait on
513 * @condition: a C expression for the event to wait for
514 * @timeout: timeout, as a ktime_t
516 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
517 * @condition evaluates to true or a signal is received.
518 * The @condition is checked each time the waitqueue @wq_head is woken up.
520 * wake_up() has to be called after changing any variable that could
521 * change the result of the wait condition.
523 * The function returns 0 if @condition became true, or -ETIME if the timeout
526 #define wait_event_hrtimeout(wq_head, condition, timeout) \
531 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
532 TASK_UNINTERRUPTIBLE); \
537 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
538 * @wq: the waitqueue to wait on
539 * @condition: a C expression for the event to wait for
540 * @timeout: timeout, as a ktime_t
542 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
543 * @condition evaluates to true or a signal is received.
544 * The @condition is checked each time the waitqueue @wq is woken up.
546 * wake_up() has to be called after changing any variable that could
547 * change the result of the wait condition.
549 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
550 * interrupted by a signal, or -ETIME if the timeout elapsed.
552 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
557 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
558 TASK_INTERRUPTIBLE); \
562 #define __wait_event_interruptible_exclusive(wq, condition) \
563 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
566 #define wait_event_interruptible_exclusive(wq, condition) \
571 __ret = __wait_event_interruptible_exclusive(wq, condition); \
575 #define __wait_event_killable_exclusive(wq, condition) \
576 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
579 #define wait_event_killable_exclusive(wq, condition) \
584 __ret = __wait_event_killable_exclusive(wq, condition); \
589 #define __wait_event_freezable_exclusive(wq, condition) \
590 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
591 schedule(); try_to_freeze())
593 #define wait_event_freezable_exclusive(wq, condition) \
598 __ret = __wait_event_freezable_exclusive(wq, condition); \
603 * wait_event_idle - wait for a condition without contributing to system load
604 * @wq_head: the waitqueue to wait on
605 * @condition: a C expression for the event to wait for
607 * The process is put to sleep (TASK_IDLE) until the
608 * @condition evaluates to true.
609 * The @condition is checked each time the waitqueue @wq_head is woken up.
611 * wake_up() has to be called after changing any variable that could
612 * change the result of the wait condition.
615 #define wait_event_idle(wq_head, condition) \
619 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
623 * wait_event_idle_exclusive - wait for a condition with contributing to system load
624 * @wq_head: the waitqueue to wait on
625 * @condition: a C expression for the event to wait for
627 * The process is put to sleep (TASK_IDLE) until the
628 * @condition evaluates to true.
629 * The @condition is checked each time the waitqueue @wq_head is woken up.
631 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
632 * set thus if other processes wait on the same list, when this
633 * process is woken further processes are not considered.
635 * wake_up() has to be called after changing any variable that could
636 * change the result of the wait condition.
639 #define wait_event_idle_exclusive(wq_head, condition) \
643 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
646 #define __wait_event_idle_timeout(wq_head, condition, timeout) \
647 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
648 TASK_IDLE, 0, timeout, \
649 __ret = schedule_timeout(__ret))
652 * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
653 * @wq_head: the waitqueue to wait on
654 * @condition: a C expression for the event to wait for
655 * @timeout: timeout, in jiffies
657 * The process is put to sleep (TASK_IDLE) until the
658 * @condition evaluates to true. The @condition is checked each time
659 * the waitqueue @wq_head is woken up.
661 * wake_up() has to be called after changing any variable that could
662 * change the result of the wait condition.
665 * 0 if the @condition evaluated to %false after the @timeout elapsed,
666 * 1 if the @condition evaluated to %true after the @timeout elapsed,
667 * or the remaining jiffies (at least 1) if the @condition evaluated
668 * to %true before the @timeout elapsed.
670 #define wait_event_idle_timeout(wq_head, condition, timeout) \
672 long __ret = timeout; \
674 if (!___wait_cond_timeout(condition)) \
675 __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
679 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
680 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
681 TASK_IDLE, 1, timeout, \
682 __ret = schedule_timeout(__ret))
685 * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
686 * @wq_head: the waitqueue to wait on
687 * @condition: a C expression for the event to wait for
688 * @timeout: timeout, in jiffies
690 * The process is put to sleep (TASK_IDLE) until the
691 * @condition evaluates to true. The @condition is checked each time
692 * the waitqueue @wq_head is woken up.
694 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
695 * set thus if other processes wait on the same list, when this
696 * process is woken further processes are not considered.
698 * wake_up() has to be called after changing any variable that could
699 * change the result of the wait condition.
702 * 0 if the @condition evaluated to %false after the @timeout elapsed,
703 * 1 if the @condition evaluated to %true after the @timeout elapsed,
704 * or the remaining jiffies (at least 1) if the @condition evaluated
705 * to %true before the @timeout elapsed.
707 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
709 long __ret = timeout; \
711 if (!___wait_cond_timeout(condition)) \
712 __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
716 extern int do_wait_intr(wait_queue_head_t
*, wait_queue_entry_t
*);
717 extern int do_wait_intr_irq(wait_queue_head_t
*, wait_queue_entry_t
*);
719 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
722 DEFINE_WAIT(__wait); \
724 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
726 __ret = fn(&(wq), &__wait); \
729 } while (!(condition)); \
730 __remove_wait_queue(&(wq), &__wait); \
731 __set_current_state(TASK_RUNNING); \
737 * wait_event_interruptible_locked - sleep until a condition gets true
738 * @wq: the waitqueue to wait on
739 * @condition: a C expression for the event to wait for
741 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
742 * @condition evaluates to true or a signal is received.
743 * The @condition is checked each time the waitqueue @wq is woken up.
745 * It must be called with wq.lock being held. This spinlock is
746 * unlocked while sleeping but @condition testing is done while lock
747 * is held and when this macro exits the lock is held.
749 * The lock is locked/unlocked using spin_lock()/spin_unlock()
750 * functions which must match the way they are locked/unlocked outside
753 * wake_up_locked() has to be called after changing any variable that could
754 * change the result of the wait condition.
756 * The function will return -ERESTARTSYS if it was interrupted by a
757 * signal and 0 if @condition evaluated to true.
759 #define wait_event_interruptible_locked(wq, condition) \
761 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
764 * wait_event_interruptible_locked_irq - sleep until a condition gets true
765 * @wq: the waitqueue to wait on
766 * @condition: a C expression for the event to wait for
768 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
769 * @condition evaluates to true or a signal is received.
770 * The @condition is checked each time the waitqueue @wq is woken up.
772 * It must be called with wq.lock being held. This spinlock is
773 * unlocked while sleeping but @condition testing is done while lock
774 * is held and when this macro exits the lock is held.
776 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
777 * functions which must match the way they are locked/unlocked outside
780 * wake_up_locked() has to be called after changing any variable that could
781 * change the result of the wait condition.
783 * The function will return -ERESTARTSYS if it was interrupted by a
784 * signal and 0 if @condition evaluated to true.
786 #define wait_event_interruptible_locked_irq(wq, condition) \
788 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
791 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
792 * @wq: the waitqueue to wait on
793 * @condition: a C expression for the event to wait for
795 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
796 * @condition evaluates to true or a signal is received.
797 * The @condition is checked each time the waitqueue @wq is woken up.
799 * It must be called with wq.lock being held. This spinlock is
800 * unlocked while sleeping but @condition testing is done while lock
801 * is held and when this macro exits the lock is held.
803 * The lock is locked/unlocked using spin_lock()/spin_unlock()
804 * functions which must match the way they are locked/unlocked outside
807 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
808 * set thus when other process waits process on the list if this
809 * process is awaken further processes are not considered.
811 * wake_up_locked() has to be called after changing any variable that could
812 * change the result of the wait condition.
814 * The function will return -ERESTARTSYS if it was interrupted by a
815 * signal and 0 if @condition evaluated to true.
817 #define wait_event_interruptible_exclusive_locked(wq, condition) \
819 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
822 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
823 * @wq: the waitqueue to wait on
824 * @condition: a C expression for the event to wait for
826 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
827 * @condition evaluates to true or a signal is received.
828 * The @condition is checked each time the waitqueue @wq is woken up.
830 * It must be called with wq.lock being held. This spinlock is
831 * unlocked while sleeping but @condition testing is done while lock
832 * is held and when this macro exits the lock is held.
834 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
835 * functions which must match the way they are locked/unlocked outside
838 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
839 * set thus when other process waits process on the list if this
840 * process is awaken further processes are not considered.
842 * wake_up_locked() has to be called after changing any variable that could
843 * change the result of the wait condition.
845 * The function will return -ERESTARTSYS if it was interrupted by a
846 * signal and 0 if @condition evaluated to true.
848 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
850 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
853 #define __wait_event_killable(wq, condition) \
854 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
857 * wait_event_killable - sleep until a condition gets true
858 * @wq_head: the waitqueue to wait on
859 * @condition: a C expression for the event to wait for
861 * The process is put to sleep (TASK_KILLABLE) until the
862 * @condition evaluates to true or a signal is received.
863 * The @condition is checked each time the waitqueue @wq_head is woken up.
865 * wake_up() has to be called after changing any variable that could
866 * change the result of the wait condition.
868 * The function will return -ERESTARTSYS if it was interrupted by a
869 * signal and 0 if @condition evaluated to true.
871 #define wait_event_killable(wq_head, condition) \
876 __ret = __wait_event_killable(wq_head, condition); \
880 #define __wait_event_killable_timeout(wq_head, condition, timeout) \
881 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
882 TASK_KILLABLE, 0, timeout, \
883 __ret = schedule_timeout(__ret))
886 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
887 * @wq_head: the waitqueue to wait on
888 * @condition: a C expression for the event to wait for
889 * @timeout: timeout, in jiffies
891 * The process is put to sleep (TASK_KILLABLE) until the
892 * @condition evaluates to true or a kill signal is received.
893 * The @condition is checked each time the waitqueue @wq_head is woken up.
895 * wake_up() has to be called after changing any variable that could
896 * change the result of the wait condition.
899 * 0 if the @condition evaluated to %false after the @timeout elapsed,
900 * 1 if the @condition evaluated to %true after the @timeout elapsed,
901 * the remaining jiffies (at least 1) if the @condition evaluated
902 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
903 * interrupted by a kill signal.
905 * Only kill signals interrupt this process.
907 #define wait_event_killable_timeout(wq_head, condition, timeout) \
909 long __ret = timeout; \
911 if (!___wait_cond_timeout(condition)) \
912 __ret = __wait_event_killable_timeout(wq_head, \
913 condition, timeout); \
918 #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
919 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
920 spin_unlock_irq(&lock); \
923 spin_lock_irq(&lock))
926 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
927 * condition is checked under the lock. This
928 * is expected to be called with the lock
930 * @wq_head: the waitqueue to wait on
931 * @condition: a C expression for the event to wait for
932 * @lock: a locked spinlock_t, which will be released before cmd
933 * and schedule() and reacquired afterwards.
934 * @cmd: a command which is invoked outside the critical section before
937 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
938 * @condition evaluates to true. The @condition is checked each time
939 * the waitqueue @wq_head is woken up.
941 * wake_up() has to be called after changing any variable that could
942 * change the result of the wait condition.
944 * This is supposed to be called while holding the lock. The lock is
945 * dropped before invoking the cmd and going to sleep and is reacquired
948 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
952 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
956 * wait_event_lock_irq - sleep until a condition gets true. The
957 * condition is checked under the lock. This
958 * is expected to be called with the lock
960 * @wq_head: the waitqueue to wait on
961 * @condition: a C expression for the event to wait for
962 * @lock: a locked spinlock_t, which will be released before schedule()
963 * and reacquired afterwards.
965 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
966 * @condition evaluates to true. The @condition is checked each time
967 * the waitqueue @wq_head is woken up.
969 * wake_up() has to be called after changing any variable that could
970 * change the result of the wait condition.
972 * This is supposed to be called while holding the lock. The lock is
973 * dropped before going to sleep and is reacquired afterwards.
975 #define wait_event_lock_irq(wq_head, condition, lock) \
979 __wait_event_lock_irq(wq_head, condition, lock, ); \
983 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
984 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
985 spin_unlock_irq(&lock); \
988 spin_lock_irq(&lock))
991 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
992 * The condition is checked under the lock. This is expected to
993 * be called with the lock taken.
994 * @wq_head: the waitqueue to wait on
995 * @condition: a C expression for the event to wait for
996 * @lock: a locked spinlock_t, which will be released before cmd and
997 * schedule() and reacquired afterwards.
998 * @cmd: a command which is invoked outside the critical section before
1001 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1002 * @condition evaluates to true or a signal is received. The @condition is
1003 * checked each time the waitqueue @wq_head is woken up.
1005 * wake_up() has to be called after changing any variable that could
1006 * change the result of the wait condition.
1008 * This is supposed to be called while holding the lock. The lock is
1009 * dropped before invoking the cmd and going to sleep and is reacquired
1012 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1013 * and 0 if @condition evaluated to true.
1015 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
1019 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1020 condition, lock, cmd); \
1025 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1026 * The condition is checked under the lock. This is expected
1027 * to be called with the lock taken.
1028 * @wq_head: the waitqueue to wait on
1029 * @condition: a C expression for the event to wait for
1030 * @lock: a locked spinlock_t, which will be released before schedule()
1031 * and reacquired afterwards.
1033 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1034 * @condition evaluates to true or signal is received. The @condition is
1035 * checked each time the waitqueue @wq_head is woken up.
1037 * wake_up() has to be called after changing any variable that could
1038 * change the result of the wait condition.
1040 * This is supposed to be called while holding the lock. The lock is
1041 * dropped before going to sleep and is reacquired afterwards.
1043 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1044 * and 0 if @condition evaluated to true.
1046 #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
1050 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1051 condition, lock,); \
1055 #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
1056 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
1057 state, 0, timeout, \
1058 spin_unlock_irq(&lock); \
1059 __ret = schedule_timeout(__ret); \
1060 spin_lock_irq(&lock));
1063 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1064 * true or a timeout elapses. The condition is checked under
1065 * the lock. This is expected to be called with the lock taken.
1066 * @wq_head: the waitqueue to wait on
1067 * @condition: a C expression for the event to wait for
1068 * @lock: a locked spinlock_t, which will be released before schedule()
1069 * and reacquired afterwards.
1070 * @timeout: timeout, in jiffies
1072 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1073 * @condition evaluates to true or signal is received. The @condition is
1074 * checked each time the waitqueue @wq_head is woken up.
1076 * wake_up() has to be called after changing any variable that could
1077 * change the result of the wait condition.
1079 * This is supposed to be called while holding the lock. The lock is
1080 * dropped before going to sleep and is reacquired afterwards.
1082 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1083 * was interrupted by a signal, and the remaining jiffies otherwise
1084 * if the condition evaluated to true before the timeout elapsed.
1086 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1089 long __ret = timeout; \
1090 if (!___wait_cond_timeout(condition)) \
1091 __ret = __wait_event_lock_irq_timeout( \
1092 wq_head, condition, lock, timeout, \
1093 TASK_INTERRUPTIBLE); \
1097 #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout) \
1099 long __ret = timeout; \
1100 if (!___wait_cond_timeout(condition)) \
1101 __ret = __wait_event_lock_irq_timeout( \
1102 wq_head, condition, lock, timeout, \
1103 TASK_UNINTERRUPTIBLE); \
1108 * Waitqueues which are removed from the waitqueue_head at wakeup time
1110 void prepare_to_wait(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
, int state
);
1111 void prepare_to_wait_exclusive(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
, int state
);
1112 long prepare_to_wait_event(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
, int state
);
1113 void finish_wait(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
);
1114 long wait_woken(struct wait_queue_entry
*wq_entry
, unsigned mode
, long timeout
);
1115 int woken_wake_function(struct wait_queue_entry
*wq_entry
, unsigned mode
, int sync
, void *key
);
1116 int autoremove_wake_function(struct wait_queue_entry
*wq_entry
, unsigned mode
, int sync
, void *key
);
1118 #define DEFINE_WAIT_FUNC(name, function) \
1119 struct wait_queue_entry name = { \
1120 .private = current, \
1122 .entry = LIST_HEAD_INIT((name).entry), \
1125 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1127 #define init_wait(wait) \
1129 (wait)->private = current; \
1130 (wait)->func = autoremove_wake_function; \
1131 INIT_LIST_HEAD(&(wait)->entry); \
1132 (wait)->flags = 0; \
1135 #endif /* _LINUX_WAIT_H */