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_bit_init(void);
55 extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry,
56 void *var, int flags);
57 extern void wake_up_var(void *var);
58 extern wait_queue_head_t *__var_waitqueue(void *p);
60 #define ___wait_var_event(var, condition, state, exclusive, ret, cmd) \
63 wait_queue_head_t *__wq_head = __var_waitqueue(var); \
64 struct wait_bit_queue_entry __wbq_entry; \
65 long __ret = ret; /* explicit shadow */ \
67 init_wait_var_entry(&__wbq_entry, var, \
68 exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
70 long __int = prepare_to_wait_event(__wq_head, \
71 &__wbq_entry.wq_entry, \
76 if (___wait_is_interruptible(state) && __int) { \
83 finish_wait(__wq_head, &__wbq_entry.wq_entry); \
87 #define __wait_var_event(var, condition) \
88 ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
91 #define wait_var_event(var, condition) \
96 __wait_var_event(var, condition); \
99 #define __wait_var_event_killable(var, condition) \
100 ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \
103 #define wait_var_event_killable(var, condition) \
108 __ret = __wait_var_event_killable(var, condition); \
112 #define __wait_var_event_timeout(var, condition, timeout) \
113 ___wait_var_event(var, ___wait_cond_timeout(condition), \
114 TASK_UNINTERRUPTIBLE, 0, timeout, \
115 __ret = schedule_timeout(__ret))
117 #define wait_var_event_timeout(var, condition, timeout) \
119 long __ret = timeout; \
121 if (!___wait_cond_timeout(condition)) \
122 __ret = __wait_var_event_timeout(var, condition, timeout); \
125 #endif /* ! HAVE_WAIT_VAR_EVENT */
127 #endif /* __LICBFS_LINUX_WAIT_BIT_H */