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