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