Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_WAIT_H |
| 2 | #define _LINUX_WAIT_H |
| 3 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/list.h> |
| 6 | #include <linux/stddef.h> |
| 7 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <asm/current.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 9 | #include <uapi/linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | typedef struct __wait_queue wait_queue_t; |
Peter Zijlstra | 7d47872 | 2009-09-14 19:55:44 +0200 | [diff] [blame] | 12 | typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key); |
| 13 | int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | struct __wait_queue { |
| 16 | unsigned int flags; |
| 17 | #define WQ_FLAG_EXCLUSIVE 0x01 |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 18 | void *private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | wait_queue_func_t func; |
| 20 | struct list_head task_list; |
| 21 | }; |
| 22 | |
| 23 | struct wait_bit_key { |
| 24 | void *flags; |
| 25 | int bit_nr; |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 26 | #define WAIT_ATOMIC_T_BIT_NR -1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | struct wait_bit_queue { |
| 30 | struct wait_bit_key key; |
| 31 | wait_queue_t wait; |
| 32 | }; |
| 33 | |
| 34 | struct __wait_queue_head { |
| 35 | spinlock_t lock; |
| 36 | struct list_head task_list; |
| 37 | }; |
| 38 | typedef struct __wait_queue_head wait_queue_head_t; |
| 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 | |
| 46 | #define __WAITQUEUE_INITIALIZER(name, tsk) { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 47 | .private = tsk, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | .func = default_wake_function, \ |
| 49 | .task_list = { NULL, NULL } } |
| 50 | |
| 51 | #define DECLARE_WAITQUEUE(name, tsk) \ |
| 52 | wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk) |
| 53 | |
| 54 | #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \ |
Ingo Molnar | e4d9191 | 2006-07-03 00:24:34 -0700 | [diff] [blame] | 55 | .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | .task_list = { &(name).task_list, &(name).task_list } } |
| 57 | |
| 58 | #define DECLARE_WAIT_QUEUE_HEAD(name) \ |
| 59 | wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name) |
| 60 | |
| 61 | #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \ |
| 62 | { .flags = word, .bit_nr = bit, } |
| 63 | |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 64 | #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \ |
| 65 | { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, } |
| 66 | |
Peter Zijlstra | f07fdec | 2011-12-13 13:20:54 +0100 | [diff] [blame] | 67 | extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *); |
Peter Zijlstra | 2fc3911 | 2009-08-10 12:33:05 +0100 | [diff] [blame] | 68 | |
| 69 | #define init_waitqueue_head(q) \ |
| 70 | do { \ |
| 71 | static struct lock_class_key __key; \ |
| 72 | \ |
Peter Zijlstra | f07fdec | 2011-12-13 13:20:54 +0100 | [diff] [blame] | 73 | __init_waitqueue_head((q), #q, &__key); \ |
Peter Zijlstra | 2fc3911 | 2009-08-10 12:33:05 +0100 | [diff] [blame] | 74 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Peter Zijlstra | 7259f0d | 2006-10-29 22:46:36 -0800 | [diff] [blame] | 76 | #ifdef CONFIG_LOCKDEP |
| 77 | # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \ |
| 78 | ({ init_waitqueue_head(&name); name; }) |
| 79 | # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \ |
| 80 | wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) |
| 81 | #else |
| 82 | # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name) |
| 83 | #endif |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p) |
| 86 | { |
| 87 | q->flags = 0; |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 88 | q->private = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | q->func = default_wake_function; |
| 90 | } |
| 91 | |
| 92 | static inline void init_waitqueue_func_entry(wait_queue_t *q, |
| 93 | wait_queue_func_t func) |
| 94 | { |
| 95 | q->flags = 0; |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 96 | q->private = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | q->func = func; |
| 98 | } |
| 99 | |
| 100 | static inline int waitqueue_active(wait_queue_head_t *q) |
| 101 | { |
| 102 | return !list_empty(&q->task_list); |
| 103 | } |
| 104 | |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 105 | extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait); |
| 106 | extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait); |
| 107 | extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new) |
| 110 | { |
| 111 | list_add(&new->task_list, &head->task_list); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Used for wake-one threads: |
| 116 | */ |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 117 | static inline void __add_wait_queue_exclusive(wait_queue_head_t *q, |
| 118 | wait_queue_t *wait) |
| 119 | { |
| 120 | wait->flags |= WQ_FLAG_EXCLUSIVE; |
| 121 | __add_wait_queue(q, wait); |
| 122 | } |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | static inline void __add_wait_queue_tail(wait_queue_head_t *head, |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 125 | wait_queue_t *new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
| 127 | list_add_tail(&new->task_list, &head->task_list); |
| 128 | } |
| 129 | |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 130 | static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q, |
| 131 | wait_queue_t *wait) |
| 132 | { |
| 133 | wait->flags |= WQ_FLAG_EXCLUSIVE; |
| 134 | __add_wait_queue_tail(q, wait); |
| 135 | } |
| 136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | static inline void __remove_wait_queue(wait_queue_head_t *head, |
| 138 | wait_queue_t *old) |
| 139 | { |
| 140 | list_del(&old->task_list); |
| 141 | } |
| 142 | |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 143 | void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key); |
Davide Libenzi | 4ede816 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 144 | void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key); |
| 145 | void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, |
| 146 | void *key); |
Thomas Gleixner | 63b2001 | 2011-12-01 00:04:00 +0100 | [diff] [blame] | 147 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr); |
Davide Libenzi | 4ede816 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 148 | void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr); |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 149 | void __wake_up_bit(wait_queue_head_t *, void *, int); |
| 150 | int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); |
| 151 | int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); |
| 152 | void wake_up_bit(void *, int); |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 153 | void wake_up_atomic_t(atomic_t *); |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 154 | int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned); |
| 155 | int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned); |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 156 | int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned); |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 157 | wait_queue_head_t *bit_waitqueue(void *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Matthew Wilcox | e64d66c | 2007-12-06 17:34:36 -0500 | [diff] [blame] | 159 | #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) |
| 160 | #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL) |
| 161 | #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL) |
Thomas Gleixner | 63b2001 | 2011-12-01 00:04:00 +0100 | [diff] [blame] | 162 | #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1) |
| 163 | #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] | 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL) |
| 166 | #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL) |
| 167 | #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] | 168 | #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] | 169 | |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 170 | /* |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 171 | * Wakeup macros to be used to report events to the targets. |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 172 | */ |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 173 | #define wake_up_poll(x, m) \ |
| 174 | __wake_up(x, TASK_NORMAL, 1, (void *) (m)) |
| 175 | #define wake_up_locked_poll(x, m) \ |
| 176 | __wake_up_locked_key((x), TASK_NORMAL, (void *) (m)) |
| 177 | #define wake_up_interruptible_poll(x, m) \ |
| 178 | __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m)) |
| 179 | #define wake_up_interruptible_sync_poll(x, m) \ |
| 180 | __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m)) |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 181 | |
Peter Zijlstra | 2953ef2 | 2013-10-02 11:22:19 +0200 | [diff] [blame] | 182 | #define ___wait_cond_timeout(condition, ret) \ |
| 183 | ({ \ |
| 184 | bool __cond = (condition); \ |
| 185 | if (__cond && !ret) \ |
| 186 | ret = 1; \ |
| 187 | __cond || !ret; \ |
| 188 | }) |
| 189 | |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 190 | #define ___wait_signal_pending(state) \ |
| 191 | ((state == TASK_INTERRUPTIBLE && signal_pending(current)) || \ |
| 192 | (state == TASK_KILLABLE && fatal_signal_pending(current))) |
| 193 | |
| 194 | #define ___wait_nop_ret int ret __always_unused |
| 195 | |
| 196 | #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \ |
| 197 | do { \ |
| 198 | __label__ __out; \ |
| 199 | DEFINE_WAIT(__wait); \ |
| 200 | \ |
| 201 | for (;;) { \ |
| 202 | if (exclusive) \ |
| 203 | prepare_to_wait_exclusive(&wq, &__wait, state); \ |
| 204 | else \ |
| 205 | prepare_to_wait(&wq, &__wait, state); \ |
| 206 | \ |
| 207 | if (condition) \ |
| 208 | break; \ |
| 209 | \ |
| 210 | if (___wait_signal_pending(state)) { \ |
| 211 | ret = -ERESTARTSYS; \ |
| 212 | if (exclusive) { \ |
| 213 | abort_exclusive_wait(&wq, &__wait, \ |
| 214 | state, NULL); \ |
| 215 | goto __out; \ |
| 216 | } \ |
| 217 | break; \ |
| 218 | } \ |
| 219 | \ |
| 220 | cmd; \ |
| 221 | } \ |
| 222 | finish_wait(&wq, &__wait); \ |
| 223 | __out: ; \ |
| 224 | } while (0) |
| 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | #define __wait_event(wq, condition) \ |
Peter Zijlstra | 854267f | 2013-10-02 11:22:22 +0200 | [diff] [blame] | 227 | ___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \ |
| 228 | ___wait_nop_ret, schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * wait_event - sleep until a condition gets true |
| 232 | * @wq: the waitqueue to wait on |
| 233 | * @condition: a C expression for the event to wait for |
| 234 | * |
| 235 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 236 | * @condition evaluates to true. The @condition is checked each time |
| 237 | * the waitqueue @wq is woken up. |
| 238 | * |
| 239 | * wake_up() has to be called after changing any variable that could |
| 240 | * change the result of the wait condition. |
| 241 | */ |
| 242 | #define wait_event(wq, condition) \ |
| 243 | do { \ |
| 244 | if (condition) \ |
| 245 | break; \ |
| 246 | __wait_event(wq, condition); \ |
| 247 | } while (0) |
| 248 | |
| 249 | #define __wait_event_timeout(wq, condition, ret) \ |
Peter Zijlstra | ddc1994 | 2013-10-02 11:22:23 +0200 | [diff] [blame^] | 250 | ___wait_event(wq, ___wait_cond_timeout(condition, ret), \ |
| 251 | TASK_UNINTERRUPTIBLE, 0, ret, \ |
| 252 | ret = schedule_timeout(ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * wait_event_timeout - sleep until a condition gets true or a timeout elapses |
| 256 | * @wq: the waitqueue to wait on |
| 257 | * @condition: a C expression for the event to wait for |
| 258 | * @timeout: timeout, in jiffies |
| 259 | * |
| 260 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 261 | * @condition evaluates to true. The @condition is checked each time |
| 262 | * the waitqueue @wq is woken up. |
| 263 | * |
| 264 | * wake_up() has to be called after changing any variable that could |
| 265 | * change the result of the wait condition. |
| 266 | * |
Imre Deak | 4c663cf | 2013-05-24 15:55:09 -0700 | [diff] [blame] | 267 | * The function returns 0 if the @timeout elapsed, or the remaining |
| 268 | * jiffies (at least 1) if the @condition evaluated to %true before |
| 269 | * the @timeout elapsed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | */ |
| 271 | #define wait_event_timeout(wq, condition, timeout) \ |
| 272 | ({ \ |
| 273 | long __ret = timeout; \ |
| 274 | if (!(condition)) \ |
| 275 | __wait_event_timeout(wq, condition, __ret); \ |
| 276 | __ret; \ |
| 277 | }) |
| 278 | |
| 279 | #define __wait_event_interruptible(wq, condition, ret) \ |
| 280 | do { \ |
| 281 | DEFINE_WAIT(__wait); \ |
| 282 | \ |
| 283 | for (;;) { \ |
| 284 | prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ |
| 285 | if (condition) \ |
| 286 | break; \ |
Peter Zijlstra | 2f2a2b6 | 2013-10-02 11:22:18 +0200 | [diff] [blame] | 287 | if (signal_pending(current)) { \ |
| 288 | ret = -ERESTARTSYS; \ |
| 289 | break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | } \ |
Peter Zijlstra | 2f2a2b6 | 2013-10-02 11:22:18 +0200 | [diff] [blame] | 291 | schedule(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } \ |
| 293 | finish_wait(&wq, &__wait); \ |
| 294 | } while (0) |
| 295 | |
| 296 | /** |
| 297 | * wait_event_interruptible - sleep until a condition gets true |
| 298 | * @wq: the waitqueue to wait on |
| 299 | * @condition: a C expression for the event to wait for |
| 300 | * |
| 301 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 302 | * @condition evaluates to true or a signal is received. |
| 303 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 304 | * |
| 305 | * wake_up() has to be called after changing any variable that could |
| 306 | * change the result of the wait condition. |
| 307 | * |
| 308 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 309 | * signal and 0 if @condition evaluated to true. |
| 310 | */ |
| 311 | #define wait_event_interruptible(wq, condition) \ |
| 312 | ({ \ |
| 313 | int __ret = 0; \ |
| 314 | if (!(condition)) \ |
| 315 | __wait_event_interruptible(wq, condition, __ret); \ |
| 316 | __ret; \ |
| 317 | }) |
| 318 | |
| 319 | #define __wait_event_interruptible_timeout(wq, condition, ret) \ |
| 320 | do { \ |
| 321 | DEFINE_WAIT(__wait); \ |
| 322 | \ |
| 323 | for (;;) { \ |
| 324 | prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ |
Peter Zijlstra | 2953ef2 | 2013-10-02 11:22:19 +0200 | [diff] [blame] | 325 | if (___wait_cond_timeout(condition, ret)) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | break; \ |
Peter Zijlstra | 2f2a2b6 | 2013-10-02 11:22:18 +0200 | [diff] [blame] | 327 | if (signal_pending(current)) { \ |
| 328 | ret = -ERESTARTSYS; \ |
| 329 | break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } \ |
Peter Zijlstra | 2f2a2b6 | 2013-10-02 11:22:18 +0200 | [diff] [blame] | 331 | ret = schedule_timeout(ret); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | } \ |
| 333 | finish_wait(&wq, &__wait); \ |
| 334 | } while (0) |
| 335 | |
| 336 | /** |
| 337 | * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses |
| 338 | * @wq: the waitqueue to wait on |
| 339 | * @condition: a C expression for the event to wait for |
| 340 | * @timeout: timeout, in jiffies |
| 341 | * |
| 342 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 343 | * @condition evaluates to true or a signal is received. |
| 344 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 345 | * |
| 346 | * wake_up() has to be called after changing any variable that could |
| 347 | * change the result of the wait condition. |
| 348 | * |
Imre Deak | 4c663cf | 2013-05-24 15:55:09 -0700 | [diff] [blame] | 349 | * Returns: |
| 350 | * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by |
| 351 | * a signal, or the remaining jiffies (at least 1) if the @condition |
| 352 | * evaluated to %true before the @timeout elapsed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | */ |
| 354 | #define wait_event_interruptible_timeout(wq, condition, timeout) \ |
| 355 | ({ \ |
| 356 | long __ret = timeout; \ |
| 357 | if (!(condition)) \ |
| 358 | __wait_event_interruptible_timeout(wq, condition, __ret); \ |
| 359 | __ret; \ |
| 360 | }) |
| 361 | |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 362 | #define __wait_event_hrtimeout(wq, condition, timeout, state) \ |
| 363 | ({ \ |
| 364 | int __ret = 0; \ |
| 365 | DEFINE_WAIT(__wait); \ |
| 366 | struct hrtimer_sleeper __t; \ |
| 367 | \ |
| 368 | hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \ |
| 369 | HRTIMER_MODE_REL); \ |
| 370 | hrtimer_init_sleeper(&__t, current); \ |
| 371 | if ((timeout).tv64 != KTIME_MAX) \ |
| 372 | hrtimer_start_range_ns(&__t.timer, timeout, \ |
| 373 | current->timer_slack_ns, \ |
| 374 | HRTIMER_MODE_REL); \ |
| 375 | \ |
| 376 | for (;;) { \ |
| 377 | prepare_to_wait(&wq, &__wait, state); \ |
| 378 | if (condition) \ |
| 379 | break; \ |
| 380 | if (state == TASK_INTERRUPTIBLE && \ |
| 381 | signal_pending(current)) { \ |
| 382 | __ret = -ERESTARTSYS; \ |
| 383 | break; \ |
| 384 | } \ |
| 385 | if (!__t.task) { \ |
| 386 | __ret = -ETIME; \ |
| 387 | break; \ |
| 388 | } \ |
| 389 | schedule(); \ |
| 390 | } \ |
| 391 | \ |
| 392 | hrtimer_cancel(&__t.timer); \ |
| 393 | destroy_hrtimer_on_stack(&__t.timer); \ |
| 394 | finish_wait(&wq, &__wait); \ |
| 395 | __ret; \ |
| 396 | }) |
| 397 | |
| 398 | /** |
| 399 | * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses |
| 400 | * @wq: the waitqueue to wait on |
| 401 | * @condition: a C expression for the event to wait for |
| 402 | * @timeout: timeout, as a ktime_t |
| 403 | * |
| 404 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 405 | * @condition evaluates to true or a signal is received. |
| 406 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 407 | * |
| 408 | * wake_up() has to be called after changing any variable that could |
| 409 | * change the result of the wait condition. |
| 410 | * |
| 411 | * The function returns 0 if @condition became true, or -ETIME if the timeout |
| 412 | * elapsed. |
| 413 | */ |
| 414 | #define wait_event_hrtimeout(wq, condition, timeout) \ |
| 415 | ({ \ |
| 416 | int __ret = 0; \ |
| 417 | if (!(condition)) \ |
| 418 | __ret = __wait_event_hrtimeout(wq, condition, timeout, \ |
| 419 | TASK_UNINTERRUPTIBLE); \ |
| 420 | __ret; \ |
| 421 | }) |
| 422 | |
| 423 | /** |
| 424 | * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses |
| 425 | * @wq: the waitqueue to wait on |
| 426 | * @condition: a C expression for the event to wait for |
| 427 | * @timeout: timeout, as a ktime_t |
| 428 | * |
| 429 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 430 | * @condition evaluates to true or a signal is received. |
| 431 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 432 | * |
| 433 | * wake_up() has to be called after changing any variable that could |
| 434 | * change the result of the wait condition. |
| 435 | * |
| 436 | * The function returns 0 if @condition became true, -ERESTARTSYS if it was |
| 437 | * interrupted by a signal, or -ETIME if the timeout elapsed. |
| 438 | */ |
| 439 | #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \ |
| 440 | ({ \ |
| 441 | long __ret = 0; \ |
| 442 | if (!(condition)) \ |
| 443 | __ret = __wait_event_hrtimeout(wq, condition, timeout, \ |
| 444 | TASK_INTERRUPTIBLE); \ |
| 445 | __ret; \ |
| 446 | }) |
| 447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | #define __wait_event_interruptible_exclusive(wq, condition, ret) \ |
| 449 | do { \ |
Peter Zijlstra | bb632bc | 2013-10-02 11:22:20 +0200 | [diff] [blame] | 450 | __label__ __out; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | DEFINE_WAIT(__wait); \ |
| 452 | \ |
| 453 | for (;;) { \ |
| 454 | prepare_to_wait_exclusive(&wq, &__wait, \ |
| 455 | TASK_INTERRUPTIBLE); \ |
Peter Zijlstra | bb632bc | 2013-10-02 11:22:20 +0200 | [diff] [blame] | 456 | if (condition) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | break; \ |
Peter Zijlstra | 2f2a2b6 | 2013-10-02 11:22:18 +0200 | [diff] [blame] | 458 | if (signal_pending(current)) { \ |
| 459 | ret = -ERESTARTSYS; \ |
| 460 | abort_exclusive_wait(&wq, &__wait, \ |
Johannes Weiner | 777c6c5 | 2009-02-04 15:12:14 -0800 | [diff] [blame] | 461 | TASK_INTERRUPTIBLE, NULL); \ |
Peter Zijlstra | bb632bc | 2013-10-02 11:22:20 +0200 | [diff] [blame] | 462 | goto __out; \ |
Peter Zijlstra | 2f2a2b6 | 2013-10-02 11:22:18 +0200 | [diff] [blame] | 463 | } \ |
| 464 | schedule(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } \ |
Peter Zijlstra | bb632bc | 2013-10-02 11:22:20 +0200 | [diff] [blame] | 466 | finish_wait(&wq, &__wait); \ |
| 467 | __out: ; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } while (0) |
| 469 | |
| 470 | #define wait_event_interruptible_exclusive(wq, condition) \ |
| 471 | ({ \ |
| 472 | int __ret = 0; \ |
| 473 | if (!(condition)) \ |
| 474 | __wait_event_interruptible_exclusive(wq, condition, __ret);\ |
| 475 | __ret; \ |
| 476 | }) |
| 477 | |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 478 | |
| 479 | #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \ |
| 480 | ({ \ |
| 481 | int __ret = 0; \ |
| 482 | DEFINE_WAIT(__wait); \ |
| 483 | if (exclusive) \ |
| 484 | __wait.flags |= WQ_FLAG_EXCLUSIVE; \ |
| 485 | do { \ |
| 486 | if (likely(list_empty(&__wait.task_list))) \ |
| 487 | __add_wait_queue_tail(&(wq), &__wait); \ |
| 488 | set_current_state(TASK_INTERRUPTIBLE); \ |
| 489 | if (signal_pending(current)) { \ |
| 490 | __ret = -ERESTARTSYS; \ |
| 491 | break; \ |
| 492 | } \ |
| 493 | if (irq) \ |
| 494 | spin_unlock_irq(&(wq).lock); \ |
| 495 | else \ |
| 496 | spin_unlock(&(wq).lock); \ |
| 497 | schedule(); \ |
| 498 | if (irq) \ |
| 499 | spin_lock_irq(&(wq).lock); \ |
| 500 | else \ |
| 501 | spin_lock(&(wq).lock); \ |
| 502 | } while (!(condition)); \ |
| 503 | __remove_wait_queue(&(wq), &__wait); \ |
| 504 | __set_current_state(TASK_RUNNING); \ |
| 505 | __ret; \ |
| 506 | }) |
| 507 | |
| 508 | |
| 509 | /** |
| 510 | * wait_event_interruptible_locked - sleep until a condition gets true |
| 511 | * @wq: the waitqueue to wait on |
| 512 | * @condition: a C expression for the event to wait for |
| 513 | * |
| 514 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 515 | * @condition evaluates to true or a signal is received. |
| 516 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 517 | * |
| 518 | * It must be called with wq.lock being held. This spinlock is |
| 519 | * unlocked while sleeping but @condition testing is done while lock |
| 520 | * is held and when this macro exits the lock is held. |
| 521 | * |
| 522 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 523 | * functions which must match the way they are locked/unlocked outside |
| 524 | * of this macro. |
| 525 | * |
| 526 | * wake_up_locked() has to be called after changing any variable that could |
| 527 | * change the result of the wait condition. |
| 528 | * |
| 529 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 530 | * signal and 0 if @condition evaluated to true. |
| 531 | */ |
| 532 | #define wait_event_interruptible_locked(wq, condition) \ |
| 533 | ((condition) \ |
| 534 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0)) |
| 535 | |
| 536 | /** |
| 537 | * wait_event_interruptible_locked_irq - sleep until a condition gets true |
| 538 | * @wq: the waitqueue to wait on |
| 539 | * @condition: a C expression for the event to wait for |
| 540 | * |
| 541 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 542 | * @condition evaluates to true or a signal is received. |
| 543 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 544 | * |
| 545 | * It must be called with wq.lock being held. This spinlock is |
| 546 | * unlocked while sleeping but @condition testing is done while lock |
| 547 | * is held and when this macro exits the lock is held. |
| 548 | * |
| 549 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 550 | * functions which must match the way they are locked/unlocked outside |
| 551 | * of this macro. |
| 552 | * |
| 553 | * wake_up_locked() has to be called after changing any variable that could |
| 554 | * change the result of the wait condition. |
| 555 | * |
| 556 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 557 | * signal and 0 if @condition evaluated to true. |
| 558 | */ |
| 559 | #define wait_event_interruptible_locked_irq(wq, condition) \ |
| 560 | ((condition) \ |
| 561 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1)) |
| 562 | |
| 563 | /** |
| 564 | * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true |
| 565 | * @wq: the waitqueue to wait on |
| 566 | * @condition: a C expression for the event to wait for |
| 567 | * |
| 568 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 569 | * @condition evaluates to true or a signal is received. |
| 570 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 571 | * |
| 572 | * It must be called with wq.lock being held. This spinlock is |
| 573 | * unlocked while sleeping but @condition testing is done while lock |
| 574 | * is held and when this macro exits the lock is held. |
| 575 | * |
| 576 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 577 | * functions which must match the way they are locked/unlocked outside |
| 578 | * of this macro. |
| 579 | * |
| 580 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 581 | * set thus when other process waits process on the list if this |
| 582 | * process is awaken further processes are not considered. |
| 583 | * |
| 584 | * wake_up_locked() has to be called after changing any variable that could |
| 585 | * change the result of the wait condition. |
| 586 | * |
| 587 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 588 | * signal and 0 if @condition evaluated to true. |
| 589 | */ |
| 590 | #define wait_event_interruptible_exclusive_locked(wq, condition) \ |
| 591 | ((condition) \ |
| 592 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0)) |
| 593 | |
| 594 | /** |
| 595 | * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true |
| 596 | * @wq: the waitqueue to wait on |
| 597 | * @condition: a C expression for the event to wait for |
| 598 | * |
| 599 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 600 | * @condition evaluates to true or a signal is received. |
| 601 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 602 | * |
| 603 | * It must be called with wq.lock being held. This spinlock is |
| 604 | * unlocked while sleeping but @condition testing is done while lock |
| 605 | * is held and when this macro exits the lock is held. |
| 606 | * |
| 607 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 608 | * functions which must match the way they are locked/unlocked outside |
| 609 | * of this macro. |
| 610 | * |
| 611 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 612 | * set thus when other process waits process on the list if this |
| 613 | * process is awaken further processes are not considered. |
| 614 | * |
| 615 | * wake_up_locked() has to be called after changing any variable that could |
| 616 | * change the result of the wait condition. |
| 617 | * |
| 618 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 619 | * signal and 0 if @condition evaluated to true. |
| 620 | */ |
| 621 | #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \ |
| 622 | ((condition) \ |
| 623 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1)) |
| 624 | |
| 625 | |
| 626 | |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 627 | #define __wait_event_killable(wq, condition, ret) \ |
| 628 | do { \ |
| 629 | DEFINE_WAIT(__wait); \ |
| 630 | \ |
| 631 | for (;;) { \ |
| 632 | prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \ |
| 633 | if (condition) \ |
| 634 | break; \ |
| 635 | if (!fatal_signal_pending(current)) { \ |
| 636 | schedule(); \ |
| 637 | continue; \ |
| 638 | } \ |
| 639 | ret = -ERESTARTSYS; \ |
| 640 | break; \ |
| 641 | } \ |
| 642 | finish_wait(&wq, &__wait); \ |
| 643 | } while (0) |
| 644 | |
| 645 | /** |
| 646 | * wait_event_killable - sleep until a condition gets true |
| 647 | * @wq: the waitqueue to wait on |
| 648 | * @condition: a C expression for the event to wait for |
| 649 | * |
| 650 | * The process is put to sleep (TASK_KILLABLE) until the |
| 651 | * @condition evaluates to true or a signal is received. |
| 652 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 653 | * |
| 654 | * wake_up() has to be called after changing any variable that could |
| 655 | * change the result of the wait condition. |
| 656 | * |
| 657 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 658 | * signal and 0 if @condition evaluated to true. |
| 659 | */ |
| 660 | #define wait_event_killable(wq, condition) \ |
| 661 | ({ \ |
| 662 | int __ret = 0; \ |
| 663 | if (!(condition)) \ |
| 664 | __wait_event_killable(wq, condition, __ret); \ |
| 665 | __ret; \ |
| 666 | }) |
| 667 | |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 668 | |
| 669 | #define __wait_event_lock_irq(wq, condition, lock, cmd) \ |
| 670 | do { \ |
| 671 | DEFINE_WAIT(__wait); \ |
| 672 | \ |
| 673 | for (;;) { \ |
| 674 | prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ |
| 675 | if (condition) \ |
| 676 | break; \ |
| 677 | spin_unlock_irq(&lock); \ |
| 678 | cmd; \ |
| 679 | schedule(); \ |
| 680 | spin_lock_irq(&lock); \ |
| 681 | } \ |
| 682 | finish_wait(&wq, &__wait); \ |
| 683 | } while (0) |
| 684 | |
| 685 | /** |
| 686 | * wait_event_lock_irq_cmd - sleep until a condition gets true. The |
| 687 | * condition is checked under the lock. This |
| 688 | * is expected to be called with the lock |
| 689 | * taken. |
| 690 | * @wq: the waitqueue to wait on |
| 691 | * @condition: a C expression for the event to wait for |
| 692 | * @lock: a locked spinlock_t, which will be released before cmd |
| 693 | * and schedule() and reacquired afterwards. |
| 694 | * @cmd: a command which is invoked outside the critical section before |
| 695 | * sleep |
| 696 | * |
| 697 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 698 | * @condition evaluates to true. The @condition is checked each time |
| 699 | * the waitqueue @wq is woken up. |
| 700 | * |
| 701 | * wake_up() has to be called after changing any variable that could |
| 702 | * change the result of the wait condition. |
| 703 | * |
| 704 | * This is supposed to be called while holding the lock. The lock is |
| 705 | * dropped before invoking the cmd and going to sleep and is reacquired |
| 706 | * afterwards. |
| 707 | */ |
| 708 | #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \ |
| 709 | do { \ |
| 710 | if (condition) \ |
| 711 | break; \ |
| 712 | __wait_event_lock_irq(wq, condition, lock, cmd); \ |
| 713 | } while (0) |
| 714 | |
| 715 | /** |
| 716 | * wait_event_lock_irq - sleep until a condition gets true. The |
| 717 | * condition is checked under the lock. This |
| 718 | * is expected to be called with the lock |
| 719 | * taken. |
| 720 | * @wq: the waitqueue to wait on |
| 721 | * @condition: a C expression for the event to wait for |
| 722 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 723 | * and reacquired afterwards. |
| 724 | * |
| 725 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 726 | * @condition evaluates to true. The @condition is checked each time |
| 727 | * the waitqueue @wq is woken up. |
| 728 | * |
| 729 | * wake_up() has to be called after changing any variable that could |
| 730 | * change the result of the wait condition. |
| 731 | * |
| 732 | * This is supposed to be called while holding the lock. The lock is |
| 733 | * dropped before going to sleep and is reacquired afterwards. |
| 734 | */ |
| 735 | #define wait_event_lock_irq(wq, condition, lock) \ |
| 736 | do { \ |
| 737 | if (condition) \ |
| 738 | break; \ |
| 739 | __wait_event_lock_irq(wq, condition, lock, ); \ |
| 740 | } while (0) |
| 741 | |
| 742 | |
| 743 | #define __wait_event_interruptible_lock_irq(wq, condition, \ |
| 744 | lock, ret, cmd) \ |
| 745 | do { \ |
| 746 | DEFINE_WAIT(__wait); \ |
| 747 | \ |
| 748 | for (;;) { \ |
| 749 | prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ |
| 750 | if (condition) \ |
| 751 | break; \ |
| 752 | if (signal_pending(current)) { \ |
| 753 | ret = -ERESTARTSYS; \ |
| 754 | break; \ |
| 755 | } \ |
| 756 | spin_unlock_irq(&lock); \ |
| 757 | cmd; \ |
| 758 | schedule(); \ |
| 759 | spin_lock_irq(&lock); \ |
| 760 | } \ |
| 761 | finish_wait(&wq, &__wait); \ |
| 762 | } while (0) |
| 763 | |
| 764 | /** |
| 765 | * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true. |
| 766 | * The condition is checked under the lock. This is expected to |
| 767 | * be called with the lock taken. |
| 768 | * @wq: the waitqueue to wait on |
| 769 | * @condition: a C expression for the event to wait for |
| 770 | * @lock: a locked spinlock_t, which will be released before cmd and |
| 771 | * schedule() and reacquired afterwards. |
| 772 | * @cmd: a command which is invoked outside the critical section before |
| 773 | * sleep |
| 774 | * |
| 775 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 776 | * @condition evaluates to true or a signal is received. The @condition is |
| 777 | * checked each time the waitqueue @wq 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 | * This is supposed to be called while holding the lock. The lock is |
| 783 | * dropped before invoking the cmd and going to sleep and is reacquired |
| 784 | * afterwards. |
| 785 | * |
| 786 | * The macro will return -ERESTARTSYS if it was interrupted by a signal |
| 787 | * and 0 if @condition evaluated to true. |
| 788 | */ |
| 789 | #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \ |
| 790 | ({ \ |
| 791 | int __ret = 0; \ |
| 792 | \ |
| 793 | if (!(condition)) \ |
| 794 | __wait_event_interruptible_lock_irq(wq, condition, \ |
| 795 | lock, __ret, cmd); \ |
| 796 | __ret; \ |
| 797 | }) |
| 798 | |
| 799 | /** |
| 800 | * wait_event_interruptible_lock_irq - sleep until a condition gets true. |
| 801 | * The condition is checked under the lock. This is expected |
| 802 | * to be called with the lock taken. |
| 803 | * @wq: the waitqueue to wait on |
| 804 | * @condition: a C expression for the event to wait for |
| 805 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 806 | * and reacquired afterwards. |
| 807 | * |
| 808 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 809 | * @condition evaluates to true or signal is received. The @condition is |
| 810 | * checked each time the waitqueue @wq is woken up. |
| 811 | * |
| 812 | * wake_up() has to be called after changing any variable that could |
| 813 | * change the result of the wait condition. |
| 814 | * |
| 815 | * This is supposed to be called while holding the lock. The lock is |
| 816 | * dropped before going to sleep and is reacquired afterwards. |
| 817 | * |
| 818 | * The macro will return -ERESTARTSYS if it was interrupted by a signal |
| 819 | * and 0 if @condition evaluated to true. |
| 820 | */ |
| 821 | #define wait_event_interruptible_lock_irq(wq, condition, lock) \ |
| 822 | ({ \ |
| 823 | int __ret = 0; \ |
| 824 | \ |
| 825 | if (!(condition)) \ |
| 826 | __wait_event_interruptible_lock_irq(wq, condition, \ |
| 827 | lock, __ret, ); \ |
| 828 | __ret; \ |
| 829 | }) |
| 830 | |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 831 | #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \ |
| 832 | lock, ret) \ |
| 833 | do { \ |
| 834 | DEFINE_WAIT(__wait); \ |
| 835 | \ |
| 836 | for (;;) { \ |
| 837 | prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ |
Peter Zijlstra | 2953ef2 | 2013-10-02 11:22:19 +0200 | [diff] [blame] | 838 | if (___wait_cond_timeout(condition, ret)) \ |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 839 | break; \ |
| 840 | if (signal_pending(current)) { \ |
| 841 | ret = -ERESTARTSYS; \ |
| 842 | break; \ |
| 843 | } \ |
| 844 | spin_unlock_irq(&lock); \ |
| 845 | ret = schedule_timeout(ret); \ |
| 846 | spin_lock_irq(&lock); \ |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 847 | } \ |
| 848 | finish_wait(&wq, &__wait); \ |
| 849 | } while (0) |
| 850 | |
| 851 | /** |
| 852 | * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses. |
| 853 | * The condition is checked under the lock. This is expected |
| 854 | * to be called with the lock taken. |
| 855 | * @wq: the waitqueue to wait on |
| 856 | * @condition: a C expression for the event to wait for |
| 857 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 858 | * and reacquired afterwards. |
| 859 | * @timeout: timeout, in jiffies |
| 860 | * |
| 861 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 862 | * @condition evaluates to true or signal is received. The @condition is |
| 863 | * checked each time the waitqueue @wq is woken up. |
| 864 | * |
| 865 | * wake_up() has to be called after changing any variable that could |
| 866 | * change the result of the wait condition. |
| 867 | * |
| 868 | * This is supposed to be called while holding the lock. The lock is |
| 869 | * dropped before going to sleep and is reacquired afterwards. |
| 870 | * |
| 871 | * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it |
| 872 | * was interrupted by a signal, and the remaining jiffies otherwise |
| 873 | * if the condition evaluated to true before the timeout elapsed. |
| 874 | */ |
| 875 | #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \ |
| 876 | timeout) \ |
| 877 | ({ \ |
| 878 | int __ret = timeout; \ |
| 879 | \ |
| 880 | if (!(condition)) \ |
| 881 | __wait_event_interruptible_lock_irq_timeout( \ |
| 882 | wq, condition, lock, __ret); \ |
| 883 | __ret; \ |
| 884 | }) |
| 885 | |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | * These are the old interfaces to sleep waiting for an event. |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 889 | * They are racy. DO NOT use them, use the wait_event* interfaces above. |
| 890 | * We plan to remove these interfaces. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | */ |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 892 | extern void sleep_on(wait_queue_head_t *q); |
| 893 | extern long sleep_on_timeout(wait_queue_head_t *q, |
| 894 | signed long timeout); |
| 895 | extern void interruptible_sleep_on(wait_queue_head_t *q); |
| 896 | extern long interruptible_sleep_on_timeout(wait_queue_head_t *q, |
| 897 | signed long timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | /* |
| 900 | * Waitqueues which are removed from the waitqueue_head at wakeup time |
| 901 | */ |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 902 | void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state); |
| 903 | void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state); |
| 904 | void finish_wait(wait_queue_head_t *q, wait_queue_t *wait); |
Johannes Weiner | 777c6c5 | 2009-02-04 15:12:14 -0800 | [diff] [blame] | 905 | void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, |
| 906 | unsigned int mode, void *key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); |
| 908 | int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); |
| 909 | |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 910 | #define DEFINE_WAIT_FUNC(name, function) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | wait_queue_t name = { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 912 | .private = current, \ |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 913 | .func = function, \ |
blaisorblade@yahoo.it | 7e43c84 | 2005-05-25 01:31:42 +0200 | [diff] [blame] | 914 | .task_list = LIST_HEAD_INIT((name).task_list), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 917 | #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function) |
| 918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | #define DEFINE_WAIT_BIT(name, word, bit) \ |
| 920 | struct wait_bit_queue name = { \ |
| 921 | .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \ |
| 922 | .wait = { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 923 | .private = current, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | .func = wake_bit_function, \ |
| 925 | .task_list = \ |
| 926 | LIST_HEAD_INIT((name).wait.task_list), \ |
| 927 | }, \ |
| 928 | } |
| 929 | |
| 930 | #define init_wait(wait) \ |
| 931 | do { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 932 | (wait)->private = current; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | (wait)->func = autoremove_wake_function; \ |
| 934 | INIT_LIST_HEAD(&(wait)->task_list); \ |
Evgeny Kuznetsov | 231d0ae | 2010-10-05 12:47:57 +0400 | [diff] [blame] | 935 | (wait)->flags = 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | } while (0) |
| 937 | |
| 938 | /** |
| 939 | * wait_on_bit - wait for a bit to be cleared |
| 940 | * @word: the word being waited on, a kernel virtual address |
| 941 | * @bit: the bit of the word being waited on |
| 942 | * @action: the function used to sleep, which may take special actions |
| 943 | * @mode: the task state to sleep in |
| 944 | * |
| 945 | * There is a standard hashed waitqueue table for generic use. This |
| 946 | * is the part of the hashtable's accessor API that waits on a bit. |
| 947 | * For instance, if one were to have waiters on a bitflag, one would |
| 948 | * call wait_on_bit() in threads waiting for the bit to clear. |
| 949 | * One uses wait_on_bit() where one is waiting for the bit to clear, |
| 950 | * but has no intention of setting it. |
| 951 | */ |
| 952 | static inline int wait_on_bit(void *word, int bit, |
| 953 | int (*action)(void *), unsigned mode) |
| 954 | { |
| 955 | if (!test_bit(bit, word)) |
| 956 | return 0; |
| 957 | return out_of_line_wait_on_bit(word, bit, action, mode); |
| 958 | } |
| 959 | |
| 960 | /** |
| 961 | * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it |
| 962 | * @word: the word being waited on, a kernel virtual address |
| 963 | * @bit: the bit of the word being waited on |
| 964 | * @action: the function used to sleep, which may take special actions |
| 965 | * @mode: the task state to sleep in |
| 966 | * |
| 967 | * There is a standard hashed waitqueue table for generic use. This |
| 968 | * is the part of the hashtable's accessor API that waits on a bit |
| 969 | * when one intends to set it, for instance, trying to lock bitflags. |
| 970 | * For instance, if one were to have waiters trying to set bitflag |
| 971 | * and waiting for it to clear before setting it, one would call |
| 972 | * wait_on_bit() in threads waiting to be able to set the bit. |
| 973 | * One uses wait_on_bit_lock() where one is waiting for the bit to |
| 974 | * clear with the intention of setting it, and when done, clearing it. |
| 975 | */ |
| 976 | static inline int wait_on_bit_lock(void *word, int bit, |
| 977 | int (*action)(void *), unsigned mode) |
| 978 | { |
| 979 | if (!test_and_set_bit(bit, word)) |
| 980 | return 0; |
| 981 | return out_of_line_wait_on_bit_lock(word, bit, action, mode); |
| 982 | } |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 983 | |
| 984 | /** |
| 985 | * wait_on_atomic_t - Wait for an atomic_t to become 0 |
| 986 | * @val: The atomic value being waited on, a kernel virtual address |
| 987 | * @action: the function used to sleep, which may take special actions |
| 988 | * @mode: the task state to sleep in |
| 989 | * |
| 990 | * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for |
| 991 | * the purpose of getting a waitqueue, but we set the key to a bit number |
| 992 | * outside of the target 'word'. |
| 993 | */ |
| 994 | static inline |
| 995 | int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode) |
| 996 | { |
| 997 | if (atomic_read(val) == 0) |
| 998 | return 0; |
| 999 | return out_of_line_wait_on_atomic_t(val, action, mode); |
| 1000 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | #endif |