blob: 0081147a9fe8cf23578a40bc6392cd8961e98b90 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_WAIT_H
2#define _LINUX_WAIT_H
3
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. */
10
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 */
14
15/* First argument to waitid: */
16#define P_ALL 0
17#define P_PID 1
18#define P_PGID 2
19
20#ifdef __KERNEL__
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
23#include <linux/stddef.h>
24#include <linux/spinlock.h>
25#include <asm/system.h>
26#include <asm/current.h>
27
28typedef struct __wait_queue wait_queue_t;
29typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int sync, void *key);
30int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
31
32struct __wait_queue {
33 unsigned int flags;
34#define WQ_FLAG_EXCLUSIVE 0x01
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070035 void *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 wait_queue_func_t func;
37 struct list_head task_list;
38};
39
40struct wait_bit_key {
41 void *flags;
42 int bit_nr;
43};
44
45struct wait_bit_queue {
46 struct wait_bit_key key;
47 wait_queue_t wait;
48};
49
50struct __wait_queue_head {
51 spinlock_t lock;
52 struct list_head task_list;
53};
54typedef struct __wait_queue_head wait_queue_head_t;
55
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080056struct task_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
59 * Macros for declaration and initialisaton of the datatypes
60 */
61
62#define __WAITQUEUE_INITIALIZER(name, tsk) { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070063 .private = tsk, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .func = default_wake_function, \
65 .task_list = { NULL, NULL } }
66
67#define DECLARE_WAITQUEUE(name, tsk) \
68 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
69
70#define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
Ingo Molnare4d91912006-07-03 00:24:34 -070071 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .task_list = { &(name).task_list, &(name).task_list } }
73
74#define DECLARE_WAIT_QUEUE_HEAD(name) \
75 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
76
77#define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
78 { .flags = word, .bit_nr = bit, }
79
Ingo Molnar21d71f52006-07-10 04:45:32 -070080extern void init_waitqueue_head(wait_queue_head_t *q);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080082#ifdef CONFIG_LOCKDEP
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)
87#else
88# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
89#endif
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
92{
93 q->flags = 0;
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070094 q->private = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 q->func = default_wake_function;
96}
97
98static inline void init_waitqueue_func_entry(wait_queue_t *q,
99 wait_queue_func_t func)
100{
101 q->flags = 0;
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700102 q->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 q->func = func;
104}
105
106static inline int waitqueue_active(wait_queue_head_t *q)
107{
108 return !list_empty(&q->task_list);
109}
110
111/*
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.
117 */
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700118#define is_sync_wait(wait) (!(wait) || ((wait)->private))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800120extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
121extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
122extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
125{
126 list_add(&new->task_list, &head->task_list);
127}
128
129/*
130 * Used for wake-one threads:
131 */
132static inline void __add_wait_queue_tail(wait_queue_head_t *head,
133 wait_queue_t *new)
134{
135 list_add_tail(&new->task_list, &head->task_list);
136}
137
138static inline void __remove_wait_queue(wait_queue_head_t *head,
139 wait_queue_t *old)
140{
141 list_del(&old->task_list);
142}
143
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800144void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
145extern void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
146extern void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
147void __wake_up_bit(wait_queue_head_t *, void *, int);
148int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
149int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
150void wake_up_bit(void *, int);
151int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
152int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
153wait_queue_head_t *bit_waitqueue(void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500155#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)
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#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)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500163#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800165#ifdef CONFIG_DEBUG_LOCK_ALLOC
166/*
167 * macro to avoid include hell
168 */
169#define wake_up_nested(x, s) \
170do { \
171 unsigned long flags; \
172 \
173 spin_lock_irqsave_nested(&(x)->lock, flags, (s)); \
174 wake_up_locked(x); \
175 spin_unlock_irqrestore(&(x)->lock, flags); \
176} while (0)
177#else
178#define wake_up_nested(x, s) wake_up(x)
179#endif
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#define __wait_event(wq, condition) \
182do { \
183 DEFINE_WAIT(__wait); \
184 \
185 for (;;) { \
186 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
187 if (condition) \
188 break; \
189 schedule(); \
190 } \
191 finish_wait(&wq, &__wait); \
192} while (0)
193
194/**
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
198 *
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.
202 *
203 * wake_up() has to be called after changing any variable that could
204 * change the result of the wait condition.
205 */
206#define wait_event(wq, condition) \
207do { \
208 if (condition) \
209 break; \
210 __wait_event(wq, condition); \
211} while (0)
212
213#define __wait_event_timeout(wq, condition, ret) \
214do { \
215 DEFINE_WAIT(__wait); \
216 \
217 for (;;) { \
218 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
219 if (condition) \
220 break; \
221 ret = schedule_timeout(ret); \
222 if (!ret) \
223 break; \
224 } \
225 finish_wait(&wq, &__wait); \
226} while (0)
227
228/**
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
233 *
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.
237 *
238 * wake_up() has to be called after changing any variable that could
239 * change the result of the wait condition.
240 *
241 * The function returns 0 if the @timeout elapsed, and the remaining
242 * jiffies if the condition evaluated to true before the timeout elapsed.
243 */
244#define wait_event_timeout(wq, condition, timeout) \
245({ \
246 long __ret = timeout; \
247 if (!(condition)) \
248 __wait_event_timeout(wq, condition, __ret); \
249 __ret; \
250})
251
252#define __wait_event_interruptible(wq, condition, ret) \
253do { \
254 DEFINE_WAIT(__wait); \
255 \
256 for (;;) { \
257 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
258 if (condition) \
259 break; \
260 if (!signal_pending(current)) { \
261 schedule(); \
262 continue; \
263 } \
264 ret = -ERESTARTSYS; \
265 break; \
266 } \
267 finish_wait(&wq, &__wait); \
268} while (0)
269
270/**
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
274 *
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.
278 *
279 * wake_up() has to be called after changing any variable that could
280 * change the result of the wait condition.
281 *
282 * The function will return -ERESTARTSYS if it was interrupted by a
283 * signal and 0 if @condition evaluated to true.
284 */
285#define wait_event_interruptible(wq, condition) \
286({ \
287 int __ret = 0; \
288 if (!(condition)) \
289 __wait_event_interruptible(wq, condition, __ret); \
290 __ret; \
291})
292
293#define __wait_event_interruptible_timeout(wq, condition, ret) \
294do { \
295 DEFINE_WAIT(__wait); \
296 \
297 for (;;) { \
298 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
299 if (condition) \
300 break; \
301 if (!signal_pending(current)) { \
302 ret = schedule_timeout(ret); \
303 if (!ret) \
304 break; \
305 continue; \
306 } \
307 ret = -ERESTARTSYS; \
308 break; \
309 } \
310 finish_wait(&wq, &__wait); \
311} while (0)
312
313/**
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
318 *
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.
322 *
323 * wake_up() has to be called after changing any variable that could
324 * change the result of the wait condition.
325 *
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.
329 */
330#define wait_event_interruptible_timeout(wq, condition, timeout) \
331({ \
332 long __ret = timeout; \
333 if (!(condition)) \
334 __wait_event_interruptible_timeout(wq, condition, __ret); \
335 __ret; \
336})
337
338#define __wait_event_interruptible_exclusive(wq, condition, ret) \
339do { \
340 DEFINE_WAIT(__wait); \
341 \
342 for (;;) { \
343 prepare_to_wait_exclusive(&wq, &__wait, \
344 TASK_INTERRUPTIBLE); \
345 if (condition) \
346 break; \
347 if (!signal_pending(current)) { \
348 schedule(); \
349 continue; \
350 } \
351 ret = -ERESTARTSYS; \
352 break; \
353 } \
354 finish_wait(&wq, &__wait); \
355} while (0)
356
357#define wait_event_interruptible_exclusive(wq, condition) \
358({ \
359 int __ret = 0; \
360 if (!(condition)) \
361 __wait_event_interruptible_exclusive(wq, condition, __ret);\
362 __ret; \
363})
364
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500365#define __wait_event_killable(wq, condition, ret) \
366do { \
367 DEFINE_WAIT(__wait); \
368 \
369 for (;;) { \
370 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
371 if (condition) \
372 break; \
373 if (!fatal_signal_pending(current)) { \
374 schedule(); \
375 continue; \
376 } \
377 ret = -ERESTARTSYS; \
378 break; \
379 } \
380 finish_wait(&wq, &__wait); \
381} while (0)
382
383/**
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
387 *
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.
391 *
392 * wake_up() has to be called after changing any variable that could
393 * change the result of the wait condition.
394 *
395 * The function will return -ERESTARTSYS if it was interrupted by a
396 * signal and 0 if @condition evaluated to true.
397 */
398#define wait_event_killable(wq, condition) \
399({ \
400 int __ret = 0; \
401 if (!(condition)) \
402 __wait_event_killable(wq, condition, __ret); \
403 __ret; \
404})
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/*
407 * Must be called with the spinlock in the wait_queue_head_t held.
408 */
409static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
410 wait_queue_t * wait)
411{
412 wait->flags |= WQ_FLAG_EXCLUSIVE;
413 __add_wait_queue_tail(q, wait);
414}
415
416/*
417 * Must be called with the spinlock in the wait_queue_head_t held.
418 */
419static inline void remove_wait_queue_locked(wait_queue_head_t *q,
420 wait_queue_t * wait)
421{
422 __remove_wait_queue(q, wait);
423}
424
425/*
426 * These are the old interfaces to sleep waiting for an event.
Ingo Molnar0fec1712007-07-09 18:52:01 +0200427 * They are racy. DO NOT use them, use the wait_event* interfaces above.
428 * We plan to remove these interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Ingo Molnar0fec1712007-07-09 18:52:01 +0200430extern void sleep_on(wait_queue_head_t *q);
431extern long sleep_on_timeout(wait_queue_head_t *q,
432 signed long timeout);
433extern void interruptible_sleep_on(wait_queue_head_t *q);
434extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
435 signed long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437/*
438 * Waitqueues which are removed from the waitqueue_head at wakeup time
439 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800440void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
441void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
442void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
444int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
445
446#define DEFINE_WAIT(name) \
447 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700448 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 .func = autoremove_wake_function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200450 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
453#define DEFINE_WAIT_BIT(name, word, bit) \
454 struct wait_bit_queue name = { \
455 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
456 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700457 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 .func = wake_bit_function, \
459 .task_list = \
460 LIST_HEAD_INIT((name).wait.task_list), \
461 }, \
462 }
463
464#define init_wait(wait) \
465 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700466 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 (wait)->func = autoremove_wake_function; \
468 INIT_LIST_HEAD(&(wait)->task_list); \
469 } while (0)
470
471/**
472 * wait_on_bit - wait for a bit to be cleared
473 * @word: the word being waited on, a kernel virtual address
474 * @bit: the bit of the word being waited on
475 * @action: the function used to sleep, which may take special actions
476 * @mode: the task state to sleep in
477 *
478 * There is a standard hashed waitqueue table for generic use. This
479 * is the part of the hashtable's accessor API that waits on a bit.
480 * For instance, if one were to have waiters on a bitflag, one would
481 * call wait_on_bit() in threads waiting for the bit to clear.
482 * One uses wait_on_bit() where one is waiting for the bit to clear,
483 * but has no intention of setting it.
484 */
485static inline int wait_on_bit(void *word, int bit,
486 int (*action)(void *), unsigned mode)
487{
488 if (!test_bit(bit, word))
489 return 0;
490 return out_of_line_wait_on_bit(word, bit, action, mode);
491}
492
493/**
494 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
495 * @word: the word being waited on, a kernel virtual address
496 * @bit: the bit of the word being waited on
497 * @action: the function used to sleep, which may take special actions
498 * @mode: the task state to sleep in
499 *
500 * There is a standard hashed waitqueue table for generic use. This
501 * is the part of the hashtable's accessor API that waits on a bit
502 * when one intends to set it, for instance, trying to lock bitflags.
503 * For instance, if one were to have waiters trying to set bitflag
504 * and waiting for it to clear before setting it, one would call
505 * wait_on_bit() in threads waiting to be able to set the bit.
506 * One uses wait_on_bit_lock() where one is waiting for the bit to
507 * clear with the intention of setting it, and when done, clearing it.
508 */
509static inline int wait_on_bit_lock(void *word, int bit,
510 int (*action)(void *), unsigned mode)
511{
512 if (!test_and_set_bit(bit, word))
513 return 0;
514 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
515}
516
517#endif /* __KERNEL__ */
518
519#endif