1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_WAIT_BIT_H
3 #define _LINUX_WAIT_BIT_H
6 * Linux wait-bit related types and methods:
8 #include <linux/wait.h>
13 unsigned long timeout;
16 struct wait_bit_queue_entry {
17 struct wait_bit_key key;
18 struct wait_queue_entry wq_entry;
21 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
22 { .flags = word, .bit_nr = bit, }
24 typedef int wait_bit_action_f(struct wait_bit_key *key, int mode);
26 void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit);
27 int __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
28 int __wait_on_bit_lock(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
29 void wake_up_bit(void *word, int bit);
30 int out_of_line_wait_on_bit(void *word, int, wait_bit_action_f *action, unsigned int mode);
31 int out_of_line_wait_on_bit_timeout(void *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout);
32 int out_of_line_wait_on_bit_lock(void *word, int, wait_bit_action_f *action, unsigned int mode);
33 struct wait_queue_head *bit_waitqueue(void *word, int bit);
34 extern void __init wait_bit_init(void);
36 int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
38 #define DEFINE_WAIT_BIT(name, word, bit) \
39 struct wait_bit_queue_entry name = { \
40 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
43 .func = wake_bit_function, \
45 LIST_HEAD_INIT((name).wq_entry.entry), \
49 extern int bit_wait(struct wait_bit_key *key, int mode);
50 extern int bit_wait_io(struct wait_bit_key *key, int mode);
51 extern int bit_wait_timeout(struct wait_bit_key *key, int mode);
52 extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode);
55 * wait_on_bit - wait for a bit to be cleared
56 * @word: the word being waited on, a kernel virtual address
57 * @bit: the bit of the word being waited on
58 * @mode: the task state to sleep in
60 * There is a standard hashed waitqueue table for generic use. This
61 * is the part of the hashtable's accessor API that waits on a bit.
62 * For instance, if one were to have waiters on a bitflag, one would
63 * call wait_on_bit() in threads waiting for the bit to clear.
64 * One uses wait_on_bit() where one is waiting for the bit to clear,
65 * but has no intention of setting it.
66 * Returned value will be zero if the bit was cleared, or non-zero
67 * if the process received a signal and the mode permitted wakeup
71 wait_on_bit(unsigned long *word, int bit, unsigned mode)
74 if (!test_bit(bit, word))
76 return out_of_line_wait_on_bit(word, bit,
82 * wait_on_bit_io - wait for a bit to be cleared
83 * @word: the word being waited on, a kernel virtual address
84 * @bit: the bit of the word being waited on
85 * @mode: the task state to sleep in
87 * Use the standard hashed waitqueue table to wait for a bit
88 * to be cleared. This is similar to wait_on_bit(), but calls
89 * io_schedule() instead of schedule() for the actual waiting.
91 * Returned value will be zero if the bit was cleared, or non-zero
92 * if the process received a signal and the mode permitted wakeup
96 wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
99 if (!test_bit(bit, word))
101 return out_of_line_wait_on_bit(word, bit,
107 * wait_on_bit_timeout - wait for a bit to be cleared or a timeout elapses
108 * @word: the word being waited on, a kernel virtual address
109 * @bit: the bit of the word being waited on
110 * @mode: the task state to sleep in
111 * @timeout: timeout, in jiffies
113 * Use the standard hashed waitqueue table to wait for a bit
114 * to be cleared. This is similar to wait_on_bit(), except also takes a
117 * Returned value will be zero if the bit was cleared before the
118 * @timeout elapsed, or non-zero if the @timeout elapsed or process
119 * received a signal and the mode permitted wakeup on that signal.
122 wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
123 unsigned long timeout)
126 if (!test_bit(bit, word))
128 return out_of_line_wait_on_bit_timeout(word, bit,
134 * wait_on_bit_action - wait for a bit to be cleared
135 * @word: the word being waited on, a kernel virtual address
136 * @bit: the bit of the word being waited on
137 * @action: the function used to sleep, which may take special actions
138 * @mode: the task state to sleep in
140 * Use the standard hashed waitqueue table to wait for a bit
141 * to be cleared, and allow the waiting action to be specified.
142 * This is like wait_on_bit() but allows fine control of how the waiting
145 * Returned value will be zero if the bit was cleared, or non-zero
146 * if the process received a signal and the mode permitted wakeup
150 wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
154 if (!test_bit(bit, word))
156 return out_of_line_wait_on_bit(word, bit, action, mode);
160 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
161 * @word: the word being waited on, a kernel virtual address
162 * @bit: the bit of the word being waited on
163 * @mode: the task state to sleep in
165 * There is a standard hashed waitqueue table for generic use. This
166 * is the part of the hashtable's accessor API that waits on a bit
167 * when one intends to set it, for instance, trying to lock bitflags.
168 * For instance, if one were to have waiters trying to set bitflag
169 * and waiting for it to clear before setting it, one would call
170 * wait_on_bit() in threads waiting to be able to set the bit.
171 * One uses wait_on_bit_lock() where one is waiting for the bit to
172 * clear with the intention of setting it, and when done, clearing it.
174 * Returns zero if the bit was (eventually) found to be clear and was
175 * set. Returns non-zero if a signal was delivered to the process and
176 * the @mode allows that signal to wake the process.
179 wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
182 if (!test_and_set_bit(bit, word))
184 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
188 * wait_on_bit_lock_io - wait for a bit to be cleared, when wanting to set it
189 * @word: the word being waited on, a kernel virtual address
190 * @bit: the bit of the word being waited on
191 * @mode: the task state to sleep in
193 * Use the standard hashed waitqueue table to wait for a bit
194 * to be cleared and then to atomically set it. This is similar
195 * to wait_on_bit(), but calls io_schedule() instead of schedule()
196 * for the actual waiting.
198 * Returns zero if the bit was (eventually) found to be clear and was
199 * set. Returns non-zero if a signal was delivered to the process and
200 * the @mode allows that signal to wake the process.
203 wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
206 if (!test_and_set_bit(bit, word))
208 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
212 * wait_on_bit_lock_action - wait for a bit to be cleared, when wanting to set it
213 * @word: the word being waited on, a kernel virtual address
214 * @bit: the bit of the word being waited on
215 * @action: the function used to sleep, which may take special actions
216 * @mode: the task state to sleep in
218 * Use the standard hashed waitqueue table to wait for a bit
219 * to be cleared and then to set it, and allow the waiting action
221 * This is like wait_on_bit() but allows fine control of how the waiting
224 * Returns zero if the bit was (eventually) found to be clear and was
225 * set. Returns non-zero if a signal was delivered to the process and
226 * the @mode allows that signal to wake the process.
229 wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
233 if (!test_and_set_bit(bit, word))
235 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
238 extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags);
239 extern void wake_up_var(void *var);
240 extern wait_queue_head_t *__var_waitqueue(void *p);
242 #define ___wait_var_event(var, condition, state, exclusive, ret, cmd) \
245 struct wait_queue_head *__wq_head = __var_waitqueue(var); \
246 struct wait_bit_queue_entry __wbq_entry; \
247 long __ret = ret; /* explicit shadow */ \
249 init_wait_var_entry(&__wbq_entry, var, \
250 exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
252 long __int = prepare_to_wait_event(__wq_head, \
253 &__wbq_entry.wq_entry, \
258 if (___wait_is_interruptible(state) && __int) { \
265 finish_wait(__wq_head, &__wbq_entry.wq_entry); \
269 #define __wait_var_event(var, condition) \
270 ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
273 #define wait_var_event(var, condition) \
278 __wait_var_event(var, condition); \
281 #define __wait_var_event_killable(var, condition) \
282 ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \
285 #define wait_var_event_killable(var, condition) \
290 __ret = __wait_var_event_killable(var, condition); \
294 #define __wait_var_event_timeout(var, condition, timeout) \
295 ___wait_var_event(var, ___wait_cond_timeout(condition), \
296 TASK_UNINTERRUPTIBLE, 0, timeout, \
297 __ret = schedule_timeout(__ret))
299 #define wait_var_event_timeout(var, condition, timeout) \
301 long __ret = timeout; \
303 if (!___wait_cond_timeout(condition)) \
304 __ret = __wait_var_event_timeout(var, condition, timeout); \
308 #define __wait_var_event_interruptible(var, condition) \
309 ___wait_var_event(var, condition, TASK_INTERRUPTIBLE, 0, 0, \
312 #define wait_var_event_interruptible(var, condition) \
317 __ret = __wait_var_event_interruptible(var, condition); \
322 * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
324 * @bit: the bit of the word being waited on
325 * @word: the word being waited on, a kernel virtual address
327 * You can use this helper if bitflags are manipulated atomically rather than
328 * non-atomically under a lock.
330 static inline void clear_and_wake_up_bit(int bit, void *word)
332 clear_bit_unlock(bit, word);
333 /* See wake_up_bit() for which memory barrier you need to use. */
334 smp_mb__after_atomic();
335 wake_up_bit(word, bit);
338 #endif /* _LINUX_WAIT_BIT_H */