aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/workqueue.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/workqueue.h')
-rw-r--r--include/linux/workqueue.h162
1 files changed, 137 insertions, 25 deletions
diff --git a/include/linux/workqueue.h b/include/linux/workqueue.h
index 9466e860d8c2..f11100f96482 100644
--- a/include/linux/workqueue.h
+++ b/include/linux/workqueue.h
@@ -9,6 +9,7 @@
9#include <linux/linkage.h> 9#include <linux/linkage.h>
10#include <linux/bitops.h> 10#include <linux/bitops.h>
11#include <linux/lockdep.h> 11#include <linux/lockdep.h>
12#include <linux/threads.h>
12#include <asm/atomic.h> 13#include <asm/atomic.h>
13 14
14struct workqueue_struct; 15struct workqueue_struct;
@@ -22,12 +23,61 @@ typedef void (*work_func_t)(struct work_struct *work);
22 */ 23 */
23#define work_data_bits(work) ((unsigned long *)(&(work)->data)) 24#define work_data_bits(work) ((unsigned long *)(&(work)->data))
24 25
26enum {
27 WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */
28 WORK_STRUCT_DELAYED_BIT = 1, /* work item is delayed */
29 WORK_STRUCT_CWQ_BIT = 2, /* data points to cwq */
30 WORK_STRUCT_LINKED_BIT = 3, /* next work is linked to this one */
31#ifdef CONFIG_DEBUG_OBJECTS_WORK
32 WORK_STRUCT_STATIC_BIT = 4, /* static initializer (debugobjects) */
33 WORK_STRUCT_COLOR_SHIFT = 5, /* color for workqueue flushing */
34#else
35 WORK_STRUCT_COLOR_SHIFT = 4, /* color for workqueue flushing */
36#endif
37
38 WORK_STRUCT_COLOR_BITS = 4,
39
40 WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT,
41 WORK_STRUCT_DELAYED = 1 << WORK_STRUCT_DELAYED_BIT,
42 WORK_STRUCT_CWQ = 1 << WORK_STRUCT_CWQ_BIT,
43 WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT,
44#ifdef CONFIG_DEBUG_OBJECTS_WORK
45 WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT,
46#else
47 WORK_STRUCT_STATIC = 0,
48#endif
49
50 /*
51 * The last color is no color used for works which don't
52 * participate in workqueue flushing.
53 */
54 WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS) - 1,
55 WORK_NO_COLOR = WORK_NR_COLORS,
56
57 /* special cpu IDs */
58 WORK_CPU_UNBOUND = NR_CPUS,
59 WORK_CPU_NONE = NR_CPUS + 1,
60 WORK_CPU_LAST = WORK_CPU_NONE,
61
62 /*
63 * Reserve 7 bits off of cwq pointer w/ debugobjects turned
64 * off. This makes cwqs aligned to 256 bytes and allows 15
65 * workqueue flush colors.
66 */
67 WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT +
68 WORK_STRUCT_COLOR_BITS,
69
70 WORK_STRUCT_FLAG_MASK = (1UL << WORK_STRUCT_FLAG_BITS) - 1,
71 WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
72 WORK_STRUCT_NO_CPU = WORK_CPU_NONE << WORK_STRUCT_FLAG_BITS,
73
74 /* bit mask for work_busy() return values */
75 WORK_BUSY_PENDING = 1 << 0,
76 WORK_BUSY_RUNNING = 1 << 1,
77};
78
25struct work_struct { 79struct work_struct {
26 atomic_long_t data; 80 atomic_long_t data;
27#define WORK_STRUCT_PENDING 0 /* T if work item pending execution */
28#define WORK_STRUCT_STATIC 1 /* static initializer (debugobjects) */
29#define WORK_STRUCT_FLAG_MASK (3UL)
30#define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)
31 struct list_head entry; 81 struct list_head entry;
32 work_func_t func; 82 work_func_t func;
33#ifdef CONFIG_LOCKDEP 83#ifdef CONFIG_LOCKDEP
@@ -35,8 +85,9 @@ struct work_struct {
35#endif 85#endif
36}; 86};
37 87
38#define WORK_DATA_INIT() ATOMIC_LONG_INIT(0) 88#define WORK_DATA_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU)
39#define WORK_DATA_STATIC_INIT() ATOMIC_LONG_INIT(2) 89#define WORK_DATA_STATIC_INIT() \
90 ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU | WORK_STRUCT_STATIC)
40 91
41struct delayed_work { 92struct delayed_work {
42 struct work_struct work; 93 struct work_struct work;
@@ -96,9 +147,14 @@ struct execute_work {
96#ifdef CONFIG_DEBUG_OBJECTS_WORK 147#ifdef CONFIG_DEBUG_OBJECTS_WORK
97extern void __init_work(struct work_struct *work, int onstack); 148extern void __init_work(struct work_struct *work, int onstack);
98extern void destroy_work_on_stack(struct work_struct *work); 149extern void destroy_work_on_stack(struct work_struct *work);
150static inline unsigned int work_static(struct work_struct *work)
151{
152 return *work_data_bits(work) & WORK_STRUCT_STATIC;
153}
99#else 154#else
100static inline void __init_work(struct work_struct *work, int onstack) { } 155static inline void __init_work(struct work_struct *work, int onstack) { }
101static inline void destroy_work_on_stack(struct work_struct *work) { } 156static inline void destroy_work_on_stack(struct work_struct *work) { }
157static inline unsigned int work_static(struct work_struct *work) { return 0; }
102#endif 158#endif
103 159
104/* 160/*
@@ -162,7 +218,7 @@ static inline void destroy_work_on_stack(struct work_struct *work) { }
162 * @work: The work item in question 218 * @work: The work item in question
163 */ 219 */
164#define work_pending(work) \ 220#define work_pending(work) \
165 test_bit(WORK_STRUCT_PENDING, work_data_bits(work)) 221 test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
166 222
167/** 223/**
168 * delayed_work_pending - Find out whether a delayable work item is currently 224 * delayed_work_pending - Find out whether a delayable work item is currently
@@ -177,16 +233,58 @@ static inline void destroy_work_on_stack(struct work_struct *work) { }
177 * @work: The work item in question 233 * @work: The work item in question
178 */ 234 */
179#define work_clear_pending(work) \ 235#define work_clear_pending(work) \
180 clear_bit(WORK_STRUCT_PENDING, work_data_bits(work)) 236 clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
237
238enum {
239 WQ_NON_REENTRANT = 1 << 0, /* guarantee non-reentrance */
240 WQ_UNBOUND = 1 << 1, /* not bound to any cpu */
241 WQ_FREEZEABLE = 1 << 2, /* freeze during suspend */
242 WQ_RESCUER = 1 << 3, /* has an rescue worker */
243 WQ_HIGHPRI = 1 << 4, /* high priority */
244 WQ_CPU_INTENSIVE = 1 << 5, /* cpu instensive workqueue */
181 245
246 WQ_DYING = 1 << 6, /* internal: workqueue is dying */
247
248 WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */
249 WQ_MAX_UNBOUND_PER_CPU = 4, /* 4 * #cpus for unbound wq */
250 WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2,
251};
252
253/* unbound wq's aren't per-cpu, scale max_active according to #cpus */
254#define WQ_UNBOUND_MAX_ACTIVE \
255 max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU)
256
257/*
258 * System-wide workqueues which are always present.
259 *
260 * system_wq is the one used by schedule[_delayed]_work[_on]().
261 * Multi-CPU multi-threaded. There are users which expect relatively
262 * short queue flush time. Don't queue works which can run for too
263 * long.
264 *
265 * system_long_wq is similar to system_wq but may host long running
266 * works. Queue flushing might take relatively long.
267 *
268 * system_nrt_wq is non-reentrant and guarantees that any given work
269 * item is never executed in parallel by multiple CPUs. Queue
270 * flushing might take relatively long.
271 *
272 * system_unbound_wq is unbound workqueue. Workers are not bound to
273 * any specific CPU, not concurrency managed, and all queued works are
274 * executed immediately as long as max_active limit is not reached and
275 * resources are available.
276 */
277extern struct workqueue_struct *system_wq;
278extern struct workqueue_struct *system_long_wq;
279extern struct workqueue_struct *system_nrt_wq;
280extern struct workqueue_struct *system_unbound_wq;
182 281
183extern struct workqueue_struct * 282extern struct workqueue_struct *
184__create_workqueue_key(const char *name, int singlethread, 283__alloc_workqueue_key(const char *name, unsigned int flags, int max_active,
185 int freezeable, int rt, struct lock_class_key *key, 284 struct lock_class_key *key, const char *lock_name);
186 const char *lock_name);
187 285
188#ifdef CONFIG_LOCKDEP 286#ifdef CONFIG_LOCKDEP
189#define __create_workqueue(name, singlethread, freezeable, rt) \ 287#define alloc_workqueue(name, flags, max_active) \
190({ \ 288({ \
191 static struct lock_class_key __key; \ 289 static struct lock_class_key __key; \
192 const char *__lock_name; \ 290 const char *__lock_name; \
@@ -196,20 +294,20 @@ __create_workqueue_key(const char *name, int singlethread,
196 else \ 294 else \
197 __lock_name = #name; \ 295 __lock_name = #name; \
198 \ 296 \
199 __create_workqueue_key((name), (singlethread), \ 297 __alloc_workqueue_key((name), (flags), (max_active), \
200 (freezeable), (rt), &__key, \ 298 &__key, __lock_name); \
201 __lock_name); \
202}) 299})
203#else 300#else
204#define __create_workqueue(name, singlethread, freezeable, rt) \ 301#define alloc_workqueue(name, flags, max_active) \
205 __create_workqueue_key((name), (singlethread), (freezeable), (rt), \ 302 __alloc_workqueue_key((name), (flags), (max_active), NULL, NULL)
206 NULL, NULL)
207#endif 303#endif
208 304
209#define create_workqueue(name) __create_workqueue((name), 0, 0, 0) 305#define create_workqueue(name) \
210#define create_rt_workqueue(name) __create_workqueue((name), 0, 0, 1) 306 alloc_workqueue((name), WQ_RESCUER, 1)
211#define create_freezeable_workqueue(name) __create_workqueue((name), 1, 1, 0) 307#define create_freezeable_workqueue(name) \
212#define create_singlethread_workqueue(name) __create_workqueue((name), 1, 0, 0) 308 alloc_workqueue((name), WQ_FREEZEABLE | WQ_UNBOUND | WQ_RESCUER, 1)
309#define create_singlethread_workqueue(name) \
310 alloc_workqueue((name), WQ_UNBOUND | WQ_RESCUER, 1)
213 311
214extern void destroy_workqueue(struct workqueue_struct *wq); 312extern void destroy_workqueue(struct workqueue_struct *wq);
215 313
@@ -231,16 +329,19 @@ extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay)
231extern int schedule_delayed_work_on(int cpu, struct delayed_work *work, 329extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
232 unsigned long delay); 330 unsigned long delay);
233extern int schedule_on_each_cpu(work_func_t func); 331extern int schedule_on_each_cpu(work_func_t func);
234extern int current_is_keventd(void);
235extern int keventd_up(void); 332extern int keventd_up(void);
236 333
237extern void init_workqueues(void);
238int execute_in_process_context(work_func_t fn, struct execute_work *); 334int execute_in_process_context(work_func_t fn, struct execute_work *);
239 335
240extern int flush_work(struct work_struct *work); 336extern int flush_work(struct work_struct *work);
241
242extern int cancel_work_sync(struct work_struct *work); 337extern int cancel_work_sync(struct work_struct *work);
243 338
339extern void workqueue_set_max_active(struct workqueue_struct *wq,
340 int max_active);
341extern bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq);
342extern unsigned int work_cpu(struct work_struct *work);
343extern unsigned int work_busy(struct work_struct *work);
344
244/* 345/*
245 * Kill off a pending schedule_delayed_work(). Note that the work callback 346 * Kill off a pending schedule_delayed_work(). Note that the work callback
246 * function may still be running on return from cancel_delayed_work(), unless 347 * function may still be running on return from cancel_delayed_work(), unless
@@ -297,4 +398,15 @@ static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
297#else 398#else
298long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg); 399long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg);
299#endif /* CONFIG_SMP */ 400#endif /* CONFIG_SMP */
401
402#ifdef CONFIG_FREEZER
403extern void freeze_workqueues_begin(void);
404extern bool freeze_workqueues_busy(void);
405extern void thaw_workqueues(void);
406#endif /* CONFIG_FREEZER */
407
408#ifdef CONFIG_LOCKDEP
409int in_workqueue_context(struct workqueue_struct *wq);
410#endif
411
300#endif 412#endif