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