]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/wait.h
sched/wait: Collapse __wait_event_interruptible()
[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_signal_pending(state)                                   \
191         ((state == TASK_INTERRUPTIBLE && signal_pending(current)) ||    \
192          (state == TASK_KILLABLE && fatal_signal_pending(current)))
193
194 #define ___wait_nop_ret         int ret __always_unused
195
196 #define ___wait_event(wq, condition, state, exclusive, ret, cmd)        \
197 do {                                                                    \
198         __label__ __out;                                                \
199         DEFINE_WAIT(__wait);                                            \
200                                                                         \
201         for (;;) {                                                      \
202                 if (exclusive)                                          \
203                         prepare_to_wait_exclusive(&wq, &__wait, state); \
204                 else                                                    \
205                         prepare_to_wait(&wq, &__wait, state);           \
206                                                                         \
207                 if (condition)                                          \
208                         break;                                          \
209                                                                         \
210                 if (___wait_signal_pending(state)) {                    \
211                         ret = -ERESTARTSYS;                             \
212                         if (exclusive) {                                \
213                                 abort_exclusive_wait(&wq, &__wait,      \
214                                                      state, NULL);      \
215                                 goto __out;                             \
216                         }                                               \
217                         break;                                          \
218                 }                                                       \
219                                                                         \
220                 cmd;                                                    \
221         }                                                               \
222         finish_wait(&wq, &__wait);                                      \
223 __out:  ;                                                               \
224 } while (0)
225
226 #define __wait_event(wq, condition)                                     \
227         ___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0,           \
228                       ___wait_nop_ret, schedule())
229
230 /**
231  * wait_event - sleep until a condition gets true
232  * @wq: the waitqueue to wait on
233  * @condition: a C expression for the event to wait for
234  *
235  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
236  * @condition evaluates to true. The @condition is checked each time
237  * the waitqueue @wq is woken up.
238  *
239  * wake_up() has to be called after changing any variable that could
240  * change the result of the wait condition.
241  */
242 #define wait_event(wq, condition)                                       \
243 do {                                                                    \
244         if (condition)                                                  \
245                 break;                                                  \
246         __wait_event(wq, condition);                                    \
247 } while (0)
248
249 #define __wait_event_timeout(wq, condition, ret)                        \
250         ___wait_event(wq, ___wait_cond_timeout(condition, ret),         \
251                       TASK_UNINTERRUPTIBLE, 0, ret,                     \
252                       ret = schedule_timeout(ret))
253
254 /**
255  * wait_event_timeout - sleep until a condition gets true or a timeout elapses
256  * @wq: the waitqueue to wait on
257  * @condition: a C expression for the event to wait for
258  * @timeout: timeout, in jiffies
259  *
260  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
261  * @condition evaluates to true. The @condition is checked each time
262  * the waitqueue @wq is woken up.
263  *
264  * wake_up() has to be called after changing any variable that could
265  * change the result of the wait condition.
266  *
267  * The function returns 0 if the @timeout elapsed, or the remaining
268  * jiffies (at least 1) if the @condition evaluated to %true before
269  * the @timeout elapsed.
270  */
271 #define wait_event_timeout(wq, condition, timeout)                      \
272 ({                                                                      \
273         long __ret = timeout;                                           \
274         if (!(condition))                                               \
275                 __wait_event_timeout(wq, condition, __ret);             \
276         __ret;                                                          \
277 })
278
279 #define __wait_event_interruptible(wq, condition, ret)                  \
280         ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, ret,        \
281                       schedule())
282
283 /**
284  * wait_event_interruptible - sleep until a condition gets true
285  * @wq: the waitqueue to wait on
286  * @condition: a C expression for the event to wait for
287  *
288  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
289  * @condition evaluates to true or a signal is received.
290  * The @condition is checked each time the waitqueue @wq is woken up.
291  *
292  * wake_up() has to be called after changing any variable that could
293  * change the result of the wait condition.
294  *
295  * The function will return -ERESTARTSYS if it was interrupted by a
296  * signal and 0 if @condition evaluated to true.
297  */
298 #define wait_event_interruptible(wq, condition)                         \
299 ({                                                                      \
300         int __ret = 0;                                                  \
301         if (!(condition))                                               \
302                 __wait_event_interruptible(wq, condition, __ret);       \
303         __ret;                                                          \
304 })
305
306 #define __wait_event_interruptible_timeout(wq, condition, ret)          \
307 do {                                                                    \
308         DEFINE_WAIT(__wait);                                            \
309                                                                         \
310         for (;;) {                                                      \
311                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
312                 if (___wait_cond_timeout(condition, ret))               \
313                         break;                                          \
314                 if (signal_pending(current)) {                          \
315                         ret = -ERESTARTSYS;                             \
316                         break;                                          \
317                 }                                                       \
318                 ret = schedule_timeout(ret);                            \
319         }                                                               \
320         finish_wait(&wq, &__wait);                                      \
321 } while (0)
322
323 /**
324  * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
325  * @wq: the waitqueue to wait on
326  * @condition: a C expression for the event to wait for
327  * @timeout: timeout, in jiffies
328  *
329  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
330  * @condition evaluates to true or a signal is received.
331  * The @condition is checked each time the waitqueue @wq is woken up.
332  *
333  * wake_up() has to be called after changing any variable that could
334  * change the result of the wait condition.
335  *
336  * Returns:
337  * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
338  * a signal, or the remaining jiffies (at least 1) if the @condition
339  * evaluated to %true before the @timeout elapsed.
340  */
341 #define wait_event_interruptible_timeout(wq, condition, timeout)        \
342 ({                                                                      \
343         long __ret = timeout;                                           \
344         if (!(condition))                                               \
345                 __wait_event_interruptible_timeout(wq, condition, __ret); \
346         __ret;                                                          \
347 })
348
349 #define __wait_event_hrtimeout(wq, condition, timeout, state)           \
350 ({                                                                      \
351         int __ret = 0;                                                  \
352         DEFINE_WAIT(__wait);                                            \
353         struct hrtimer_sleeper __t;                                     \
354                                                                         \
355         hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC,              \
356                               HRTIMER_MODE_REL);                        \
357         hrtimer_init_sleeper(&__t, current);                            \
358         if ((timeout).tv64 != KTIME_MAX)                                \
359                 hrtimer_start_range_ns(&__t.timer, timeout,             \
360                                        current->timer_slack_ns,         \
361                                        HRTIMER_MODE_REL);               \
362                                                                         \
363         for (;;) {                                                      \
364                 prepare_to_wait(&wq, &__wait, state);                   \
365                 if (condition)                                          \
366                         break;                                          \
367                 if (state == TASK_INTERRUPTIBLE &&                      \
368                     signal_pending(current)) {                          \
369                         __ret = -ERESTARTSYS;                           \
370                         break;                                          \
371                 }                                                       \
372                 if (!__t.task) {                                        \
373                         __ret = -ETIME;                                 \
374                         break;                                          \
375                 }                                                       \
376                 schedule();                                             \
377         }                                                               \
378                                                                         \
379         hrtimer_cancel(&__t.timer);                                     \
380         destroy_hrtimer_on_stack(&__t.timer);                           \
381         finish_wait(&wq, &__wait);                                      \
382         __ret;                                                          \
383 })
384
385 /**
386  * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
387  * @wq: the waitqueue to wait on
388  * @condition: a C expression for the event to wait for
389  * @timeout: timeout, as a ktime_t
390  *
391  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
392  * @condition evaluates to true or a signal is received.
393  * The @condition is checked each time the waitqueue @wq is woken up.
394  *
395  * wake_up() has to be called after changing any variable that could
396  * change the result of the wait condition.
397  *
398  * The function returns 0 if @condition became true, or -ETIME if the timeout
399  * elapsed.
400  */
401 #define wait_event_hrtimeout(wq, condition, timeout)                    \
402 ({                                                                      \
403         int __ret = 0;                                                  \
404         if (!(condition))                                               \
405                 __ret = __wait_event_hrtimeout(wq, condition, timeout,  \
406                                                TASK_UNINTERRUPTIBLE);   \
407         __ret;                                                          \
408 })
409
410 /**
411  * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
412  * @wq: the waitqueue to wait on
413  * @condition: a C expression for the event to wait for
414  * @timeout: timeout, as a ktime_t
415  *
416  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
417  * @condition evaluates to true or a signal is received.
418  * The @condition is checked each time the waitqueue @wq is woken up.
419  *
420  * wake_up() has to be called after changing any variable that could
421  * change the result of the wait condition.
422  *
423  * The function returns 0 if @condition became true, -ERESTARTSYS if it was
424  * interrupted by a signal, or -ETIME if the timeout elapsed.
425  */
426 #define wait_event_interruptible_hrtimeout(wq, condition, timeout)      \
427 ({                                                                      \
428         long __ret = 0;                                                 \
429         if (!(condition))                                               \
430                 __ret = __wait_event_hrtimeout(wq, condition, timeout,  \
431                                                TASK_INTERRUPTIBLE);     \
432         __ret;                                                          \
433 })
434
435 #define __wait_event_interruptible_exclusive(wq, condition, ret)        \
436 do {                                                                    \
437         __label__ __out;                                                \
438         DEFINE_WAIT(__wait);                                            \
439                                                                         \
440         for (;;) {                                                      \
441                 prepare_to_wait_exclusive(&wq, &__wait,                 \
442                                         TASK_INTERRUPTIBLE);            \
443                 if (condition)                                          \
444                         break;                                          \
445                 if (signal_pending(current)) {                          \
446                         ret = -ERESTARTSYS;                             \
447                         abort_exclusive_wait(&wq, &__wait,              \
448                                 TASK_INTERRUPTIBLE, NULL);              \
449                         goto __out;                                     \
450                 }                                                       \
451                 schedule();                                             \
452         }                                                               \
453         finish_wait(&wq, &__wait);                                      \
454 __out:  ;                                                               \
455 } while (0)
456
457 #define wait_event_interruptible_exclusive(wq, condition)               \
458 ({                                                                      \
459         int __ret = 0;                                                  \
460         if (!(condition))                                               \
461                 __wait_event_interruptible_exclusive(wq, condition, __ret);\
462         __ret;                                                          \
463 })
464
465
466 #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
467 ({                                                                      \
468         int __ret = 0;                                                  \
469         DEFINE_WAIT(__wait);                                            \
470         if (exclusive)                                                  \
471                 __wait.flags |= WQ_FLAG_EXCLUSIVE;                      \
472         do {                                                            \
473                 if (likely(list_empty(&__wait.task_list)))              \
474                         __add_wait_queue_tail(&(wq), &__wait);          \
475                 set_current_state(TASK_INTERRUPTIBLE);                  \
476                 if (signal_pending(current)) {                          \
477                         __ret = -ERESTARTSYS;                           \
478                         break;                                          \
479                 }                                                       \
480                 if (irq)                                                \
481                         spin_unlock_irq(&(wq).lock);                    \
482                 else                                                    \
483                         spin_unlock(&(wq).lock);                        \
484                 schedule();                                             \
485                 if (irq)                                                \
486                         spin_lock_irq(&(wq).lock);                      \
487                 else                                                    \
488                         spin_lock(&(wq).lock);                          \
489         } while (!(condition));                                         \
490         __remove_wait_queue(&(wq), &__wait);                            \
491         __set_current_state(TASK_RUNNING);                              \
492         __ret;                                                          \
493 })
494
495
496 /**
497  * wait_event_interruptible_locked - sleep until a condition gets true
498  * @wq: the waitqueue to wait on
499  * @condition: a C expression for the event to wait for
500  *
501  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
502  * @condition evaluates to true or a signal is received.
503  * The @condition is checked each time the waitqueue @wq is woken up.
504  *
505  * It must be called with wq.lock being held.  This spinlock is
506  * unlocked while sleeping but @condition testing is done while lock
507  * is held and when this macro exits the lock is held.
508  *
509  * The lock is locked/unlocked using spin_lock()/spin_unlock()
510  * functions which must match the way they are locked/unlocked outside
511  * of this macro.
512  *
513  * wake_up_locked() has to be called after changing any variable that could
514  * change the result of the wait condition.
515  *
516  * The function will return -ERESTARTSYS if it was interrupted by a
517  * signal and 0 if @condition evaluated to true.
518  */
519 #define wait_event_interruptible_locked(wq, condition)                  \
520         ((condition)                                                    \
521          ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
522
523 /**
524  * wait_event_interruptible_locked_irq - sleep until a condition gets true
525  * @wq: the waitqueue to wait on
526  * @condition: a C expression for the event to wait for
527  *
528  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
529  * @condition evaluates to true or a signal is received.
530  * The @condition is checked each time the waitqueue @wq is woken up.
531  *
532  * It must be called with wq.lock being held.  This spinlock is
533  * unlocked while sleeping but @condition testing is done while lock
534  * is held and when this macro exits the lock is held.
535  *
536  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
537  * functions which must match the way they are locked/unlocked outside
538  * of this macro.
539  *
540  * wake_up_locked() has to be called after changing any variable that could
541  * change the result of the wait condition.
542  *
543  * The function will return -ERESTARTSYS if it was interrupted by a
544  * signal and 0 if @condition evaluated to true.
545  */
546 #define wait_event_interruptible_locked_irq(wq, condition)              \
547         ((condition)                                                    \
548          ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
549
550 /**
551  * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
552  * @wq: the waitqueue to wait on
553  * @condition: a C expression for the event to wait for
554  *
555  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
556  * @condition evaluates to true or a signal is received.
557  * The @condition is checked each time the waitqueue @wq is woken up.
558  *
559  * It must be called with wq.lock being held.  This spinlock is
560  * unlocked while sleeping but @condition testing is done while lock
561  * is held and when this macro exits the lock is held.
562  *
563  * The lock is locked/unlocked using spin_lock()/spin_unlock()
564  * functions which must match the way they are locked/unlocked outside
565  * of this macro.
566  *
567  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
568  * set thus when other process waits process on the list if this
569  * process is awaken further processes are not considered.
570  *
571  * wake_up_locked() has to be called after changing any variable that could
572  * change the result of the wait condition.
573  *
574  * The function will return -ERESTARTSYS if it was interrupted by a
575  * signal and 0 if @condition evaluated to true.
576  */
577 #define wait_event_interruptible_exclusive_locked(wq, condition)        \
578         ((condition)                                                    \
579          ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
580
581 /**
582  * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
583  * @wq: the waitqueue to wait on
584  * @condition: a C expression for the event to wait for
585  *
586  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
587  * @condition evaluates to true or a signal is received.
588  * The @condition is checked each time the waitqueue @wq is woken up.
589  *
590  * It must be called with wq.lock being held.  This spinlock is
591  * unlocked while sleeping but @condition testing is done while lock
592  * is held and when this macro exits the lock is held.
593  *
594  * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
595  * functions which must match the way they are locked/unlocked outside
596  * of this macro.
597  *
598  * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
599  * set thus when other process waits process on the list if this
600  * process is awaken further processes are not considered.
601  *
602  * wake_up_locked() has to be called after changing any variable that could
603  * change the result of the wait condition.
604  *
605  * The function will return -ERESTARTSYS if it was interrupted by a
606  * signal and 0 if @condition evaluated to true.
607  */
608 #define wait_event_interruptible_exclusive_locked_irq(wq, condition)    \
609         ((condition)                                                    \
610          ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
611
612
613
614 #define __wait_event_killable(wq, condition, ret)                       \
615 do {                                                                    \
616         DEFINE_WAIT(__wait);                                            \
617                                                                         \
618         for (;;) {                                                      \
619                 prepare_to_wait(&wq, &__wait, TASK_KILLABLE);           \
620                 if (condition)                                          \
621                         break;                                          \
622                 if (!fatal_signal_pending(current)) {                   \
623                         schedule();                                     \
624                         continue;                                       \
625                 }                                                       \
626                 ret = -ERESTARTSYS;                                     \
627                 break;                                                  \
628         }                                                               \
629         finish_wait(&wq, &__wait);                                      \
630 } while (0)
631
632 /**
633  * wait_event_killable - sleep until a condition gets true
634  * @wq: the waitqueue to wait on
635  * @condition: a C expression for the event to wait for
636  *
637  * The process is put to sleep (TASK_KILLABLE) until the
638  * @condition evaluates to true or a signal is received.
639  * The @condition is checked each time the waitqueue @wq is woken up.
640  *
641  * wake_up() has to be called after changing any variable that could
642  * change the result of the wait condition.
643  *
644  * The function will return -ERESTARTSYS if it was interrupted by a
645  * signal and 0 if @condition evaluated to true.
646  */
647 #define wait_event_killable(wq, condition)                              \
648 ({                                                                      \
649         int __ret = 0;                                                  \
650         if (!(condition))                                               \
651                 __wait_event_killable(wq, condition, __ret);            \
652         __ret;                                                          \
653 })
654
655
656 #define __wait_event_lock_irq(wq, condition, lock, cmd)                 \
657 do {                                                                    \
658         DEFINE_WAIT(__wait);                                            \
659                                                                         \
660         for (;;) {                                                      \
661                 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
662                 if (condition)                                          \
663                         break;                                          \
664                 spin_unlock_irq(&lock);                                 \
665                 cmd;                                                    \
666                 schedule();                                             \
667                 spin_lock_irq(&lock);                                   \
668         }                                                               \
669         finish_wait(&wq, &__wait);                                      \
670 } while (0)
671
672 /**
673  * wait_event_lock_irq_cmd - sleep until a condition gets true. The
674  *                           condition is checked under the lock. This
675  *                           is expected to be called with the lock
676  *                           taken.
677  * @wq: the waitqueue to wait on
678  * @condition: a C expression for the event to wait for
679  * @lock: a locked spinlock_t, which will be released before cmd
680  *        and schedule() and reacquired afterwards.
681  * @cmd: a command which is invoked outside the critical section before
682  *       sleep
683  *
684  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
685  * @condition evaluates to true. The @condition is checked each time
686  * the waitqueue @wq is woken up.
687  *
688  * wake_up() has to be called after changing any variable that could
689  * change the result of the wait condition.
690  *
691  * This is supposed to be called while holding the lock. The lock is
692  * dropped before invoking the cmd and going to sleep and is reacquired
693  * afterwards.
694  */
695 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd)               \
696 do {                                                                    \
697         if (condition)                                                  \
698                 break;                                                  \
699         __wait_event_lock_irq(wq, condition, lock, cmd);                \
700 } while (0)
701
702 /**
703  * wait_event_lock_irq - sleep until a condition gets true. The
704  *                       condition is checked under the lock. This
705  *                       is expected to be called with the lock
706  *                       taken.
707  * @wq: the waitqueue to wait on
708  * @condition: a C expression for the event to wait for
709  * @lock: a locked spinlock_t, which will be released before schedule()
710  *        and reacquired afterwards.
711  *
712  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
713  * @condition evaluates to true. The @condition is checked each time
714  * the waitqueue @wq is woken up.
715  *
716  * wake_up() has to be called after changing any variable that could
717  * change the result of the wait condition.
718  *
719  * This is supposed to be called while holding the lock. The lock is
720  * dropped before going to sleep and is reacquired afterwards.
721  */
722 #define wait_event_lock_irq(wq, condition, lock)                        \
723 do {                                                                    \
724         if (condition)                                                  \
725                 break;                                                  \
726         __wait_event_lock_irq(wq, condition, lock, );                   \
727 } while (0)
728
729
730 #define __wait_event_interruptible_lock_irq(wq, condition,              \
731                                             lock, ret, cmd)             \
732 do {                                                                    \
733         DEFINE_WAIT(__wait);                                            \
734                                                                         \
735         for (;;) {                                                      \
736                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
737                 if (condition)                                          \
738                         break;                                          \
739                 if (signal_pending(current)) {                          \
740                         ret = -ERESTARTSYS;                             \
741                         break;                                          \
742                 }                                                       \
743                 spin_unlock_irq(&lock);                                 \
744                 cmd;                                                    \
745                 schedule();                                             \
746                 spin_lock_irq(&lock);                                   \
747         }                                                               \
748         finish_wait(&wq, &__wait);                                      \
749 } while (0)
750
751 /**
752  * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
753  *              The condition is checked under the lock. This is expected to
754  *              be called with the lock taken.
755  * @wq: the waitqueue to wait on
756  * @condition: a C expression for the event to wait for
757  * @lock: a locked spinlock_t, which will be released before cmd and
758  *        schedule() and reacquired afterwards.
759  * @cmd: a command which is invoked outside the critical section before
760  *       sleep
761  *
762  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
763  * @condition evaluates to true or a signal is received. The @condition is
764  * checked each time the waitqueue @wq is woken up.
765  *
766  * wake_up() has to be called after changing any variable that could
767  * change the result of the wait condition.
768  *
769  * This is supposed to be called while holding the lock. The lock is
770  * dropped before invoking the cmd and going to sleep and is reacquired
771  * afterwards.
772  *
773  * The macro will return -ERESTARTSYS if it was interrupted by a signal
774  * and 0 if @condition evaluated to true.
775  */
776 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
777 ({                                                                      \
778         int __ret = 0;                                                  \
779                                                                         \
780         if (!(condition))                                               \
781                 __wait_event_interruptible_lock_irq(wq, condition,      \
782                                                     lock, __ret, cmd);  \
783         __ret;                                                          \
784 })
785
786 /**
787  * wait_event_interruptible_lock_irq - sleep until a condition gets true.
788  *              The condition is checked under the lock. This is expected
789  *              to be called with the lock taken.
790  * @wq: the waitqueue to wait on
791  * @condition: a C expression for the event to wait for
792  * @lock: a locked spinlock_t, which will be released before schedule()
793  *        and reacquired afterwards.
794  *
795  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
796  * @condition evaluates to true or signal is received. The @condition is
797  * checked each time the waitqueue @wq is woken up.
798  *
799  * wake_up() has to be called after changing any variable that could
800  * change the result of the wait condition.
801  *
802  * This is supposed to be called while holding the lock. The lock is
803  * dropped before going to sleep and is reacquired afterwards.
804  *
805  * The macro will return -ERESTARTSYS if it was interrupted by a signal
806  * and 0 if @condition evaluated to true.
807  */
808 #define wait_event_interruptible_lock_irq(wq, condition, lock)          \
809 ({                                                                      \
810         int __ret = 0;                                                  \
811                                                                         \
812         if (!(condition))                                               \
813                 __wait_event_interruptible_lock_irq(wq, condition,      \
814                                                     lock, __ret, );     \
815         __ret;                                                          \
816 })
817
818 #define __wait_event_interruptible_lock_irq_timeout(wq, condition,      \
819                                                     lock, ret)          \
820 do {                                                                    \
821         DEFINE_WAIT(__wait);                                            \
822                                                                         \
823         for (;;) {                                                      \
824                 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);      \
825                 if (___wait_cond_timeout(condition, ret))               \
826                         break;                                          \
827                 if (signal_pending(current)) {                          \
828                         ret = -ERESTARTSYS;                             \
829                         break;                                          \
830                 }                                                       \
831                 spin_unlock_irq(&lock);                                 \
832                 ret = schedule_timeout(ret);                            \
833                 spin_lock_irq(&lock);                                   \
834         }                                                               \
835         finish_wait(&wq, &__wait);                                      \
836 } while (0)
837
838 /**
839  * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
840  *              The condition is checked under the lock. This is expected
841  *              to be called with the lock taken.
842  * @wq: the waitqueue to wait on
843  * @condition: a C expression for the event to wait for
844  * @lock: a locked spinlock_t, which will be released before schedule()
845  *        and reacquired afterwards.
846  * @timeout: timeout, in jiffies
847  *
848  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
849  * @condition evaluates to true or signal is received. The @condition is
850  * checked each time the waitqueue @wq is woken up.
851  *
852  * wake_up() has to be called after changing any variable that could
853  * change the result of the wait condition.
854  *
855  * This is supposed to be called while holding the lock. The lock is
856  * dropped before going to sleep and is reacquired afterwards.
857  *
858  * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
859  * was interrupted by a signal, and the remaining jiffies otherwise
860  * if the condition evaluated to true before the timeout elapsed.
861  */
862 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock,  \
863                                                   timeout)              \
864 ({                                                                      \
865         int __ret = timeout;                                            \
866                                                                         \
867         if (!(condition))                                               \
868                 __wait_event_interruptible_lock_irq_timeout(            \
869                                         wq, condition, lock, __ret);    \
870         __ret;                                                          \
871 })
872
873
874 /*
875  * These are the old interfaces to sleep waiting for an event.
876  * They are racy.  DO NOT use them, use the wait_event* interfaces above.
877  * We plan to remove these interfaces.
878  */
879 extern void sleep_on(wait_queue_head_t *q);
880 extern long sleep_on_timeout(wait_queue_head_t *q,
881                                       signed long timeout);
882 extern void interruptible_sleep_on(wait_queue_head_t *q);
883 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
884                                            signed long timeout);
885
886 /*
887  * Waitqueues which are removed from the waitqueue_head at wakeup time
888  */
889 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
890 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
891 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
892 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
893                         unsigned int mode, void *key);
894 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
895 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
896
897 #define DEFINE_WAIT_FUNC(name, function)                                \
898         wait_queue_t name = {                                           \
899                 .private        = current,                              \
900                 .func           = function,                             \
901                 .task_list      = LIST_HEAD_INIT((name).task_list),     \
902         }
903
904 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
905
906 #define DEFINE_WAIT_BIT(name, word, bit)                                \
907         struct wait_bit_queue name = {                                  \
908                 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit),           \
909                 .wait   = {                                             \
910                         .private        = current,                      \
911                         .func           = wake_bit_function,            \
912                         .task_list      =                               \
913                                 LIST_HEAD_INIT((name).wait.task_list),  \
914                 },                                                      \
915         }
916
917 #define init_wait(wait)                                                 \
918         do {                                                            \
919                 (wait)->private = current;                              \
920                 (wait)->func = autoremove_wake_function;                \
921                 INIT_LIST_HEAD(&(wait)->task_list);                     \
922                 (wait)->flags = 0;                                      \
923         } while (0)
924
925 /**
926  * wait_on_bit - wait for a bit to be cleared
927  * @word: the word being waited on, a kernel virtual address
928  * @bit: the bit of the word being waited on
929  * @action: the function used to sleep, which may take special actions
930  * @mode: the task state to sleep in
931  *
932  * There is a standard hashed waitqueue table for generic use. This
933  * is the part of the hashtable's accessor API that waits on a bit.
934  * For instance, if one were to have waiters on a bitflag, one would
935  * call wait_on_bit() in threads waiting for the bit to clear.
936  * One uses wait_on_bit() where one is waiting for the bit to clear,
937  * but has no intention of setting it.
938  */
939 static inline int wait_on_bit(void *word, int bit,
940                                 int (*action)(void *), unsigned mode)
941 {
942         if (!test_bit(bit, word))
943                 return 0;
944         return out_of_line_wait_on_bit(word, bit, action, mode);
945 }
946
947 /**
948  * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
949  * @word: the word being waited on, a kernel virtual address
950  * @bit: the bit of the word being waited on
951  * @action: the function used to sleep, which may take special actions
952  * @mode: the task state to sleep in
953  *
954  * There is a standard hashed waitqueue table for generic use. This
955  * is the part of the hashtable's accessor API that waits on a bit
956  * when one intends to set it, for instance, trying to lock bitflags.
957  * For instance, if one were to have waiters trying to set bitflag
958  * and waiting for it to clear before setting it, one would call
959  * wait_on_bit() in threads waiting to be able to set the bit.
960  * One uses wait_on_bit_lock() where one is waiting for the bit to
961  * clear with the intention of setting it, and when done, clearing it.
962  */
963 static inline int wait_on_bit_lock(void *word, int bit,
964                                 int (*action)(void *), unsigned mode)
965 {
966         if (!test_and_set_bit(bit, word))
967                 return 0;
968         return out_of_line_wait_on_bit_lock(word, bit, action, mode);
969 }
970
971 /**
972  * wait_on_atomic_t - Wait for an atomic_t to become 0
973  * @val: The atomic value being waited on, a kernel virtual address
974  * @action: the function used to sleep, which may take special actions
975  * @mode: the task state to sleep in
976  *
977  * Wait for an atomic_t to become 0.  We abuse the bit-wait waitqueue table for
978  * the purpose of getting a waitqueue, but we set the key to a bit number
979  * outside of the target 'word'.
980  */
981 static inline
982 int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
983 {
984         if (atomic_read(val) == 0)
985                 return 0;
986         return out_of_line_wait_on_atomic_t(val, action, mode);
987 }
988         
989 #endif