0001
0002 #ifndef _LINUX_WAIT_BIT_H
0003 #define _LINUX_WAIT_BIT_H
0004
0005
0006
0007
0008 #include <linux/wait.h>
0009
0010 struct wait_bit_key {
0011 void *flags;
0012 int bit_nr;
0013 unsigned long timeout;
0014 };
0015
0016 struct wait_bit_queue_entry {
0017 struct wait_bit_key key;
0018 struct wait_queue_entry wq_entry;
0019 };
0020
0021 #define __WAIT_BIT_KEY_INITIALIZER(word, bit) \
0022 { .flags = word, .bit_nr = bit, }
0023
0024 typedef int wait_bit_action_f(struct wait_bit_key *key, int mode);
0025
0026 void __wake_up_bit(struct wait_queue_head *wq_head, void *word, int bit);
0027 int __wait_on_bit(struct wait_queue_head *wq_head, struct wait_bit_queue_entry *wbq_entry, wait_bit_action_f *action, unsigned int mode);
0028 int __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);
0029 void wake_up_bit(void *word, int bit);
0030 int out_of_line_wait_on_bit(void *word, int, wait_bit_action_f *action, unsigned int mode);
0031 int out_of_line_wait_on_bit_timeout(void *word, int, wait_bit_action_f *action, unsigned int mode, unsigned long timeout);
0032 int out_of_line_wait_on_bit_lock(void *word, int, wait_bit_action_f *action, unsigned int mode);
0033 struct wait_queue_head *bit_waitqueue(void *word, int bit);
0034 extern void __init wait_bit_init(void);
0035
0036 int wake_bit_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
0037
0038 #define DEFINE_WAIT_BIT(name, word, bit) \
0039 struct wait_bit_queue_entry name = { \
0040 .key = __WAIT_BIT_KEY_INITIALIZER(word, bit), \
0041 .wq_entry = { \
0042 .private = current, \
0043 .func = wake_bit_function, \
0044 .entry = \
0045 LIST_HEAD_INIT((name).wq_entry.entry), \
0046 }, \
0047 }
0048
0049 extern int bit_wait(struct wait_bit_key *key, int mode);
0050 extern int bit_wait_io(struct wait_bit_key *key, int mode);
0051 extern int bit_wait_timeout(struct wait_bit_key *key, int mode);
0052 extern int bit_wait_io_timeout(struct wait_bit_key *key, int mode);
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 static inline int
0071 wait_on_bit(unsigned long *word, int bit, unsigned mode)
0072 {
0073 might_sleep();
0074 if (!test_bit_acquire(bit, word))
0075 return 0;
0076 return out_of_line_wait_on_bit(word, bit,
0077 bit_wait,
0078 mode);
0079 }
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095 static inline int
0096 wait_on_bit_io(unsigned long *word, int bit, unsigned mode)
0097 {
0098 might_sleep();
0099 if (!test_bit_acquire(bit, word))
0100 return 0;
0101 return out_of_line_wait_on_bit(word, bit,
0102 bit_wait_io,
0103 mode);
0104 }
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121 static inline int
0122 wait_on_bit_timeout(unsigned long *word, int bit, unsigned mode,
0123 unsigned long timeout)
0124 {
0125 might_sleep();
0126 if (!test_bit_acquire(bit, word))
0127 return 0;
0128 return out_of_line_wait_on_bit_timeout(word, bit,
0129 bit_wait_timeout,
0130 mode, timeout);
0131 }
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149 static inline int
0150 wait_on_bit_action(unsigned long *word, int bit, wait_bit_action_f *action,
0151 unsigned mode)
0152 {
0153 might_sleep();
0154 if (!test_bit_acquire(bit, word))
0155 return 0;
0156 return out_of_line_wait_on_bit(word, bit, action, mode);
0157 }
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178 static inline int
0179 wait_on_bit_lock(unsigned long *word, int bit, unsigned mode)
0180 {
0181 might_sleep();
0182 if (!test_and_set_bit(bit, word))
0183 return 0;
0184 return out_of_line_wait_on_bit_lock(word, bit, bit_wait, mode);
0185 }
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 static inline int
0203 wait_on_bit_lock_io(unsigned long *word, int bit, unsigned mode)
0204 {
0205 might_sleep();
0206 if (!test_and_set_bit(bit, word))
0207 return 0;
0208 return out_of_line_wait_on_bit_lock(word, bit, bit_wait_io, mode);
0209 }
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228 static inline int
0229 wait_on_bit_lock_action(unsigned long *word, int bit, wait_bit_action_f *action,
0230 unsigned mode)
0231 {
0232 might_sleep();
0233 if (!test_and_set_bit(bit, word))
0234 return 0;
0235 return out_of_line_wait_on_bit_lock(word, bit, action, mode);
0236 }
0237
0238 extern void init_wait_var_entry(struct wait_bit_queue_entry *wbq_entry, void *var, int flags);
0239 extern void wake_up_var(void *var);
0240 extern wait_queue_head_t *__var_waitqueue(void *p);
0241
0242 #define ___wait_var_event(var, condition, state, exclusive, ret, cmd) \
0243 ({ \
0244 __label__ __out; \
0245 struct wait_queue_head *__wq_head = __var_waitqueue(var); \
0246 struct wait_bit_queue_entry __wbq_entry; \
0247 long __ret = ret; \
0248 \
0249 init_wait_var_entry(&__wbq_entry, var, \
0250 exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
0251 for (;;) { \
0252 long __int = prepare_to_wait_event(__wq_head, \
0253 &__wbq_entry.wq_entry, \
0254 state); \
0255 if (condition) \
0256 break; \
0257 \
0258 if (___wait_is_interruptible(state) && __int) { \
0259 __ret = __int; \
0260 goto __out; \
0261 } \
0262 \
0263 cmd; \
0264 } \
0265 finish_wait(__wq_head, &__wbq_entry.wq_entry); \
0266 __out: __ret; \
0267 })
0268
0269 #define __wait_var_event(var, condition) \
0270 ___wait_var_event(var, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
0271 schedule())
0272
0273 #define wait_var_event(var, condition) \
0274 do { \
0275 might_sleep(); \
0276 if (condition) \
0277 break; \
0278 __wait_var_event(var, condition); \
0279 } while (0)
0280
0281 #define __wait_var_event_killable(var, condition) \
0282 ___wait_var_event(var, condition, TASK_KILLABLE, 0, 0, \
0283 schedule())
0284
0285 #define wait_var_event_killable(var, condition) \
0286 ({ \
0287 int __ret = 0; \
0288 might_sleep(); \
0289 if (!(condition)) \
0290 __ret = __wait_var_event_killable(var, condition); \
0291 __ret; \
0292 })
0293
0294 #define __wait_var_event_timeout(var, condition, timeout) \
0295 ___wait_var_event(var, ___wait_cond_timeout(condition), \
0296 TASK_UNINTERRUPTIBLE, 0, timeout, \
0297 __ret = schedule_timeout(__ret))
0298
0299 #define wait_var_event_timeout(var, condition, timeout) \
0300 ({ \
0301 long __ret = timeout; \
0302 might_sleep(); \
0303 if (!___wait_cond_timeout(condition)) \
0304 __ret = __wait_var_event_timeout(var, condition, timeout); \
0305 __ret; \
0306 })
0307
0308 #define __wait_var_event_interruptible(var, condition) \
0309 ___wait_var_event(var, condition, TASK_INTERRUPTIBLE, 0, 0, \
0310 schedule())
0311
0312 #define wait_var_event_interruptible(var, condition) \
0313 ({ \
0314 int __ret = 0; \
0315 might_sleep(); \
0316 if (!(condition)) \
0317 __ret = __wait_var_event_interruptible(var, condition); \
0318 __ret; \
0319 })
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330 static inline void clear_and_wake_up_bit(int bit, void *word)
0331 {
0332 clear_bit_unlock(bit, word);
0333
0334 smp_mb__after_atomic();
0335 wake_up_bit(word, bit);
0336 }
0337
0338 #endif