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 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; 29 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int sync, void *key); 30 int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); 31 32 struct __wait_queue { 33 unsigned int flags; 34 #define WQ_FLAG_EXCLUSIVE 0x01 35 void *private; 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 56 struct task_struct; 57 58 /* 59 * Macros for declaration and initialisaton of the datatypes 60 */ 61 62 #define __WAITQUEUE_INITIALIZER(name, tsk) { \ 63 .private = tsk, \ 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) { \ 71 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ 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 80 extern void init_waitqueue_head(wait_queue_head_t *q); 81 82 #ifdef CONFIG_LOCKDEP 83 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \ 84 ({ init_waitqueue_head(&name); name; }) 85 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \ 86 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) 87 #else 88 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name) 89 #endif 90 91 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p) 92 { 93 q->flags = 0; 94 q->private = p; 95 q->func = default_wake_function; 96 } 97 98 static inline void init_waitqueue_func_entry(wait_queue_t *q, 99 wait_queue_func_t func) 100 { 101 q->flags = 0; 102 q->private = NULL; 103 q->func = func; 104 } 105 106 static inline int waitqueue_active(wait_queue_head_t *q) 107 { 108 return !list_empty(&q->task_list); 109 } 110 111 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait); 112 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait); 113 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait); 114 115 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new) 116 { 117 list_add(&new->task_list, &head->task_list); 118 } 119 120 /* 121 * Used for wake-one threads: 122 */ 123 static inline void __add_wait_queue_tail(wait_queue_head_t *head, 124 wait_queue_t *new) 125 { 126 list_add_tail(&new->task_list, &head->task_list); 127 } 128 129 static inline void __remove_wait_queue(wait_queue_head_t *head, 130 wait_queue_t *old) 131 { 132 list_del(&old->task_list); 133 } 134 135 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key); 136 extern void __wake_up_locked(wait_queue_head_t *q, unsigned int mode); 137 extern void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr); 138 void __wake_up_bit(wait_queue_head_t *, void *, int); 139 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); 140 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned); 141 void wake_up_bit(void *, int); 142 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned); 143 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned); 144 wait_queue_head_t *bit_waitqueue(void *, int); 145 146 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) 147 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL) 148 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL) 149 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL) 150 151 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL) 152 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL) 153 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL) 154 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1) 155 156 #ifdef CONFIG_DEBUG_LOCK_ALLOC 157 /* 158 * macro to avoid include hell 159 */ 160 #define wake_up_nested(x, s) \ 161 do { \ 162 unsigned long flags; \ 163 \ 164 spin_lock_irqsave_nested(&(x)->lock, flags, (s)); \ 165 wake_up_locked(x); \ 166 spin_unlock_irqrestore(&(x)->lock, flags); \ 167 } while (0) 168 #else 169 #define wake_up_nested(x, s) wake_up(x) 170 #endif 171 172 #define __wait_event(wq, condition) \ 173 do { \ 174 DEFINE_WAIT(__wait); \ 175 \ 176 for (;;) { \ 177 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ 178 if (condition) \ 179 break; \ 180 schedule(); \ 181 } \ 182 finish_wait(&wq, &__wait); \ 183 } while (0) 184 185 /** 186 * wait_event - sleep until a condition gets true 187 * @wq: the waitqueue to wait on 188 * @condition: a C expression for the event to wait for 189 * 190 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the 191 * @condition evaluates to true. The @condition is checked each time 192 * the waitqueue @wq is woken up. 193 * 194 * wake_up() has to be called after changing any variable that could 195 * change the result of the wait condition. 196 */ 197 #define wait_event(wq, condition) \ 198 do { \ 199 if (condition) \ 200 break; \ 201 __wait_event(wq, condition); \ 202 } while (0) 203 204 #define __wait_event_timeout(wq, condition, ret) \ 205 do { \ 206 DEFINE_WAIT(__wait); \ 207 \ 208 for (;;) { \ 209 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ 210 if (condition) \ 211 break; \ 212 ret = schedule_timeout(ret); \ 213 if (!ret) \ 214 break; \ 215 } \ 216 finish_wait(&wq, &__wait); \ 217 } while (0) 218 219 /** 220 * wait_event_timeout - sleep until a condition gets true or a timeout elapses 221 * @wq: the waitqueue to wait on 222 * @condition: a C expression for the event to wait for 223 * @timeout: timeout, in jiffies 224 * 225 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the 226 * @condition evaluates to true. The @condition is checked each time 227 * the waitqueue @wq is woken up. 228 * 229 * wake_up() has to be called after changing any variable that could 230 * change the result of the wait condition. 231 * 232 * The function returns 0 if the @timeout elapsed, and the remaining 233 * jiffies if the condition evaluated to true before the timeout elapsed. 234 */ 235 #define wait_event_timeout(wq, condition, timeout) \ 236 ({ \ 237 long __ret = timeout; \ 238 if (!(condition)) \ 239 __wait_event_timeout(wq, condition, __ret); \ 240 __ret; \ 241 }) 242 243 #define __wait_event_interruptible(wq, condition, ret) \ 244 do { \ 245 DEFINE_WAIT(__wait); \ 246 \ 247 for (;;) { \ 248 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ 249 if (condition) \ 250 break; \ 251 if (!signal_pending(current)) { \ 252 schedule(); \ 253 continue; \ 254 } \ 255 ret = -ERESTARTSYS; \ 256 break; \ 257 } \ 258 finish_wait(&wq, &__wait); \ 259 } while (0) 260 261 /** 262 * wait_event_interruptible - sleep until a condition gets true 263 * @wq: the waitqueue to wait on 264 * @condition: a C expression for the event to wait for 265 * 266 * The process is put to sleep (TASK_INTERRUPTIBLE) until the 267 * @condition evaluates to true or a signal is received. 268 * The @condition is checked each time the waitqueue @wq is woken up. 269 * 270 * wake_up() has to be called after changing any variable that could 271 * change the result of the wait condition. 272 * 273 * The function will return -ERESTARTSYS if it was interrupted by a 274 * signal and 0 if @condition evaluated to true. 275 */ 276 #define wait_event_interruptible(wq, condition) \ 277 ({ \ 278 int __ret = 0; \ 279 if (!(condition)) \ 280 __wait_event_interruptible(wq, condition, __ret); \ 281 __ret; \ 282 }) 283 284 #define __wait_event_interruptible_timeout(wq, condition, ret) \ 285 do { \ 286 DEFINE_WAIT(__wait); \ 287 \ 288 for (;;) { \ 289 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \ 290 if (condition) \ 291 break; \ 292 if (!signal_pending(current)) { \ 293 ret = schedule_timeout(ret); \ 294 if (!ret) \ 295 break; \ 296 continue; \ 297 } \ 298 ret = -ERESTARTSYS; \ 299 break; \ 300 } \ 301 finish_wait(&wq, &__wait); \ 302 } while (0) 303 304 /** 305 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses 306 * @wq: the waitqueue to wait on 307 * @condition: a C expression for the event to wait for 308 * @timeout: timeout, in jiffies 309 * 310 * The process is put to sleep (TASK_INTERRUPTIBLE) until the 311 * @condition evaluates to true or a signal is received. 312 * The @condition is checked each time the waitqueue @wq is woken up. 313 * 314 * wake_up() has to be called after changing any variable that could 315 * change the result of the wait condition. 316 * 317 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it 318 * was interrupted by a signal, and the remaining jiffies otherwise 319 * if the condition evaluated to true before the timeout elapsed. 320 */ 321 #define wait_event_interruptible_timeout(wq, condition, timeout) \ 322 ({ \ 323 long __ret = timeout; \ 324 if (!(condition)) \ 325 __wait_event_interruptible_timeout(wq, condition, __ret); \ 326 __ret; \ 327 }) 328 329 #define __wait_event_interruptible_exclusive(wq, condition, ret) \ 330 do { \ 331 DEFINE_WAIT(__wait); \ 332 \ 333 for (;;) { \ 334 prepare_to_wait_exclusive(&wq, &__wait, \ 335 TASK_INTERRUPTIBLE); \ 336 if (condition) \ 337 break; \ 338 if (!signal_pending(current)) { \ 339 schedule(); \ 340 continue; \ 341 } \ 342 ret = -ERESTARTSYS; \ 343 break; \ 344 } \ 345 finish_wait(&wq, &__wait); \ 346 } while (0) 347 348 #define wait_event_interruptible_exclusive(wq, condition) \ 349 ({ \ 350 int __ret = 0; \ 351 if (!(condition)) \ 352 __wait_event_interruptible_exclusive(wq, condition, __ret);\ 353 __ret; \ 354 }) 355 356 #define __wait_event_killable(wq, condition, ret) \ 357 do { \ 358 DEFINE_WAIT(__wait); \ 359 \ 360 for (;;) { \ 361 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \ 362 if (condition) \ 363 break; \ 364 if (!fatal_signal_pending(current)) { \ 365 schedule(); \ 366 continue; \ 367 } \ 368 ret = -ERESTARTSYS; \ 369 break; \ 370 } \ 371 finish_wait(&wq, &__wait); \ 372 } while (0) 373 374 /** 375 * wait_event_killable - sleep until a condition gets true 376 * @wq: the waitqueue to wait on 377 * @condition: a C expression for the event to wait for 378 * 379 * The process is put to sleep (TASK_KILLABLE) until the 380 * @condition evaluates to true or a signal is received. 381 * The @condition is checked each time the waitqueue @wq is woken up. 382 * 383 * wake_up() has to be called after changing any variable that could 384 * change the result of the wait condition. 385 * 386 * The function will return -ERESTARTSYS if it was interrupted by a 387 * signal and 0 if @condition evaluated to true. 388 */ 389 #define wait_event_killable(wq, condition) \ 390 ({ \ 391 int __ret = 0; \ 392 if (!(condition)) \ 393 __wait_event_killable(wq, condition, __ret); \ 394 __ret; \ 395 }) 396 397 /* 398 * Must be called with the spinlock in the wait_queue_head_t held. 399 */ 400 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q, 401 wait_queue_t * wait) 402 { 403 wait->flags |= WQ_FLAG_EXCLUSIVE; 404 __add_wait_queue_tail(q, wait); 405 } 406 407 /* 408 * Must be called with the spinlock in the wait_queue_head_t held. 409 */ 410 static inline void remove_wait_queue_locked(wait_queue_head_t *q, 411 wait_queue_t * wait) 412 { 413 __remove_wait_queue(q, wait); 414 } 415 416 /* 417 * These are the old interfaces to sleep waiting for an event. 418 * They are racy. DO NOT use them, use the wait_event* interfaces above. 419 * We plan to remove these interfaces. 420 */ 421 extern void sleep_on(wait_queue_head_t *q); 422 extern long sleep_on_timeout(wait_queue_head_t *q, 423 signed long timeout); 424 extern void interruptible_sleep_on(wait_queue_head_t *q); 425 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q, 426 signed long timeout); 427 428 /* 429 * Waitqueues which are removed from the waitqueue_head at wakeup time 430 */ 431 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state); 432 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state); 433 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait); 434 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); 435 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); 436 437 #define DEFINE_WAIT(name) \ 438 wait_queue_t name = { \ 439 .private = current, \ 440 .func = autoremove_wake_function, \ 441 .task_list = LIST_HEAD_INIT((name).task_list), \ 442 } 443 444 #define DEFINE_WAIT_BIT(name, word, bit) \ 445 struct wait_bit_queue name = { \ 446 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \ 447 .wait = { \ 448 .private = current, \ 449 .func = wake_bit_function, \ 450 .task_list = \ 451 LIST_HEAD_INIT((name).wait.task_list), \ 452 }, \ 453 } 454 455 #define init_wait(wait) \ 456 do { \ 457 (wait)->private = current; \ 458 (wait)->func = autoremove_wake_function; \ 459 INIT_LIST_HEAD(&(wait)->task_list); \ 460 } while (0) 461 462 /** 463 * wait_on_bit - wait for a bit to be cleared 464 * @word: the word being waited on, a kernel virtual address 465 * @bit: the bit of the word being waited on 466 * @action: the function used to sleep, which may take special actions 467 * @mode: the task state to sleep in 468 * 469 * There is a standard hashed waitqueue table for generic use. This 470 * is the part of the hashtable's accessor API that waits on a bit. 471 * For instance, if one were to have waiters on a bitflag, one would 472 * call wait_on_bit() in threads waiting for the bit to clear. 473 * One uses wait_on_bit() where one is waiting for the bit to clear, 474 * but has no intention of setting it. 475 */ 476 static inline int wait_on_bit(void *word, int bit, 477 int (*action)(void *), unsigned mode) 478 { 479 if (!test_bit(bit, word)) 480 return 0; 481 return out_of_line_wait_on_bit(word, bit, action, mode); 482 } 483 484 /** 485 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it 486 * @word: the word being waited on, a kernel virtual address 487 * @bit: the bit of the word being waited on 488 * @action: the function used to sleep, which may take special actions 489 * @mode: the task state to sleep in 490 * 491 * There is a standard hashed waitqueue table for generic use. This 492 * is the part of the hashtable's accessor API that waits on a bit 493 * when one intends to set it, for instance, trying to lock bitflags. 494 * For instance, if one were to have waiters trying to set bitflag 495 * and waiting for it to clear before setting it, one would call 496 * wait_on_bit() in threads waiting to be able to set the bit. 497 * One uses wait_on_bit_lock() where one is waiting for the bit to 498 * clear with the intention of setting it, and when done, clearing it. 499 */ 500 static inline int wait_on_bit_lock(void *word, int bit, 501 int (*action)(void *), unsigned mode) 502 { 503 if (!test_and_set_bit(bit, word)) 504 return 0; 505 return out_of_line_wait_on_bit_lock(word, bit, action, mode); 506 } 507 508 #endif /* __KERNEL__ */ 509 510 #endif 511