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_tail(wait_queue_head_t *head,
133 list_add_tail(&new->task_list, &head->task_list);
136 static inline void __remove_wait_queue(wait_queue_head_t *head,
139 list_del(&old->task_list);
142 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
143 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
144 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
146 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
147 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
148 void __wake_up_bit(wait_queue_head_t *, void *, int);
149 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
150 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
151 void wake_up_bit(void *, int);
152 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned, struct wait_bit_queue *);
153 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned, struct wait_bit_queue *);
154 wait_queue_head_t *bit_waitqueue(void *, int);
156 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
157 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
158 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
159 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
161 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
162 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
163 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
164 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
167 * Wakeup macros to be used to report events to the targets.
169 #define wake_up_poll(x, m) \
170 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
171 #define wake_up_locked_poll(x, m) \
172 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
173 #define wake_up_interruptible_poll(x, m) \
174 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
175 #define wake_up_interruptible_sync_poll(x, m) \
176 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
178 #define __wait_event(wq, condition) \
180 DEFINE_WAIT(__wait); \
183 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
188 finish_wait(&wq, &__wait); \
192 * wait_event - sleep until a condition gets true
193 * @wq: the waitqueue to wait on
194 * @condition: a C expression for the event to wait for
196 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
197 * @condition evaluates to true. The @condition is checked each time
198 * the waitqueue @wq is woken up.
200 * wake_up() has to be called after changing any variable that could
201 * change the result of the wait condition.
203 #define wait_event(wq, condition) \
207 __wait_event(wq, condition); \
210 #define __wait_event_timeout(wq, condition, ret) \
212 DEFINE_WAIT(__wait); \
215 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
218 ret = schedule_timeout(ret); \
222 finish_wait(&wq, &__wait); \
226 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
227 * @wq: the waitqueue to wait on
228 * @condition: a C expression for the event to wait for
229 * @timeout: timeout, in jiffies
231 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
232 * @condition evaluates to true. The @condition is checked each time
233 * the waitqueue @wq is woken up.
235 * wake_up() has to be called after changing any variable that could
236 * change the result of the wait condition.
238 * The function returns 0 if the @timeout elapsed, and the remaining
239 * jiffies if the condition evaluated to true before the timeout elapsed.
241 #define wait_event_timeout(wq, condition, timeout) \
243 long __ret = timeout; \
245 __wait_event_timeout(wq, condition, __ret); \
249 #define __wait_event_interruptible(wq, condition, ret) \
251 DEFINE_WAIT(__wait); \
254 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
257 if (!signal_pending(current)) { \
261 ret = -ERESTARTSYS; \
264 finish_wait(&wq, &__wait); \
268 * wait_event_interruptible - sleep until a condition gets true
269 * @wq: the waitqueue to wait on
270 * @condition: a C expression for the event to wait for
272 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
273 * @condition evaluates to true or a signal is received.
274 * The @condition is checked each time the waitqueue @wq is woken up.
276 * wake_up() has to be called after changing any variable that could
277 * change the result of the wait condition.
279 * The function will return -ERESTARTSYS if it was interrupted by a
280 * signal and 0 if @condition evaluated to true.
282 #define wait_event_interruptible(wq, condition) \
286 __wait_event_interruptible(wq, condition, __ret); \
290 #define __wait_event_interruptible_timeout(wq, condition, ret) \
292 DEFINE_WAIT(__wait); \
295 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
298 if (!signal_pending(current)) { \
299 ret = schedule_timeout(ret); \
304 ret = -ERESTARTSYS; \
307 finish_wait(&wq, &__wait); \
311 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
312 * @wq: the waitqueue to wait on
313 * @condition: a C expression for the event to wait for
314 * @timeout: timeout, in jiffies
316 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
317 * @condition evaluates to true or a signal is received.
318 * The @condition is checked each time the waitqueue @wq is woken up.
320 * wake_up() has to be called after changing any variable that could
321 * change the result of the wait condition.
323 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
324 * was interrupted by a signal, and the remaining jiffies otherwise
325 * if the condition evaluated to true before the timeout elapsed.
327 #define wait_event_interruptible_timeout(wq, condition, timeout) \
329 long __ret = timeout; \
331 __wait_event_interruptible_timeout(wq, condition, __ret); \
335 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
337 DEFINE_WAIT(__wait); \
340 prepare_to_wait_exclusive(&wq, &__wait, \
341 TASK_INTERRUPTIBLE); \
343 finish_wait(&wq, &__wait); \
346 if (!signal_pending(current)) { \
350 ret = -ERESTARTSYS; \
351 abort_exclusive_wait(&wq, &__wait, \
352 TASK_INTERRUPTIBLE, NULL); \
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); \
406 static inline int is_sync_wait(wait_queue_t *wait)
408 return !wait || wait->private != NULL;
411 static inline int is_sync_wait_bit_queue(struct wait_bit_queue *wq)
414 return is_sync_wait(&wq->wait);
419 #define in_aio(tsk) is_sync_wait_bit_queue((tsk)->io_wait)
422 * Must be called with the spinlock in the wait_queue_head_t held.
424 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
427 wait->flags |= WQ_FLAG_EXCLUSIVE;
428 __add_wait_queue_tail(q, wait);
432 * Must be called with the spinlock in the wait_queue_head_t held.
434 static inline void remove_wait_queue_locked(wait_queue_head_t *q,
437 __remove_wait_queue(q, wait);
441 * These are the old interfaces to sleep waiting for an event.
442 * They are racy. DO NOT use them, use the wait_event* interfaces above.
443 * We plan to remove these interfaces.
445 extern void sleep_on(wait_queue_head_t *q);
446 extern long sleep_on_timeout(wait_queue_head_t *q,
447 signed long timeout);
448 extern void interruptible_sleep_on(wait_queue_head_t *q);
449 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
450 signed long timeout);
453 * Waitqueues which are removed from the waitqueue_head at wakeup time
455 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
456 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
457 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
458 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
459 unsigned int mode, void *key);
460 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
461 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
463 static inline int wait_bit_cleared(struct wait_bit_queue *wb,
464 struct wait_bit_key *key)
466 if (wb->key.flags != key->flags || wb->key.bit_nr != key->bit_nr ||
467 test_bit(key->bit_nr, key->flags))
473 #define DEFINE_WAIT_FUNC(name, function) \
474 wait_queue_t name = { \
475 .private = current, \
477 .task_list = LIST_HEAD_INIT((name).task_list), \
480 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
482 #define DEFINE_WAIT_BIT(name, word, bit) \
483 struct wait_bit_queue name = { \
484 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
486 .private = current, \
487 .func = wake_bit_function, \
489 LIST_HEAD_INIT((name).wait.task_list), \
493 #define init_wait(wait) \
495 (wait)->private = current; \
496 (wait)->func = autoremove_wake_function; \
497 INIT_LIST_HEAD(&(wait)->task_list); \
501 * wait_on_bit - wait for a bit to be cleared
502 * @word: the word being waited on, a kernel virtual address
503 * @bit: the bit of the word being waited on
504 * @action: the function used to sleep, which may take special actions
505 * @mode: the task state to sleep in
507 * There is a standard hashed waitqueue table for generic use. This
508 * is the part of the hashtable's accessor API that waits on a bit.
509 * For instance, if one were to have waiters on a bitflag, one would
510 * call wait_on_bit() in threads waiting for the bit to clear.
511 * One uses wait_on_bit() where one is waiting for the bit to clear,
512 * but has no intention of setting it.
514 static inline int wait_on_bit(void *word, int bit,
515 int (*action)(void *), unsigned mode)
517 if (!test_bit(bit, word))
519 return out_of_line_wait_on_bit(word, bit, action, mode, NULL);
522 static inline int wait_on_bit_async(void *word, int bit,
523 int (*action)(void *), unsigned mode,
524 struct wait_bit_queue *wait)
526 if (!test_bit(bit, word))
528 return out_of_line_wait_on_bit(word, bit, action, mode, wait);
532 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
533 * @word: the word being waited on, a kernel virtual address
534 * @bit: the bit of the word being waited on
535 * @action: the function used to sleep, which may take special actions
536 * @mode: the task state to sleep in
538 * There is a standard hashed waitqueue table for generic use. This
539 * is the part of the hashtable's accessor API that waits on a bit
540 * when one intends to set it, for instance, trying to lock bitflags.
541 * For instance, if one were to have waiters trying to set bitflag
542 * and waiting for it to clear before setting it, one would call
543 * wait_on_bit() in threads waiting to be able to set the bit.
544 * One uses wait_on_bit_lock() where one is waiting for the bit to
545 * clear with the intention of setting it, and when done, clearing it.
547 static inline int wait_on_bit_lock(void *word, int bit,
548 int (*action)(void *), unsigned mode)
550 if (!test_and_set_bit(bit, word))
552 return out_of_line_wait_on_bit_lock(word, bit, action, mode, NULL);
555 static inline int wait_on_bit_lock_async(void *word, int bit,
556 int (*action)(void *), unsigned mode,
557 struct wait_bit_queue *wait)
559 if (!test_and_set_bit(bit, word))
561 return out_of_line_wait_on_bit_lock(word, bit, action, mode, wait);
564 #endif /* __KERNEL__ */