blob: 6c6c20eca3ddfa90775ea7cfe2e6610717081afa [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
355#define __wait_event_interruptible_exclusive(wq, condition, ret) \
356do { \
357 DEFINE_WAIT(__wait); \
358 \
359 for (;;) { \
360 prepare_to_wait_exclusive(&wq, &__wait, \
361 TASK_INTERRUPTIBLE); \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800362 if (condition) { \
363 finish_wait(&wq, &__wait); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break; \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800365 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (!signal_pending(current)) { \
367 schedule(); \
368 continue; \
369 } \
370 ret = -ERESTARTSYS; \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800371 abort_exclusive_wait(&wq, &__wait, \
372 TASK_INTERRUPTIBLE, NULL); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break; \
374 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375} while (0)
376
377#define wait_event_interruptible_exclusive(wq, condition) \
378({ \
379 int __ret = 0; \
380 if (!(condition)) \
381 __wait_event_interruptible_exclusive(wq, condition, __ret);\
382 __ret; \
383})
384
Michal Nazarewicz22c43c82010-05-05 12:53:11 +0200385
386#define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \
387({ \
388 int __ret = 0; \
389 DEFINE_WAIT(__wait); \
390 if (exclusive) \
391 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
392 do { \
393 if (likely(list_empty(&__wait.task_list))) \
394 __add_wait_queue_tail(&(wq), &__wait); \
395 set_current_state(TASK_INTERRUPTIBLE); \
396 if (signal_pending(current)) { \
397 __ret = -ERESTARTSYS; \
398 break; \
399 } \
400 if (irq) \
401 spin_unlock_irq(&(wq).lock); \
402 else \
403 spin_unlock(&(wq).lock); \
404 schedule(); \
405 if (irq) \
406 spin_lock_irq(&(wq).lock); \
407 else \
408 spin_lock(&(wq).lock); \
409 } while (!(condition)); \
410 __remove_wait_queue(&(wq), &__wait); \
411 __set_current_state(TASK_RUNNING); \
412 __ret; \
413})
414
415
416/**
417 * wait_event_interruptible_locked - sleep until a condition gets true
418 * @wq: the waitqueue to wait on
419 * @condition: a C expression for the event to wait for
420 *
421 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
422 * @condition evaluates to true or a signal is received.
423 * The @condition is checked each time the waitqueue @wq is woken up.
424 *
425 * It must be called with wq.lock being held. This spinlock is
426 * unlocked while sleeping but @condition testing is done while lock
427 * is held and when this macro exits the lock is held.
428 *
429 * The lock is locked/unlocked using spin_lock()/spin_unlock()
430 * functions which must match the way they are locked/unlocked outside
431 * of this macro.
432 *
433 * wake_up_locked() has to be called after changing any variable that could
434 * change the result of the wait condition.
435 *
436 * The function will return -ERESTARTSYS if it was interrupted by a
437 * signal and 0 if @condition evaluated to true.
438 */
439#define wait_event_interruptible_locked(wq, condition) \
440 ((condition) \
441 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0))
442
443/**
444 * wait_event_interruptible_locked_irq - sleep until a condition gets true
445 * @wq: the waitqueue to wait on
446 * @condition: a C expression for the event to wait for
447 *
448 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
449 * @condition evaluates to true or a signal is received.
450 * The @condition is checked each time the waitqueue @wq is woken up.
451 *
452 * It must be called with wq.lock being held. This spinlock is
453 * unlocked while sleeping but @condition testing is done while lock
454 * is held and when this macro exits the lock is held.
455 *
456 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
457 * functions which must match the way they are locked/unlocked outside
458 * of this macro.
459 *
460 * wake_up_locked() has to be called after changing any variable that could
461 * change the result of the wait condition.
462 *
463 * The function will return -ERESTARTSYS if it was interrupted by a
464 * signal and 0 if @condition evaluated to true.
465 */
466#define wait_event_interruptible_locked_irq(wq, condition) \
467 ((condition) \
468 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1))
469
470/**
471 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
472 * @wq: the waitqueue to wait on
473 * @condition: a C expression for the event to wait for
474 *
475 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
476 * @condition evaluates to true or a signal is received.
477 * The @condition is checked each time the waitqueue @wq is woken up.
478 *
479 * It must be called with wq.lock being held. This spinlock is
480 * unlocked while sleeping but @condition testing is done while lock
481 * is held and when this macro exits the lock is held.
482 *
483 * The lock is locked/unlocked using spin_lock()/spin_unlock()
484 * functions which must match the way they are locked/unlocked outside
485 * of this macro.
486 *
487 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
488 * set thus when other process waits process on the list if this
489 * process is awaken further processes are not considered.
490 *
491 * wake_up_locked() has to be called after changing any variable that could
492 * change the result of the wait condition.
493 *
494 * The function will return -ERESTARTSYS if it was interrupted by a
495 * signal and 0 if @condition evaluated to true.
496 */
497#define wait_event_interruptible_exclusive_locked(wq, condition) \
498 ((condition) \
499 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0))
500
501/**
502 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
503 * @wq: the waitqueue to wait on
504 * @condition: a C expression for the event to wait for
505 *
506 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
507 * @condition evaluates to true or a signal is received.
508 * The @condition is checked each time the waitqueue @wq is woken up.
509 *
510 * It must be called with wq.lock being held. This spinlock is
511 * unlocked while sleeping but @condition testing is done while lock
512 * is held and when this macro exits the lock is held.
513 *
514 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
515 * functions which must match the way they are locked/unlocked outside
516 * of this macro.
517 *
518 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
519 * set thus when other process waits process on the list if this
520 * process is awaken further processes are not considered.
521 *
522 * wake_up_locked() has to be called after changing any variable that could
523 * change the result of the wait condition.
524 *
525 * The function will return -ERESTARTSYS if it was interrupted by a
526 * signal and 0 if @condition evaluated to true.
527 */
528#define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
529 ((condition) \
530 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1))
531
532
533
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500534#define __wait_event_killable(wq, condition, ret) \
535do { \
536 DEFINE_WAIT(__wait); \
537 \
538 for (;;) { \
539 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
540 if (condition) \
541 break; \
542 if (!fatal_signal_pending(current)) { \
543 schedule(); \
544 continue; \
545 } \
546 ret = -ERESTARTSYS; \
547 break; \
548 } \
549 finish_wait(&wq, &__wait); \
550} while (0)
551
552/**
553 * wait_event_killable - sleep until a condition gets true
554 * @wq: the waitqueue to wait on
555 * @condition: a C expression for the event to wait for
556 *
557 * The process is put to sleep (TASK_KILLABLE) until the
558 * @condition evaluates to true or a signal is received.
559 * The @condition is checked each time the waitqueue @wq is woken up.
560 *
561 * wake_up() 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_killable(wq, condition) \
568({ \
569 int __ret = 0; \
570 if (!(condition)) \
571 __wait_event_killable(wq, condition, __ret); \
572 __ret; \
573})
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * These are the old interfaces to sleep waiting for an event.
Ingo Molnar0fec1712007-07-09 18:52:01 +0200577 * They are racy. DO NOT use them, use the wait_event* interfaces above.
578 * We plan to remove these interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
Ingo Molnar0fec1712007-07-09 18:52:01 +0200580extern void sleep_on(wait_queue_head_t *q);
581extern long sleep_on_timeout(wait_queue_head_t *q,
582 signed long timeout);
583extern void interruptible_sleep_on(wait_queue_head_t *q);
584extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
585 signed long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587/*
588 * Waitqueues which are removed from the waitqueue_head at wakeup time
589 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800590void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
591void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
592void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Johannes Weiner777c6c52009-02-04 15:12:14 -0800593void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
594 unsigned int mode, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
596int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
597
Eric Dumazetbf368e42009-04-28 02:24:21 -0700598#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700600 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -0700601 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200602 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
Eric Dumazetbf368e42009-04-28 02:24:21 -0700605#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607#define DEFINE_WAIT_BIT(name, word, bit) \
608 struct wait_bit_queue name = { \
609 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
610 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700611 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 .func = wake_bit_function, \
613 .task_list = \
614 LIST_HEAD_INIT((name).wait.task_list), \
615 }, \
616 }
617
618#define init_wait(wait) \
619 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700620 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 (wait)->func = autoremove_wake_function; \
622 INIT_LIST_HEAD(&(wait)->task_list); \
Evgeny Kuznetsov231d0ae2010-10-05 12:47:57 +0400623 (wait)->flags = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 } while (0)
625
626/**
627 * wait_on_bit - wait for a bit to be cleared
628 * @word: the word being waited on, a kernel virtual address
629 * @bit: the bit of the word being waited on
630 * @action: the function used to sleep, which may take special actions
631 * @mode: the task state to sleep in
632 *
633 * There is a standard hashed waitqueue table for generic use. This
634 * is the part of the hashtable's accessor API that waits on a bit.
635 * For instance, if one were to have waiters on a bitflag, one would
636 * call wait_on_bit() in threads waiting for the bit to clear.
637 * One uses wait_on_bit() where one is waiting for the bit to clear,
638 * but has no intention of setting it.
639 */
640static inline int wait_on_bit(void *word, int bit,
641 int (*action)(void *), unsigned mode)
642{
643 if (!test_bit(bit, word))
644 return 0;
645 return out_of_line_wait_on_bit(word, bit, action, mode);
646}
647
648/**
649 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
650 * @word: the word being waited on, a kernel virtual address
651 * @bit: the bit of the word being waited on
652 * @action: the function used to sleep, which may take special actions
653 * @mode: the task state to sleep in
654 *
655 * There is a standard hashed waitqueue table for generic use. This
656 * is the part of the hashtable's accessor API that waits on a bit
657 * when one intends to set it, for instance, trying to lock bitflags.
658 * For instance, if one were to have waiters trying to set bitflag
659 * and waiting for it to clear before setting it, one would call
660 * wait_on_bit() in threads waiting to be able to set the bit.
661 * One uses wait_on_bit_lock() where one is waiting for the bit to
662 * clear with the intention of setting it, and when done, clearing it.
663 */
664static inline int wait_on_bit_lock(void *word, int bit,
665 int (*action)(void *), unsigned mode)
666{
667 if (!test_and_set_bit(bit, word))
668 return 0;
669 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
670}
671
672#endif /* __KERNEL__ */
673
674#endif