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