user namespace: add the framework
[linux-2.6.git] / include / linux / wait.h
1 #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
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>
27
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);
31
32 struct __wait_queue {
33         unsigned int flags;
34 #define WQ_FLAG_EXCLUSIVE       0x01
35         void *private;
36         wait_queue_func_t func;
37         struct list_head task_list;
38 };
39
40 struct wait_bit_key {
41         void *flags;
42         int bit_nr;
43 };
44
45 struct wait_bit_queue {
46         struct wait_bit_key key;
47         wait_queue_t wait;
48 };
49
50 struct __wait_queue_head {
51         spinlock_t lock;
52         struct list_head task_list;
53 };
54 typedef struct __wait_queue_head wait_queue_head_t;
55
56 struct task_struct;
57
58 /*
59  * Macros for declaration and initialisaton of the datatypes
60  */
61
62 #define __WAITQUEUE_INITIALIZER(name, tsk) {                            \
63         .private        = tsk,                                          \
64         .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) {                           \
71         .lock           = __SPIN_LOCK_UNLOCKED(name.lock),              \
72         .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
80 extern void init_waitqueue_head(wait_queue_head_t *q);
81
82 #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
91 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
92 {
93         q->flags = 0;
94         q->private = p;
95         q->func = default_wake_function;
96 }
97
98 static inline void init_waitqueue_func_entry(wait_queue_t *q,
99                                         wait_queue_func_t func)
100 {
101         q->flags = 0;
102         q->private = NULL;
103         q->func = func;
104 }
105
106 static 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  */
118 #define is_sync_wait(wait)      (!(wait) || ((wait)->private))
119
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));
123
124 static 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  */
132 static 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
138 static inline void __remove_wait_queue(wait_queue_head_t *head,
139                                                         wait_queue_t *old)
140 {
141         list_del(&old->task_list);
142 }
143
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));
154
155 #define wake_up(x)                      __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, NULL)
156 #define wake_up_nr(x, nr)               __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, nr, NULL)
157 #define wake_up_all(x)                  __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 0, NULL)
158 #define wake_up_interruptible(x)        __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
159 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
160 #define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
161 #define wake_up_locked(x)               __wake_up_locked((x), TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE)
162 #define wake_up_interruptible_sync(x)   __wake_up_sync((x),TASK_INTERRUPTIBLE, 1)
163
164 #define __wait_event(wq, condition)                                     \
165 do {                                                                    \
166         DEFINE_WAIT(__wait);                                            \
167                                                                         \
168         for (;;) {                                                      \
169                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
170                 if (condition)                                          \
171                         break;                                          \
172                 schedule();                                             \
173         }                                                               \
174         finish_wait(&wq, &__wait);                                      \
175 } while (0)
176
177 /**
178  * wait_event - sleep until a condition gets true
179  * @wq: the waitqueue to wait on
180  * @condition: a C expression for the event to wait for
181  *
182  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
183  * @condition evaluates to true. The @condition is checked each time
184  * the waitqueue @wq is woken up.
185  *
186  * wake_up() has to be called after changing any variable that could
187  * change the result of the wait condition.
188  */
189 #define wait_event(wq, condition)                                       \
190 do {                                                                    \
191         if (condition)                                                  \
192                 break;                                                  \
193         __wait_event(wq, condition);                                    \
194 } while (0)
195
196 #define __wait_event_timeout(wq, condition, ret)                        \
197 do {                                                                    \
198         DEFINE_WAIT(__wait);                                            \
199                                                                         \
200         for (;;) {                                                      \
201                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
202                 if (condition)                                          \
203                         break;                                          \
204                 ret = schedule_timeout(ret);                            \
205                 if (!ret)                                               \
206                         break;                                          \
207         }                                                               \
208         finish_wait(&wq, &__wait);                                      \
209 } while (0)
210
211 /**
212  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
213  * @wq: the waitqueue to wait on
214  * @condition: a C expression for the event to wait for
215  * @timeout: timeout, in jiffies
216  *
217  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
218  * @condition evaluates to true. The @condition is checked each time
219  * the waitqueue @wq is woken up.
220  *
221  * wake_up() has to be called after changing any variable that could
222  * change the result of the wait condition.
223  *
224  * The function returns 0 if the @timeout elapsed, and the remaining
225  * jiffies if the condition evaluated to true before the timeout elapsed.
226  */
227 #define wait_event_timeout(wq, condition, timeout)                      \
228 ({                                                                      \
229         long __ret = timeout;                                           \
230         if (!(condition))                                               \
231                 __wait_event_timeout(wq, condition, __ret);             \
232         __ret;                                                          \
233 })
234
235 #define __wait_event_interruptible(wq, condition, ret)                  \
236 do {                                                                    \
237         DEFINE_WAIT(__wait);                                            \
238                                                                         \
239         for (;;) {                                                      \
240                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
241                 if (condition)                                          \
242                         break;                                          \
243                 if (!signal_pending(current)) {                         \
244                         schedule();                                     \
245                         continue;                                       \
246                 }                                                       \
247                 ret = -ERESTARTSYS;                                     \
248                 break;                                                  \
249         }                                                               \
250         finish_wait(&wq, &__wait);                                      \
251 } while (0)
252
253 /**
254  * wait_event_interruptible - sleep until a condition gets true
255  * @wq: the waitqueue to wait on
256  * @condition: a C expression for the event to wait for
257  *
258  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
259  * @condition evaluates to true or a signal is received.
260  * The @condition is checked each time the waitqueue @wq is woken up.
261  *
262  * wake_up() has to be called after changing any variable that could
263  * change the result of the wait condition.
264  *
265  * The function will return -ERESTARTSYS if it was interrupted by a
266  * signal and 0 if @condition evaluated to true.
267  */
268 #define wait_event_interruptible(wq, condition)                         \
269 ({                                                                      \
270         int __ret = 0;                                                  \
271         if (!(condition))                                               \
272                 __wait_event_interruptible(wq, condition, __ret);       \
273         __ret;                                                          \
274 })
275
276 #define __wait_event_interruptible_timeout(wq, condition, ret)          \
277 do {                                                                    \
278         DEFINE_WAIT(__wait);                                            \
279                                                                         \
280         for (;;) {                                                      \
281                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
282                 if (condition)                                          \
283                         break;                                          \
284                 if (!signal_pending(current)) {                         \
285                         ret = schedule_timeout(ret);                    \
286                         if (!ret)                                       \
287                                 break;                                  \
288                         continue;                                       \
289                 }                                                       \
290                 ret = -ERESTARTSYS;                                     \
291                 break;                                                  \
292         }                                                               \
293         finish_wait(&wq, &__wait);                                      \
294 } while (0)
295
296 /**
297  * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
298  * @wq: the waitqueue to wait on
299  * @condition: a C expression for the event to wait for
300  * @timeout: timeout, in jiffies
301  *
302  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
303  * @condition evaluates to true or a signal is received.
304  * The @condition is checked each time the waitqueue @wq is woken up.
305  *
306  * wake_up() has to be called after changing any variable that could
307  * change the result of the wait condition.
308  *
309  * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
310  * was interrupted by a signal, and the remaining jiffies otherwise
311  * if the condition evaluated to true before the timeout elapsed.
312  */
313 #define wait_event_interruptible_timeout(wq, condition, timeout)        \
314 ({                                                                      \
315         long __ret = timeout;                                           \
316         if (!(condition))                                               \
317                 __wait_event_interruptible_timeout(wq, condition, __ret); \
318         __ret;                                                          \
319 })
320
321 #define __wait_event_interruptible_exclusive(wq, condition, ret)        \
322 do {                                                                    \
323         DEFINE_WAIT(__wait);                                            \
324                                                                         \
325         for (;;) {                                                      \
326                 prepare_to_wait_exclusive(&wq, &__wait,                 \
327                                         TASK_INTERRUPTIBLE);            \
328                 if (condition)                                          \
329                         break;                                          \
330                 if (!signal_pending(current)) {                         \
331                         schedule();                                     \
332                         continue;                                       \
333                 }                                                       \
334                 ret = -ERESTARTSYS;                                     \
335                 break;                                                  \
336         }                                                               \
337         finish_wait(&wq, &__wait);                                      \
338 } while (0)
339
340 #define wait_event_interruptible_exclusive(wq, condition)               \
341 ({                                                                      \
342         int __ret = 0;                                                  \
343         if (!(condition))                                               \
344                 __wait_event_interruptible_exclusive(wq, condition, __ret);\
345         __ret;                                                          \
346 })
347
348 /*
349  * Must be called with the spinlock in the wait_queue_head_t held.
350  */
351 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
352                                                    wait_queue_t * wait)
353 {
354         wait->flags |= WQ_FLAG_EXCLUSIVE;
355         __add_wait_queue_tail(q,  wait);
356 }
357
358 /*
359  * Must be called with the spinlock in the wait_queue_head_t held.
360  */
361 static inline void remove_wait_queue_locked(wait_queue_head_t *q,
362                                             wait_queue_t * wait)
363 {
364         __remove_wait_queue(q,  wait);
365 }
366
367 /*
368  * These are the old interfaces to sleep waiting for an event.
369  * They are racy.  DO NOT use them, use the wait_event* interfaces above.
370  * We plan to remove these interfaces.
371  */
372 extern void sleep_on(wait_queue_head_t *q);
373 extern long sleep_on_timeout(wait_queue_head_t *q,
374                                       signed long timeout);
375 extern void interruptible_sleep_on(wait_queue_head_t *q);
376 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
377                                            signed long timeout);
378
379 /*
380  * Waitqueues which are removed from the waitqueue_head at wakeup time
381  */
382 void FASTCALL(prepare_to_wait(wait_queue_head_t *q,
383                                 wait_queue_t *wait, int state));
384 void FASTCALL(prepare_to_wait_exclusive(wait_queue_head_t *q,
385                                 wait_queue_t *wait, int state));
386 void FASTCALL(finish_wait(wait_queue_head_t *q, wait_queue_t *wait));
387 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
388 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
389
390 #define DEFINE_WAIT(name)                                               \
391         wait_queue_t name = {                                           \
392                 .private        = current,                              \
393                 .func           = autoremove_wake_function,             \
394                 .task_list      = LIST_HEAD_INIT((name).task_list),     \
395         }
396
397 #define DEFINE_WAIT_BIT(name, word, bit)                                \
398         struct wait_bit_queue name = {                                  \
399                 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit),           \
400                 .wait   = {                                             \
401                         .private        = current,                      \
402                         .func           = wake_bit_function,            \
403                         .task_list      =                               \
404                                 LIST_HEAD_INIT((name).wait.task_list),  \
405                 },                                                      \
406         }
407
408 #define init_wait(wait)                                                 \
409         do {                                                            \
410                 (wait)->private = current;                              \
411                 (wait)->func = autoremove_wake_function;                \
412                 INIT_LIST_HEAD(&(wait)->task_list);                     \
413         } while (0)
414
415 /**
416  * wait_on_bit - wait for a bit to be cleared
417  * @word: the word being waited on, a kernel virtual address
418  * @bit: the bit of the word being waited on
419  * @action: the function used to sleep, which may take special actions
420  * @mode: the task state to sleep in
421  *
422  * There is a standard hashed waitqueue table for generic use. This
423  * is the part of the hashtable's accessor API that waits on a bit.
424  * For instance, if one were to have waiters on a bitflag, one would
425  * call wait_on_bit() in threads waiting for the bit to clear.
426  * One uses wait_on_bit() where one is waiting for the bit to clear,
427  * but has no intention of setting it.
428  */
429 static inline int wait_on_bit(void *word, int bit,
430                                 int (*action)(void *), unsigned mode)
431 {
432         if (!test_bit(bit, word))
433                 return 0;
434         return out_of_line_wait_on_bit(word, bit, action, mode);
435 }
436
437 /**
438  * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
439  * @word: the word being waited on, a kernel virtual address
440  * @bit: the bit of the word being waited on
441  * @action: the function used to sleep, which may take special actions
442  * @mode: the task state to sleep in
443  *
444  * There is a standard hashed waitqueue table for generic use. This
445  * is the part of the hashtable's accessor API that waits on a bit
446  * when one intends to set it, for instance, trying to lock bitflags.
447  * For instance, if one were to have waiters trying to set bitflag
448  * and waiting for it to clear before setting it, one would call
449  * wait_on_bit() in threads waiting to be able to set the bit.
450  * One uses wait_on_bit_lock() where one is waiting for the bit to
451  * clear with the intention of setting it, and when done, clearing it.
452  */
453 static inline int wait_on_bit_lock(void *word, int bit,
454                                 int (*action)(void *), unsigned mode)
455 {
456         if (!test_and_set_bit(bit, word))
457                 return 0;
458         return out_of_line_wait_on_bit_lock(word, bit, action, mode);
459 }
460         
461 #endif /* __KERNEL__ */
462
463 #endif