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);
208 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
209 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
210 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
211 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
212 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
214 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
215 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
216 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
217 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
220 * Wakeup macros to be used to report events to the targets.
222 #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
223 #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
224 #define wake_up_poll(x, m) \
225 __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
226 #define wake_up_locked_poll(x, m) \
227 __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
228 #define wake_up_interruptible_poll(x, m) \
229 __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
230 #define wake_up_interruptible_sync_poll(x, m) \
231 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, poll_to_key(m))
233 #define ___wait_cond_timeout(condition) \
235 bool __cond = (condition); \
236 if (__cond && !__ret) \
241 #define ___wait_is_interruptible(state) \
242 (!__builtin_constant_p(state) || \
243 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
245 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
248 * The below macro ___wait_event() has an explicit shadow of the __ret
249 * variable when used from the wait_event_*() macros.
251 * This is so that both can use the ___wait_cond_timeout() construct
252 * to wrap the condition.
254 * The type inconsistency of the wait_event_*() __ret variable is also
255 * on purpose; we use long where we can return timeout values and int
259 #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
262 struct wait_queue_entry __wq_entry; \
263 long __ret = ret; /* explicit shadow */ \
265 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
267 long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
272 if (___wait_is_interruptible(state) && __int) { \
279 finish_wait(&wq_head, &__wq_entry); \
283 #define __wait_event(wq_head, condition) \
284 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
288 * wait_event - sleep until a condition gets true
289 * @wq_head: the waitqueue to wait on
290 * @condition: a C expression for the event to wait for
292 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
293 * @condition evaluates to true. The @condition is checked each time
294 * the waitqueue @wq_head is woken up.
296 * wake_up() has to be called after changing any variable that could
297 * change the result of the wait condition.
299 #define wait_event(wq_head, condition) \
304 __wait_event(wq_head, condition); \
307 #define __io_wait_event(wq_head, condition) \
308 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
312 * io_wait_event() -- like wait_event() but with io_schedule()
314 #define io_wait_event(wq_head, condition) \
319 __io_wait_event(wq_head, condition); \
322 #define __wait_event_freezable(wq_head, condition) \
323 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
324 freezable_schedule())
327 * wait_event_freezable - sleep (or freeze) until a condition gets true
328 * @wq_head: the waitqueue to wait on
329 * @condition: a C expression for the event to wait for
331 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
332 * to system load) until the @condition evaluates to true. The
333 * @condition is checked each time the waitqueue @wq_head is woken up.
335 * wake_up() has to be called after changing any variable that could
336 * change the result of the wait condition.
338 #define wait_event_freezable(wq_head, condition) \
343 __ret = __wait_event_freezable(wq_head, condition); \
347 #define __wait_event_timeout(wq_head, condition, timeout) \
348 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
349 TASK_UNINTERRUPTIBLE, 0, timeout, \
350 __ret = schedule_timeout(__ret))
353 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
354 * @wq_head: the waitqueue to wait on
355 * @condition: a C expression for the event to wait for
356 * @timeout: timeout, in jiffies
358 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
359 * @condition evaluates to true. The @condition is checked each time
360 * the waitqueue @wq_head is woken up.
362 * wake_up() has to be called after changing any variable that could
363 * change the result of the wait condition.
366 * 0 if the @condition evaluated to %false after the @timeout elapsed,
367 * 1 if the @condition evaluated to %true after the @timeout elapsed,
368 * or the remaining jiffies (at least 1) if the @condition evaluated
369 * to %true before the @timeout elapsed.
371 #define wait_event_timeout(wq_head, condition, timeout) \
373 long __ret = timeout; \
375 if (!___wait_cond_timeout(condition)) \
376 __ret = __wait_event_timeout(wq_head, condition, timeout); \
380 #define __wait_event_freezable_timeout(wq_head, condition, timeout) \
381 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
382 TASK_INTERRUPTIBLE, 0, timeout, \
383 __ret = freezable_schedule_timeout(__ret))
386 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
387 * increasing load and is freezable.
389 #define wait_event_freezable_timeout(wq_head, condition, timeout) \
391 long __ret = timeout; \
393 if (!___wait_cond_timeout(condition)) \
394 __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
398 #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
399 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
400 cmd1; schedule(); cmd2)
402 * Just like wait_event_cmd(), except it sets exclusive flag
404 #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
408 __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
411 #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
412 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
413 cmd1; schedule(); cmd2)
416 * wait_event_cmd - sleep until a condition gets true
417 * @wq_head: the waitqueue to wait on
418 * @condition: a C expression for the event to wait for
419 * @cmd1: the command will be executed before sleep
420 * @cmd2: the command will be executed after sleep
422 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
423 * @condition evaluates to true. The @condition is checked each time
424 * the waitqueue @wq_head is woken up.
426 * wake_up() has to be called after changing any variable that could
427 * change the result of the wait condition.
429 #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
433 __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
436 #define __wait_event_interruptible(wq_head, condition) \
437 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
441 * wait_event_interruptible - sleep until a condition gets true
442 * @wq_head: the waitqueue to wait on
443 * @condition: a C expression for the event to wait for
445 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
446 * @condition evaluates to true or a signal is received.
447 * The @condition is checked each time the waitqueue @wq_head is woken up.
449 * wake_up() has to be called after changing any variable that could
450 * change the result of the wait condition.
452 * The function will return -ERESTARTSYS if it was interrupted by a
453 * signal and 0 if @condition evaluated to true.
455 #define wait_event_interruptible(wq_head, condition) \
460 __ret = __wait_event_interruptible(wq_head, condition); \
464 #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
465 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
466 TASK_INTERRUPTIBLE, 0, timeout, \
467 __ret = schedule_timeout(__ret))
470 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
471 * @wq_head: the waitqueue to wait on
472 * @condition: a C expression for the event to wait for
473 * @timeout: timeout, in jiffies
475 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
476 * @condition evaluates to true or a signal is received.
477 * The @condition is checked each time the waitqueue @wq_head is woken up.
479 * wake_up() has to be called after changing any variable that could
480 * change the result of the wait condition.
483 * 0 if the @condition evaluated to %false after the @timeout elapsed,
484 * 1 if the @condition evaluated to %true after the @timeout elapsed,
485 * the remaining jiffies (at least 1) if the @condition evaluated
486 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
487 * interrupted by a signal.
489 #define wait_event_interruptible_timeout(wq_head, condition, timeout) \
491 long __ret = timeout; \
493 if (!___wait_cond_timeout(condition)) \
494 __ret = __wait_event_interruptible_timeout(wq_head, \
495 condition, timeout); \
499 #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
502 struct hrtimer_sleeper __t; \
504 hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC, \
506 if ((timeout) != KTIME_MAX) \
507 hrtimer_start_range_ns(&__t.timer, timeout, \
508 current->timer_slack_ns, \
511 __ret = ___wait_event(wq_head, condition, state, 0, 0, \
518 hrtimer_cancel(&__t.timer); \
519 destroy_hrtimer_on_stack(&__t.timer); \
524 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
525 * @wq_head: the waitqueue to wait on
526 * @condition: a C expression for the event to wait for
527 * @timeout: timeout, as a ktime_t
529 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
530 * @condition evaluates to true or a signal is received.
531 * The @condition is checked each time the waitqueue @wq_head is woken up.
533 * wake_up() has to be called after changing any variable that could
534 * change the result of the wait condition.
536 * The function returns 0 if @condition became true, or -ETIME if the timeout
539 #define wait_event_hrtimeout(wq_head, condition, timeout) \
544 __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
545 TASK_UNINTERRUPTIBLE); \
550 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
551 * @wq: the waitqueue to wait on
552 * @condition: a C expression for the event to wait for
553 * @timeout: timeout, as a ktime_t
555 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
556 * @condition evaluates to true or a signal is received.
557 * The @condition is checked each time the waitqueue @wq is woken up.
559 * wake_up() has to be called after changing any variable that could
560 * change the result of the wait condition.
562 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
563 * interrupted by a signal, or -ETIME if the timeout elapsed.
565 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
570 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
571 TASK_INTERRUPTIBLE); \
575 #define __wait_event_interruptible_exclusive(wq, condition) \
576 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
579 #define wait_event_interruptible_exclusive(wq, condition) \
584 __ret = __wait_event_interruptible_exclusive(wq, condition); \
588 #define __wait_event_killable_exclusive(wq, condition) \
589 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
592 #define wait_event_killable_exclusive(wq, condition) \
597 __ret = __wait_event_killable_exclusive(wq, condition); \
602 #define __wait_event_freezable_exclusive(wq, condition) \
603 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
604 freezable_schedule())
606 #define wait_event_freezable_exclusive(wq, condition) \
611 __ret = __wait_event_freezable_exclusive(wq, condition); \
616 * wait_event_idle - wait for a condition without contributing to system load
617 * @wq_head: the waitqueue to wait on
618 * @condition: a C expression for the event to wait for
620 * The process is put to sleep (TASK_IDLE) until the
621 * @condition evaluates to true.
622 * The @condition is checked each time the waitqueue @wq_head is woken up.
624 * wake_up() has to be called after changing any variable that could
625 * change the result of the wait condition.
628 #define wait_event_idle(wq_head, condition) \
632 ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
636 * wait_event_idle_exclusive - wait for a condition with contributing to system load
637 * @wq_head: the waitqueue to wait on
638 * @condition: a C expression for the event to wait for
640 * The process is put to sleep (TASK_IDLE) until the
641 * @condition evaluates to true.
642 * The @condition is checked each time the waitqueue @wq_head is woken up.
644 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
645 * set thus if other processes wait on the same list, when this
646 * process is woken further processes are not considered.
648 * wake_up() has to be called after changing any variable that could
649 * change the result of the wait condition.
652 #define wait_event_idle_exclusive(wq_head, condition) \
656 ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
659 #define __wait_event_idle_timeout(wq_head, condition, timeout) \
660 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
661 TASK_IDLE, 0, timeout, \
662 __ret = schedule_timeout(__ret))
665 * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
666 * @wq_head: the waitqueue to wait on
667 * @condition: a C expression for the event to wait for
668 * @timeout: timeout, in jiffies
670 * The process is put to sleep (TASK_IDLE) until the
671 * @condition evaluates to true. The @condition is checked each time
672 * the waitqueue @wq_head is woken up.
674 * wake_up() has to be called after changing any variable that could
675 * change the result of the wait condition.
678 * 0 if the @condition evaluated to %false after the @timeout elapsed,
679 * 1 if the @condition evaluated to %true after the @timeout elapsed,
680 * or the remaining jiffies (at least 1) if the @condition evaluated
681 * to %true before the @timeout elapsed.
683 #define wait_event_idle_timeout(wq_head, condition, timeout) \
685 long __ret = timeout; \
687 if (!___wait_cond_timeout(condition)) \
688 __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
692 #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
693 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
694 TASK_IDLE, 1, timeout, \
695 __ret = schedule_timeout(__ret))
698 * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
699 * @wq_head: the waitqueue to wait on
700 * @condition: a C expression for the event to wait for
701 * @timeout: timeout, in jiffies
703 * The process is put to sleep (TASK_IDLE) until the
704 * @condition evaluates to true. The @condition is checked each time
705 * the waitqueue @wq_head is woken up.
707 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
708 * set thus if other processes wait on the same list, when this
709 * process is woken further processes are not considered.
711 * wake_up() has to be called after changing any variable that could
712 * change the result of the wait condition.
715 * 0 if the @condition evaluated to %false after the @timeout elapsed,
716 * 1 if the @condition evaluated to %true after the @timeout elapsed,
717 * or the remaining jiffies (at least 1) if the @condition evaluated
718 * to %true before the @timeout elapsed.
720 #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
722 long __ret = timeout; \
724 if (!___wait_cond_timeout(condition)) \
725 __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
729 extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
730 extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
732 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
735 DEFINE_WAIT(__wait); \
737 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
739 __ret = fn(&(wq), &__wait); \
742 } while (!(condition)); \
743 __remove_wait_queue(&(wq), &__wait); \
744 __set_current_state(TASK_RUNNING); \
750 * wait_event_interruptible_locked - sleep until a condition gets true
751 * @wq: the waitqueue to wait on
752 * @condition: a C expression for the event to wait for
754 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
755 * @condition evaluates to true or a signal is received.
756 * The @condition is checked each time the waitqueue @wq is woken up.
758 * It must be called with wq.lock being held. This spinlock is
759 * unlocked while sleeping but @condition testing is done while lock
760 * is held and when this macro exits the lock is held.
762 * The lock is locked/unlocked using spin_lock()/spin_unlock()
763 * functions which must match the way they are locked/unlocked outside
766 * wake_up_locked() has to be called after changing any variable that could
767 * change the result of the wait condition.
769 * The function will return -ERESTARTSYS if it was interrupted by a
770 * signal and 0 if @condition evaluated to true.
772 #define wait_event_interruptible_locked(wq, condition) \
774 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
777 * wait_event_interruptible_locked_irq - 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_irq()/spin_unlock_irq()
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_irq(wq, condition) \
801 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
804 * wait_event_interruptible_exclusive_locked - sleep exclusively 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()/spin_unlock()
817 * functions which must match the way they are locked/unlocked outside
820 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
821 * set thus when other process waits process on the list if this
822 * process is awaken further processes are not considered.
824 * wake_up_locked() has to be called after changing any variable that could
825 * change the result of the wait condition.
827 * The function will return -ERESTARTSYS if it was interrupted by a
828 * signal and 0 if @condition evaluated to true.
830 #define wait_event_interruptible_exclusive_locked(wq, condition) \
832 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
835 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
836 * @wq: the waitqueue to wait on
837 * @condition: a C expression for the event to wait for
839 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
840 * @condition evaluates to true or a signal is received.
841 * The @condition is checked each time the waitqueue @wq is woken up.
843 * It must be called with wq.lock being held. This spinlock is
844 * unlocked while sleeping but @condition testing is done while lock
845 * is held and when this macro exits the lock is held.
847 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
848 * functions which must match the way they are locked/unlocked outside
851 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
852 * set thus when other process waits process on the list if this
853 * process is awaken further processes are not considered.
855 * wake_up_locked() has to be called after changing any variable that could
856 * change the result of the wait condition.
858 * The function will return -ERESTARTSYS if it was interrupted by a
859 * signal and 0 if @condition evaluated to true.
861 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
863 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
866 #define __wait_event_killable(wq, condition) \
867 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
870 * wait_event_killable - sleep until a condition gets true
871 * @wq_head: the waitqueue to wait on
872 * @condition: a C expression for the event to wait for
874 * The process is put to sleep (TASK_KILLABLE) until the
875 * @condition evaluates to true or a signal is received.
876 * The @condition is checked each time the waitqueue @wq_head is woken up.
878 * wake_up() has to be called after changing any variable that could
879 * change the result of the wait condition.
881 * The function will return -ERESTARTSYS if it was interrupted by a
882 * signal and 0 if @condition evaluated to true.
884 #define wait_event_killable(wq_head, condition) \
889 __ret = __wait_event_killable(wq_head, condition); \
893 #define __wait_event_killable_timeout(wq_head, condition, timeout) \
894 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
895 TASK_KILLABLE, 0, timeout, \
896 __ret = schedule_timeout(__ret))
899 * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
900 * @wq_head: the waitqueue to wait on
901 * @condition: a C expression for the event to wait for
902 * @timeout: timeout, in jiffies
904 * The process is put to sleep (TASK_KILLABLE) until the
905 * @condition evaluates to true or a kill signal is received.
906 * The @condition is checked each time the waitqueue @wq_head is woken up.
908 * wake_up() has to be called after changing any variable that could
909 * change the result of the wait condition.
912 * 0 if the @condition evaluated to %false after the @timeout elapsed,
913 * 1 if the @condition evaluated to %true after the @timeout elapsed,
914 * the remaining jiffies (at least 1) if the @condition evaluated
915 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
916 * interrupted by a kill signal.
918 * Only kill signals interrupt this process.
920 #define wait_event_killable_timeout(wq_head, condition, timeout) \
922 long __ret = timeout; \
924 if (!___wait_cond_timeout(condition)) \
925 __ret = __wait_event_killable_timeout(wq_head, \
926 condition, timeout); \
931 #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
932 (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
933 spin_unlock_irq(&lock); \
936 spin_lock_irq(&lock))
939 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
940 * condition is checked under the lock. This
941 * is expected to be called with the lock
943 * @wq_head: the waitqueue to wait on
944 * @condition: a C expression for the event to wait for
945 * @lock: a locked spinlock_t, which will be released before cmd
946 * and schedule() and reacquired afterwards.
947 * @cmd: a command which is invoked outside the critical section before
950 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
951 * @condition evaluates to true. The @condition is checked each time
952 * the waitqueue @wq_head is woken up.
954 * wake_up() has to be called after changing any variable that could
955 * change the result of the wait condition.
957 * This is supposed to be called while holding the lock. The lock is
958 * dropped before invoking the cmd and going to sleep and is reacquired
961 #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
965 __wait_event_lock_irq(wq_head, condition, lock, cmd); \
969 * wait_event_lock_irq - sleep until a condition gets true. The
970 * condition is checked under the lock. This
971 * is expected to be called with the lock
973 * @wq_head: the waitqueue to wait on
974 * @condition: a C expression for the event to wait for
975 * @lock: a locked spinlock_t, which will be released before schedule()
976 * and reacquired afterwards.
978 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
979 * @condition evaluates to true. The @condition is checked each time
980 * the waitqueue @wq_head is woken up.
982 * wake_up() has to be called after changing any variable that could
983 * change the result of the wait condition.
985 * This is supposed to be called while holding the lock. The lock is
986 * dropped before going to sleep and is reacquired afterwards.
988 #define wait_event_lock_irq(wq_head, condition, lock) \
992 __wait_event_lock_irq(wq_head, condition, lock, ); \
996 #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
997 ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
998 spin_unlock_irq(&lock); \
1001 spin_lock_irq(&lock))
1004 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
1005 * The condition is checked under the lock. This is expected to
1006 * be called with the lock taken.
1007 * @wq_head: the waitqueue to wait on
1008 * @condition: a C expression for the event to wait for
1009 * @lock: a locked spinlock_t, which will be released before cmd and
1010 * schedule() and reacquired afterwards.
1011 * @cmd: a command which is invoked outside the critical section before
1014 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1015 * @condition evaluates to true or a signal is received. The @condition is
1016 * checked each time the waitqueue @wq_head is woken up.
1018 * wake_up() has to be called after changing any variable that could
1019 * change the result of the wait condition.
1021 * This is supposed to be called while holding the lock. The lock is
1022 * dropped before invoking the cmd and going to sleep and is reacquired
1025 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1026 * and 0 if @condition evaluated to true.
1028 #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
1032 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1033 condition, lock, cmd); \
1038 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
1039 * The condition is checked under the lock. This is expected
1040 * to be called with the lock taken.
1041 * @wq_head: the waitqueue to wait on
1042 * @condition: a C expression for the event to wait for
1043 * @lock: a locked spinlock_t, which will be released before schedule()
1044 * and reacquired afterwards.
1046 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1047 * @condition evaluates to true or signal is received. The @condition is
1048 * checked each time the waitqueue @wq_head is woken up.
1050 * wake_up() has to be called after changing any variable that could
1051 * change the result of the wait condition.
1053 * This is supposed to be called while holding the lock. The lock is
1054 * dropped before going to sleep and is reacquired afterwards.
1056 * The macro will return -ERESTARTSYS if it was interrupted by a signal
1057 * and 0 if @condition evaluated to true.
1059 #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
1063 __ret = __wait_event_interruptible_lock_irq(wq_head, \
1064 condition, lock,); \
1068 #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
1069 ___wait_event(wq_head, ___wait_cond_timeout(condition), \
1070 state, 0, timeout, \
1071 spin_unlock_irq(&lock); \
1072 __ret = schedule_timeout(__ret); \
1073 spin_lock_irq(&lock));
1076 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
1077 * true or a timeout elapses. The condition is checked under
1078 * the lock. This is expected to be called with the lock taken.
1079 * @wq_head: the waitqueue to wait on
1080 * @condition: a C expression for the event to wait for
1081 * @lock: a locked spinlock_t, which will be released before schedule()
1082 * and reacquired afterwards.
1083 * @timeout: timeout, in jiffies
1085 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
1086 * @condition evaluates to true or signal is received. The @condition is
1087 * checked each time the waitqueue @wq_head is woken up.
1089 * wake_up() has to be called after changing any variable that could
1090 * change the result of the wait condition.
1092 * This is supposed to be called while holding the lock. The lock is
1093 * dropped before going to sleep and is reacquired afterwards.
1095 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
1096 * was interrupted by a signal, and the remaining jiffies otherwise
1097 * if the condition evaluated to true before the timeout elapsed.
1099 #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
1102 long __ret = timeout; \
1103 if (!___wait_cond_timeout(condition)) \
1104 __ret = __wait_event_lock_irq_timeout( \
1105 wq_head, condition, lock, timeout, \
1106 TASK_INTERRUPTIBLE); \
1110 #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout) \
1112 long __ret = timeout; \
1113 if (!___wait_cond_timeout(condition)) \
1114 __ret = __wait_event_lock_irq_timeout( \
1115 wq_head, condition, lock, timeout, \
1116 TASK_UNINTERRUPTIBLE); \
1121 * Waitqueues which are removed from the waitqueue_head at wakeup time
1123 void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1124 void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1125 long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
1126 void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
1127 long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
1128 int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1129 int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
1131 #define DEFINE_WAIT_FUNC(name, function) \
1132 struct wait_queue_entry name = { \
1133 .private = current, \
1135 .entry = LIST_HEAD_INIT((name).entry), \
1138 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
1140 #define init_wait(wait) \
1142 (wait)->private = current; \
1143 (wait)->func = autoremove_wake_function; \
1144 INIT_LIST_HEAD(&(wait)->entry); \
1145 (wait)->flags = 0; \
1148 #endif /* _LINUX_WAIT_H */