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/config.h>
23 #include <linux/list.h>
24 #include <linux/stddef.h>
25 #include <linux/spinlock.h>
26 #include <asm/system.h>
27 #include <asm/current.h>
29 typedef struct __wait_queue wait_queue_t
;
30 typedef int (*wait_queue_func_t
)(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
);
31 int default_wake_function(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
);
35 #define WQ_FLAG_EXCLUSIVE 0x01
37 wait_queue_func_t func
;
38 struct list_head task_list
;
46 struct wait_bit_queue
{
47 struct wait_bit_key key
;
51 struct __wait_queue_head
{
53 struct list_head task_list
;
55 typedef struct __wait_queue_head wait_queue_head_t
;
60 * Macros for declaration and initialisaton of the datatypes
63 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
65 .func = default_wake_function, \
66 .task_list = { NULL, NULL } }
68 #define DECLARE_WAITQUEUE(name, tsk) \
69 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
71 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
72 .lock = SPIN_LOCK_UNLOCKED, \
73 .task_list = { &(name).task_list, &(name).task_list } }
75 #define DECLARE_WAIT_QUEUE_HEAD(name) \
76 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
78 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
79 { .flags = word, .bit_nr = bit, }
81 static inline void init_waitqueue_head(wait_queue_head_t
*q
)
83 spin_lock_init(&q
->lock
);
84 INIT_LIST_HEAD(&q
->task_list
);
87 static inline void init_waitqueue_entry(wait_queue_t
*q
, struct task_struct
*p
)
91 q
->func
= default_wake_function
;
94 static inline void init_waitqueue_func_entry(wait_queue_t
*q
,
95 wait_queue_func_t func
)
102 static inline int waitqueue_active(wait_queue_head_t
*q
)
104 return !list_empty(&q
->task_list
);
108 * Used to distinguish between sync and async io wait context:
109 * sync i/o typically specifies a NULL wait queue entry or a wait
110 * queue entry bound to a task (current task) to wake up.
111 * aio specifies a wait queue entry with an async notification
112 * callback routine, not associated with any task.
114 #define is_sync_wait(wait) (!(wait) || ((wait)->private))
116 extern void FASTCALL(add_wait_queue(wait_queue_head_t
*q
, wait_queue_t
* wait
));
117 extern void FASTCALL(add_wait_queue_exclusive(wait_queue_head_t
*q
, wait_queue_t
* wait
));
118 extern void FASTCALL(remove_wait_queue(wait_queue_head_t
*q
, wait_queue_t
* wait
));
120 static inline void __add_wait_queue(wait_queue_head_t
*head
, wait_queue_t
*new)
122 list_add(&new->task_list
, &head
->task_list
);
126 * Used for wake-one threads:
128 static inline void __add_wait_queue_tail(wait_queue_head_t
*head
,
131 list_add_tail(&new->task_list
, &head
->task_list
);
134 static inline void __remove_wait_queue(wait_queue_head_t
*head
,
137 list_del(&old
->task_list
);
140 void FASTCALL(__wake_up(wait_queue_head_t
*q
, unsigned int mode
, int nr
, void *key
));
141 extern void FASTCALL(__wake_up_locked(wait_queue_head_t
*q
, unsigned int mode
));
142 extern void FASTCALL(__wake_up_sync(wait_queue_head_t
*q
, unsigned int mode
, int nr
));
143 void FASTCALL(__wake_up_bit(wait_queue_head_t
*, void *, int));
144 int FASTCALL(__wait_on_bit(wait_queue_head_t
*, struct wait_bit_queue
*, int (*)(void *), unsigned));
145 int FASTCALL(__wait_on_bit_lock(wait_queue_head_t
*, struct wait_bit_queue
*, int (*)(void *), unsigned));
146 void FASTCALL(wake_up_bit(void *, int));
147 int FASTCALL(out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned));
148 int FASTCALL(out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned));
149 wait_queue_head_t
*FASTCALL(bit_waitqueue(void *, int));
151 #define wake_up(x) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, NULL)
152 #define wake_up_nr(x, nr) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, nr, NULL)
153 #define wake_up_all(x) __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 0, NULL)
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_locked(x) __wake_up_locked((x), TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE)
158 #define wake_up_interruptible_sync(x) __wake_up_sync((x),TASK_INTERRUPTIBLE, 1)
160 #define __wait_event(wq, condition) \
162 DEFINE_WAIT(__wait); \
165 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
170 finish_wait(&wq, &__wait); \
174 * wait_event - sleep until a condition gets true
175 * @wq: the waitqueue to wait on
176 * @condition: a C expression for the event to wait for
178 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
179 * @condition evaluates to true. The @condition is checked each time
180 * the waitqueue @wq is woken up.
182 * wake_up() has to be called after changing any variable that could
183 * change the result of the wait condition.
185 #define wait_event(wq, condition) \
189 __wait_event(wq, condition); \
192 #define __wait_event_timeout(wq, condition, ret) \
194 DEFINE_WAIT(__wait); \
197 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
200 ret = schedule_timeout(ret); \
204 finish_wait(&wq, &__wait); \
208 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
209 * @wq: the waitqueue to wait on
210 * @condition: a C expression for the event to wait for
211 * @timeout: timeout, in jiffies
213 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
214 * @condition evaluates to true. The @condition is checked each time
215 * the waitqueue @wq is woken up.
217 * wake_up() has to be called after changing any variable that could
218 * change the result of the wait condition.
220 * The function returns 0 if the @timeout elapsed, and the remaining
221 * jiffies if the condition evaluated to true before the timeout elapsed.
223 #define wait_event_timeout(wq, condition, timeout) \
225 long __ret = timeout; \
227 __wait_event_timeout(wq, condition, __ret); \
231 #define __wait_event_interruptible(wq, condition, ret) \
233 DEFINE_WAIT(__wait); \
236 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
239 if (!signal_pending(current)) { \
243 ret = -ERESTARTSYS; \
246 finish_wait(&wq, &__wait); \
250 * wait_event_interruptible - sleep until a condition gets true
251 * @wq: the waitqueue to wait on
252 * @condition: a C expression for the event to wait for
254 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
255 * @condition evaluates to true or a signal is received.
256 * The @condition is checked each time the waitqueue @wq is woken up.
258 * wake_up() has to be called after changing any variable that could
259 * change the result of the wait condition.
261 * The function will return -ERESTARTSYS if it was interrupted by a
262 * signal and 0 if @condition evaluated to true.
264 #define wait_event_interruptible(wq, condition) \
268 __wait_event_interruptible(wq, condition, __ret); \
272 #define __wait_event_interruptible_timeout(wq, condition, ret) \
274 DEFINE_WAIT(__wait); \
277 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
280 if (!signal_pending(current)) { \
281 ret = schedule_timeout(ret); \
286 ret = -ERESTARTSYS; \
289 finish_wait(&wq, &__wait); \
293 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
294 * @wq: the waitqueue to wait on
295 * @condition: a C expression for the event to wait for
296 * @timeout: timeout, in jiffies
298 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
299 * @condition evaluates to true or a signal is received.
300 * The @condition is checked each time the waitqueue @wq is woken up.
302 * wake_up() has to be called after changing any variable that could
303 * change the result of the wait condition.
305 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
306 * was interrupted by a signal, and the remaining jiffies otherwise
307 * if the condition evaluated to true before the timeout elapsed.
309 #define wait_event_interruptible_timeout(wq, condition, timeout) \
311 long __ret = timeout; \
313 __wait_event_interruptible_timeout(wq, condition, __ret); \
317 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
319 DEFINE_WAIT(__wait); \
322 prepare_to_wait_exclusive(&wq, &__wait, \
323 TASK_INTERRUPTIBLE); \
326 if (!signal_pending(current)) { \
330 ret = -ERESTARTSYS; \
333 finish_wait(&wq, &__wait); \
336 #define wait_event_interruptible_exclusive(wq, condition) \
340 __wait_event_interruptible_exclusive(wq, condition, __ret);\
345 * Must be called with the spinlock in the wait_queue_head_t held.
347 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t
*q
,
350 wait
->flags
|= WQ_FLAG_EXCLUSIVE
;
351 __add_wait_queue_tail(q
, wait
);
355 * Must be called with the spinlock in the wait_queue_head_t held.
357 static inline void remove_wait_queue_locked(wait_queue_head_t
*q
,
360 __remove_wait_queue(q
, wait
);
364 * These are the old interfaces to sleep waiting for an event.
365 * They are racy. DO NOT use them, use the wait_event* interfaces above.
366 * We plan to remove these interfaces during 2.7.
368 extern void FASTCALL(sleep_on(wait_queue_head_t
*q
));
369 extern long FASTCALL(sleep_on_timeout(wait_queue_head_t
*q
,
370 signed long timeout
));
371 extern void FASTCALL(interruptible_sleep_on(wait_queue_head_t
*q
));
372 extern long FASTCALL(interruptible_sleep_on_timeout(wait_queue_head_t
*q
,
373 signed long timeout
));
376 * Waitqueues which are removed from the waitqueue_head at wakeup time
378 void FASTCALL(prepare_to_wait(wait_queue_head_t
*q
,
379 wait_queue_t
*wait
, int state
));
380 void FASTCALL(prepare_to_wait_exclusive(wait_queue_head_t
*q
,
381 wait_queue_t
*wait
, int state
));
382 void FASTCALL(finish_wait(wait_queue_head_t
*q
, wait_queue_t
*wait
));
383 int autoremove_wake_function(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
);
384 int wake_bit_function(wait_queue_t
*wait
, unsigned mode
, int sync
, void *key
);
386 #define DEFINE_WAIT(name) \
387 wait_queue_t name = { \
388 .private = current, \
389 .func = autoremove_wake_function, \
390 .task_list = LIST_HEAD_INIT((name).task_list), \
393 #define DEFINE_WAIT_BIT(name, word, bit) \
394 struct wait_bit_queue name = { \
395 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
397 .private = current, \
398 .func = wake_bit_function, \
400 LIST_HEAD_INIT((name).wait.task_list), \
404 #define init_wait(wait) \
406 (wait)->private = current; \
407 (wait)->func = autoremove_wake_function; \
408 INIT_LIST_HEAD(&(wait)->task_list); \
412 * wait_on_bit - wait for a bit to be cleared
413 * @word: the word being waited on, a kernel virtual address
414 * @bit: the bit of the word being waited on
415 * @action: the function used to sleep, which may take special actions
416 * @mode: the task state to sleep in
418 * There is a standard hashed waitqueue table for generic use. This
419 * is the part of the hashtable's accessor API that waits on a bit.
420 * For instance, if one were to have waiters on a bitflag, one would
421 * call wait_on_bit() in threads waiting for the bit to clear.
422 * One uses wait_on_bit() where one is waiting for the bit to clear,
423 * but has no intention of setting it.
425 static inline int wait_on_bit(void *word
, int bit
,
426 int (*action
)(void *), unsigned mode
)
428 if (!test_bit(bit
, word
))
430 return out_of_line_wait_on_bit(word
, bit
, action
, mode
);
434 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
435 * @word: the word being waited on, a kernel virtual address
436 * @bit: the bit of the word being waited on
437 * @action: the function used to sleep, which may take special actions
438 * @mode: the task state to sleep in
440 * There is a standard hashed waitqueue table for generic use. This
441 * is the part of the hashtable's accessor API that waits on a bit
442 * when one intends to set it, for instance, trying to lock bitflags.
443 * For instance, if one were to have waiters trying to set bitflag
444 * and waiting for it to clear before setting it, one would call
445 * wait_on_bit() in threads waiting to be able to set the bit.
446 * One uses wait_on_bit_lock() where one is waiting for the bit to
447 * clear with the intention of setting it, and when done, clearing it.
449 static inline int wait_on_bit_lock(void *word
, int bit
,
450 int (*action
)(void *), unsigned mode
)
452 if (!test_and_set_bit(bit
, word
))
454 return out_of_line_wait_on_bit_lock(word
, bit
, action
, mode
);
457 #endif /* __KERNEL__ */