]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/wait.h
b2afd665e4ea288877e2315c8d9e3092ccd878f4
[karo-tx-linux.git] / include / linux / wait.h
1 #ifndef _LINUX_WAIT_H
2 #define _LINUX_WAIT_H
3
4
5 #include <linux/list.h>
6 #include <linux/stddef.h>
7 #include <linux/spinlock.h>
8 #include <asm/current.h>
9 #include <uapi/linux/wait.h>
10
11 typedef struct __wait_queue wait_queue_t;
12 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
13 int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
14
15 struct __wait_queue {
16         unsigned int flags;
17 #define WQ_FLAG_EXCLUSIVE       0x01
18         void *private;
19         wait_queue_func_t func;
20         struct list_head task_list;
21 };
22
23 struct wait_bit_key {
24         void *flags;
25         int bit_nr;
26 #define WAIT_ATOMIC_T_BIT_NR -1
27 };
28
29 struct wait_bit_queue {
30         struct wait_bit_key key;
31         wait_queue_t wait;
32 };
33
34 struct __wait_queue_head {
35         spinlock_t lock;
36         struct list_head task_list;
37 };
38 typedef struct __wait_queue_head wait_queue_head_t;
39
40 struct task_struct;
41
42 /*
43  * Macros for declaration and initialisaton of the datatypes
44  */
45
46 #define __WAITQUEUE_INITIALIZER(name, tsk) {                            \
47         .private        = tsk,                                          \
48         .func           = default_wake_function,                        \
49         .task_list      = { NULL, NULL } }
50
51 #define DECLARE_WAITQUEUE(name, tsk)                                    \
52         wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
53
54 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) {                           \
55         .lock           = __SPIN_LOCK_UNLOCKED(name.lock),              \
56         .task_list      = { &(name).task_list, &(name).task_list } }
57
58 #define DECLARE_WAIT_QUEUE_HEAD(name) \
59         wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
60
61 #define __WAIT_BIT_KEY_INITIALIZER(word, bit)                           \
62         { .flags = word, .bit_nr = bit, }
63
64 #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p)                              \
65         { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
66
67 extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
68
69 #define init_waitqueue_head(q)                          \
70         do {                                            \
71                 static struct lock_class_key __key;     \
72                                                         \
73                 __init_waitqueue_head((q), #q, &__key); \
74         } while (0)
75
76 #ifdef CONFIG_LOCKDEP
77 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
78         ({ init_waitqueue_head(&name); name; })
79 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
80         wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
81 #else
82 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
83 #endif
84
85 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
86 {
87         q->flags = 0;
88         q->private = p;
89         q->func = default_wake_function;
90 }
91
92 static inline void init_waitqueue_func_entry(wait_queue_t *q,
93                                         wait_queue_func_t func)
94 {
95         q->flags = 0;
96         q->private = NULL;
97         q->func = func;
98 }
99
100 static inline int waitqueue_active(wait_queue_head_t *q)
101 {
102         return !list_empty(&q->task_list);
103 }
104
105 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
106 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
107 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
108
109 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
110 {
111         list_add(&new->task_list, &head->task_list);
112 }
113
114 /*
115  * Used for wake-one threads:
116  */
117 static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
118                                               wait_queue_t *wait)
119 {
120         wait->flags |= WQ_FLAG_EXCLUSIVE;
121         __add_wait_queue(q, wait);
122 }
123
124 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
125                                          wait_queue_t *new)
126 {
127         list_add_tail(&new->task_list, &head->task_list);
128 }
129
130 static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q,
131                                               wait_queue_t *wait)
132 {
133         wait->flags |= WQ_FLAG_EXCLUSIVE;
134         __add_wait_queue_tail(q, wait);
135 }
136
137 static inline void __remove_wait_queue(wait_queue_head_t *head,
138                                                         wait_queue_t *old)
139 {
140         list_del(&old->task_list);
141 }
142
143 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
144 void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
145 void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
146                         void *key);
147 void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
148 void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
149 void __wake_up_bit(wait_queue_head_t *, void *, int);
150 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
151 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
152 void wake_up_bit(void *, int);
153 void wake_up_atomic_t(atomic_t *);
154 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
155 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
156 int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
157 wait_queue_head_t *bit_waitqueue(void *, int);
158
159 #define wake_up(x)                      __wake_up(x, TASK_NORMAL, 1, NULL)
160 #define wake_up_nr(x, nr)               __wake_up(x, TASK_NORMAL, nr, NULL)
161 #define wake_up_all(x)                  __wake_up(x, TASK_NORMAL, 0, NULL)
162 #define wake_up_locked(x)               __wake_up_locked((x), TASK_NORMAL, 1)
163 #define wake_up_all_locked(x)           __wake_up_locked((x), TASK_NORMAL, 0)
164
165 #define wake_up_interruptible(x)        __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
166 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
167 #define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
168 #define wake_up_interruptible_sync(x)   __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
169
170 /*
171  * Wakeup macros to be used to report events to the targets.
172  */
173 #define wake_up_poll(x, m)                              \
174         __wake_up(x, TASK_NORMAL, 1, (void *) (m))
175 #define wake_up_locked_poll(x, m)                               \
176         __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
177 #define wake_up_interruptible_poll(x, m)                        \
178         __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
179 #define wake_up_interruptible_sync_poll(x, m)                           \
180         __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
181
182 #define ___wait_cond_timeout(condition, ret)                            \
183 ({                                                                      \
184         bool __cond = (condition);                                      \
185         if (__cond && !ret)                                             \
186                 ret = 1;                                                \
187         __cond || !ret;                                                 \
188 })
189
190 #define __wait_event(wq, condition)                                     \
191 do {                                                                    \
192         DEFINE_WAIT(__wait);                                            \
193                                                                         \
194         for (;;) {                                                      \
195                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
196                 if (condition)                                          \
197                         break;                                          \
198                 schedule();                                             \
199         }                                                               \
200         finish_wait(&wq, &__wait);                                      \
201 } while (0)
202
203 /**
204  * wait_event - sleep until a condition gets true
205  * @wq: the waitqueue to wait on
206  * @condition: a C expression for the event to wait for
207  *
208  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
209  * @condition evaluates to true. The @condition is checked each time
210  * the waitqueue @wq is woken up.
211  *
212  * wake_up() has to be called after changing any variable that could
213  * change the result of the wait condition.
214  */
215 #define wait_event(wq, condition)                                       \
216 do {                                                                    \
217         if (condition)                                                  \
218                 break;                                                  \
219         __wait_event(wq, condition);                                    \
220 } while (0)
221
222 #define __wait_event_timeout(wq, condition, ret)                        \
223 do {                                                                    \
224         DEFINE_WAIT(__wait);                                            \
225                                                                         \
226         for (;;) {                                                      \
227                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
228                 if (___wait_cond_timeout(condition, ret))               \
229                         break;                                          \
230                 ret = schedule_timeout(ret);                            \
231         }                                                               \
232         finish_wait(&wq, &__wait);                                      \
233 } while (0)
234
235 /**
236  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
237  * @wq: the waitqueue to wait on
238  * @condition: a C expression for the event to wait for
239  * @timeout: timeout, in jiffies
240  *
241  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
242  * @condition evaluates to true. The @condition is checked each time
243  * the waitqueue @wq is woken up.
244  *
245  * wake_up() has to be called after changing any variable that could
246  * change the result of the wait condition.
247  *
248  * The function returns 0 if the @timeout elapsed, or the remaining
249  * jiffies (at least 1) if the @condition evaluated to %true before
250  * the @timeout elapsed.
251  */
252 #define wait_event_timeout(wq, condition, timeout)                      \
253 ({                                                                      \
254         long __ret = timeout;                                           \
255         if (!(condition))                                               \
256                 __wait_event_timeout(wq, condition, __ret);             \
257         __ret;                                                          \
258 })
259
260 #define __wait_event_interruptible(wq, condition, ret)                  \
261 do {                                                                    \
262         DEFINE_WAIT(__wait);                                            \
263                                                                         \
264         for (;;) {                                                      \
265                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
266                 if (condition)                                          \
267                         break;                                          \
268                 if (signal_pending(current)) {                          \
269                         ret = -ERESTARTSYS;                             \
270                         break;                                          \
271                 }                                                       \
272                 schedule();                                             \
273         }                                                               \
274         finish_wait(&wq, &__wait);                                      \
275 } while (0)
276
277 /**
278  * wait_event_interruptible - sleep until a condition gets true
279  * @wq: the waitqueue to wait on
280  * @condition: a C expression for the event to wait for
281  *
282  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
283  * @condition evaluates to true or a signal is received.
284  * The @condition is checked each time the waitqueue @wq is woken up.
285  *
286  * wake_up() has to be called after changing any variable that could
287  * change the result of the wait condition.
288  *
289  * The function will return -ERESTARTSYS if it was interrupted by a
290  * signal and 0 if @condition evaluated to true.
291  */
292 #define wait_event_interruptible(wq, condition)                         \
293 ({                                                                      \
294         int __ret = 0;                                                  \
295         if (!(condition))                                               \
296                 __wait_event_interruptible(wq, condition, __ret);       \
297         __ret;                                                          \
298 })
299
300 #define __wait_event_interruptible_timeout(wq, condition, ret)          \
301 do {                                                                    \
302         DEFINE_WAIT(__wait);                                            \
303                                                                         \
304         for (;;) {                                                      \
305                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
306                 if (___wait_cond_timeout(condition, ret))               \
307                         break;                                          \
308                 if (signal_pending(current)) {                          \
309                         ret = -ERESTARTSYS;                             \
310                         break;                                          \
311                 }                                                       \
312                 ret = schedule_timeout(ret);                            \
313         }                                                               \
314         finish_wait(&wq, &__wait);                                      \
315 } while (0)
316
317 /**
318  * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
319  * @wq: the waitqueue to wait on
320  * @condition: a C expression for the event to wait for
321  * @timeout: timeout, in jiffies
322  *
323  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
324  * @condition evaluates to true or a signal is received.
325  * The @condition is checked each time the waitqueue @wq is woken up.
326  *
327  * wake_up() has to be called after changing any variable that could
328  * change the result of the wait condition.
329  *
330  * Returns:
331  * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
332  * a signal, or the remaining jiffies (at least 1) if the @condition
333  * evaluated to %true before the @timeout elapsed.
334  */
335 #define wait_event_interruptible_timeout(wq, condition, timeout)        \
336 ({                                                                      \
337         long __ret = timeout;                                           \
338         if (!(condition))                                               \
339                 __wait_event_interruptible_timeout(wq, condition, __ret); \
340         __ret;                                                          \
341 })
342
343 #define __wait_event_hrtimeout(wq, condition, timeout, state)           \
344 ({                                                                      \
345         int __ret = 0;                                                  \
346         DEFINE_WAIT(__wait);                                            \
347         struct hrtimer_sleeper __t;                                     \
348                                                                         \
349         hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC,              \
350                               HRTIMER_MODE_REL);                        \
351         hrtimer_init_sleeper(&__t, current);                            \
352         if ((timeout).tv64 != KTIME_MAX)                                \
353                 hrtimer_start_range_ns(&__t.timer, timeout,             \
354                                        current->timer_slack_ns,         \
355                                        HRTIMER_MODE_REL);               \
356                                                                         \
357         for (;;) {                                                      \
358                 prepare_to_wait(&wq, &__wait, state);                   \
359                 if (condition)                                          \
360                         break;                                          \
361                 if (state == TASK_INTERRUPTIBLE &&                      \
362                     signal_pending(current)) {                          \
363                         __ret = -ERESTARTSYS;                           \
364                         break;                                          \
365                 }                                                       \
366                 if (!__t.task) {                                        \
367                         __ret = -ETIME;                                 \
368                         break;                                          \
369                 }                                                       \
370                 schedule();                                             \
371         }                                                               \
372                                                                         \
373         hrtimer_cancel(&__t.timer);                                     \
374         destroy_hrtimer_on_stack(&__t.timer);                           \
375         finish_wait(&wq, &__wait);                                      \
376         __ret;                                                          \
377 })
378
379 /**
380  * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
381  * @wq: the waitqueue to wait on
382  * @condition: a C expression for the event to wait for
383  * @timeout: timeout, as a ktime_t
384  *
385  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
386  * @condition evaluates to true or a signal is received.
387  * The @condition is checked each time the waitqueue @wq is woken up.
388  *
389  * wake_up() has to be called after changing any variable that could
390  * change the result of the wait condition.
391  *
392  * The function returns 0 if @condition became true, or -ETIME if the timeout
393  * elapsed.
394  */
395 #define wait_event_hrtimeout(wq, condition, timeout)                    \
396 ({                                                                      \
397         int __ret = 0;                                                  \
398         if (!(condition))                                               \
399                 __ret = __wait_event_hrtimeout(wq, condition, timeout,  \
400                                                TASK_UNINTERRUPTIBLE);   \
401         __ret;                                                          \
402 })
403
404 /**
405  * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
406  * @wq: the waitqueue to wait on
407  * @condition: a C expression for the event to wait for
408  * @timeout: timeout, as a ktime_t
409  *
410  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
411  * @condition evaluates to true or a signal is received.
412  * The @condition is checked each time the waitqueue @wq is woken up.
413  *
414  * wake_up() has to be called after changing any variable that could
415  * change the result of the wait condition.
416  *
417  * The function returns 0 if @condition became true, -ERESTARTSYS if it was
418  * interrupted by a signal, or -ETIME if the timeout elapsed.
419  */
420 #define wait_event_interruptible_hrtimeout(wq, condition, timeout)      \
421 ({                                                                      \
422         long __ret = 0;                                                 \
423         if (!(condition))                                               \
424                 __ret = __wait_event_hrtimeout(wq, condition, timeout,  \
425                                                TASK_INTERRUPTIBLE);     \
426         __ret;                                                          \
427 })
428
429 #define __wait_event_interruptible_exclusive(wq, condition, ret)        \
430 do {                                                                    \
431         DEFINE_WAIT(__wait);                                            \
432                                                                         \
433         for (;;) {                                                      \
434                 prepare_to_wait_exclusive(&wq, &__wait,                 \
435                                         TASK_INTERRUPTIBLE);            \
436                 if (condition) {                                        \
437                         finish_wait(&wq, &__wait);                      \
438                         break;                                          \
439                 }                                                       \
440                 if (signal_pending(current)) {                          \
441                         ret = -ERESTARTSYS;                             \
442                         abort_exclusive_wait(&wq, &__wait,              \
443                                 TASK_INTERRUPTIBLE, NULL);              \
444                         break;                                          \
445                 }                                                       \
446                 schedule();                                             \
447         }                                                               \
448 } while (0)
449
450 #define wait_event_interruptible_exclusive(wq, condition)               \
451 ({                                                                      \
452         int __ret = 0;                                                  \
453         if (!(condition))                                               \
454                 __wait_event_interruptible_exclusive(wq, condition, __ret);\
455         __ret;                                                          \
456 })
457
458
459 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
460 ({                                                                      \
461         int __ret = 0;                                                  \
462         DEFINE_WAIT(__wait);                                            \
463         if (exclusive)                                                  \
464                 __wait.flags |= WQ_FLAG_EXCLUSIVE;                      \
465         do {                                                            \
466                 if (likely(list_empty(&__wait.task_list)))              \
467                         __add_wait_queue_tail(&(wq), &__wait);          \
468                 set_current_state(TASK_INTERRUPTIBLE);                  \
469                 if (signal_pending(current)) {                          \
470                         __ret = -ERESTARTSYS;                           \
471                         break;                                          \
472                 }                                                       \
473                 if (irq)                                                \
474                         spin_unlock_irq(&(wq).lock);                    \
475                 else                                                    \
476                         spin_unlock(&(wq).lock);                        \
477                 schedule();                                             \
478                 if (irq)                                                \
479                         spin_lock_irq(&(wq).lock);                      \
480                 else                                                    \
481                         spin_lock(&(wq).lock);                          \
482         } while (!(condition));                                         \
483         __remove_wait_queue(&(wq), &__wait);                            \
484         __set_current_state(TASK_RUNNING);                              \
485         __ret;                                                          \
486 })
487
488
489 /**
490  * wait_event_interruptible_locked - sleep until a condition gets true
491  * @wq: the waitqueue to wait on
492  * @condition: a C expression for the event to wait for
493  *
494  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
495  * @condition evaluates to true or a signal is received.
496  * The @condition is checked each time the waitqueue @wq is woken up.
497  *
498  * It must be called with wq.lock being held.  This spinlock is
499  * unlocked while sleeping but @condition testing is done while lock
500  * is held and when this macro exits the lock is held.
501  *
502  * The lock is locked/unlocked using spin_lock()/spin_unlock()
503  * functions which must match the way they are locked/unlocked outside
504  * of this macro.
505  *
506  * wake_up_locked() has to be called after changing any variable that could
507  * change the result of the wait condition.
508  *
509  * The function will return -ERESTARTSYS if it was interrupted by a
510  * signal and 0 if @condition evaluated to true.
511  */
512 #define wait_event_interruptible_locked(wq, condition)                  \
513         ((condition)                                                    \
514          ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
515
516 /**
517  * wait_event_interruptible_locked_irq - sleep until a condition gets true
518  * @wq: the waitqueue to wait on
519  * @condition: a C expression for the event to wait for
520  *
521  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
522  * @condition evaluates to true or a signal is received.
523  * The @condition is checked each time the waitqueue @wq is woken up.
524  *
525  * It must be called with wq.lock being held.  This spinlock is
526  * unlocked while sleeping but @condition testing is done while lock
527  * is held and when this macro exits the lock is held.
528  *
529  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
530  * functions which must match the way they are locked/unlocked outside
531  * of this macro.
532  *
533  * wake_up_locked() has to be called after changing any variable that could
534  * change the result of the wait condition.
535  *
536  * The function will return -ERESTARTSYS if it was interrupted by a
537  * signal and 0 if @condition evaluated to true.
538  */
539 #define wait_event_interruptible_locked_irq(wq, condition)              \
540         ((condition)                                                    \
541          ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
542
543 /**
544  * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
545  * @wq: the waitqueue to wait on
546  * @condition: a C expression for the event to wait for
547  *
548  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
549  * @condition evaluates to true or a signal is received.
550  * The @condition is checked each time the waitqueue @wq is woken up.
551  *
552  * It must be called with wq.lock being held.  This spinlock is
553  * unlocked while sleeping but @condition testing is done while lock
554  * is held and when this macro exits the lock is held.
555  *
556  * The lock is locked/unlocked using spin_lock()/spin_unlock()
557  * functions which must match the way they are locked/unlocked outside
558  * of this macro.
559  *
560  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
561  * set thus when other process waits process on the list if this
562  * process is awaken further processes are not considered.
563  *
564  * wake_up_locked() has to be called after changing any variable that could
565  * change the result of the wait condition.
566  *
567  * The function will return -ERESTARTSYS if it was interrupted by a
568  * signal and 0 if @condition evaluated to true.
569  */
570 #define wait_event_interruptible_exclusive_locked(wq, condition)        \
571         ((condition)                                                    \
572          ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
573
574 /**
575  * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
576  * @wq: the waitqueue to wait on
577  * @condition: a C expression for the event to wait for
578  *
579  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
580  * @condition evaluates to true or a signal is received.
581  * The @condition is checked each time the waitqueue @wq is woken up.
582  *
583  * It must be called with wq.lock being held.  This spinlock is
584  * unlocked while sleeping but @condition testing is done while lock
585  * is held and when this macro exits the lock is held.
586  *
587  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
588  * functions which must match the way they are locked/unlocked outside
589  * of this macro.
590  *
591  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
592  * set thus when other process waits process on the list if this
593  * process is awaken further processes are not considered.
594  *
595  * wake_up_locked() has to be called after changing any variable that could
596  * change the result of the wait condition.
597  *
598  * The function will return -ERESTARTSYS if it was interrupted by a
599  * signal and 0 if @condition evaluated to true.
600  */
601 #define wait_event_interruptible_exclusive_locked_irq(wq, condition)    \
602         ((condition)                                                    \
603          ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
604
605
606
607 #define __wait_event_killable(wq, condition, ret)                       \
608 do {                                                                    \
609         DEFINE_WAIT(__wait);                                            \
610                                                                         \
611         for (;;) {                                                      \
612                 prepare_to_wait(&wq, &__wait, TASK_KILLABLE);           \
613                 if (condition)                                          \
614                         break;                                          \
615                 if (!fatal_signal_pending(current)) {                   \
616                         schedule();                                     \
617                         continue;                                       \
618                 }                                                       \
619                 ret = -ERESTARTSYS;                                     \
620                 break;                                                  \
621         }                                                               \
622         finish_wait(&wq, &__wait);                                      \
623 } while (0)
624
625 /**
626  * wait_event_killable - sleep until a condition gets true
627  * @wq: the waitqueue to wait on
628  * @condition: a C expression for the event to wait for
629  *
630  * The process is put to sleep (TASK_KILLABLE) until the
631  * @condition evaluates to true or a signal is received.
632  * The @condition is checked each time the waitqueue @wq is woken up.
633  *
634  * wake_up() has to be called after changing any variable that could
635  * change the result of the wait condition.
636  *
637  * The function will return -ERESTARTSYS if it was interrupted by a
638  * signal and 0 if @condition evaluated to true.
639  */
640 #define wait_event_killable(wq, condition)                              \
641 ({                                                                      \
642         int __ret = 0;                                                  \
643         if (!(condition))                                               \
644                 __wait_event_killable(wq, condition, __ret);            \
645         __ret;                                                          \
646 })
647
648
649 #define __wait_event_lock_irq(wq, condition, lock, cmd)                 \
650 do {                                                                    \
651         DEFINE_WAIT(__wait);                                            \
652                                                                         \
653         for (;;) {                                                      \
654                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
655                 if (condition)                                          \
656                         break;                                          \
657                 spin_unlock_irq(&lock);                                 \
658                 cmd;                                                    \
659                 schedule();                                             \
660                 spin_lock_irq(&lock);                                   \
661         }                                                               \
662         finish_wait(&wq, &__wait);                                      \
663 } while (0)
664
665 /**
666  * wait_event_lock_irq_cmd - sleep until a condition gets true. The
667  *                           condition is checked under the lock. This
668  *                           is expected to be called with the lock
669  *                           taken.
670  * @wq: the waitqueue to wait on
671  * @condition: a C expression for the event to wait for
672  * @lock: a locked spinlock_t, which will be released before cmd
673  *        and schedule() and reacquired afterwards.
674  * @cmd: a command which is invoked outside the critical section before
675  *       sleep
676  *
677  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
678  * @condition evaluates to true. The @condition is checked each time
679  * the waitqueue @wq is woken up.
680  *
681  * wake_up() has to be called after changing any variable that could
682  * change the result of the wait condition.
683  *
684  * This is supposed to be called while holding the lock. The lock is
685  * dropped before invoking the cmd and going to sleep and is reacquired
686  * afterwards.
687  */
688 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd)               \
689 do {                                                                    \
690         if (condition)                                                  \
691                 break;                                                  \
692         __wait_event_lock_irq(wq, condition, lock, cmd);                \
693 } while (0)
694
695 /**
696  * wait_event_lock_irq - sleep until a condition gets true. The
697  *                       condition is checked under the lock. This
698  *                       is expected to be called with the lock
699  *                       taken.
700  * @wq: the waitqueue to wait on
701  * @condition: a C expression for the event to wait for
702  * @lock: a locked spinlock_t, which will be released before schedule()
703  *        and reacquired afterwards.
704  *
705  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
706  * @condition evaluates to true. The @condition is checked each time
707  * the waitqueue @wq is woken up.
708  *
709  * wake_up() has to be called after changing any variable that could
710  * change the result of the wait condition.
711  *
712  * This is supposed to be called while holding the lock. The lock is
713  * dropped before going to sleep and is reacquired afterwards.
714  */
715 #define wait_event_lock_irq(wq, condition, lock)                        \
716 do {                                                                    \
717         if (condition)                                                  \
718                 break;                                                  \
719         __wait_event_lock_irq(wq, condition, lock, );                   \
720 } while (0)
721
722
723 #define __wait_event_interruptible_lock_irq(wq, condition,              \
724                                             lock, ret, cmd)             \
725 do {                                                                    \
726         DEFINE_WAIT(__wait);                                            \
727                                                                         \
728         for (;;) {                                                      \
729                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
730                 if (condition)                                          \
731                         break;                                          \
732                 if (signal_pending(current)) {                          \
733                         ret = -ERESTARTSYS;                             \
734                         break;                                          \
735                 }                                                       \
736                 spin_unlock_irq(&lock);                                 \
737                 cmd;                                                    \
738                 schedule();                                             \
739                 spin_lock_irq(&lock);                                   \
740         }                                                               \
741         finish_wait(&wq, &__wait);                                      \
742 } while (0)
743
744 /**
745  * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
746  *              The condition is checked under the lock. This is expected to
747  *              be called with the lock taken.
748  * @wq: the waitqueue to wait on
749  * @condition: a C expression for the event to wait for
750  * @lock: a locked spinlock_t, which will be released before cmd and
751  *        schedule() and reacquired afterwards.
752  * @cmd: a command which is invoked outside the critical section before
753  *       sleep
754  *
755  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
756  * @condition evaluates to true or a signal is received. The @condition is
757  * checked each time the waitqueue @wq is woken up.
758  *
759  * wake_up() has to be called after changing any variable that could
760  * change the result of the wait condition.
761  *
762  * This is supposed to be called while holding the lock. The lock is
763  * dropped before invoking the cmd and going to sleep and is reacquired
764  * afterwards.
765  *
766  * The macro will return -ERESTARTSYS if it was interrupted by a signal
767  * and 0 if @condition evaluated to true.
768  */
769 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
770 ({                                                                      \
771         int __ret = 0;                                                  \
772                                                                         \
773         if (!(condition))                                               \
774                 __wait_event_interruptible_lock_irq(wq, condition,      \
775                                                     lock, __ret, cmd);  \
776         __ret;                                                          \
777 })
778
779 /**
780  * wait_event_interruptible_lock_irq - sleep until a condition gets true.
781  *              The condition is checked under the lock. This is expected
782  *              to be called with the lock taken.
783  * @wq: the waitqueue to wait on
784  * @condition: a C expression for the event to wait for
785  * @lock: a locked spinlock_t, which will be released before schedule()
786  *        and reacquired afterwards.
787  *
788  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
789  * @condition evaluates to true or signal is received. The @condition is
790  * checked each time the waitqueue @wq is woken up.
791  *
792  * wake_up() has to be called after changing any variable that could
793  * change the result of the wait condition.
794  *
795  * This is supposed to be called while holding the lock. The lock is
796  * dropped before going to sleep and is reacquired afterwards.
797  *
798  * The macro will return -ERESTARTSYS if it was interrupted by a signal
799  * and 0 if @condition evaluated to true.
800  */
801 #define wait_event_interruptible_lock_irq(wq, condition, lock)          \
802 ({                                                                      \
803         int __ret = 0;                                                  \
804                                                                         \
805         if (!(condition))                                               \
806                 __wait_event_interruptible_lock_irq(wq, condition,      \
807                                                     lock, __ret, );     \
808         __ret;                                                          \
809 })
810
811 #define __wait_event_interruptible_lock_irq_timeout(wq, condition,      \
812                                                     lock, ret)          \
813 do {                                                                    \
814         DEFINE_WAIT(__wait);                                            \
815                                                                         \
816         for (;;) {                                                      \
817                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
818                 if (___wait_cond_timeout(condition, ret))               \
819                         break;                                          \
820                 if (signal_pending(current)) {                          \
821                         ret = -ERESTARTSYS;                             \
822                         break;                                          \
823                 }                                                       \
824                 spin_unlock_irq(&lock);                                 \
825                 ret = schedule_timeout(ret);                            \
826                 spin_lock_irq(&lock);                                   \
827         }                                                               \
828         finish_wait(&wq, &__wait);                                      \
829 } while (0)
830
831 /**
832  * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
833  *              The condition is checked under the lock. This is expected
834  *              to be called with the lock taken.
835  * @wq: the waitqueue to wait on
836  * @condition: a C expression for the event to wait for
837  * @lock: a locked spinlock_t, which will be released before schedule()
838  *        and reacquired afterwards.
839  * @timeout: timeout, in jiffies
840  *
841  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
842  * @condition evaluates to true or signal is received. The @condition is
843  * checked each time the waitqueue @wq is woken up.
844  *
845  * wake_up() has to be called after changing any variable that could
846  * change the result of the wait condition.
847  *
848  * This is supposed to be called while holding the lock. The lock is
849  * dropped before going to sleep and is reacquired afterwards.
850  *
851  * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
852  * was interrupted by a signal, and the remaining jiffies otherwise
853  * if the condition evaluated to true before the timeout elapsed.
854  */
855 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock,  \
856                                                   timeout)              \
857 ({                                                                      \
858         int __ret = timeout;                                            \
859                                                                         \
860         if (!(condition))                                               \
861                 __wait_event_interruptible_lock_irq_timeout(            \
862                                         wq, condition, lock, __ret);    \
863         __ret;                                                          \
864 })
865
866
867 /*
868  * These are the old interfaces to sleep waiting for an event.
869  * They are racy.  DO NOT use them, use the wait_event* interfaces above.
870  * We plan to remove these interfaces.
871  */
872 extern void sleep_on(wait_queue_head_t *q);
873 extern long sleep_on_timeout(wait_queue_head_t *q,
874                                       signed long timeout);
875 extern void interruptible_sleep_on(wait_queue_head_t *q);
876 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
877                                            signed long timeout);
878
879 /*
880  * Waitqueues which are removed from the waitqueue_head at wakeup time
881  */
882 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
883 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
884 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
885 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
886                         unsigned int mode, void *key);
887 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
888 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
889
890 #define DEFINE_WAIT_FUNC(name, function)                                \
891         wait_queue_t name = {                                           \
892                 .private        = current,                              \
893                 .func           = function,                             \
894                 .task_list      = LIST_HEAD_INIT((name).task_list),     \
895         }
896
897 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
898
899 #define DEFINE_WAIT_BIT(name, word, bit)                                \
900         struct wait_bit_queue name = {                                  \
901                 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit),           \
902                 .wait   = {                                             \
903                         .private        = current,                      \
904                         .func           = wake_bit_function,            \
905                         .task_list      =                               \
906                                 LIST_HEAD_INIT((name).wait.task_list),  \
907                 },                                                      \
908         }
909
910 #define init_wait(wait)                                                 \
911         do {                                                            \
912                 (wait)->private = current;                              \
913                 (wait)->func = autoremove_wake_function;                \
914                 INIT_LIST_HEAD(&(wait)->task_list);                     \
915                 (wait)->flags = 0;                                      \
916         } while (0)
917
918 /**
919  * wait_on_bit - wait for a bit to be cleared
920  * @word: the word being waited on, a kernel virtual address
921  * @bit: the bit of the word being waited on
922  * @action: the function used to sleep, which may take special actions
923  * @mode: the task state to sleep in
924  *
925  * There is a standard hashed waitqueue table for generic use. This
926  * is the part of the hashtable's accessor API that waits on a bit.
927  * For instance, if one were to have waiters on a bitflag, one would
928  * call wait_on_bit() in threads waiting for the bit to clear.
929  * One uses wait_on_bit() where one is waiting for the bit to clear,
930  * but has no intention of setting it.
931  */
932 static inline int wait_on_bit(void *word, int bit,
933                                 int (*action)(void *), unsigned mode)
934 {
935         if (!test_bit(bit, word))
936                 return 0;
937         return out_of_line_wait_on_bit(word, bit, action, mode);
938 }
939
940 /**
941  * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
942  * @word: the word being waited on, a kernel virtual address
943  * @bit: the bit of the word being waited on
944  * @action: the function used to sleep, which may take special actions
945  * @mode: the task state to sleep in
946  *
947  * There is a standard hashed waitqueue table for generic use. This
948  * is the part of the hashtable's accessor API that waits on a bit
949  * when one intends to set it, for instance, trying to lock bitflags.
950  * For instance, if one were to have waiters trying to set bitflag
951  * and waiting for it to clear before setting it, one would call
952  * wait_on_bit() in threads waiting to be able to set the bit.
953  * One uses wait_on_bit_lock() where one is waiting for the bit to
954  * clear with the intention of setting it, and when done, clearing it.
955  */
956 static inline int wait_on_bit_lock(void *word, int bit,
957                                 int (*action)(void *), unsigned mode)
958 {
959         if (!test_and_set_bit(bit, word))
960                 return 0;
961         return out_of_line_wait_on_bit_lock(word, bit, action, mode);
962 }
963
964 /**
965  * wait_on_atomic_t - Wait for an atomic_t to become 0
966  * @val: The atomic value being waited on, a kernel virtual address
967  * @action: the function used to sleep, which may take special actions
968  * @mode: the task state to sleep in
969  *
970  * Wait for an atomic_t to become 0.  We abuse the bit-wait waitqueue table for
971  * the purpose of getting a waitqueue, but we set the key to a bit number
972  * outside of the target 'word'.
973  */
974 static inline
975 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
976 {
977         if (atomic_read(val) == 0)
978                 return 0;
979         return out_of_line_wait_on_atomic_t(val, action, mode);
980 }
981         
982 #endif