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);
112 * Used to distinguish between sync and async io wait context:
113 * sync i/o typically specifies a NULL wait queue entry or a wait
114 * queue entry bound to a task (current task) to wake up.
115 * aio specifies a wait queue entry with an async notification
116 * callback routine, not associated with any task.
118 #define is_sync_wait(wait) (!(wait) || ((wait)->private))
120 extern void FASTCALL(add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
121 extern void FASTCALL(add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait));
122 extern void FASTCALL(remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
124 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
126 list_add(&new->task_list, &head->task_list);
130 * Used for wake-one threads:
132 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
135 list_add_tail(&new->task_list, &head->task_list);
138 static inline void __remove_wait_queue(wait_queue_head_t *head,
141 list_del(&old->task_list);
144 void FASTCALL(__wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key));
145 extern void FASTCALL(__wake_up_locked(wait_queue_head_t *q, unsigned int mode));
146 extern void FASTCALL(__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr));
147 void FASTCALL(__wake_up_bit(wait_queue_head_t *, void *, int));
148 int FASTCALL(__wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
149 int FASTCALL(__wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
150 void FASTCALL(wake_up_bit(void *, int));
151 int FASTCALL(out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned));
152 int FASTCALL(out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned));
153 wait_queue_head_t *FASTCALL(bit_waitqueue(void *, int));
155 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
156 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
157 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
158 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
160 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
161 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
162 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
163 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
165 #ifdef CONFIG_DEBUG_LOCK_ALLOC
167 * macro to avoid include hell
169 #define wake_up_nested(x, s) \
171 unsigned long flags; \
173 spin_lock_irqsave_nested(&(x)->lock, flags, (s)); \
175 spin_unlock_irqrestore(&(x)->lock, flags); \
178 #define wake_up_nested(x, s) wake_up(x)
181 #define __wait_event(wq, condition) \
183 DEFINE_WAIT(__wait); \
186 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
191 finish_wait(&wq, &__wait); \
195 * wait_event - sleep until a condition gets true
196 * @wq: the waitqueue to wait on
197 * @condition: a C expression for the event to wait for
199 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
200 * @condition evaluates to true. The @condition is checked each time
201 * the waitqueue @wq is woken up.
203 * wake_up() has to be called after changing any variable that could
204 * change the result of the wait condition.
206 #define wait_event(wq, condition) \
210 __wait_event(wq, condition); \
213 #define __wait_event_timeout(wq, condition, ret) \
215 DEFINE_WAIT(__wait); \
218 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
221 ret = schedule_timeout(ret); \
225 finish_wait(&wq, &__wait); \
229 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
230 * @wq: the waitqueue to wait on
231 * @condition: a C expression for the event to wait for
232 * @timeout: timeout, in jiffies
234 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
235 * @condition evaluates to true. The @condition is checked each time
236 * the waitqueue @wq is woken up.
238 * wake_up() has to be called after changing any variable that could
239 * change the result of the wait condition.
241 * The function returns 0 if the @timeout elapsed, and the remaining
242 * jiffies if the condition evaluated to true before the timeout elapsed.
244 #define wait_event_timeout(wq, condition, timeout) \
246 long __ret = timeout; \
248 __wait_event_timeout(wq, condition, __ret); \
252 #define __wait_event_interruptible(wq, condition, ret) \
254 DEFINE_WAIT(__wait); \
257 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
260 if (!signal_pending(current)) { \
264 ret = -ERESTARTSYS; \
267 finish_wait(&wq, &__wait); \
271 * wait_event_interruptible - sleep until a condition gets true
272 * @wq: the waitqueue to wait on
273 * @condition: a C expression for the event to wait for
275 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
276 * @condition evaluates to true or a signal is received.
277 * The @condition is checked each time the waitqueue @wq is woken up.
279 * wake_up() has to be called after changing any variable that could
280 * change the result of the wait condition.
282 * The function will return -ERESTARTSYS if it was interrupted by a
283 * signal and 0 if @condition evaluated to true.
285 #define wait_event_interruptible(wq, condition) \
289 __wait_event_interruptible(wq, condition, __ret); \
293 #define __wait_event_interruptible_timeout(wq, condition, ret) \
295 DEFINE_WAIT(__wait); \
298 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
301 if (!signal_pending(current)) { \
302 ret = schedule_timeout(ret); \
307 ret = -ERESTARTSYS; \
310 finish_wait(&wq, &__wait); \
314 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
315 * @wq: the waitqueue to wait on
316 * @condition: a C expression for the event to wait for
317 * @timeout: timeout, in jiffies
319 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
320 * @condition evaluates to true or a signal is received.
321 * The @condition is checked each time the waitqueue @wq is woken up.
323 * wake_up() has to be called after changing any variable that could
324 * change the result of the wait condition.
326 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
327 * was interrupted by a signal, and the remaining jiffies otherwise
328 * if the condition evaluated to true before the timeout elapsed.
330 #define wait_event_interruptible_timeout(wq, condition, timeout) \
332 long __ret = timeout; \
334 __wait_event_interruptible_timeout(wq, condition, __ret); \
338 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
340 DEFINE_WAIT(__wait); \
343 prepare_to_wait_exclusive(&wq, &__wait, \
344 TASK_INTERRUPTIBLE); \
347 if (!signal_pending(current)) { \
351 ret = -ERESTARTSYS; \
354 finish_wait(&wq, &__wait); \
357 #define wait_event_interruptible_exclusive(wq, condition) \
361 __wait_event_interruptible_exclusive(wq, condition, __ret);\
365 #define __wait_event_killable(wq, condition, ret) \
367 DEFINE_WAIT(__wait); \
370 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
373 if (!fatal_signal_pending(current)) { \
377 ret = -ERESTARTSYS; \
380 finish_wait(&wq, &__wait); \
384 * wait_event_killable - sleep until a condition gets true
385 * @wq: the waitqueue to wait on
386 * @condition: a C expression for the event to wait for
388 * The process is put to sleep (TASK_KILLABLE) until the
389 * @condition evaluates to true or a signal is received.
390 * The @condition is checked each time the waitqueue @wq is woken up.
392 * wake_up() has to be called after changing any variable that could
393 * change the result of the wait condition.
395 * The function will return -ERESTARTSYS if it was interrupted by a
396 * signal and 0 if @condition evaluated to true.
398 #define wait_event_killable(wq, condition) \
402 __wait_event_killable(wq, condition, __ret); \
407 * Must be called with the spinlock in the wait_queue_head_t held.
409 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
412 wait->flags |= WQ_FLAG_EXCLUSIVE;
413 __add_wait_queue_tail(q, wait);
417 * Must be called with the spinlock in the wait_queue_head_t held.
419 static inline void remove_wait_queue_locked(wait_queue_head_t *q,
422 __remove_wait_queue(q, wait);
426 * These are the old interfaces to sleep waiting for an event.
427 * They are racy. DO NOT use them, use the wait_event* interfaces above.
428 * We plan to remove these interfaces.
430 extern void sleep_on(wait_queue_head_t *q);
431 extern long sleep_on_timeout(wait_queue_head_t *q,
432 signed long timeout);
433 extern void interruptible_sleep_on(wait_queue_head_t *q);
434 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
435 signed long timeout);
438 * Waitqueues which are removed from the waitqueue_head at wakeup time
440 void FASTCALL(prepare_to_wait(wait_queue_head_t *q,
441 wait_queue_t *wait, int state));
442 void FASTCALL(prepare_to_wait_exclusive(wait_queue_head_t *q,
443 wait_queue_t *wait, int state));
444 void FASTCALL(finish_wait(wait_queue_head_t *q, wait_queue_t *wait));
445 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
446 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
448 #define DEFINE_WAIT(name) \
449 wait_queue_t name = { \
450 .private = current, \
451 .func = autoremove_wake_function, \
452 .task_list = LIST_HEAD_INIT((name).task_list), \
455 #define DEFINE_WAIT_BIT(name, word, bit) \
456 struct wait_bit_queue name = { \
457 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
459 .private = current, \
460 .func = wake_bit_function, \
462 LIST_HEAD_INIT((name).wait.task_list), \
466 #define init_wait(wait) \
468 (wait)->private = current; \
469 (wait)->func = autoremove_wake_function; \
470 INIT_LIST_HEAD(&(wait)->task_list); \
474 * wait_on_bit - wait for a bit to be cleared
475 * @word: the word being waited on, a kernel virtual address
476 * @bit: the bit of the word being waited on
477 * @action: the function used to sleep, which may take special actions
478 * @mode: the task state to sleep in
480 * There is a standard hashed waitqueue table for generic use. This
481 * is the part of the hashtable's accessor API that waits on a bit.
482 * For instance, if one were to have waiters on a bitflag, one would
483 * call wait_on_bit() in threads waiting for the bit to clear.
484 * One uses wait_on_bit() where one is waiting for the bit to clear,
485 * but has no intention of setting it.
487 static inline int wait_on_bit(void *word, int bit,
488 int (*action)(void *), unsigned mode)
490 if (!test_bit(bit, word))
492 return out_of_line_wait_on_bit(word, bit, action, mode);
496 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
497 * @word: the word being waited on, a kernel virtual address
498 * @bit: the bit of the word being waited on
499 * @action: the function used to sleep, which may take special actions
500 * @mode: the task state to sleep in
502 * There is a standard hashed waitqueue table for generic use. This
503 * is the part of the hashtable's accessor API that waits on a bit
504 * when one intends to set it, for instance, trying to lock bitflags.
505 * For instance, if one were to have waiters trying to set bitflag
506 * and waiting for it to clear before setting it, one would call
507 * wait_on_bit() in threads waiting to be able to set the bit.
508 * One uses wait_on_bit_lock() where one is waiting for the bit to
509 * clear with the intention of setting it, and when done, clearing it.
511 static inline int wait_on_bit_lock(void *word, int bit,
512 int (*action)(void *), unsigned mode)
514 if (!test_and_set_bit(bit, word))
516 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
519 #endif /* __KERNEL__ */