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