]>
Commit | Line | Data |
---|---|---|
13b35686 PZI |
1 | #ifndef _LINUX_SWAIT_H |
2 | #define _LINUX_SWAIT_H | |
3 | ||
4 | #include <linux/list.h> | |
5 | #include <linux/stddef.h> | |
6 | #include <linux/spinlock.h> | |
7 | #include <asm/current.h> | |
8 | ||
9 | /* | |
10 | * Simple wait queues | |
11 | * | |
12 | * While these are very similar to the other/complex wait queues (wait.h) the | |
13 | * most important difference is that the simple waitqueue allows for | |
14 | * deterministic behaviour -- IOW it has strictly bounded IRQ and lock hold | |
15 | * times. | |
16 | * | |
17 | * In order to make this so, we had to drop a fair number of features of the | |
18 | * other waitqueue code; notably: | |
19 | * | |
20 | * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE sleeps on the same waitqueue; | |
21 | * all wakeups are TASK_NORMAL in order to avoid O(n) lookups for the right | |
22 | * sleeper state. | |
23 | * | |
24 | * - the exclusive mode; because this requires preserving the list order | |
25 | * and this is hard. | |
26 | * | |
27 | * - custom wake functions; because you cannot give any guarantees about | |
28 | * random code. | |
29 | * | |
30 | * As a side effect of this; the data structures are slimmer. | |
31 | * | |
32 | * One would recommend using this wait queue where possible. | |
33 | */ | |
34 | ||
35 | struct task_struct; | |
36 | ||
37 | struct swait_queue_head { | |
38 | raw_spinlock_t lock; | |
39 | struct list_head task_list; | |
40 | }; | |
41 | ||
42 | struct swait_queue { | |
43 | struct task_struct *task; | |
44 | struct list_head task_list; | |
45 | }; | |
46 | ||
47 | #define __SWAITQUEUE_INITIALIZER(name) { \ | |
48 | .task = current, \ | |
49 | .task_list = LIST_HEAD_INIT((name).task_list), \ | |
50 | } | |
51 | ||
52 | #define DECLARE_SWAITQUEUE(name) \ | |
53 | struct swait_queue name = __SWAITQUEUE_INITIALIZER(name) | |
54 | ||
55 | #define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { \ | |
56 | .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \ | |
57 | .task_list = LIST_HEAD_INIT((name).task_list), \ | |
58 | } | |
59 | ||
60 | #define DECLARE_SWAIT_QUEUE_HEAD(name) \ | |
61 | struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name) | |
62 | ||
63 | extern void __init_swait_queue_head(struct swait_queue_head *q, const char *name, | |
64 | struct lock_class_key *key); | |
65 | ||
66 | #define init_swait_queue_head(q) \ | |
67 | do { \ | |
68 | static struct lock_class_key __key; \ | |
69 | __init_swait_queue_head((q), #q, &__key); \ | |
70 | } while (0) | |
71 | ||
72 | #ifdef CONFIG_LOCKDEP | |
73 | # define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) \ | |
74 | ({ init_swait_queue_head(&name); name; }) | |
75 | # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \ | |
76 | struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) | |
77 | #else | |
78 | # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) \ | |
79 | DECLARE_SWAIT_QUEUE_HEAD(name) | |
80 | #endif | |
81 | ||
8cd641e3 DB |
82 | /** |
83 | * swait_active -- locklessly test for waiters on the queue | |
84 | * @wq: the waitqueue to test for waiters | |
85 | * | |
86 | * returns true if the wait list is not empty | |
87 | * | |
88 | * NOTE: this function is lockless and requires care, incorrect usage _will_ | |
89 | * lead to sporadic and non-obvious failure. | |
90 | * | |
91 | * NOTE2: this function has the same above implications as regular waitqueues. | |
92 | * | |
93 | * Use either while holding swait_queue_head::lock or when used for wakeups | |
94 | * with an extra smp_mb() like: | |
95 | * | |
96 | * CPU0 - waker CPU1 - waiter | |
97 | * | |
98 | * for (;;) { | |
99 | * @cond = true; prepare_to_swait(&wq_head, &wait, state); | |
100 | * smp_mb(); // smp_mb() from set_current_state() | |
101 | * if (swait_active(wq_head)) if (@cond) | |
102 | * wake_up(wq_head); break; | |
103 | * schedule(); | |
104 | * } | |
105 | * finish_swait(&wq_head, &wait); | |
106 | * | |
107 | * Because without the explicit smp_mb() it's possible for the | |
108 | * swait_active() load to get hoisted over the @cond store such that we'll | |
109 | * observe an empty wait list while the waiter might not observe @cond. | |
110 | * This, in turn, can trigger missing wakeups. | |
111 | * | |
112 | * Also note that this 'optimization' trades a spin_lock() for an smp_mb(), | |
113 | * which (when the lock is uncontended) are of roughly equal cost. | |
114 | */ | |
115 | static inline int swait_active(struct swait_queue_head *wq) | |
116 | { | |
117 | return !list_empty(&wq->task_list); | |
118 | } | |
119 | ||
120 | /** | |
121 | * swq_has_sleeper - check if there are any waiting processes | |
122 | * @wq: the waitqueue to test for waiters | |
123 | * | |
124 | * Returns true if @wq has waiting processes | |
125 | * | |
126 | * Please refer to the comment for swait_active. | |
127 | */ | |
128 | static inline bool swq_has_sleeper(struct swait_queue_head *wq) | |
13b35686 | 129 | { |
8cd641e3 DB |
130 | /* |
131 | * We need to be sure we are in sync with the list_add() | |
132 | * modifications to the wait queue (task_list). | |
133 | * | |
134 | * This memory barrier should be paired with one on the | |
135 | * waiting side. | |
136 | */ | |
137 | smp_mb(); | |
138 | return swait_active(wq); | |
13b35686 PZI |
139 | } |
140 | ||
141 | extern void swake_up(struct swait_queue_head *q); | |
142 | extern void swake_up_all(struct swait_queue_head *q); | |
143 | extern void swake_up_locked(struct swait_queue_head *q); | |
144 | ||
145 | extern void __prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait); | |
146 | extern void prepare_to_swait(struct swait_queue_head *q, struct swait_queue *wait, int state); | |
147 | extern long prepare_to_swait_event(struct swait_queue_head *q, struct swait_queue *wait, int state); | |
148 | ||
149 | extern void __finish_swait(struct swait_queue_head *q, struct swait_queue *wait); | |
150 | extern void finish_swait(struct swait_queue_head *q, struct swait_queue *wait); | |
151 | ||
152 | /* as per ___wait_event() but for swait, therefore "exclusive == 0" */ | |
153 | #define ___swait_event(wq, condition, state, ret, cmd) \ | |
154 | ({ \ | |
155 | struct swait_queue __wait; \ | |
156 | long __ret = ret; \ | |
157 | \ | |
158 | INIT_LIST_HEAD(&__wait.task_list); \ | |
159 | for (;;) { \ | |
160 | long __int = prepare_to_swait_event(&wq, &__wait, state);\ | |
161 | \ | |
162 | if (condition) \ | |
163 | break; \ | |
164 | \ | |
165 | if (___wait_is_interruptible(state) && __int) { \ | |
166 | __ret = __int; \ | |
167 | break; \ | |
168 | } \ | |
169 | \ | |
170 | cmd; \ | |
171 | } \ | |
172 | finish_swait(&wq, &__wait); \ | |
173 | __ret; \ | |
174 | }) | |
175 | ||
176 | #define __swait_event(wq, condition) \ | |
177 | (void)___swait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \ | |
178 | schedule()) | |
179 | ||
180 | #define swait_event(wq, condition) \ | |
181 | do { \ | |
182 | if (condition) \ | |
183 | break; \ | |
184 | __swait_event(wq, condition); \ | |
185 | } while (0) | |
186 | ||
187 | #define __swait_event_timeout(wq, condition, timeout) \ | |
188 | ___swait_event(wq, ___wait_cond_timeout(condition), \ | |
189 | TASK_UNINTERRUPTIBLE, timeout, \ | |
190 | __ret = schedule_timeout(__ret)) | |
191 | ||
192 | #define swait_event_timeout(wq, condition, timeout) \ | |
193 | ({ \ | |
194 | long __ret = timeout; \ | |
195 | if (!___wait_cond_timeout(condition)) \ | |
196 | __ret = __swait_event_timeout(wq, condition, timeout); \ | |
197 | __ret; \ | |
198 | }) | |
199 | ||
200 | #define __swait_event_interruptible(wq, condition) \ | |
201 | ___swait_event(wq, condition, TASK_INTERRUPTIBLE, 0, \ | |
202 | schedule()) | |
203 | ||
204 | #define swait_event_interruptible(wq, condition) \ | |
205 | ({ \ | |
206 | int __ret = 0; \ | |
207 | if (!(condition)) \ | |
208 | __ret = __swait_event_interruptible(wq, condition); \ | |
209 | __ret; \ | |
210 | }) | |
211 | ||
212 | #define __swait_event_interruptible_timeout(wq, condition, timeout) \ | |
213 | ___swait_event(wq, ___wait_cond_timeout(condition), \ | |
214 | TASK_INTERRUPTIBLE, timeout, \ | |
215 | __ret = schedule_timeout(__ret)) | |
216 | ||
217 | #define swait_event_interruptible_timeout(wq, condition, timeout) \ | |
218 | ({ \ | |
219 | long __ret = timeout; \ | |
220 | if (!___wait_cond_timeout(condition)) \ | |
221 | __ret = __swait_event_interruptible_timeout(wq, \ | |
222 | condition, timeout); \ | |
223 | __ret; \ | |
224 | }) | |
225 | ||
352eee12 LR |
226 | #define __swait_event_idle(wq, condition) \ |
227 | (void)___swait_event(wq, condition, TASK_IDLE, 0, schedule()) | |
228 | ||
229 | /** | |
230 | * swait_event_idle - wait without system load contribution | |
231 | * @wq: the waitqueue to wait on | |
232 | * @condition: a C expression for the event to wait for | |
233 | * | |
234 | * The process is put to sleep (TASK_IDLE) until the @condition evaluates to | |
235 | * true. The @condition is checked each time the waitqueue @wq is woken up. | |
236 | * | |
237 | * This function is mostly used when a kthread or workqueue waits for some | |
238 | * condition and doesn't want to contribute to system load. Signals are | |
239 | * ignored. | |
240 | */ | |
241 | #define swait_event_idle(wq, condition) \ | |
242 | do { \ | |
243 | if (condition) \ | |
244 | break; \ | |
245 | __swait_event_idle(wq, condition); \ | |
246 | } while (0) | |
247 | ||
248 | #define __swait_event_idle_timeout(wq, condition, timeout) \ | |
249 | ___swait_event(wq, ___wait_cond_timeout(condition), \ | |
250 | TASK_IDLE, timeout, \ | |
251 | __ret = schedule_timeout(__ret)) | |
252 | ||
253 | /** | |
254 | * swait_event_idle_timeout - wait up to timeout without load contribution | |
255 | * @wq: the waitqueue to wait on | |
256 | * @condition: a C expression for the event to wait for | |
257 | * @timeout: timeout at which we'll give up in jiffies | |
258 | * | |
259 | * The process is put to sleep (TASK_IDLE) until the @condition evaluates to | |
260 | * true. The @condition is checked each time the waitqueue @wq is woken up. | |
261 | * | |
262 | * This function is mostly used when a kthread or workqueue waits for some | |
263 | * condition and doesn't want to contribute to system load. Signals are | |
264 | * ignored. | |
265 | * | |
266 | * Returns: | |
267 | * 0 if the @condition evaluated to %false after the @timeout elapsed, | |
268 | * 1 if the @condition evaluated to %true after the @timeout elapsed, | |
269 | * or the remaining jiffies (at least 1) if the @condition evaluated | |
270 | * to %true before the @timeout elapsed. | |
271 | */ | |
272 | #define swait_event_idle_timeout(wq, condition, timeout) \ | |
273 | ({ \ | |
274 | long __ret = timeout; \ | |
275 | if (!___wait_cond_timeout(condition)) \ | |
276 | __ret = __swait_event_idle_timeout(wq, \ | |
277 | condition, timeout); \ | |
278 | __ret; \ | |
279 | }) | |
280 | ||
13b35686 | 281 | #endif /* _LINUX_SWAIT_H */ |