Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_WAIT_H |
| 3 | #define _LINUX_WAIT_H |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 4 | /* |
| 5 | * Linux wait queue related types and methods |
| 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/list.h> |
| 8 | #include <linux/stddef.h> |
| 9 | #include <linux/spinlock.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <asm/current.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 12 | #include <uapi/linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 14 | typedef struct wait_queue_entry wait_queue_entry_t; |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 15 | |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 19 | /* wait_queue_entry::flags */ |
Peter Zijlstra | 61ada52 | 2014-09-24 10:18:47 +0200 | [diff] [blame] | 20 | #define WQ_FLAG_EXCLUSIVE 0x01 |
| 21 | #define WQ_FLAG_WOKEN 0x02 |
Tim Chen | 2554db9 | 2017-08-25 09:13:54 -0700 | [diff] [blame] | 22 | #define WQ_FLAG_BOOKMARK 0x04 |
Peter Zijlstra | 61ada52 | 2014-09-24 10:18:47 +0200 | [diff] [blame] | 23 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 24 | /* |
| 25 | * A single wait-queue entry structure: |
| 26 | */ |
| 27 | struct wait_queue_entry { |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 28 | unsigned int flags; |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 29 | void *private; |
| 30 | wait_queue_func_t func; |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 31 | struct list_head entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 34 | struct wait_queue_head { |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 35 | spinlock_t lock; |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 36 | struct list_head head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 38 | typedef struct wait_queue_head wait_queue_head_t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 40 | struct task_struct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Macros for declaration and initialisaton of the datatypes |
| 44 | */ |
| 45 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 46 | #define __WAITQUEUE_INITIALIZER(name, tsk) { \ |
| 47 | .private = tsk, \ |
| 48 | .func = default_wake_function, \ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 49 | .entry = { NULL, NULL } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 51 | #define DECLARE_WAITQUEUE(name, tsk) \ |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 52 | struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 54 | #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \ |
| 55 | .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 56 | .head = { &(name).head, &(name).head } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | #define DECLARE_WAIT_QUEUE_HEAD(name) \ |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 59 | struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 61 | extern void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *); |
Peter Zijlstra | 2fc3911 | 2009-08-10 12:33:05 +0100 | [diff] [blame] | 62 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 63 | #define init_waitqueue_head(wq_head) \ |
| 64 | do { \ |
| 65 | static struct lock_class_key __key; \ |
| 66 | \ |
| 67 | __init_waitqueue_head((wq_head), #wq_head, &__key); \ |
Peter Zijlstra | 2fc3911 | 2009-08-10 12:33:05 +0100 | [diff] [blame] | 68 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Peter Zijlstra | 7259f0d | 2006-10-29 22:46:36 -0800 | [diff] [blame] | 70 | #ifdef CONFIG_LOCKDEP |
| 71 | # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \ |
| 72 | ({ init_waitqueue_head(&name); name; }) |
| 73 | # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \ |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 74 | struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) |
Peter Zijlstra | 7259f0d | 2006-10-29 22:46:36 -0800 | [diff] [blame] | 75 | #else |
| 76 | # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name) |
| 77 | #endif |
| 78 | |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 79 | static inline void init_waitqueue_entry(struct wait_queue_entry *wq_entry, struct task_struct *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 81 | wq_entry->flags = 0; |
| 82 | wq_entry->private = p; |
| 83 | wq_entry->func = default_wake_function; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 86 | static inline void |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 87 | init_waitqueue_func_entry(struct wait_queue_entry *wq_entry, wait_queue_func_t func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 89 | wq_entry->flags = 0; |
| 90 | wq_entry->private = NULL; |
| 91 | wq_entry->func = func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Peter Zijlstra | 69e51e92 | 2015-10-23 14:32:34 +0200 | [diff] [blame] | 94 | /** |
| 95 | * waitqueue_active -- locklessly test for waiters on the queue |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 96 | * @wq_head: the waitqueue to test for waiters |
Peter Zijlstra | 69e51e92 | 2015-10-23 14:32:34 +0200 | [diff] [blame] | 97 | * |
| 98 | * returns true if the wait list is not empty |
| 99 | * |
| 100 | * NOTE: this function is lockless and requires care, incorrect usage _will_ |
| 101 | * lead to sporadic and non-obvious failure. |
| 102 | * |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 103 | * Use either while holding wait_queue_head::lock or when used for wakeups |
Peter Zijlstra | 69e51e92 | 2015-10-23 14:32:34 +0200 | [diff] [blame] | 104 | * with an extra smp_mb() like: |
| 105 | * |
| 106 | * CPU0 - waker CPU1 - waiter |
| 107 | * |
| 108 | * for (;;) { |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 109 | * @cond = true; prepare_to_wait(&wq_head, &wait, state); |
Peter Zijlstra | 69e51e92 | 2015-10-23 14:32:34 +0200 | [diff] [blame] | 110 | * smp_mb(); // smp_mb() from set_current_state() |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 111 | * if (waitqueue_active(wq_head)) if (@cond) |
| 112 | * wake_up(wq_head); break; |
Peter Zijlstra | 69e51e92 | 2015-10-23 14:32:34 +0200 | [diff] [blame] | 113 | * schedule(); |
| 114 | * } |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 115 | * finish_wait(&wq_head, &wait); |
Peter Zijlstra | 69e51e92 | 2015-10-23 14:32:34 +0200 | [diff] [blame] | 116 | * |
| 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. |
| 120 | * |
| 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. |
| 123 | */ |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 124 | static inline int waitqueue_active(struct wait_queue_head *wq_head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 126 | return !list_empty(&wq_head->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 129 | /** |
| 130 | * wq_has_sleeper - check if there are any waiting processes |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 131 | * @wq_head: wait queue head |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 132 | * |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 133 | * Returns true if wq_head has waiting processes |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 134 | * |
| 135 | * Please refer to the comment for waitqueue_active. |
| 136 | */ |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 137 | static inline bool wq_has_sleeper(struct wait_queue_head *wq_head) |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 138 | { |
| 139 | /* |
| 140 | * We need to be sure we are in sync with the |
| 141 | * add_wait_queue modifications to the wait queue. |
| 142 | * |
| 143 | * This memory barrier should be paired with one on the |
| 144 | * waiting side. |
| 145 | */ |
| 146 | smp_mb(); |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 147 | return waitqueue_active(wq_head); |
Herbert Xu | 1ce0bf5 | 2015-11-26 13:55:39 +0800 | [diff] [blame] | 148 | } |
| 149 | |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 154 | static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 156 | list_add(&wq_entry->entry, &wq_head->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Used for wake-one threads: |
| 161 | */ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 162 | static inline void |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 163 | __add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry) |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 164 | { |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 165 | wq_entry->flags |= WQ_FLAG_EXCLUSIVE; |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 166 | __add_wait_queue(wq_head, wq_entry); |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 169 | static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | { |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 171 | list_add_tail(&wq_entry->entry, &wq_head->head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 174 | static inline void |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 175 | __add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry) |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 176 | { |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 177 | wq_entry->flags |= WQ_FLAG_EXCLUSIVE; |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 178 | __add_wait_queue_entry_tail(wq_head, wq_entry); |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 179 | } |
| 180 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 181 | static inline void |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 182 | __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 184 | list_del(&wq_entry->entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 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); |
Tim Chen | 11a19c7 | 2017-08-25 09:13:55 -0700 | [diff] [blame] | 189 | void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head, |
| 190 | unsigned int mode, void *key, wait_queue_entry_t *bookmark); |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
Matthew Wilcox | e64d66c | 2007-12-06 17:34:36 -0500 | [diff] [blame] | 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) |
Thomas Gleixner | 63b2001 | 2011-12-01 00:04:00 +0100 | [diff] [blame] | 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) |
Matthew Wilcox | e64d66c | 2007-12-06 17:34:36 -0500 | [diff] [blame] | 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Matthew Wilcox | e64d66c | 2007-12-06 17:34:36 -0500 | [diff] [blame] | 204 | #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 206 | /* |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 207 | * Wakeup macros to be used to report events to the targets. |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 208 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 209 | #define wake_up_poll(x, m) \ |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 210 | __wake_up(x, TASK_NORMAL, 1, (void *) (m)) |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 211 | #define wake_up_locked_poll(x, m) \ |
Andrea Arcangeli | ac5be6b | 2015-09-22 14:58:49 -0700 | [diff] [blame] | 212 | __wake_up_locked_key((x), TASK_NORMAL, (void *) (m)) |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 213 | #define wake_up_interruptible_poll(x, m) \ |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 214 | __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m)) |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 215 | #define wake_up_interruptible_sync_poll(x, m) \ |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 216 | __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m)) |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 217 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 218 | #define ___wait_cond_timeout(condition) \ |
| 219 | ({ \ |
| 220 | bool __cond = (condition); \ |
| 221 | if (__cond && !__ret) \ |
| 222 | __ret = 1; \ |
| 223 | __cond || !__ret; \ |
Peter Zijlstra | 2953ef2 | 2013-10-02 11:22:19 +0200 | [diff] [blame] | 224 | }) |
| 225 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 226 | #define ___wait_is_interruptible(state) \ |
| 227 | (!__builtin_constant_p(state) || \ |
| 228 | state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \ |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 229 | |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 230 | extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags); |
Oleg Nesterov | 0176bea | 2016-09-06 16:00:55 +0200 | [diff] [blame] | 231 | |
Peter Zijlstra | 8b32201 | 2014-04-18 15:07:17 -0700 | [diff] [blame] | 232 | /* |
| 233 | * The below macro ___wait_event() has an explicit shadow of the __ret |
| 234 | * variable when used from the wait_event_*() macros. |
| 235 | * |
| 236 | * This is so that both can use the ___wait_cond_timeout() construct |
| 237 | * to wrap the condition. |
| 238 | * |
| 239 | * The type inconsistency of the wait_event_*() __ret variable is also |
| 240 | * on purpose; we use long where we can return timeout values and int |
| 241 | * otherwise. |
| 242 | */ |
| 243 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 244 | #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \ |
| 245 | ({ \ |
| 246 | __label__ __out; \ |
| 247 | struct wait_queue_entry __wq_entry; \ |
| 248 | long __ret = ret; /* explicit shadow */ \ |
| 249 | \ |
| 250 | init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \ |
| 251 | for (;;) { \ |
| 252 | long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\ |
| 253 | \ |
| 254 | if (condition) \ |
| 255 | break; \ |
| 256 | \ |
| 257 | if (___wait_is_interruptible(state) && __int) { \ |
| 258 | __ret = __int; \ |
| 259 | goto __out; \ |
| 260 | } \ |
| 261 | \ |
| 262 | cmd; \ |
| 263 | } \ |
| 264 | finish_wait(&wq_head, &__wq_entry); \ |
| 265 | __out: __ret; \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 266 | }) |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 267 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 268 | #define __wait_event(wq_head, condition) \ |
| 269 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 270 | schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | /** |
| 273 | * wait_event - sleep until a condition gets true |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 274 | * @wq_head: the waitqueue to wait on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | * @condition: a C expression for the event to wait for |
| 276 | * |
| 277 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 278 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 279 | * the waitqueue @wq_head is woken up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | * |
| 281 | * wake_up() has to be called after changing any variable that could |
| 282 | * change the result of the wait condition. |
| 283 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 284 | #define wait_event(wq_head, condition) \ |
| 285 | do { \ |
| 286 | might_sleep(); \ |
| 287 | if (condition) \ |
| 288 | break; \ |
| 289 | __wait_event(wq_head, condition); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } while (0) |
| 291 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 292 | #define __io_wait_event(wq_head, condition) \ |
| 293 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | 2c56124 | 2015-02-03 12:55:31 +0100 | [diff] [blame] | 294 | io_schedule()) |
| 295 | |
| 296 | /* |
| 297 | * io_wait_event() -- like wait_event() but with io_schedule() |
| 298 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 299 | #define io_wait_event(wq_head, condition) \ |
| 300 | do { \ |
| 301 | might_sleep(); \ |
| 302 | if (condition) \ |
| 303 | break; \ |
| 304 | __io_wait_event(wq_head, condition); \ |
Peter Zijlstra | 2c56124 | 2015-02-03 12:55:31 +0100 | [diff] [blame] | 305 | } while (0) |
| 306 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 307 | #define __wait_event_freezable(wq_head, condition) \ |
| 308 | ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 309 | schedule(); try_to_freeze()) |
| 310 | |
| 311 | /** |
Stafford Horne | f4bcfa1 | 2016-02-23 22:39:28 +0900 | [diff] [blame] | 312 | * wait_event_freezable - sleep (or freeze) until a condition gets true |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 313 | * @wq_head: the waitqueue to wait on |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 314 | * @condition: a C expression for the event to wait for |
| 315 | * |
| 316 | * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute |
| 317 | * to system load) until the @condition evaluates to true. The |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 318 | * @condition is checked each time the waitqueue @wq_head is woken up. |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 319 | * |
| 320 | * wake_up() has to be called after changing any variable that could |
| 321 | * change the result of the wait condition. |
| 322 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 323 | #define wait_event_freezable(wq_head, condition) \ |
| 324 | ({ \ |
| 325 | int __ret = 0; \ |
| 326 | might_sleep(); \ |
| 327 | if (!(condition)) \ |
| 328 | __ret = __wait_event_freezable(wq_head, condition); \ |
| 329 | __ret; \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 330 | }) |
| 331 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 332 | #define __wait_event_timeout(wq_head, condition, timeout) \ |
| 333 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 334 | TASK_UNINTERRUPTIBLE, 0, timeout, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 335 | __ret = schedule_timeout(__ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | /** |
| 338 | * wait_event_timeout - sleep until a condition gets true or a timeout elapses |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 339 | * @wq_head: the waitqueue to wait on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | * @condition: a C expression for the event to wait for |
| 341 | * @timeout: timeout, in jiffies |
| 342 | * |
| 343 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 344 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 345 | * the waitqueue @wq_head is woken up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | * |
| 347 | * wake_up() has to be called after changing any variable that could |
| 348 | * change the result of the wait condition. |
| 349 | * |
Scot Doyle | 6b44f51 | 2014-08-24 17:12:27 +0000 | [diff] [blame] | 350 | * Returns: |
| 351 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 352 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 353 | * or the remaining jiffies (at least 1) if the @condition evaluated |
| 354 | * to %true before the @timeout elapsed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 356 | #define wait_event_timeout(wq_head, condition, timeout) \ |
| 357 | ({ \ |
| 358 | long __ret = timeout; \ |
| 359 | might_sleep(); \ |
| 360 | if (!___wait_cond_timeout(condition)) \ |
| 361 | __ret = __wait_event_timeout(wq_head, condition, timeout); \ |
| 362 | __ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | }) |
| 364 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 365 | #define __wait_event_freezable_timeout(wq_head, condition, timeout) \ |
| 366 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 367 | TASK_INTERRUPTIBLE, 0, timeout, \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 368 | __ret = schedule_timeout(__ret); try_to_freeze()) |
| 369 | |
| 370 | /* |
| 371 | * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid |
| 372 | * increasing load and is freezable. |
| 373 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 374 | #define wait_event_freezable_timeout(wq_head, condition, timeout) \ |
| 375 | ({ \ |
| 376 | long __ret = timeout; \ |
| 377 | might_sleep(); \ |
| 378 | if (!___wait_cond_timeout(condition)) \ |
| 379 | __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \ |
| 380 | __ret; \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 381 | }) |
| 382 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 383 | #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \ |
| 384 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \ |
Yuanhan Liu | 9f3520c | 2015-05-08 18:19:05 +1000 | [diff] [blame] | 385 | cmd1; schedule(); cmd2) |
| 386 | /* |
| 387 | * Just like wait_event_cmd(), except it sets exclusive flag |
| 388 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 389 | #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \ |
| 390 | do { \ |
| 391 | if (condition) \ |
| 392 | break; \ |
| 393 | __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \ |
Yuanhan Liu | 9f3520c | 2015-05-08 18:19:05 +1000 | [diff] [blame] | 394 | } while (0) |
| 395 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 396 | #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \ |
| 397 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 398 | cmd1; schedule(); cmd2) |
| 399 | |
| 400 | /** |
| 401 | * wait_event_cmd - sleep until a condition gets true |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 402 | * @wq_head: the waitqueue to wait on |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 403 | * @condition: a C expression for the event to wait for |
Masanari Iida | f434f7a | 2014-01-22 01:22:06 +0900 | [diff] [blame] | 404 | * @cmd1: the command will be executed before sleep |
| 405 | * @cmd2: the command will be executed after sleep |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 406 | * |
| 407 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 408 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 409 | * the waitqueue @wq_head is woken up. |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 410 | * |
| 411 | * wake_up() has to be called after changing any variable that could |
| 412 | * change the result of the wait condition. |
| 413 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 414 | #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \ |
| 415 | do { \ |
| 416 | if (condition) \ |
| 417 | break; \ |
| 418 | __wait_event_cmd(wq_head, condition, cmd1, cmd2); \ |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 419 | } while (0) |
| 420 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 421 | #define __wait_event_interruptible(wq_head, condition) \ |
| 422 | ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | f13f4c4 | 2013-10-02 11:22:24 +0200 | [diff] [blame] | 423 | schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | /** |
| 426 | * wait_event_interruptible - sleep until a condition gets true |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 427 | * @wq_head: the waitqueue to wait on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | * @condition: a C expression for the event to wait for |
| 429 | * |
| 430 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 431 | * @condition evaluates to true or a signal is received. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 432 | * The @condition is checked each time the waitqueue @wq_head is woken up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | * |
| 434 | * wake_up() has to be called after changing any variable that could |
| 435 | * change the result of the wait condition. |
| 436 | * |
| 437 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 438 | * signal and 0 if @condition evaluated to true. |
| 439 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 440 | #define wait_event_interruptible(wq_head, condition) \ |
| 441 | ({ \ |
| 442 | int __ret = 0; \ |
| 443 | might_sleep(); \ |
| 444 | if (!(condition)) \ |
| 445 | __ret = __wait_event_interruptible(wq_head, condition); \ |
| 446 | __ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | }) |
| 448 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 449 | #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \ |
| 450 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 451 | TASK_INTERRUPTIBLE, 0, timeout, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 452 | __ret = schedule_timeout(__ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | /** |
| 455 | * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 456 | * @wq_head: the waitqueue to wait on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | * @condition: a C expression for the event to wait for |
| 458 | * @timeout: timeout, in jiffies |
| 459 | * |
| 460 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 461 | * @condition evaluates to true or a signal is received. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 462 | * The @condition is checked each time the waitqueue @wq_head is woken up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * |
| 464 | * wake_up() has to be called after changing any variable that could |
| 465 | * change the result of the wait condition. |
| 466 | * |
Imre Deak | 4c663cf | 2013-05-24 15:55:09 -0700 | [diff] [blame] | 467 | * Returns: |
Scot Doyle | 6b44f51 | 2014-08-24 17:12:27 +0000 | [diff] [blame] | 468 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 469 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 470 | * the remaining jiffies (at least 1) if the @condition evaluated |
| 471 | * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was |
| 472 | * interrupted by a signal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 474 | #define wait_event_interruptible_timeout(wq_head, condition, timeout) \ |
| 475 | ({ \ |
| 476 | long __ret = timeout; \ |
| 477 | might_sleep(); \ |
| 478 | if (!___wait_cond_timeout(condition)) \ |
| 479 | __ret = __wait_event_interruptible_timeout(wq_head, \ |
| 480 | condition, timeout); \ |
| 481 | __ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | }) |
| 483 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 484 | #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \ |
| 485 | ({ \ |
| 486 | int __ret = 0; \ |
| 487 | struct hrtimer_sleeper __t; \ |
| 488 | \ |
| 489 | hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); \ |
| 490 | hrtimer_init_sleeper(&__t, current); \ |
| 491 | if ((timeout) != KTIME_MAX) \ |
| 492 | hrtimer_start_range_ns(&__t.timer, timeout, \ |
| 493 | current->timer_slack_ns, \ |
| 494 | HRTIMER_MODE_REL); \ |
| 495 | \ |
| 496 | __ret = ___wait_event(wq_head, condition, state, 0, 0, \ |
| 497 | if (!__t.task) { \ |
| 498 | __ret = -ETIME; \ |
| 499 | break; \ |
| 500 | } \ |
| 501 | schedule()); \ |
| 502 | \ |
| 503 | hrtimer_cancel(&__t.timer); \ |
| 504 | destroy_hrtimer_on_stack(&__t.timer); \ |
| 505 | __ret; \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 506 | }) |
| 507 | |
| 508 | /** |
| 509 | * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 510 | * @wq_head: the waitqueue to wait on |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 511 | * @condition: a C expression for the event to wait for |
| 512 | * @timeout: timeout, as a ktime_t |
| 513 | * |
| 514 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 515 | * @condition evaluates to true or a signal is received. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 516 | * The @condition is checked each time the waitqueue @wq_head is woken up. |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 517 | * |
| 518 | * wake_up() has to be called after changing any variable that could |
| 519 | * change the result of the wait condition. |
| 520 | * |
| 521 | * The function returns 0 if @condition became true, or -ETIME if the timeout |
| 522 | * elapsed. |
| 523 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 524 | #define wait_event_hrtimeout(wq_head, condition, timeout) \ |
| 525 | ({ \ |
| 526 | int __ret = 0; \ |
| 527 | might_sleep(); \ |
| 528 | if (!(condition)) \ |
| 529 | __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \ |
| 530 | TASK_UNINTERRUPTIBLE); \ |
| 531 | __ret; \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 532 | }) |
| 533 | |
| 534 | /** |
| 535 | * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses |
Jonathan Corbet | 6c423f5 | 2017-07-24 13:58:00 -0600 | [diff] [blame] | 536 | * @wq: the waitqueue to wait on |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 537 | * @condition: a C expression for the event to wait for |
| 538 | * @timeout: timeout, as a ktime_t |
| 539 | * |
| 540 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 541 | * @condition evaluates to true or a signal is received. |
Jonathan Corbet | 6c423f5 | 2017-07-24 13:58:00 -0600 | [diff] [blame] | 542 | * The @condition is checked each time the waitqueue @wq is woken up. |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 543 | * |
| 544 | * wake_up() has to be called after changing any variable that could |
| 545 | * change the result of the wait condition. |
| 546 | * |
| 547 | * The function returns 0 if @condition became true, -ERESTARTSYS if it was |
| 548 | * interrupted by a signal, or -ETIME if the timeout elapsed. |
| 549 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 550 | #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \ |
| 551 | ({ \ |
| 552 | long __ret = 0; \ |
| 553 | might_sleep(); \ |
| 554 | if (!(condition)) \ |
| 555 | __ret = __wait_event_hrtimeout(wq, condition, timeout, \ |
| 556 | TASK_INTERRUPTIBLE); \ |
| 557 | __ret; \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 558 | }) |
| 559 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 560 | #define __wait_event_interruptible_exclusive(wq, condition) \ |
| 561 | ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \ |
Peter Zijlstra | 48c2521 | 2013-10-02 11:22:26 +0200 | [diff] [blame] | 562 | schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 564 | #define wait_event_interruptible_exclusive(wq, condition) \ |
| 565 | ({ \ |
| 566 | int __ret = 0; \ |
| 567 | might_sleep(); \ |
| 568 | if (!(condition)) \ |
| 569 | __ret = __wait_event_interruptible_exclusive(wq, condition); \ |
| 570 | __ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | }) |
| 572 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 573 | #define __wait_event_killable_exclusive(wq, condition) \ |
| 574 | ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \ |
Al Viro | 6a0fb30 | 2016-07-19 03:04:34 -0400 | [diff] [blame] | 575 | schedule()) |
| 576 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 577 | #define wait_event_killable_exclusive(wq, condition) \ |
| 578 | ({ \ |
| 579 | int __ret = 0; \ |
| 580 | might_sleep(); \ |
| 581 | if (!(condition)) \ |
| 582 | __ret = __wait_event_killable_exclusive(wq, condition); \ |
| 583 | __ret; \ |
Al Viro | 6a0fb30 | 2016-07-19 03:04:34 -0400 | [diff] [blame] | 584 | }) |
| 585 | |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 586 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 587 | #define __wait_event_freezable_exclusive(wq, condition) \ |
| 588 | ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 589 | schedule(); try_to_freeze()) |
| 590 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 591 | #define wait_event_freezable_exclusive(wq, condition) \ |
| 592 | ({ \ |
| 593 | int __ret = 0; \ |
| 594 | might_sleep(); \ |
| 595 | if (!(condition)) \ |
| 596 | __ret = __wait_event_freezable_exclusive(wq, condition); \ |
| 597 | __ret; \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 598 | }) |
| 599 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 600 | extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *); |
| 601 | extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *); |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 602 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 603 | #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \ |
| 604 | ({ \ |
| 605 | int __ret; \ |
| 606 | DEFINE_WAIT(__wait); \ |
| 607 | if (exclusive) \ |
| 608 | __wait.flags |= WQ_FLAG_EXCLUSIVE; \ |
| 609 | do { \ |
| 610 | __ret = fn(&(wq), &__wait); \ |
| 611 | if (__ret) \ |
| 612 | break; \ |
| 613 | } while (!(condition)); \ |
| 614 | __remove_wait_queue(&(wq), &__wait); \ |
| 615 | __set_current_state(TASK_RUNNING); \ |
| 616 | __ret; \ |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 617 | }) |
| 618 | |
| 619 | |
| 620 | /** |
| 621 | * wait_event_interruptible_locked - sleep until a condition gets true |
| 622 | * @wq: the waitqueue to wait on |
| 623 | * @condition: a C expression for the event to wait for |
| 624 | * |
| 625 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 626 | * @condition evaluates to true or a signal is received. |
| 627 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 628 | * |
| 629 | * It must be called with wq.lock being held. This spinlock is |
| 630 | * unlocked while sleeping but @condition testing is done while lock |
| 631 | * is held and when this macro exits the lock is held. |
| 632 | * |
| 633 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 634 | * functions which must match the way they are locked/unlocked outside |
| 635 | * of this macro. |
| 636 | * |
| 637 | * wake_up_locked() has to be called after changing any variable that could |
| 638 | * change the result of the wait condition. |
| 639 | * |
| 640 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 641 | * signal and 0 if @condition evaluated to true. |
| 642 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 643 | #define wait_event_interruptible_locked(wq, condition) \ |
| 644 | ((condition) \ |
Linus Torvalds | bd0f9b3 | 2017-03-07 15:33:14 -0800 | [diff] [blame] | 645 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr)) |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 646 | |
| 647 | /** |
| 648 | * wait_event_interruptible_locked_irq - sleep until a condition gets true |
| 649 | * @wq: the waitqueue to wait on |
| 650 | * @condition: a C expression for the event to wait for |
| 651 | * |
| 652 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 653 | * @condition evaluates to true or a signal is received. |
| 654 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 655 | * |
| 656 | * It must be called with wq.lock being held. This spinlock is |
| 657 | * unlocked while sleeping but @condition testing is done while lock |
| 658 | * is held and when this macro exits the lock is held. |
| 659 | * |
| 660 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 661 | * functions which must match the way they are locked/unlocked outside |
| 662 | * of this macro. |
| 663 | * |
| 664 | * wake_up_locked() has to be called after changing any variable that could |
| 665 | * change the result of the wait condition. |
| 666 | * |
| 667 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 668 | * signal and 0 if @condition evaluated to true. |
| 669 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 670 | #define wait_event_interruptible_locked_irq(wq, condition) \ |
| 671 | ((condition) \ |
Linus Torvalds | bd0f9b3 | 2017-03-07 15:33:14 -0800 | [diff] [blame] | 672 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq)) |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 673 | |
| 674 | /** |
| 675 | * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true |
| 676 | * @wq: the waitqueue to wait on |
| 677 | * @condition: a C expression for the event to wait for |
| 678 | * |
| 679 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 680 | * @condition evaluates to true or a signal is received. |
| 681 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 682 | * |
| 683 | * It must be called with wq.lock being held. This spinlock is |
| 684 | * unlocked while sleeping but @condition testing is done while lock |
| 685 | * is held and when this macro exits the lock is held. |
| 686 | * |
| 687 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 688 | * functions which must match the way they are locked/unlocked outside |
| 689 | * of this macro. |
| 690 | * |
| 691 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 692 | * set thus when other process waits process on the list if this |
| 693 | * process is awaken further processes are not considered. |
| 694 | * |
| 695 | * wake_up_locked() has to be called after changing any variable that could |
| 696 | * change the result of the wait condition. |
| 697 | * |
| 698 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 699 | * signal and 0 if @condition evaluated to true. |
| 700 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 701 | #define wait_event_interruptible_exclusive_locked(wq, condition) \ |
| 702 | ((condition) \ |
Linus Torvalds | bd0f9b3 | 2017-03-07 15:33:14 -0800 | [diff] [blame] | 703 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr)) |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 704 | |
| 705 | /** |
| 706 | * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true |
| 707 | * @wq: the waitqueue to wait on |
| 708 | * @condition: a C expression for the event to wait for |
| 709 | * |
| 710 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 711 | * @condition evaluates to true or a signal is received. |
| 712 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 713 | * |
| 714 | * It must be called with wq.lock being held. This spinlock is |
| 715 | * unlocked while sleeping but @condition testing is done while lock |
| 716 | * is held and when this macro exits the lock is held. |
| 717 | * |
| 718 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 719 | * functions which must match the way they are locked/unlocked outside |
| 720 | * of this macro. |
| 721 | * |
| 722 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 723 | * set thus when other process waits process on the list if this |
| 724 | * process is awaken further processes are not considered. |
| 725 | * |
| 726 | * wake_up_locked() has to be called after changing any variable that could |
| 727 | * change the result of the wait condition. |
| 728 | * |
| 729 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 730 | * signal and 0 if @condition evaluated to true. |
| 731 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 732 | #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \ |
| 733 | ((condition) \ |
Linus Torvalds | bd0f9b3 | 2017-03-07 15:33:14 -0800 | [diff] [blame] | 734 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq)) |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 735 | |
| 736 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 737 | #define __wait_event_killable(wq, condition) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 738 | ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule()) |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 739 | |
| 740 | /** |
| 741 | * wait_event_killable - sleep until a condition gets true |
Jonathan Corbet | 6c423f5 | 2017-07-24 13:58:00 -0600 | [diff] [blame] | 742 | * @wq_head: the waitqueue to wait on |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 743 | * @condition: a C expression for the event to wait for |
| 744 | * |
| 745 | * The process is put to sleep (TASK_KILLABLE) until the |
| 746 | * @condition evaluates to true or a signal is received. |
Jonathan Corbet | 6c423f5 | 2017-07-24 13:58:00 -0600 | [diff] [blame] | 747 | * The @condition is checked each time the waitqueue @wq_head is woken up. |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 748 | * |
| 749 | * wake_up() has to be called after changing any variable that could |
| 750 | * change the result of the wait condition. |
| 751 | * |
| 752 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 753 | * signal and 0 if @condition evaluated to true. |
| 754 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 755 | #define wait_event_killable(wq_head, condition) \ |
| 756 | ({ \ |
| 757 | int __ret = 0; \ |
| 758 | might_sleep(); \ |
| 759 | if (!(condition)) \ |
| 760 | __ret = __wait_event_killable(wq_head, condition); \ |
| 761 | __ret; \ |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 762 | }) |
| 763 | |
Luis R. Rodriguez | 8ada927 | 2017-08-18 15:15:55 -0700 | [diff] [blame] | 764 | #define __wait_event_killable_timeout(wq_head, condition, timeout) \ |
| 765 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 766 | TASK_KILLABLE, 0, timeout, \ |
| 767 | __ret = schedule_timeout(__ret)) |
| 768 | |
| 769 | /** |
| 770 | * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses |
| 771 | * @wq_head: the waitqueue to wait on |
| 772 | * @condition: a C expression for the event to wait for |
| 773 | * @timeout: timeout, in jiffies |
| 774 | * |
| 775 | * The process is put to sleep (TASK_KILLABLE) until the |
| 776 | * @condition evaluates to true or a kill signal is received. |
| 777 | * The @condition is checked each time the waitqueue @wq_head is woken up. |
| 778 | * |
| 779 | * wake_up() has to be called after changing any variable that could |
| 780 | * change the result of the wait condition. |
| 781 | * |
| 782 | * Returns: |
| 783 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 784 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 785 | * the remaining jiffies (at least 1) if the @condition evaluated |
| 786 | * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was |
| 787 | * interrupted by a kill signal. |
| 788 | * |
| 789 | * Only kill signals interrupt this process. |
| 790 | */ |
| 791 | #define wait_event_killable_timeout(wq_head, condition, timeout) \ |
| 792 | ({ \ |
| 793 | long __ret = timeout; \ |
| 794 | might_sleep(); \ |
| 795 | if (!___wait_cond_timeout(condition)) \ |
| 796 | __ret = __wait_event_killable_timeout(wq_head, \ |
| 797 | condition, timeout); \ |
| 798 | __ret; \ |
| 799 | }) |
| 800 | |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 801 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 802 | #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \ |
| 803 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
| 804 | spin_unlock_irq(&lock); \ |
| 805 | cmd; \ |
| 806 | schedule(); \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 807 | spin_lock_irq(&lock)) |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 808 | |
| 809 | /** |
| 810 | * wait_event_lock_irq_cmd - sleep until a condition gets true. The |
| 811 | * condition is checked under the lock. This |
| 812 | * is expected to be called with the lock |
| 813 | * taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 814 | * @wq_head: the waitqueue to wait on |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 815 | * @condition: a C expression for the event to wait for |
| 816 | * @lock: a locked spinlock_t, which will be released before cmd |
| 817 | * and schedule() and reacquired afterwards. |
| 818 | * @cmd: a command which is invoked outside the critical section before |
| 819 | * sleep |
| 820 | * |
| 821 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 822 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 823 | * the waitqueue @wq_head is woken up. |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 824 | * |
| 825 | * wake_up() has to be called after changing any variable that could |
| 826 | * change the result of the wait condition. |
| 827 | * |
| 828 | * This is supposed to be called while holding the lock. The lock is |
| 829 | * dropped before invoking the cmd and going to sleep and is reacquired |
| 830 | * afterwards. |
| 831 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 832 | #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \ |
| 833 | do { \ |
| 834 | if (condition) \ |
| 835 | break; \ |
| 836 | __wait_event_lock_irq(wq_head, condition, lock, cmd); \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 837 | } while (0) |
| 838 | |
| 839 | /** |
| 840 | * wait_event_lock_irq - sleep until a condition gets true. The |
| 841 | * condition is checked under the lock. This |
| 842 | * is expected to be called with the lock |
| 843 | * taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 844 | * @wq_head: the waitqueue to wait on |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 845 | * @condition: a C expression for the event to wait for |
| 846 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 847 | * and reacquired afterwards. |
| 848 | * |
| 849 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 850 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 851 | * the waitqueue @wq_head is woken up. |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 852 | * |
| 853 | * wake_up() has to be called after changing any variable that could |
| 854 | * change the result of the wait condition. |
| 855 | * |
| 856 | * This is supposed to be called while holding the lock. The lock is |
| 857 | * dropped before going to sleep and is reacquired afterwards. |
| 858 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 859 | #define wait_event_lock_irq(wq_head, condition, lock) \ |
| 860 | do { \ |
| 861 | if (condition) \ |
| 862 | break; \ |
| 863 | __wait_event_lock_irq(wq_head, condition, lock, ); \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 864 | } while (0) |
| 865 | |
| 866 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 867 | #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \ |
| 868 | ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \ |
| 869 | spin_unlock_irq(&lock); \ |
| 870 | cmd; \ |
| 871 | schedule(); \ |
Peter Zijlstra | 8fbd88f | 2013-10-02 11:22:28 +0200 | [diff] [blame] | 872 | spin_lock_irq(&lock)) |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 873 | |
| 874 | /** |
| 875 | * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true. |
| 876 | * The condition is checked under the lock. This is expected to |
| 877 | * be called with the lock taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 878 | * @wq_head: the waitqueue to wait on |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 879 | * @condition: a C expression for the event to wait for |
| 880 | * @lock: a locked spinlock_t, which will be released before cmd and |
| 881 | * schedule() and reacquired afterwards. |
| 882 | * @cmd: a command which is invoked outside the critical section before |
| 883 | * sleep |
| 884 | * |
| 885 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 886 | * @condition evaluates to true or a signal is received. The @condition is |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 887 | * checked each time the waitqueue @wq_head is woken up. |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 888 | * |
| 889 | * wake_up() has to be called after changing any variable that could |
| 890 | * change the result of the wait condition. |
| 891 | * |
| 892 | * This is supposed to be called while holding the lock. The lock is |
| 893 | * dropped before invoking the cmd and going to sleep and is reacquired |
| 894 | * afterwards. |
| 895 | * |
| 896 | * The macro will return -ERESTARTSYS if it was interrupted by a signal |
| 897 | * and 0 if @condition evaluated to true. |
| 898 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 899 | #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \ |
| 900 | ({ \ |
| 901 | int __ret = 0; \ |
| 902 | if (!(condition)) \ |
| 903 | __ret = __wait_event_interruptible_lock_irq(wq_head, \ |
| 904 | condition, lock, cmd); \ |
| 905 | __ret; \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 906 | }) |
| 907 | |
| 908 | /** |
| 909 | * wait_event_interruptible_lock_irq - sleep until a condition gets true. |
| 910 | * The condition is checked under the lock. This is expected |
| 911 | * to be called with the lock taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 912 | * @wq_head: the waitqueue to wait on |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 913 | * @condition: a C expression for the event to wait for |
| 914 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 915 | * and reacquired afterwards. |
| 916 | * |
| 917 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 918 | * @condition evaluates to true or signal is received. The @condition is |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 919 | * checked each time the waitqueue @wq_head is woken up. |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 920 | * |
| 921 | * wake_up() has to be called after changing any variable that could |
| 922 | * change the result of the wait condition. |
| 923 | * |
| 924 | * This is supposed to be called while holding the lock. The lock is |
| 925 | * dropped before going to sleep and is reacquired afterwards. |
| 926 | * |
| 927 | * The macro will return -ERESTARTSYS if it was interrupted by a signal |
| 928 | * and 0 if @condition evaluated to true. |
| 929 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 930 | #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \ |
| 931 | ({ \ |
| 932 | int __ret = 0; \ |
| 933 | if (!(condition)) \ |
| 934 | __ret = __wait_event_interruptible_lock_irq(wq_head, \ |
| 935 | condition, lock,); \ |
| 936 | __ret; \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 937 | }) |
| 938 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 939 | #define __wait_event_interruptible_lock_irq_timeout(wq_head, condition, \ |
| 940 | lock, timeout) \ |
| 941 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 942 | TASK_INTERRUPTIBLE, 0, timeout, \ |
| 943 | spin_unlock_irq(&lock); \ |
| 944 | __ret = schedule_timeout(__ret); \ |
Peter Zijlstra | a1dc685 | 2013-10-02 11:22:29 +0200 | [diff] [blame] | 945 | spin_lock_irq(&lock)); |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 946 | |
| 947 | /** |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 948 | * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets |
| 949 | * true or a timeout elapses. The condition is checked under |
| 950 | * the lock. This is expected to be called with the lock taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 951 | * @wq_head: the waitqueue to wait on |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 952 | * @condition: a C expression for the event to wait for |
| 953 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 954 | * and reacquired afterwards. |
| 955 | * @timeout: timeout, in jiffies |
| 956 | * |
| 957 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 958 | * @condition evaluates to true or signal is received. The @condition is |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 959 | * checked each time the waitqueue @wq_head is woken up. |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 960 | * |
| 961 | * wake_up() has to be called after changing any variable that could |
| 962 | * change the result of the wait condition. |
| 963 | * |
| 964 | * This is supposed to be called while holding the lock. The lock is |
| 965 | * dropped before going to sleep and is reacquired afterwards. |
| 966 | * |
| 967 | * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it |
| 968 | * was interrupted by a signal, and the remaining jiffies otherwise |
| 969 | * if the condition evaluated to true before the timeout elapsed. |
| 970 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 971 | #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \ |
| 972 | timeout) \ |
| 973 | ({ \ |
| 974 | long __ret = timeout; \ |
| 975 | if (!___wait_cond_timeout(condition)) \ |
| 976 | __ret = __wait_event_interruptible_lock_irq_timeout( \ |
| 977 | wq_head, condition, lock, timeout); \ |
| 978 | __ret; \ |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 979 | }) |
| 980 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | /* |
| 982 | * Waitqueues which are removed from the waitqueue_head at wakeup time |
| 983 | */ |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 984 | void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state); |
| 985 | void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state); |
| 986 | long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state); |
| 987 | void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry); |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 988 | long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout); |
| 989 | int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key); |
| 990 | int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 992 | #define DEFINE_WAIT_FUNC(name, function) \ |
| 993 | struct wait_queue_entry name = { \ |
| 994 | .private = current, \ |
| 995 | .func = function, \ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 996 | .entry = LIST_HEAD_INIT((name).entry), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 999 | #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function) |
| 1000 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1001 | #define init_wait(wait) \ |
| 1002 | do { \ |
| 1003 | (wait)->private = current; \ |
| 1004 | (wait)->func = autoremove_wake_function; \ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1005 | INIT_LIST_HEAD(&(wait)->entry); \ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1006 | (wait)->flags = 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } while (0) |
| 1008 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 1009 | #endif /* _LINUX_WAIT_H */ |