Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_WAIT_H |
| 2 | #define _LINUX_WAIT_H |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 3 | /* |
| 4 | * Linux wait queue related types and methods |
| 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/list.h> |
| 7 | #include <linux/stddef.h> |
| 8 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <asm/current.h> |
David Howells | 607ca46 | 2012-10-13 10:46:48 +0100 | [diff] [blame] | 10 | #include <uapi/linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | typedef struct __wait_queue wait_queue_t; |
Peter Zijlstra | 7d47872 | 2009-09-14 19:55:44 +0200 | [diff] [blame] | 13 | typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key); |
| 14 | int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Peter Zijlstra | 61ada52 | 2014-09-24 10:18:47 +0200 | [diff] [blame] | 16 | /* __wait_queue::flags */ |
| 17 | #define WQ_FLAG_EXCLUSIVE 0x01 |
| 18 | #define WQ_FLAG_WOKEN 0x02 |
| 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | struct __wait_queue { |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 21 | unsigned int flags; |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 22 | void *private; |
| 23 | wait_queue_func_t func; |
| 24 | struct list_head task_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | struct wait_bit_key { |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 28 | void *flags; |
| 29 | int bit_nr; |
| 30 | #define WAIT_ATOMIC_T_BIT_NR -1 |
NeilBrown | cbbce82 | 2014-09-25 13:55:19 +1000 | [diff] [blame] | 31 | unsigned long timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | struct wait_bit_queue { |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 35 | struct wait_bit_key key; |
| 36 | wait_queue_t wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct __wait_queue_head { |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 40 | spinlock_t lock; |
| 41 | struct list_head task_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | typedef struct __wait_queue_head wait_queue_head_t; |
| 44 | |
Tim Schmielau | 8c65b4a | 2005-11-07 00:59:43 -0800 | [diff] [blame] | 45 | struct task_struct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | /* |
| 48 | * Macros for declaration and initialisaton of the datatypes |
| 49 | */ |
| 50 | |
| 51 | #define __WAITQUEUE_INITIALIZER(name, tsk) { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 52 | .private = tsk, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | .func = default_wake_function, \ |
| 54 | .task_list = { NULL, NULL } } |
| 55 | |
| 56 | #define DECLARE_WAITQUEUE(name, tsk) \ |
| 57 | wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk) |
| 58 | |
| 59 | #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \ |
Ingo Molnar | e4d9191 | 2006-07-03 00:24:34 -0700 | [diff] [blame] | 60 | .lock = __SPIN_LOCK_UNLOCKED(name.lock), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | .task_list = { &(name).task_list, &(name).task_list } } |
| 62 | |
| 63 | #define DECLARE_WAIT_QUEUE_HEAD(name) \ |
| 64 | wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name) |
| 65 | |
| 66 | #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \ |
| 67 | { .flags = word, .bit_nr = bit, } |
| 68 | |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 69 | #define __WAIT_ATOMIC_T_KEY_INITIALIZER(p) \ |
| 70 | { .flags = p, .bit_nr = WAIT_ATOMIC_T_BIT_NR, } |
| 71 | |
Peter Zijlstra | f07fdec | 2011-12-13 13:20:54 +0100 | [diff] [blame] | 72 | extern void __init_waitqueue_head(wait_queue_head_t *q, const char *name, struct lock_class_key *); |
Peter Zijlstra | 2fc3911 | 2009-08-10 12:33:05 +0100 | [diff] [blame] | 73 | |
| 74 | #define init_waitqueue_head(q) \ |
| 75 | do { \ |
| 76 | static struct lock_class_key __key; \ |
| 77 | \ |
Peter Zijlstra | f07fdec | 2011-12-13 13:20:54 +0100 | [diff] [blame] | 78 | __init_waitqueue_head((q), #q, &__key); \ |
Peter Zijlstra | 2fc3911 | 2009-08-10 12:33:05 +0100 | [diff] [blame] | 79 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Peter Zijlstra | 7259f0d | 2006-10-29 22:46:36 -0800 | [diff] [blame] | 81 | #ifdef CONFIG_LOCKDEP |
| 82 | # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \ |
| 83 | ({ init_waitqueue_head(&name); name; }) |
| 84 | # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \ |
| 85 | wait_queue_head_t name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) |
| 86 | #else |
| 87 | # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name) |
| 88 | #endif |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p) |
| 91 | { |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 92 | q->flags = 0; |
| 93 | q->private = p; |
| 94 | q->func = default_wake_function; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 97 | static inline void |
| 98 | init_waitqueue_func_entry(wait_queue_t *q, wait_queue_func_t func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 100 | q->flags = 0; |
| 101 | q->private = NULL; |
| 102 | q->func = func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static inline int waitqueue_active(wait_queue_head_t *q) |
| 106 | { |
| 107 | return !list_empty(&q->task_list); |
| 108 | } |
| 109 | |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 110 | extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait); |
| 111 | extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait); |
| 112 | extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new) |
| 115 | { |
| 116 | list_add(&new->task_list, &head->task_list); |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Used for wake-one threads: |
| 121 | */ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 122 | static inline void |
| 123 | __add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait) |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 124 | { |
| 125 | wait->flags |= WQ_FLAG_EXCLUSIVE; |
| 126 | __add_wait_queue(q, wait); |
| 127 | } |
| 128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | static inline void __add_wait_queue_tail(wait_queue_head_t *head, |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 130 | wait_queue_t *new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | list_add_tail(&new->task_list, &head->task_list); |
| 133 | } |
| 134 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 135 | static inline void |
| 136 | __add_wait_queue_tail_exclusive(wait_queue_head_t *q, wait_queue_t *wait) |
Changli Gao | a93d2f1 | 2010-05-07 14:33:26 +0800 | [diff] [blame] | 137 | { |
| 138 | wait->flags |= WQ_FLAG_EXCLUSIVE; |
| 139 | __add_wait_queue_tail(q, wait); |
| 140 | } |
| 141 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 142 | static inline void |
| 143 | __remove_wait_queue(wait_queue_head_t *head, wait_queue_t *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | list_del(&old->task_list); |
| 146 | } |
| 147 | |
NeilBrown | c122132 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 148 | typedef int wait_bit_action_f(struct wait_bit_key *); |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 149 | void __wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key); |
Davide Libenzi | 4ede816 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 150 | void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key); |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 151 | void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode, int nr, void *key); |
Thomas Gleixner | 63b2001 | 2011-12-01 00:04:00 +0100 | [diff] [blame] | 152 | void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr); |
Davide Libenzi | 4ede816 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 153 | void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr); |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 154 | void __wake_up_bit(wait_queue_head_t *, void *, int); |
NeilBrown | c122132 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 155 | int __wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned); |
| 156 | int __wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, wait_bit_action_f *, unsigned); |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 157 | void wake_up_bit(void *, int); |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 158 | void wake_up_atomic_t(atomic_t *); |
NeilBrown | c122132 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 159 | int out_of_line_wait_on_bit(void *, int, wait_bit_action_f *, unsigned); |
NeilBrown | cbbce82 | 2014-09-25 13:55:19 +1000 | [diff] [blame] | 160 | int out_of_line_wait_on_bit_timeout(void *, int, wait_bit_action_f *, unsigned, unsigned long); |
NeilBrown | c122132 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 161 | int out_of_line_wait_on_bit_lock(void *, int, wait_bit_action_f *, unsigned); |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 162 | int out_of_line_wait_on_atomic_t(atomic_t *, int (*)(atomic_t *), unsigned); |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 163 | wait_queue_head_t *bit_waitqueue(void *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Matthew Wilcox | e64d66c | 2007-12-06 17:34:36 -0500 | [diff] [blame] | 165 | #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) |
| 166 | #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL) |
| 167 | #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL) |
Thomas Gleixner | 63b2001 | 2011-12-01 00:04:00 +0100 | [diff] [blame] | 168 | #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1) |
| 169 | #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0) |
Matthew Wilcox | e64d66c | 2007-12-06 17:34:36 -0500 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL) |
| 172 | #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL) |
| 173 | #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL) |
Matthew Wilcox | e64d66c | 2007-12-06 17:34:36 -0500 | [diff] [blame] | 174 | #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE, 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 176 | /* |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 177 | * Wakeup macros to be used to report events to the targets. |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 178 | */ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 179 | #define wake_up_poll(x, m) \ |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 180 | __wake_up(x, TASK_NORMAL, 1, (void *) (m)) |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 181 | #define wake_up_locked_poll(x, m) \ |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 182 | __wake_up_locked_key((x), TASK_NORMAL, (void *) (m)) |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 183 | #define wake_up_interruptible_poll(x, m) \ |
Davide Libenzi | c0da377 | 2009-03-31 15:24:20 -0700 | [diff] [blame] | 184 | __wake_up(x, TASK_INTERRUPTIBLE, 1, (void *) (m)) |
| 185 | #define wake_up_interruptible_sync_poll(x, m) \ |
| 186 | __wake_up_sync_key((x), TASK_INTERRUPTIBLE, 1, (void *) (m)) |
Peter Zijlstra | 0ccf831 | 2008-02-04 22:27:20 -0800 | [diff] [blame] | 187 | |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 188 | #define ___wait_cond_timeout(condition) \ |
Peter Zijlstra | 2953ef2 | 2013-10-02 11:22:19 +0200 | [diff] [blame] | 189 | ({ \ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 190 | bool __cond = (condition); \ |
| 191 | if (__cond && !__ret) \ |
| 192 | __ret = 1; \ |
| 193 | __cond || !__ret; \ |
Peter Zijlstra | 2953ef2 | 2013-10-02 11:22:19 +0200 | [diff] [blame] | 194 | }) |
| 195 | |
Oleg Nesterov | c2d8164 | 2013-10-07 18:18:24 +0200 | [diff] [blame] | 196 | #define ___wait_is_interruptible(state) \ |
| 197 | (!__builtin_constant_p(state) || \ |
| 198 | state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \ |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 199 | |
Peter Zijlstra | 8b32201 | 2014-04-18 15:07:17 -0700 | [diff] [blame] | 200 | /* |
| 201 | * The below macro ___wait_event() has an explicit shadow of the __ret |
| 202 | * variable when used from the wait_event_*() macros. |
| 203 | * |
| 204 | * This is so that both can use the ___wait_cond_timeout() construct |
| 205 | * to wrap the condition. |
| 206 | * |
| 207 | * The type inconsistency of the wait_event_*() __ret variable is also |
| 208 | * on purpose; we use long where we can return timeout values and int |
| 209 | * otherwise. |
| 210 | */ |
| 211 | |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 212 | #define ___wait_event(wq, condition, state, exclusive, ret, cmd) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 213 | ({ \ |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 214 | __label__ __out; \ |
Oleg Nesterov | c2d8164 | 2013-10-07 18:18:24 +0200 | [diff] [blame] | 215 | wait_queue_t __wait; \ |
Peter Zijlstra | 8b32201 | 2014-04-18 15:07:17 -0700 | [diff] [blame] | 216 | long __ret = ret; /* explicit shadow */ \ |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 217 | \ |
Oleg Nesterov | c2d8164 | 2013-10-07 18:18:24 +0200 | [diff] [blame] | 218 | INIT_LIST_HEAD(&__wait.task_list); \ |
| 219 | if (exclusive) \ |
| 220 | __wait.flags = WQ_FLAG_EXCLUSIVE; \ |
| 221 | else \ |
| 222 | __wait.flags = 0; \ |
| 223 | \ |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 224 | for (;;) { \ |
Oleg Nesterov | c2d8164 | 2013-10-07 18:18:24 +0200 | [diff] [blame] | 225 | long __int = prepare_to_wait_event(&wq, &__wait, state);\ |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 226 | \ |
| 227 | if (condition) \ |
| 228 | break; \ |
| 229 | \ |
Oleg Nesterov | c2d8164 | 2013-10-07 18:18:24 +0200 | [diff] [blame] | 230 | if (___wait_is_interruptible(state) && __int) { \ |
| 231 | __ret = __int; \ |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 232 | if (exclusive) { \ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 233 | abort_exclusive_wait(&wq, &__wait, \ |
| 234 | state, NULL); \ |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 235 | goto __out; \ |
| 236 | } \ |
| 237 | break; \ |
| 238 | } \ |
| 239 | \ |
| 240 | cmd; \ |
| 241 | } \ |
| 242 | finish_wait(&wq, &__wait); \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 243 | __out: __ret; \ |
| 244 | }) |
Peter Zijlstra | 41a1431 | 2013-10-02 11:22:21 +0200 | [diff] [blame] | 245 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 246 | #define __wait_event(wq, condition) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 247 | (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
| 248 | schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | /** |
| 251 | * wait_event - sleep until a condition gets true |
| 252 | * @wq: the waitqueue to wait on |
| 253 | * @condition: a C expression for the event to wait for |
| 254 | * |
| 255 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 256 | * @condition evaluates to true. The @condition is checked each time |
| 257 | * the waitqueue @wq is woken up. |
| 258 | * |
| 259 | * wake_up() has to be called after changing any variable that could |
| 260 | * change the result of the wait condition. |
| 261 | */ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 262 | #define wait_event(wq, condition) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | do { \ |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 264 | might_sleep(); \ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 265 | if (condition) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | break; \ |
| 267 | __wait_event(wq, condition); \ |
| 268 | } while (0) |
| 269 | |
Peter Zijlstra | 2c56124 | 2015-02-03 12:55:31 +0100 | [diff] [blame] | 270 | #define __io_wait_event(wq, condition) \ |
| 271 | (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
| 272 | io_schedule()) |
| 273 | |
| 274 | /* |
| 275 | * io_wait_event() -- like wait_event() but with io_schedule() |
| 276 | */ |
| 277 | #define io_wait_event(wq, condition) \ |
| 278 | do { \ |
| 279 | might_sleep(); \ |
| 280 | if (condition) \ |
| 281 | break; \ |
| 282 | __io_wait_event(wq, condition); \ |
| 283 | } while (0) |
| 284 | |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 285 | #define __wait_event_freezable(wq, condition) \ |
| 286 | ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \ |
| 287 | schedule(); try_to_freeze()) |
| 288 | |
| 289 | /** |
| 290 | * wait_event - sleep (or freeze) until a condition gets true |
| 291 | * @wq: the waitqueue to wait on |
| 292 | * @condition: a C expression for the event to wait for |
| 293 | * |
| 294 | * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute |
| 295 | * to system load) until the @condition evaluates to true. The |
| 296 | * @condition is checked each time the waitqueue @wq is woken up. |
| 297 | * |
| 298 | * wake_up() has to be called after changing any variable that could |
| 299 | * change the result of the wait condition. |
| 300 | */ |
| 301 | #define wait_event_freezable(wq, condition) \ |
| 302 | ({ \ |
| 303 | int __ret = 0; \ |
| 304 | might_sleep(); \ |
| 305 | if (!(condition)) \ |
| 306 | __ret = __wait_event_freezable(wq, condition); \ |
| 307 | __ret; \ |
| 308 | }) |
| 309 | |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 310 | #define __wait_event_timeout(wq, condition, timeout) \ |
| 311 | ___wait_event(wq, ___wait_cond_timeout(condition), \ |
| 312 | TASK_UNINTERRUPTIBLE, 0, timeout, \ |
| 313 | __ret = schedule_timeout(__ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| 315 | /** |
| 316 | * wait_event_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 |
| 320 | * |
| 321 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 322 | * @condition evaluates to true. The @condition is checked each time |
| 323 | * the waitqueue @wq is woken up. |
| 324 | * |
| 325 | * wake_up() has to be called after changing any variable that could |
| 326 | * change the result of the wait condition. |
| 327 | * |
Scot Doyle | 6b44f51 | 2014-08-24 17:12:27 +0000 | [diff] [blame] | 328 | * Returns: |
| 329 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 330 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 331 | * or the remaining jiffies (at least 1) if the @condition evaluated |
| 332 | * to %true before the @timeout elapsed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | */ |
| 334 | #define wait_event_timeout(wq, condition, timeout) \ |
| 335 | ({ \ |
| 336 | long __ret = timeout; \ |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 337 | might_sleep(); \ |
Oleg Nesterov | 8922915 | 2013-10-07 20:31:06 +0200 | [diff] [blame] | 338 | if (!___wait_cond_timeout(condition)) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 339 | __ret = __wait_event_timeout(wq, condition, timeout); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | __ret; \ |
| 341 | }) |
| 342 | |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 343 | #define __wait_event_freezable_timeout(wq, condition, timeout) \ |
| 344 | ___wait_event(wq, ___wait_cond_timeout(condition), \ |
| 345 | TASK_INTERRUPTIBLE, 0, timeout, \ |
| 346 | __ret = schedule_timeout(__ret); try_to_freeze()) |
| 347 | |
| 348 | /* |
| 349 | * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid |
| 350 | * increasing load and is freezable. |
| 351 | */ |
| 352 | #define wait_event_freezable_timeout(wq, condition, timeout) \ |
| 353 | ({ \ |
| 354 | long __ret = timeout; \ |
| 355 | might_sleep(); \ |
| 356 | if (!___wait_cond_timeout(condition)) \ |
| 357 | __ret = __wait_event_freezable_timeout(wq, condition, timeout); \ |
| 358 | __ret; \ |
| 359 | }) |
| 360 | |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 361 | #define __wait_event_cmd(wq, condition, cmd1, cmd2) \ |
| 362 | (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
| 363 | cmd1; schedule(); cmd2) |
| 364 | |
| 365 | /** |
| 366 | * wait_event_cmd - sleep until a condition gets true |
| 367 | * @wq: the waitqueue to wait on |
| 368 | * @condition: a C expression for the event to wait for |
Masanari Iida | f434f7a | 2014-01-22 01:22:06 +0900 | [diff] [blame] | 369 | * @cmd1: the command will be executed before sleep |
| 370 | * @cmd2: the command will be executed after sleep |
Shaohua Li | 82e06c8 | 2013-11-14 15:16:16 +1100 | [diff] [blame] | 371 | * |
| 372 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 373 | * @condition evaluates to true. The @condition is checked each time |
| 374 | * the waitqueue @wq is woken up. |
| 375 | * |
| 376 | * wake_up() has to be called after changing any variable that could |
| 377 | * change the result of the wait condition. |
| 378 | */ |
| 379 | #define wait_event_cmd(wq, condition, cmd1, cmd2) \ |
| 380 | do { \ |
| 381 | if (condition) \ |
| 382 | break; \ |
| 383 | __wait_event_cmd(wq, condition, cmd1, cmd2); \ |
| 384 | } while (0) |
| 385 | |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 386 | #define __wait_event_interruptible(wq, condition) \ |
| 387 | ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | f13f4c4 | 2013-10-02 11:22:24 +0200 | [diff] [blame] | 388 | schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * wait_event_interruptible - sleep until a condition gets true |
| 392 | * @wq: the waitqueue to wait on |
| 393 | * @condition: a C expression for the event to wait for |
| 394 | * |
| 395 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 396 | * @condition evaluates to true or a signal is received. |
| 397 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 398 | * |
| 399 | * wake_up() has to be called after changing any variable that could |
| 400 | * change the result of the wait condition. |
| 401 | * |
| 402 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 403 | * signal and 0 if @condition evaluated to true. |
| 404 | */ |
| 405 | #define wait_event_interruptible(wq, condition) \ |
| 406 | ({ \ |
| 407 | int __ret = 0; \ |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 408 | might_sleep(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | if (!(condition)) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 410 | __ret = __wait_event_interruptible(wq, condition); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | __ret; \ |
| 412 | }) |
| 413 | |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 414 | #define __wait_event_interruptible_timeout(wq, condition, timeout) \ |
| 415 | ___wait_event(wq, ___wait_cond_timeout(condition), \ |
| 416 | TASK_INTERRUPTIBLE, 0, timeout, \ |
| 417 | __ret = schedule_timeout(__ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | /** |
| 420 | * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses |
| 421 | * @wq: the waitqueue to wait on |
| 422 | * @condition: a C expression for the event to wait for |
| 423 | * @timeout: timeout, in jiffies |
| 424 | * |
| 425 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 426 | * @condition evaluates to true or a signal is received. |
| 427 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 428 | * |
| 429 | * wake_up() has to be called after changing any variable that could |
| 430 | * change the result of the wait condition. |
| 431 | * |
Imre Deak | 4c663cf | 2013-05-24 15:55:09 -0700 | [diff] [blame] | 432 | * Returns: |
Scot Doyle | 6b44f51 | 2014-08-24 17:12:27 +0000 | [diff] [blame] | 433 | * 0 if the @condition evaluated to %false after the @timeout elapsed, |
| 434 | * 1 if the @condition evaluated to %true after the @timeout elapsed, |
| 435 | * the remaining jiffies (at least 1) if the @condition evaluated |
| 436 | * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was |
| 437 | * interrupted by a signal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | */ |
| 439 | #define wait_event_interruptible_timeout(wq, condition, timeout) \ |
| 440 | ({ \ |
| 441 | long __ret = timeout; \ |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 442 | might_sleep(); \ |
Oleg Nesterov | 8922915 | 2013-10-07 20:31:06 +0200 | [diff] [blame] | 443 | if (!___wait_cond_timeout(condition)) \ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 444 | __ret = __wait_event_interruptible_timeout(wq, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 445 | condition, timeout); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | __ret; \ |
| 447 | }) |
| 448 | |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 449 | #define __wait_event_hrtimeout(wq, condition, timeout, state) \ |
| 450 | ({ \ |
| 451 | int __ret = 0; \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 452 | struct hrtimer_sleeper __t; \ |
| 453 | \ |
| 454 | hrtimer_init_on_stack(&__t.timer, CLOCK_MONOTONIC, \ |
| 455 | HRTIMER_MODE_REL); \ |
| 456 | hrtimer_init_sleeper(&__t, current); \ |
| 457 | if ((timeout).tv64 != KTIME_MAX) \ |
| 458 | hrtimer_start_range_ns(&__t.timer, timeout, \ |
| 459 | current->timer_slack_ns, \ |
| 460 | HRTIMER_MODE_REL); \ |
| 461 | \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 462 | __ret = ___wait_event(wq, condition, state, 0, 0, \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 463 | if (!__t.task) { \ |
| 464 | __ret = -ETIME; \ |
| 465 | break; \ |
| 466 | } \ |
Peter Zijlstra | ebdc195 | 2013-10-02 11:22:32 +0200 | [diff] [blame] | 467 | schedule()); \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 468 | \ |
| 469 | hrtimer_cancel(&__t.timer); \ |
| 470 | destroy_hrtimer_on_stack(&__t.timer); \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 471 | __ret; \ |
| 472 | }) |
| 473 | |
| 474 | /** |
| 475 | * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses |
| 476 | * @wq: the waitqueue to wait on |
| 477 | * @condition: a C expression for the event to wait for |
| 478 | * @timeout: timeout, as a ktime_t |
| 479 | * |
| 480 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 481 | * @condition evaluates to true or a signal is received. |
| 482 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 483 | * |
| 484 | * wake_up() has to be called after changing any variable that could |
| 485 | * change the result of the wait condition. |
| 486 | * |
| 487 | * The function returns 0 if @condition became true, or -ETIME if the timeout |
| 488 | * elapsed. |
| 489 | */ |
| 490 | #define wait_event_hrtimeout(wq, condition, timeout) \ |
| 491 | ({ \ |
| 492 | int __ret = 0; \ |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 493 | might_sleep(); \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 494 | if (!(condition)) \ |
| 495 | __ret = __wait_event_hrtimeout(wq, condition, timeout, \ |
| 496 | TASK_UNINTERRUPTIBLE); \ |
| 497 | __ret; \ |
| 498 | }) |
| 499 | |
| 500 | /** |
| 501 | * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses |
| 502 | * @wq: the waitqueue to wait on |
| 503 | * @condition: a C expression for the event to wait for |
| 504 | * @timeout: timeout, as a ktime_t |
| 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 | * wake_up() has to be called after changing any variable that could |
| 511 | * change the result of the wait condition. |
| 512 | * |
| 513 | * The function returns 0 if @condition became true, -ERESTARTSYS if it was |
| 514 | * interrupted by a signal, or -ETIME if the timeout elapsed. |
| 515 | */ |
| 516 | #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \ |
| 517 | ({ \ |
| 518 | long __ret = 0; \ |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 519 | might_sleep(); \ |
Kent Overstreet | 774a08b | 2013-05-07 16:18:43 -0700 | [diff] [blame] | 520 | if (!(condition)) \ |
| 521 | __ret = __wait_event_hrtimeout(wq, condition, timeout, \ |
| 522 | TASK_INTERRUPTIBLE); \ |
| 523 | __ret; \ |
| 524 | }) |
| 525 | |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 526 | #define __wait_event_interruptible_exclusive(wq, condition) \ |
| 527 | ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \ |
Peter Zijlstra | 48c2521 | 2013-10-02 11:22:26 +0200 | [diff] [blame] | 528 | schedule()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
| 530 | #define wait_event_interruptible_exclusive(wq, condition) \ |
| 531 | ({ \ |
| 532 | int __ret = 0; \ |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 533 | might_sleep(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (!(condition)) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 535 | __ret = __wait_event_interruptible_exclusive(wq, condition);\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | __ret; \ |
| 537 | }) |
| 538 | |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 539 | |
Peter Zijlstra | 36df04b | 2014-10-29 12:21:57 +0100 | [diff] [blame] | 540 | #define __wait_event_freezable_exclusive(wq, condition) \ |
| 541 | ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \ |
| 542 | schedule(); try_to_freeze()) |
| 543 | |
| 544 | #define wait_event_freezable_exclusive(wq, condition) \ |
| 545 | ({ \ |
| 546 | int __ret = 0; \ |
| 547 | might_sleep(); \ |
| 548 | if (!(condition)) \ |
| 549 | __ret = __wait_event_freezable_exclusive(wq, condition);\ |
| 550 | __ret; \ |
| 551 | }) |
| 552 | |
| 553 | |
Michal Nazarewicz | 22c43c8 | 2010-05-05 12:53:11 +0200 | [diff] [blame] | 554 | #define __wait_event_interruptible_locked(wq, condition, exclusive, irq) \ |
| 555 | ({ \ |
| 556 | int __ret = 0; \ |
| 557 | DEFINE_WAIT(__wait); \ |
| 558 | if (exclusive) \ |
| 559 | __wait.flags |= WQ_FLAG_EXCLUSIVE; \ |
| 560 | do { \ |
| 561 | if (likely(list_empty(&__wait.task_list))) \ |
| 562 | __add_wait_queue_tail(&(wq), &__wait); \ |
| 563 | set_current_state(TASK_INTERRUPTIBLE); \ |
| 564 | if (signal_pending(current)) { \ |
| 565 | __ret = -ERESTARTSYS; \ |
| 566 | break; \ |
| 567 | } \ |
| 568 | if (irq) \ |
| 569 | spin_unlock_irq(&(wq).lock); \ |
| 570 | else \ |
| 571 | spin_unlock(&(wq).lock); \ |
| 572 | schedule(); \ |
| 573 | if (irq) \ |
| 574 | spin_lock_irq(&(wq).lock); \ |
| 575 | else \ |
| 576 | spin_lock(&(wq).lock); \ |
| 577 | } while (!(condition)); \ |
| 578 | __remove_wait_queue(&(wq), &__wait); \ |
| 579 | __set_current_state(TASK_RUNNING); \ |
| 580 | __ret; \ |
| 581 | }) |
| 582 | |
| 583 | |
| 584 | /** |
| 585 | * wait_event_interruptible_locked - sleep until a condition gets true |
| 586 | * @wq: the waitqueue to wait on |
| 587 | * @condition: a C expression for the event to wait for |
| 588 | * |
| 589 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 590 | * @condition evaluates to true or a signal is received. |
| 591 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 592 | * |
| 593 | * It must be called with wq.lock being held. This spinlock is |
| 594 | * unlocked while sleeping but @condition testing is done while lock |
| 595 | * is held and when this macro exits the lock is held. |
| 596 | * |
| 597 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 598 | * functions which must match the way they are locked/unlocked outside |
| 599 | * of this macro. |
| 600 | * |
| 601 | * wake_up_locked() has to be called after changing any variable that could |
| 602 | * change the result of the wait condition. |
| 603 | * |
| 604 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 605 | * signal and 0 if @condition evaluated to true. |
| 606 | */ |
| 607 | #define wait_event_interruptible_locked(wq, condition) \ |
| 608 | ((condition) \ |
| 609 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 0)) |
| 610 | |
| 611 | /** |
| 612 | * wait_event_interruptible_locked_irq - sleep until a condition gets true |
| 613 | * @wq: the waitqueue to wait on |
| 614 | * @condition: a C expression for the event to wait for |
| 615 | * |
| 616 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 617 | * @condition evaluates to true or a signal is received. |
| 618 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 619 | * |
| 620 | * It must be called with wq.lock being held. This spinlock is |
| 621 | * unlocked while sleeping but @condition testing is done while lock |
| 622 | * is held and when this macro exits the lock is held. |
| 623 | * |
| 624 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 625 | * functions which must match the way they are locked/unlocked outside |
| 626 | * of this macro. |
| 627 | * |
| 628 | * wake_up_locked() has to be called after changing any variable that could |
| 629 | * change the result of the wait condition. |
| 630 | * |
| 631 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 632 | * signal and 0 if @condition evaluated to true. |
| 633 | */ |
| 634 | #define wait_event_interruptible_locked_irq(wq, condition) \ |
| 635 | ((condition) \ |
| 636 | ? 0 : __wait_event_interruptible_locked(wq, condition, 0, 1)) |
| 637 | |
| 638 | /** |
| 639 | * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true |
| 640 | * @wq: the waitqueue to wait on |
| 641 | * @condition: a C expression for the event to wait for |
| 642 | * |
| 643 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 644 | * @condition evaluates to true or a signal is received. |
| 645 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 646 | * |
| 647 | * It must be called with wq.lock being held. This spinlock is |
| 648 | * unlocked while sleeping but @condition testing is done while lock |
| 649 | * is held and when this macro exits the lock is held. |
| 650 | * |
| 651 | * The lock is locked/unlocked using spin_lock()/spin_unlock() |
| 652 | * functions which must match the way they are locked/unlocked outside |
| 653 | * of this macro. |
| 654 | * |
| 655 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 656 | * set thus when other process waits process on the list if this |
| 657 | * process is awaken further processes are not considered. |
| 658 | * |
| 659 | * wake_up_locked() has to be called after changing any variable that could |
| 660 | * change the result of the wait condition. |
| 661 | * |
| 662 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 663 | * signal and 0 if @condition evaluated to true. |
| 664 | */ |
| 665 | #define wait_event_interruptible_exclusive_locked(wq, condition) \ |
| 666 | ((condition) \ |
| 667 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 0)) |
| 668 | |
| 669 | /** |
| 670 | * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true |
| 671 | * @wq: the waitqueue to wait on |
| 672 | * @condition: a C expression for the event to wait for |
| 673 | * |
| 674 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 675 | * @condition evaluates to true or a signal is received. |
| 676 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 677 | * |
| 678 | * It must be called with wq.lock being held. This spinlock is |
| 679 | * unlocked while sleeping but @condition testing is done while lock |
| 680 | * is held and when this macro exits the lock is held. |
| 681 | * |
| 682 | * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq() |
| 683 | * functions which must match the way they are locked/unlocked outside |
| 684 | * of this macro. |
| 685 | * |
| 686 | * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag |
| 687 | * set thus when other process waits process on the list if this |
| 688 | * process is awaken further processes are not considered. |
| 689 | * |
| 690 | * wake_up_locked() has to be called after changing any variable that could |
| 691 | * change the result of the wait condition. |
| 692 | * |
| 693 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 694 | * signal and 0 if @condition evaluated to true. |
| 695 | */ |
| 696 | #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \ |
| 697 | ((condition) \ |
| 698 | ? 0 : __wait_event_interruptible_locked(wq, condition, 1, 1)) |
| 699 | |
| 700 | |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 701 | #define __wait_event_killable(wq, condition) \ |
| 702 | ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule()) |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 703 | |
| 704 | /** |
| 705 | * wait_event_killable - sleep until a condition gets true |
| 706 | * @wq: the waitqueue to wait on |
| 707 | * @condition: a C expression for the event to wait for |
| 708 | * |
| 709 | * The process is put to sleep (TASK_KILLABLE) until the |
| 710 | * @condition evaluates to true or a signal is received. |
| 711 | * The @condition is checked each time the waitqueue @wq is woken up. |
| 712 | * |
| 713 | * wake_up() has to be called after changing any variable that could |
| 714 | * change the result of the wait condition. |
| 715 | * |
| 716 | * The function will return -ERESTARTSYS if it was interrupted by a |
| 717 | * signal and 0 if @condition evaluated to true. |
| 718 | */ |
| 719 | #define wait_event_killable(wq, condition) \ |
| 720 | ({ \ |
| 721 | int __ret = 0; \ |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 722 | might_sleep(); \ |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 723 | if (!(condition)) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 724 | __ret = __wait_event_killable(wq, condition); \ |
Matthew Wilcox | 1411d5a | 2007-12-06 12:00:00 -0500 | [diff] [blame] | 725 | __ret; \ |
| 726 | }) |
| 727 | |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 728 | |
| 729 | #define __wait_event_lock_irq(wq, condition, lock, cmd) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 730 | (void)___wait_event(wq, condition, TASK_UNINTERRUPTIBLE, 0, 0, \ |
| 731 | spin_unlock_irq(&lock); \ |
| 732 | cmd; \ |
| 733 | schedule(); \ |
| 734 | spin_lock_irq(&lock)) |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 735 | |
| 736 | /** |
| 737 | * wait_event_lock_irq_cmd - sleep until a condition gets true. The |
| 738 | * condition is checked under the lock. This |
| 739 | * is expected to be called with the lock |
| 740 | * taken. |
| 741 | * @wq: the waitqueue to wait on |
| 742 | * @condition: a C expression for the event to wait for |
| 743 | * @lock: a locked spinlock_t, which will be released before cmd |
| 744 | * and schedule() and reacquired afterwards. |
| 745 | * @cmd: a command which is invoked outside the critical section before |
| 746 | * sleep |
| 747 | * |
| 748 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 749 | * @condition evaluates to true. The @condition is checked each time |
| 750 | * the waitqueue @wq is woken up. |
| 751 | * |
| 752 | * wake_up() has to be called after changing any variable that could |
| 753 | * change the result of the wait condition. |
| 754 | * |
| 755 | * This is supposed to be called while holding the lock. The lock is |
| 756 | * dropped before invoking the cmd and going to sleep and is reacquired |
| 757 | * afterwards. |
| 758 | */ |
| 759 | #define wait_event_lock_irq_cmd(wq, condition, lock, cmd) \ |
| 760 | do { \ |
| 761 | if (condition) \ |
| 762 | break; \ |
| 763 | __wait_event_lock_irq(wq, condition, lock, cmd); \ |
| 764 | } while (0) |
| 765 | |
| 766 | /** |
| 767 | * wait_event_lock_irq - sleep until a condition gets true. The |
| 768 | * condition is checked under the lock. This |
| 769 | * is expected to be called with the lock |
| 770 | * taken. |
| 771 | * @wq: the waitqueue to wait on |
| 772 | * @condition: a C expression for the event to wait for |
| 773 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 774 | * and reacquired afterwards. |
| 775 | * |
| 776 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 777 | * @condition evaluates to true. The @condition is checked each time |
| 778 | * the waitqueue @wq is woken up. |
| 779 | * |
| 780 | * wake_up() has to be called after changing any variable that could |
| 781 | * change the result of the wait condition. |
| 782 | * |
| 783 | * This is supposed to be called while holding the lock. The lock is |
| 784 | * dropped before going to sleep and is reacquired afterwards. |
| 785 | */ |
| 786 | #define wait_event_lock_irq(wq, condition, lock) \ |
| 787 | do { \ |
| 788 | if (condition) \ |
| 789 | break; \ |
| 790 | __wait_event_lock_irq(wq, condition, lock, ); \ |
| 791 | } while (0) |
| 792 | |
| 793 | |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 794 | #define __wait_event_interruptible_lock_irq(wq, condition, lock, cmd) \ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 795 | ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 0, 0, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 796 | spin_unlock_irq(&lock); \ |
| 797 | cmd; \ |
| 798 | schedule(); \ |
Peter Zijlstra | 8fbd88f | 2013-10-02 11:22:28 +0200 | [diff] [blame] | 799 | spin_lock_irq(&lock)) |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 800 | |
| 801 | /** |
| 802 | * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true. |
| 803 | * The condition is checked under the lock. This is expected to |
| 804 | * be called with the lock taken. |
| 805 | * @wq: the waitqueue to wait on |
| 806 | * @condition: a C expression for the event to wait for |
| 807 | * @lock: a locked spinlock_t, which will be released before cmd and |
| 808 | * schedule() and reacquired afterwards. |
| 809 | * @cmd: a command which is invoked outside the critical section before |
| 810 | * sleep |
| 811 | * |
| 812 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 813 | * @condition evaluates to true or a signal is received. The @condition is |
| 814 | * checked each time the waitqueue @wq is woken up. |
| 815 | * |
| 816 | * wake_up() has to be called after changing any variable that could |
| 817 | * change the result of the wait condition. |
| 818 | * |
| 819 | * This is supposed to be called while holding the lock. The lock is |
| 820 | * dropped before invoking the cmd and going to sleep and is reacquired |
| 821 | * afterwards. |
| 822 | * |
| 823 | * The macro will return -ERESTARTSYS if it was interrupted by a signal |
| 824 | * and 0 if @condition evaluated to true. |
| 825 | */ |
| 826 | #define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd) \ |
| 827 | ({ \ |
| 828 | int __ret = 0; \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 829 | if (!(condition)) \ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 830 | __ret = __wait_event_interruptible_lock_irq(wq, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 831 | condition, lock, cmd); \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 832 | __ret; \ |
| 833 | }) |
| 834 | |
| 835 | /** |
| 836 | * wait_event_interruptible_lock_irq - sleep until a condition gets true. |
| 837 | * The condition is checked under the lock. This is expected |
| 838 | * to be called with the lock taken. |
| 839 | * @wq: the waitqueue to wait on |
| 840 | * @condition: a C expression for the event to wait for |
| 841 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 842 | * and reacquired afterwards. |
| 843 | * |
| 844 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 845 | * @condition evaluates to true or signal is received. The @condition is |
| 846 | * checked each time the waitqueue @wq is woken up. |
| 847 | * |
| 848 | * wake_up() has to be called after changing any variable that could |
| 849 | * change the result of the wait condition. |
| 850 | * |
| 851 | * This is supposed to be called while holding the lock. The lock is |
| 852 | * dropped before going to sleep and is reacquired afterwards. |
| 853 | * |
| 854 | * The macro will return -ERESTARTSYS if it was interrupted by a signal |
| 855 | * and 0 if @condition evaluated to true. |
| 856 | */ |
| 857 | #define wait_event_interruptible_lock_irq(wq, condition, lock) \ |
| 858 | ({ \ |
| 859 | int __ret = 0; \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 860 | if (!(condition)) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 861 | __ret = __wait_event_interruptible_lock_irq(wq, \ |
Thierry Reding | 92ec118 | 2013-10-23 13:40:55 +0200 | [diff] [blame] | 862 | condition, lock,); \ |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 863 | __ret; \ |
| 864 | }) |
| 865 | |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 866 | #define __wait_event_interruptible_lock_irq_timeout(wq, condition, \ |
| 867 | lock, timeout) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 868 | ___wait_event(wq, ___wait_cond_timeout(condition), \ |
Heiko Carstens | 7d71645 | 2013-10-31 12:48:14 +0100 | [diff] [blame] | 869 | TASK_INTERRUPTIBLE, 0, timeout, \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 870 | spin_unlock_irq(&lock); \ |
| 871 | __ret = schedule_timeout(__ret); \ |
Peter Zijlstra | a1dc685 | 2013-10-02 11:22:29 +0200 | [diff] [blame] | 872 | spin_lock_irq(&lock)); |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 873 | |
| 874 | /** |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 875 | * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets |
| 876 | * true or a timeout elapses. The condition is checked under |
| 877 | * the lock. This is expected to be called with the lock taken. |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 878 | * @wq: the waitqueue to wait on |
| 879 | * @condition: a C expression for the event to wait for |
| 880 | * @lock: a locked spinlock_t, which will be released before schedule() |
| 881 | * and reacquired afterwards. |
| 882 | * @timeout: timeout, in jiffies |
| 883 | * |
| 884 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 885 | * @condition evaluates to true or signal is received. The @condition is |
| 886 | * checked each time the waitqueue @wq is woken up. |
| 887 | * |
| 888 | * wake_up() has to be called after changing any variable that could |
| 889 | * change the result of the wait condition. |
| 890 | * |
| 891 | * This is supposed to be called while holding the lock. The lock is |
| 892 | * dropped before going to sleep and is reacquired afterwards. |
| 893 | * |
| 894 | * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it |
| 895 | * was interrupted by a signal, and the remaining jiffies otherwise |
| 896 | * if the condition evaluated to true before the timeout elapsed. |
| 897 | */ |
| 898 | #define wait_event_interruptible_lock_irq_timeout(wq, condition, lock, \ |
| 899 | timeout) \ |
| 900 | ({ \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 901 | long __ret = timeout; \ |
Oleg Nesterov | 8922915 | 2013-10-07 20:31:06 +0200 | [diff] [blame] | 902 | if (!___wait_cond_timeout(condition)) \ |
Peter Zijlstra | 35a2af9 | 2013-10-02 11:22:33 +0200 | [diff] [blame] | 903 | __ret = __wait_event_interruptible_lock_irq_timeout( \ |
| 904 | wq, condition, lock, timeout); \ |
Martin Peschke | d79ff14 | 2013-08-22 17:45:36 +0200 | [diff] [blame] | 905 | __ret; \ |
| 906 | }) |
| 907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | /* |
| 909 | * Waitqueues which are removed from the waitqueue_head at wakeup time |
| 910 | */ |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 911 | void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state); |
| 912 | void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state); |
Oleg Nesterov | c2d8164 | 2013-10-07 18:18:24 +0200 | [diff] [blame] | 913 | long prepare_to_wait_event(wait_queue_head_t *q, wait_queue_t *wait, int state); |
Harvey Harrison | b3c9752 | 2008-02-13 15:03:15 -0800 | [diff] [blame] | 914 | void finish_wait(wait_queue_head_t *q, wait_queue_t *wait); |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 915 | void abort_exclusive_wait(wait_queue_head_t *q, wait_queue_t *wait, unsigned int mode, void *key); |
Peter Zijlstra | 61ada52 | 2014-09-24 10:18:47 +0200 | [diff] [blame] | 916 | long wait_woken(wait_queue_t *wait, unsigned mode, long timeout); |
| 917 | int woken_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key); |
| 919 | int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key); |
| 920 | |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 921 | #define DEFINE_WAIT_FUNC(name, function) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | wait_queue_t name = { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 923 | .private = current, \ |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 924 | .func = function, \ |
blaisorblade@yahoo.it | 7e43c84 | 2005-05-25 01:31:42 +0200 | [diff] [blame] | 925 | .task_list = LIST_HEAD_INIT((name).task_list), \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Eric Dumazet | bf368e4 | 2009-04-28 02:24:21 -0700 | [diff] [blame] | 928 | #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function) |
| 929 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | #define DEFINE_WAIT_BIT(name, word, bit) \ |
| 931 | struct wait_bit_queue name = { \ |
| 932 | .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \ |
| 933 | .wait = { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 934 | .private = current, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | .func = wake_bit_function, \ |
| 936 | .task_list = \ |
| 937 | LIST_HEAD_INIT((name).wait.task_list), \ |
| 938 | }, \ |
| 939 | } |
| 940 | |
| 941 | #define init_wait(wait) \ |
| 942 | do { \ |
Benjamin LaHaise | c43dc2f | 2005-06-23 00:10:27 -0700 | [diff] [blame] | 943 | (wait)->private = current; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | (wait)->func = autoremove_wake_function; \ |
| 945 | INIT_LIST_HEAD(&(wait)->task_list); \ |
Evgeny Kuznetsov | 231d0ae | 2010-10-05 12:47:57 +0400 | [diff] [blame] | 946 | (wait)->flags = 0; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | } while (0) |
| 948 | |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 949 | |
NeilBrown | c122132 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 950 | extern int bit_wait(struct wait_bit_key *); |
| 951 | extern int bit_wait_io(struct wait_bit_key *); |
NeilBrown | cbbce82 | 2014-09-25 13:55:19 +1000 | [diff] [blame] | 952 | extern int bit_wait_timeout(struct wait_bit_key *); |
| 953 | extern int bit_wait_io_timeout(struct wait_bit_key *); |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | /** |
| 956 | * wait_on_bit - wait for a bit to be cleared |
| 957 | * @word: the word being waited on, a kernel virtual address |
| 958 | * @bit: the bit of the word being waited on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | * @mode: the task state to sleep in |
| 960 | * |
| 961 | * There is a standard hashed waitqueue table for generic use. This |
| 962 | * is the part of the hashtable's accessor API that waits on a bit. |
| 963 | * For instance, if one were to have waiters on a bitflag, one would |
| 964 | * call wait_on_bit() in threads waiting for the bit to clear. |
| 965 | * One uses wait_on_bit() where one is waiting for the bit to clear, |
| 966 | * but has no intention of setting it. |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 967 | * Returned value will be zero if the bit was cleared, or non-zero |
| 968 | * if the process received a signal and the mode permitted wakeup |
| 969 | * on that signal. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | */ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 971 | static inline int |
Palmer Dabbelt | 7e60598 | 2015-04-30 21:19:56 -0700 | [diff] [blame] | 972 | wait_on_bit(unsigned long *word, int bit, unsigned mode) |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 973 | { |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 974 | might_sleep(); |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 975 | if (!test_bit(bit, word)) |
| 976 | return 0; |
| 977 | return out_of_line_wait_on_bit(word, bit, |
| 978 | bit_wait, |
| 979 | mode); |
| 980 | } |
| 981 | |
| 982 | /** |
| 983 | * wait_on_bit_io - wait for a bit to be cleared |
| 984 | * @word: the word being waited on, a kernel virtual address |
| 985 | * @bit: the bit of the word being waited on |
| 986 | * @mode: the task state to sleep in |
| 987 | * |
| 988 | * Use the standard hashed waitqueue table to wait for a bit |
| 989 | * to be cleared. This is similar to wait_on_bit(), but calls |
| 990 | * io_schedule() instead of schedule() for the actual waiting. |
| 991 | * |
| 992 | * Returned value will be zero if the bit was cleared, or non-zero |
| 993 | * if the process received a signal and the mode permitted wakeup |
| 994 | * on that signal. |
| 995 | */ |
| 996 | static inline int |
Palmer Dabbelt | 7e60598 | 2015-04-30 21:19:56 -0700 | [diff] [blame] | 997 | wait_on_bit_io(unsigned long *word, int bit, unsigned mode) |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 998 | { |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 999 | might_sleep(); |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1000 | if (!test_bit(bit, word)) |
| 1001 | return 0; |
| 1002 | return out_of_line_wait_on_bit(word, bit, |
| 1003 | bit_wait_io, |
| 1004 | mode); |
| 1005 | } |
| 1006 | |
| 1007 | /** |
Johan Hedberg | 44fc0e5 | 2015-01-30 13:14:36 +0200 | [diff] [blame] | 1008 | * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses |
| 1009 | * @word: the word being waited on, a kernel virtual address |
| 1010 | * @bit: the bit of the word being waited on |
| 1011 | * @mode: the task state to sleep in |
| 1012 | * @timeout: timeout, in jiffies |
| 1013 | * |
| 1014 | * Use the standard hashed waitqueue table to wait for a bit |
| 1015 | * to be cleared. This is similar to wait_on_bit(), except also takes a |
| 1016 | * timeout parameter. |
| 1017 | * |
| 1018 | * Returned value will be zero if the bit was cleared before the |
| 1019 | * @timeout elapsed, or non-zero if the @timeout elapsed or process |
| 1020 | * received a signal and the mode permitted wakeup on that signal. |
| 1021 | */ |
| 1022 | static inline int |
Palmer Dabbelt | 7e60598 | 2015-04-30 21:19:56 -0700 | [diff] [blame] | 1023 | wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode, |
| 1024 | unsigned long timeout) |
Johan Hedberg | 44fc0e5 | 2015-01-30 13:14:36 +0200 | [diff] [blame] | 1025 | { |
| 1026 | might_sleep(); |
| 1027 | if (!test_bit(bit, word)) |
| 1028 | return 0; |
| 1029 | return out_of_line_wait_on_bit_timeout(word, bit, |
| 1030 | bit_wait_timeout, |
| 1031 | mode, timeout); |
| 1032 | } |
| 1033 | |
| 1034 | /** |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1035 | * wait_on_bit_action - wait for a bit to be cleared |
| 1036 | * @word: the word being waited on, a kernel virtual address |
| 1037 | * @bit: the bit of the word being waited on |
| 1038 | * @action: the function used to sleep, which may take special actions |
| 1039 | * @mode: the task state to sleep in |
| 1040 | * |
| 1041 | * Use the standard hashed waitqueue table to wait for a bit |
| 1042 | * to be cleared, and allow the waiting action to be specified. |
| 1043 | * This is like wait_on_bit() but allows fine control of how the waiting |
| 1044 | * is done. |
| 1045 | * |
| 1046 | * Returned value will be zero if the bit was cleared, or non-zero |
| 1047 | * if the process received a signal and the mode permitted wakeup |
| 1048 | * on that signal. |
| 1049 | */ |
| 1050 | static inline int |
Palmer Dabbelt | 7e60598 | 2015-04-30 21:19:56 -0700 | [diff] [blame] | 1051 | wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action, |
| 1052 | unsigned mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | { |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 1054 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | if (!test_bit(bit, word)) |
| 1056 | return 0; |
| 1057 | return out_of_line_wait_on_bit(word, bit, action, mode); |
| 1058 | } |
| 1059 | |
| 1060 | /** |
| 1061 | * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it |
| 1062 | * @word: the word being waited on, a kernel virtual address |
| 1063 | * @bit: the bit of the word being waited on |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | * @mode: the task state to sleep in |
| 1065 | * |
| 1066 | * There is a standard hashed waitqueue table for generic use. This |
| 1067 | * is the part of the hashtable's accessor API that waits on a bit |
| 1068 | * when one intends to set it, for instance, trying to lock bitflags. |
| 1069 | * For instance, if one were to have waiters trying to set bitflag |
| 1070 | * and waiting for it to clear before setting it, one would call |
| 1071 | * wait_on_bit() in threads waiting to be able to set the bit. |
| 1072 | * One uses wait_on_bit_lock() where one is waiting for the bit to |
| 1073 | * clear with the intention of setting it, and when done, clearing it. |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1074 | * |
| 1075 | * Returns zero if the bit was (eventually) found to be clear and was |
| 1076 | * set. Returns non-zero if a signal was delivered to the process and |
| 1077 | * the @mode allows that signal to wake the process. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | */ |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 1079 | static inline int |
Palmer Dabbelt | 7e60598 | 2015-04-30 21:19:56 -0700 | [diff] [blame] | 1080 | wait_on_bit_lock(unsigned long *word, int bit, unsigned mode) |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1081 | { |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 1082 | might_sleep(); |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1083 | if (!test_and_set_bit(bit, word)) |
| 1084 | return 0; |
| 1085 | return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode); |
| 1086 | } |
| 1087 | |
| 1088 | /** |
| 1089 | * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it |
| 1090 | * @word: the word being waited on, a kernel virtual address |
| 1091 | * @bit: the bit of the word being waited on |
| 1092 | * @mode: the task state to sleep in |
| 1093 | * |
| 1094 | * Use the standard hashed waitqueue table to wait for a bit |
| 1095 | * to be cleared and then to atomically set it. This is similar |
| 1096 | * to wait_on_bit(), but calls io_schedule() instead of schedule() |
| 1097 | * for the actual waiting. |
| 1098 | * |
| 1099 | * Returns zero if the bit was (eventually) found to be clear and was |
| 1100 | * set. Returns non-zero if a signal was delivered to the process and |
| 1101 | * the @mode allows that signal to wake the process. |
| 1102 | */ |
| 1103 | static inline int |
Palmer Dabbelt | 7e60598 | 2015-04-30 21:19:56 -0700 | [diff] [blame] | 1104 | wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode) |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1105 | { |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 1106 | might_sleep(); |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 1107 | if (!test_and_set_bit(bit, word)) |
| 1108 | return 0; |
| 1109 | return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode); |
| 1110 | } |
| 1111 | |
| 1112 | /** |
| 1113 | * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it |
| 1114 | * @word: the word being waited on, a kernel virtual address |
| 1115 | * @bit: the bit of the word being waited on |
| 1116 | * @action: the function used to sleep, which may take special actions |
| 1117 | * @mode: the task state to sleep in |
| 1118 | * |
| 1119 | * Use the standard hashed waitqueue table to wait for a bit |
| 1120 | * to be cleared and then to set it, and allow the waiting action |
| 1121 | * to be specified. |
| 1122 | * This is like wait_on_bit() but allows fine control of how the waiting |
| 1123 | * is done. |
| 1124 | * |
| 1125 | * Returns zero if the bit was (eventually) found to be clear and was |
| 1126 | * set. Returns non-zero if a signal was delivered to the process and |
| 1127 | * the @mode allows that signal to wake the process. |
| 1128 | */ |
| 1129 | static inline int |
Palmer Dabbelt | 7e60598 | 2015-04-30 21:19:56 -0700 | [diff] [blame] | 1130 | wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action, |
| 1131 | unsigned mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | { |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 1133 | might_sleep(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | if (!test_and_set_bit(bit, word)) |
| 1135 | return 0; |
| 1136 | return out_of_line_wait_on_bit_lock(word, bit, action, mode); |
| 1137 | } |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 1138 | |
| 1139 | /** |
| 1140 | * wait_on_atomic_t - Wait for an atomic_t to become 0 |
| 1141 | * @val: The atomic value being waited on, a kernel virtual address |
| 1142 | * @action: the function used to sleep, which may take special actions |
| 1143 | * @mode: the task state to sleep in |
| 1144 | * |
| 1145 | * Wait for an atomic_t to become 0. We abuse the bit-wait waitqueue table for |
| 1146 | * the purpose of getting a waitqueue, but we set the key to a bit number |
| 1147 | * outside of the target 'word'. |
| 1148 | */ |
| 1149 | static inline |
| 1150 | int wait_on_atomic_t(atomic_t *val, int (*action)(atomic_t *), unsigned mode) |
| 1151 | { |
Peter Zijlstra | e22b886 | 2014-09-24 10:18:48 +0200 | [diff] [blame] | 1152 | might_sleep(); |
David Howells | cb65537 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 1153 | if (atomic_read(val) == 0) |
| 1154 | return 0; |
| 1155 | return out_of_line_wait_on_atomic_t(val, action, mode); |
| 1156 | } |
Ingo Molnar | fb869b6 | 2013-10-04 10:24:49 +0200 | [diff] [blame] | 1157 | |
| 1158 | #endif /* _LINUX_WAIT_H */ |