Merge branch 'core/softlockup' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[linux-2.6] / 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 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
112 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
113 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
114
115 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
116 {
117         list_add(&new->task_list, &head->task_list);
118 }
119
120 /*
121  * Used for wake-one threads:
122  */
123 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
124                                                 wait_queue_t *new)
125 {
126         list_add_tail(&new->task_list, &head->task_list);
127 }
128
129 static inline void __remove_wait_queue(wait_queue_head_t *head,
130                                                         wait_queue_t *old)
131 {
132         list_del(&old->task_list);
133 }
134
135 void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
136                         int nr_exclusive, int sync, void *key);
137 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
138 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
139 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
140                         void *key);
141 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
142 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
143 void __wake_up_bit(wait_queue_head_t *, void *, int);
144 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
145 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
146 void wake_up_bit(void *, int);
147 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
148 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
149 wait_queue_head_t *bit_waitqueue(void *, int);
150
151 #define wake_up(x)                      __wake_up(x, TASK_NORMAL, 1, NULL)
152 #define wake_up_nr(x, nr)               __wake_up(x, TASK_NORMAL, nr, NULL)
153 #define wake_up_all(x)                  __wake_up(x, TASK_NORMAL, 0, NULL)
154 #define wake_up_locked(x)               __wake_up_locked((x), TASK_NORMAL)
155
156 #define wake_up_interruptible(x)        __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
157 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
158 #define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
159 #define wake_up_interruptible_sync(x)   __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
160
161 /*
162  * Wakeup macros to be used to report events to the targets.
163  */
164 #define wake_up_poll(x, m)                              \
165         __wake_up(x, TASK_NORMAL, 1, (void *) (m))
166 #define wake_up_locked_poll(x, m)                               \
167         __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
168 #define wake_up_interruptible_poll(x, m)                        \
169         __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
170 #define wake_up_interruptible_sync_poll(x, m)                           \
171         __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
172
173 #define __wait_event(wq, condition)                                     \
174 do {                                                                    \
175         DEFINE_WAIT(__wait);                                            \
176                                                                         \
177         for (;;) {                                                      \
178                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
179                 if (condition)                                          \
180                         break;                                          \
181                 schedule();                                             \
182         }                                                               \
183         finish_wait(&wq, &__wait);                                      \
184 } while (0)
185
186 /**
187  * wait_event - sleep until a condition gets true
188  * @wq: the waitqueue to wait on
189  * @condition: a C expression for the event to wait for
190  *
191  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
192  * @condition evaluates to true. The @condition is checked each time
193  * the waitqueue @wq is woken up.
194  *
195  * wake_up() has to be called after changing any variable that could
196  * change the result of the wait condition.
197  */
198 #define wait_event(wq, condition)                                       \
199 do {                                                                    \
200         if (condition)                                                  \
201                 break;                                                  \
202         __wait_event(wq, condition);                                    \
203 } while (0)
204
205 #define __wait_event_timeout(wq, condition, ret)                        \
206 do {                                                                    \
207         DEFINE_WAIT(__wait);                                            \
208                                                                         \
209         for (;;) {                                                      \
210                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
211                 if (condition)                                          \
212                         break;                                          \
213                 ret = schedule_timeout(ret);                            \
214                 if (!ret)                                               \
215                         break;                                          \
216         }                                                               \
217         finish_wait(&wq, &__wait);                                      \
218 } while (0)
219
220 /**
221  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
222  * @wq: the waitqueue to wait on
223  * @condition: a C expression for the event to wait for
224  * @timeout: timeout, in jiffies
225  *
226  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
227  * @condition evaluates to true. The @condition is checked each time
228  * the waitqueue @wq is woken up.
229  *
230  * wake_up() has to be called after changing any variable that could
231  * change the result of the wait condition.
232  *
233  * The function returns 0 if the @timeout elapsed, and the remaining
234  * jiffies if the condition evaluated to true before the timeout elapsed.
235  */
236 #define wait_event_timeout(wq, condition, timeout)                      \
237 ({                                                                      \
238         long __ret = timeout;                                           \
239         if (!(condition))                                               \
240                 __wait_event_timeout(wq, condition, __ret);             \
241         __ret;                                                          \
242 })
243
244 #define __wait_event_interruptible(wq, condition, ret)                  \
245 do {                                                                    \
246         DEFINE_WAIT(__wait);                                            \
247                                                                         \
248         for (;;) {                                                      \
249                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
250                 if (condition)                                          \
251                         break;                                          \
252                 if (!signal_pending(current)) {                         \
253                         schedule();                                     \
254                         continue;                                       \
255                 }                                                       \
256                 ret = -ERESTARTSYS;                                     \
257                 break;                                                  \
258         }                                                               \
259         finish_wait(&wq, &__wait);                                      \
260 } while (0)
261
262 /**
263  * wait_event_interruptible - sleep until a condition gets true
264  * @wq: the waitqueue to wait on
265  * @condition: a C expression for the event to wait for
266  *
267  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
268  * @condition evaluates to true or a signal is received.
269  * The @condition is checked each time the waitqueue @wq is woken up.
270  *
271  * wake_up() has to be called after changing any variable that could
272  * change the result of the wait condition.
273  *
274  * The function will return -ERESTARTSYS if it was interrupted by a
275  * signal and 0 if @condition evaluated to true.
276  */
277 #define wait_event_interruptible(wq, condition)                         \
278 ({                                                                      \
279         int __ret = 0;                                                  \
280         if (!(condition))                                               \
281                 __wait_event_interruptible(wq, condition, __ret);       \
282         __ret;                                                          \
283 })
284
285 #define __wait_event_interruptible_timeout(wq, condition, ret)          \
286 do {                                                                    \
287         DEFINE_WAIT(__wait);                                            \
288                                                                         \
289         for (;;) {                                                      \
290                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
291                 if (condition)                                          \
292                         break;                                          \
293                 if (!signal_pending(current)) {                         \
294                         ret = schedule_timeout(ret);                    \
295                         if (!ret)                                       \
296                                 break;                                  \
297                         continue;                                       \
298                 }                                                       \
299                 ret = -ERESTARTSYS;                                     \
300                 break;                                                  \
301         }                                                               \
302         finish_wait(&wq, &__wait);                                      \
303 } while (0)
304
305 /**
306  * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
307  * @wq: the waitqueue to wait on
308  * @condition: a C expression for the event to wait for
309  * @timeout: timeout, in jiffies
310  *
311  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
312  * @condition evaluates to true or a signal is received.
313  * The @condition is checked each time the waitqueue @wq is woken up.
314  *
315  * wake_up() has to be called after changing any variable that could
316  * change the result of the wait condition.
317  *
318  * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
319  * was interrupted by a signal, and the remaining jiffies otherwise
320  * if the condition evaluated to true before the timeout elapsed.
321  */
322 #define wait_event_interruptible_timeout(wq, condition, timeout)        \
323 ({                                                                      \
324         long __ret = timeout;                                           \
325         if (!(condition))                                               \
326                 __wait_event_interruptible_timeout(wq, condition, __ret); \
327         __ret;                                                          \
328 })
329
330 #define __wait_event_interruptible_exclusive(wq, condition, ret)        \
331 do {                                                                    \
332         DEFINE_WAIT(__wait);                                            \
333                                                                         \
334         for (;;) {                                                      \
335                 prepare_to_wait_exclusive(&wq, &__wait,                 \
336                                         TASK_INTERRUPTIBLE);            \
337                 if (condition) {                                        \
338                         finish_wait(&wq, &__wait);                      \
339                         break;                                          \
340                 }                                                       \
341                 if (!signal_pending(current)) {                         \
342                         schedule();                                     \
343                         continue;                                       \
344                 }                                                       \
345                 ret = -ERESTARTSYS;                                     \
346                 abort_exclusive_wait(&wq, &__wait,                      \
347                                 TASK_INTERRUPTIBLE, NULL);              \
348                 break;                                                  \
349         }                                                               \
350 } while (0)
351
352 #define wait_event_interruptible_exclusive(wq, condition)               \
353 ({                                                                      \
354         int __ret = 0;                                                  \
355         if (!(condition))                                               \
356                 __wait_event_interruptible_exclusive(wq, condition, __ret);\
357         __ret;                                                          \
358 })
359
360 #define __wait_event_killable(wq, condition, ret)                       \
361 do {                                                                    \
362         DEFINE_WAIT(__wait);                                            \
363                                                                         \
364         for (;;) {                                                      \
365                 prepare_to_wait(&wq, &__wait, TASK_KILLABLE);           \
366                 if (condition)                                          \
367                         break;                                          \
368                 if (!fatal_signal_pending(current)) {                   \
369                         schedule();                                     \
370                         continue;                                       \
371                 }                                                       \
372                 ret = -ERESTARTSYS;                                     \
373                 break;                                                  \
374         }                                                               \
375         finish_wait(&wq, &__wait);                                      \
376 } while (0)
377
378 /**
379  * wait_event_killable - sleep until a condition gets true
380  * @wq: the waitqueue to wait on
381  * @condition: a C expression for the event to wait for
382  *
383  * The process is put to sleep (TASK_KILLABLE) until the
384  * @condition evaluates to true or a signal is received.
385  * The @condition is checked each time the waitqueue @wq is woken up.
386  *
387  * wake_up() has to be called after changing any variable that could
388  * change the result of the wait condition.
389  *
390  * The function will return -ERESTARTSYS if it was interrupted by a
391  * signal and 0 if @condition evaluated to true.
392  */
393 #define wait_event_killable(wq, condition)                              \
394 ({                                                                      \
395         int __ret = 0;                                                  \
396         if (!(condition))                                               \
397                 __wait_event_killable(wq, condition, __ret);            \
398         __ret;                                                          \
399 })
400
401 /*
402  * Must be called with the spinlock in the wait_queue_head_t held.
403  */
404 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
405                                                    wait_queue_t * wait)
406 {
407         wait->flags |= WQ_FLAG_EXCLUSIVE;
408         __add_wait_queue_tail(q,  wait);
409 }
410
411 /*
412  * Must be called with the spinlock in the wait_queue_head_t held.
413  */
414 static inline void remove_wait_queue_locked(wait_queue_head_t *q,
415                                             wait_queue_t * wait)
416 {
417         __remove_wait_queue(q,  wait);
418 }
419
420 /*
421  * These are the old interfaces to sleep waiting for an event.
422  * They are racy.  DO NOT use them, use the wait_event* interfaces above.
423  * We plan to remove these interfaces.
424  */
425 extern void sleep_on(wait_queue_head_t *q);
426 extern long sleep_on_timeout(wait_queue_head_t *q,
427                                       signed long timeout);
428 extern void interruptible_sleep_on(wait_queue_head_t *q);
429 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
430                                            signed long timeout);
431
432 /*
433  * Waitqueues which are removed from the waitqueue_head at wakeup time
434  */
435 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
436 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
437 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
438 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
439                         unsigned int mode, void *key);
440 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
441 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
442
443 #define DEFINE_WAIT(name)                                               \
444         wait_queue_t name = {                                           \
445                 .private        = current,                              \
446                 .func           = autoremove_wake_function,             \
447                 .task_list      = LIST_HEAD_INIT((name).task_list),     \
448         }
449
450 #define DEFINE_WAIT_BIT(name, word, bit)                                \
451         struct wait_bit_queue name = {                                  \
452                 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit),           \
453                 .wait   = {                                             \
454                         .private        = current,                      \
455                         .func           = wake_bit_function,            \
456                         .task_list      =                               \
457                                 LIST_HEAD_INIT((name).wait.task_list),  \
458                 },                                                      \
459         }
460
461 #define init_wait(wait)                                                 \
462         do {                                                            \
463                 (wait)->private = current;                              \
464                 (wait)->func = autoremove_wake_function;                \
465                 INIT_LIST_HEAD(&(wait)->task_list);                     \
466         } while (0)
467
468 /**
469  * wait_on_bit - wait for a bit to be cleared
470  * @word: the word being waited on, a kernel virtual address
471  * @bit: the bit of the word being waited on
472  * @action: the function used to sleep, which may take special actions
473  * @mode: the task state to sleep in
474  *
475  * There is a standard hashed waitqueue table for generic use. This
476  * is the part of the hashtable's accessor API that waits on a bit.
477  * For instance, if one were to have waiters on a bitflag, one would
478  * call wait_on_bit() in threads waiting for the bit to clear.
479  * One uses wait_on_bit() where one is waiting for the bit to clear,
480  * but has no intention of setting it.
481  */
482 static inline int wait_on_bit(void *word, int bit,
483                                 int (*action)(void *), unsigned mode)
484 {
485         if (!test_bit(bit, word))
486                 return 0;
487         return out_of_line_wait_on_bit(word, bit, action, mode);
488 }
489
490 /**
491  * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
492  * @word: the word being waited on, a kernel virtual address
493  * @bit: the bit of the word being waited on
494  * @action: the function used to sleep, which may take special actions
495  * @mode: the task state to sleep in
496  *
497  * There is a standard hashed waitqueue table for generic use. This
498  * is the part of the hashtable's accessor API that waits on a bit
499  * when one intends to set it, for instance, trying to lock bitflags.
500  * For instance, if one were to have waiters trying to set bitflag
501  * and waiting for it to clear before setting it, one would call
502  * wait_on_bit() in threads waiting to be able to set the bit.
503  * One uses wait_on_bit_lock() where one is waiting for the bit to
504  * clear with the intention of setting it, and when done, clearing it.
505  */
506 static inline int wait_on_bit_lock(void *word, int bit,
507                                 int (*action)(void *), unsigned mode)
508 {
509         if (!test_and_set_bit(bit, word))
510                 return 0;
511         return out_of_line_wait_on_bit_lock(word, bit, action, mode);
512 }
513         
514 #endif /* __KERNEL__ */
515
516 #endif