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, }
81 * lockdep: we want one lock-class for all waitqueue locks.
83 extern struct lock_class_key waitqueue_lock_key
;
85 static inline void init_waitqueue_head(wait_queue_head_t
*q
)
87 spin_lock_init(&q
->lock
);
88 lockdep_set_class(&q
->lock
, &waitqueue_lock_key
);
89 INIT_LIST_HEAD(&q
->task_list
);
92 static inline void init_waitqueue_entry(wait_queue_t
*q
, struct task_struct
*p
)
96 q
->func
= default_wake_function
;
99 static inline void init_waitqueue_func_entry(wait_queue_t
*q
,
100 wait_queue_func_t func
)
107 static inline int waitqueue_active(wait_queue_head_t
*q
)
109 return !list_empty(&q
->task_list
);
113 * Used to distinguish between sync and async io wait context:
114 * sync i/o typically specifies a NULL wait queue entry or a wait
115 * queue entry bound to a task (current task) to wake up.
116 * aio specifies a wait queue entry with an async notification
117 * callback routine, not associated with any task.
119 #define is_sync_wait(wait) (!(wait) || ((wait)->private))
121 extern void FASTCALL(add_wait_queue(wait_queue_head_t
*q
, wait_queue_t
* wait
));
122 extern void FASTCALL(add_wait_queue_exclusive(wait_queue_head_t
*q
, wait_queue_t
* wait
));
123 extern void FASTCALL(remove_wait_queue(wait_queue_head_t
*q
, wait_queue_t
* wait
));
125 static inline void __add_wait_queue(wait_queue_head_t
*head
, wait_queue_t
*new)
127 list_add(&new->task_list
, &head
->task_list
);
131 * Used for wake-one threads:
133 static inline void __add_wait_queue_tail(wait_queue_head_t
*head
,
136 list_add_tail(&new->task_list
, &head
->task_list
);
139 static inline void __remove_wait_queue(wait_queue_head_t
*head
,
142 list_del(&old
->task_list
);
145 void FASTCALL(__wake_up(wait_queue_head_t
*q
, unsigned int mode
, int nr
, void *key
));
146 extern void FASTCALL(__wake_up_locked(wait_queue_head_t
*q
, unsigned int mode
));
147 extern void FASTCALL(__wake_up_sync(wait_queue_head_t
*q
, unsigned int mode
, int nr
));
148 void FASTCALL(__wake_up_bit(wait_queue_head_t
*, void *, int));
149 int FASTCALL(__wait_on_bit(wait_queue_head_t
*, struct wait_bit_queue
*, int (*)(void *), unsigned));
150 int FASTCALL(__wait_on_bit_lock(wait_queue_head_t
*, struct wait_bit_queue
*, int (*)(void *), unsigned));
151 void FASTCALL(wake_up_bit(void *, int));
152 int FASTCALL(out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned));
153 int FASTCALL(out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned));
154 wait_queue_head_t
*FASTCALL(bit_waitqueue(void *, int));
156 #define wake_up(x) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, NULL)
157 #define wake_up_nr(x, nr) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, nr, NULL)
158 #define wake_up_all(x) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 0, NULL)
159 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
160 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
161 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
162 #define wake_up_locked(x) __wake_up_locked((x), TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE)
163 #define wake_up_interruptible_sync(x) __wake_up_sync((x),TASK_INTERRUPTIBLE, 1)
165 #define __wait_event(wq, condition) \
167 DEFINE_WAIT(__wait); \
170 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
175 finish_wait(&wq, &__wait); \
179 * wait_event - sleep until a condition gets true
180 * @wq: the waitqueue to wait on
181 * @condition: a C expression for the event to wait for
183 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
184 * @condition evaluates to true. The @condition is checked each time
185 * the waitqueue @wq is woken up.
187 * wake_up() has to be called after changing any variable that could
188 * change the result of the wait condition.
190 #define wait_event(wq, condition) \
194 __wait_event(wq, condition); \
197 #define __wait_event_timeout(wq, condition, ret) \
199 DEFINE_WAIT(__wait); \
202 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
205 ret = schedule_timeout(ret); \
209 finish_wait(&wq, &__wait); \
213 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
214 * @wq: the waitqueue to wait on
215 * @condition: a C expression for the event to wait for
216 * @timeout: timeout, in jiffies
218 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
219 * @condition evaluates to true. The @condition is checked each time
220 * the waitqueue @wq is woken up.
222 * wake_up() has to be called after changing any variable that could
223 * change the result of the wait condition.
225 * The function returns 0 if the @timeout elapsed, and the remaining
226 * jiffies if the condition evaluated to true before the timeout elapsed.
228 #define wait_event_timeout(wq, condition, timeout) \
230 long __ret = timeout; \
232 __wait_event_timeout(wq, condition, __ret); \
236 #define __wait_event_interruptible(wq, condition, ret) \
238 DEFINE_WAIT(__wait); \
241 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
244 if (!signal_pending(current)) { \
248 ret = -ERESTARTSYS; \
251 finish_wait(&wq, &__wait); \
255 * wait_event_interruptible - sleep until a condition gets true
256 * @wq: the waitqueue to wait on
257 * @condition: a C expression for the event to wait for
259 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
260 * @condition evaluates to true or a signal is received.
261 * The @condition is checked each time the waitqueue @wq is woken up.
263 * wake_up() has to be called after changing any variable that could
264 * change the result of the wait condition.
266 * The function will return -ERESTARTSYS if it was interrupted by a
267 * signal and 0 if @condition evaluated to true.
269 #define wait_event_interruptible(wq, condition) \
273 __wait_event_interruptible(wq, condition, __ret); \
277 #define __wait_event_interruptible_timeout(wq, condition, ret) \
279 DEFINE_WAIT(__wait); \
282 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
285 if (!signal_pending(current)) { \
286 ret = schedule_timeout(ret); \
291 ret = -ERESTARTSYS; \
294 finish_wait(&wq, &__wait); \
298 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
299 * @wq: the waitqueue to wait on
300 * @condition: a C expression for the event to wait for
301 * @timeout: timeout, in jiffies
303 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
304 * @condition evaluates to true or a signal is received.
305 * The @condition is checked each time the waitqueue @wq is woken up.
307 * wake_up() has to be called after changing any variable that could
308 * change the result of the wait condition.
310 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
311 * was interrupted by a signal, and the remaining jiffies otherwise
312 * if the condition evaluated to true before the timeout elapsed.
314 #define wait_event_interruptible_timeout(wq, condition, timeout) \
316 long __ret = timeout; \
318 __wait_event_interruptible_timeout(wq, condition, __ret); \
322 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
324 DEFINE_WAIT(__wait); \
327 prepare_to_wait_exclusive(&wq, &__wait, \
328 TASK_INTERRUPTIBLE); \
331 if (!signal_pending(current)) { \
335 ret = -ERESTARTSYS; \
338 finish_wait(&wq, &__wait); \
341 #define wait_event_interruptible_exclusive(wq, condition) \
345 __wait_event_interruptible_exclusive(wq, condition, __ret);\
350 * Must be called with the spinlock in the wait_queue_head_t held.
352 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t
*q
,
355 wait
->flags
|= WQ_FLAG_EXCLUSIVE
;
356 __add_wait_queue_tail(q
, wait
);
360 * Must be called with the spinlock in the wait_queue_head_t held.
362 static inline void remove_wait_queue_locked(wait_queue_head_t
*q
,
365 __remove_wait_queue(q
, wait
);
369 * These are the old interfaces to sleep waiting for an event.
370 * They are racy. DO NOT use them, use the wait_event* interfaces above.
371 * We plan to remove these interfaces during 2.7.
373 extern void FASTCALL(sleep_on(wait_queue_head_t
*q
));
374 extern long FASTCALL(sleep_on_timeout(wait_queue_head_t
*q
,
375 signed long timeout
));
376 extern void FASTCALL(interruptible_sleep_on(wait_queue_head_t
*q
));
377 extern long FASTCALL(interruptible_sleep_on_timeout(wait_queue_head_t
*q
,
378 signed long timeout
));
381 * Waitqueues which are removed from the waitqueue_head at wakeup time
383 void FASTCALL(prepare_to_wait(wait_queue_head_t
*q
,
384 wait_queue_t
*wait
, int state
));
385 void FASTCALL(prepare_to_wait_exclusive(wait_queue_head_t
*q
,
386 wait_queue_t
*wait
, int state
));
387 void FASTCALL(finish_wait(wait_queue_head_t
*q
, wait_queue_t
*wait
));
388 int autoremove_wake_function(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
);
389 int wake_bit_function(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
);
391 #define DEFINE_WAIT(name) \
392 wait_queue_t name = { \
393 .private = current, \
394 .func = autoremove_wake_function, \
395 .task_list = LIST_HEAD_INIT((name).task_list), \
398 #define DEFINE_WAIT_BIT(name, word, bit) \
399 struct wait_bit_queue name = { \
400 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
402 .private = current, \
403 .func = wake_bit_function, \
405 LIST_HEAD_INIT((name).wait.task_list), \
409 #define init_wait(wait) \
411 (wait)->private = current; \
412 (wait)->func = autoremove_wake_function; \
413 INIT_LIST_HEAD(&(wait)->task_list); \
417 * wait_on_bit - wait for a bit to be cleared
418 * @word: the word being waited on, a kernel virtual address
419 * @bit: the bit of the word being waited on
420 * @action: the function used to sleep, which may take special actions
421 * @mode: the task state to sleep in
423 * There is a standard hashed waitqueue table for generic use. This
424 * is the part of the hashtable's accessor API that waits on a bit.
425 * For instance, if one were to have waiters on a bitflag, one would
426 * call wait_on_bit() in threads waiting for the bit to clear.
427 * One uses wait_on_bit() where one is waiting for the bit to clear,
428 * but has no intention of setting it.
430 static inline int wait_on_bit(void *word
, int bit
,
431 int (*action
)(void *), unsigned mode
)
433 if (!test_bit(bit
, word
))
435 return out_of_line_wait_on_bit(word
, bit
, action
, mode
);
439 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
440 * @word: the word being waited on, a kernel virtual address
441 * @bit: the bit of the word being waited on
442 * @action: the function used to sleep, which may take special actions
443 * @mode: the task state to sleep in
445 * There is a standard hashed waitqueue table for generic use. This
446 * is the part of the hashtable's accessor API that waits on a bit
447 * when one intends to set it, for instance, trying to lock bitflags.
448 * For instance, if one were to have waiters trying to set bitflag
449 * and waiting for it to clear before setting it, one would call
450 * wait_on_bit() in threads waiting to be able to set the bit.
451 * One uses wait_on_bit_lock() where one is waiting for the bit to
452 * clear with the intention of setting it, and when done, clearing it.
454 static inline int wait_on_bit_lock(void *word
, int bit
,
455 int (*action
)(void *), unsigned mode
)
457 if (!test_and_set_bit(bit
, word
))
459 return out_of_line_wait_on_bit_lock(word
, bit
, action
, mode
);
462 #endif /* __KERNEL__ */