xref: /openbmc/linux/include/linux/workqueue.h (revision 60063497)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * workqueue.h --- work queue handling for Linux.
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds #ifndef _LINUX_WORKQUEUE_H
61da177e4SLinus Torvalds #define _LINUX_WORKQUEUE_H
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds #include <linux/timer.h>
91da177e4SLinus Torvalds #include <linux/linkage.h>
101da177e4SLinus Torvalds #include <linux/bitops.h>
114e6045f1SJohannes Berg #include <linux/lockdep.h>
127a22ad75STejun Heo #include <linux/threads.h>
1360063497SArun Sharma #include <linux/atomic.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds struct workqueue_struct;
161da177e4SLinus Torvalds 
1765f27f38SDavid Howells struct work_struct;
1865f27f38SDavid Howells typedef void (*work_func_t)(struct work_struct *work);
196bb49e59SDavid Howells 
20a08727baSLinus Torvalds /*
21a08727baSLinus Torvalds  * The first word is the work queue pointer and the flags rolled into
22a08727baSLinus Torvalds  * one
23a08727baSLinus Torvalds  */
24a08727baSLinus Torvalds #define work_data_bits(work) ((unsigned long *)(&(work)->data))
25a08727baSLinus Torvalds 
2622df02bbSTejun Heo enum {
2722df02bbSTejun Heo 	WORK_STRUCT_PENDING_BIT	= 0,	/* work item is pending execution */
288a2e8e5dSTejun Heo 	WORK_STRUCT_DELAYED_BIT	= 1,	/* work item is delayed */
298a2e8e5dSTejun Heo 	WORK_STRUCT_CWQ_BIT	= 2,	/* data points to cwq */
308a2e8e5dSTejun Heo 	WORK_STRUCT_LINKED_BIT	= 3,	/* next work is linked to this one */
3122df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK
328a2e8e5dSTejun Heo 	WORK_STRUCT_STATIC_BIT	= 4,	/* static initializer (debugobjects) */
338a2e8e5dSTejun Heo 	WORK_STRUCT_COLOR_SHIFT	= 5,	/* color for workqueue flushing */
340f900049STejun Heo #else
358a2e8e5dSTejun Heo 	WORK_STRUCT_COLOR_SHIFT	= 4,	/* color for workqueue flushing */
3622df02bbSTejun Heo #endif
3722df02bbSTejun Heo 
3873f53c4aSTejun Heo 	WORK_STRUCT_COLOR_BITS	= 4,
3973f53c4aSTejun Heo 
4022df02bbSTejun Heo 	WORK_STRUCT_PENDING	= 1 << WORK_STRUCT_PENDING_BIT,
418a2e8e5dSTejun Heo 	WORK_STRUCT_DELAYED	= 1 << WORK_STRUCT_DELAYED_BIT,
42e120153dSTejun Heo 	WORK_STRUCT_CWQ		= 1 << WORK_STRUCT_CWQ_BIT,
43affee4b2STejun Heo 	WORK_STRUCT_LINKED	= 1 << WORK_STRUCT_LINKED_BIT,
4422df02bbSTejun Heo #ifdef CONFIG_DEBUG_OBJECTS_WORK
4522df02bbSTejun Heo 	WORK_STRUCT_STATIC	= 1 << WORK_STRUCT_STATIC_BIT,
4622df02bbSTejun Heo #else
4722df02bbSTejun Heo 	WORK_STRUCT_STATIC	= 0,
4822df02bbSTejun Heo #endif
4922df02bbSTejun Heo 
5073f53c4aSTejun Heo 	/*
5173f53c4aSTejun Heo 	 * The last color is no color used for works which don't
5273f53c4aSTejun Heo 	 * participate in workqueue flushing.
5373f53c4aSTejun Heo 	 */
5473f53c4aSTejun Heo 	WORK_NR_COLORS		= (1 << WORK_STRUCT_COLOR_BITS) - 1,
5573f53c4aSTejun Heo 	WORK_NO_COLOR		= WORK_NR_COLORS,
5673f53c4aSTejun Heo 
57bdbc5dd7STejun Heo 	/* special cpu IDs */
58f3421797STejun Heo 	WORK_CPU_UNBOUND	= NR_CPUS,
59f3421797STejun Heo 	WORK_CPU_NONE		= NR_CPUS + 1,
60bdbc5dd7STejun Heo 	WORK_CPU_LAST		= WORK_CPU_NONE,
61bdbc5dd7STejun Heo 
6273f53c4aSTejun Heo 	/*
63e120153dSTejun Heo 	 * Reserve 7 bits off of cwq pointer w/ debugobjects turned
648a2e8e5dSTejun Heo 	 * off.  This makes cwqs aligned to 256 bytes and allows 15
658a2e8e5dSTejun Heo 	 * workqueue flush colors.
6673f53c4aSTejun Heo 	 */
6773f53c4aSTejun Heo 	WORK_STRUCT_FLAG_BITS	= WORK_STRUCT_COLOR_SHIFT +
6873f53c4aSTejun Heo 				  WORK_STRUCT_COLOR_BITS,
6973f53c4aSTejun Heo 
700f900049STejun Heo 	WORK_STRUCT_FLAG_MASK	= (1UL << WORK_STRUCT_FLAG_BITS) - 1,
7122df02bbSTejun Heo 	WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
72bdbc5dd7STejun Heo 	WORK_STRUCT_NO_CPU	= WORK_CPU_NONE << WORK_STRUCT_FLAG_BITS,
73dcd989cbSTejun Heo 
74dcd989cbSTejun Heo 	/* bit mask for work_busy() return values */
75dcd989cbSTejun Heo 	WORK_BUSY_PENDING	= 1 << 0,
76dcd989cbSTejun Heo 	WORK_BUSY_RUNNING	= 1 << 1,
7722df02bbSTejun Heo };
7822df02bbSTejun Heo 
791da177e4SLinus Torvalds struct work_struct {
80a08727baSLinus Torvalds 	atomic_long_t data;
811da177e4SLinus Torvalds 	struct list_head entry;
826bb49e59SDavid Howells 	work_func_t func;
834e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
844e6045f1SJohannes Berg 	struct lockdep_map lockdep_map;
854e6045f1SJohannes Berg #endif
8652bad64dSDavid Howells };
8752bad64dSDavid Howells 
887a22ad75STejun Heo #define WORK_DATA_INIT()	ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU)
897a22ad75STejun Heo #define WORK_DATA_STATIC_INIT()	\
907a22ad75STejun Heo 	ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU | WORK_STRUCT_STATIC)
91a08727baSLinus Torvalds 
9252bad64dSDavid Howells struct delayed_work {
9352bad64dSDavid Howells 	struct work_struct work;
941da177e4SLinus Torvalds 	struct timer_list timer;
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
97bf6aede7SJean Delvare static inline struct delayed_work *to_delayed_work(struct work_struct *work)
98bf6aede7SJean Delvare {
99bf6aede7SJean Delvare 	return container_of(work, struct delayed_work, work);
100bf6aede7SJean Delvare }
101bf6aede7SJean Delvare 
1021fa44ecaSJames Bottomley struct execute_work {
1031fa44ecaSJames Bottomley 	struct work_struct work;
1041fa44ecaSJames Bottomley };
1051fa44ecaSJames Bottomley 
1064e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1074e6045f1SJohannes Berg /*
1084e6045f1SJohannes Berg  * NB: because we have to copy the lockdep_map, setting _key
1094e6045f1SJohannes Berg  * here is required, otherwise it could get initialised to the
1104e6045f1SJohannes Berg  * copy of the lockdep_map!
1114e6045f1SJohannes Berg  */
1124e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \
1134e6045f1SJohannes Berg 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
1144e6045f1SJohannes Berg #else
1154e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k)
1164e6045f1SJohannes Berg #endif
1174e6045f1SJohannes Berg 
11865f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) {				\
119dc186ad7SThomas Gleixner 	.data = WORK_DATA_STATIC_INIT(),			\
12065f27f38SDavid Howells 	.entry	= { &(n).entry, &(n).entry },			\
12165f27f38SDavid Howells 	.func = (f),						\
1224e6045f1SJohannes Berg 	__WORK_INIT_LOCKDEP_MAP(#n, &(n))			\
12365f27f38SDavid Howells 	}
12465f27f38SDavid Howells 
12565f27f38SDavid Howells #define __DELAYED_WORK_INITIALIZER(n, f) {			\
12665f27f38SDavid Howells 	.work = __WORK_INITIALIZER((n).work, (f)),		\
1271da177e4SLinus Torvalds 	.timer = TIMER_INITIALIZER(NULL, 0, 0),			\
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
130dd6414b5SPhil Carmody #define __DEFERRED_WORK_INITIALIZER(n, f) {			\
131dd6414b5SPhil Carmody 	.work = __WORK_INITIALIZER((n).work, (f)),		\
132dd6414b5SPhil Carmody 	.timer = TIMER_DEFERRED_INITIALIZER(NULL, 0, 0),	\
133dd6414b5SPhil Carmody 	}
134dd6414b5SPhil Carmody 
13565f27f38SDavid Howells #define DECLARE_WORK(n, f)					\
13665f27f38SDavid Howells 	struct work_struct n = __WORK_INITIALIZER(n, f)
13765f27f38SDavid Howells 
13865f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f)				\
13965f27f38SDavid Howells 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
14065f27f38SDavid Howells 
141dd6414b5SPhil Carmody #define DECLARE_DEFERRED_WORK(n, f)				\
142dd6414b5SPhil Carmody 	struct delayed_work n = __DEFERRED_WORK_INITIALIZER(n, f)
143dd6414b5SPhil Carmody 
1441da177e4SLinus Torvalds /*
14565f27f38SDavid Howells  * initialize a work item's function pointer
1461da177e4SLinus Torvalds  */
14765f27f38SDavid Howells #define PREPARE_WORK(_work, _func)				\
1481da177e4SLinus Torvalds 	do {							\
14952bad64dSDavid Howells 		(_work)->func = (_func);			\
1501da177e4SLinus Torvalds 	} while (0)
1511da177e4SLinus Torvalds 
15265f27f38SDavid Howells #define PREPARE_DELAYED_WORK(_work, _func)			\
15365f27f38SDavid Howells 	PREPARE_WORK(&(_work)->work, (_func))
15452bad64dSDavid Howells 
155dc186ad7SThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_WORK
156dc186ad7SThomas Gleixner extern void __init_work(struct work_struct *work, int onstack);
157dc186ad7SThomas Gleixner extern void destroy_work_on_stack(struct work_struct *work);
1584690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work)
1594690c4abSTejun Heo {
16022df02bbSTejun Heo 	return *work_data_bits(work) & WORK_STRUCT_STATIC;
1614690c4abSTejun Heo }
162dc186ad7SThomas Gleixner #else
163dc186ad7SThomas Gleixner static inline void __init_work(struct work_struct *work, int onstack) { }
164dc186ad7SThomas Gleixner static inline void destroy_work_on_stack(struct work_struct *work) { }
1654690c4abSTejun Heo static inline unsigned int work_static(struct work_struct *work) { return 0; }
166dc186ad7SThomas Gleixner #endif
167dc186ad7SThomas Gleixner 
1681da177e4SLinus Torvalds /*
16952bad64dSDavid Howells  * initialize all of a work item in one go
170a08727baSLinus Torvalds  *
171b9049df5SDmitri Vorobiev  * NOTE! No point in using "atomic_long_set()": using a direct
172a08727baSLinus Torvalds  * assignment of the work data initializer allows the compiler
173a08727baSLinus Torvalds  * to generate better code.
1741da177e4SLinus Torvalds  */
1754e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
176dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack)				\
1774e6045f1SJohannes Berg 	do {								\
1784e6045f1SJohannes Berg 		static struct lock_class_key __key;			\
1794e6045f1SJohannes Berg 									\
180dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
1814e6045f1SJohannes Berg 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
1824e6045f1SJohannes Berg 		lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0);\
1834e6045f1SJohannes Berg 		INIT_LIST_HEAD(&(_work)->entry);			\
1844e6045f1SJohannes Berg 		PREPARE_WORK((_work), (_func));				\
1854e6045f1SJohannes Berg 	} while (0)
1864e6045f1SJohannes Berg #else
187dc186ad7SThomas Gleixner #define __INIT_WORK(_work, _func, _onstack)				\
1881da177e4SLinus Torvalds 	do {								\
189dc186ad7SThomas Gleixner 		__init_work((_work), _onstack);				\
19023b2e599SOleg Nesterov 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
19165f27f38SDavid Howells 		INIT_LIST_HEAD(&(_work)->entry);			\
19265f27f38SDavid Howells 		PREPARE_WORK((_work), (_func));				\
19365f27f38SDavid Howells 	} while (0)
1944e6045f1SJohannes Berg #endif
19565f27f38SDavid Howells 
196dc186ad7SThomas Gleixner #define INIT_WORK(_work, _func)					\
197dc186ad7SThomas Gleixner 	do {							\
198dc186ad7SThomas Gleixner 		__INIT_WORK((_work), (_func), 0);		\
199dc186ad7SThomas Gleixner 	} while (0)
200dc186ad7SThomas Gleixner 
201ca1cab37SAndrew Morton #define INIT_WORK_ONSTACK(_work, _func)				\
202dc186ad7SThomas Gleixner 	do {							\
203dc186ad7SThomas Gleixner 		__INIT_WORK((_work), (_func), 1);		\
204dc186ad7SThomas Gleixner 	} while (0)
205dc186ad7SThomas Gleixner 
20665f27f38SDavid Howells #define INIT_DELAYED_WORK(_work, _func)				\
20765f27f38SDavid Howells 	do {							\
20865f27f38SDavid Howells 		INIT_WORK(&(_work)->work, (_func));		\
20965f27f38SDavid Howells 		init_timer(&(_work)->timer);			\
21065f27f38SDavid Howells 	} while (0)
21165f27f38SDavid Howells 
212ca1cab37SAndrew Morton #define INIT_DELAYED_WORK_ONSTACK(_work, _func)			\
2136d612b0fSPeter Zijlstra 	do {							\
214ca1cab37SAndrew Morton 		INIT_WORK_ONSTACK(&(_work)->work, (_func));	\
2156d612b0fSPeter Zijlstra 		init_timer_on_stack(&(_work)->timer);		\
2166d612b0fSPeter Zijlstra 	} while (0)
2176d612b0fSPeter Zijlstra 
21828287033SVenki Pallipadi #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func)		\
21928287033SVenki Pallipadi 	do {							\
22028287033SVenki Pallipadi 		INIT_WORK(&(_work)->work, (_func));		\
22128287033SVenki Pallipadi 		init_timer_deferrable(&(_work)->timer);		\
22228287033SVenki Pallipadi 	} while (0)
22328287033SVenki Pallipadi 
224365970a1SDavid Howells /**
225365970a1SDavid Howells  * work_pending - Find out whether a work item is currently pending
226365970a1SDavid Howells  * @work: The work item in question
227365970a1SDavid Howells  */
228365970a1SDavid Howells #define work_pending(work) \
22922df02bbSTejun Heo 	test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
230365970a1SDavid Howells 
231365970a1SDavid Howells /**
232365970a1SDavid Howells  * delayed_work_pending - Find out whether a delayable work item is currently
233365970a1SDavid Howells  * pending
234365970a1SDavid Howells  * @work: The work item in question
235365970a1SDavid Howells  */
2360221872aSLinus Torvalds #define delayed_work_pending(w) \
2370221872aSLinus Torvalds 	work_pending(&(w)->work)
238365970a1SDavid Howells 
23965f27f38SDavid Howells /**
24023b2e599SOleg Nesterov  * work_clear_pending - for internal use only, mark a work item as not pending
24123b2e599SOleg Nesterov  * @work: The work item in question
24265f27f38SDavid Howells  */
24323b2e599SOleg Nesterov #define work_clear_pending(work) \
24422df02bbSTejun Heo 	clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
24565f27f38SDavid Howells 
246c54fce6eSTejun Heo /*
247c54fce6eSTejun Heo  * Workqueue flags and constants.  For details, please refer to
248c54fce6eSTejun Heo  * Documentation/workqueue.txt.
249c54fce6eSTejun Heo  */
25097e37d7bSTejun Heo enum {
251bdbc5dd7STejun Heo 	WQ_NON_REENTRANT	= 1 << 0, /* guarantee non-reentrance */
252c7fc77f7STejun Heo 	WQ_UNBOUND		= 1 << 1, /* not bound to any cpu */
25358a69cb4STejun Heo 	WQ_FREEZABLE		= 1 << 2, /* freeze during suspend */
2546370a6adSTejun Heo 	WQ_MEM_RECLAIM		= 1 << 3, /* may be used for memory reclaim */
255649027d7STejun Heo 	WQ_HIGHPRI		= 1 << 4, /* high priority */
256fb0e7bebSTejun Heo 	WQ_CPU_INTENSIVE	= 1 << 5, /* cpu instensive workqueue */
257b71ab8c2STejun Heo 
2589c5a2ba7STejun Heo 	WQ_DRAINING		= 1 << 6, /* internal: workqueue is draining */
2596370a6adSTejun Heo 	WQ_RESCUER		= 1 << 7, /* internal: workqueue has rescuer */
260e41e704bSTejun Heo 
261b71ab8c2STejun Heo 	WQ_MAX_ACTIVE		= 512,	  /* I like 512, better ideas? */
262f3421797STejun Heo 	WQ_MAX_UNBOUND_PER_CPU	= 4,	  /* 4 * #cpus for unbound wq */
263b71ab8c2STejun Heo 	WQ_DFL_ACTIVE		= WQ_MAX_ACTIVE / 2,
26497e37d7bSTejun Heo };
26552bad64dSDavid Howells 
266f3421797STejun Heo /* unbound wq's aren't per-cpu, scale max_active according to #cpus */
267f3421797STejun Heo #define WQ_UNBOUND_MAX_ACTIVE	\
268f3421797STejun Heo 	max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU)
269f3421797STejun Heo 
270d320c038STejun Heo /*
271d320c038STejun Heo  * System-wide workqueues which are always present.
272d320c038STejun Heo  *
273d320c038STejun Heo  * system_wq is the one used by schedule[_delayed]_work[_on]().
274d320c038STejun Heo  * Multi-CPU multi-threaded.  There are users which expect relatively
275d320c038STejun Heo  * short queue flush time.  Don't queue works which can run for too
276d320c038STejun Heo  * long.
277d320c038STejun Heo  *
278d320c038STejun Heo  * system_long_wq is similar to system_wq but may host long running
279d320c038STejun Heo  * works.  Queue flushing might take relatively long.
280d320c038STejun Heo  *
281d320c038STejun Heo  * system_nrt_wq is non-reentrant and guarantees that any given work
282d320c038STejun Heo  * item is never executed in parallel by multiple CPUs.  Queue
283d320c038STejun Heo  * flushing might take relatively long.
284f3421797STejun Heo  *
285f3421797STejun Heo  * system_unbound_wq is unbound workqueue.  Workers are not bound to
286f3421797STejun Heo  * any specific CPU, not concurrency managed, and all queued works are
287f3421797STejun Heo  * executed immediately as long as max_active limit is not reached and
288f3421797STejun Heo  * resources are available.
2894149efb2STejun Heo  *
29024d51addSTejun Heo  * system_freezable_wq is equivalent to system_wq except that it's
29124d51addSTejun Heo  * freezable.
292d320c038STejun Heo  */
293d320c038STejun Heo extern struct workqueue_struct *system_wq;
294d320c038STejun Heo extern struct workqueue_struct *system_long_wq;
295d320c038STejun Heo extern struct workqueue_struct *system_nrt_wq;
296f3421797STejun Heo extern struct workqueue_struct *system_unbound_wq;
29724d51addSTejun Heo extern struct workqueue_struct *system_freezable_wq;
2981da177e4SLinus Torvalds 
2994e6045f1SJohannes Berg extern struct workqueue_struct *
300d320c038STejun Heo __alloc_workqueue_key(const char *name, unsigned int flags, int max_active,
301c790bce0STejun Heo 		      struct lock_class_key *key, const char *lock_name);
3024e6045f1SJohannes Berg 
3034e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
304d320c038STejun Heo #define alloc_workqueue(name, flags, max_active)		\
3054e6045f1SJohannes Berg ({								\
3064e6045f1SJohannes Berg 	static struct lock_class_key __key;			\
307eb13ba87SJohannes Berg 	const char *__lock_name;				\
308eb13ba87SJohannes Berg 								\
309eb13ba87SJohannes Berg 	if (__builtin_constant_p(name))				\
310eb13ba87SJohannes Berg 		__lock_name = (name);				\
311eb13ba87SJohannes Berg 	else							\
312eb13ba87SJohannes Berg 		__lock_name = #name;				\
3134e6045f1SJohannes Berg 								\
314d320c038STejun Heo 	__alloc_workqueue_key((name), (flags), (max_active),	\
3151e19ffc6STejun Heo 			      &__key, __lock_name);		\
3164e6045f1SJohannes Berg })
3174e6045f1SJohannes Berg #else
318d320c038STejun Heo #define alloc_workqueue(name, flags, max_active)		\
319d320c038STejun Heo 	__alloc_workqueue_key((name), (flags), (max_active), NULL, NULL)
3204e6045f1SJohannes Berg #endif
3214e6045f1SJohannes Berg 
32281dcaf65STejun Heo /**
32381dcaf65STejun Heo  * alloc_ordered_workqueue - allocate an ordered workqueue
32481dcaf65STejun Heo  * @name: name of the workqueue
32558a69cb4STejun Heo  * @flags: WQ_* flags (only WQ_FREEZABLE and WQ_MEM_RECLAIM are meaningful)
32681dcaf65STejun Heo  *
32781dcaf65STejun Heo  * Allocate an ordered workqueue.  An ordered workqueue executes at
32881dcaf65STejun Heo  * most one work item at any given time in the queued order.  They are
32981dcaf65STejun Heo  * implemented as unbound workqueues with @max_active of one.
33081dcaf65STejun Heo  *
33181dcaf65STejun Heo  * RETURNS:
33281dcaf65STejun Heo  * Pointer to the allocated workqueue on success, %NULL on failure.
33381dcaf65STejun Heo  */
33481dcaf65STejun Heo static inline struct workqueue_struct *
33581dcaf65STejun Heo alloc_ordered_workqueue(const char *name, unsigned int flags)
33681dcaf65STejun Heo {
33781dcaf65STejun Heo 	return alloc_workqueue(name, WQ_UNBOUND | flags, 1);
33881dcaf65STejun Heo }
33981dcaf65STejun Heo 
34097e37d7bSTejun Heo #define create_workqueue(name)					\
3416370a6adSTejun Heo 	alloc_workqueue((name), WQ_MEM_RECLAIM, 1)
34258a69cb4STejun Heo #define create_freezable_workqueue(name)			\
34358a69cb4STejun Heo 	alloc_workqueue((name), WQ_FREEZABLE | WQ_UNBOUND | WQ_MEM_RECLAIM, 1)
34497e37d7bSTejun Heo #define create_singlethread_workqueue(name)			\
3456370a6adSTejun Heo 	alloc_workqueue((name), WQ_UNBOUND | WQ_MEM_RECLAIM, 1)
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq);
3481da177e4SLinus Torvalds 
349b3c97528SHarvey Harrison extern int queue_work(struct workqueue_struct *wq, struct work_struct *work);
350c1a220e7SZhang Rui extern int queue_work_on(int cpu, struct workqueue_struct *wq,
351c1a220e7SZhang Rui 			struct work_struct *work);
352b3c97528SHarvey Harrison extern int queue_delayed_work(struct workqueue_struct *wq,
353b3c97528SHarvey Harrison 			struct delayed_work *work, unsigned long delay);
3547a6bc1cdSVenkatesh Pallipadi extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
35552bad64dSDavid Howells 			struct delayed_work *work, unsigned long delay);
35628e53bddSOleg Nesterov 
357b3c97528SHarvey Harrison extern void flush_workqueue(struct workqueue_struct *wq);
3589c5a2ba7STejun Heo extern void drain_workqueue(struct workqueue_struct *wq);
35928e53bddSOleg Nesterov extern void flush_scheduled_work(void);
3601da177e4SLinus Torvalds 
361b3c97528SHarvey Harrison extern int schedule_work(struct work_struct *work);
362c1a220e7SZhang Rui extern int schedule_work_on(int cpu, struct work_struct *work);
363b3c97528SHarvey Harrison extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay);
36428e53bddSOleg Nesterov extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
36528e53bddSOleg Nesterov 					unsigned long delay);
36665f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func);
3671da177e4SLinus Torvalds extern int keventd_up(void);
3681da177e4SLinus Torvalds 
36965f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *);
3701da177e4SLinus Torvalds 
371401a8d04STejun Heo extern bool flush_work(struct work_struct *work);
37209383498STejun Heo extern bool flush_work_sync(struct work_struct *work);
373401a8d04STejun Heo extern bool cancel_work_sync(struct work_struct *work);
374401a8d04STejun Heo 
375401a8d04STejun Heo extern bool flush_delayed_work(struct delayed_work *dwork);
37609383498STejun Heo extern bool flush_delayed_work_sync(struct delayed_work *work);
377401a8d04STejun Heo extern bool cancel_delayed_work_sync(struct delayed_work *dwork);
37828e53bddSOleg Nesterov 
379dcd989cbSTejun Heo extern void workqueue_set_max_active(struct workqueue_struct *wq,
380dcd989cbSTejun Heo 				     int max_active);
381dcd989cbSTejun Heo extern bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq);
382dcd989cbSTejun Heo extern unsigned int work_cpu(struct work_struct *work);
383dcd989cbSTejun Heo extern unsigned int work_busy(struct work_struct *work);
384dcd989cbSTejun Heo 
3851da177e4SLinus Torvalds /*
3861da177e4SLinus Torvalds  * Kill off a pending schedule_delayed_work().  Note that the work callback
387071b6386SOleg Nesterov  * function may still be running on return from cancel_delayed_work(), unless
388071b6386SOleg Nesterov  * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or
38928e53bddSOleg Nesterov  * cancel_work_sync() to wait on it.
3901da177e4SLinus Torvalds  */
391401a8d04STejun Heo static inline bool cancel_delayed_work(struct delayed_work *work)
3921da177e4SLinus Torvalds {
393401a8d04STejun Heo 	bool ret;
3941da177e4SLinus Torvalds 
395223a10a9SOleg Nesterov 	ret = del_timer_sync(&work->timer);
3961da177e4SLinus Torvalds 	if (ret)
39723b2e599SOleg Nesterov 		work_clear_pending(&work->work);
3981da177e4SLinus Torvalds 	return ret;
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4014e49627bSOleg Nesterov /*
4024e49627bSOleg Nesterov  * Like above, but uses del_timer() instead of del_timer_sync(). This means,
4034e49627bSOleg Nesterov  * if it returns 0 the timer function may be running and the queueing is in
4044e49627bSOleg Nesterov  * progress.
4054e49627bSOleg Nesterov  */
406401a8d04STejun Heo static inline bool __cancel_delayed_work(struct delayed_work *work)
4074e49627bSOleg Nesterov {
408401a8d04STejun Heo 	bool ret;
4094e49627bSOleg Nesterov 
4104e49627bSOleg Nesterov 	ret = del_timer(&work->timer);
4114e49627bSOleg Nesterov 	if (ret)
4124e49627bSOleg Nesterov 		work_clear_pending(&work->work);
4134e49627bSOleg Nesterov 	return ret;
4144e49627bSOleg Nesterov }
4154e49627bSOleg Nesterov 
4162d3854a3SRusty Russell #ifndef CONFIG_SMP
4172d3854a3SRusty Russell static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
4182d3854a3SRusty Russell {
4192d3854a3SRusty Russell 	return fn(arg);
4202d3854a3SRusty Russell }
4212d3854a3SRusty Russell #else
4222d3854a3SRusty Russell long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg);
4232d3854a3SRusty Russell #endif /* CONFIG_SMP */
424a25909a4SPaul E. McKenney 
425a0a1a5fdSTejun Heo #ifdef CONFIG_FREEZER
426a0a1a5fdSTejun Heo extern void freeze_workqueues_begin(void);
427a0a1a5fdSTejun Heo extern bool freeze_workqueues_busy(void);
428a0a1a5fdSTejun Heo extern void thaw_workqueues(void);
429a0a1a5fdSTejun Heo #endif /* CONFIG_FREEZER */
430a0a1a5fdSTejun Heo 
4311da177e4SLinus Torvalds #endif
432