Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_WAIT_H |
| 2 | #define _LINUX_WAIT_H |
| 3 | |
| 4 | #define WNOHANG 0x00000001 |
| 5 | #define WUNTRACED 0x00000002 |
| 6 | #define WSTOPPED WUNTRACED |
| 7 | #define WEXITED 0x00000004 |
| 8 | #define WCONTINUED 0x00000008 |
| 9 | #define WNOWAIT 0x01000000 /* Don't reap, just poll status. */ |
| 10 | |
| 11 | #define __WNOTHREAD 0x20000000 /* Don't wait on children of other threads in this group */ |
| 12 | #define __WALL 0x40000000 /* Wait on all children, regardless of type */ |
| 13 | #define __WCLONE 0x80000000 /* Wait only on non-SIGCHLD children */ |
| 14 | |
| 15 | /* First argument to waitid: */ |
| 16 | #define P_ALL 0 |
| 17 | #define P_PID 1 |
| 18 | #define P_PGID 2 |
| 19 | |
| 20 | #ifdef __KERNEL__ |
| 21 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/list.h> |
| 23 | #include <linux/stddef.h> |
| 24 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/current.h> |
| 26 | |
| 27 | typedef struct __wait_queue wait_queue_t; |
Peter Zijlstra | 7d47872 | 2009-09-14 19:55:44 +0200 | [diff] [blame] | 28 | typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key); |
| 29 | 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] | 30 | |
| 31 | struct __wait_queue { |
| 32 | unsigned int flags; |
| 33 | #define WQ_FLAG_EXCLUSIVE 0x01 |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 34 | void *private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | wait_queue_func_t func; |
| 36 | struct list_head task_list; |
| 37 | }; |
| 38 | |
| 39 | struct wait_bit_key { |
| 40 | void *flags; |
| 41 | int bit_nr; |
| 42 | }; |
| 43 | |
| 44 | struct wait_bit_queue { |
| 45 | struct wait_bit_key key; |
| 46 | wait_queue_t wait; |
| 47 | }; |
| 48 | |
| 49 | struct __wait_queue_head { |
| 50 | spinlock_t lock; |
| 51 | struct list_head task_list; |
| 52 | }; |
| 53 | typedef struct __wait_queue_head wait_queue_head_t; |
| 54 | |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 55 | struct task_struct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Macros for declaration and initialisaton of the datatypes |
| 59 | */ |
| 60 | |
| 61 | #define __WAITQUEUE_INITIALIZER(name, tsk) { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 62 | .private = tsk, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | .func = default_wake_function, \ |
| 64 | .task_list = { NULL, NULL } } |
| 65 | |
| 66 | #define DECLARE_WAITQUEUE(name, tsk) \ |
| 67 | wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk) |
| 68 | |
| 69 | #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \ |
Ingo Molnar | e4d9191 | 2006-07-03 00:24:34 -0700 | [diff] [blame] | 70 | .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | .task_list = { &(name).task_list, &(name).task_list } } |
| 72 | |
| 73 | #define DECLARE_WAIT_QUEUE_HEAD(name) \ |
| 74 | wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name) |
| 75 | |
| 76 | #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \ |
| 77 | { .flags = word, .bit_nr = bit, } |
| 78 | |
Peter Zijlstra | f07fdec | 2011-12-13 13:20:54 +0100 | [diff] [blame] | 79 | 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] | 80 | |
| 81 | #define init_waitqueue_head(q) \ |
| 82 | do { \ |
| 83 | static struct lock_class_key __key; \ |
| 84 | \ |
Peter Zijlstra | f07fdec | 2011-12-13 13:20:54 +0100 | [diff] [blame] | 85 | __init_waitqueue_head((q), #q, &__key); \ |
Peter Zijlstra | 2fc3911 | 2009-08-10 12:33:05 +0100 | [diff] [blame] | 86 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Peter Zijlstra | 7259f0d | 2006-10-29 22:46:36 -0800 | [diff] [blame] | 88 | #ifdef CONFIG_LOCKDEP |
| 89 | # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \ |
| 90 | ({ init_waitqueue_head(&name); name; }) |
| 91 | # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \ |
| 92 | wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) |
| 93 | #else |
| 94 | # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name) |
| 95 | #endif |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p) |
| 98 | { |
| 99 | q->flags = 0; |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 100 | q->private = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | q->func = default_wake_function; |
| 102 | } |
| 103 | |
| 104 | static inline void init_waitqueue_func_entry(wait_queue_t *q, |
| 105 | wait_queue_func_t func) |
| 106 | { |
| 107 | q->flags = 0; |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 108 | q->private = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | q->func = func; |
| 110 | } |
| 111 | |
| 112 | static inline int waitqueue_active(wait_queue_head_t *q) |
| 113 | { |
| 114 | return !list_empty(&q->task_list); |
| 115 | } |
| 116 | |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 117 | extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait); |
| 118 | extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait); |
| 119 | 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] | 120 | |
| 121 | static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new) |
| 122 | { |
| 123 | list_add(&new->task_list, &head->task_list); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Used for wake-one threads: |
| 128 | */ |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 129 | static inline void __add_wait_queue_exclusive(wait_queue_head_t *q, |
| 130 | wait_queue_t *wait) |
| 131 | { |
| 132 | wait->flags |= WQ_FLAG_EXCLUSIVE; |
| 133 | __add_wait_queue(q, wait); |
| 134 | } |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | static inline void __add_wait_queue_tail(wait_queue_head_t *head, |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 137 | wait_queue_t *new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | { |
| 139 | list_add_tail(&new->task_list, &head->task_list); |
| 140 | } |
| 141 | |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 142 | static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q, |
| 143 | wait_queue_t *wait) |
| 144 | { |
| 145 | wait->flags |= WQ_FLAG_EXCLUSIVE; |
| 146 | __add_wait_queue_tail(q, wait); |
| 147 | } |
| 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static inline void __remove_wait_queue(wait_queue_head_t *head, |
| 150 | wait_queue_t *old) |
| 151 | { |
| 152 | list_del(&old->task_list); |
| 153 | } |
| 154 | |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 155 | 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] | 156 | void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key); |
| 157 | void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, |
| 158 | void *key); |
Thomas Gleixner | 63b2001 | 2011-12-01 00:04:00 +0100 | [diff] [blame] | 159 | 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] | 160 | 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] | 161 | void __wake_up_bit(wait_queue_head_t *, void *, int); |
| 162 | int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); |
| 163 | int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); |
| 164 | void wake_up_bit(void *, int); |
| 165 | int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned); |
| 166 | int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned); |
| 167 | wait_queue_head_t *bit_waitqueue(void *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Matthew Wilcox | e64d66c | 2007-12-06 17:34:36 -0500 | [diff] [blame] | 169 | #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) |
| 170 | #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL) |
| 171 | #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL) |
Thomas Gleixner | 63b2001 | 2011-12-01 00:04:00 +0100 | [diff] [blame] | 172 | #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1) |
| 173 | #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] | 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL) |
| 176 | #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL) |
| 177 | #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] | 178 | #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] | 179 | |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 180 | /* |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 181 | * Wakeup macros to be used to report events to the targets. |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 182 | */ |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 183 | #define wake_up_poll(x, m) \ |
| 184 | __wake_up(x, TASK_NORMAL, 1, (void *) (m)) |
| 185 | #define wake_up_locked_poll(x, m) \ |
| 186 | __wake_up_locked_key((x), TASK_NORMAL, (void *) (m)) |
| 187 | #define wake_up_interruptible_poll(x, m) \ |
| 188 | __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m)) |
| 189 | #define wake_up_interruptible_sync_poll(x, m) \ |
| 190 | __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m)) |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | #define __wait_event(wq, condition) \ |
| 193 | do { \ |
| 194 | DEFINE_WAIT(__wait); \ |
| 195 | \ |
| 196 | for (;;) { \ |
| 197 | prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ |
| 198 | if (condition) \ |
| 199 | break; \ |
| 200 | schedule(); \ |
| 201 | } \ |
| 202 | finish_wait(&wq, &__wait); \ |
| 203 | } while (0) |
| 204 | |
| 205 | /** |
| 206 | * wait_event - sleep until a condition gets true |
| 207 | * @wq: the waitqueue to wait on |
| 208 | * @condition: a C expression for the event to wait for |
| 209 | * |
| 210 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 211 | * @condition evaluates to true. The @condition is checked each time |
| 212 | * the waitqueue @wq is woken up. |
| 213 | * |
| 214 | * wake_up() has to be called after changing any variable that could |
| 215 | * change the result of the wait condition. |
| 216 | */ |
| 217 | #define wait_event(wq, condition) \ |
| 218 | do { \ |
| 219 | if (condition) \ |
| 220 | break; \ |
| 221 | __wait_event(wq, condition); \ |
| 222 | } while (0) |
| 223 | |
| 224 | #define __wait_event_timeout(wq, condition, ret) \ |
| 225 | do { \ |
| 226 | DEFINE_WAIT(__wait); \ |
| 227 | \ |
| 228 | for (;;) { \ |
| 229 | prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ |
| 230 | if (condition) \ |
| 231 | break; \ |
| 232 | ret = schedule_timeout(ret); \ |
| 233 | if (!ret) \ |
| 234 | break; \ |
| 235 | } \ |
| 236 | finish_wait(&wq, &__wait); \ |
| 237 | } while (0) |
| 238 | |
| 239 | /** |
| 240 | * wait_event_timeout - sleep until a condition gets true or a timeout elapses |
| 241 | * @wq: the waitqueue to wait on |
| 242 | * @condition: a C expression for the event to wait for |
| 243 | * @timeout: timeout, in jiffies |
| 244 | * |
| 245 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 246 | * @condition evaluates to true. The @condition is checked each time |
| 247 | * the waitqueue @wq is woken up. |
| 248 | * |
| 249 | * wake_up() has to be called after changing any variable that could |
| 250 | * change the result of the wait condition. |
| 251 | * |
| 252 | * The function returns 0 if the @timeout elapsed, and the remaining |
| 253 | * jiffies if the condition evaluated to true before the timeout elapsed. |
| 254 | */ |
| 255 | #define wait_event_timeout(wq, condition, timeout) \ |
| 256 | ({ \ |
| 257 | long __ret = timeout; \ |
| 258 | if (!(condition)) \ |
| 259 | __wait_event_timeout(wq, condition, __ret); \ |
| 260 | __ret; \ |
| 261 | }) |
| 262 | |
| 263 | #define __wait_event_interruptible(wq, condition, ret) \ |
| 264 | do { \ |
| 265 | DEFINE_WAIT(__wait); \ |
| 266 | \ |
| 267 | for (;;) { \ |
| 268 | prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ |
| 269 | if (condition) \ |
| 270 | break; \ |
| 271 | if (!signal_pending(current)) { \ |
| 272 | schedule(); \ |
| 273 | continue; \ |
| 274 | } \ |
| 275 | ret = -ERESTARTSYS; \ |
| 276 | break; \ |
| 277 | } \ |
| 278 | finish_wait(&wq, &__wait); \ |
| 279 | } while (0) |
| 280 | |
| 281 | /** |
| 282 | * wait_event_interruptible - sleep until a condition gets true |
| 283 | * @wq: the waitqueue to wait on |
| 284 | * @condition: a C expression for the event to wait for |
| 285 | * |
| 286 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 287 | * @condition evaluates to true or a signal is received. |
| 288 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 289 | * |
| 290 | * wake_up() has to be called after changing any variable that could |
| 291 | * change the result of the wait condition. |
| 292 | * |
| 293 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 294 | * signal and 0 if @condition evaluated to true. |
| 295 | */ |
| 296 | #define wait_event_interruptible(wq, condition) \ |
| 297 | ({ \ |
| 298 | int __ret = 0; \ |
| 299 | if (!(condition)) \ |
| 300 | __wait_event_interruptible(wq, condition, __ret); \ |
| 301 | __ret; \ |
| 302 | }) |
| 303 | |
| 304 | #define __wait_event_interruptible_timeout(wq, condition, ret) \ |
| 305 | do { \ |
| 306 | DEFINE_WAIT(__wait); \ |
| 307 | \ |
| 308 | for (;;) { \ |
| 309 | prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ |
| 310 | if (condition) \ |
| 311 | break; \ |
| 312 | if (!signal_pending(current)) { \ |
| 313 | ret = schedule_timeout(ret); \ |
| 314 | if (!ret) \ |
| 315 | break; \ |
| 316 | continue; \ |
| 317 | } \ |
| 318 | ret = -ERESTARTSYS; \ |
| 319 | break; \ |
| 320 | } \ |
| 321 | finish_wait(&wq, &__wait); \ |
| 322 | } while (0) |
| 323 | |
| 324 | /** |
| 325 | * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses |
| 326 | * @wq: the waitqueue to wait on |
| 327 | * @condition: a C expression for the event to wait for |
| 328 | * @timeout: timeout, in jiffies |
| 329 | * |
| 330 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 331 | * @condition evaluates to true or a signal is received. |
| 332 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 333 | * |
| 334 | * wake_up() has to be called after changing any variable that could |
| 335 | * change the result of the wait condition. |
| 336 | * |
| 337 | * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it |
| 338 | * was interrupted by a signal, and the remaining jiffies otherwise |
| 339 | * if the condition evaluated to true before the timeout elapsed. |
| 340 | */ |
| 341 | #define wait_event_interruptible_timeout(wq, condition, timeout) \ |
| 342 | ({ \ |
| 343 | long __ret = timeout; \ |
| 344 | if (!(condition)) \ |
| 345 | __wait_event_interruptible_timeout(wq, condition, __ret); \ |
| 346 | __ret; \ |
| 347 | }) |
| 348 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 349 | #define __wait_io_event_interruptible(wq, condition, ret) \ |
| 350 | do { \ |
| 351 | DEFINE_WAIT(__wait); \ |
| 352 | \ |
| 353 | for (;;) { \ |
| 354 | prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ |
| 355 | if (condition) \ |
| 356 | break; \ |
| 357 | if (!signal_pending(current)) { \ |
| 358 | io_schedule(); \ |
| 359 | continue; \ |
| 360 | } \ |
| 361 | ret = -ERESTARTSYS; \ |
| 362 | break; \ |
| 363 | } \ |
| 364 | finish_wait(&wq, &__wait); \ |
| 365 | } while (0) |
| 366 | |
| 367 | /** |
| 368 | * wait_io_event_interruptible - sleep until an io condition gets true |
| 369 | * @wq: the waitqueue to wait on |
| 370 | * @condition: a C expression for the event to wait for |
| 371 | * |
| 372 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 373 | * @condition evaluates to true or a signal is received. |
| 374 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 375 | * |
| 376 | * wake_up() has to be called after changing any variable that could |
| 377 | * change the result of the wait condition. |
| 378 | * |
| 379 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 380 | * signal and 0 if @condition evaluated to true. |
| 381 | */ |
| 382 | #define wait_io_event_interruptible(wq, condition) \ |
| 383 | ({ \ |
| 384 | int __ret = 0; \ |
| 385 | if (!(condition)) \ |
| 386 | __wait_io_event_interruptible(wq, condition, __ret); \ |
| 387 | __ret; \ |
| 388 | }) |
| 389 | |
| 390 | #define __wait_io_event_interruptible_timeout(wq, condition, ret) \ |
| 391 | do { \ |
| 392 | DEFINE_WAIT(__wait); \ |
| 393 | \ |
| 394 | for (;;) { \ |
| 395 | prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ |
| 396 | if (condition) \ |
| 397 | break; \ |
| 398 | if (!signal_pending(current)) { \ |
| 399 | ret = io_schedule_timeout(ret); \ |
| 400 | if (!ret) \ |
| 401 | break; \ |
| 402 | continue; \ |
| 403 | } \ |
| 404 | ret = -ERESTARTSYS; \ |
| 405 | break; \ |
| 406 | } \ |
| 407 | finish_wait(&wq, &__wait); \ |
| 408 | } while (0) |
| 409 | |
| 410 | /** |
| 411 | * wait_io_event_interruptible_timeout - sleep until an io condition gets true or a timeout elapses |
| 412 | * @wq: the waitqueue to wait on |
| 413 | * @condition: a C expression for the event to wait for |
| 414 | * @timeout: timeout, in jiffies |
| 415 | * |
| 416 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 417 | * @condition evaluates to true or a signal is received. |
| 418 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 419 | * |
| 420 | * wake_up() has to be called after changing any variable that could |
| 421 | * change the result of the wait condition. |
| 422 | * |
| 423 | * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it |
| 424 | * was interrupted by a signal, and the remaining jiffies otherwise |
| 425 | * if the condition evaluated to true before the timeout elapsed. |
| 426 | */ |
| 427 | |
| 428 | #define wait_io_event_interruptible_timeout(wq, condition, timeout) \ |
| 429 | ({ \ |
| 430 | long __ret = timeout; \ |
| 431 | if (!(condition)) \ |
| 432 | __wait_io_event_interruptible_timeout(wq, condition, __ret); \ |
| 433 | __ret; \ |
| 434 | }) |
| 435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | #define __wait_event_interruptible_exclusive(wq, condition, ret) \ |
| 437 | do { \ |
| 438 | DEFINE_WAIT(__wait); \ |
| 439 | \ |
| 440 | for (;;) { \ |
| 441 | prepare_to_wait_exclusive(&wq, &__wait, \ |
| 442 | TASK_INTERRUPTIBLE); \ |
Johannes Weiner | 777c6c5 | 2009-02-04 15:12:14 -0800 | [diff] [blame] | 443 | if (condition) { \ |
| 444 | finish_wait(&wq, &__wait); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | break; \ |
Johannes Weiner | 777c6c5 | 2009-02-04 15:12:14 -0800 | [diff] [blame] | 446 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | if (!signal_pending(current)) { \ |
| 448 | schedule(); \ |
| 449 | continue; \ |
| 450 | } \ |
| 451 | ret = -ERESTARTSYS; \ |
Johannes Weiner | 777c6c5 | 2009-02-04 15:12:14 -0800 | [diff] [blame] | 452 | abort_exclusive_wait(&wq, &__wait, \ |
| 453 | TASK_INTERRUPTIBLE, NULL); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | break; \ |
| 455 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | } while (0) |
| 457 | |
| 458 | #define wait_event_interruptible_exclusive(wq, condition) \ |
| 459 | ({ \ |
| 460 | int __ret = 0; \ |
| 461 | if (!(condition)) \ |
| 462 | __wait_event_interruptible_exclusive(wq, condition, __ret);\ |
| 463 | __ret; \ |
| 464 | }) |
| 465 | |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 466 | |
| 467 | #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \ |
| 468 | ({ \ |
| 469 | int __ret = 0; \ |
| 470 | DEFINE_WAIT(__wait); \ |
| 471 | if (exclusive) \ |
| 472 | __wait.flags |= WQ_FLAG_EXCLUSIVE; \ |
| 473 | do { \ |
| 474 | if (likely(list_empty(&__wait.task_list))) \ |
| 475 | __add_wait_queue_tail(&(wq), &__wait); \ |
| 476 | set_current_state(TASK_INTERRUPTIBLE); \ |
| 477 | if (signal_pending(current)) { \ |
| 478 | __ret = -ERESTARTSYS; \ |
| 479 | break; \ |
| 480 | } \ |
| 481 | if (irq) \ |
| 482 | spin_unlock_irq(&(wq).lock); \ |
| 483 | else \ |
| 484 | spin_unlock(&(wq).lock); \ |
| 485 | schedule(); \ |
| 486 | if (irq) \ |
| 487 | spin_lock_irq(&(wq).lock); \ |
| 488 | else \ |
| 489 | spin_lock(&(wq).lock); \ |
| 490 | } while (!(condition)); \ |
| 491 | __remove_wait_queue(&(wq), &__wait); \ |
| 492 | __set_current_state(TASK_RUNNING); \ |
| 493 | __ret; \ |
| 494 | }) |
| 495 | |
| 496 | |
| 497 | /** |
| 498 | * wait_event_interruptible_locked - sleep until a condition gets true |
| 499 | * @wq: the waitqueue to wait on |
| 500 | * @condition: a C expression for the event to wait for |
| 501 | * |
| 502 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 503 | * @condition evaluates to true or a signal is received. |
| 504 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 505 | * |
| 506 | * It must be called with wq.lock being held. This spinlock is |
| 507 | * unlocked while sleeping but @condition testing is done while lock |
| 508 | * is held and when this macro exits the lock is held. |
| 509 | * |
| 510 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 511 | * functions which must match the way they are locked/unlocked outside |
| 512 | * of this macro. |
| 513 | * |
| 514 | * wake_up_locked() has to be called after changing any variable that could |
| 515 | * change the result of the wait condition. |
| 516 | * |
| 517 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 518 | * signal and 0 if @condition evaluated to true. |
| 519 | */ |
| 520 | #define wait_event_interruptible_locked(wq, condition) \ |
| 521 | ((condition) \ |
| 522 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0)) |
| 523 | |
| 524 | /** |
| 525 | * wait_event_interruptible_locked_irq - sleep until a condition gets true |
| 526 | * @wq: the waitqueue to wait on |
| 527 | * @condition: a C expression for the event to wait for |
| 528 | * |
| 529 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 530 | * @condition evaluates to true or a signal is received. |
| 531 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 532 | * |
| 533 | * It must be called with wq.lock being held. This spinlock is |
| 534 | * unlocked while sleeping but @condition testing is done while lock |
| 535 | * is held and when this macro exits the lock is held. |
| 536 | * |
| 537 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 538 | * functions which must match the way they are locked/unlocked outside |
| 539 | * of this macro. |
| 540 | * |
| 541 | * wake_up_locked() has to be called after changing any variable that could |
| 542 | * change the result of the wait condition. |
| 543 | * |
| 544 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 545 | * signal and 0 if @condition evaluated to true. |
| 546 | */ |
| 547 | #define wait_event_interruptible_locked_irq(wq, condition) \ |
| 548 | ((condition) \ |
| 549 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1)) |
| 550 | |
| 551 | /** |
| 552 | * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true |
| 553 | * @wq: the waitqueue to wait on |
| 554 | * @condition: a C expression for the event to wait for |
| 555 | * |
| 556 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 557 | * @condition evaluates to true or a signal is received. |
| 558 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 559 | * |
| 560 | * It must be called with wq.lock being held. This spinlock is |
| 561 | * unlocked while sleeping but @condition testing is done while lock |
| 562 | * is held and when this macro exits the lock is held. |
| 563 | * |
| 564 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 565 | * functions which must match the way they are locked/unlocked outside |
| 566 | * of this macro. |
| 567 | * |
| 568 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 569 | * set thus when other process waits process on the list if this |
| 570 | * process is awaken further processes are not considered. |
| 571 | * |
| 572 | * wake_up_locked() has to be called after changing any variable that could |
| 573 | * change the result of the wait condition. |
| 574 | * |
| 575 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 576 | * signal and 0 if @condition evaluated to true. |
| 577 | */ |
| 578 | #define wait_event_interruptible_exclusive_locked(wq, condition) \ |
| 579 | ((condition) \ |
| 580 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0)) |
| 581 | |
| 582 | /** |
| 583 | * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true |
| 584 | * @wq: the waitqueue to wait on |
| 585 | * @condition: a C expression for the event to wait for |
| 586 | * |
| 587 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 588 | * @condition evaluates to true or a signal is received. |
| 589 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 590 | * |
| 591 | * It must be called with wq.lock being held. This spinlock is |
| 592 | * unlocked while sleeping but @condition testing is done while lock |
| 593 | * is held and when this macro exits the lock is held. |
| 594 | * |
| 595 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 596 | * functions which must match the way they are locked/unlocked outside |
| 597 | * of this macro. |
| 598 | * |
| 599 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 600 | * set thus when other process waits process on the list if this |
| 601 | * process is awaken further processes are not considered. |
| 602 | * |
| 603 | * wake_up_locked() has to be called after changing any variable that could |
| 604 | * change the result of the wait condition. |
| 605 | * |
| 606 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 607 | * signal and 0 if @condition evaluated to true. |
| 608 | */ |
| 609 | #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \ |
| 610 | ((condition) \ |
| 611 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1)) |
| 612 | |
| 613 | |
| 614 | |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 615 | #define __wait_event_killable(wq, condition, ret) \ |
| 616 | do { \ |
| 617 | DEFINE_WAIT(__wait); \ |
| 618 | \ |
| 619 | for (;;) { \ |
| 620 | prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \ |
| 621 | if (condition) \ |
| 622 | break; \ |
| 623 | if (!fatal_signal_pending(current)) { \ |
| 624 | schedule(); \ |
| 625 | continue; \ |
| 626 | } \ |
| 627 | ret = -ERESTARTSYS; \ |
| 628 | break; \ |
| 629 | } \ |
| 630 | finish_wait(&wq, &__wait); \ |
| 631 | } while (0) |
| 632 | |
| 633 | /** |
| 634 | * wait_event_killable - sleep until a condition gets true |
| 635 | * @wq: the waitqueue to wait on |
| 636 | * @condition: a C expression for the event to wait for |
| 637 | * |
| 638 | * The process is put to sleep (TASK_KILLABLE) until the |
| 639 | * @condition evaluates to true or a signal is received. |
| 640 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 641 | * |
| 642 | * wake_up() has to be called after changing any variable that could |
| 643 | * change the result of the wait condition. |
| 644 | * |
| 645 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 646 | * signal and 0 if @condition evaluated to true. |
| 647 | */ |
| 648 | #define wait_event_killable(wq, condition) \ |
| 649 | ({ \ |
| 650 | int __ret = 0; \ |
| 651 | if (!(condition)) \ |
| 652 | __wait_event_killable(wq, condition, __ret); \ |
| 653 | __ret; \ |
| 654 | }) |
| 655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | * These are the old interfaces to sleep waiting for an event. |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 658 | * They are racy. DO NOT use them, use the wait_event* interfaces above. |
| 659 | * We plan to remove these interfaces. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | */ |
Ingo Molnar | 0fec171 | 2007-07-09 18:52:01 +0200 | [diff] [blame] | 661 | extern void sleep_on(wait_queue_head_t *q); |
| 662 | extern long sleep_on_timeout(wait_queue_head_t *q, |
| 663 | signed long timeout); |
| 664 | extern void interruptible_sleep_on(wait_queue_head_t *q); |
| 665 | extern long interruptible_sleep_on_timeout(wait_queue_head_t *q, |
| 666 | signed long timeout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * Waitqueues which are removed from the waitqueue_head at wakeup time |
| 670 | */ |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 671 | void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state); |
| 672 | void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state); |
| 673 | void finish_wait(wait_queue_head_t *q, wait_queue_t *wait); |
Johannes Weiner | 777c6c5 | 2009-02-04 15:12:14 -0800 | [diff] [blame] | 674 | void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, |
| 675 | unsigned int mode, void *key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); |
| 677 | int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); |
| 678 | |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 679 | #define DEFINE_WAIT_FUNC(name, function) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | wait_queue_t name = { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 681 | .private = current, \ |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 682 | .func = function, \ |
blaisorblade@yahoo.it | 7e43c84 | 2005-05-25 01:31:42 +0200 | [diff] [blame] | 683 | .task_list = LIST_HEAD_INIT((name).task_list), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 686 | #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function) |
| 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | #define DEFINE_WAIT_BIT(name, word, bit) \ |
| 689 | struct wait_bit_queue name = { \ |
| 690 | .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \ |
| 691 | .wait = { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 692 | .private = current, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | .func = wake_bit_function, \ |
| 694 | .task_list = \ |
| 695 | LIST_HEAD_INIT((name).wait.task_list), \ |
| 696 | }, \ |
| 697 | } |
| 698 | |
| 699 | #define init_wait(wait) \ |
| 700 | do { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 701 | (wait)->private = current; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | (wait)->func = autoremove_wake_function; \ |
| 703 | INIT_LIST_HEAD(&(wait)->task_list); \ |
Evgeny Kuznetsov | 231d0ae | 2010-10-05 12:47:57 +0400 | [diff] [blame] | 704 | (wait)->flags = 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } while (0) |
| 706 | |
| 707 | /** |
| 708 | * wait_on_bit - wait for a bit to be cleared |
| 709 | * @word: the word being waited on, a kernel virtual address |
| 710 | * @bit: the bit of the word being waited on |
| 711 | * @action: the function used to sleep, which may take special actions |
| 712 | * @mode: the task state to sleep in |
| 713 | * |
| 714 | * There is a standard hashed waitqueue table for generic use. This |
| 715 | * is the part of the hashtable's accessor API that waits on a bit. |
| 716 | * For instance, if one were to have waiters on a bitflag, one would |
| 717 | * call wait_on_bit() in threads waiting for the bit to clear. |
| 718 | * One uses wait_on_bit() where one is waiting for the bit to clear, |
| 719 | * but has no intention of setting it. |
| 720 | */ |
| 721 | static inline int wait_on_bit(void *word, int bit, |
| 722 | int (*action)(void *), unsigned mode) |
| 723 | { |
| 724 | if (!test_bit(bit, word)) |
| 725 | return 0; |
| 726 | return out_of_line_wait_on_bit(word, bit, action, mode); |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it |
| 731 | * @word: the word being waited on, a kernel virtual address |
| 732 | * @bit: the bit of the word being waited on |
| 733 | * @action: the function used to sleep, which may take special actions |
| 734 | * @mode: the task state to sleep in |
| 735 | * |
| 736 | * There is a standard hashed waitqueue table for generic use. This |
| 737 | * is the part of the hashtable's accessor API that waits on a bit |
| 738 | * when one intends to set it, for instance, trying to lock bitflags. |
| 739 | * For instance, if one were to have waiters trying to set bitflag |
| 740 | * and waiting for it to clear before setting it, one would call |
| 741 | * wait_on_bit() in threads waiting to be able to set the bit. |
| 742 | * One uses wait_on_bit_lock() where one is waiting for the bit to |
| 743 | * clear with the intention of setting it, and when done, clearing it. |
| 744 | */ |
| 745 | static inline int wait_on_bit_lock(void *word, int bit, |
| 746 | int (*action)(void *), unsigned mode) |
| 747 | { |
| 748 | if (!test_and_set_bit(bit, word)) |
| 749 | return 0; |
| 750 | return out_of_line_wait_on_bit_lock(word, bit, action, mode); |
| 751 | } |
| 752 | |
| 753 | #endif /* __KERNEL__ */ |
| 754 | |
| 755 | #endif |