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