2 * workqueue.h --- work queue handling for Linux.
5 #ifndef _LINUX_WORKQUEUE_H
6 #define _LINUX_WORKQUEUE_H
8 #include <linux/timer.h>
9 #include <linux/linkage.h>
10 #include <linux/bitops.h>
11 #include <linux/lockdep.h>
12 #include <asm/atomic.h>
14 struct workqueue_struct;
17 typedef void (*work_func_t)(struct work_struct *work);
20 * The first word is the work queue pointer and the flags rolled into
23 #define work_data_bits(work) ((unsigned long *)(&(work)->data))
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) */
31 WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT,
32 #ifdef CONFIG_DEBUG_OBJECTS_WORK
33 WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT,
35 WORK_STRUCT_STATIC = 0,
38 WORK_STRUCT_FLAG_MASK = 3UL,
39 WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
44 struct list_head entry;
47 struct lockdep_map lockdep_map;
51 #define WORK_DATA_INIT() ATOMIC_LONG_INIT(0)
52 #define WORK_DATA_STATIC_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_STATIC)
55 struct work_struct work;
56 struct timer_list timer;
59 static inline struct delayed_work *to_delayed_work(struct work_struct *work)
61 return container_of(work, struct delayed_work, work);
65 struct work_struct work;
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!
74 #define __WORK_INIT_LOCKDEP_MAP(n, k) \
75 .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
77 #define __WORK_INIT_LOCKDEP_MAP(n, k)
80 #define __WORK_INITIALIZER(n, f) { \
81 .data = WORK_DATA_STATIC_INIT(), \
82 .entry = { &(n).entry, &(n).entry }, \
84 __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \
87 #define __DELAYED_WORK_INITIALIZER(n, f) { \
88 .work = __WORK_INITIALIZER((n).work, (f)), \
89 .timer = TIMER_INITIALIZER(NULL, 0, 0), \
92 #define DECLARE_WORK(n, f) \
93 struct work_struct n = __WORK_INITIALIZER(n, f)
95 #define DECLARE_DELAYED_WORK(n, f) \
96 struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
99 * initialize a work item's function pointer
101 #define PREPARE_WORK(_work, _func) \
103 (_work)->func = (_func); \
106 #define PREPARE_DELAYED_WORK(_work, _func) \
107 PREPARE_WORK(&(_work)->work, (_func))
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);
112 static inline unsigned int work_static(struct work_struct *work)
114 return *work_data_bits(work) & WORK_STRUCT_STATIC;
117 static inline void __init_work(struct work_struct *work, int onstack) { }
118 static inline void destroy_work_on_stack(struct work_struct *work) { }
119 static inline unsigned int work_static(struct work_struct *work) { return 0; }
123 * initialize all of a work item in one go
125 * NOTE! No point in using "atomic_long_set()": using a direct
126 * assignment of the work data initializer allows the compiler
127 * to generate better code.
129 #ifdef CONFIG_LOCKDEP
130 #define __INIT_WORK(_work, _func, _onstack) \
132 static struct lock_class_key __key; \
134 __init_work((_work), _onstack); \
135 (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
136 lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0);\
137 INIT_LIST_HEAD(&(_work)->entry); \
138 PREPARE_WORK((_work), (_func)); \
141 #define __INIT_WORK(_work, _func, _onstack) \
143 __init_work((_work), _onstack); \
144 (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
145 INIT_LIST_HEAD(&(_work)->entry); \
146 PREPARE_WORK((_work), (_func)); \
150 #define INIT_WORK(_work, _func) \
152 __INIT_WORK((_work), (_func), 0); \
155 #define INIT_WORK_ON_STACK(_work, _func) \
157 __INIT_WORK((_work), (_func), 1); \
160 #define INIT_DELAYED_WORK(_work, _func) \
162 INIT_WORK(&(_work)->work, (_func)); \
163 init_timer(&(_work)->timer); \
166 #define INIT_DELAYED_WORK_ON_STACK(_work, _func) \
168 INIT_WORK_ON_STACK(&(_work)->work, (_func)); \
169 init_timer_on_stack(&(_work)->timer); \
172 #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func) \
174 INIT_WORK(&(_work)->work, (_func)); \
175 init_timer_deferrable(&(_work)->timer); \
179 * work_pending - Find out whether a work item is currently pending
180 * @work: The work item in question
182 #define work_pending(work) \
183 test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
186 * delayed_work_pending - Find out whether a delayable work item is currently
188 * @work: The work item in question
190 #define delayed_work_pending(w) \
191 work_pending(&(w)->work)
194 * work_clear_pending - for internal use only, mark a work item as not pending
195 * @work: The work item in question
197 #define work_clear_pending(work) \
198 clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
201 WQ_FREEZEABLE = 1 << 0, /* freeze during suspend */
202 WQ_SINGLE_THREAD = 1 << 1, /* no per-cpu worker */
205 extern struct workqueue_struct *
206 __create_workqueue_key(const char *name, unsigned int flags,
207 struct lock_class_key *key, const char *lock_name);
209 #ifdef CONFIG_LOCKDEP
210 #define __create_workqueue(name, flags) \
212 static struct lock_class_key __key; \
213 const char *__lock_name; \
215 if (__builtin_constant_p(name)) \
216 __lock_name = (name); \
218 __lock_name = #name; \
220 __create_workqueue_key((name), (flags), &__key, \
224 #define __create_workqueue(name, flags) \
225 __create_workqueue_key((name), (flags), NULL, NULL)
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)
235 extern void destroy_workqueue(struct workqueue_struct *wq);
237 extern int queue_work(struct workqueue_struct *wq, struct work_struct *work);
238 extern int queue_work_on(int cpu, struct workqueue_struct *wq,
239 struct work_struct *work);
240 extern int queue_delayed_work(struct workqueue_struct *wq,
241 struct delayed_work *work, unsigned long delay);
242 extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
243 struct delayed_work *work, unsigned long delay);
245 extern void flush_workqueue(struct workqueue_struct *wq);
246 extern void flush_scheduled_work(void);
247 extern void flush_delayed_work(struct delayed_work *work);
249 extern int schedule_work(struct work_struct *work);
250 extern int schedule_work_on(int cpu, struct work_struct *work);
251 extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay);
252 extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
253 unsigned long delay);
254 extern int schedule_on_each_cpu(work_func_t func);
255 extern int current_is_keventd(void);
256 extern int keventd_up(void);
258 extern void init_workqueues(void);
259 int execute_in_process_context(work_func_t fn, struct execute_work *);
261 extern int flush_work(struct work_struct *work);
263 extern int cancel_work_sync(struct work_struct *work);
266 * Kill off a pending schedule_delayed_work(). Note that the work callback
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
269 * cancel_work_sync() to wait on it.
271 static inline int cancel_delayed_work(struct delayed_work *work)
275 ret = del_timer_sync(&work->timer);
277 work_clear_pending(&work->work);
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
286 static inline int __cancel_delayed_work(struct delayed_work *work)
290 ret = del_timer(&work->timer);
292 work_clear_pending(&work->work);
296 extern int cancel_delayed_work_sync(struct delayed_work *work);
298 /* Obsolete. use cancel_delayed_work_sync() */
300 void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
301 struct delayed_work *work)
303 cancel_delayed_work_sync(work);
306 /* Obsolete. use cancel_delayed_work_sync() */
308 void cancel_rearming_delayed_work(struct delayed_work *work)
310 cancel_delayed_work_sync(work);
314 static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
319 long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg);
320 #endif /* CONFIG_SMP */