4 * Linux wait queue related types and methods
6 #include <linux/list.h>
7 #include <linux/stddef.h>
8 #include <linux/spinlock.h>
10 #include <asm/current.h>
11 #include <uapi/linux/wait.h>
13 typedef struct wait_queue_entry wait_queue_entry_t
;
15 typedef int (*wait_queue_func_t
)(struct wait_queue_entry
*wq_entry
, unsigned mode
, int flags
, void *key
);
16 int default_wake_function(struct wait_queue_entry
*wq_entry
, unsigned mode
, int flags
, void *key
);
18 /* wait_queue_entry::flags */
19 #define WQ_FLAG_EXCLUSIVE 0x01
20 #define WQ_FLAG_WOKEN 0x02
23 * A single wait-queue entry structure:
25 struct wait_queue_entry
{
28 wait_queue_func_t func
;
29 struct list_head task_list
;
35 #define WAIT_ATOMIC_T_BIT_NR -1
36 unsigned long timeout
;
39 struct wait_bit_queue
{
40 struct wait_bit_key key
;
41 struct wait_queue_entry wq_entry
;
44 struct wait_queue_head
{
46 struct list_head task_list
;
48 typedef struct wait_queue_head wait_queue_head_t
;
53 * Macros for declaration and initialisaton of the datatypes
56 #define __WAITQUEUE_INITIALIZER(name, tsk) { \
58 .func = default_wake_function, \
59 .task_list = { NULL, NULL } }
61 #define DECLARE_WAITQUEUE(name, tsk) \
62 struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
64 #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
65 .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
66 .task_list = { &(name).task_list, &(name).task_list } }
68 #define DECLARE_WAIT_QUEUE_HEAD(name) \
69 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
71 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
72 { .flags = word, .bit_nr = bit, }
74 #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \
75 { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, }
77 extern void __init_waitqueue_head(struct wait_queue_head
*wq_head
, const char *name
, struct lock_class_key
*);
79 #define init_waitqueue_head(wq_head) \
81 static struct lock_class_key __key; \
83 __init_waitqueue_head((wq_head), #wq_head, &__key); \
87 # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
88 ({ init_waitqueue_head(&name); name; })
89 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
90 struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
92 # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
95 static inline void init_waitqueue_entry(struct wait_queue_entry
*wq_entry
, struct task_struct
*p
)
98 wq_entry
->private = p
;
99 wq_entry
->func
= default_wake_function
;
103 init_waitqueue_func_entry(struct wait_queue_entry
*wq_entry
, wait_queue_func_t func
)
106 wq_entry
->private = NULL
;
107 wq_entry
->func
= func
;
111 * waitqueue_active -- locklessly test for waiters on the queue
112 * @wq_head: the waitqueue to test for waiters
114 * returns true if the wait list is not empty
116 * NOTE: this function is lockless and requires care, incorrect usage _will_
117 * lead to sporadic and non-obvious failure.
119 * Use either while holding wait_queue_head::lock or when used for wakeups
120 * with an extra smp_mb() like:
122 * CPU0 - waker CPU1 - waiter
125 * @cond = true; prepare_to_wait(&wq, &wait, state);
126 * smp_mb(); // smp_mb() from set_current_state()
127 * if (waitqueue_active(wq)) if (@cond)
128 * wake_up(wq); break;
131 * finish_wait(&wq, &wait);
133 * Because without the explicit smp_mb() it's possible for the
134 * waitqueue_active() load to get hoisted over the @cond store such that we'll
135 * observe an empty wait list while the waiter might not observe @cond.
137 * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
138 * which (when the lock is uncontended) are of roughly equal cost.
140 static inline int waitqueue_active(struct wait_queue_head
*wq_head
)
142 return !list_empty(&wq_head
->task_list
);
146 * wq_has_sleeper - check if there are any waiting processes
147 * @wq: wait queue head
149 * Returns true if wq has waiting processes
151 * Please refer to the comment for waitqueue_active.
153 static inline bool wq_has_sleeper(struct wait_queue_head
*wq_head
)
156 * We need to be sure we are in sync with the
157 * add_wait_queue modifications to the wait queue.
159 * This memory barrier should be paired with one on the
163 return waitqueue_active(wq_head
);
166 extern void add_wait_queue(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
);
167 extern void add_wait_queue_exclusive(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
);
168 extern void remove_wait_queue(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
);
170 static inline void __add_wait_queue(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
172 list_add(&wq_entry
->task_list
, &wq_head
->task_list
);
176 * Used for wake-one threads:
179 __add_wait_queue_exclusive(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
181 wq_entry
->flags
|= WQ_FLAG_EXCLUSIVE
;
182 __add_wait_queue(wq_head
, wq_entry
);
185 static inline void __add_wait_queue_entry_tail(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
187 list_add_tail(&wq_entry
->task_list
, &wq_head
->task_list
);
191 __add_wait_queue_entry_tail_exclusive(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
193 wq_entry
->flags
|= WQ_FLAG_EXCLUSIVE
;
194 __add_wait_queue_entry_tail(wq_head
, wq_entry
);
198 __remove_wait_queue(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
)
200 list_del(&wq_entry
->task_list
);
203 typedef int wait_bit_action_f(struct wait_bit_key
*, int mode
);
204 void __wake_up(struct wait_queue_head
*wq_head
, unsigned int mode
, int nr
, void *key
);
205 void __wake_up_locked_key(struct wait_queue_head
*wq_head
, unsigned int mode
, void *key
);
206 void __wake_up_sync_key(struct wait_queue_head
*wq_head
, unsigned int mode
, int nr
, void *key
);
207 void __wake_up_locked(struct wait_queue_head
*wq_head
, unsigned int mode
, int nr
);
208 void __wake_up_sync(struct wait_queue_head
*wq_head
, unsigned int mode
, int nr
);
209 void __wake_up_bit(struct wait_queue_head
*, void *, int);
210 int __wait_on_bit(struct wait_queue_head
*, struct wait_bit_queue
*, wait_bit_action_f
*, unsigned);
211 int __wait_on_bit_lock(struct wait_queue_head
*, struct wait_bit_queue
*, wait_bit_action_f
*, unsigned);
212 void wake_up_bit(void *, int);
213 void wake_up_atomic_t(atomic_t
*);
214 int out_of_line_wait_on_bit(void *, int, wait_bit_action_f
*, unsigned);
215 int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f
*, unsigned, unsigned long);
216 int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f
*, unsigned);
217 int out_of_line_wait_on_atomic_t(atomic_t
*, int (*)(atomic_t
*), unsigned);
218 struct wait_queue_head
*bit_waitqueue(void *, int);
220 #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
221 #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
222 #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
223 #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
224 #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
226 #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
227 #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
228 #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
229 #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)
232 * Wakeup macros to be used to report events to the targets.
234 #define wake_up_poll(x, m) \
235 __wake_up(x, TASK_NORMAL, 1, (void *) (m))
236 #define wake_up_locked_poll(x, m) \
237 __wake_up_locked_key((x), TASK_NORMAL, (void *) (m))
238 #define wake_up_interruptible_poll(x, m) \
239 __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m))
240 #define wake_up_interruptible_sync_poll(x, m) \
241 __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m))
243 #define ___wait_cond_timeout(condition) \
245 bool __cond = (condition); \
246 if (__cond && !__ret) \
251 #define ___wait_is_interruptible(state) \
252 (!__builtin_constant_p(state) || \
253 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
255 extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
258 * The below macro ___wait_event() has an explicit shadow of the __ret
259 * variable when used from the wait_event_*() macros.
261 * This is so that both can use the ___wait_cond_timeout() construct
262 * to wrap the condition.
264 * The type inconsistency of the wait_event_*() __ret variable is also
265 * on purpose; we use long where we can return timeout values and int
269 #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \
272 struct wait_queue_entry __wq_entry; \
273 long __ret = ret; /* explicit shadow */ \
275 init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0);\
277 long __int = prepare_to_wait_event(&wq, &__wq_entry, state);\
282 if (___wait_is_interruptible(state) && __int) { \
289 finish_wait(&wq, &__wq_entry); \
293 #define __wait_event(wq, condition) \
294 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
298 * wait_event - sleep until a condition gets true
299 * @wq: the waitqueue to wait on
300 * @condition: a C expression for the event to wait for
302 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
303 * @condition evaluates to true. The @condition is checked each time
304 * the waitqueue @wq is woken up.
306 * wake_up() has to be called after changing any variable that could
307 * change the result of the wait condition.
309 #define wait_event(wq, condition) \
314 __wait_event(wq, condition); \
317 #define __io_wait_event(wq, condition) \
318 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
322 * io_wait_event() -- like wait_event() but with io_schedule()
324 #define io_wait_event(wq, condition) \
329 __io_wait_event(wq, condition); \
332 #define __wait_event_freezable(wq, condition) \
333 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
334 schedule(); try_to_freeze())
337 * wait_event_freezable - sleep (or freeze) until a condition gets true
338 * @wq: the waitqueue to wait on
339 * @condition: a C expression for the event to wait for
341 * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
342 * to system load) until the @condition evaluates to true. The
343 * @condition is checked each time the waitqueue @wq is woken up.
345 * wake_up() has to be called after changing any variable that could
346 * change the result of the wait condition.
348 #define wait_event_freezable(wq, condition) \
353 __ret = __wait_event_freezable(wq, condition); \
357 #define __wait_event_timeout(wq, condition, timeout) \
358 ___wait_event(wq, ___wait_cond_timeout(condition), \
359 TASK_UNINTERRUPTIBLE, 0, timeout, \
360 __ret = schedule_timeout(__ret))
363 * wait_event_timeout - sleep until a condition gets true or a timeout elapses
364 * @wq: the waitqueue to wait on
365 * @condition: a C expression for the event to wait for
366 * @timeout: timeout, in jiffies
368 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
369 * @condition evaluates to true. The @condition is checked each time
370 * the waitqueue @wq is woken up.
372 * wake_up() has to be called after changing any variable that could
373 * change the result of the wait condition.
376 * 0 if the @condition evaluated to %false after the @timeout elapsed,
377 * 1 if the @condition evaluated to %true after the @timeout elapsed,
378 * or the remaining jiffies (at least 1) if the @condition evaluated
379 * to %true before the @timeout elapsed.
381 #define wait_event_timeout(wq, condition, timeout) \
383 long __ret = timeout; \
385 if (!___wait_cond_timeout(condition)) \
386 __ret = __wait_event_timeout(wq, condition, timeout); \
390 #define __wait_event_freezable_timeout(wq, condition, timeout) \
391 ___wait_event(wq, ___wait_cond_timeout(condition), \
392 TASK_INTERRUPTIBLE, 0, timeout, \
393 __ret = schedule_timeout(__ret); try_to_freeze())
396 * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
397 * increasing load and is freezable.
399 #define wait_event_freezable_timeout(wq, condition, timeout) \
401 long __ret = timeout; \
403 if (!___wait_cond_timeout(condition)) \
404 __ret = __wait_event_freezable_timeout(wq, condition, timeout); \
408 #define __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
409 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
410 cmd1; schedule(); cmd2)
412 * Just like wait_event_cmd(), except it sets exclusive flag
414 #define wait_event_exclusive_cmd(wq, condition, cmd1, cmd2) \
418 __wait_event_exclusive_cmd(wq, condition, cmd1, cmd2); \
421 #define __wait_event_cmd(wq, condition, cmd1, cmd2) \
422 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
423 cmd1; schedule(); cmd2)
426 * wait_event_cmd - sleep until a condition gets true
427 * @wq: the waitqueue to wait on
428 * @condition: a C expression for the event to wait for
429 * @cmd1: the command will be executed before sleep
430 * @cmd2: the command will be executed after sleep
432 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
433 * @condition evaluates to true. The @condition is checked each time
434 * the waitqueue @wq is woken up.
436 * wake_up() has to be called after changing any variable that could
437 * change the result of the wait condition.
439 #define wait_event_cmd(wq, condition, cmd1, cmd2) \
443 __wait_event_cmd(wq, condition, cmd1, cmd2); \
446 #define __wait_event_interruptible(wq, condition) \
447 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
451 * wait_event_interruptible - sleep until a condition gets true
452 * @wq: the waitqueue to wait on
453 * @condition: a C expression for the event to wait for
455 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
456 * @condition evaluates to true or a signal is received.
457 * The @condition is checked each time the waitqueue @wq is woken up.
459 * wake_up() has to be called after changing any variable that could
460 * change the result of the wait condition.
462 * The function will return -ERESTARTSYS if it was interrupted by a
463 * signal and 0 if @condition evaluated to true.
465 #define wait_event_interruptible(wq, condition) \
470 __ret = __wait_event_interruptible(wq, condition); \
474 #define __wait_event_interruptible_timeout(wq, condition, timeout) \
475 ___wait_event(wq, ___wait_cond_timeout(condition), \
476 TASK_INTERRUPTIBLE, 0, timeout, \
477 __ret = schedule_timeout(__ret))
480 * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
481 * @wq: the waitqueue to wait on
482 * @condition: a C expression for the event to wait for
483 * @timeout: timeout, in jiffies
485 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
486 * @condition evaluates to true or a signal is received.
487 * The @condition is checked each time the waitqueue @wq is woken up.
489 * wake_up() has to be called after changing any variable that could
490 * change the result of the wait condition.
493 * 0 if the @condition evaluated to %false after the @timeout elapsed,
494 * 1 if the @condition evaluated to %true after the @timeout elapsed,
495 * the remaining jiffies (at least 1) if the @condition evaluated
496 * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
497 * interrupted by a signal.
499 #define wait_event_interruptible_timeout(wq, condition, timeout) \
501 long __ret = timeout; \
503 if (!___wait_cond_timeout(condition)) \
504 __ret = __wait_event_interruptible_timeout(wq, \
505 condition, timeout); \
509 #define __wait_event_hrtimeout(wq, condition, timeout, state) \
512 struct hrtimer_sleeper __t; \
514 hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \
516 hrtimer_init_sleeper(&__t, current); \
517 if ((timeout) != KTIME_MAX) \
518 hrtimer_start_range_ns(&__t.timer, timeout, \
519 current->timer_slack_ns, \
522 __ret = ___wait_event(wq, condition, state, 0, 0, \
529 hrtimer_cancel(&__t.timer); \
530 destroy_hrtimer_on_stack(&__t.timer); \
535 * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
536 * @wq: the waitqueue to wait on
537 * @condition: a C expression for the event to wait for
538 * @timeout: timeout, as a ktime_t
540 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
541 * @condition evaluates to true or a signal is received.
542 * The @condition is checked each time the waitqueue @wq is woken up.
544 * wake_up() has to be called after changing any variable that could
545 * change the result of the wait condition.
547 * The function returns 0 if @condition became true, or -ETIME if the timeout
550 #define wait_event_hrtimeout(wq, condition, timeout) \
555 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
556 TASK_UNINTERRUPTIBLE); \
561 * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
562 * @wq: the waitqueue to wait on
563 * @condition: a C expression for the event to wait for
564 * @timeout: timeout, as a ktime_t
566 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
567 * @condition evaluates to true or a signal is received.
568 * The @condition is checked each time the waitqueue @wq is woken up.
570 * wake_up() has to be called after changing any variable that could
571 * change the result of the wait condition.
573 * The function returns 0 if @condition became true, -ERESTARTSYS if it was
574 * interrupted by a signal, or -ETIME if the timeout elapsed.
576 #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
581 __ret = __wait_event_hrtimeout(wq, condition, timeout, \
582 TASK_INTERRUPTIBLE); \
586 #define __wait_event_interruptible_exclusive(wq, condition) \
587 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
590 #define wait_event_interruptible_exclusive(wq, condition) \
595 __ret = __wait_event_interruptible_exclusive(wq, condition);\
599 #define __wait_event_killable_exclusive(wq, condition) \
600 ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
603 #define wait_event_killable_exclusive(wq, condition) \
608 __ret = __wait_event_killable_exclusive(wq, condition); \
613 #define __wait_event_freezable_exclusive(wq, condition) \
614 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
615 schedule(); try_to_freeze())
617 #define wait_event_freezable_exclusive(wq, condition) \
622 __ret = __wait_event_freezable_exclusive(wq, condition);\
626 extern int do_wait_intr(wait_queue_head_t
*, wait_queue_entry_t
*);
627 extern int do_wait_intr_irq(wait_queue_head_t
*, wait_queue_entry_t
*);
629 #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
632 DEFINE_WAIT(__wait); \
634 __wait.flags |= WQ_FLAG_EXCLUSIVE; \
636 __ret = fn(&(wq), &__wait); \
639 } while (!(condition)); \
640 __remove_wait_queue(&(wq), &__wait); \
641 __set_current_state(TASK_RUNNING); \
647 * wait_event_interruptible_locked - sleep until a condition gets true
648 * @wq: the waitqueue to wait on
649 * @condition: a C expression for the event to wait for
651 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
652 * @condition evaluates to true or a signal is received.
653 * The @condition is checked each time the waitqueue @wq is woken up.
655 * It must be called with wq.lock being held. This spinlock is
656 * unlocked while sleeping but @condition testing is done while lock
657 * is held and when this macro exits the lock is held.
659 * The lock is locked/unlocked using spin_lock()/spin_unlock()
660 * functions which must match the way they are locked/unlocked outside
663 * wake_up_locked() has to be called after changing any variable that could
664 * change the result of the wait condition.
666 * The function will return -ERESTARTSYS if it was interrupted by a
667 * signal and 0 if @condition evaluated to true.
669 #define wait_event_interruptible_locked(wq, condition) \
671 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
674 * wait_event_interruptible_locked_irq - sleep until a condition gets true
675 * @wq: the waitqueue to wait on
676 * @condition: a C expression for the event to wait for
678 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
679 * @condition evaluates to true or a signal is received.
680 * The @condition is checked each time the waitqueue @wq is woken up.
682 * It must be called with wq.lock being held. This spinlock is
683 * unlocked while sleeping but @condition testing is done while lock
684 * is held and when this macro exits the lock is held.
686 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
687 * functions which must match the way they are locked/unlocked outside
690 * wake_up_locked() has to be called after changing any variable that could
691 * change the result of the wait condition.
693 * The function will return -ERESTARTSYS if it was interrupted by a
694 * signal and 0 if @condition evaluated to true.
696 #define wait_event_interruptible_locked_irq(wq, condition) \
698 ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
701 * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
702 * @wq: the waitqueue to wait on
703 * @condition: a C expression for the event to wait for
705 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
706 * @condition evaluates to true or a signal is received.
707 * The @condition is checked each time the waitqueue @wq is woken up.
709 * It must be called with wq.lock being held. This spinlock is
710 * unlocked while sleeping but @condition testing is done while lock
711 * is held and when this macro exits the lock is held.
713 * The lock is locked/unlocked using spin_lock()/spin_unlock()
714 * functions which must match the way they are locked/unlocked outside
717 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
718 * set thus when other process waits process on the list if this
719 * process is awaken further processes are not considered.
721 * wake_up_locked() has to be called after changing any variable that could
722 * change the result of the wait condition.
724 * The function will return -ERESTARTSYS if it was interrupted by a
725 * signal and 0 if @condition evaluated to true.
727 #define wait_event_interruptible_exclusive_locked(wq, condition) \
729 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
732 * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
733 * @wq: the waitqueue to wait on
734 * @condition: a C expression for the event to wait for
736 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
737 * @condition evaluates to true or a signal is received.
738 * The @condition is checked each time the waitqueue @wq is woken up.
740 * It must be called with wq.lock being held. This spinlock is
741 * unlocked while sleeping but @condition testing is done while lock
742 * is held and when this macro exits the lock is held.
744 * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
745 * functions which must match the way they are locked/unlocked outside
748 * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
749 * set thus when other process waits process on the list if this
750 * process is awaken further processes are not considered.
752 * wake_up_locked() has to be called after changing any variable that could
753 * change the result of the wait condition.
755 * The function will return -ERESTARTSYS if it was interrupted by a
756 * signal and 0 if @condition evaluated to true.
758 #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
760 ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
763 #define __wait_event_killable(wq, condition) \
764 ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
767 * wait_event_killable - sleep until a condition gets true
768 * @wq: the waitqueue to wait on
769 * @condition: a C expression for the event to wait for
771 * The process is put to sleep (TASK_KILLABLE) until the
772 * @condition evaluates to true or a signal is received.
773 * The @condition is checked each time the waitqueue @wq is woken up.
775 * wake_up() has to be called after changing any variable that could
776 * change the result of the wait condition.
778 * The function will return -ERESTARTSYS if it was interrupted by a
779 * signal and 0 if @condition evaluated to true.
781 #define wait_event_killable(wq, condition) \
786 __ret = __wait_event_killable(wq, condition); \
791 #define __wait_event_lock_irq(wq, condition, lock, cmd) \
792 (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
793 spin_unlock_irq(&lock); \
796 spin_lock_irq(&lock))
799 * wait_event_lock_irq_cmd - sleep until a condition gets true. The
800 * condition is checked under the lock. This
801 * is expected to be called with the lock
803 * @wq: the waitqueue to wait on
804 * @condition: a C expression for the event to wait for
805 * @lock: a locked spinlock_t, which will be released before cmd
806 * and schedule() and reacquired afterwards.
807 * @cmd: a command which is invoked outside the critical section before
810 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
811 * @condition evaluates to true. The @condition is checked each time
812 * the waitqueue @wq is woken up.
814 * wake_up() has to be called after changing any variable that could
815 * change the result of the wait condition.
817 * This is supposed to be called while holding the lock. The lock is
818 * dropped before invoking the cmd and going to sleep and is reacquired
821 #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \
825 __wait_event_lock_irq(wq, condition, lock, cmd); \
829 * wait_event_lock_irq - sleep until a condition gets true. The
830 * condition is checked under the lock. This
831 * is expected to be called with the lock
833 * @wq: the waitqueue to wait on
834 * @condition: a C expression for the event to wait for
835 * @lock: a locked spinlock_t, which will be released before schedule()
836 * and reacquired afterwards.
838 * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
839 * @condition evaluates to true. The @condition is checked each time
840 * the waitqueue @wq is woken up.
842 * wake_up() has to be called after changing any variable that could
843 * change the result of the wait condition.
845 * This is supposed to be called while holding the lock. The lock is
846 * dropped before going to sleep and is reacquired afterwards.
848 #define wait_event_lock_irq(wq, condition, lock) \
852 __wait_event_lock_irq(wq, condition, lock, ); \
856 #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \
857 ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \
858 spin_unlock_irq(&lock); \
861 spin_lock_irq(&lock))
864 * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
865 * The condition is checked under the lock. This is expected to
866 * be called with the lock taken.
867 * @wq: the waitqueue to wait on
868 * @condition: a C expression for the event to wait for
869 * @lock: a locked spinlock_t, which will be released before cmd and
870 * schedule() and reacquired afterwards.
871 * @cmd: a command which is invoked outside the critical section before
874 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
875 * @condition evaluates to true or a signal is received. The @condition is
876 * checked each time the waitqueue @wq is woken up.
878 * wake_up() has to be called after changing any variable that could
879 * change the result of the wait condition.
881 * This is supposed to be called while holding the lock. The lock is
882 * dropped before invoking the cmd and going to sleep and is reacquired
885 * The macro will return -ERESTARTSYS if it was interrupted by a signal
886 * and 0 if @condition evaluated to true.
888 #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \
892 __ret = __wait_event_interruptible_lock_irq(wq, \
893 condition, lock, cmd); \
898 * wait_event_interruptible_lock_irq - sleep until a condition gets true.
899 * The condition is checked under the lock. This is expected
900 * to be called with the lock taken.
901 * @wq: the waitqueue to wait on
902 * @condition: a C expression for the event to wait for
903 * @lock: a locked spinlock_t, which will be released before schedule()
904 * and reacquired afterwards.
906 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
907 * @condition evaluates to true or signal is received. The @condition is
908 * checked each time the waitqueue @wq is woken up.
910 * wake_up() has to be called after changing any variable that could
911 * change the result of the wait condition.
913 * This is supposed to be called while holding the lock. The lock is
914 * dropped before going to sleep and is reacquired afterwards.
916 * The macro will return -ERESTARTSYS if it was interrupted by a signal
917 * and 0 if @condition evaluated to true.
919 #define wait_event_interruptible_lock_irq(wq, condition, lock) \
923 __ret = __wait_event_interruptible_lock_irq(wq, \
928 #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \
930 ___wait_event(wq, ___wait_cond_timeout(condition), \
931 TASK_INTERRUPTIBLE, 0, timeout, \
932 spin_unlock_irq(&lock); \
933 __ret = schedule_timeout(__ret); \
934 spin_lock_irq(&lock));
937 * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
938 * true or a timeout elapses. The condition is checked under
939 * the lock. This is expected to be called with the lock taken.
940 * @wq: the waitqueue to wait on
941 * @condition: a C expression for the event to wait for
942 * @lock: a locked spinlock_t, which will be released before schedule()
943 * and reacquired afterwards.
944 * @timeout: timeout, in jiffies
946 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
947 * @condition evaluates to true or signal is received. The @condition is
948 * checked each time the waitqueue @wq is woken up.
950 * wake_up() has to be called after changing any variable that could
951 * change the result of the wait condition.
953 * This is supposed to be called while holding the lock. The lock is
954 * dropped before going to sleep and is reacquired afterwards.
956 * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
957 * was interrupted by a signal, and the remaining jiffies otherwise
958 * if the condition evaluated to true before the timeout elapsed.
960 #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \
963 long __ret = timeout; \
964 if (!___wait_cond_timeout(condition)) \
965 __ret = __wait_event_interruptible_lock_irq_timeout( \
966 wq, condition, lock, timeout); \
971 * Waitqueues which are removed from the waitqueue_head at wakeup time
973 void prepare_to_wait(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
, int state
);
974 void prepare_to_wait_exclusive(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
, int state
);
975 long prepare_to_wait_event(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
, int state
);
976 void finish_wait(struct wait_queue_head
*wq_head
, struct wait_queue_entry
*wq_entry
);
977 long wait_woken(struct wait_queue_entry
*wq_entry
, unsigned mode
, long timeout
);
978 int woken_wake_function(struct wait_queue_entry
*wq_entry
, unsigned mode
, int sync
, void *key
);
979 int autoremove_wake_function(struct wait_queue_entry
*wq_entry
, unsigned mode
, int sync
, void *key
);
980 int wake_bit_function(struct wait_queue_entry
*wq_entry
, unsigned mode
, int sync
, void *key
);
982 #define DEFINE_WAIT_FUNC(name, function) \
983 struct wait_queue_entry name = { \
984 .private = current, \
986 .task_list = LIST_HEAD_INIT((name).task_list), \
989 #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
991 #define DEFINE_WAIT_BIT(name, word, bit) \
992 struct wait_bit_queue name = { \
993 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
995 .private = current, \
996 .func = wake_bit_function, \
998 LIST_HEAD_INIT((name).wq_entry.task_list), \
1002 #define init_wait(wait) \
1004 (wait)->private = current; \
1005 (wait)->func = autoremove_wake_function; \
1006 INIT_LIST_HEAD(&(wait)->task_list); \
1007 (wait)->flags = 0; \
1011 extern int bit_wait(struct wait_bit_key
*, int);
1012 extern int bit_wait_io(struct wait_bit_key
*, int);
1013 extern int bit_wait_timeout(struct wait_bit_key
*, int);
1014 extern int bit_wait_io_timeout(struct wait_bit_key
*, int);
1017 * wait_on_bit - wait for a bit to be cleared
1018 * @word: the word being waited on, a kernel virtual address
1019 * @bit: the bit of the word being waited on
1020 * @mode: the task state to sleep in
1022 * There is a standard hashed waitqueue table for generic use. This
1023 * is the part of the hashtable's accessor API that waits on a bit.
1024 * For instance, if one were to have waiters on a bitflag, one would
1025 * call wait_on_bit() in threads waiting for the bit to clear.
1026 * One uses wait_on_bit() where one is waiting for the bit to clear,
1027 * but has no intention of setting it.
1028 * Returned value will be zero if the bit was cleared, or non-zero
1029 * if the process received a signal and the mode permitted wakeup
1033 wait_on_bit(unsigned long *word
, int bit
, unsigned mode
)
1036 if (!test_bit(bit
, word
))
1038 return out_of_line_wait_on_bit(word
, bit
,
1044 * wait_on_bit_io - wait for a bit to be cleared
1045 * @word: the word being waited on, a kernel virtual address
1046 * @bit: the bit of the word being waited on
1047 * @mode: the task state to sleep in
1049 * Use the standard hashed waitqueue table to wait for a bit
1050 * to be cleared. This is similar to wait_on_bit(), but calls
1051 * io_schedule() instead of schedule() for the actual waiting.
1053 * Returned value will be zero if the bit was cleared, or non-zero
1054 * if the process received a signal and the mode permitted wakeup
1058 wait_on_bit_io(unsigned long *word
, int bit
, unsigned mode
)
1061 if (!test_bit(bit
, word
))
1063 return out_of_line_wait_on_bit(word
, bit
,
1069 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
1070 * @word: the word being waited on, a kernel virtual address
1071 * @bit: the bit of the word being waited on
1072 * @mode: the task state to sleep in
1073 * @timeout: timeout, in jiffies
1075 * Use the standard hashed waitqueue table to wait for a bit
1076 * to be cleared. This is similar to wait_on_bit(), except also takes a
1077 * timeout parameter.
1079 * Returned value will be zero if the bit was cleared before the
1080 * @timeout elapsed, or non-zero if the @timeout elapsed or process
1081 * received a signal and the mode permitted wakeup on that signal.
1084 wait_on_bit_timeout(unsigned long *word
, int bit
, unsigned mode
,
1085 unsigned long timeout
)
1088 if (!test_bit(bit
, word
))
1090 return out_of_line_wait_on_bit_timeout(word
, bit
,
1096 * wait_on_bit_action - wait for a bit to be cleared
1097 * @word: the word being waited on, a kernel virtual address
1098 * @bit: the bit of the word being waited on
1099 * @action: the function used to sleep, which may take special actions
1100 * @mode: the task state to sleep in
1102 * Use the standard hashed waitqueue table to wait for a bit
1103 * to be cleared, and allow the waiting action to be specified.
1104 * This is like wait_on_bit() but allows fine control of how the waiting
1107 * Returned value will be zero if the bit was cleared, or non-zero
1108 * if the process received a signal and the mode permitted wakeup
1112 wait_on_bit_action(unsigned long *word
, int bit
, wait_bit_action_f
*action
,
1116 if (!test_bit(bit
, word
))
1118 return out_of_line_wait_on_bit(word
, bit
, action
, mode
);
1122 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
1123 * @word: the word being waited on, a kernel virtual address
1124 * @bit: the bit of the word being waited on
1125 * @mode: the task state to sleep in
1127 * There is a standard hashed waitqueue table for generic use. This
1128 * is the part of the hashtable's accessor API that waits on a bit
1129 * when one intends to set it, for instance, trying to lock bitflags.
1130 * For instance, if one were to have waiters trying to set bitflag
1131 * and waiting for it to clear before setting it, one would call
1132 * wait_on_bit() in threads waiting to be able to set the bit.
1133 * One uses wait_on_bit_lock() where one is waiting for the bit to
1134 * clear with the intention of setting it, and when done, clearing it.
1136 * Returns zero if the bit was (eventually) found to be clear and was
1137 * set. Returns non-zero if a signal was delivered to the process and
1138 * the @mode allows that signal to wake the process.
1141 wait_on_bit_lock(unsigned long *word
, int bit
, unsigned mode
)
1144 if (!test_and_set_bit(bit
, word
))
1146 return out_of_line_wait_on_bit_lock(word
, bit
, bit_wait
, mode
);
1150 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
1151 * @word: the word being waited on, a kernel virtual address
1152 * @bit: the bit of the word being waited on
1153 * @mode: the task state to sleep in
1155 * Use the standard hashed waitqueue table to wait for a bit
1156 * to be cleared and then to atomically set it. This is similar
1157 * to wait_on_bit(), but calls io_schedule() instead of schedule()
1158 * for the actual waiting.
1160 * Returns zero if the bit was (eventually) found to be clear and was
1161 * set. Returns non-zero if a signal was delivered to the process and
1162 * the @mode allows that signal to wake the process.
1165 wait_on_bit_lock_io(unsigned long *word
, int bit
, unsigned mode
)
1168 if (!test_and_set_bit(bit
, word
))
1170 return out_of_line_wait_on_bit_lock(word
, bit
, bit_wait_io
, mode
);
1174 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
1175 * @word: the word being waited on, a kernel virtual address
1176 * @bit: the bit of the word being waited on
1177 * @action: the function used to sleep, which may take special actions
1178 * @mode: the task state to sleep in
1180 * Use the standard hashed waitqueue table to wait for a bit
1181 * to be cleared and then to set it, and allow the waiting action
1183 * This is like wait_on_bit() but allows fine control of how the waiting
1186 * Returns zero if the bit was (eventually) found to be clear and was
1187 * set. Returns non-zero if a signal was delivered to the process and
1188 * the @mode allows that signal to wake the process.
1191 wait_on_bit_lock_action(unsigned long *word
, int bit
, wait_bit_action_f
*action
,
1195 if (!test_and_set_bit(bit
, word
))
1197 return out_of_line_wait_on_bit_lock(word
, bit
, action
, mode
);
1201 * wait_on_atomic_t - Wait for an atomic_t to become 0
1202 * @val: The atomic value being waited on, a kernel virtual address
1203 * @action: the function used to sleep, which may take special actions
1204 * @mode: the task state to sleep in
1206 * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for
1207 * the purpose of getting a waitqueue, but we set the key to a bit number
1208 * outside of the target 'word'.
1211 int wait_on_atomic_t(atomic_t
*val
, int (*action
)(atomic_t
*), unsigned mode
)
1214 if (atomic_read(val
) == 0)
1216 return out_of_line_wait_on_atomic_t(val
, action
, mode
);
1219 #endif /* _LINUX_WAIT_H */