ACPI: thinkpad-acpi: preserve radio state across shutdown
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / include / linux / wait.h
blob3c2f411510e89f9a2f7c6774ce6372097587bb75
1 #ifndef _LINUX_WAIT_H
2 #define _LINUX_WAIT_H
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. */
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 */
15 /* First argument to waitid: */
16 #define P_ALL 0
17 #define P_PID 1
18 #define P_PGID 2
20 #ifdef __KERNEL__
22 #include <linux/list.h>
23 #include <linux/stddef.h>
24 #include <linux/spinlock.h>
25 #include <asm/system.h>
26 #include <asm/current.h>
28 typedef struct __wait_queue wait_queue_t;
29 typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int sync, void *key);
30 int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
32 struct __wait_queue {
33 unsigned int flags;
34 #define WQ_FLAG_EXCLUSIVE 0x01
35 void *private;
36 wait_queue_func_t func;
37 struct list_head task_list;
40 struct wait_bit_key {
41 void *flags;
42 int bit_nr;
45 struct wait_bit_queue {
46 struct wait_bit_key key;
47 wait_queue_t wait;
50 struct __wait_queue_head {
51 spinlock_t lock;
52 struct list_head task_list;
54 typedef struct __wait_queue_head wait_queue_head_t;
56 struct task_struct;
59 * Macros for declaration and initialisaton of the datatypes
62 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
63 .private = tsk, \
64 .func = default_wake_function, \
65 .task_list = { NULL, NULL } }
67 #define DECLARE_WAITQUEUE(name, tsk) \
68 wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)
70 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
71 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
72 .task_list = { &(name).task_list, &(name).task_list } }
74 #define DECLARE_WAIT_QUEUE_HEAD(name) \
75 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
77 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
78 { .flags = word, .bit_nr = bit, }
80 extern void init_waitqueue_head(wait_queue_head_t *q);
82 #ifdef CONFIG_LOCKDEP
83 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
84 ({ init_waitqueue_head(&name); name; })
85 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
86 wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
87 #else
88 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
89 #endif
91 static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
93 q->flags = 0;
94 q->private = p;
95 q->func = default_wake_function;
98 static inline void init_waitqueue_func_entry(wait_queue_t *q,
99 wait_queue_func_t func)
101 q->flags = 0;
102 q->private = NULL;
103 q->func = func;
106 static inline int waitqueue_active(wait_queue_head_t *q)
108 return !list_empty(&q->task_list);
112 * Used to distinguish between sync and async io wait context:
113 * sync i/o typically specifies a NULL wait queue entry or a wait
114 * queue entry bound to a task (current task) to wake up.
115 * aio specifies a wait queue entry with an async notification
116 * callback routine, not associated with any task.
118 #define is_sync_wait(wait) (!(wait) || ((wait)->private))
120 extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
121 extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
122 extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
124 static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
126 list_add(&new->task_list, &head->task_list);
130 * Used for wake-one threads:
132 static inline void __add_wait_queue_tail(wait_queue_head_t *head,
133 wait_queue_t *new)
135 list_add_tail(&new->task_list, &head->task_list);
138 static inline void __remove_wait_queue(wait_queue_head_t *head,
139 wait_queue_t *old)
141 list_del(&old->task_list);
144 void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
145 int nr_exclusive, int sync, void *key);
146 void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key);
147 extern void __wake_up_locked(wait_queue_head_t *q, unsigned int mode);
148 extern void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr);
149 void __wake_up_bit(wait_queue_head_t *, void *, int);
150 int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
151 int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned);
152 void wake_up_bit(void *, int);
153 int out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned);
154 int out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned);
155 wait_queue_head_t *bit_waitqueue(void *, int);
157 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
158 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
159 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
160 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL)
162 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
163 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
164 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
165 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
167 #ifdef CONFIG_DEBUG_LOCK_ALLOC
169 * macro to avoid include hell
171 #define wake_up_nested(x, s) \
172 do { \
173 unsigned long flags; \
175 spin_lock_irqsave_nested(&(x)->lock, flags, (s)); \
176 wake_up_locked(x); \
177 spin_unlock_irqrestore(&(x)->lock, flags); \
178 } while (0)
179 #else
180 #define wake_up_nested(x, s) wake_up(x)
181 #endif
183 #define __wait_event(wq, condition) \
184 do { \
185 DEFINE_WAIT(__wait); \
187 for (;;) { \
188 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
189 if (condition) \
190 break; \
191 schedule(); \
193 finish_wait(&wq, &__wait); \
194 } while (0)
197 * wait_event - sleep until a condition gets true
198 * @wq: the waitqueue to wait on
199 * @condition: a C expression for the event to wait for
201 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
202 * @condition evaluates to true. The @condition is checked each time
203 * the waitqueue @wq is woken up.
205 * wake_up() has to be called after changing any variable that could
206 * change the result of the wait condition.
208 #define wait_event(wq, condition) \
209 do { \
210 if (condition) \
211 break; \
212 __wait_event(wq, condition); \
213 } while (0)
215 #define __wait_event_timeout(wq, condition, ret) \
216 do { \
217 DEFINE_WAIT(__wait); \
219 for (;;) { \
220 prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE); \
221 if (condition) \
222 break; \
223 ret = schedule_timeout(ret); \
224 if (!ret) \
225 break; \
227 finish_wait(&wq, &__wait); \
228 } while (0)
231 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
232 * @wq: the waitqueue to wait on
233 * @condition: a C expression for the event to wait for
234 * @timeout: timeout, in jiffies
236 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
237 * @condition evaluates to true. The @condition is checked each time
238 * the waitqueue @wq is woken up.
240 * wake_up() has to be called after changing any variable that could
241 * change the result of the wait condition.
243 * The function returns 0 if the @timeout elapsed, and the remaining
244 * jiffies if the condition evaluated to true before the timeout elapsed.
246 #define wait_event_timeout(wq, condition, timeout) \
247 ({ \
248 long __ret = timeout; \
249 if (!(condition)) \
250 __wait_event_timeout(wq, condition, __ret); \
251 __ret; \
254 #define __wait_event_interruptible(wq, condition, ret) \
255 do { \
256 DEFINE_WAIT(__wait); \
258 for (;;) { \
259 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
260 if (condition) \
261 break; \
262 if (!signal_pending(current)) { \
263 schedule(); \
264 continue; \
266 ret = -ERESTARTSYS; \
267 break; \
269 finish_wait(&wq, &__wait); \
270 } while (0)
273 * wait_event_interruptible - sleep until a condition gets true
274 * @wq: the waitqueue to wait on
275 * @condition: a C expression for the event to wait for
277 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
278 * @condition evaluates to true or a signal is received.
279 * The @condition is checked each time the waitqueue @wq is woken up.
281 * wake_up() has to be called after changing any variable that could
282 * change the result of the wait condition.
284 * The function will return -ERESTARTSYS if it was interrupted by a
285 * signal and 0 if @condition evaluated to true.
287 #define wait_event_interruptible(wq, condition) \
288 ({ \
289 int __ret = 0; \
290 if (!(condition)) \
291 __wait_event_interruptible(wq, condition, __ret); \
292 __ret; \
295 #define __wait_event_interruptible_timeout(wq, condition, ret) \
296 do { \
297 DEFINE_WAIT(__wait); \
299 for (;;) { \
300 prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE); \
301 if (condition) \
302 break; \
303 if (!signal_pending(current)) { \
304 ret = schedule_timeout(ret); \
305 if (!ret) \
306 break; \
307 continue; \
309 ret = -ERESTARTSYS; \
310 break; \
312 finish_wait(&wq, &__wait); \
313 } while (0)
316 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
317 * @wq: the waitqueue to wait on
318 * @condition: a C expression for the event to wait for
319 * @timeout: timeout, in jiffies
321 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
322 * @condition evaluates to true or a signal is received.
323 * The @condition is checked each time the waitqueue @wq is woken up.
325 * wake_up() has to be called after changing any variable that could
326 * change the result of the wait condition.
328 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
329 * was interrupted by a signal, and the remaining jiffies otherwise
330 * if the condition evaluated to true before the timeout elapsed.
332 #define wait_event_interruptible_timeout(wq, condition, timeout) \
333 ({ \
334 long __ret = timeout; \
335 if (!(condition)) \
336 __wait_event_interruptible_timeout(wq, condition, __ret); \
337 __ret; \
340 #define __wait_event_interruptible_exclusive(wq, condition, ret) \
341 do { \
342 DEFINE_WAIT(__wait); \
344 for (;;) { \
345 prepare_to_wait_exclusive(&wq, &__wait, \
346 TASK_INTERRUPTIBLE); \
347 if (condition) { \
348 finish_wait(&wq, &__wait); \
349 break; \
351 if (!signal_pending(current)) { \
352 schedule(); \
353 continue; \
355 ret = -ERESTARTSYS; \
356 abort_exclusive_wait(&wq, &__wait, \
357 TASK_INTERRUPTIBLE, NULL); \
358 break; \
360 } while (0)
362 #define wait_event_interruptible_exclusive(wq, condition) \
363 ({ \
364 int __ret = 0; \
365 if (!(condition)) \
366 __wait_event_interruptible_exclusive(wq, condition, __ret);\
367 __ret; \
370 #define __wait_event_killable(wq, condition, ret) \
371 do { \
372 DEFINE_WAIT(__wait); \
374 for (;;) { \
375 prepare_to_wait(&wq, &__wait, TASK_KILLABLE); \
376 if (condition) \
377 break; \
378 if (!fatal_signal_pending(current)) { \
379 schedule(); \
380 continue; \
382 ret = -ERESTARTSYS; \
383 break; \
385 finish_wait(&wq, &__wait); \
386 } while (0)
389 * wait_event_killable - sleep until a condition gets true
390 * @wq: the waitqueue to wait on
391 * @condition: a C expression for the event to wait for
393 * The process is put to sleep (TASK_KILLABLE) until the
394 * @condition evaluates to true or a signal is received.
395 * The @condition is checked each time the waitqueue @wq is woken up.
397 * wake_up() has to be called after changing any variable that could
398 * change the result of the wait condition.
400 * The function will return -ERESTARTSYS if it was interrupted by a
401 * signal and 0 if @condition evaluated to true.
403 #define wait_event_killable(wq, condition) \
404 ({ \
405 int __ret = 0; \
406 if (!(condition)) \
407 __wait_event_killable(wq, condition, __ret); \
408 __ret; \
412 * Must be called with the spinlock in the wait_queue_head_t held.
414 static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
415 wait_queue_t * wait)
417 wait->flags |= WQ_FLAG_EXCLUSIVE;
418 __add_wait_queue_tail(q, wait);
422 * Must be called with the spinlock in the wait_queue_head_t held.
424 static inline void remove_wait_queue_locked(wait_queue_head_t *q,
425 wait_queue_t * wait)
427 __remove_wait_queue(q, wait);
431 * These are the old interfaces to sleep waiting for an event.
432 * They are racy. DO NOT use them, use the wait_event* interfaces above.
433 * We plan to remove these interfaces.
435 extern void sleep_on(wait_queue_head_t *q);
436 extern long sleep_on_timeout(wait_queue_head_t *q,
437 signed long timeout);
438 extern void interruptible_sleep_on(wait_queue_head_t *q);
439 extern long interruptible_sleep_on_timeout(wait_queue_head_t *q,
440 signed long timeout);
443 * Waitqueues which are removed from the waitqueue_head at wakeup time
445 void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
446 void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
447 void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);
448 void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait,
449 unsigned int mode, void *key);
450 int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
451 int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
453 #define DEFINE_WAIT(name) \
454 wait_queue_t name = { \
455 .private = current, \
456 .func = autoremove_wake_function, \
457 .task_list = LIST_HEAD_INIT((name).task_list), \
460 #define DEFINE_WAIT_BIT(name, word, bit) \
461 struct wait_bit_queue name = { \
462 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
463 .wait = { \
464 .private = current, \
465 .func = wake_bit_function, \
466 .task_list = \
467 LIST_HEAD_INIT((name).wait.task_list), \
468 }, \
471 #define init_wait(wait) \
472 do { \
473 (wait)->private = current; \
474 (wait)->func = autoremove_wake_function; \
475 INIT_LIST_HEAD(&(wait)->task_list); \
476 } while (0)
479 * wait_on_bit - wait for a bit to be cleared
480 * @word: the word being waited on, a kernel virtual address
481 * @bit: the bit of the word being waited on
482 * @action: the function used to sleep, which may take special actions
483 * @mode: the task state to sleep in
485 * There is a standard hashed waitqueue table for generic use. This
486 * is the part of the hashtable's accessor API that waits on a bit.
487 * For instance, if one were to have waiters on a bitflag, one would
488 * call wait_on_bit() in threads waiting for the bit to clear.
489 * One uses wait_on_bit() where one is waiting for the bit to clear,
490 * but has no intention of setting it.
492 static inline int wait_on_bit(void *word, int bit,
493 int (*action)(void *), unsigned mode)
495 if (!test_bit(bit, word))
496 return 0;
497 return out_of_line_wait_on_bit(word, bit, action, mode);
501 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
502 * @word: the word being waited on, a kernel virtual address
503 * @bit: the bit of the word being waited on
504 * @action: the function used to sleep, which may take special actions
505 * @mode: the task state to sleep in
507 * There is a standard hashed waitqueue table for generic use. This
508 * is the part of the hashtable's accessor API that waits on a bit
509 * when one intends to set it, for instance, trying to lock bitflags.
510 * For instance, if one were to have waiters trying to set bitflag
511 * and waiting for it to clear before setting it, one would call
512 * wait_on_bit() in threads waiting to be able to set the bit.
513 * One uses wait_on_bit_lock() where one is waiting for the bit to
514 * clear with the intention of setting it, and when done, clearing it.
516 static inline int wait_on_bit_lock(void *word, int bit,
517 int (*action)(void *), unsigned mode)
519 if (!test_and_set_bit(bit, word))
520 return 0;
521 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
524 #endif /* __KERNEL__ */
526 #endif