blob: 76d96d035ea03920ac919a13e2b255e93cc77f35 [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>
25#include <asm/system.h>
26#include <asm/current.h>
27
28typedef struct __wait_queue wait_queue_t;
Peter Zijlstra7d478722009-09-14 19:55:44 +020029typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
30int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32struct __wait_queue {
33 unsigned int flags;
34#define WQ_FLAG_EXCLUSIVE 0x01
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070035 void *private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 wait_queue_func_t func;
37 struct list_head task_list;
38};
39
40struct wait_bit_key {
41 void *flags;
42 int bit_nr;
43};
44
45struct wait_bit_queue {
46 struct wait_bit_key key;
47 wait_queue_t wait;
48};
49
50struct __wait_queue_head {
51 spinlock_t lock;
52 struct list_head task_list;
53};
54typedef struct __wait_queue_head wait_queue_head_t;
55
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080056struct task_struct;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
59 * Macros for declaration and initialisaton of the datatypes
60 */
61
62#define __WAITQUEUE_INITIALIZER(name, tsk) { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -070063 .private = tsk, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .func = default_wake_function, \
65 .task_list = { NULL, NULL } }
66
67#define DECLARE_WAITQUEUE(name, tsk) \
68 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
69
70#define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
Ingo Molnare4d91912006-07-03 00:24:34 -070071 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 .task_list = { &(name).task_list, &(name).task_list } }
73
74#define DECLARE_WAIT_QUEUE_HEAD(name) \
75 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
76
77#define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
78 { .flags = word, .bit_nr = bit, }
79
Peter Zijlstra2fc39112009-08-10 12:33:05 +010080extern void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *);
81
82#define init_waitqueue_head(q) \
83 do { \
84 static struct lock_class_key __key; \
85 \
86 __init_waitqueue_head((q), &__key); \
87 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Peter Zijlstra7259f0d2006-10-29 22:46:36 -080089#ifdef CONFIG_LOCKDEP
90# define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
91 ({ init_waitqueue_head(&name); name; })
92# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
93 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
94#else
95# define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
96#endif
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
99{
100 q->flags = 0;
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700101 q->private = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 q->func = default_wake_function;
103}
104
105static inline void init_waitqueue_func_entry(wait_queue_t *q,
106 wait_queue_func_t func)
107{
108 q->flags = 0;
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700109 q->private = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 q->func = func;
111}
112
113static inline int waitqueue_active(wait_queue_head_t *q)
114{
115 return !list_empty(&q->task_list);
116}
117
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800118extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
119extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
120extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
123{
124 list_add(&new->task_list, &head->task_list);
125}
126
127/*
128 * Used for wake-one threads:
129 */
Changli Gaoa93d2f12010-05-07 14:33:26 +0800130static inline void __add_wait_queue_exclusive(wait_queue_head_t *q,
131 wait_queue_t *wait)
132{
133 wait->flags |= WQ_FLAG_EXCLUSIVE;
134 __add_wait_queue(q, wait);
135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137static inline void __add_wait_queue_tail(wait_queue_head_t *head,
Changli Gaoa93d2f12010-05-07 14:33:26 +0800138 wait_queue_t *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 list_add_tail(&new->task_list, &head->task_list);
141}
142
Changli Gaoa93d2f12010-05-07 14:33:26 +0800143static inline void __add_wait_queue_tail_exclusive(wait_queue_head_t *q,
144 wait_queue_t *wait)
145{
146 wait->flags |= WQ_FLAG_EXCLUSIVE;
147 __add_wait_queue_tail(q, wait);
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static inline void __remove_wait_queue(wait_queue_head_t *head,
151 wait_queue_t *old)
152{
153 list_del(&old->task_list);
154}
155
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800156void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
Davide Libenzi4ede8162009-03-31 15:24:20 -0700157void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key);
158void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr,
159 void *key);
160void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
161void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800162void __wake_up_bit(wait_queue_head_t *, void *, int);
163int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
164int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
165void wake_up_bit(void *, int);
166int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
167int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
168wait_queue_head_t *bit_waitqueue(void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Matthew Wilcoxe64d66c2007-12-06 17:34:36 -0500170#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
171#define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
172#define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
173#define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
174
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 } \
236 finish_wait(&wq, &__wait); \
237} while (0)
238
239/**
240 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
241 * @wq: the waitqueue to wait on
242 * @condition: a C expression for the event to wait for
243 * @timeout: timeout, in jiffies
244 *
245 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
246 * @condition evaluates to true. The @condition is checked each time
247 * the waitqueue @wq is woken up.
248 *
249 * wake_up() has to be called after changing any variable that could
250 * change the result of the wait condition.
251 *
252 * The function returns 0 if the @timeout elapsed, and the remaining
253 * jiffies if the condition evaluated to true before the timeout elapsed.
254 */
255#define wait_event_timeout(wq, condition, timeout) \
256({ \
257 long __ret = timeout; \
258 if (!(condition)) \
259 __wait_event_timeout(wq, condition, __ret); \
260 __ret; \
261})
262
263#define __wait_event_interruptible(wq, condition, ret) \
264do { \
265 DEFINE_WAIT(__wait); \
266 \
267 for (;;) { \
268 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
269 if (condition) \
270 break; \
271 if (!signal_pending(current)) { \
272 schedule(); \
273 continue; \
274 } \
275 ret = -ERESTARTSYS; \
276 break; \
277 } \
278 finish_wait(&wq, &__wait); \
279} while (0)
280
281/**
282 * wait_event_interruptible - sleep until a condition gets true
283 * @wq: the waitqueue to wait on
284 * @condition: a C expression for the event to wait for
285 *
286 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
287 * @condition evaluates to true or a signal is received.
288 * The @condition is checked each time the waitqueue @wq is woken up.
289 *
290 * wake_up() has to be called after changing any variable that could
291 * change the result of the wait condition.
292 *
293 * The function will return -ERESTARTSYS if it was interrupted by a
294 * signal and 0 if @condition evaluated to true.
295 */
296#define wait_event_interruptible(wq, condition) \
297({ \
298 int __ret = 0; \
299 if (!(condition)) \
300 __wait_event_interruptible(wq, condition, __ret); \
301 __ret; \
302})
303
304#define __wait_event_interruptible_timeout(wq, condition, ret) \
305do { \
306 DEFINE_WAIT(__wait); \
307 \
308 for (;;) { \
309 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
310 if (condition) \
311 break; \
312 if (!signal_pending(current)) { \
313 ret = schedule_timeout(ret); \
314 if (!ret) \
315 break; \
316 continue; \
317 } \
318 ret = -ERESTARTSYS; \
319 break; \
320 } \
321 finish_wait(&wq, &__wait); \
322} while (0)
323
324/**
325 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
326 * @wq: the waitqueue to wait on
327 * @condition: a C expression for the event to wait for
328 * @timeout: timeout, in jiffies
329 *
330 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
331 * @condition evaluates to true or a signal is received.
332 * The @condition is checked each time the waitqueue @wq is woken up.
333 *
334 * wake_up() has to be called after changing any variable that could
335 * change the result of the wait condition.
336 *
337 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
338 * was interrupted by a signal, and the remaining jiffies otherwise
339 * if the condition evaluated to true before the timeout elapsed.
340 */
341#define wait_event_interruptible_timeout(wq, condition, timeout) \
342({ \
343 long __ret = timeout; \
344 if (!(condition)) \
345 __wait_event_interruptible_timeout(wq, condition, __ret); \
346 __ret; \
347})
348
349#define __wait_event_interruptible_exclusive(wq, condition, ret) \
350do { \
351 DEFINE_WAIT(__wait); \
352 \
353 for (;;) { \
354 prepare_to_wait_exclusive(&wq, &__wait, \
355 TASK_INTERRUPTIBLE); \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800356 if (condition) { \
357 finish_wait(&wq, &__wait); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break; \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800359 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!signal_pending(current)) { \
361 schedule(); \
362 continue; \
363 } \
364 ret = -ERESTARTSYS; \
Johannes Weiner777c6c52009-02-04 15:12:14 -0800365 abort_exclusive_wait(&wq, &__wait, \
366 TASK_INTERRUPTIBLE, NULL); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break; \
368 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369} while (0)
370
371#define wait_event_interruptible_exclusive(wq, condition) \
372({ \
373 int __ret = 0; \
374 if (!(condition)) \
375 __wait_event_interruptible_exclusive(wq, condition, __ret);\
376 __ret; \
377})
378
Matthew Wilcox1411d5a2007-12-06 12:00:00 -0500379#define __wait_event_killable(wq, condition, ret) \
380do { \
381 DEFINE_WAIT(__wait); \
382 \
383 for (;;) { \
384 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
385 if (condition) \
386 break; \
387 if (!fatal_signal_pending(current)) { \
388 schedule(); \
389 continue; \
390 } \
391 ret = -ERESTARTSYS; \
392 break; \
393 } \
394 finish_wait(&wq, &__wait); \
395} while (0)
396
397/**
398 * wait_event_killable - sleep until a condition gets true
399 * @wq: the waitqueue to wait on
400 * @condition: a C expression for the event to wait for
401 *
402 * The process is put to sleep (TASK_KILLABLE) until the
403 * @condition evaluates to true or a signal is received.
404 * The @condition is checked each time the waitqueue @wq is woken up.
405 *
406 * wake_up() has to be called after changing any variable that could
407 * change the result of the wait condition.
408 *
409 * The function will return -ERESTARTSYS if it was interrupted by a
410 * signal and 0 if @condition evaluated to true.
411 */
412#define wait_event_killable(wq, condition) \
413({ \
414 int __ret = 0; \
415 if (!(condition)) \
416 __wait_event_killable(wq, condition, __ret); \
417 __ret; \
418})
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * These are the old interfaces to sleep waiting for an event.
Ingo Molnar0fec1712007-07-09 18:52:01 +0200422 * They are racy. DO NOT use them, use the wait_event* interfaces above.
423 * We plan to remove these interfaces.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
Ingo Molnar0fec1712007-07-09 18:52:01 +0200425extern void sleep_on(wait_queue_head_t *q);
426extern long sleep_on_timeout(wait_queue_head_t *q,
427 signed long timeout);
428extern void interruptible_sleep_on(wait_queue_head_t *q);
429extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
430 signed long timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432/*
433 * Waitqueues which are removed from the waitqueue_head at wakeup time
434 */
Harvey Harrisonb3c97522008-02-13 15:03:15 -0800435void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
436void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
437void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
Johannes Weiner777c6c52009-02-04 15:12:14 -0800438void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
439 unsigned int mode, void *key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
441int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
442
Eric Dumazetbf368e42009-04-28 02:24:21 -0700443#define DEFINE_WAIT_FUNC(name, function) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 wait_queue_t name = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700445 .private = current, \
Eric Dumazetbf368e42009-04-28 02:24:21 -0700446 .func = function, \
blaisorblade@yahoo.it7e43c842005-05-25 01:31:42 +0200447 .task_list = LIST_HEAD_INIT((name).task_list), \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
Eric Dumazetbf368e42009-04-28 02:24:21 -0700450#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452#define DEFINE_WAIT_BIT(name, word, bit) \
453 struct wait_bit_queue name = { \
454 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
455 .wait = { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700456 .private = current, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .func = wake_bit_function, \
458 .task_list = \
459 LIST_HEAD_INIT((name).wait.task_list), \
460 }, \
461 }
462
463#define init_wait(wait) \
464 do { \
Benjamin LaHaisec43dc2f2005-06-23 00:10:27 -0700465 (wait)->private = current; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 (wait)->func = autoremove_wake_function; \
467 INIT_LIST_HEAD(&(wait)->task_list); \
468 } while (0)
469
470/**
471 * wait_on_bit - wait for a bit to be cleared
472 * @word: the word being waited on, a kernel virtual address
473 * @bit: the bit of the word being waited on
474 * @action: the function used to sleep, which may take special actions
475 * @mode: the task state to sleep in
476 *
477 * There is a standard hashed waitqueue table for generic use. This
478 * is the part of the hashtable's accessor API that waits on a bit.
479 * For instance, if one were to have waiters on a bitflag, one would
480 * call wait_on_bit() in threads waiting for the bit to clear.
481 * One uses wait_on_bit() where one is waiting for the bit to clear,
482 * but has no intention of setting it.
483 */
484static inline int wait_on_bit(void *word, int bit,
485 int (*action)(void *), unsigned mode)
486{
487 if (!test_bit(bit, word))
488 return 0;
489 return out_of_line_wait_on_bit(word, bit, action, mode);
490}
491
492/**
493 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
494 * @word: the word being waited on, a kernel virtual address
495 * @bit: the bit of the word being waited on
496 * @action: the function used to sleep, which may take special actions
497 * @mode: the task state to sleep in
498 *
499 * There is a standard hashed waitqueue table for generic use. This
500 * is the part of the hashtable's accessor API that waits on a bit
501 * when one intends to set it, for instance, trying to lock bitflags.
502 * For instance, if one were to have waiters trying to set bitflag
503 * and waiting for it to clear before setting it, one would call
504 * wait_on_bit() in threads waiting to be able to set the bit.
505 * One uses wait_on_bit_lock() where one is waiting for the bit to
506 * clear with the intention of setting it, and when done, clearing it.
507 */
508static inline int wait_on_bit_lock(void *word, int bit,
509 int (*action)(void *), unsigned mode)
510{
511 if (!test_and_set_bit(bit, word))
512 return 0;
513 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
514}
515
516#endif /* __KERNEL__ */
517
518#endif