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. */
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 */
15 /* First argument to waitid: */
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>
28 typedef struct __wait_queue wait_queue_t;
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);
34 #define WQ_FLAG_EXCLUSIVE 0x01
36 wait_queue_func_t func;
37 struct list_head task_list;
45 struct wait_bit_queue {
46 struct wait_bit_key key;
50 struct __wait_queue_head {
52 struct list_head task_list;
54 typedef struct __wait_queue_head wait_queue_head_t;
59 * Macros for declaration and initialisaton of the datatypes
62 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
64 .func = default_wake_function, \
65 .task_list = { NULL, NULL } }
67 #define DECLARE_WAITQUEUE(name, tsk) \
68 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
70 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
71 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
72 .task_list = { &(name).task_list, &(name).task_list } }
74 #define DECLARE_WAIT_QUEUE_HEAD(name) \
75 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
77 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
78 { .flags = word, .bit_nr = bit, }
80 extern void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *);
82 #define init_waitqueue_head(q) \
84 static struct lock_class_key __key; \
86 __init_waitqueue_head((q), &__key); \
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)
95 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
98 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
102 q->func = default_wake_function;
105 static inline void init_waitqueue_func_entry(wait_queue_t *q,
106 wait_queue_func_t func)
113 static inline int waitqueue_active(wait_queue_head_t *q)
115 return !list_empty(&q->task_list);
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);
122 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
124 list_add(&new->task_list, &head->task_list);
128 * Used for wake-one threads:
130 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
133 wait->flags |= WQ_FLAG_EXCLUSIVE;
134 __add_wait_queue(q, wait);
137 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
140 list_add_tail(&new->task_list, &head->task_list);
143 static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q,
146 wait->flags |= WQ_FLAG_EXCLUSIVE;
147 __add_wait_queue_tail(q, wait);
150 static inline void __remove_wait_queue(wait_queue_head_t *head,
153 list_del(&old->task_list);
156 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
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,
160 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
161 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
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);
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)
173 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
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)
178 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
181 * Wakeup macros to be used to report events to the targets.
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))
192 #define __wait_event(wq, condition) \
194 DEFINE_WAIT(__wait); \
197 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
202 finish_wait(&wq, &__wait); \
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
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.
214 * wake_up() has to be called after changing any variable that could
215 * change the result of the wait condition.
217 #define wait_event(wq, condition) \
221 __wait_event(wq, condition); \
224 #define __wait_event_timeout(wq, condition, ret) \
226 DEFINE_WAIT(__wait); \
229 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
232 ret = schedule_timeout(ret); \
236 if (!ret && (condition)) \
238 finish_wait(&wq, &__wait); \
242 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
243 * @wq: the waitqueue to wait on
244 * @condition: a C expression for the event to wait for
245 * @timeout: timeout, in jiffies
247 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
248 * @condition evaluates to true. The @condition is checked each time
249 * the waitqueue @wq is woken up.
251 * wake_up() has to be called after changing any variable that could
252 * change the result of the wait condition.
254 * The function returns 0 if the @timeout elapsed, or the remaining
255 * jiffies (at least 1) if the @condition evaluated to %true before
256 * the @timeout elapsed.
258 #define wait_event_timeout(wq, condition, timeout) \
260 long __ret = timeout; \
262 __wait_event_timeout(wq, condition, __ret); \
266 #define __wait_event_interruptible(wq, condition, ret) \
268 DEFINE_WAIT(__wait); \
271 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
274 if (!signal_pending(current)) { \
278 ret = -ERESTARTSYS; \
281 finish_wait(&wq, &__wait); \
285 * wait_event_interruptible - sleep until a condition gets true
286 * @wq: the waitqueue to wait on
287 * @condition: a C expression for the event to wait for
289 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
290 * @condition evaluates to true or a signal is received.
291 * The @condition is checked each time the waitqueue @wq is woken up.
293 * wake_up() has to be called after changing any variable that could
294 * change the result of the wait condition.
296 * The function will return -ERESTARTSYS if it was interrupted by a
297 * signal and 0 if @condition evaluated to true.
299 #define wait_event_interruptible(wq, condition) \
303 __wait_event_interruptible(wq, condition, __ret); \
307 #define __wait_event_interruptible_timeout(wq, condition, ret) \
309 DEFINE_WAIT(__wait); \
312 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
315 if (!signal_pending(current)) { \
316 ret = schedule_timeout(ret); \
321 ret = -ERESTARTSYS; \
324 if (!ret && (condition)) \
326 finish_wait(&wq, &__wait); \
330 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
331 * @wq: the waitqueue to wait on
332 * @condition: a C expression for the event to wait for
333 * @timeout: timeout, in jiffies
335 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
336 * @condition evaluates to true or a signal is received.
337 * The @condition is checked each time the waitqueue @wq is woken up.
339 * wake_up() has to be called after changing any variable that could
340 * change the result of the wait condition.
343 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
344 * a signal, or the remaining jiffies (at least 1) if the @condition
345 * evaluated to %true before the @timeout elapsed.
347 #define wait_event_interruptible_timeout(wq, condition, timeout) \
349 long __ret = timeout; \
351 __wait_event_interruptible_timeout(wq, condition, __ret); \
355 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
357 DEFINE_WAIT(__wait); \
360 prepare_to_wait_exclusive(&wq, &__wait, \
361 TASK_INTERRUPTIBLE); \
363 finish_wait(&wq, &__wait); \
366 if (!signal_pending(current)) { \
370 ret = -ERESTARTSYS; \
371 abort_exclusive_wait(&wq, &__wait, \
372 TASK_INTERRUPTIBLE, NULL); \
377 #define wait_event_interruptible_exclusive(wq, condition) \
381 __wait_event_interruptible_exclusive(wq, condition, __ret);\
386 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
389 DEFINE_WAIT(__wait); \
391 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
393 if (likely(list_empty(&__wait.task_list))) \
394 __add_wait_queue_tail(&(wq), &__wait); \
395 set_current_state(TASK_INTERRUPTIBLE); \
396 if (signal_pending(current)) { \
397 __ret = -ERESTARTSYS; \
401 spin_unlock_irq(&(wq).lock); \
403 spin_unlock(&(wq).lock); \
406 spin_lock_irq(&(wq).lock); \
408 spin_lock(&(wq).lock); \
409 } while (!(condition)); \
410 __remove_wait_queue(&(wq), &__wait); \
411 __set_current_state(TASK_RUNNING); \
417 * wait_event_interruptible_locked - sleep until a condition gets true
418 * @wq: the waitqueue to wait on
419 * @condition: a C expression for the event to wait for
421 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
422 * @condition evaluates to true or a signal is received.
423 * The @condition is checked each time the waitqueue @wq is woken up.
425 * It must be called with wq.lock being held. This spinlock is
426 * unlocked while sleeping but @condition testing is done while lock
427 * is held and when this macro exits the lock is held.
429 * The lock is locked/unlocked using spin_lock()/spin_unlock()
430 * functions which must match the way they are locked/unlocked outside
433 * wake_up_locked() has to be called after changing any variable that could
434 * change the result of the wait condition.
436 * The function will return -ERESTARTSYS if it was interrupted by a
437 * signal and 0 if @condition evaluated to true.
439 #define wait_event_interruptible_locked(wq, condition) \
441 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
444 * wait_event_interruptible_locked_irq - sleep until a condition gets true
445 * @wq: the waitqueue to wait on
446 * @condition: a C expression for the event to wait for
448 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
449 * @condition evaluates to true or a signal is received.
450 * The @condition is checked each time the waitqueue @wq is woken up.
452 * It must be called with wq.lock being held. This spinlock is
453 * unlocked while sleeping but @condition testing is done while lock
454 * is held and when this macro exits the lock is held.
456 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
457 * functions which must match the way they are locked/unlocked outside
460 * wake_up_locked() has to be called after changing any variable that could
461 * change the result of the wait condition.
463 * The function will return -ERESTARTSYS if it was interrupted by a
464 * signal and 0 if @condition evaluated to true.
466 #define wait_event_interruptible_locked_irq(wq, condition) \
468 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
471 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
472 * @wq: the waitqueue to wait on
473 * @condition: a C expression for the event to wait for
475 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
476 * @condition evaluates to true or a signal is received.
477 * The @condition is checked each time the waitqueue @wq is woken up.
479 * It must be called with wq.lock being held. This spinlock is
480 * unlocked while sleeping but @condition testing is done while lock
481 * is held and when this macro exits the lock is held.
483 * The lock is locked/unlocked using spin_lock()/spin_unlock()
484 * functions which must match the way they are locked/unlocked outside
487 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
488 * set thus when other process waits process on the list if this
489 * process is awaken further processes are not considered.
491 * wake_up_locked() has to be called after changing any variable that could
492 * change the result of the wait condition.
494 * The function will return -ERESTARTSYS if it was interrupted by a
495 * signal and 0 if @condition evaluated to true.
497 #define wait_event_interruptible_exclusive_locked(wq, condition) \
499 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
502 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
503 * @wq: the waitqueue to wait on
504 * @condition: a C expression for the event to wait for
506 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
507 * @condition evaluates to true or a signal is received.
508 * The @condition is checked each time the waitqueue @wq is woken up.
510 * It must be called with wq.lock being held. This spinlock is
511 * unlocked while sleeping but @condition testing is done while lock
512 * is held and when this macro exits the lock is held.
514 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
515 * functions which must match the way they are locked/unlocked outside
518 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
519 * set thus when other process waits process on the list if this
520 * process is awaken further processes are not considered.
522 * wake_up_locked() has to be called after changing any variable that could
523 * change the result of the wait condition.
525 * The function will return -ERESTARTSYS if it was interrupted by a
526 * signal and 0 if @condition evaluated to true.
528 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
530 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
534 #define __wait_event_killable(wq, condition, ret) \
536 DEFINE_WAIT(__wait); \
539 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
542 if (!fatal_signal_pending(current)) { \
546 ret = -ERESTARTSYS; \
549 finish_wait(&wq, &__wait); \
553 * wait_event_killable - sleep until a condition gets true
554 * @wq: the waitqueue to wait on
555 * @condition: a C expression for the event to wait for
557 * The process is put to sleep (TASK_KILLABLE) until the
558 * @condition evaluates to true or a signal is received.
559 * The @condition is checked each time the waitqueue @wq is woken up.
561 * wake_up() has to be called after changing any variable that could
562 * change the result of the wait condition.
564 * The function will return -ERESTARTSYS if it was interrupted by a
565 * signal and 0 if @condition evaluated to true.
567 #define wait_event_killable(wq, condition) \
571 __wait_event_killable(wq, condition, __ret); \
576 * These are the old interfaces to sleep waiting for an event.
577 * They are racy. DO NOT use them, use the wait_event* interfaces above.
578 * We plan to remove these interfaces.
580 extern void sleep_on(wait_queue_head_t *q);
581 extern long sleep_on_timeout(wait_queue_head_t *q,
582 signed long timeout);
583 extern void interruptible_sleep_on(wait_queue_head_t *q);
584 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
585 signed long timeout);
588 * Waitqueues which are removed from the waitqueue_head at wakeup time
590 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
591 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
592 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
593 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
594 unsigned int mode, void *key);
595 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
596 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
598 #define DEFINE_WAIT_FUNC(name, function) \
599 wait_queue_t name = { \
600 .private = current, \
602 .task_list = LIST_HEAD_INIT((name).task_list), \
605 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
607 #define DEFINE_WAIT_BIT(name, word, bit) \
608 struct wait_bit_queue name = { \
609 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
611 .private = current, \
612 .func = wake_bit_function, \
614 LIST_HEAD_INIT((name).wait.task_list), \
618 #define init_wait(wait) \
620 (wait)->private = current; \
621 (wait)->func = autoremove_wake_function; \
622 INIT_LIST_HEAD(&(wait)->task_list); \
627 * wait_on_bit - wait for a bit to be cleared
628 * @word: the word being waited on, a kernel virtual address
629 * @bit: the bit of the word being waited on
630 * @action: the function used to sleep, which may take special actions
631 * @mode: the task state to sleep in
633 * There is a standard hashed waitqueue table for generic use. This
634 * is the part of the hashtable's accessor API that waits on a bit.
635 * For instance, if one were to have waiters on a bitflag, one would
636 * call wait_on_bit() in threads waiting for the bit to clear.
637 * One uses wait_on_bit() where one is waiting for the bit to clear,
638 * but has no intention of setting it.
640 static inline int wait_on_bit(void *word, int bit,
641 int (*action)(void *), unsigned mode)
643 if (!test_bit(bit, word))
645 return out_of_line_wait_on_bit(word, bit, action, mode);
649 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
650 * @word: the word being waited on, a kernel virtual address
651 * @bit: the bit of the word being waited on
652 * @action: the function used to sleep, which may take special actions
653 * @mode: the task state to sleep in
655 * There is a standard hashed waitqueue table for generic use. This
656 * is the part of the hashtable's accessor API that waits on a bit
657 * when one intends to set it, for instance, trying to lock bitflags.
658 * For instance, if one were to have waiters trying to set bitflag
659 * and waiting for it to clear before setting it, one would call
660 * wait_on_bit() in threads waiting to be able to set the bit.
661 * One uses wait_on_bit_lock() where one is waiting for the bit to
662 * clear with the intention of setting it, and when done, clearing it.
664 static inline int wait_on_bit_lock(void *word, int bit,
665 int (*action)(void *), unsigned mode)
667 if (!test_and_set_bit(bit, word))
669 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
672 #endif /* __KERNEL__ */