1
2#ifndef _LINUX_WAIT_BIT_H
3#define _LINUX_WAIT_BIT_H
4
5
6
7
8#include <linux/wait.h>
9
10struct wait_bit_key {
11 void *flags;
12 int bit_nr;
13 unsigned long timeout;
14};
15
16struct wait_bit_queue_entry {
17 struct wait_bit_key key;
18 struct wait_queue_entry wq_entry;
19};
20
21#define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
22 { .flags = word, .bit_nr = bit, }
23
24typedef int wait_bit_action_f(struct wait_bit_key *key, int mode);
25
26void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit);
27int __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
28int __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);
29void wake_up_bit(void *word, int bit);
30int out_of_line_wait_on_bit(void *word, int, wait_bit_action_f *action, unsigned int mode);
31int out_of_line_wait_on_bit_timeout(void *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout);
32int out_of_line_wait_on_bit_lock(void *word, int, wait_bit_action_f *action, unsigned int mode);
33struct wait_queue_head *bit_waitqueue(void *word, int bit);
34extern void __init wait_bit_init(void);
35
36int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
37
38#define DEFINE_WAIT_BIT(name, word, bit) \
39 struct wait_bit_queue_entry name = { \
40 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
41 .wq_entry = { \
42 .private = current, \
43 .func = wake_bit_function, \
44 .entry = \
45 LIST_HEAD_INIT((name).wq_entry.entry), \
46 }, \
47 }
48
49extern int bit_wait(struct wait_bit_key *key, int mode);
50extern int bit_wait_io(struct wait_bit_key *key, int mode);
51extern int bit_wait_timeout(struct wait_bit_key *key, int mode);
52extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode);
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70static inline int
71wait_on_bit(unsigned long *word, int bit, unsigned mode)
72{
73 might_sleep();
74 if (!test_bit(bit, word))
75 return 0;
76 return out_of_line_wait_on_bit(word, bit,
77 bit_wait,
78 mode);
79}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95static inline int
96wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
97{
98 might_sleep();
99 if (!test_bit(bit, word))
100 return 0;
101 return out_of_line_wait_on_bit(word, bit,
102 bit_wait_io,
103 mode);
104}
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121static inline int
122wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
123 unsigned long timeout)
124{
125 might_sleep();
126 if (!test_bit(bit, word))
127 return 0;
128 return out_of_line_wait_on_bit_timeout(word, bit,
129 bit_wait_timeout,
130 mode, timeout);
131}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149static inline int
150wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
151 unsigned mode)
152{
153 might_sleep();
154 if (!test_bit(bit, word))
155 return 0;
156 return out_of_line_wait_on_bit(word, bit, action, mode);
157}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178static inline int
179wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
180{
181 might_sleep();
182 if (!test_and_set_bit(bit, word))
183 return 0;
184 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
185}
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202static inline int
203wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
204{
205 might_sleep();
206 if (!test_and_set_bit(bit, word))
207 return 0;
208 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
209}
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228static inline int
229wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
230 unsigned mode)
231{
232 might_sleep();
233 if (!test_and_set_bit(bit, word))
234 return 0;
235 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
236}
237
238extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags);
239extern void wake_up_var(void *var);
240extern wait_queue_head_t *__var_waitqueue(void *p);
241
242#define ___wait_var_event(var, condition, state, exclusive, ret, cmd) \
243({ \
244 __label__ __out; \
245 struct wait_queue_head *__wq_head = __var_waitqueue(var); \
246 struct wait_bit_queue_entry __wbq_entry; \
247 long __ret = ret; \
248 \
249 init_wait_var_entry(&__wbq_entry, var, \
250 exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
251 for (;;) { \
252 long __int = prepare_to_wait_event(__wq_head, \
253 &__wbq_entry.wq_entry, \
254 state); \
255 if (condition) \
256 break; \
257 \
258 if (___wait_is_interruptible(state) && __int) { \
259 __ret = __int; \
260 goto __out; \
261 } \
262 \
263 cmd; \
264 } \
265 finish_wait(__wq_head, &__wbq_entry.wq_entry); \
266__out: __ret; \
267})
268
269#define __wait_var_event(var, condition) \
270 ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
271 schedule())
272
273#define wait_var_event(var, condition) \
274do { \
275 might_sleep(); \
276 if (condition) \
277 break; \
278 __wait_var_event(var, condition); \
279} while (0)
280
281#define __wait_var_event_killable(var, condition) \
282 ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \
283 schedule())
284
285#define wait_var_event_killable(var, condition) \
286({ \
287 int __ret = 0; \
288 might_sleep(); \
289 if (!(condition)) \
290 __ret = __wait_var_event_killable(var, condition); \
291 __ret; \
292})
293
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))
298
299#define wait_var_event_timeout(var, condition, timeout) \
300({ \
301 long __ret = timeout; \
302 might_sleep(); \
303 if (!___wait_cond_timeout(condition)) \
304 __ret = __wait_var_event_timeout(var, condition, timeout); \
305 __ret; \
306})
307
308#define __wait_var_event_interruptible(var, condition) \
309 ___wait_var_event(var, condition, TASK_INTERRUPTIBLE, 0, 0, \
310 schedule())
311
312#define wait_var_event_interruptible(var, condition) \
313({ \
314 int __ret = 0; \
315 might_sleep(); \
316 if (!(condition)) \
317 __ret = __wait_var_event_interruptible(var, condition); \
318 __ret; \
319})
320
321
322
323
324
325
326
327
328
329
330static inline void clear_and_wake_up_bit(int bit, void *word)
331{
332 clear_bit_unlock(bit, word);
333
334 smp_mb__after_atomic();
335 wake_up_bit(word, bit);
336}
337
338#endif
339