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 sync, void *key);
30 int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, 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);
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)
88 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
91 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
95 q->func = default_wake_function;
98 static inline void init_waitqueue_func_entry(wait_queue_t *q,
99 wait_queue_func_t func)
106 static inline int waitqueue_active(wait_queue_head_t *q)
108 return !list_empty(&q->task_list);
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);
115 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
117 list_add(&new->task_list, &head->task_list);
121 * Used for wake-one threads:
123 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
126 list_add_tail(&new->task_list, &head->task_list);
129 static inline void __remove_wait_queue(wait_queue_head_t *head,
132 list_del(&old->task_list);
135 void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
136 int nr_exclusive, int sync, void *key);
137 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
138 extern void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
139 extern void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
140 void __wake_up_bit(wait_queue_head_t *, void *, int);
141 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
142 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
143 void wake_up_bit(void *, int);
144 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
145 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
146 wait_queue_head_t *bit_waitqueue(void *, int);
148 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
149 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
150 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
151 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
153 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
154 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
155 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
156 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
158 #ifdef CONFIG_DEBUG_LOCK_ALLOC
160 * macro to avoid include hell
162 #define wake_up_nested(x, s) \
164 unsigned long flags; \
166 spin_lock_irqsave_nested(&(x)->lock, flags, (s)); \
168 spin_unlock_irqrestore(&(x)->lock, flags); \
171 #define wake_up_nested(x, s) wake_up(x)
174 #define __wait_event(wq, condition) \
176 DEFINE_WAIT(__wait); \
179 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
184 finish_wait(&wq, &__wait); \
188 * wait_event - sleep until a condition gets true
189 * @wq: the waitqueue to wait on
190 * @condition: a C expression for the event to wait for
192 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
193 * @condition evaluates to true. The @condition is checked each time
194 * the waitqueue @wq is woken up.
196 * wake_up() has to be called after changing any variable that could
197 * change the result of the wait condition.
199 #define wait_event(wq, condition) \
203 __wait_event(wq, condition); \
206 #define __wait_event_timeout(wq, condition, ret) \
208 DEFINE_WAIT(__wait); \
211 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
214 ret = schedule_timeout(ret); \
218 finish_wait(&wq, &__wait); \
222 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
223 * @wq: the waitqueue to wait on
224 * @condition: a C expression for the event to wait for
225 * @timeout: timeout, in jiffies
227 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
228 * @condition evaluates to true. The @condition is checked each time
229 * the waitqueue @wq is woken up.
231 * wake_up() has to be called after changing any variable that could
232 * change the result of the wait condition.
234 * The function returns 0 if the @timeout elapsed, and the remaining
235 * jiffies if the condition evaluated to true before the timeout elapsed.
237 #define wait_event_timeout(wq, condition, timeout) \
239 long __ret = timeout; \
241 __wait_event_timeout(wq, condition, __ret); \
245 #define __wait_event_interruptible(wq, condition, ret) \
247 DEFINE_WAIT(__wait); \
250 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
253 if (!signal_pending(current)) { \
257 ret = -ERESTARTSYS; \
260 finish_wait(&wq, &__wait); \
264 * wait_event_interruptible - sleep until a condition gets true
265 * @wq: the waitqueue to wait on
266 * @condition: a C expression for the event to wait for
268 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
269 * @condition evaluates to true or a signal is received.
270 * The @condition is checked each time the waitqueue @wq is woken up.
272 * wake_up() has to be called after changing any variable that could
273 * change the result of the wait condition.
275 * The function will return -ERESTARTSYS if it was interrupted by a
276 * signal and 0 if @condition evaluated to true.
278 #define wait_event_interruptible(wq, condition) \
282 __wait_event_interruptible(wq, condition, __ret); \
286 #define __wait_event_interruptible_timeout(wq, condition, ret) \
288 DEFINE_WAIT(__wait); \
291 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
294 if (!signal_pending(current)) { \
295 ret = schedule_timeout(ret); \
300 ret = -ERESTARTSYS; \
303 finish_wait(&wq, &__wait); \
307 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
308 * @wq: the waitqueue to wait on
309 * @condition: a C expression for the event to wait for
310 * @timeout: timeout, in jiffies
312 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
313 * @condition evaluates to true or a signal is received.
314 * The @condition is checked each time the waitqueue @wq is woken up.
316 * wake_up() has to be called after changing any variable that could
317 * change the result of the wait condition.
319 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
320 * was interrupted by a signal, and the remaining jiffies otherwise
321 * if the condition evaluated to true before the timeout elapsed.
323 #define wait_event_interruptible_timeout(wq, condition, timeout) \
325 long __ret = timeout; \
327 __wait_event_interruptible_timeout(wq, condition, __ret); \
331 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
333 DEFINE_WAIT(__wait); \
336 prepare_to_wait_exclusive(&wq, &__wait, \
337 TASK_INTERRUPTIBLE); \
339 finish_wait(&wq, &__wait); \
342 if (!signal_pending(current)) { \
346 ret = -ERESTARTSYS; \
347 abort_exclusive_wait(&wq, &__wait, \
348 TASK_INTERRUPTIBLE, NULL); \
353 #define wait_event_interruptible_exclusive(wq, condition) \
357 __wait_event_interruptible_exclusive(wq, condition, __ret);\
361 #define __wait_event_killable(wq, condition, ret) \
363 DEFINE_WAIT(__wait); \
366 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
369 if (!fatal_signal_pending(current)) { \
373 ret = -ERESTARTSYS; \
376 finish_wait(&wq, &__wait); \
380 * wait_event_killable - sleep until a condition gets true
381 * @wq: the waitqueue to wait on
382 * @condition: a C expression for the event to wait for
384 * The process is put to sleep (TASK_KILLABLE) until the
385 * @condition evaluates to true or a signal is received.
386 * The @condition is checked each time the waitqueue @wq is woken up.
388 * wake_up() has to be called after changing any variable that could
389 * change the result of the wait condition.
391 * The function will return -ERESTARTSYS if it was interrupted by a
392 * signal and 0 if @condition evaluated to true.
394 #define wait_event_killable(wq, condition) \
398 __wait_event_killable(wq, condition, __ret); \
403 * Must be called with the spinlock in the wait_queue_head_t held.
405 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
408 wait->flags |= WQ_FLAG_EXCLUSIVE;
409 __add_wait_queue_tail(q, wait);
413 * Must be called with the spinlock in the wait_queue_head_t held.
415 static inline void remove_wait_queue_locked(wait_queue_head_t *q,
418 __remove_wait_queue(q, wait);
422 * These are the old interfaces to sleep waiting for an event.
423 * They are racy. DO NOT use them, use the wait_event* interfaces above.
424 * We plan to remove these interfaces.
426 extern void sleep_on(wait_queue_head_t *q);
427 extern long sleep_on_timeout(wait_queue_head_t *q,
428 signed long timeout);
429 extern void interruptible_sleep_on(wait_queue_head_t *q);
430 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
431 signed long timeout);
434 * Waitqueues which are removed from the waitqueue_head at wakeup time
436 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
437 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
438 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
439 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
440 unsigned int mode, void *key);
441 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
442 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
444 #define DEFINE_WAIT(name) \
445 wait_queue_t name = { \
446 .private = current, \
447 .func = autoremove_wake_function, \
448 .task_list = LIST_HEAD_INIT((name).task_list), \
451 #define DEFINE_WAIT_BIT(name, word, bit) \
452 struct wait_bit_queue name = { \
453 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
455 .private = current, \
456 .func = wake_bit_function, \
458 LIST_HEAD_INIT((name).wait.task_list), \
462 #define init_wait(wait) \
464 (wait)->private = current; \
465 (wait)->func = autoremove_wake_function; \
466 INIT_LIST_HEAD(&(wait)->task_list); \
470 * wait_on_bit - wait for a bit to be cleared
471 * @word: the word being waited on, a kernel virtual address
472 * @bit: the bit of the word being waited on
473 * @action: the function used to sleep, which may take special actions
474 * @mode: the task state to sleep in
476 * There is a standard hashed waitqueue table for generic use. This
477 * is the part of the hashtable's accessor API that waits on a bit.
478 * For instance, if one were to have waiters on a bitflag, one would
479 * call wait_on_bit() in threads waiting for the bit to clear.
480 * One uses wait_on_bit() where one is waiting for the bit to clear,
481 * but has no intention of setting it.
483 static inline int wait_on_bit(void *word, int bit,
484 int (*action)(void *), unsigned mode)
486 if (!test_bit(bit, word))
488 return out_of_line_wait_on_bit(word, bit, action, mode);
492 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
493 * @word: the word being waited on, a kernel virtual address
494 * @bit: the bit of the word being waited on
495 * @action: the function used to sleep, which may take special actions
496 * @mode: the task state to sleep in
498 * There is a standard hashed waitqueue table for generic use. This
499 * is the part of the hashtable's accessor API that waits on a bit
500 * when one intends to set it, for instance, trying to lock bitflags.
501 * For instance, if one were to have waiters trying to set bitflag
502 * and waiting for it to clear before setting it, one would call
503 * wait_on_bit() in threads waiting to be able to set the bit.
504 * One uses wait_on_bit_lock() where one is waiting for the bit to
505 * clear with the intention of setting it, and when done, clearing it.
507 static inline int wait_on_bit_lock(void *word, int bit,
508 int (*action)(void *), unsigned mode)
510 if (!test_and_set_bit(bit, word))
512 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
515 #endif /* __KERNEL__ */