Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / include / linux / wait-simple.h
1 #ifndef _LINUX_WAIT_SIMPLE_H
2 #define _LINUX_WAIT_SIMPLE_H
3
4 #include <linux/spinlock.h>
5 #include <linux/list.h>
6
7 #include <asm/current.h>
8
9 struct swaiter {
10         struct task_struct      *task;
11         struct list_head        node;
12 };
13
14 #define DEFINE_SWAITER(name)                                    \
15         struct swaiter name = {                                 \
16                 .task   = current,                              \
17                 .node   = LIST_HEAD_INIT((name).node),          \
18         }
19
20 struct swait_head {
21         raw_spinlock_t          lock;
22         struct list_head        list;
23 };
24
25 #define SWAIT_HEAD_INITIALIZER(name) {                          \
26                 .lock   = __RAW_SPIN_LOCK_UNLOCKED(name.lock),  \
27                 .list   = LIST_HEAD_INIT((name).list),          \
28         }
29
30 #define DEFINE_SWAIT_HEAD(name)                                 \
31         struct swait_head name = SWAIT_HEAD_INITIALIZER(name)
32
33 extern void __init_swait_head(struct swait_head *h, struct lock_class_key *key);
34
35 #define init_swait_head(swh)                                    \
36         do {                                                    \
37                 static struct lock_class_key __key;             \
38                                                                 \
39                 __init_swait_head((swh), &__key);               \
40         } while (0)
41
42 /*
43  * Waiter functions
44  */
45 extern void swait_prepare_locked(struct swait_head *head, struct swaiter *w);
46 extern void swait_prepare(struct swait_head *head, struct swaiter *w, int state);
47 extern void swait_finish_locked(struct swait_head *head, struct swaiter *w);
48 extern void swait_finish(struct swait_head *head, struct swaiter *w);
49
50 /* Check whether a head has waiters enqueued */
51 static inline bool swaitqueue_active(struct swait_head *h)
52 {
53         /* Make sure the condition is visible before checking list_empty() */
54         smp_mb();
55         return !list_empty(&h->list);
56 }
57
58 /*
59  * Wakeup functions
60  */
61 extern unsigned int __swait_wake(struct swait_head *head, unsigned int state, unsigned int num);
62 extern unsigned int __swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num);
63
64 #define swait_wake(head)                        __swait_wake(head, TASK_NORMAL, 1)
65 #define swait_wake_interruptible(head)          __swait_wake(head, TASK_INTERRUPTIBLE, 1)
66 #define swait_wake_all(head)                    __swait_wake(head, TASK_NORMAL, 0)
67 #define swait_wake_all_interruptible(head)      __swait_wake(head, TASK_INTERRUPTIBLE, 0)
68
69 /*
70  * Event API
71  */
72 #define __swait_event(wq, condition)                                    \
73 do {                                                                    \
74         DEFINE_SWAITER(__wait);                                         \
75                                                                         \
76         for (;;) {                                                      \
77                 swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE);      \
78                 if (condition)                                          \
79                         break;                                          \
80                 schedule();                                             \
81         }                                                               \
82         swait_finish(&wq, &__wait);                                     \
83 } while (0)
84
85 /**
86  * swait_event - sleep until a condition gets true
87  * @wq: the waitqueue to wait on
88  * @condition: a C expression for the event to wait for
89  *
90  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
91  * @condition evaluates to true. The @condition is checked each time
92  * the waitqueue @wq is woken up.
93  *
94  * wake_up() has to be called after changing any variable that could
95  * change the result of the wait condition.
96  */
97 #define swait_event(wq, condition)                                      \
98 do {                                                                    \
99         if (condition)                                                  \
100                 break;                                                  \
101         __swait_event(wq, condition);                                   \
102 } while (0)
103
104 #define __swait_event_interruptible(wq, condition, ret)                 \
105 do {                                                                    \
106         DEFINE_SWAITER(__wait);                                         \
107                                                                         \
108         for (;;) {                                                      \
109                 swait_prepare(&wq, &__wait, TASK_INTERRUPTIBLE);        \
110                 if (condition)                                          \
111                         break;                                          \
112                 if (signal_pending(current)) {                          \
113                         ret = -ERESTARTSYS;                             \
114                         break;                                          \
115                 }                                                       \
116                 schedule();                                             \
117         }                                                               \
118         swait_finish(&wq, &__wait);                                     \
119 } while (0)
120
121 #define __swait_event_interruptible_timeout(wq, condition, ret)         \
122 do {                                                                    \
123         DEFINE_SWAITER(__wait);                                         \
124                                                                         \
125         for (;;) {                                                      \
126                 swait_prepare(&wq, &__wait, TASK_INTERRUPTIBLE);        \
127                 if (condition)                                          \
128                         break;                                          \
129                 if (signal_pending(current)) {                          \
130                         ret = -ERESTARTSYS;                             \
131                         break;                                          \
132                 }                                                       \
133                 ret = schedule_timeout(ret);                            \
134                 if (!ret)                                               \
135                         break;                                          \
136         }                                                               \
137         swait_finish(&wq, &__wait);                                     \
138 } while (0)
139
140 /**
141  * swait_event_interruptible - sleep until a condition gets true
142  * @wq: the waitqueue to wait on
143  * @condition: a C expression for the event to wait for
144  *
145  * The process is put to sleep (TASK_INTERRUPTIBLE) until the
146  * @condition evaluates to true. The @condition is checked each time
147  * the waitqueue @wq is woken up.
148  *
149  * wake_up() has to be called after changing any variable that could
150  * change the result of the wait condition.
151  */
152 #define swait_event_interruptible(wq, condition)                        \
153 ({                                                                      \
154         int __ret = 0;                                                  \
155         if (!(condition))                                               \
156                 __swait_event_interruptible(wq, condition, __ret);      \
157         __ret;                                                          \
158 })
159
160 #define swait_event_interruptible_timeout(wq, condition, timeout)       \
161 ({                                                                      \
162         int __ret = timeout;                                            \
163         if (!(condition))                                               \
164                 __swait_event_interruptible_timeout(wq, condition, __ret);      \
165         __ret;                                                          \
166 })
167
168 #define __swait_event_timeout(wq, condition, ret)                       \
169 do {                                                                    \
170         DEFINE_SWAITER(__wait);                                         \
171                                                                         \
172         for (;;) {                                                      \
173                 swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE);      \
174                 if (condition)                                          \
175                         break;                                          \
176                 ret = schedule_timeout(ret);                            \
177                 if (!ret)                                               \
178                         break;                                          \
179         }                                                               \
180         swait_finish(&wq, &__wait);                                     \
181 } while (0)
182
183 /**
184  * swait_event_timeout - sleep until a condition gets true or a timeout elapses
185  * @wq: the waitqueue to wait on
186  * @condition: a C expression for the event to wait for
187  * @timeout: timeout, in jiffies
188  *
189  * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
190  * @condition evaluates to true. The @condition is checked each time
191  * the waitqueue @wq is woken up.
192  *
193  * wake_up() has to be called after changing any variable that could
194  * change the result of the wait condition.
195  *
196  * The function returns 0 if the @timeout elapsed, and the remaining
197  * jiffies if the condition evaluated to true before the timeout elapsed.
198  */
199 #define swait_event_timeout(wq, condition, timeout)                     \
200 ({                                                                      \
201         long __ret = timeout;                                           \
202         if (!(condition))                                               \
203                 __swait_event_timeout(wq, condition, __ret);            \
204         __ret;                                                          \
205 })
206
207 #endif