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