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
26 * A single wait-queue entry structure:
28 struct wait_queue_entry {
31 wait_queue_func_t func;
32 struct list_head entry;
35 struct wait_queue_head {
37 struct list_head head;
39 typedef struct wait_queue_head wait_queue_head_t;
44 * Macros for declaration and initialisaton of the datatypes
47 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
49 .func = default_wake_function, \
50 .entry = { NULL, NULL } }
52 #define DECLARE_WAITQUEUE(name, tsk) \
53 struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
55 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
56 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
57 .head = { &(name).head, &(name).head } }
59 #define DECLARE_WAIT_QUEUE_HEAD(name) \
60 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
62 extern void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *);
64 #define init_waitqueue_head(wq_head) \
66 static struct lock_class_key __key; \
68 __init_waitqueue_head((wq_head), #wq_head, &__key); \
72 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
73 ({ init_waitqueue_head(&name); name; })
74 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
75 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
77 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
80 static inline void init_waitqueue_entry(struct wait_queue_entry *wq_entry, struct task_struct *p)
83 wq_entry->private = p;
84 wq_entry->func = default_wake_function;
88 init_waitqueue_func_entry(struct wait_queue_entry *wq_entry, wait_queue_func_t func)
91 wq_entry->private = NULL;
92 wq_entry->func = func;
96 * waitqueue_active -- locklessly test for waiters on the queue
97 * @wq_head: the waitqueue to test for waiters
99 * returns true if the wait list is not empty
101 * NOTE: this function is lockless and requires care, incorrect usage _will_
102 * lead to sporadic and non-obvious failure.
104 * Use either while holding wait_queue_head::lock or when used for wakeups
105 * with an extra smp_mb() like::
107 * CPU0 - waker CPU1 - waiter
110 * @cond = true; prepare_to_wait(&wq_head, &wait, state);
111 * smp_mb(); // smp_mb() from set_current_state()
112 * if (waitqueue_active(wq_head)) if (@cond)
113 * wake_up(wq_head); break;
116 * finish_wait(&wq_head, &wait);
118 * Because without the explicit smp_mb() it's possible for the
119 * waitqueue_active() load to get hoisted over the @cond store such that we'll
120 * observe an empty wait list while the waiter might not observe @cond.
122 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
123 * which (when the lock is uncontended) are of roughly equal cost.
125 static inline int waitqueue_active(struct wait_queue_head *wq_head)
127 return !list_empty(&wq_head->head);
131 * wq_has_single_sleeper - check if there is only one sleeper
132 * @wq_head: wait queue head
134 * Returns true of wq_head has only one sleeper on the list.
136 * Please refer to the comment for waitqueue_active.
138 static inline bool wq_has_single_sleeper(struct wait_queue_head *wq_head)
140 return list_is_singular(&wq_head->head);
144 * wq_has_sleeper - check if there are any waiting processes
145 * @wq_head: wait queue head
147 * Returns true if wq_head has waiting processes
149 * Please refer to the comment for waitqueue_active.
151 static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
154 * We need to be sure we are in sync with the
155 * add_wait_queue modifications to the wait queue.
157 * This memory barrier should be paired with one on the
161 return waitqueue_active(wq_head);
164 extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
165 extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
166 extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
168 static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
170 list_add(&wq_entry->entry, &wq_head->head);
174 * Used for wake-one threads:
177 __add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
179 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
180 __add_wait_queue(wq_head, wq_entry);
183 static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
185 list_add_tail(&wq_entry->entry, &wq_head->head);
189 __add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
191 wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
192 __add_wait_queue_entry_tail(wq_head, wq_entry);
196 __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
198 list_del(&wq_entry->entry);
201 void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
202 void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
203 void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
204 unsigned int mode, void *key, wait_queue_entry_t *bookmark);
205 void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
206 void __wake_up_locked_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
207 void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
208 void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode);
210 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
211 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
212 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
213 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
214 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
216 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
217 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
218 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
219 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE)
222 * Wakeup macros to be used to report events to the targets.
224 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
225 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
226 #define wake_up_poll(x, m) \
227 __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
228 #define wake_up_locked_poll(x, m) \
229 __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
230 #define wake_up_interruptible_poll(x, m) \
231 __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
232 #define wake_up_interruptible_sync_poll(x, m) \
233 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
234 #define wake_up_interruptible_sync_poll_locked(x, m) \
235 __wake_up_locked_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
237 #define ___wait_cond_timeout(condition) \
239 bool __cond = (condition); \
240 if (__cond && !__ret) \
245 #define ___wait_is_interruptible(state) \
246 (!__builtin_constant_p(state) || \
247 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
249 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
252 * The below macro ___wait_event() has an explicit shadow of the __ret
253 * variable when used from the wait_event_*() macros.
255 * This is so that both can use the ___wait_cond_timeout() construct
256 * to wrap the condition.
258 * The type inconsistency of the wait_event_*() __ret variable is also
259 * on purpose; we use long where we can return timeout values and int
263 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
266 struct wait_queue_entry __wq_entry; \
267 long __ret = ret; /* explicit shadow */ \
269 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
271 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
276 if (___wait_is_interruptible(state) && __int) { \
283 finish_wait(&wq_head, &__wq_entry); \
287 #define __wait_event(wq_head, condition) \
288 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
292 * wait_event - sleep until a condition gets true
293 * @wq_head: the waitqueue to wait on
294 * @condition: a C expression for the event to wait for
296 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
297 * @condition evaluates to true. The @condition is checked each time
298 * the waitqueue @wq_head is woken up.
300 * wake_up() has to be called after changing any variable that could
301 * change the result of the wait condition.
303 #define wait_event(wq_head, condition) \
308 __wait_event(wq_head, condition); \
311 #define __io_wait_event(wq_head, condition) \
312 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
316 * io_wait_event() -- like wait_event() but with io_schedule()
318 #define io_wait_event(wq_head, condition) \
323 __io_wait_event(wq_head, condition); \
326 #define __wait_event_freezable(wq_head, condition) \
327 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
328 freezable_schedule())
331 * wait_event_freezable - sleep (or freeze) until a condition gets true
332 * @wq_head: the waitqueue to wait on
333 * @condition: a C expression for the event to wait for
335 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
336 * to system load) until the @condition evaluates to true. The
337 * @condition is checked each time the waitqueue @wq_head is woken up.
339 * wake_up() has to be called after changing any variable that could
340 * change the result of the wait condition.
342 #define wait_event_freezable(wq_head, condition) \
347 __ret = __wait_event_freezable(wq_head, condition); \
351 #define __wait_event_timeout(wq_head, condition, timeout) \
352 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
353 TASK_UNINTERRUPTIBLE, 0, timeout, \
354 __ret = schedule_timeout(__ret))
357 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
358 * @wq_head: the waitqueue to wait on
359 * @condition: a C expression for the event to wait for
360 * @timeout: timeout, in jiffies
362 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
363 * @condition evaluates to true. The @condition is checked each time
364 * 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.
370 * 0 if the @condition evaluated to %false after the @timeout elapsed,
371 * 1 if the @condition evaluated to %true after the @timeout elapsed,
372 * or the remaining jiffies (at least 1) if the @condition evaluated
373 * to %true before the @timeout elapsed.
375 #define wait_event_timeout(wq_head, condition, timeout) \
377 long __ret = timeout; \
379 if (!___wait_cond_timeout(condition)) \
380 __ret = __wait_event_timeout(wq_head, condition, timeout); \
384 #define __wait_event_freezable_timeout(wq_head, condition, timeout) \
385 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
386 TASK_INTERRUPTIBLE, 0, timeout, \
387 __ret = freezable_schedule_timeout(__ret))
390 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
391 * increasing load and is freezable.
393 #define wait_event_freezable_timeout(wq_head, condition, timeout) \
395 long __ret = timeout; \
397 if (!___wait_cond_timeout(condition)) \
398 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
402 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
403 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
404 cmd1; schedule(); cmd2)
406 * Just like wait_event_cmd(), except it sets exclusive flag
408 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
412 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
415 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
416 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
417 cmd1; schedule(); cmd2)
420 * wait_event_cmd - sleep until a condition gets true
421 * @wq_head: the waitqueue to wait on
422 * @condition: a C expression for the event to wait for
423 * @cmd1: the command will be executed before sleep
424 * @cmd2: the command will be executed after sleep
426 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
427 * @condition evaluates to true. The @condition is checked each time
428 * the waitqueue @wq_head is woken up.
430 * wake_up() has to be called after changing any variable that could
431 * change the result of the wait condition.
433 #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
437 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
440 #define __wait_event_interruptible(wq_head, condition) \
441 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
445 * wait_event_interruptible - sleep until a condition gets true
446 * @wq_head: the waitqueue to wait on
447 * @condition: a C expression for the event to wait for
449 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
450 * @condition evaluates to true or a signal is received.
451 * The @condition is checked each time the waitqueue @wq_head is woken up.
453 * wake_up() has to be called after changing any variable that could
454 * change the result of the wait condition.
456 * The function will return -ERESTARTSYS if it was interrupted by a
457 * signal and 0 if @condition evaluated to true.
459 #define wait_event_interruptible(wq_head, condition) \
464 __ret = __wait_event_interruptible(wq_head, condition); \
468 #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
469 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
470 TASK_INTERRUPTIBLE, 0, timeout, \
471 __ret = schedule_timeout(__ret))
474 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
475 * @wq_head: the waitqueue to wait on
476 * @condition: a C expression for the event to wait for
477 * @timeout: timeout, in jiffies
479 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
480 * @condition evaluates to true or a signal is received.
481 * The @condition is checked each time the waitqueue @wq_head is woken up.
483 * wake_up() has to be called after changing any variable that could
484 * change the result of the wait condition.
487 * 0 if the @condition evaluated to %false after the @timeout elapsed,
488 * 1 if the @condition evaluated to %true after the @timeout elapsed,
489 * the remaining jiffies (at least 1) if the @condition evaluated
490 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
491 * interrupted by a signal.
493 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \
495 long __ret = timeout; \
497 if (!___wait_cond_timeout(condition)) \
498 __ret = __wait_event_interruptible_timeout(wq_head, \
499 condition, timeout); \
503 #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
506 struct hrtimer_sleeper __t; \
508 hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC, \
510 if ((timeout) != KTIME_MAX) \
511 hrtimer_start_range_ns(&__t.timer, timeout, \
512 current->timer_slack_ns, \
515 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
522 hrtimer_cancel(&__t.timer); \
523 destroy_hrtimer_on_stack(&__t.timer); \
528 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
529 * @wq_head: the waitqueue to wait on
530 * @condition: a C expression for the event to wait for
531 * @timeout: timeout, as a ktime_t
533 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
534 * @condition evaluates to true or a signal is received.
535 * The @condition is checked each time the waitqueue @wq_head is woken up.
537 * wake_up() has to be called after changing any variable that could
538 * change the result of the wait condition.
540 * The function returns 0 if @condition became true, or -ETIME if the timeout
543 #define wait_event_hrtimeout(wq_head, condition, timeout) \
548 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
549 TASK_UNINTERRUPTIBLE); \
554 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
555 * @wq: the waitqueue to wait on
556 * @condition: a C expression for the event to wait for
557 * @timeout: timeout, as a ktime_t
559 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
560 * @condition evaluates to true or a signal is received.
561 * The @condition is checked each time the waitqueue @wq is woken up.
563 * wake_up() has to be called after changing any variable that could
564 * change the result of the wait condition.
566 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
567 * interrupted by a signal, or -ETIME if the timeout elapsed.
569 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
574 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
575 TASK_INTERRUPTIBLE); \
579 #define __wait_event_interruptible_exclusive(wq, condition) \
580 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
583 #define wait_event_interruptible_exclusive(wq, condition) \
588 __ret = __wait_event_interruptible_exclusive(wq, condition); \
592 #define __wait_event_killable_exclusive(wq, condition) \
593 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
596 #define wait_event_killable_exclusive(wq, condition) \
601 __ret = __wait_event_killable_exclusive(wq, condition); \
606 #define __wait_event_freezable_exclusive(wq, condition) \
607 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
608 freezable_schedule())
610 #define wait_event_freezable_exclusive(wq, condition) \
615 __ret = __wait_event_freezable_exclusive(wq, condition); \
620 * wait_event_idle - wait for a condition without contributing to system load
621 * @wq_head: the waitqueue to wait on
622 * @condition: a C expression for the event to wait for
624 * The process is put to sleep (TASK_IDLE) until the
625 * @condition evaluates to true.
626 * The @condition is checked each time the waitqueue @wq_head is woken up.
628 * wake_up() has to be called after changing any variable that could
629 * change the result of the wait condition.
632 #define wait_event_idle(wq_head, condition) \
636 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
640 * wait_event_idle_exclusive - wait for a condition with contributing to system load
641 * @wq_head: the waitqueue to wait on
642 * @condition: a C expression for the event to wait for
644 * The process is put to sleep (TASK_IDLE) until the
645 * @condition evaluates to true.
646 * The @condition is checked each time the waitqueue @wq_head is woken up.
648 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
649 * set thus if other processes wait on the same list, when this
650 * process is woken further processes are not considered.
652 * wake_up() has to be called after changing any variable that could
653 * change the result of the wait condition.
656 #define wait_event_idle_exclusive(wq_head, condition) \
660 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
663 #define __wait_event_idle_timeout(wq_head, condition, timeout) \
664 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
665 TASK_IDLE, 0, timeout, \
666 __ret = schedule_timeout(__ret))
669 * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
670 * @wq_head: the waitqueue to wait on
671 * @condition: a C expression for the event to wait for
672 * @timeout: timeout, in jiffies
674 * The process is put to sleep (TASK_IDLE) until the
675 * @condition evaluates to true. The @condition is checked each time
676 * the waitqueue @wq_head is woken up.
678 * wake_up() has to be called after changing any variable that could
679 * change the result of the wait condition.
682 * 0 if the @condition evaluated to %false after the @timeout elapsed,
683 * 1 if the @condition evaluated to %true after the @timeout elapsed,
684 * or the remaining jiffies (at least 1) if the @condition evaluated
685 * to %true before the @timeout elapsed.
687 #define wait_event_idle_timeout(wq_head, condition, timeout) \
689 long __ret = timeout; \
691 if (!___wait_cond_timeout(condition)) \
692 __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
696 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
697 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
698 TASK_IDLE, 1, timeout, \
699 __ret = schedule_timeout(__ret))
702 * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
703 * @wq_head: the waitqueue to wait on
704 * @condition: a C expression for the event to wait for
705 * @timeout: timeout, in jiffies
707 * The process is put to sleep (TASK_IDLE) until the
708 * @condition evaluates to true. The @condition is checked each time
709 * the waitqueue @wq_head is woken up.
711 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
712 * set thus if other processes wait on the same list, when this
713 * process is woken further processes are not considered.
715 * wake_up() has to be called after changing any variable that could
716 * change the result of the wait condition.
719 * 0 if the @condition evaluated to %false after the @timeout elapsed,
720 * 1 if the @condition evaluated to %true after the @timeout elapsed,
721 * or the remaining jiffies (at least 1) if the @condition evaluated
722 * to %true before the @timeout elapsed.
724 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
726 long __ret = timeout; \
728 if (!___wait_cond_timeout(condition)) \
729 __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
733 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
734 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
736 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
739 DEFINE_WAIT(__wait); \
741 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
743 __ret = fn(&(wq), &__wait); \
746 } while (!(condition)); \
747 __remove_wait_queue(&(wq), &__wait); \
748 __set_current_state(TASK_RUNNING); \
754 * wait_event_interruptible_locked - sleep until a condition gets true
755 * @wq: the waitqueue to wait on
756 * @condition: a C expression for the event to wait for
758 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
759 * @condition evaluates to true or a signal is received.
760 * The @condition is checked each time the waitqueue @wq is woken up.
762 * It must be called with wq.lock being held. This spinlock is
763 * unlocked while sleeping but @condition testing is done while lock
764 * is held and when this macro exits the lock is held.
766 * The lock is locked/unlocked using spin_lock()/spin_unlock()
767 * functions which must match the way they are locked/unlocked outside
770 * wake_up_locked() has to be called after changing any variable that could
771 * change the result of the wait condition.
773 * The function will return -ERESTARTSYS if it was interrupted by a
774 * signal and 0 if @condition evaluated to true.
776 #define wait_event_interruptible_locked(wq, condition) \
778 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
781 * wait_event_interruptible_locked_irq - sleep until a condition gets true
782 * @wq: the waitqueue to wait on
783 * @condition: a C expression for the event to wait for
785 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
786 * @condition evaluates to true or a signal is received.
787 * The @condition is checked each time the waitqueue @wq is woken up.
789 * It must be called with wq.lock being held. This spinlock is
790 * unlocked while sleeping but @condition testing is done while lock
791 * is held and when this macro exits the lock is held.
793 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
794 * functions which must match the way they are locked/unlocked outside
797 * wake_up_locked() has to be called after changing any variable that could
798 * change the result of the wait condition.
800 * The function will return -ERESTARTSYS if it was interrupted by a
801 * signal and 0 if @condition evaluated to true.
803 #define wait_event_interruptible_locked_irq(wq, condition) \
805 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
808 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
809 * @wq: the waitqueue to wait on
810 * @condition: a C expression for the event to wait for
812 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
813 * @condition evaluates to true or a signal is received.
814 * The @condition is checked each time the waitqueue @wq is woken up.
816 * It must be called with wq.lock being held. This spinlock is
817 * unlocked while sleeping but @condition testing is done while lock
818 * is held and when this macro exits the lock is held.
820 * The lock is locked/unlocked using spin_lock()/spin_unlock()
821 * functions which must match the way they are locked/unlocked outside
824 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
825 * set thus when other process waits process on the list if this
826 * process is awaken further processes are not considered.
828 * wake_up_locked() has to be called after changing any variable that could
829 * change the result of the wait condition.
831 * The function will return -ERESTARTSYS if it was interrupted by a
832 * signal and 0 if @condition evaluated to true.
834 #define wait_event_interruptible_exclusive_locked(wq, condition) \
836 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
839 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
840 * @wq: the waitqueue to wait on
841 * @condition: a C expression for the event to wait for
843 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
844 * @condition evaluates to true or a signal is received.
845 * The @condition is checked each time the waitqueue @wq is woken up.
847 * It must be called with wq.lock being held. This spinlock is
848 * unlocked while sleeping but @condition testing is done while lock
849 * is held and when this macro exits the lock is held.
851 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
852 * functions which must match the way they are locked/unlocked outside
855 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
856 * set thus when other process waits process on the list if this
857 * process is awaken further processes are not considered.
859 * wake_up_locked() has to be called after changing any variable that could
860 * change the result of the wait condition.
862 * The function will return -ERESTARTSYS if it was interrupted by a
863 * signal and 0 if @condition evaluated to true.
865 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
867 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
870 #define __wait_event_killable(wq, condition) \
871 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
874 * wait_event_killable - sleep until a condition gets true
875 * @wq_head: the waitqueue to wait on
876 * @condition: a C expression for the event to wait for
878 * The process is put to sleep (TASK_KILLABLE) until the
879 * @condition evaluates to true or a signal is received.
880 * The @condition is checked each time the waitqueue @wq_head is woken up.
882 * wake_up() 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_killable(wq_head, condition) \
893 __ret = __wait_event_killable(wq_head, condition); \
897 #define __wait_event_killable_timeout(wq_head, condition, timeout) \
898 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
899 TASK_KILLABLE, 0, timeout, \
900 __ret = schedule_timeout(__ret))
903 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
904 * @wq_head: the waitqueue to wait on
905 * @condition: a C expression for the event to wait for
906 * @timeout: timeout, in jiffies
908 * The process is put to sleep (TASK_KILLABLE) until the
909 * @condition evaluates to true or a kill signal is received.
910 * The @condition is checked each time the waitqueue @wq_head is woken up.
912 * wake_up() has to be called after changing any variable that could
913 * change the result of the wait condition.
916 * 0 if the @condition evaluated to %false after the @timeout elapsed,
917 * 1 if the @condition evaluated to %true after the @timeout elapsed,
918 * the remaining jiffies (at least 1) if the @condition evaluated
919 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
920 * interrupted by a kill signal.
922 * Only kill signals interrupt this process.
924 #define wait_event_killable_timeout(wq_head, condition, timeout) \
926 long __ret = timeout; \
928 if (!___wait_cond_timeout(condition)) \
929 __ret = __wait_event_killable_timeout(wq_head, \
930 condition, timeout); \
935 #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
936 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
937 spin_unlock_irq(&lock); \
940 spin_lock_irq(&lock))
943 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
944 * condition is checked under the lock. This
945 * is expected to be called with the lock
947 * @wq_head: the waitqueue to wait on
948 * @condition: a C expression for the event to wait for
949 * @lock: a locked spinlock_t, which will be released before cmd
950 * and schedule() and reacquired afterwards.
951 * @cmd: a command which is invoked outside the critical section before
954 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
955 * @condition evaluates to true. The @condition is checked each time
956 * the waitqueue @wq_head is woken up.
958 * wake_up() has to be called after changing any variable that could
959 * change the result of the wait condition.
961 * This is supposed to be called while holding the lock. The lock is
962 * dropped before invoking the cmd and going to sleep and is reacquired
965 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
969 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
973 * wait_event_lock_irq - sleep until a condition gets true. The
974 * condition is checked under the lock. This
975 * is expected to be called with the lock
977 * @wq_head: the waitqueue to wait on
978 * @condition: a C expression for the event to wait for
979 * @lock: a locked spinlock_t, which will be released before schedule()
980 * and reacquired afterwards.
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 going to sleep and is reacquired afterwards.
992 #define wait_event_lock_irq(wq_head, condition, lock) \
996 __wait_event_lock_irq(wq_head, condition, lock, ); \
1000 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
1001 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
1002 spin_unlock_irq(&lock); \
1005 spin_lock_irq(&lock))
1008 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
1009 * The condition is checked under the lock. This is expected to
1010 * be called with the lock taken.
1011 * @wq_head: the waitqueue to wait on
1012 * @condition: a C expression for the event to wait for
1013 * @lock: a locked spinlock_t, which will be released before cmd and
1014 * schedule() and reacquired afterwards.
1015 * @cmd: a command which is invoked outside the critical section before
1018 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1019 * @condition evaluates to true or a signal is received. The @condition is
1020 * checked each time the waitqueue @wq_head is woken up.
1022 * wake_up() has to be called after changing any variable that could
1023 * change the result of the wait condition.
1025 * This is supposed to be called while holding the lock. The lock is
1026 * dropped before invoking the cmd and going to sleep and is reacquired
1029 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1030 * and 0 if @condition evaluated to true.
1032 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
1036 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1037 condition, lock, cmd); \
1042 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1043 * The condition is checked under the lock. This is expected
1044 * to be called with the lock taken.
1045 * @wq_head: the waitqueue to wait on
1046 * @condition: a C expression for the event to wait for
1047 * @lock: a locked spinlock_t, which will be released before schedule()
1048 * and reacquired afterwards.
1050 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1051 * @condition evaluates to true or signal is received. The @condition is
1052 * checked each time the waitqueue @wq_head is woken up.
1054 * wake_up() has to be called after changing any variable that could
1055 * change the result of the wait condition.
1057 * This is supposed to be called while holding the lock. The lock is
1058 * dropped before going to sleep and is reacquired afterwards.
1060 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1061 * and 0 if @condition evaluated to true.
1063 #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
1067 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1068 condition, lock,); \
1072 #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
1073 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
1074 state, 0, timeout, \
1075 spin_unlock_irq(&lock); \
1076 __ret = schedule_timeout(__ret); \
1077 spin_lock_irq(&lock));
1080 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1081 * true or a timeout elapses. The condition is checked under
1082 * the lock. This is expected to be called with the lock taken.
1083 * @wq_head: the waitqueue to wait on
1084 * @condition: a C expression for the event to wait for
1085 * @lock: a locked spinlock_t, which will be released before schedule()
1086 * and reacquired afterwards.
1087 * @timeout: timeout, in jiffies
1089 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1090 * @condition evaluates to true or signal is received. The @condition is
1091 * checked each time the waitqueue @wq_head is woken up.
1093 * wake_up() has to be called after changing any variable that could
1094 * change the result of the wait condition.
1096 * This is supposed to be called while holding the lock. The lock is
1097 * dropped before going to sleep and is reacquired afterwards.
1099 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1100 * was interrupted by a signal, and the remaining jiffies otherwise
1101 * if the condition evaluated to true before the timeout elapsed.
1103 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1106 long __ret = timeout; \
1107 if (!___wait_cond_timeout(condition)) \
1108 __ret = __wait_event_lock_irq_timeout( \
1109 wq_head, condition, lock, timeout, \
1110 TASK_INTERRUPTIBLE); \
1114 #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout) \
1116 long __ret = timeout; \
1117 if (!___wait_cond_timeout(condition)) \
1118 __ret = __wait_event_lock_irq_timeout( \
1119 wq_head, condition, lock, timeout, \
1120 TASK_UNINTERRUPTIBLE); \
1125 * Waitqueues which are removed from the waitqueue_head at wakeup time
1127 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1128 void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1129 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1130 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1131 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1132 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1133 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1135 #define DEFINE_WAIT_FUNC(name, function) \
1136 struct wait_queue_entry name = { \
1137 .private = current, \
1139 .entry = LIST_HEAD_INIT((name).entry), \
1142 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1144 #define init_wait(wait) \
1146 (wait)->private = current; \
1147 (wait)->func = autoremove_wake_function; \
1148 INIT_LIST_HEAD(&(wait)->entry); \
1149 (wait)->flags = 0; \
1152 #endif /* _LINUX_WAIT_H */