vserver 1.9.5.x5
[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/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>
28
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);
32
33 struct __wait_queue {
34         unsigned int flags;
35 #define WQ_FLAG_EXCLUSIVE       0x01
36         struct task_struct * task;
37         wait_queue_func_t func;
38         struct list_head task_list;
39 };
40
41 struct wait_bit_key {
42         void *flags;
43         int bit_nr;
44 };
45
46 struct wait_bit_queue {
47         struct wait_bit_key key;
48         wait_queue_t wait;
49 };
50
51 struct __wait_queue_head {
52         spinlock_t lock;
53         struct list_head task_list;
54 };
55 typedef struct __wait_queue_head wait_queue_head_t;
56
57
58 /*
59  * Macros for declaration and initialisaton of the datatypes
60  */
61
62 #define __WAITQUEUE_INITIALIZER(name, tsk) {                            \
63         .task           = 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,                           \
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 static inline void init_waitqueue_head(wait_queue_head_t *q)
81 {
82         q->lock = SPIN_LOCK_UNLOCKED;
83         INIT_LIST_HEAD(&q->task_list);
84 }
85
86 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
87 {
88         q->flags = 0;
89         q->task = p;
90         q->func = default_wake_function;
91 }
92
93 static inline void init_waitqueue_func_entry(wait_queue_t *q,
94                                         wait_queue_func_t func)
95 {
96         q->flags = 0;
97         q->task = NULL;
98         q->func = func;
99 }
100
101 static inline int waitqueue_active(wait_queue_head_t *q)
102 {
103         return !list_empty(&q->task_list);
104 }
105
106 /*
107  * Used to distinguish between sync and async io wait context:
108  * sync i/o typically specifies a NULL wait queue entry or a wait
109  * queue entry bound to a task (current task) to wake up.
110  * aio specifies a wait queue entry with an async notification
111  * callback routine, not associated with any task.
112  */
113 #define is_sync_wait(wait)      (!(wait) || ((wait)->task))
114
115 extern void FASTCALL(add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
116 extern void FASTCALL(add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait));
117 extern void FASTCALL(remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
118
119 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
120 {
121         list_add(&new->task_list, &head->task_list);
122 }
123
124 /*
125  * Used for wake-one threads:
126  */
127 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
128                                                 wait_queue_t *new)
129 {
130         list_add_tail(&new->task_list, &head->task_list);
131 }
132
133 static inline void __remove_wait_queue(wait_queue_head_t *head,
134                                                         wait_queue_t *old)
135 {
136         list_del(&old->task_list);
137 }
138
139 void FASTCALL(__wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key));
140 extern void FASTCALL(__wake_up_locked(wait_queue_head_t *q, unsigned int mode));
141 extern void FASTCALL(__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr));
142 void FASTCALL(__wake_up_bit(wait_queue_head_t *, void *, int));
143 int FASTCALL(__wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
144 int FASTCALL(__wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
145 void FASTCALL(wake_up_bit(void *, int));
146 int FASTCALL(out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned));
147 int FASTCALL(out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned));
148 wait_queue_head_t *FASTCALL(bit_waitqueue(void *, int));
149
150 #define wake_up(x)                      __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, NULL)
151 #define wake_up_nr(x, nr)               __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, nr, NULL)
152 #define wake_up_all(x)                  __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 0, NULL)
153 #define wake_up_interruptible(x)        __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
154 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
155 #define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
156 #define wake_up_locked(x)               __wake_up_locked((x), TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE)
157 #define wake_up_interruptible_sync(x)   __wake_up_sync((x),TASK_INTERRUPTIBLE, 1)
158
159 #define __wait_event(wq, condition)                                     \
160 do {                                                                    \
161         DEFINE_WAIT(__wait);                                            \
162                                                                         \
163         for (;;) {                                                      \
164                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
165                 if (condition)                                          \
166                         break;                                          \
167                 schedule();                                             \
168         }                                                               \
169         finish_wait(&wq, &__wait);                                      \
170 } while (0)
171
172 #define wait_event(wq, condition)                                       \
173 do {                                                                    \
174         if (condition)                                                  \
175                 break;                                                  \
176         __wait_event(wq, condition);                                    \
177 } while (0)
178
179 #define __wait_event_timeout(wq, condition, ret)                        \
180 do {                                                                    \
181         DEFINE_WAIT(__wait);                                            \
182                                                                         \
183         for (;;) {                                                      \
184                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
185                 if (condition)                                          \
186                         break;                                          \
187                 ret = schedule_timeout(ret);                            \
188                 if (!ret)                                               \
189                         break;                                          \
190         }                                                               \
191         finish_wait(&wq, &__wait);                                      \
192 } while (0)
193
194 #define wait_event_timeout(wq, condition, timeout)                      \
195 ({                                                                      \
196         long __ret = timeout;                                           \
197         if (!(condition))                                               \
198                 __wait_event_timeout(wq, condition, __ret);             \
199         __ret;                                                          \
200 })
201
202 #define __wait_event_interruptible(wq, condition, ret)                  \
203 do {                                                                    \
204         DEFINE_WAIT(__wait);                                            \
205                                                                         \
206         for (;;) {                                                      \
207                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
208                 if (condition)                                          \
209                         break;                                          \
210                 if (!signal_pending(current)) {                         \
211                         schedule();                                     \
212                         continue;                                       \
213                 }                                                       \
214                 ret = -ERESTARTSYS;                                     \
215                 break;                                                  \
216         }                                                               \
217         finish_wait(&wq, &__wait);                                      \
218 } while (0)
219
220 #define wait_event_interruptible(wq, condition)                         \
221 ({                                                                      \
222         int __ret = 0;                                                  \
223         if (!(condition))                                               \
224                 __wait_event_interruptible(wq, condition, __ret);       \
225         __ret;                                                          \
226 })
227
228 #define __wait_event_interruptible_timeout(wq, condition, ret)          \
229 do {                                                                    \
230         DEFINE_WAIT(__wait);                                            \
231                                                                         \
232         for (;;) {                                                      \
233                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
234                 if (condition)                                          \
235                         break;                                          \
236                 if (!signal_pending(current)) {                         \
237                         ret = schedule_timeout(ret);                    \
238                         if (!ret)                                       \
239                                 break;                                  \
240                         continue;                                       \
241                 }                                                       \
242                 ret = -ERESTARTSYS;                                     \
243                 break;                                                  \
244         }                                                               \
245         finish_wait(&wq, &__wait);                                      \
246 } while (0)
247
248 #define wait_event_interruptible_timeout(wq, condition, timeout)        \
249 ({                                                                      \
250         long __ret = timeout;                                           \
251         if (!(condition))                                               \
252                 __wait_event_interruptible_timeout(wq, condition, __ret); \
253         __ret;                                                          \
254 })
255
256 #define __wait_event_interruptible_exclusive(wq, condition, ret)        \
257 do {                                                                    \
258         DEFINE_WAIT(__wait);                                            \
259                                                                         \
260         for (;;) {                                                      \
261                 prepare_to_wait_exclusive(&wq, &__wait,                 \
262                                         TASK_INTERRUPTIBLE);            \
263                 if (condition)                                          \
264                         break;                                          \
265                 if (!signal_pending(current)) {                         \
266                         schedule();                                     \
267                         continue;                                       \
268                 }                                                       \
269                 ret = -ERESTARTSYS;                                     \
270                 break;                                                  \
271         }                                                               \
272         finish_wait(&wq, &__wait);                                      \
273 } while (0)
274
275 #define wait_event_interruptible_exclusive(wq, condition)               \
276 ({                                                                      \
277         int __ret = 0;                                                  \
278         if (!(condition))                                               \
279                 __wait_event_interruptible_exclusive(wq, condition, __ret);\
280         __ret;                                                          \
281 })
282
283 /*
284  * Must be called with the spinlock in the wait_queue_head_t held.
285  */
286 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
287                                                    wait_queue_t * wait)
288 {
289         wait->flags |= WQ_FLAG_EXCLUSIVE;
290         __add_wait_queue_tail(q,  wait);
291 }
292
293 /*
294  * Must be called with the spinlock in the wait_queue_head_t held.
295  */
296 static inline void remove_wait_queue_locked(wait_queue_head_t *q,
297                                             wait_queue_t * wait)
298 {
299         __remove_wait_queue(q,  wait);
300 }
301
302 /*
303  * These are the old interfaces to sleep waiting for an event.
304  * They are racy.  DO NOT use them, use the wait_event* interfaces above.  
305  * We plan to remove these interfaces during 2.7.
306  */
307 extern void FASTCALL(sleep_on(wait_queue_head_t *q));
308 extern long FASTCALL(sleep_on_timeout(wait_queue_head_t *q,
309                                       signed long timeout));
310 extern void FASTCALL(interruptible_sleep_on(wait_queue_head_t *q));
311 extern long FASTCALL(interruptible_sleep_on_timeout(wait_queue_head_t *q,
312                                                     signed long timeout));
313
314 /*
315  * Waitqueues which are removed from the waitqueue_head at wakeup time
316  */
317 void FASTCALL(prepare_to_wait(wait_queue_head_t *q,
318                                 wait_queue_t *wait, int state));
319 void FASTCALL(prepare_to_wait_exclusive(wait_queue_head_t *q,
320                                 wait_queue_t *wait, int state));
321 void FASTCALL(finish_wait(wait_queue_head_t *q, wait_queue_t *wait));
322 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
323 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
324
325 #define DEFINE_WAIT(name)                                               \
326         wait_queue_t name = {                                           \
327                 .task           = current,                              \
328                 .func           = autoremove_wake_function,             \
329                 .task_list      = {     .next = &(name).task_list,      \
330                                         .prev = &(name).task_list,      \
331                                 },                                      \
332         }
333
334 #define DEFINE_WAIT_BIT(name, word, bit)                                \
335         struct wait_bit_queue name = {                                  \
336                 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit),           \
337                 .wait   = {                                             \
338                         .task           = current,                      \
339                         .func           = wake_bit_function,            \
340                         .task_list      =                               \
341                                 LIST_HEAD_INIT((name).wait.task_list),  \
342                 },                                                      \
343         }
344
345 #define init_wait(wait)                                                 \
346         do {                                                            \
347                 (wait)->task = current;                                 \
348                 (wait)->func = autoremove_wake_function;                \
349                 INIT_LIST_HEAD(&(wait)->task_list);                     \
350         } while (0)
351
352 /**
353  * wait_on_bit - wait for a bit to be cleared
354  * @word: the word being waited on, a kernel virtual address
355  * @bit: the bit of the word being waited on
356  * @action: the function used to sleep, which may take special actions
357  * @mode: the task state to sleep in
358  *
359  * There is a standard hashed waitqueue table for generic use. This
360  * is the part of the hashtable's accessor API that waits on a bit.
361  * For instance, if one were to have waiters on a bitflag, one would
362  * call wait_on_bit() in threads waiting for the bit to clear.
363  * One uses wait_on_bit() where one is waiting for the bit to clear,
364  * but has no intention of setting it.
365  */
366 static inline int wait_on_bit(void *word, int bit,
367                                 int (*action)(void *), unsigned mode)
368 {
369         if (!test_bit(bit, word))
370                 return 0;
371         return out_of_line_wait_on_bit(word, bit, action, mode);
372 }
373
374 /**
375  * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
376  * @word: the word being waited on, a kernel virtual address
377  * @bit: the bit of the word being waited on
378  * @action: the function used to sleep, which may take special actions
379  * @mode: the task state to sleep in
380  *
381  * There is a standard hashed waitqueue table for generic use. This
382  * is the part of the hashtable's accessor API that waits on a bit
383  * when one intends to set it, for instance, trying to lock bitflags.
384  * For instance, if one were to have waiters trying to set bitflag
385  * and waiting for it to clear before setting it, one would call
386  * wait_on_bit() in threads waiting to be able to set the bit.
387  * One uses wait_on_bit_lock() where one is waiting for the bit to
388  * clear with the intention of setting it, and when done, clearing it.
389  */
390 static inline int wait_on_bit_lock(void *word, int bit,
391                                 int (*action)(void *), unsigned mode)
392 {
393         if (!test_and_set_bit(bit, word))
394                 return 0;
395         return out_of_line_wait_on_bit_lock(word, bit, action, mode);
396 }
397         
398 #endif /* __KERNEL__ */
399
400 #endif