Whamcloud - gitweb
LU-9859 libcfs: initialize bit_wait_table
[fs/lustre-release.git] / libcfs / include / libcfs / linux / linux-wait.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBCFS_LINUX_WAIT_BIT_H
3 #define __LIBCFS_LINUX_WAIT_BIT_H
4
5 /*
6  * Linux wait-bit related types and methods:
7  */
8 #ifdef HAVE_WAIT_BIT_HEADER_H
9 #include <linux/wait_bit.h>
10 #endif
11 #include <linux/wait.h>
12
13 #ifndef HAVE_WAIT_QUEUE_ENTRY
14 #define wait_queue_entry_t wait_queue_t
15 #endif
16
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;
21 };
22
23 #define ___wait_is_interruptible(state)                                         \
24         (!__builtin_constant_p(state) ||                                        \
25                 state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE)          \
26
27 #endif /* ! HAVE_WAIT_BIT_HEADER_H */
28
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);
32 #endif
33
34 #ifndef HAVE_CLEAR_AND_WAKE_UP_BIT
35 /**
36  * clear_and_wake_up_bit - clear a bit and wake up anyone waiting on that bit
37  *
38  * @bit: the bit of the word being waited on
39  * @word: the word being waited on, a kernel virtual address
40  *
41  * You can use this helper if bitflags are manipulated atomically rather than
42  * non-atomically under a lock.
43  */
44 static inline void clear_and_wake_up_bit(int bit, void *word)
45 {
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);
50 }
51 #endif /* ! HAVE_CLEAR_AND_WAKE_UP_BIT */
52
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);
59
60 #define ___wait_var_event(var, condition, state, exclusive, ret, cmd)   \
61 ({                                                                      \
62         __label__ __out;                                                \
63         wait_queue_head_t *__wq_head = __var_waitqueue(var);            \
64         struct wait_bit_queue_entry __wbq_entry;                        \
65         long __ret = ret; /* explicit shadow */                         \
66                                                                         \
67         init_wait_var_entry(&__wbq_entry, var,                          \
68                             exclusive ? WQ_FLAG_EXCLUSIVE : 0);         \
69         for (;;) {                                                      \
70                 long __int = prepare_to_wait_event(__wq_head,           \
71                                                    &__wbq_entry.wq_entry, \
72                                                    state);              \
73                 if (condition)                                          \
74                         break;                                          \
75                                                                         \
76                 if (___wait_is_interruptible(state) && __int) {         \
77                         __ret = __int;                                  \
78                         goto __out;                                     \
79                 }                                                       \
80                                                                         \
81                 cmd;                                                    \
82         }                                                               \
83         finish_wait(__wq_head, &__wbq_entry.wq_entry);                  \
84 __out:  __ret;                                                          \
85 })
86
87 #define __wait_var_event(var, condition)                                \
88         ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0,   \
89                           schedule())
90
91 #define wait_var_event(var, condition)                                  \
92 do {                                                                    \
93         might_sleep();                                                  \
94         if (condition)                                                  \
95                 break;                                                  \
96         __wait_var_event(var, condition);                               \
97 } while (0)
98
99 #define __wait_var_event_killable(var, condition)                       \
100         ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0,          \
101                           schedule())
102
103 #define wait_var_event_killable(var, condition)                         \
104 ({                                                                      \
105         int __ret = 0;                                                  \
106         might_sleep();                                                  \
107         if (!(condition))                                               \
108                 __ret = __wait_var_event_killable(var, condition);      \
109         __ret;                                                          \
110 })
111
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))
116
117 #define wait_var_event_timeout(var, condition, timeout)                 \
118 ({                                                                      \
119         long __ret = timeout;                                           \
120         might_sleep();                                                  \
121         if (!___wait_cond_timeout(condition))                           \
122                 __ret = __wait_var_event_timeout(var, condition, timeout); \
123         __ret;                                                          \
124 })
125 #endif /* ! HAVE_WAIT_VAR_EVENT */
126
127 #endif /* __LICBFS_LINUX_WAIT_BIT_H */