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