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