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