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 | */ |
Al Viro | 3ad6f93 | 2017-07-03 20:14:56 -0400 | [diff] [blame] | 209 | #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m)) |
| 210 | #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m)) |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 211 | #define wake_up_poll(x, m) \ |
Al Viro | 3ad6f93 | 2017-07-03 20:14:56 -0400 | [diff] [blame] | 212 | __wake_up(x, TASK_NORMAL, 1, poll_to_key(m)) |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 213 | #define wake_up_locked_poll(x, m) \ |
Al Viro | 3ad6f93 | 2017-07-03 20:14:56 -0400 | [diff] [blame] | 214 | __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m)) |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 215 | #define wake_up_interruptible_poll(x, m) \ |
Al Viro | 3ad6f93 | 2017-07-03 20:14:56 -0400 | [diff] [blame] | 216 | __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m)) |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 217 | #define wake_up_interruptible_sync_poll(x, m) \ |
Al Viro | 3ad6f93 | 2017-07-03 20:14:56 -0400 | [diff] [blame] | 218 | __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, poll_to_key(m)) |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 219 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 220 | #define ___wait_cond_timeout(condition) \ |
| 221 | ({ \ |
| 222 | bool __cond = (condition); \ |
| 223 | if (__cond && !__ret) \ |
| 224 | __ret = 1; \ |
| 225 | __cond || !__ret; \ |
Peter Zijlstra | 2953ef2 | 2013-10-02 11:22:19 +0200 | [diff] [blame] | 226 | }) |
| 227 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 228 | #define ___wait_is_interruptible(state) \ |
| 229 | (!__builtin_constant_p(state) || \ |
| 230 | state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \ |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 231 | |
Ingo Molnar | 50816c4 | 2017-03-05 10:33:16 +0100 | [diff] [blame] | 232 | 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] | 233 | |
Peter Zijlstra | 8b32201 | 2014-04-18 15:07:17 -0700 | [diff] [blame] | 234 | /* |
| 235 | * The below macro ___wait_event() has an explicit shadow of the __ret |
| 236 | * variable when used from the wait_event_*() macros. |
| 237 | * |
| 238 | * This is so that both can use the ___wait_cond_timeout() construct |
| 239 | * to wrap the condition. |
| 240 | * |
| 241 | * The type inconsistency of the wait_event_*() __ret variable is also |
| 242 | * on purpose; we use long where we can return timeout values and int |
| 243 | * otherwise. |
| 244 | */ |
| 245 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 246 | #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \ |
| 247 | ({ \ |
| 248 | __label__ __out; \ |
| 249 | struct wait_queue_entry __wq_entry; \ |
| 250 | long __ret = ret; /* explicit shadow */ \ |
| 251 | \ |
| 252 | init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \ |
| 253 | for (;;) { \ |
| 254 | long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\ |
| 255 | \ |
| 256 | if (condition) \ |
| 257 | break; \ |
| 258 | \ |
| 259 | if (___wait_is_interruptible(state) && __int) { \ |
| 260 | __ret = __int; \ |
| 261 | goto __out; \ |
| 262 | } \ |
| 263 | \ |
| 264 | cmd; \ |
| 265 | } \ |
| 266 | finish_wait(&wq_head, &__wq_entry); \ |
| 267 | __out: __ret; \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 268 | }) |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 269 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 270 | #define __wait_event(wq_head, condition) \ |
| 271 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 272 | schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * wait_event - sleep until a condition gets true |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 276 | * @wq_head: the waitqueue to wait on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | * @condition: a C expression for the event to wait for |
| 278 | * |
| 279 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 280 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 281 | * the waitqueue @wq_head is woken up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | * |
| 283 | * wake_up() has to be called after changing any variable that could |
| 284 | * change the result of the wait condition. |
| 285 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 286 | #define wait_event(wq_head, condition) \ |
| 287 | do { \ |
| 288 | might_sleep(); \ |
| 289 | if (condition) \ |
| 290 | break; \ |
| 291 | __wait_event(wq_head, condition); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } while (0) |
| 293 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 294 | #define __io_wait_event(wq_head, condition) \ |
| 295 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | 2c56124 | 2015-02-03 12:55:31 +0100 | [diff] [blame] | 296 | io_schedule()) |
| 297 | |
| 298 | /* |
| 299 | * io_wait_event() -- like wait_event() but with io_schedule() |
| 300 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 301 | #define io_wait_event(wq_head, condition) \ |
| 302 | do { \ |
| 303 | might_sleep(); \ |
| 304 | if (condition) \ |
| 305 | break; \ |
| 306 | __io_wait_event(wq_head, condition); \ |
Peter Zijlstra | 2c56124 | 2015-02-03 12:55:31 +0100 | [diff] [blame] | 307 | } while (0) |
| 308 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 309 | #define __wait_event_freezable(wq_head, condition) \ |
| 310 | ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 311 | schedule(); try_to_freeze()) |
| 312 | |
| 313 | /** |
Stafford Horne | f4bcfa1 | 2016-02-23 22:39:28 +0900 | [diff] [blame] | 314 | * wait_event_freezable - sleep (or freeze) until a condition gets true |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 315 | * @wq_head: the waitqueue to wait on |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 316 | * @condition: a C expression for the event to wait for |
| 317 | * |
| 318 | * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute |
| 319 | * to system load) until the @condition evaluates to true. The |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 320 | * @condition is checked each time the waitqueue @wq_head is woken up. |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 321 | * |
| 322 | * wake_up() has to be called after changing any variable that could |
| 323 | * change the result of the wait condition. |
| 324 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 325 | #define wait_event_freezable(wq_head, condition) \ |
| 326 | ({ \ |
| 327 | int __ret = 0; \ |
| 328 | might_sleep(); \ |
| 329 | if (!(condition)) \ |
| 330 | __ret = __wait_event_freezable(wq_head, condition); \ |
| 331 | __ret; \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 332 | }) |
| 333 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 334 | #define __wait_event_timeout(wq_head, condition, timeout) \ |
| 335 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 336 | TASK_UNINTERRUPTIBLE, 0, timeout, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 337 | __ret = schedule_timeout(__ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | /** |
| 340 | * 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] | 341 | * @wq_head: the waitqueue to wait on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | * @condition: a C expression for the event to wait for |
| 343 | * @timeout: timeout, in jiffies |
| 344 | * |
| 345 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 346 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 347 | * the waitqueue @wq_head is woken up. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | * |
| 349 | * wake_up() has to be called after changing any variable that could |
| 350 | * change the result of the wait condition. |
| 351 | * |
Scot Doyle | 6b44f51 | 2014-08-24 17:12:27 +0000 | [diff] [blame] | 352 | * Returns: |
| 353 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 354 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 355 | * or the remaining jiffies (at least 1) if the @condition evaluated |
| 356 | * to %true before the @timeout elapsed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 358 | #define wait_event_timeout(wq_head, condition, timeout) \ |
| 359 | ({ \ |
| 360 | long __ret = timeout; \ |
| 361 | might_sleep(); \ |
| 362 | if (!___wait_cond_timeout(condition)) \ |
| 363 | __ret = __wait_event_timeout(wq_head, condition, timeout); \ |
| 364 | __ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | }) |
| 366 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 367 | #define __wait_event_freezable_timeout(wq_head, condition, timeout) \ |
| 368 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 369 | TASK_INTERRUPTIBLE, 0, timeout, \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 370 | __ret = schedule_timeout(__ret); try_to_freeze()) |
| 371 | |
| 372 | /* |
| 373 | * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid |
| 374 | * increasing load and is freezable. |
| 375 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 376 | #define wait_event_freezable_timeout(wq_head, condition, timeout) \ |
| 377 | ({ \ |
| 378 | long __ret = timeout; \ |
| 379 | might_sleep(); \ |
| 380 | if (!___wait_cond_timeout(condition)) \ |
| 381 | __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \ |
| 382 | __ret; \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 383 | }) |
| 384 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 385 | #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \ |
| 386 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \ |
Yuanhan Liu | 9f3520c | 2015-05-08 18:19:05 +1000 | [diff] [blame] | 387 | cmd1; schedule(); cmd2) |
| 388 | /* |
| 389 | * Just like wait_event_cmd(), except it sets exclusive flag |
| 390 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 391 | #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \ |
| 392 | do { \ |
| 393 | if (condition) \ |
| 394 | break; \ |
| 395 | __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \ |
Yuanhan Liu | 9f3520c | 2015-05-08 18:19:05 +1000 | [diff] [blame] | 396 | } while (0) |
| 397 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 398 | #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \ |
| 399 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 400 | cmd1; schedule(); cmd2) |
| 401 | |
| 402 | /** |
| 403 | * wait_event_cmd - sleep until a condition gets true |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 404 | * @wq_head: the waitqueue to wait on |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 405 | * @condition: a C expression for the event to wait for |
Masanari Iida | f434f7a | 2014-01-22 01:22:06 +0900 | [diff] [blame] | 406 | * @cmd1: the command will be executed before sleep |
| 407 | * @cmd2: the command will be executed after sleep |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 408 | * |
| 409 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 410 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 411 | * the waitqueue @wq_head is woken up. |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 412 | * |
| 413 | * wake_up() has to be called after changing any variable that could |
| 414 | * change the result of the wait condition. |
| 415 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 416 | #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \ |
| 417 | do { \ |
| 418 | if (condition) \ |
| 419 | break; \ |
| 420 | __wait_event_cmd(wq_head, condition, cmd1, cmd2); \ |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 421 | } while (0) |
| 422 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 423 | #define __wait_event_interruptible(wq_head, condition) \ |
| 424 | ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | f13f4c4 | 2013-10-02 11:22:24 +0200 | [diff] [blame] | 425 | schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
| 427 | /** |
| 428 | * wait_event_interruptible - sleep until a condition gets true |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 429 | * @wq_head: the waitqueue to wait on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | * @condition: a C expression for the event to wait for |
| 431 | * |
| 432 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 433 | * @condition evaluates to true or a signal is received. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 434 | * 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] | 435 | * |
| 436 | * wake_up() has to be called after changing any variable that could |
| 437 | * change the result of the wait condition. |
| 438 | * |
| 439 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 440 | * signal and 0 if @condition evaluated to true. |
| 441 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 442 | #define wait_event_interruptible(wq_head, condition) \ |
| 443 | ({ \ |
| 444 | int __ret = 0; \ |
| 445 | might_sleep(); \ |
| 446 | if (!(condition)) \ |
| 447 | __ret = __wait_event_interruptible(wq_head, condition); \ |
| 448 | __ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | }) |
| 450 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 451 | #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \ |
| 452 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 453 | TASK_INTERRUPTIBLE, 0, timeout, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 454 | __ret = schedule_timeout(__ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | /** |
| 457 | * 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] | 458 | * @wq_head: the waitqueue to wait on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | * @condition: a C expression for the event to wait for |
| 460 | * @timeout: timeout, in jiffies |
| 461 | * |
| 462 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 463 | * @condition evaluates to true or a signal is received. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 464 | * 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] | 465 | * |
| 466 | * wake_up() has to be called after changing any variable that could |
| 467 | * change the result of the wait condition. |
| 468 | * |
Imre Deak | 4c663cf | 2013-05-24 15:55:09 -0700 | [diff] [blame] | 469 | * Returns: |
Scot Doyle | 6b44f51 | 2014-08-24 17:12:27 +0000 | [diff] [blame] | 470 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 471 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 472 | * the remaining jiffies (at least 1) if the @condition evaluated |
| 473 | * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was |
| 474 | * interrupted by a signal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 476 | #define wait_event_interruptible_timeout(wq_head, condition, timeout) \ |
| 477 | ({ \ |
| 478 | long __ret = timeout; \ |
| 479 | might_sleep(); \ |
| 480 | if (!___wait_cond_timeout(condition)) \ |
| 481 | __ret = __wait_event_interruptible_timeout(wq_head, \ |
| 482 | condition, timeout); \ |
| 483 | __ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | }) |
| 485 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 486 | #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \ |
| 487 | ({ \ |
| 488 | int __ret = 0; \ |
| 489 | struct hrtimer_sleeper __t; \ |
| 490 | \ |
| 491 | hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); \ |
| 492 | hrtimer_init_sleeper(&__t, current); \ |
| 493 | if ((timeout) != KTIME_MAX) \ |
| 494 | hrtimer_start_range_ns(&__t.timer, timeout, \ |
| 495 | current->timer_slack_ns, \ |
| 496 | HRTIMER_MODE_REL); \ |
| 497 | \ |
| 498 | __ret = ___wait_event(wq_head, condition, state, 0, 0, \ |
| 499 | if (!__t.task) { \ |
| 500 | __ret = -ETIME; \ |
| 501 | break; \ |
| 502 | } \ |
| 503 | schedule()); \ |
| 504 | \ |
| 505 | hrtimer_cancel(&__t.timer); \ |
| 506 | destroy_hrtimer_on_stack(&__t.timer); \ |
| 507 | __ret; \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 508 | }) |
| 509 | |
| 510 | /** |
| 511 | * 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] | 512 | * @wq_head: the waitqueue to wait on |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 513 | * @condition: a C expression for the event to wait for |
| 514 | * @timeout: timeout, as a ktime_t |
| 515 | * |
| 516 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 517 | * @condition evaluates to true or a signal is received. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 518 | * 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] | 519 | * |
| 520 | * wake_up() has to be called after changing any variable that could |
| 521 | * change the result of the wait condition. |
| 522 | * |
| 523 | * The function returns 0 if @condition became true, or -ETIME if the timeout |
| 524 | * elapsed. |
| 525 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 526 | #define wait_event_hrtimeout(wq_head, condition, timeout) \ |
| 527 | ({ \ |
| 528 | int __ret = 0; \ |
| 529 | might_sleep(); \ |
| 530 | if (!(condition)) \ |
| 531 | __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \ |
| 532 | TASK_UNINTERRUPTIBLE); \ |
| 533 | __ret; \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 534 | }) |
| 535 | |
| 536 | /** |
| 537 | * 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] | 538 | * @wq: the waitqueue to wait on |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 539 | * @condition: a C expression for the event to wait for |
| 540 | * @timeout: timeout, as a ktime_t |
| 541 | * |
| 542 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 543 | * @condition evaluates to true or a signal is received. |
Jonathan Corbet | 6c423f5 | 2017-07-24 13:58:00 -0600 | [diff] [blame] | 544 | * The @condition is checked each time the waitqueue @wq is woken up. |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 545 | * |
| 546 | * wake_up() has to be called after changing any variable that could |
| 547 | * change the result of the wait condition. |
| 548 | * |
| 549 | * The function returns 0 if @condition became true, -ERESTARTSYS if it was |
| 550 | * interrupted by a signal, or -ETIME if the timeout elapsed. |
| 551 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 552 | #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \ |
| 553 | ({ \ |
| 554 | long __ret = 0; \ |
| 555 | might_sleep(); \ |
| 556 | if (!(condition)) \ |
| 557 | __ret = __wait_event_hrtimeout(wq, condition, timeout, \ |
| 558 | TASK_INTERRUPTIBLE); \ |
| 559 | __ret; \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 560 | }) |
| 561 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 562 | #define __wait_event_interruptible_exclusive(wq, condition) \ |
| 563 | ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \ |
Peter Zijlstra | 48c2521 | 2013-10-02 11:22:26 +0200 | [diff] [blame] | 564 | schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 566 | #define wait_event_interruptible_exclusive(wq, condition) \ |
| 567 | ({ \ |
| 568 | int __ret = 0; \ |
| 569 | might_sleep(); \ |
| 570 | if (!(condition)) \ |
| 571 | __ret = __wait_event_interruptible_exclusive(wq, condition); \ |
| 572 | __ret; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | }) |
| 574 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 575 | #define __wait_event_killable_exclusive(wq, condition) \ |
| 576 | ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \ |
Al Viro | 6a0fb30 | 2016-07-19 03:04:34 -0400 | [diff] [blame] | 577 | schedule()) |
| 578 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 579 | #define wait_event_killable_exclusive(wq, condition) \ |
| 580 | ({ \ |
| 581 | int __ret = 0; \ |
| 582 | might_sleep(); \ |
| 583 | if (!(condition)) \ |
| 584 | __ret = __wait_event_killable_exclusive(wq, condition); \ |
| 585 | __ret; \ |
Al Viro | 6a0fb30 | 2016-07-19 03:04:34 -0400 | [diff] [blame] | 586 | }) |
| 587 | |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 588 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 589 | #define __wait_event_freezable_exclusive(wq, condition) \ |
| 590 | ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 591 | schedule(); try_to_freeze()) |
| 592 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 593 | #define wait_event_freezable_exclusive(wq, condition) \ |
| 594 | ({ \ |
| 595 | int __ret = 0; \ |
| 596 | might_sleep(); \ |
| 597 | if (!(condition)) \ |
| 598 | __ret = __wait_event_freezable_exclusive(wq, condition); \ |
| 599 | __ret; \ |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 600 | }) |
| 601 | |
NeilBrown | 0957a2c | 2018-02-13 08:22:36 +1100 | [diff] [blame] | 602 | /** |
| 603 | * wait_event_idle - wait for a condition without contributing to system load |
| 604 | * @wq_head: the waitqueue to wait on |
| 605 | * @condition: a C expression for the event to wait for |
| 606 | * |
| 607 | * The process is put to sleep (TASK_IDLE) until the |
| 608 | * @condition evaluates to true. |
| 609 | * The @condition is checked each time the waitqueue @wq_head is woken up. |
| 610 | * |
| 611 | * wake_up() has to be called after changing any variable that could |
| 612 | * change the result of the wait condition. |
| 613 | * |
| 614 | */ |
| 615 | #define wait_event_idle(wq_head, condition) \ |
| 616 | do { \ |
| 617 | might_sleep(); \ |
| 618 | if (!(condition)) \ |
| 619 | ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \ |
| 620 | } while (0) |
| 621 | |
| 622 | /** |
| 623 | * wait_event_idle_exclusive - wait for a condition with contributing to system load |
| 624 | * @wq_head: the waitqueue to wait on |
| 625 | * @condition: a C expression for the event to wait for |
| 626 | * |
| 627 | * The process is put to sleep (TASK_IDLE) until the |
| 628 | * @condition evaluates to true. |
| 629 | * The @condition is checked each time the waitqueue @wq_head is woken up. |
| 630 | * |
| 631 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 632 | * set thus if other processes wait on the same list, when this |
| 633 | * process is woken further processes are not considered. |
| 634 | * |
| 635 | * wake_up() has to be called after changing any variable that could |
| 636 | * change the result of the wait condition. |
| 637 | * |
| 638 | */ |
| 639 | #define wait_event_idle_exclusive(wq_head, condition) \ |
| 640 | do { \ |
| 641 | might_sleep(); \ |
| 642 | if (!(condition)) \ |
| 643 | ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \ |
| 644 | } while (0) |
| 645 | |
| 646 | #define __wait_event_idle_timeout(wq_head, condition, timeout) \ |
| 647 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 648 | TASK_IDLE, 0, timeout, \ |
| 649 | __ret = schedule_timeout(__ret)) |
| 650 | |
| 651 | /** |
| 652 | * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses |
| 653 | * @wq_head: the waitqueue to wait on |
| 654 | * @condition: a C expression for the event to wait for |
| 655 | * @timeout: timeout, in jiffies |
| 656 | * |
| 657 | * The process is put to sleep (TASK_IDLE) until the |
| 658 | * @condition evaluates to true. The @condition is checked each time |
| 659 | * the waitqueue @wq_head is woken up. |
| 660 | * |
| 661 | * wake_up() has to be called after changing any variable that could |
| 662 | * change the result of the wait condition. |
| 663 | * |
| 664 | * Returns: |
| 665 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 666 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 667 | * or the remaining jiffies (at least 1) if the @condition evaluated |
| 668 | * to %true before the @timeout elapsed. |
| 669 | */ |
| 670 | #define wait_event_idle_timeout(wq_head, condition, timeout) \ |
| 671 | ({ \ |
| 672 | long __ret = timeout; \ |
| 673 | might_sleep(); \ |
| 674 | if (!___wait_cond_timeout(condition)) \ |
| 675 | __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \ |
| 676 | __ret; \ |
| 677 | }) |
| 678 | |
| 679 | #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \ |
| 680 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 681 | TASK_IDLE, 1, timeout, \ |
| 682 | __ret = schedule_timeout(__ret)) |
| 683 | |
| 684 | /** |
| 685 | * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses |
| 686 | * @wq_head: the waitqueue to wait on |
| 687 | * @condition: a C expression for the event to wait for |
| 688 | * @timeout: timeout, in jiffies |
| 689 | * |
| 690 | * The process is put to sleep (TASK_IDLE) until the |
| 691 | * @condition evaluates to true. The @condition is checked each time |
| 692 | * the waitqueue @wq_head is woken up. |
| 693 | * |
| 694 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 695 | * set thus if other processes wait on the same list, when this |
| 696 | * process is woken further processes are not considered. |
| 697 | * |
| 698 | * wake_up() has to be called after changing any variable that could |
| 699 | * change the result of the wait condition. |
| 700 | * |
| 701 | * Returns: |
| 702 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 703 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 704 | * or the remaining jiffies (at least 1) if the @condition evaluated |
| 705 | * to %true before the @timeout elapsed. |
| 706 | */ |
| 707 | #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \ |
| 708 | ({ \ |
| 709 | long __ret = timeout; \ |
| 710 | might_sleep(); \ |
| 711 | if (!___wait_cond_timeout(condition)) \ |
| 712 | __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\ |
| 713 | __ret; \ |
| 714 | }) |
| 715 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 716 | extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *); |
| 717 | extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *); |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 718 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 719 | #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \ |
| 720 | ({ \ |
| 721 | int __ret; \ |
| 722 | DEFINE_WAIT(__wait); \ |
| 723 | if (exclusive) \ |
| 724 | __wait.flags |= WQ_FLAG_EXCLUSIVE; \ |
| 725 | do { \ |
| 726 | __ret = fn(&(wq), &__wait); \ |
| 727 | if (__ret) \ |
| 728 | break; \ |
| 729 | } while (!(condition)); \ |
| 730 | __remove_wait_queue(&(wq), &__wait); \ |
| 731 | __set_current_state(TASK_RUNNING); \ |
| 732 | __ret; \ |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 733 | }) |
| 734 | |
| 735 | |
| 736 | /** |
| 737 | * wait_event_interruptible_locked - sleep until a condition gets true |
| 738 | * @wq: the waitqueue to wait on |
| 739 | * @condition: a C expression for the event to wait for |
| 740 | * |
| 741 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 742 | * @condition evaluates to true or a signal is received. |
| 743 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 744 | * |
| 745 | * It must be called with wq.lock being held. This spinlock is |
| 746 | * unlocked while sleeping but @condition testing is done while lock |
| 747 | * is held and when this macro exits the lock is held. |
| 748 | * |
| 749 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 750 | * functions which must match the way they are locked/unlocked outside |
| 751 | * of this macro. |
| 752 | * |
| 753 | * wake_up_locked() has to be called after changing any variable that could |
| 754 | * change the result of the wait condition. |
| 755 | * |
| 756 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 757 | * signal and 0 if @condition evaluated to true. |
| 758 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 759 | #define wait_event_interruptible_locked(wq, condition) \ |
| 760 | ((condition) \ |
Linus Torvalds | bd0f9b3 | 2017-03-07 15:33:14 -0800 | [diff] [blame] | 761 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr)) |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 762 | |
| 763 | /** |
| 764 | * wait_event_interruptible_locked_irq - sleep until a condition gets true |
| 765 | * @wq: the waitqueue to wait on |
| 766 | * @condition: a C expression for the event to wait for |
| 767 | * |
| 768 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 769 | * @condition evaluates to true or a signal is received. |
| 770 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 771 | * |
| 772 | * It must be called with wq.lock being held. This spinlock is |
| 773 | * unlocked while sleeping but @condition testing is done while lock |
| 774 | * is held and when this macro exits the lock is held. |
| 775 | * |
| 776 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 777 | * functions which must match the way they are locked/unlocked outside |
| 778 | * of this macro. |
| 779 | * |
| 780 | * wake_up_locked() has to be called after changing any variable that could |
| 781 | * change the result of the wait condition. |
| 782 | * |
| 783 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 784 | * signal and 0 if @condition evaluated to true. |
| 785 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 786 | #define wait_event_interruptible_locked_irq(wq, condition) \ |
| 787 | ((condition) \ |
Linus Torvalds | bd0f9b3 | 2017-03-07 15:33:14 -0800 | [diff] [blame] | 788 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq)) |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 789 | |
| 790 | /** |
| 791 | * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true |
| 792 | * @wq: the waitqueue to wait on |
| 793 | * @condition: a C expression for the event to wait for |
| 794 | * |
| 795 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 796 | * @condition evaluates to true or a signal is received. |
| 797 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 798 | * |
| 799 | * It must be called with wq.lock being held. This spinlock is |
| 800 | * unlocked while sleeping but @condition testing is done while lock |
| 801 | * is held and when this macro exits the lock is held. |
| 802 | * |
| 803 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 804 | * functions which must match the way they are locked/unlocked outside |
| 805 | * of this macro. |
| 806 | * |
| 807 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 808 | * set thus when other process waits process on the list if this |
| 809 | * process is awaken further processes are not considered. |
| 810 | * |
| 811 | * wake_up_locked() has to be called after changing any variable that could |
| 812 | * change the result of the wait condition. |
| 813 | * |
| 814 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 815 | * signal and 0 if @condition evaluated to true. |
| 816 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 817 | #define wait_event_interruptible_exclusive_locked(wq, condition) \ |
| 818 | ((condition) \ |
Linus Torvalds | bd0f9b3 | 2017-03-07 15:33:14 -0800 | [diff] [blame] | 819 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr)) |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 820 | |
| 821 | /** |
| 822 | * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true |
| 823 | * @wq: the waitqueue to wait on |
| 824 | * @condition: a C expression for the event to wait for |
| 825 | * |
| 826 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 827 | * @condition evaluates to true or a signal is received. |
| 828 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 829 | * |
| 830 | * It must be called with wq.lock being held. This spinlock is |
| 831 | * unlocked while sleeping but @condition testing is done while lock |
| 832 | * is held and when this macro exits the lock is held. |
| 833 | * |
| 834 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 835 | * functions which must match the way they are locked/unlocked outside |
| 836 | * of this macro. |
| 837 | * |
| 838 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 839 | * set thus when other process waits process on the list if this |
| 840 | * process is awaken further processes are not considered. |
| 841 | * |
| 842 | * wake_up_locked() has to be called after changing any variable that could |
| 843 | * change the result of the wait condition. |
| 844 | * |
| 845 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 846 | * signal and 0 if @condition evaluated to true. |
| 847 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 848 | #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \ |
| 849 | ((condition) \ |
Linus Torvalds | bd0f9b3 | 2017-03-07 15:33:14 -0800 | [diff] [blame] | 850 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq)) |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 851 | |
| 852 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 853 | #define __wait_event_killable(wq, condition) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 854 | ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule()) |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 855 | |
| 856 | /** |
| 857 | * wait_event_killable - sleep until a condition gets true |
Jonathan Corbet | 6c423f5 | 2017-07-24 13:58:00 -0600 | [diff] [blame] | 858 | * @wq_head: the waitqueue to wait on |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 859 | * @condition: a C expression for the event to wait for |
| 860 | * |
| 861 | * The process is put to sleep (TASK_KILLABLE) until the |
| 862 | * @condition evaluates to true or a signal is received. |
Jonathan Corbet | 6c423f5 | 2017-07-24 13:58:00 -0600 | [diff] [blame] | 863 | * 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] | 864 | * |
| 865 | * wake_up() has to be called after changing any variable that could |
| 866 | * change the result of the wait condition. |
| 867 | * |
| 868 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 869 | * signal and 0 if @condition evaluated to true. |
| 870 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 871 | #define wait_event_killable(wq_head, condition) \ |
| 872 | ({ \ |
| 873 | int __ret = 0; \ |
| 874 | might_sleep(); \ |
| 875 | if (!(condition)) \ |
| 876 | __ret = __wait_event_killable(wq_head, condition); \ |
| 877 | __ret; \ |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 878 | }) |
| 879 | |
Luis R. Rodriguez | 8ada927 | 2017-08-18 15:15:55 -0700 | [diff] [blame] | 880 | #define __wait_event_killable_timeout(wq_head, condition, timeout) \ |
| 881 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 882 | TASK_KILLABLE, 0, timeout, \ |
| 883 | __ret = schedule_timeout(__ret)) |
| 884 | |
| 885 | /** |
| 886 | * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses |
| 887 | * @wq_head: the waitqueue to wait on |
| 888 | * @condition: a C expression for the event to wait for |
| 889 | * @timeout: timeout, in jiffies |
| 890 | * |
| 891 | * The process is put to sleep (TASK_KILLABLE) until the |
| 892 | * @condition evaluates to true or a kill signal is received. |
| 893 | * The @condition is checked each time the waitqueue @wq_head is woken up. |
| 894 | * |
| 895 | * wake_up() has to be called after changing any variable that could |
| 896 | * change the result of the wait condition. |
| 897 | * |
| 898 | * Returns: |
| 899 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 900 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 901 | * the remaining jiffies (at least 1) if the @condition evaluated |
| 902 | * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was |
| 903 | * interrupted by a kill signal. |
| 904 | * |
| 905 | * Only kill signals interrupt this process. |
| 906 | */ |
| 907 | #define wait_event_killable_timeout(wq_head, condition, timeout) \ |
| 908 | ({ \ |
| 909 | long __ret = timeout; \ |
| 910 | might_sleep(); \ |
| 911 | if (!___wait_cond_timeout(condition)) \ |
| 912 | __ret = __wait_event_killable_timeout(wq_head, \ |
| 913 | condition, timeout); \ |
| 914 | __ret; \ |
| 915 | }) |
| 916 | |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 917 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 918 | #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \ |
| 919 | (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
| 920 | spin_unlock_irq(&lock); \ |
| 921 | cmd; \ |
| 922 | schedule(); \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 923 | spin_lock_irq(&lock)) |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 924 | |
| 925 | /** |
| 926 | * wait_event_lock_irq_cmd - sleep until a condition gets true. The |
| 927 | * condition is checked under the lock. This |
| 928 | * is expected to be called with the lock |
| 929 | * taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 930 | * @wq_head: the waitqueue to wait on |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 931 | * @condition: a C expression for the event to wait for |
| 932 | * @lock: a locked spinlock_t, which will be released before cmd |
| 933 | * and schedule() and reacquired afterwards. |
| 934 | * @cmd: a command which is invoked outside the critical section before |
| 935 | * sleep |
| 936 | * |
| 937 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 938 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 939 | * the waitqueue @wq_head is woken up. |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 940 | * |
| 941 | * wake_up() has to be called after changing any variable that could |
| 942 | * change the result of the wait condition. |
| 943 | * |
| 944 | * This is supposed to be called while holding the lock. The lock is |
| 945 | * dropped before invoking the cmd and going to sleep and is reacquired |
| 946 | * afterwards. |
| 947 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 948 | #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \ |
| 949 | do { \ |
| 950 | if (condition) \ |
| 951 | break; \ |
| 952 | __wait_event_lock_irq(wq_head, condition, lock, cmd); \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 953 | } while (0) |
| 954 | |
| 955 | /** |
| 956 | * wait_event_lock_irq - sleep until a condition gets true. The |
| 957 | * condition is checked under the lock. This |
| 958 | * is expected to be called with the lock |
| 959 | * taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 960 | * @wq_head: the waitqueue to wait on |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 961 | * @condition: a C expression for the event to wait for |
| 962 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 963 | * and reacquired afterwards. |
| 964 | * |
| 965 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 966 | * @condition evaluates to true. The @condition is checked each time |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 967 | * the waitqueue @wq_head is woken up. |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 968 | * |
| 969 | * wake_up() has to be called after changing any variable that could |
| 970 | * change the result of the wait condition. |
| 971 | * |
| 972 | * This is supposed to be called while holding the lock. The lock is |
| 973 | * dropped before going to sleep and is reacquired afterwards. |
| 974 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 975 | #define wait_event_lock_irq(wq_head, condition, lock) \ |
| 976 | do { \ |
| 977 | if (condition) \ |
| 978 | break; \ |
| 979 | __wait_event_lock_irq(wq_head, condition, lock, ); \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 980 | } while (0) |
| 981 | |
| 982 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 983 | #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \ |
| 984 | ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \ |
| 985 | spin_unlock_irq(&lock); \ |
| 986 | cmd; \ |
| 987 | schedule(); \ |
Peter Zijlstra | 8fbd88f | 2013-10-02 11:22:28 +0200 | [diff] [blame] | 988 | spin_lock_irq(&lock)) |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 989 | |
| 990 | /** |
| 991 | * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true. |
| 992 | * The condition is checked under the lock. This is expected to |
| 993 | * be called with the lock taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 994 | * @wq_head: the waitqueue to wait on |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 995 | * @condition: a C expression for the event to wait for |
| 996 | * @lock: a locked spinlock_t, which will be released before cmd and |
| 997 | * schedule() and reacquired afterwards. |
| 998 | * @cmd: a command which is invoked outside the critical section before |
| 999 | * sleep |
| 1000 | * |
| 1001 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 1002 | * @condition evaluates to true or a signal is received. The @condition is |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1003 | * checked each time the waitqueue @wq_head is woken up. |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1004 | * |
| 1005 | * wake_up() has to be called after changing any variable that could |
| 1006 | * change the result of the wait condition. |
| 1007 | * |
| 1008 | * This is supposed to be called while holding the lock. The lock is |
| 1009 | * dropped before invoking the cmd and going to sleep and is reacquired |
| 1010 | * afterwards. |
| 1011 | * |
| 1012 | * The macro will return -ERESTARTSYS if it was interrupted by a signal |
| 1013 | * and 0 if @condition evaluated to true. |
| 1014 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1015 | #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \ |
| 1016 | ({ \ |
| 1017 | int __ret = 0; \ |
| 1018 | if (!(condition)) \ |
| 1019 | __ret = __wait_event_interruptible_lock_irq(wq_head, \ |
| 1020 | condition, lock, cmd); \ |
| 1021 | __ret; \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1022 | }) |
| 1023 | |
| 1024 | /** |
| 1025 | * wait_event_interruptible_lock_irq - sleep until a condition gets true. |
| 1026 | * The condition is checked under the lock. This is expected |
| 1027 | * to be called with the lock taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1028 | * @wq_head: the waitqueue to wait on |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1029 | * @condition: a C expression for the event to wait for |
| 1030 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 1031 | * and reacquired afterwards. |
| 1032 | * |
| 1033 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 1034 | * @condition evaluates to true or signal is received. The @condition is |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1035 | * checked each time the waitqueue @wq_head is woken up. |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1036 | * |
| 1037 | * wake_up() has to be called after changing any variable that could |
| 1038 | * change the result of the wait condition. |
| 1039 | * |
| 1040 | * This is supposed to be called while holding the lock. The lock is |
| 1041 | * dropped before going to sleep and is reacquired afterwards. |
| 1042 | * |
| 1043 | * The macro will return -ERESTARTSYS if it was interrupted by a signal |
| 1044 | * and 0 if @condition evaluated to true. |
| 1045 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1046 | #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \ |
| 1047 | ({ \ |
| 1048 | int __ret = 0; \ |
| 1049 | if (!(condition)) \ |
| 1050 | __ret = __wait_event_interruptible_lock_irq(wq_head, \ |
| 1051 | condition, lock,); \ |
| 1052 | __ret; \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1053 | }) |
| 1054 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1055 | #define __wait_event_interruptible_lock_irq_timeout(wq_head, condition, \ |
| 1056 | lock, timeout) \ |
| 1057 | ___wait_event(wq_head, ___wait_cond_timeout(condition), \ |
| 1058 | TASK_INTERRUPTIBLE, 0, timeout, \ |
| 1059 | spin_unlock_irq(&lock); \ |
| 1060 | __ret = schedule_timeout(__ret); \ |
Peter Zijlstra | a1dc685 | 2013-10-02 11:22:29 +0200 | [diff] [blame] | 1061 | spin_lock_irq(&lock)); |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 1062 | |
| 1063 | /** |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 1064 | * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets |
| 1065 | * true or a timeout elapses. The condition is checked under |
| 1066 | * the lock. This is expected to be called with the lock taken. |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1067 | * @wq_head: the waitqueue to wait on |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 1068 | * @condition: a C expression for the event to wait for |
| 1069 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 1070 | * and reacquired afterwards. |
| 1071 | * @timeout: timeout, in jiffies |
| 1072 | * |
| 1073 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 1074 | * @condition evaluates to true or signal is received. The @condition is |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1075 | * checked each time the waitqueue @wq_head is woken up. |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 1076 | * |
| 1077 | * wake_up() has to be called after changing any variable that could |
| 1078 | * change the result of the wait condition. |
| 1079 | * |
| 1080 | * This is supposed to be called while holding the lock. The lock is |
| 1081 | * dropped before going to sleep and is reacquired afterwards. |
| 1082 | * |
| 1083 | * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it |
| 1084 | * was interrupted by a signal, and the remaining jiffies otherwise |
| 1085 | * if the condition evaluated to true before the timeout elapsed. |
| 1086 | */ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1087 | #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \ |
| 1088 | timeout) \ |
| 1089 | ({ \ |
| 1090 | long __ret = timeout; \ |
| 1091 | if (!___wait_cond_timeout(condition)) \ |
| 1092 | __ret = __wait_event_interruptible_lock_irq_timeout( \ |
| 1093 | wq_head, condition, lock, timeout); \ |
| 1094 | __ret; \ |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 1095 | }) |
| 1096 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | /* |
| 1098 | * Waitqueues which are removed from the waitqueue_head at wakeup time |
| 1099 | */ |
Ingo Molnar | 9d9d676 | 2017-03-05 11:10:18 +0100 | [diff] [blame] | 1100 | void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state); |
| 1101 | void prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state); |
| 1102 | long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state); |
| 1103 | 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] | 1104 | long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout); |
| 1105 | int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key); |
| 1106 | 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] | 1107 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1108 | #define DEFINE_WAIT_FUNC(name, function) \ |
| 1109 | struct wait_queue_entry name = { \ |
| 1110 | .private = current, \ |
| 1111 | .func = function, \ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1112 | .entry = LIST_HEAD_INIT((name).entry), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 1115 | #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function) |
| 1116 | |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1117 | #define init_wait(wait) \ |
| 1118 | do { \ |
| 1119 | (wait)->private = current; \ |
| 1120 | (wait)->func = autoremove_wake_function; \ |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 1121 | INIT_LIST_HEAD(&(wait)->entry); \ |
Ingo Molnar | 4b1c480 | 2017-03-05 12:07:33 +0100 | [diff] [blame] | 1122 | (wait)->flags = 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | } while (0) |
| 1124 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 1125 | #endif /* _LINUX_WAIT_H */ |