1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBCFS_LINUX_WAIT_BIT_H
3 #define __LIBCFS_LINUX_WAIT_BIT_H
6 * Linux wait-bit related types and methods:
8 #ifdef HAVE_WAIT_BIT_HEADER_H
9 #include <linux/wait_bit.h>
11 #include <linux/wait.h>
13 #ifndef HAVE_WAIT_QUEUE_ENTRY
14 #define wait_queue_entry_t wait_queue_t
17 #ifndef HAVE_WAIT_BIT_HEADER_H
18 struct wait_bit_queue_entry {
19 struct wait_bit_key key;
20 wait_queue_entry_t wq_entry;
23 #define ___wait_is_interruptible(state) \
24 (!__builtin_constant_p(state) || \
25 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
27 #endif /* ! HAVE_WAIT_BIT_HEADER_H */
29 #ifndef HAVE_PREPARE_TO_WAIT_EVENT
30 extern long prepare_to_wait_event(wait_queue_head_t *wq_head,
31 wait_queue_entry_t *wq_entry, int state);
34 #ifndef HAVE_CLEAR_AND_WAKE_UP_BIT
36 * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
38 * @bit: the bit of the word being waited on
39 * @word: the word being waited on, a kernel virtual address
41 * You can use this helper if bitflags are manipulated atomically rather than
42 * non-atomically under a lock.
44 static inline void clear_and_wake_up_bit(int bit, void *word)
46 clear_bit_unlock(bit, word);
47 /* See wake_up_bit() for which memory barrier you need to use. */
48 smp_mb__after_atomic();
49 wake_up_bit(word, bit);
51 #endif /* ! HAVE_CLEAR_AND_WAKE_UP_BIT */
53 #ifndef HAVE_WAIT_VAR_EVENT
54 extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry,
55 void *var, int flags);
56 extern void wake_up_var(void *var);
57 extern wait_queue_head_t *__var_waitqueue(void *p);
59 #define ___wait_var_event(var, condition, state, exclusive, ret, cmd) \
62 wait_queue_head_t *__wq_head = __var_waitqueue(var); \
63 struct wait_bit_queue_entry __wbq_entry; \
64 long __ret = ret; /* explicit shadow */ \
66 init_wait_var_entry(&__wbq_entry, var, \
67 exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
69 long __int = prepare_to_wait_event(__wq_head, \
70 &__wbq_entry.wq_entry, \
75 if (___wait_is_interruptible(state) && __int) { \
82 finish_wait(__wq_head, &__wbq_entry.wq_entry); \
86 #define __wait_var_event(var, condition) \
87 ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
90 #define wait_var_event(var, condition) \
95 __wait_var_event(var, condition); \
98 #define __wait_var_event_killable(var, condition) \
99 ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \
102 #define wait_var_event_killable(var, condition) \
107 __ret = __wait_var_event_killable(var, condition); \
111 #define __wait_var_event_timeout(var, condition, timeout) \
112 ___wait_var_event(var, ___wait_cond_timeout(condition), \
113 TASK_UNINTERRUPTIBLE, 0, timeout, \
114 __ret = schedule_timeout(__ret))
116 #define wait_var_event_timeout(var, condition, timeout) \
118 long __ret = timeout; \
120 if (!___wait_cond_timeout(condition)) \
121 __ret = __wait_var_event_timeout(var, condition, timeout); \
124 #endif /* ! HAVE_WAIT_VAR_EVENT */
126 #endif /* __LICBFS_LINUX_WAIT_BIT_H */