]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * workqueue.h --- work queue handling for Linux. | |
3 | */ | |
4 | ||
5 | #ifndef _LINUX_WORKQUEUE_H | |
6 | #define _LINUX_WORKQUEUE_H | |
7 | ||
8 | #include <linux/timer.h> | |
9 | #include <linux/linkage.h> | |
10 | #include <linux/bitops.h> | |
4e6045f1 | 11 | #include <linux/lockdep.h> |
a08727ba | 12 | #include <asm/atomic.h> |
1da177e4 LT |
13 | |
14 | struct workqueue_struct; | |
15 | ||
65f27f38 DH |
16 | struct work_struct; |
17 | typedef void (*work_func_t)(struct work_struct *work); | |
6bb49e59 | 18 | |
a08727ba LT |
19 | /* |
20 | * The first word is the work queue pointer and the flags rolled into | |
21 | * one | |
22 | */ | |
23 | #define work_data_bits(work) ((unsigned long *)(&(work)->data)) | |
24 | ||
22df02bb TH |
25 | enum { |
26 | WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */ | |
27 | #ifdef CONFIG_DEBUG_OBJECTS_WORK | |
28 | WORK_STRUCT_STATIC_BIT = 1, /* static initializer (debugobjects) */ | |
29 | #endif | |
30 | ||
31 | WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT, | |
32 | #ifdef CONFIG_DEBUG_OBJECTS_WORK | |
33 | WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT, | |
34 | #else | |
35 | WORK_STRUCT_STATIC = 0, | |
36 | #endif | |
37 | ||
38 | WORK_STRUCT_FLAG_MASK = 3UL, | |
39 | WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK, | |
40 | }; | |
41 | ||
1da177e4 | 42 | struct work_struct { |
a08727ba | 43 | atomic_long_t data; |
1da177e4 | 44 | struct list_head entry; |
6bb49e59 | 45 | work_func_t func; |
4e6045f1 JB |
46 | #ifdef CONFIG_LOCKDEP |
47 | struct lockdep_map lockdep_map; | |
48 | #endif | |
52bad64d DH |
49 | }; |
50 | ||
23b2e599 | 51 | #define WORK_DATA_INIT() ATOMIC_LONG_INIT(0) |
22df02bb | 52 | #define WORK_DATA_STATIC_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_STATIC) |
a08727ba | 53 | |
52bad64d DH |
54 | struct delayed_work { |
55 | struct work_struct work; | |
1da177e4 LT |
56 | struct timer_list timer; |
57 | }; | |
58 | ||
bf6aede7 JD |
59 | static inline struct delayed_work *to_delayed_work(struct work_struct *work) |
60 | { | |
61 | return container_of(work, struct delayed_work, work); | |
62 | } | |
63 | ||
1fa44eca JB |
64 | struct execute_work { |
65 | struct work_struct work; | |
66 | }; | |
67 | ||
4e6045f1 JB |
68 | #ifdef CONFIG_LOCKDEP |
69 | /* | |
70 | * NB: because we have to copy the lockdep_map, setting _key | |
71 | * here is required, otherwise it could get initialised to the | |
72 | * copy of the lockdep_map! | |
73 | */ | |
74 | #define __WORK_INIT_LOCKDEP_MAP(n, k) \ | |
75 | .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k), | |
76 | #else | |
77 | #define __WORK_INIT_LOCKDEP_MAP(n, k) | |
78 | #endif | |
79 | ||
65f27f38 | 80 | #define __WORK_INITIALIZER(n, f) { \ |
dc186ad7 | 81 | .data = WORK_DATA_STATIC_INIT(), \ |
23b2e599 | 82 | .entry = { &(n).entry, &(n).entry }, \ |
65f27f38 | 83 | .func = (f), \ |
4e6045f1 | 84 | __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \ |
65f27f38 DH |
85 | } |
86 | ||
87 | #define __DELAYED_WORK_INITIALIZER(n, f) { \ | |
88 | .work = __WORK_INITIALIZER((n).work, (f)), \ | |
89 | .timer = TIMER_INITIALIZER(NULL, 0, 0), \ | |
90 | } | |
91 | ||
65f27f38 DH |
92 | #define DECLARE_WORK(n, f) \ |
93 | struct work_struct n = __WORK_INITIALIZER(n, f) | |
94 | ||
65f27f38 DH |
95 | #define DECLARE_DELAYED_WORK(n, f) \ |
96 | struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f) | |
97 | ||
1da177e4 | 98 | /* |
65f27f38 | 99 | * initialize a work item's function pointer |
1da177e4 | 100 | */ |
65f27f38 | 101 | #define PREPARE_WORK(_work, _func) \ |
1da177e4 | 102 | do { \ |
52bad64d | 103 | (_work)->func = (_func); \ |
1da177e4 LT |
104 | } while (0) |
105 | ||
65f27f38 DH |
106 | #define PREPARE_DELAYED_WORK(_work, _func) \ |
107 | PREPARE_WORK(&(_work)->work, (_func)) | |
52bad64d | 108 | |
dc186ad7 TG |
109 | #ifdef CONFIG_DEBUG_OBJECTS_WORK |
110 | extern void __init_work(struct work_struct *work, int onstack); | |
111 | extern void destroy_work_on_stack(struct work_struct *work); | |
4690c4ab TH |
112 | static inline unsigned int work_static(struct work_struct *work) |
113 | { | |
22df02bb | 114 | return *work_data_bits(work) & WORK_STRUCT_STATIC; |
4690c4ab | 115 | } |
dc186ad7 TG |
116 | #else |
117 | static inline void __init_work(struct work_struct *work, int onstack) { } | |
118 | static inline void destroy_work_on_stack(struct work_struct *work) { } | |
4690c4ab | 119 | static inline unsigned int work_static(struct work_struct *work) { return 0; } |
dc186ad7 TG |
120 | #endif |
121 | ||
1da177e4 | 122 | /* |
52bad64d | 123 | * initialize all of a work item in one go |
a08727ba | 124 | * |
b9049df5 | 125 | * NOTE! No point in using "atomic_long_set()": using a direct |
a08727ba LT |
126 | * assignment of the work data initializer allows the compiler |
127 | * to generate better code. | |
1da177e4 | 128 | */ |
4e6045f1 | 129 | #ifdef CONFIG_LOCKDEP |
dc186ad7 | 130 | #define __INIT_WORK(_work, _func, _onstack) \ |
65f27f38 | 131 | do { \ |
4e6045f1 JB |
132 | static struct lock_class_key __key; \ |
133 | \ | |
dc186ad7 | 134 | __init_work((_work), _onstack); \ |
23b2e599 | 135 | (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ |
4e6045f1 | 136 | lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0);\ |
65f27f38 DH |
137 | INIT_LIST_HEAD(&(_work)->entry); \ |
138 | PREPARE_WORK((_work), (_func)); \ | |
139 | } while (0) | |
4e6045f1 | 140 | #else |
dc186ad7 | 141 | #define __INIT_WORK(_work, _func, _onstack) \ |
4e6045f1 | 142 | do { \ |
dc186ad7 | 143 | __init_work((_work), _onstack); \ |
4e6045f1 JB |
144 | (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \ |
145 | INIT_LIST_HEAD(&(_work)->entry); \ | |
146 | PREPARE_WORK((_work), (_func)); \ | |
147 | } while (0) | |
148 | #endif | |
65f27f38 | 149 | |
dc186ad7 TG |
150 | #define INIT_WORK(_work, _func) \ |
151 | do { \ | |
152 | __INIT_WORK((_work), (_func), 0); \ | |
153 | } while (0) | |
154 | ||
155 | #define INIT_WORK_ON_STACK(_work, _func) \ | |
156 | do { \ | |
157 | __INIT_WORK((_work), (_func), 1); \ | |
158 | } while (0) | |
159 | ||
65f27f38 DH |
160 | #define INIT_DELAYED_WORK(_work, _func) \ |
161 | do { \ | |
162 | INIT_WORK(&(_work)->work, (_func)); \ | |
163 | init_timer(&(_work)->timer); \ | |
52bad64d DH |
164 | } while (0) |
165 | ||
6d612b0f PZ |
166 | #define INIT_DELAYED_WORK_ON_STACK(_work, _func) \ |
167 | do { \ | |
dc186ad7 | 168 | INIT_WORK_ON_STACK(&(_work)->work, (_func)); \ |
6d612b0f PZ |
169 | init_timer_on_stack(&(_work)->timer); \ |
170 | } while (0) | |
171 | ||
dc186ad7 | 172 | #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func) \ |
28287033 VP |
173 | do { \ |
174 | INIT_WORK(&(_work)->work, (_func)); \ | |
175 | init_timer_deferrable(&(_work)->timer); \ | |
176 | } while (0) | |
177 | ||
365970a1 DH |
178 | /** |
179 | * work_pending - Find out whether a work item is currently pending | |
180 | * @work: The work item in question | |
181 | */ | |
182 | #define work_pending(work) \ | |
22df02bb | 183 | test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) |
365970a1 DH |
184 | |
185 | /** | |
186 | * delayed_work_pending - Find out whether a delayable work item is currently | |
187 | * pending | |
188 | * @work: The work item in question | |
189 | */ | |
0221872a LT |
190 | #define delayed_work_pending(w) \ |
191 | work_pending(&(w)->work) | |
365970a1 | 192 | |
65f27f38 | 193 | /** |
23b2e599 ON |
194 | * work_clear_pending - for internal use only, mark a work item as not pending |
195 | * @work: The work item in question | |
65f27f38 | 196 | */ |
23b2e599 | 197 | #define work_clear_pending(work) \ |
22df02bb | 198 | clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work)) |
65f27f38 | 199 | |
97e37d7b TH |
200 | enum { |
201 | WQ_FREEZEABLE = 1 << 0, /* freeze during suspend */ | |
202 | WQ_SINGLE_THREAD = 1 << 1, /* no per-cpu worker */ | |
203 | }; | |
52bad64d | 204 | |
4e6045f1 | 205 | extern struct workqueue_struct * |
97e37d7b | 206 | __create_workqueue_key(const char *name, unsigned int flags, |
c790bce0 | 207 | struct lock_class_key *key, const char *lock_name); |
4e6045f1 JB |
208 | |
209 | #ifdef CONFIG_LOCKDEP | |
97e37d7b | 210 | #define __create_workqueue(name, flags) \ |
4e6045f1 JB |
211 | ({ \ |
212 | static struct lock_class_key __key; \ | |
eb13ba87 JB |
213 | const char *__lock_name; \ |
214 | \ | |
215 | if (__builtin_constant_p(name)) \ | |
216 | __lock_name = (name); \ | |
217 | else \ | |
218 | __lock_name = #name; \ | |
4e6045f1 | 219 | \ |
97e37d7b | 220 | __create_workqueue_key((name), (flags), &__key, \ |
eb13ba87 | 221 | __lock_name); \ |
4e6045f1 JB |
222 | }) |
223 | #else | |
97e37d7b TH |
224 | #define __create_workqueue(name, flags) \ |
225 | __create_workqueue_key((name), (flags), NULL, NULL) | |
4e6045f1 JB |
226 | #endif |
227 | ||
97e37d7b TH |
228 | #define create_workqueue(name) \ |
229 | __create_workqueue((name), 0) | |
230 | #define create_freezeable_workqueue(name) \ | |
231 | __create_workqueue((name), WQ_FREEZEABLE | WQ_SINGLE_THREAD) | |
232 | #define create_singlethread_workqueue(name) \ | |
233 | __create_workqueue((name), WQ_SINGLE_THREAD) | |
1da177e4 LT |
234 | |
235 | extern void destroy_workqueue(struct workqueue_struct *wq); | |
236 | ||
b3c97528 | 237 | extern int queue_work(struct workqueue_struct *wq, struct work_struct *work); |
c1a220e7 ZR |
238 | extern int queue_work_on(int cpu, struct workqueue_struct *wq, |
239 | struct work_struct *work); | |
b3c97528 HH |
240 | extern int queue_delayed_work(struct workqueue_struct *wq, |
241 | struct delayed_work *work, unsigned long delay); | |
7a6bc1cd | 242 | extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq, |
28e53bdd ON |
243 | struct delayed_work *work, unsigned long delay); |
244 | ||
b3c97528 | 245 | extern void flush_workqueue(struct workqueue_struct *wq); |
28e53bdd | 246 | extern void flush_scheduled_work(void); |
43046b60 | 247 | extern void flush_delayed_work(struct delayed_work *work); |
1da177e4 | 248 | |
b3c97528 | 249 | extern int schedule_work(struct work_struct *work); |
c1a220e7 | 250 | extern int schedule_work_on(int cpu, struct work_struct *work); |
b3c97528 | 251 | extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay); |
28e53bdd ON |
252 | extern int schedule_delayed_work_on(int cpu, struct delayed_work *work, |
253 | unsigned long delay); | |
65f27f38 | 254 | extern int schedule_on_each_cpu(work_func_t func); |
1da177e4 LT |
255 | extern int current_is_keventd(void); |
256 | extern int keventd_up(void); | |
257 | ||
258 | extern void init_workqueues(void); | |
65f27f38 | 259 | int execute_in_process_context(work_func_t fn, struct execute_work *); |
1da177e4 | 260 | |
db700897 ON |
261 | extern int flush_work(struct work_struct *work); |
262 | ||
1f1f642e | 263 | extern int cancel_work_sync(struct work_struct *work); |
28e53bdd | 264 | |
1da177e4 LT |
265 | /* |
266 | * Kill off a pending schedule_delayed_work(). Note that the work callback | |
071b6386 ON |
267 | * function may still be running on return from cancel_delayed_work(), unless |
268 | * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or | |
28e53bdd | 269 | * cancel_work_sync() to wait on it. |
1da177e4 | 270 | */ |
52bad64d | 271 | static inline int cancel_delayed_work(struct delayed_work *work) |
1da177e4 LT |
272 | { |
273 | int ret; | |
274 | ||
223a10a9 | 275 | ret = del_timer_sync(&work->timer); |
1da177e4 | 276 | if (ret) |
23b2e599 | 277 | work_clear_pending(&work->work); |
1da177e4 LT |
278 | return ret; |
279 | } | |
280 | ||
4e49627b ON |
281 | /* |
282 | * Like above, but uses del_timer() instead of del_timer_sync(). This means, | |
283 | * if it returns 0 the timer function may be running and the queueing is in | |
284 | * progress. | |
285 | */ | |
286 | static inline int __cancel_delayed_work(struct delayed_work *work) | |
287 | { | |
288 | int ret; | |
289 | ||
290 | ret = del_timer(&work->timer); | |
291 | if (ret) | |
292 | work_clear_pending(&work->work); | |
293 | return ret; | |
294 | } | |
295 | ||
1f1f642e | 296 | extern int cancel_delayed_work_sync(struct delayed_work *work); |
1634c48f | 297 | |
f5a421a4 | 298 | /* Obsolete. use cancel_delayed_work_sync() */ |
1634c48f ON |
299 | static inline |
300 | void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq, | |
301 | struct delayed_work *work) | |
302 | { | |
f5a421a4 ON |
303 | cancel_delayed_work_sync(work); |
304 | } | |
305 | ||
306 | /* Obsolete. use cancel_delayed_work_sync() */ | |
307 | static inline | |
308 | void cancel_rearming_delayed_work(struct delayed_work *work) | |
309 | { | |
310 | cancel_delayed_work_sync(work); | |
1634c48f ON |
311 | } |
312 | ||
2d3854a3 RR |
313 | #ifndef CONFIG_SMP |
314 | static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg) | |
315 | { | |
316 | return fn(arg); | |
317 | } | |
318 | #else | |
319 | long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg); | |
320 | #endif /* CONFIG_SMP */ | |
1da177e4 | 321 | #endif |