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