blob: c6b249904702e0ce29c159a131b2b62345ace13a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_WAIT_H
2#define _LINUX_WAIT_H
3
4#define WNOHANG 0x00000001
5#define WUNTRACED 0x00000002
6#define WSTOPPED WUNTRACED
7#define WEXITED 0x00000004
8#define WCONTINUED 0x00000008
9#define WNOWAIT 0x01000000 /* Don't reap, just poll status. */
10
11#define __WNOTHREAD 0x20000000 /* Don't wait on children of other threads in this group */
12#define __WALL 0x40000000 /* Wait on all children, regardless of type */
13#define __WCLONE 0x80000000 /* Wait only on non-SIGCHLD children */
14
15/* First argument to waitid: */
16#define P_ALL 0
17#define P_PID 1
18#define P_PGID 2
19
20#ifdef __KERNEL__
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/list.h>
23#include <linux/stddef.h>
24#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/current.h>
26
27typedef struct __wait_queue wait_queue_t;
Peter Zijlstra7d478722009-09-14 19:55:44 +020028typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
29int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31struct __wait_queue {
32 unsigned int flags;
33#define WQ_FLAG_EXCLUSIVE 0x01
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070034 void *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 wait_queue_func_t func;
36 struct list_head task_list;
37};
38
39struct wait_bit_key {
40 void *flags;
41 int bit_nr;
42};
43
44struct wait_bit_queue {
45 struct wait_bit_key key;
46 wait_queue_t wait;
47};
48
49struct __wait_queue_head {
50 spinlock_t lock;
51 struct list_head task_list;
52};
53typedef struct __wait_queue_head wait_queue_head_t;
54
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080055struct task_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * Macros for declaration and initialisaton of the datatypes
59 */
60
61#define __WAITQUEUE_INITIALIZER(name, tsk) { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070062 .private = tsk, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .func = default_wake_function, \
64 .task_list = { NULL, NULL } }
65
66#define DECLARE_WAITQUEUE(name, tsk) \
67 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
68
69#define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
Ingo Molnare4d91912006-07-03 00:24:34 -070070 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .task_list = { &(name).task_list, &(name).task_list } }
72
73#define DECLARE_WAIT_QUEUE_HEAD(name) \
74 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
75
76#define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
77 { .flags = word, .bit_nr = bit, }
78
Peter Zijlstraf07fdec2011-12-13 13:20:54 +010079extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *);
Peter Zijlstra2fc39112009-08-10 12:33:05 +010080
81#define init_waitqueue_head(q) \
82 do { \
83 static struct lock_class_key __key; \
84 \
Peter Zijlstraf07fdec2011-12-13 13:20:54 +010085 __init_waitqueue_head((q), #q, &__key); \
Peter Zijlstra2fc39112009-08-10 12:33:05 +010086 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080088#ifdef CONFIG_LOCKDEP
89# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
90 ({ init_waitqueue_head(&name); name; })
91# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
92 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
93#else
94# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
95#endif
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
98{
99 q->flags = 0;
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700100 q->private = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 q->func = default_wake_function;
102}
103
104static inline void init_waitqueue_func_entry(wait_queue_t *q,
105 wait_queue_func_t func)
106{
107 q->flags = 0;
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700108 q->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 q->func = func;
110}
111
112static inline int waitqueue_active(wait_queue_head_t *q)
113{
114 return !list_empty(&q->task_list);
115}
116
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800117extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
118extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
119extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
122{
123 list_add(&new->task_list, &head->task_list);
124}
125
126/*
127 * Used for wake-one threads:
128 */
Changli Gaoa93d2f12010-05-07 14:33:26 +0800129static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
130 wait_queue_t *wait)
131{
132 wait->flags |= WQ_FLAG_EXCLUSIVE;
133 __add_wait_queue(q, wait);
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static inline void __add_wait_queue_tail(wait_queue_head_t *head,
Changli Gaoa93d2f12010-05-07 14:33:26 +0800137 wait_queue_t *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 list_add_tail(&new->task_list, &head->task_list);
140}
141
Changli Gaoa93d2f12010-05-07 14:33:26 +0800142static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q,
143 wait_queue_t *wait)
144{
145 wait->flags |= WQ_FLAG_EXCLUSIVE;
146 __add_wait_queue_tail(q, wait);
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static inline void __remove_wait_queue(wait_queue_head_t *head,
150 wait_queue_t *old)
151{
152 list_del(&old->task_list);
153}
154
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800155void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700156void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
157void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
158 void *key);
Thomas Gleixner63b20012011-12-01 00:04:00 +0100159void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700160void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800161void __wake_up_bit(wait_queue_head_t *, void *, int);
162int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
163int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
164void wake_up_bit(void *, int);
165int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
166int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
167wait_queue_head_t *bit_waitqueue(void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500169#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
170#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
171#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
Thomas Gleixner63b20012011-12-01 00:04:00 +0100172#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
173#define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
176#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
177#define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500178#define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800180/*
Davide Libenzic0da3772009-03-31 15:24:20 -0700181 * Wakeup macros to be used to report events to the targets.
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800182 */
Davide Libenzic0da3772009-03-31 15:24:20 -0700183#define wake_up_poll(x, m) \
184 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
185#define wake_up_locked_poll(x, m) \
186 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
187#define wake_up_interruptible_poll(x, m) \
188 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
189#define wake_up_interruptible_sync_poll(x, m) \
190 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
Peter Zijlstra0ccf8312008-02-04 22:27:20 -0800191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192#define __wait_event(wq, condition) \
193do { \
194 DEFINE_WAIT(__wait); \
195 \
196 for (;;) { \
197 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
198 if (condition) \
199 break; \
200 schedule(); \
201 } \
202 finish_wait(&wq, &__wait); \
203} while (0)
204
205/**
206 * wait_event - sleep until a condition gets true
207 * @wq: the waitqueue to wait on
208 * @condition: a C expression for the event to wait for
209 *
210 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
211 * @condition evaluates to true. The @condition is checked each time
212 * the waitqueue @wq is woken up.
213 *
214 * wake_up() has to be called after changing any variable that could
215 * change the result of the wait condition.
216 */
217#define wait_event(wq, condition) \
218do { \
219 if (condition) \
220 break; \
221 __wait_event(wq, condition); \
222} while (0)
223
224#define __wait_event_timeout(wq, condition, ret) \
225do { \
226 DEFINE_WAIT(__wait); \
227 \
228 for (;;) { \
229 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
230 if (condition) \
231 break; \
232 ret = schedule_timeout(ret); \
233 if (!ret) \
234 break; \
235 } \
Imre Deak954dc412013-05-24 15:55:09 -0700236 if (!ret && (condition)) \
237 ret = 1; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 finish_wait(&wq, &__wait); \
239} while (0)
240
241/**
242 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
243 * @wq: the waitqueue to wait on
244 * @condition: a C expression for the event to wait for
245 * @timeout: timeout, in jiffies
246 *
247 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
248 * @condition evaluates to true. The @condition is checked each time
249 * the waitqueue @wq is woken up.
250 *
251 * wake_up() has to be called after changing any variable that could
252 * change the result of the wait condition.
253 *
Imre Deak954dc412013-05-24 15:55:09 -0700254 * The function returns 0 if the @timeout elapsed, or the remaining
255 * jiffies (at least 1) if the @condition evaluated to %true before
256 * the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258#define wait_event_timeout(wq, condition, timeout) \
259({ \
260 long __ret = timeout; \
261 if (!(condition)) \
262 __wait_event_timeout(wq, condition, __ret); \
263 __ret; \
264})
265
266#define __wait_event_interruptible(wq, condition, ret) \
267do { \
268 DEFINE_WAIT(__wait); \
269 \
270 for (;;) { \
271 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
272 if (condition) \
273 break; \
274 if (!signal_pending(current)) { \
275 schedule(); \
276 continue; \
277 } \
278 ret = -ERESTARTSYS; \
279 break; \
280 } \
281 finish_wait(&wq, &__wait); \
282} while (0)
283
284/**
285 * wait_event_interruptible - sleep until a condition gets true
286 * @wq: the waitqueue to wait on
287 * @condition: a C expression for the event to wait for
288 *
289 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
290 * @condition evaluates to true or a signal is received.
291 * The @condition is checked each time the waitqueue @wq is woken up.
292 *
293 * wake_up() has to be called after changing any variable that could
294 * change the result of the wait condition.
295 *
296 * The function will return -ERESTARTSYS if it was interrupted by a
297 * signal and 0 if @condition evaluated to true.
298 */
299#define wait_event_interruptible(wq, condition) \
300({ \
301 int __ret = 0; \
302 if (!(condition)) \
303 __wait_event_interruptible(wq, condition, __ret); \
304 __ret; \
305})
306
307#define __wait_event_interruptible_timeout(wq, condition, ret) \
308do { \
309 DEFINE_WAIT(__wait); \
310 \
311 for (;;) { \
312 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
313 if (condition) \
314 break; \
315 if (!signal_pending(current)) { \
316 ret = schedule_timeout(ret); \
317 if (!ret) \
318 break; \
319 continue; \
320 } \
321 ret = -ERESTARTSYS; \
322 break; \
323 } \
Imre Deak954dc412013-05-24 15:55:09 -0700324 if (!ret && (condition)) \
325 ret = 1; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 finish_wait(&wq, &__wait); \
327} while (0)
328
329/**
330 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
331 * @wq: the waitqueue to wait on
332 * @condition: a C expression for the event to wait for
333 * @timeout: timeout, in jiffies
334 *
335 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
336 * @condition evaluates to true or a signal is received.
337 * The @condition is checked each time the waitqueue @wq is woken up.
338 *
339 * wake_up() has to be called after changing any variable that could
340 * change the result of the wait condition.
341 *
Imre Deak954dc412013-05-24 15:55:09 -0700342 * Returns:
343 * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
344 * a signal, or the remaining jiffies (at least 1) if the @condition
345 * evaluated to %true before the @timeout elapsed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
347#define wait_event_interruptible_timeout(wq, condition, timeout) \
348({ \
349 long __ret = timeout; \
350 if (!(condition)) \
351 __wait_event_interruptible_timeout(wq, condition, __ret); \
352 __ret; \
353})
354
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355#define __wait_io_event_interruptible(wq, condition, ret) \
356do { \
357 DEFINE_WAIT(__wait); \
358 \
359 for (;;) { \
360 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
361 if (condition) \
362 break; \
363 if (!signal_pending(current)) { \
364 io_schedule(); \
365 continue; \
366 } \
367 ret = -ERESTARTSYS; \
368 break; \
369 } \
370 finish_wait(&wq, &__wait); \
371} while (0)
372
373/**
374 * wait_io_event_interruptible - sleep until an io condition gets true
375 * @wq: the waitqueue to wait on
376 * @condition: a C expression for the event to wait for
377 *
378 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
379 * @condition evaluates to true or a signal is received.
380 * The @condition is checked each time the waitqueue @wq is woken up.
381 *
382 * wake_up() has to be called after changing any variable that could
383 * change the result of the wait condition.
384 *
385 * The function will return -ERESTARTSYS if it was interrupted by a
386 * signal and 0 if @condition evaluated to true.
387 */
388#define wait_io_event_interruptible(wq, condition) \
389({ \
390 int __ret = 0; \
391 if (!(condition)) \
392 __wait_io_event_interruptible(wq, condition, __ret); \
393 __ret; \
394})
395
396#define __wait_io_event_interruptible_timeout(wq, condition, ret) \
397do { \
398 DEFINE_WAIT(__wait); \
399 \
400 for (;;) { \
401 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
402 if (condition) \
403 break; \
404 if (!signal_pending(current)) { \
405 ret = io_schedule_timeout(ret); \
406 if (!ret) \
407 break; \
408 continue; \
409 } \
410 ret = -ERESTARTSYS; \
411 break; \
412 } \
413 finish_wait(&wq, &__wait); \
414} while (0)
415
416/**
417 * wait_io_event_interruptible_timeout - sleep until an io condition gets true or a timeout elapses
418 * @wq: the waitqueue to wait on
419 * @condition: a C expression for the event to wait for
420 * @timeout: timeout, in jiffies
421 *
422 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
423 * @condition evaluates to true or a signal is received.
424 * The @condition is checked each time the waitqueue @wq is woken up.
425 *
426 * wake_up() has to be called after changing any variable that could
427 * change the result of the wait condition.
428 *
429 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
430 * was interrupted by a signal, and the remaining jiffies otherwise
431 * if the condition evaluated to true before the timeout elapsed.
432 */
433
434#define wait_io_event_interruptible_timeout(wq, condition, timeout) \
435({ \
436 long __ret = timeout; \
437 if (!(condition)) \
438 __wait_io_event_interruptible_timeout(wq, condition, __ret); \
439 __ret; \
440})
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#define __wait_event_interruptible_exclusive(wq, condition, ret) \
443do { \
444 DEFINE_WAIT(__wait); \
445 \
446 for (;;) { \
447 prepare_to_wait_exclusive(&wq, &__wait, \
448 TASK_INTERRUPTIBLE); \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800449 if (condition) { \
450 finish_wait(&wq, &__wait); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break; \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800452 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (!signal_pending(current)) { \
454 schedule(); \
455 continue; \
456 } \
457 ret = -ERESTARTSYS; \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800458 abort_exclusive_wait(&wq, &__wait, \
459 TASK_INTERRUPTIBLE, NULL); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break; \
461 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462} while (0)
463
464#define wait_event_interruptible_exclusive(wq, condition) \
465({ \
466 int __ret = 0; \
467 if (!(condition)) \
468 __wait_event_interruptible_exclusive(wq, condition, __ret);\
469 __ret; \
470})
471
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200472
473#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
474({ \
475 int __ret = 0; \
476 DEFINE_WAIT(__wait); \
477 if (exclusive) \
478 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
479 do { \
480 if (likely(list_empty(&__wait.task_list))) \
481 __add_wait_queue_tail(&(wq), &__wait); \
482 set_current_state(TASK_INTERRUPTIBLE); \
483 if (signal_pending(current)) { \
484 __ret = -ERESTARTSYS; \
485 break; \
486 } \
487 if (irq) \
488 spin_unlock_irq(&(wq).lock); \
489 else \
490 spin_unlock(&(wq).lock); \
491 schedule(); \
492 if (irq) \
493 spin_lock_irq(&(wq).lock); \
494 else \
495 spin_lock(&(wq).lock); \
496 } while (!(condition)); \
497 __remove_wait_queue(&(wq), &__wait); \
498 __set_current_state(TASK_RUNNING); \
499 __ret; \
500})
501
502
503/**
504 * wait_event_interruptible_locked - sleep until a condition gets true
505 * @wq: the waitqueue to wait on
506 * @condition: a C expression for the event to wait for
507 *
508 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
509 * @condition evaluates to true or a signal is received.
510 * The @condition is checked each time the waitqueue @wq is woken up.
511 *
512 * It must be called with wq.lock being held. This spinlock is
513 * unlocked while sleeping but @condition testing is done while lock
514 * is held and when this macro exits the lock is held.
515 *
516 * The lock is locked/unlocked using spin_lock()/spin_unlock()
517 * functions which must match the way they are locked/unlocked outside
518 * of this macro.
519 *
520 * wake_up_locked() has to be called after changing any variable that could
521 * change the result of the wait condition.
522 *
523 * The function will return -ERESTARTSYS if it was interrupted by a
524 * signal and 0 if @condition evaluated to true.
525 */
526#define wait_event_interruptible_locked(wq, condition) \
527 ((condition) \
528 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
529
530/**
531 * wait_event_interruptible_locked_irq - sleep until a condition gets true
532 * @wq: the waitqueue to wait on
533 * @condition: a C expression for the event to wait for
534 *
535 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
536 * @condition evaluates to true or a signal is received.
537 * The @condition is checked each time the waitqueue @wq is woken up.
538 *
539 * It must be called with wq.lock being held. This spinlock is
540 * unlocked while sleeping but @condition testing is done while lock
541 * is held and when this macro exits the lock is held.
542 *
543 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
544 * functions which must match the way they are locked/unlocked outside
545 * of this macro.
546 *
547 * wake_up_locked() has to be called after changing any variable that could
548 * change the result of the wait condition.
549 *
550 * The function will return -ERESTARTSYS if it was interrupted by a
551 * signal and 0 if @condition evaluated to true.
552 */
553#define wait_event_interruptible_locked_irq(wq, condition) \
554 ((condition) \
555 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
556
557/**
558 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
559 * @wq: the waitqueue to wait on
560 * @condition: a C expression for the event to wait for
561 *
562 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
563 * @condition evaluates to true or a signal is received.
564 * The @condition is checked each time the waitqueue @wq is woken up.
565 *
566 * It must be called with wq.lock being held. This spinlock is
567 * unlocked while sleeping but @condition testing is done while lock
568 * is held and when this macro exits the lock is held.
569 *
570 * The lock is locked/unlocked using spin_lock()/spin_unlock()
571 * functions which must match the way they are locked/unlocked outside
572 * of this macro.
573 *
574 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
575 * set thus when other process waits process on the list if this
576 * process is awaken further processes are not considered.
577 *
578 * wake_up_locked() has to be called after changing any variable that could
579 * change the result of the wait condition.
580 *
581 * The function will return -ERESTARTSYS if it was interrupted by a
582 * signal and 0 if @condition evaluated to true.
583 */
584#define wait_event_interruptible_exclusive_locked(wq, condition) \
585 ((condition) \
586 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
587
588/**
589 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
590 * @wq: the waitqueue to wait on
591 * @condition: a C expression for the event to wait for
592 *
593 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
594 * @condition evaluates to true or a signal is received.
595 * The @condition is checked each time the waitqueue @wq is woken up.
596 *
597 * It must be called with wq.lock being held. This spinlock is
598 * unlocked while sleeping but @condition testing is done while lock
599 * is held and when this macro exits the lock is held.
600 *
601 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
602 * functions which must match the way they are locked/unlocked outside
603 * of this macro.
604 *
605 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
606 * set thus when other process waits process on the list if this
607 * process is awaken further processes are not considered.
608 *
609 * wake_up_locked() has to be called after changing any variable that could
610 * change the result of the wait condition.
611 *
612 * The function will return -ERESTARTSYS if it was interrupted by a
613 * signal and 0 if @condition evaluated to true.
614 */
615#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
616 ((condition) \
617 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
618
619
Martin Peschke09c75652013-08-22 17:45:36 +0200620#define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
621 lock, ret) \
622do { \
623 DEFINE_WAIT(__wait); \
624 \
625 for (;;) { \
626 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
627 if (condition) \
628 break; \
629 if (signal_pending(current)) { \
630 ret = -ERESTARTSYS; \
631 break; \
632 } \
633 spin_unlock_irq(&lock); \
634 ret = schedule_timeout(ret); \
635 spin_lock_irq(&lock); \
636 if (!ret) \
637 break; \
638 } \
639 finish_wait(&wq, &__wait); \
640} while (0)
641
642/**
643 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets true or a timeout elapses.
644 * The condition is checked under the lock. This is expected
645 * to be called with the lock taken.
646 * @wq: the waitqueue to wait on
647 * @condition: a C expression for the event to wait for
648 * @lock: a locked spinlock_t, which will be released before schedule()
649 * and reacquired afterwards.
650 * @timeout: timeout, in jiffies
651 *
652 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
653 * @condition evaluates to true or signal is received. The @condition is
654 * checked each time the waitqueue @wq is woken up.
655 *
656 * wake_up() has to be called after changing any variable that could
657 * change the result of the wait condition.
658 *
659 * This is supposed to be called while holding the lock. The lock is
660 * dropped before going to sleep and is reacquired afterwards.
661 *
662 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
663 * was interrupted by a signal, and the remaining jiffies otherwise
664 * if the condition evaluated to true before the timeout elapsed.
665 */
666#define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
667 timeout) \
668({ \
669 int __ret = timeout; \
670 \
671 if (!(condition)) \
672 __wait_event_interruptible_lock_irq_timeout( \
673 wq, condition, lock, __ret); \
674 __ret; \
675})
676
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200677
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500678#define __wait_event_killable(wq, condition, ret) \
679do { \
680 DEFINE_WAIT(__wait); \
681 \
682 for (;;) { \
683 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
684 if (condition) \
685 break; \
686 if (!fatal_signal_pending(current)) { \
687 schedule(); \
688 continue; \
689 } \
690 ret = -ERESTARTSYS; \
691 break; \
692 } \
693 finish_wait(&wq, &__wait); \
694} while (0)
695
696/**
697 * wait_event_killable - sleep until a condition gets true
698 * @wq: the waitqueue to wait on
699 * @condition: a C expression for the event to wait for
700 *
701 * The process is put to sleep (TASK_KILLABLE) until the
702 * @condition evaluates to true or a signal is received.
703 * The @condition is checked each time the waitqueue @wq is woken up.
704 *
705 * wake_up() has to be called after changing any variable that could
706 * change the result of the wait condition.
707 *
708 * The function will return -ERESTARTSYS if it was interrupted by a
709 * signal and 0 if @condition evaluated to true.
710 */
711#define wait_event_killable(wq, condition) \
712({ \
713 int __ret = 0; \
714 if (!(condition)) \
715 __wait_event_killable(wq, condition, __ret); \
716 __ret; \
717})
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * These are the old interfaces to sleep waiting for an event.
Ingo Molnar0fec1712007-07-09 18:52:01 +0200721 * They are racy. DO NOT use them, use the wait_event* interfaces above.
722 * We plan to remove these interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 */
Ingo Molnar0fec1712007-07-09 18:52:01 +0200724extern void sleep_on(wait_queue_head_t *q);
725extern long sleep_on_timeout(wait_queue_head_t *q,
726 signed long timeout);
727extern void interruptible_sleep_on(wait_queue_head_t *q);
728extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
729 signed long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731/*
732 * Waitqueues which are removed from the waitqueue_head at wakeup time
733 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800734void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
735void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
736void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Johannes Weiner777c6c52009-02-04 15:12:14 -0800737void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
738 unsigned int mode, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
740int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
741
Eric Dumazetbf368e42009-04-28 02:24:21 -0700742#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700744 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -0700745 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200746 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
Eric Dumazetbf368e42009-04-28 02:24:21 -0700749#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#define DEFINE_WAIT_BIT(name, word, bit) \
752 struct wait_bit_queue name = { \
753 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
754 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700755 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 .func = wake_bit_function, \
757 .task_list = \
758 LIST_HEAD_INIT((name).wait.task_list), \
759 }, \
760 }
761
762#define init_wait(wait) \
763 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700764 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 (wait)->func = autoremove_wake_function; \
766 INIT_LIST_HEAD(&(wait)->task_list); \
Evgeny Kuznetsov231d0ae2010-10-05 12:47:57 +0400767 (wait)->flags = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 } while (0)
769
770/**
771 * wait_on_bit - wait for a bit to be cleared
772 * @word: the word being waited on, a kernel virtual address
773 * @bit: the bit of the word being waited on
774 * @action: the function used to sleep, which may take special actions
775 * @mode: the task state to sleep in
776 *
777 * There is a standard hashed waitqueue table for generic use. This
778 * is the part of the hashtable's accessor API that waits on a bit.
779 * For instance, if one were to have waiters on a bitflag, one would
780 * call wait_on_bit() in threads waiting for the bit to clear.
781 * One uses wait_on_bit() where one is waiting for the bit to clear,
782 * but has no intention of setting it.
783 */
784static inline int wait_on_bit(void *word, int bit,
785 int (*action)(void *), unsigned mode)
786{
787 if (!test_bit(bit, word))
788 return 0;
789 return out_of_line_wait_on_bit(word, bit, action, mode);
790}
791
792/**
793 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
794 * @word: the word being waited on, a kernel virtual address
795 * @bit: the bit of the word being waited on
796 * @action: the function used to sleep, which may take special actions
797 * @mode: the task state to sleep in
798 *
799 * There is a standard hashed waitqueue table for generic use. This
800 * is the part of the hashtable's accessor API that waits on a bit
801 * when one intends to set it, for instance, trying to lock bitflags.
802 * For instance, if one were to have waiters trying to set bitflag
803 * and waiting for it to clear before setting it, one would call
804 * wait_on_bit() in threads waiting to be able to set the bit.
805 * One uses wait_on_bit_lock() where one is waiting for the bit to
806 * clear with the intention of setting it, and when done, clearing it.
807 */
808static inline int wait_on_bit_lock(void *word, int bit,
809 int (*action)(void *), unsigned mode)
810{
811 if (!test_and_set_bit(bit, word))
812 return 0;
813 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
814}
815
816#endif /* __KERNEL__ */
817
818#endif