Whamcloud - gitweb
LU-11089 obd: use wait_event_var() in lu_context_key_degister()
[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_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);
58
59 #define ___wait_var_event(var, condition, state, exclusive, ret, cmd)   \
60 ({                                                                      \
61         __label__ __out;                                                \
62         wait_queue_head_t *__wq_head = __var_waitqueue(var);            \
63         struct wait_bit_queue_entry __wbq_entry;                        \
64         long __ret = ret; /* explicit shadow */                         \
65                                                                         \
66         init_wait_var_entry(&__wbq_entry, var,                          \
67                             exclusive ? WQ_FLAG_EXCLUSIVE : 0);         \
68         for (;;) {                                                      \
69                 long __int = prepare_to_wait_event(__wq_head,           \
70                                                    &__wbq_entry.wq_entry, \
71                                                    state);              \
72                 if (condition)                                          \
73                         break;                                          \
74                                                                         \
75                 if (___wait_is_interruptible(state) && __int) {         \
76                         __ret = __int;                                  \
77                         goto __out;                                     \
78                 }                                                       \
79                                                                         \
80                 cmd;                                                    \
81         }                                                               \
82         finish_wait(__wq_head, &__wbq_entry.wq_entry);                  \
83 __out:  __ret;                                                          \
84 })
85
86 #define __wait_var_event(var, condition)                                \
87         ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0,   \
88                           schedule())
89
90 #define wait_var_event(var, condition)                                  \
91 do {                                                                    \
92         might_sleep();                                                  \
93         if (condition)                                                  \
94                 break;                                                  \
95         __wait_var_event(var, condition);                               \
96 } while (0)
97
98 #define __wait_var_event_killable(var, condition)                       \
99         ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0,          \
100                           schedule())
101
102 #define wait_var_event_killable(var, condition)                         \
103 ({                                                                      \
104         int __ret = 0;                                                  \
105         might_sleep();                                                  \
106         if (!(condition))                                               \
107                 __ret = __wait_var_event_killable(var, condition);      \
108         __ret;                                                          \
109 })
110
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))
115
116 #define wait_var_event_timeout(var, condition, timeout)                 \
117 ({                                                                      \
118         long __ret = timeout;                                           \
119         might_sleep();                                                  \
120         if (!___wait_cond_timeout(condition))                           \
121                 __ret = __wait_var_event_timeout(var, condition, timeout); \
122         __ret;                                                          \
123 })
124 #endif /* ! HAVE_WAIT_VAR_EVENT */
125
126 #endif /* __LICBFS_LINUX_WAIT_BIT_H */