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