blob: 68e3a628e1578440928cde498dd357ec7b0447d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_WAIT_H
2#define _LINUX_WAIT_H
3
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/list.h>
6#include <linux/stddef.h>
7#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <asm/current.h>
David Howells607ca462012-10-13 10:46:48 +01009#include <uapi/linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11typedef struct __wait_queue wait_queue_t;
Peter Zijlstra7d478722009-09-14 19:55:44 +020012typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
13int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15struct __wait_queue {
16 unsigned int flags;
17#define WQ_FLAG_EXCLUSIVE 0x01
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070018 void *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 wait_queue_func_t func;
20 struct list_head task_list;
21};
22
23struct wait_bit_key {
24 void *flags;
25 int bit_nr;
David Howellscb655372013-05-10 19:50:26 +010026#define WAIT_ATOMIC_T_BIT_NR -1
Linus Torvalds1da177e2005-04-16 15:20:36 -070027};
28
29struct wait_bit_queue {
30 struct wait_bit_key key;
31 wait_queue_t wait;
32};
33
34struct __wait_queue_head {
35 spinlock_t lock;
36 struct list_head task_list;
37};
38typedef struct __wait_queue_head wait_queue_head_t;
39
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080040struct task_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 * Macros for declaration and initialisaton of the datatypes
44 */
45
46#define __WAITQUEUE_INITIALIZER(name, tsk) { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070047 .private = tsk, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 .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) { \
Ingo Molnare4d91912006-07-03 00:24:34 -070055 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .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
David Howellscb655372013-05-10 19:50:26 +010064#define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
65 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
66
Peter Zijlstraf07fdec2011-12-13 13:20:54 +010067extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
Peter Zijlstra2fc39112009-08-10 12:33:05 +010068
69#define init_waitqueue_head(q) \
70 do { \
71 static struct lock_class_key __key; \
72 \
Peter Zijlstraf07fdec2011-12-13 13:20:54 +010073 __init_waitqueue_head((q), #q, &__key); \
Peter Zijlstra2fc39112009-08-10 12:33:05 +010074 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080076#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
86{
87 q->flags = 0;
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070088 q->private = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 q->func = default_wake_function;
90}
91
92static inline void init_waitqueue_func_entry(wait_queue_t *q,
93 wait_queue_func_t func)
94{
95 q->flags = 0;
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070096 q->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 q->func = func;
98}
99
100static inline int waitqueue_active(wait_queue_head_t *q)
101{
102 return !list_empty(&q->task_list);
103}
104
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800105extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
106extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
107extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109static 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 */
Changli Gaoa93d2f12010-05-07 14:33:26 +0800117static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static inline void __add_wait_queue_tail(wait_queue_head_t *head,
Changli Gaoa93d2f12010-05-07 14:33:26 +0800125 wait_queue_t *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 list_add_tail(&new->task_list, &head->task_list);
128}
129
Changli Gaoa93d2f12010-05-07 14:33:26 +0800130static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static inline void __remove_wait_queue(wait_queue_head_t *head,
138 wait_queue_t *old)
139{
140 list_del(&old->task_list);
141}
142
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800143void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700144void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
145void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
146 void *key);
Thomas Gleixner63b20012011-12-01 00:04:00 +0100147void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700148void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800149void __wake_up_bit(wait_queue_head_t *, void *, int);
150int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
151int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
152void wake_up_bit(void *, int);
David Howellscb655372013-05-10 19:50:26 +0100153void wake_up_atomic_t(atomic_t *);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800154int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
155int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
David Howellscb655372013-05-10 19:50:26 +0100156int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800157wait_queue_head_t *bit_waitqueue(void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500159#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)
Thomas Gleixner63b20012011-12-01 00:04:00 +0100162#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)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#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)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500168#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800170/*
Davide Libenzic0da3772009-03-31 15:24:20 -0700171 * Wakeup macros to be used to report events to the targets.
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800172 */
Davide Libenzic0da3772009-03-31 15:24:20 -0700173#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))
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800181
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200182#define ___wait_cond_timeout(condition, ret) \
183({ \
184 bool __cond = (condition); \
185 if (__cond && !ret) \
186 ret = 1; \
187 __cond || !ret; \
188})
189
Peter Zijlstra41a14312013-10-02 11:22:21 +0200190#define ___wait_signal_pending(state) \
191 ((state == TASK_INTERRUPTIBLE && signal_pending(current)) || \
192 (state == TASK_KILLABLE && fatal_signal_pending(current)))
193
194#define ___wait_nop_ret int ret __always_unused
195
196#define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
197do { \
198 __label__ __out; \
199 DEFINE_WAIT(__wait); \
200 \
201 for (;;) { \
202 if (exclusive) \
203 prepare_to_wait_exclusive(&wq, &__wait, state); \
204 else \
205 prepare_to_wait(&wq, &__wait, state); \
206 \
207 if (condition) \
208 break; \
209 \
210 if (___wait_signal_pending(state)) { \
211 ret = -ERESTARTSYS; \
212 if (exclusive) { \
213 abort_exclusive_wait(&wq, &__wait, \
214 state, NULL); \
215 goto __out; \
216 } \
217 break; \
218 } \
219 \
220 cmd; \
221 } \
222 finish_wait(&wq, &__wait); \
223__out: ; \
224} while (0)
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#define __wait_event(wq, condition) \
Peter Zijlstra854267f2013-10-02 11:22:22 +0200227 ___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, \
228 ___wait_nop_ret, schedule())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230/**
231 * wait_event - sleep until a condition gets true
232 * @wq: the waitqueue to wait on
233 * @condition: a C expression for the event to wait for
234 *
235 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
236 * @condition evaluates to true. The @condition is checked each time
237 * the waitqueue @wq is woken up.
238 *
239 * wake_up() has to be called after changing any variable that could
240 * change the result of the wait condition.
241 */
242#define wait_event(wq, condition) \
243do { \
244 if (condition) \
245 break; \
246 __wait_event(wq, condition); \
247} while (0)
248
249#define __wait_event_timeout(wq, condition, ret) \
250do { \
251 DEFINE_WAIT(__wait); \
252 \
253 for (;;) { \
254 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200255 if (___wait_cond_timeout(condition, ret)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 break; \
257 ret = schedule_timeout(ret); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 } \
259 finish_wait(&wq, &__wait); \
260} while (0)
261
262/**
263 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
264 * @wq: the waitqueue to wait on
265 * @condition: a C expression for the event to wait for
266 * @timeout: timeout, in jiffies
267 *
268 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
269 * @condition evaluates to true. The @condition is checked each time
270 * the waitqueue @wq is woken up.
271 *
272 * wake_up() has to be called after changing any variable that could
273 * change the result of the wait condition.
274 *
Imre Deak4c663cf2013-05-24 15:55:09 -0700275 * The function returns 0 if the @timeout elapsed, or the remaining
276 * jiffies (at least 1) if the @condition evaluated to %true before
277 * the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279#define wait_event_timeout(wq, condition, timeout) \
280({ \
281 long __ret = timeout; \
282 if (!(condition)) \
283 __wait_event_timeout(wq, condition, __ret); \
284 __ret; \
285})
286
287#define __wait_event_interruptible(wq, condition, ret) \
288do { \
289 DEFINE_WAIT(__wait); \
290 \
291 for (;;) { \
292 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
293 if (condition) \
294 break; \
Peter Zijlstra2f2a2b62013-10-02 11:22:18 +0200295 if (signal_pending(current)) { \
296 ret = -ERESTARTSYS; \
297 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 } \
Peter Zijlstra2f2a2b62013-10-02 11:22:18 +0200299 schedule(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 } \
301 finish_wait(&wq, &__wait); \
302} while (0)
303
304/**
305 * wait_event_interruptible - sleep until a condition gets true
306 * @wq: the waitqueue to wait on
307 * @condition: a C expression for the event to wait for
308 *
309 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
310 * @condition evaluates to true or a signal is received.
311 * The @condition is checked each time the waitqueue @wq is woken up.
312 *
313 * wake_up() has to be called after changing any variable that could
314 * change the result of the wait condition.
315 *
316 * The function will return -ERESTARTSYS if it was interrupted by a
317 * signal and 0 if @condition evaluated to true.
318 */
319#define wait_event_interruptible(wq, condition) \
320({ \
321 int __ret = 0; \
322 if (!(condition)) \
323 __wait_event_interruptible(wq, condition, __ret); \
324 __ret; \
325})
326
327#define __wait_event_interruptible_timeout(wq, condition, ret) \
328do { \
329 DEFINE_WAIT(__wait); \
330 \
331 for (;;) { \
332 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200333 if (___wait_cond_timeout(condition, ret)) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break; \
Peter Zijlstra2f2a2b62013-10-02 11:22:18 +0200335 if (signal_pending(current)) { \
336 ret = -ERESTARTSYS; \
337 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 } \
Peter Zijlstra2f2a2b62013-10-02 11:22:18 +0200339 ret = schedule_timeout(ret); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 } \
341 finish_wait(&wq, &__wait); \
342} while (0)
343
344/**
345 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
346 * @wq: the waitqueue to wait on
347 * @condition: a C expression for the event to wait for
348 * @timeout: timeout, in jiffies
349 *
350 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
351 * @condition evaluates to true or a signal is received.
352 * The @condition is checked each time the waitqueue @wq is woken up.
353 *
354 * wake_up() has to be called after changing any variable that could
355 * change the result of the wait condition.
356 *
Imre Deak4c663cf2013-05-24 15:55:09 -0700357 * Returns:
358 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
359 * a signal, or the remaining jiffies (at least 1) if the @condition
360 * evaluated to %true before the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 */
362#define wait_event_interruptible_timeout(wq, condition, timeout) \
363({ \
364 long __ret = timeout; \
365 if (!(condition)) \
366 __wait_event_interruptible_timeout(wq, condition, __ret); \
367 __ret; \
368})
369
Kent Overstreet774a08b2013-05-07 16:18:43 -0700370#define __wait_event_hrtimeout(wq, condition, timeout, state) \
371({ \
372 int __ret = 0; \
373 DEFINE_WAIT(__wait); \
374 struct hrtimer_sleeper __t; \
375 \
376 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
377 HRTIMER_MODE_REL); \
378 hrtimer_init_sleeper(&__t, current); \
379 if ((timeout).tv64 != KTIME_MAX) \
380 hrtimer_start_range_ns(&__t.timer, timeout, \
381 current->timer_slack_ns, \
382 HRTIMER_MODE_REL); \
383 \
384 for (;;) { \
385 prepare_to_wait(&wq, &__wait, state); \
386 if (condition) \
387 break; \
388 if (state == TASK_INTERRUPTIBLE && \
389 signal_pending(current)) { \
390 __ret = -ERESTARTSYS; \
391 break; \
392 } \
393 if (!__t.task) { \
394 __ret = -ETIME; \
395 break; \
396 } \
397 schedule(); \
398 } \
399 \
400 hrtimer_cancel(&__t.timer); \
401 destroy_hrtimer_on_stack(&__t.timer); \
402 finish_wait(&wq, &__wait); \
403 __ret; \
404})
405
406/**
407 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
408 * @wq: the waitqueue to wait on
409 * @condition: a C expression for the event to wait for
410 * @timeout: timeout, as a ktime_t
411 *
412 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
413 * @condition evaluates to true or a signal is received.
414 * The @condition is checked each time the waitqueue @wq is woken up.
415 *
416 * wake_up() has to be called after changing any variable that could
417 * change the result of the wait condition.
418 *
419 * The function returns 0 if @condition became true, or -ETIME if the timeout
420 * elapsed.
421 */
422#define wait_event_hrtimeout(wq, condition, timeout) \
423({ \
424 int __ret = 0; \
425 if (!(condition)) \
426 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
427 TASK_UNINTERRUPTIBLE); \
428 __ret; \
429})
430
431/**
432 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
433 * @wq: the waitqueue to wait on
434 * @condition: a C expression for the event to wait for
435 * @timeout: timeout, as a ktime_t
436 *
437 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
438 * @condition evaluates to true or a signal is received.
439 * The @condition is checked each time the waitqueue @wq is woken up.
440 *
441 * wake_up() has to be called after changing any variable that could
442 * change the result of the wait condition.
443 *
444 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
445 * interrupted by a signal, or -ETIME if the timeout elapsed.
446 */
447#define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
448({ \
449 long __ret = 0; \
450 if (!(condition)) \
451 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
452 TASK_INTERRUPTIBLE); \
453 __ret; \
454})
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456#define __wait_event_interruptible_exclusive(wq, condition, ret) \
457do { \
Peter Zijlstrabb632bc2013-10-02 11:22:20 +0200458 __label__ __out; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 DEFINE_WAIT(__wait); \
460 \
461 for (;;) { \
462 prepare_to_wait_exclusive(&wq, &__wait, \
463 TASK_INTERRUPTIBLE); \
Peter Zijlstrabb632bc2013-10-02 11:22:20 +0200464 if (condition) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break; \
Peter Zijlstra2f2a2b62013-10-02 11:22:18 +0200466 if (signal_pending(current)) { \
467 ret = -ERESTARTSYS; \
468 abort_exclusive_wait(&wq, &__wait, \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800469 TASK_INTERRUPTIBLE, NULL); \
Peter Zijlstrabb632bc2013-10-02 11:22:20 +0200470 goto __out; \
Peter Zijlstra2f2a2b62013-10-02 11:22:18 +0200471 } \
472 schedule(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } \
Peter Zijlstrabb632bc2013-10-02 11:22:20 +0200474 finish_wait(&wq, &__wait); \
475__out: ; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476} while (0)
477
478#define wait_event_interruptible_exclusive(wq, condition) \
479({ \
480 int __ret = 0; \
481 if (!(condition)) \
482 __wait_event_interruptible_exclusive(wq, condition, __ret);\
483 __ret; \
484})
485
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200486
487#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
488({ \
489 int __ret = 0; \
490 DEFINE_WAIT(__wait); \
491 if (exclusive) \
492 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
493 do { \
494 if (likely(list_empty(&__wait.task_list))) \
495 __add_wait_queue_tail(&(wq), &__wait); \
496 set_current_state(TASK_INTERRUPTIBLE); \
497 if (signal_pending(current)) { \
498 __ret = -ERESTARTSYS; \
499 break; \
500 } \
501 if (irq) \
502 spin_unlock_irq(&(wq).lock); \
503 else \
504 spin_unlock(&(wq).lock); \
505 schedule(); \
506 if (irq) \
507 spin_lock_irq(&(wq).lock); \
508 else \
509 spin_lock(&(wq).lock); \
510 } while (!(condition)); \
511 __remove_wait_queue(&(wq), &__wait); \
512 __set_current_state(TASK_RUNNING); \
513 __ret; \
514})
515
516
517/**
518 * wait_event_interruptible_locked - 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()/spin_unlock()
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(wq, condition) \
541 ((condition) \
542 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
543
544/**
545 * wait_event_interruptible_locked_irq - sleep 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_irq()/spin_unlock_irq()
558 * functions which must match the way they are locked/unlocked outside
559 * of this macro.
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_locked_irq(wq, condition) \
568 ((condition) \
569 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
570
571/**
572 * wait_event_interruptible_exclusive_locked - sleep exclusively 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()/spin_unlock()
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(wq, condition) \
599 ((condition) \
600 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
601
602/**
603 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
604 * @wq: the waitqueue to wait on
605 * @condition: a C expression for the event to wait for
606 *
607 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
608 * @condition evaluates to true or a signal is received.
609 * The @condition is checked each time the waitqueue @wq is woken up.
610 *
611 * It must be called with wq.lock being held. This spinlock is
612 * unlocked while sleeping but @condition testing is done while lock
613 * is held and when this macro exits the lock is held.
614 *
615 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
616 * functions which must match the way they are locked/unlocked outside
617 * of this macro.
618 *
619 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
620 * set thus when other process waits process on the list if this
621 * process is awaken further processes are not considered.
622 *
623 * wake_up_locked() has to be called after changing any variable that could
624 * change the result of the wait condition.
625 *
626 * The function will return -ERESTARTSYS if it was interrupted by a
627 * signal and 0 if @condition evaluated to true.
628 */
629#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
630 ((condition) \
631 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
632
633
634
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500635#define __wait_event_killable(wq, condition, ret) \
636do { \
637 DEFINE_WAIT(__wait); \
638 \
639 for (;;) { \
640 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
641 if (condition) \
642 break; \
643 if (!fatal_signal_pending(current)) { \
644 schedule(); \
645 continue; \
646 } \
647 ret = -ERESTARTSYS; \
648 break; \
649 } \
650 finish_wait(&wq, &__wait); \
651} while (0)
652
653/**
654 * wait_event_killable - sleep until a condition gets true
655 * @wq: the waitqueue to wait on
656 * @condition: a C expression for the event to wait for
657 *
658 * The process is put to sleep (TASK_KILLABLE) until the
659 * @condition evaluates to true or a signal is received.
660 * The @condition is checked each time the waitqueue @wq is woken up.
661 *
662 * wake_up() has to be called after changing any variable that could
663 * change the result of the wait condition.
664 *
665 * The function will return -ERESTARTSYS if it was interrupted by a
666 * signal and 0 if @condition evaluated to true.
667 */
668#define wait_event_killable(wq, condition) \
669({ \
670 int __ret = 0; \
671 if (!(condition)) \
672 __wait_event_killable(wq, condition, __ret); \
673 __ret; \
674})
675
Lukas Czernereed8c022012-11-30 11:42:40 +0100676
677#define __wait_event_lock_irq(wq, condition, lock, cmd) \
678do { \
679 DEFINE_WAIT(__wait); \
680 \
681 for (;;) { \
682 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
683 if (condition) \
684 break; \
685 spin_unlock_irq(&lock); \
686 cmd; \
687 schedule(); \
688 spin_lock_irq(&lock); \
689 } \
690 finish_wait(&wq, &__wait); \
691} while (0)
692
693/**
694 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
695 * condition is checked under the lock. This
696 * is expected to be called with the lock
697 * taken.
698 * @wq: the waitqueue to wait on
699 * @condition: a C expression for the event to wait for
700 * @lock: a locked spinlock_t, which will be released before cmd
701 * and schedule() and reacquired afterwards.
702 * @cmd: a command which is invoked outside the critical section before
703 * sleep
704 *
705 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
706 * @condition evaluates to true. The @condition is checked each time
707 * the waitqueue @wq is woken up.
708 *
709 * wake_up() has to be called after changing any variable that could
710 * change the result of the wait condition.
711 *
712 * This is supposed to be called while holding the lock. The lock is
713 * dropped before invoking the cmd and going to sleep and is reacquired
714 * afterwards.
715 */
716#define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
717do { \
718 if (condition) \
719 break; \
720 __wait_event_lock_irq(wq, condition, lock, cmd); \
721} while (0)
722
723/**
724 * wait_event_lock_irq - sleep until a condition gets true. The
725 * condition is checked under the lock. This
726 * is expected to be called with the lock
727 * taken.
728 * @wq: the waitqueue to wait on
729 * @condition: a C expression for the event to wait for
730 * @lock: a locked spinlock_t, which will be released before schedule()
731 * and reacquired afterwards.
732 *
733 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
734 * @condition evaluates to true. The @condition is checked each time
735 * the waitqueue @wq is woken up.
736 *
737 * wake_up() has to be called after changing any variable that could
738 * change the result of the wait condition.
739 *
740 * This is supposed to be called while holding the lock. The lock is
741 * dropped before going to sleep and is reacquired afterwards.
742 */
743#define wait_event_lock_irq(wq, condition, lock) \
744do { \
745 if (condition) \
746 break; \
747 __wait_event_lock_irq(wq, condition, lock, ); \
748} while (0)
749
750
751#define __wait_event_interruptible_lock_irq(wq, condition, \
752 lock, ret, cmd) \
753do { \
754 DEFINE_WAIT(__wait); \
755 \
756 for (;;) { \
757 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
758 if (condition) \
759 break; \
760 if (signal_pending(current)) { \
761 ret = -ERESTARTSYS; \
762 break; \
763 } \
764 spin_unlock_irq(&lock); \
765 cmd; \
766 schedule(); \
767 spin_lock_irq(&lock); \
768 } \
769 finish_wait(&wq, &__wait); \
770} while (0)
771
772/**
773 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
774 * The condition is checked under the lock. This is expected to
775 * be called with the lock taken.
776 * @wq: the waitqueue to wait on
777 * @condition: a C expression for the event to wait for
778 * @lock: a locked spinlock_t, which will be released before cmd and
779 * schedule() and reacquired afterwards.
780 * @cmd: a command which is invoked outside the critical section before
781 * sleep
782 *
783 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
784 * @condition evaluates to true or a signal is received. The @condition is
785 * checked each time the waitqueue @wq is woken up.
786 *
787 * wake_up() has to be called after changing any variable that could
788 * change the result of the wait condition.
789 *
790 * This is supposed to be called while holding the lock. The lock is
791 * dropped before invoking the cmd and going to sleep and is reacquired
792 * afterwards.
793 *
794 * The macro will return -ERESTARTSYS if it was interrupted by a signal
795 * and 0 if @condition evaluated to true.
796 */
797#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
798({ \
799 int __ret = 0; \
800 \
801 if (!(condition)) \
802 __wait_event_interruptible_lock_irq(wq, condition, \
803 lock, __ret, cmd); \
804 __ret; \
805})
806
807/**
808 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
809 * The condition is checked under the lock. This is expected
810 * to be called with the lock taken.
811 * @wq: the waitqueue to wait on
812 * @condition: a C expression for the event to wait for
813 * @lock: a locked spinlock_t, which will be released before schedule()
814 * and reacquired afterwards.
815 *
816 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
817 * @condition evaluates to true or signal is received. The @condition is
818 * checked each time the waitqueue @wq is woken up.
819 *
820 * wake_up() has to be called after changing any variable that could
821 * change the result of the wait condition.
822 *
823 * This is supposed to be called while holding the lock. The lock is
824 * dropped before going to sleep and is reacquired afterwards.
825 *
826 * The macro will return -ERESTARTSYS if it was interrupted by a signal
827 * and 0 if @condition evaluated to true.
828 */
829#define wait_event_interruptible_lock_irq(wq, condition, lock) \
830({ \
831 int __ret = 0; \
832 \
833 if (!(condition)) \
834 __wait_event_interruptible_lock_irq(wq, condition, \
835 lock, __ret, ); \
836 __ret; \
837})
838
Martin Peschked79ff142013-08-22 17:45:36 +0200839#define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
840 lock, ret) \
841do { \
842 DEFINE_WAIT(__wait); \
843 \
844 for (;;) { \
845 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
Peter Zijlstra2953ef22013-10-02 11:22:19 +0200846 if (___wait_cond_timeout(condition, ret)) \
Martin Peschked79ff142013-08-22 17:45:36 +0200847 break; \
848 if (signal_pending(current)) { \
849 ret = -ERESTARTSYS; \
850 break; \
851 } \
852 spin_unlock_irq(&lock); \
853 ret = schedule_timeout(ret); \
854 spin_lock_irq(&lock); \
Martin Peschked79ff142013-08-22 17:45:36 +0200855 } \
856 finish_wait(&wq, &__wait); \
857} while (0)
858
859/**
860 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
861 * The condition is checked under the lock. This is expected
862 * to be called with the lock taken.
863 * @wq: the waitqueue to wait on
864 * @condition: a C expression for the event to wait for
865 * @lock: a locked spinlock_t, which will be released before schedule()
866 * and reacquired afterwards.
867 * @timeout: timeout, in jiffies
868 *
869 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
870 * @condition evaluates to true or signal is received. The @condition is
871 * checked each time the waitqueue @wq is woken up.
872 *
873 * wake_up() has to be called after changing any variable that could
874 * change the result of the wait condition.
875 *
876 * This is supposed to be called while holding the lock. The lock is
877 * dropped before going to sleep and is reacquired afterwards.
878 *
879 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
880 * was interrupted by a signal, and the remaining jiffies otherwise
881 * if the condition evaluated to true before the timeout elapsed.
882 */
883#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
884 timeout) \
885({ \
886 int __ret = timeout; \
887 \
888 if (!(condition)) \
889 __wait_event_interruptible_lock_irq_timeout( \
890 wq, condition, lock, __ret); \
891 __ret; \
892})
893
Lukas Czernereed8c022012-11-30 11:42:40 +0100894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 * These are the old interfaces to sleep waiting for an event.
Ingo Molnar0fec1712007-07-09 18:52:01 +0200897 * They are racy. DO NOT use them, use the wait_event* interfaces above.
898 * We plan to remove these interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 */
Ingo Molnar0fec1712007-07-09 18:52:01 +0200900extern void sleep_on(wait_queue_head_t *q);
901extern long sleep_on_timeout(wait_queue_head_t *q,
902 signed long timeout);
903extern void interruptible_sleep_on(wait_queue_head_t *q);
904extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
905 signed long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907/*
908 * Waitqueues which are removed from the waitqueue_head at wakeup time
909 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800910void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
911void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
912void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Johannes Weiner777c6c52009-02-04 15:12:14 -0800913void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
914 unsigned int mode, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
916int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
917
Eric Dumazetbf368e42009-04-28 02:24:21 -0700918#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700920 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -0700921 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200922 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
Eric Dumazetbf368e42009-04-28 02:24:21 -0700925#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927#define DEFINE_WAIT_BIT(name, word, bit) \
928 struct wait_bit_queue name = { \
929 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
930 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700931 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 .func = wake_bit_function, \
933 .task_list = \
934 LIST_HEAD_INIT((name).wait.task_list), \
935 }, \
936 }
937
938#define init_wait(wait) \
939 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700940 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 (wait)->func = autoremove_wake_function; \
942 INIT_LIST_HEAD(&(wait)->task_list); \
Evgeny Kuznetsov231d0ae2010-10-05 12:47:57 +0400943 (wait)->flags = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 } while (0)
945
946/**
947 * wait_on_bit - wait for a bit to be cleared
948 * @word: the word being waited on, a kernel virtual address
949 * @bit: the bit of the word being waited on
950 * @action: the function used to sleep, which may take special actions
951 * @mode: the task state to sleep in
952 *
953 * There is a standard hashed waitqueue table for generic use. This
954 * is the part of the hashtable's accessor API that waits on a bit.
955 * For instance, if one were to have waiters on a bitflag, one would
956 * call wait_on_bit() in threads waiting for the bit to clear.
957 * One uses wait_on_bit() where one is waiting for the bit to clear,
958 * but has no intention of setting it.
959 */
960static inline int wait_on_bit(void *word, int bit,
961 int (*action)(void *), unsigned mode)
962{
963 if (!test_bit(bit, word))
964 return 0;
965 return out_of_line_wait_on_bit(word, bit, action, mode);
966}
967
968/**
969 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
970 * @word: the word being waited on, a kernel virtual address
971 * @bit: the bit of the word being waited on
972 * @action: the function used to sleep, which may take special actions
973 * @mode: the task state to sleep in
974 *
975 * There is a standard hashed waitqueue table for generic use. This
976 * is the part of the hashtable's accessor API that waits on a bit
977 * when one intends to set it, for instance, trying to lock bitflags.
978 * For instance, if one were to have waiters trying to set bitflag
979 * and waiting for it to clear before setting it, one would call
980 * wait_on_bit() in threads waiting to be able to set the bit.
981 * One uses wait_on_bit_lock() where one is waiting for the bit to
982 * clear with the intention of setting it, and when done, clearing it.
983 */
984static inline int wait_on_bit_lock(void *word, int bit,
985 int (*action)(void *), unsigned mode)
986{
987 if (!test_and_set_bit(bit, word))
988 return 0;
989 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
990}
David Howellscb655372013-05-10 19:50:26 +0100991
992/**
993 * wait_on_atomic_t - Wait for an atomic_t to become 0
994 * @val: The atomic value being waited on, a kernel virtual address
995 * @action: the function used to sleep, which may take special actions
996 * @mode: the task state to sleep in
997 *
998 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
999 * the purpose of getting a waitqueue, but we set the key to a bit number
1000 * outside of the target 'word'.
1001 */
1002static inline
1003int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode)
1004{
1005 if (atomic_read(val) == 0)
1006 return 0;
1007 return out_of_line_wait_on_atomic_t(val, action, mode);
1008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010#endif