xref: /openbmc/linux/include/linux/workqueue.h (revision c1a220e7)
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>
12a08727baSLinus Torvalds #include <asm/atomic.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds struct workqueue_struct;
151da177e4SLinus Torvalds 
1665f27f38SDavid Howells struct work_struct;
1765f27f38SDavid Howells typedef void (*work_func_t)(struct work_struct *work);
186bb49e59SDavid Howells 
19a08727baSLinus Torvalds /*
20a08727baSLinus Torvalds  * The first word is the work queue pointer and the flags rolled into
21a08727baSLinus Torvalds  * one
22a08727baSLinus Torvalds  */
23a08727baSLinus Torvalds #define work_data_bits(work) ((unsigned long *)(&(work)->data))
24a08727baSLinus Torvalds 
251da177e4SLinus Torvalds struct work_struct {
26a08727baSLinus Torvalds 	atomic_long_t data;
27365970a1SDavid Howells #define WORK_STRUCT_PENDING 0		/* T if work item pending execution */
28365970a1SDavid Howells #define WORK_STRUCT_FLAG_MASK (3UL)
29365970a1SDavid Howells #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)
301da177e4SLinus Torvalds 	struct list_head entry;
316bb49e59SDavid Howells 	work_func_t func;
324e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
334e6045f1SJohannes Berg 	struct lockdep_map lockdep_map;
344e6045f1SJohannes Berg #endif
3552bad64dSDavid Howells };
3652bad64dSDavid Howells 
3723b2e599SOleg Nesterov #define WORK_DATA_INIT()	ATOMIC_LONG_INIT(0)
38a08727baSLinus Torvalds 
3952bad64dSDavid Howells struct delayed_work {
4052bad64dSDavid Howells 	struct work_struct work;
411da177e4SLinus Torvalds 	struct timer_list timer;
421da177e4SLinus Torvalds };
431da177e4SLinus Torvalds 
441fa44ecaSJames Bottomley struct execute_work {
451fa44ecaSJames Bottomley 	struct work_struct work;
461fa44ecaSJames Bottomley };
471fa44ecaSJames Bottomley 
484e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
494e6045f1SJohannes Berg /*
504e6045f1SJohannes Berg  * NB: because we have to copy the lockdep_map, setting _key
514e6045f1SJohannes Berg  * here is required, otherwise it could get initialised to the
524e6045f1SJohannes Berg  * copy of the lockdep_map!
534e6045f1SJohannes Berg  */
544e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k) \
554e6045f1SJohannes Berg 	.lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
564e6045f1SJohannes Berg #else
574e6045f1SJohannes Berg #define __WORK_INIT_LOCKDEP_MAP(n, k)
584e6045f1SJohannes Berg #endif
594e6045f1SJohannes Berg 
6065f27f38SDavid Howells #define __WORK_INITIALIZER(n, f) {				\
6123b2e599SOleg Nesterov 	.data = WORK_DATA_INIT(),				\
6265f27f38SDavid Howells 	.entry	= { &(n).entry, &(n).entry },			\
6365f27f38SDavid Howells 	.func = (f),						\
644e6045f1SJohannes Berg 	__WORK_INIT_LOCKDEP_MAP(#n, &(n))			\
6565f27f38SDavid Howells 	}
6665f27f38SDavid Howells 
6765f27f38SDavid Howells #define __DELAYED_WORK_INITIALIZER(n, f) {			\
6865f27f38SDavid Howells 	.work = __WORK_INITIALIZER((n).work, (f)),		\
691da177e4SLinus Torvalds 	.timer = TIMER_INITIALIZER(NULL, 0, 0),			\
701da177e4SLinus Torvalds 	}
711da177e4SLinus Torvalds 
7265f27f38SDavid Howells #define DECLARE_WORK(n, f)					\
7365f27f38SDavid Howells 	struct work_struct n = __WORK_INITIALIZER(n, f)
7465f27f38SDavid Howells 
7565f27f38SDavid Howells #define DECLARE_DELAYED_WORK(n, f)				\
7665f27f38SDavid Howells 	struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
7765f27f38SDavid Howells 
781da177e4SLinus Torvalds /*
7965f27f38SDavid Howells  * initialize a work item's function pointer
801da177e4SLinus Torvalds  */
8165f27f38SDavid Howells #define PREPARE_WORK(_work, _func)				\
821da177e4SLinus Torvalds 	do {							\
8352bad64dSDavid Howells 		(_work)->func = (_func);			\
841da177e4SLinus Torvalds 	} while (0)
851da177e4SLinus Torvalds 
8665f27f38SDavid Howells #define PREPARE_DELAYED_WORK(_work, _func)			\
8765f27f38SDavid Howells 	PREPARE_WORK(&(_work)->work, (_func))
8852bad64dSDavid Howells 
891da177e4SLinus Torvalds /*
9052bad64dSDavid Howells  * initialize all of a work item in one go
91a08727baSLinus Torvalds  *
92a08727baSLinus Torvalds  * NOTE! No point in using "atomic_long_set()": useing a direct
93a08727baSLinus Torvalds  * assignment of the work data initializer allows the compiler
94a08727baSLinus Torvalds  * to generate better code.
951da177e4SLinus Torvalds  */
964e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
974e6045f1SJohannes Berg #define INIT_WORK(_work, _func)						\
984e6045f1SJohannes Berg 	do {								\
994e6045f1SJohannes Berg 		static struct lock_class_key __key;			\
1004e6045f1SJohannes Berg 									\
1014e6045f1SJohannes Berg 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
1024e6045f1SJohannes Berg 		lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0);\
1034e6045f1SJohannes Berg 		INIT_LIST_HEAD(&(_work)->entry);			\
1044e6045f1SJohannes Berg 		PREPARE_WORK((_work), (_func));				\
1054e6045f1SJohannes Berg 	} while (0)
1064e6045f1SJohannes Berg #else
10765f27f38SDavid Howells #define INIT_WORK(_work, _func)						\
1081da177e4SLinus Torvalds 	do {								\
10923b2e599SOleg Nesterov 		(_work)->data = (atomic_long_t) WORK_DATA_INIT();	\
11065f27f38SDavid Howells 		INIT_LIST_HEAD(&(_work)->entry);			\
11165f27f38SDavid Howells 		PREPARE_WORK((_work), (_func));				\
11265f27f38SDavid Howells 	} while (0)
1134e6045f1SJohannes Berg #endif
11465f27f38SDavid Howells 
11565f27f38SDavid Howells #define INIT_DELAYED_WORK(_work, _func)				\
11665f27f38SDavid Howells 	do {							\
11765f27f38SDavid Howells 		INIT_WORK(&(_work)->work, (_func));		\
11865f27f38SDavid Howells 		init_timer(&(_work)->timer);			\
11965f27f38SDavid Howells 	} while (0)
12065f27f38SDavid Howells 
12128287033SVenki Pallipadi #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func)			\
12228287033SVenki Pallipadi 	do {							\
12328287033SVenki Pallipadi 		INIT_WORK(&(_work)->work, (_func));		\
12428287033SVenki Pallipadi 		init_timer_deferrable(&(_work)->timer);		\
12528287033SVenki Pallipadi 	} while (0)
12628287033SVenki Pallipadi 
127365970a1SDavid Howells /**
128365970a1SDavid Howells  * work_pending - Find out whether a work item is currently pending
129365970a1SDavid Howells  * @work: The work item in question
130365970a1SDavid Howells  */
131365970a1SDavid Howells #define work_pending(work) \
132a08727baSLinus Torvalds 	test_bit(WORK_STRUCT_PENDING, work_data_bits(work))
133365970a1SDavid Howells 
134365970a1SDavid Howells /**
135365970a1SDavid Howells  * delayed_work_pending - Find out whether a delayable work item is currently
136365970a1SDavid Howells  * pending
137365970a1SDavid Howells  * @work: The work item in question
138365970a1SDavid Howells  */
1390221872aSLinus Torvalds #define delayed_work_pending(w) \
1400221872aSLinus Torvalds 	work_pending(&(w)->work)
141365970a1SDavid Howells 
14265f27f38SDavid Howells /**
14323b2e599SOleg Nesterov  * work_clear_pending - for internal use only, mark a work item as not pending
14423b2e599SOleg Nesterov  * @work: The work item in question
14565f27f38SDavid Howells  */
14623b2e599SOleg Nesterov #define work_clear_pending(work) \
147a08727baSLinus Torvalds 	clear_bit(WORK_STRUCT_PENDING, work_data_bits(work))
14865f27f38SDavid Howells 
14952bad64dSDavid Howells 
1504e6045f1SJohannes Berg extern struct workqueue_struct *
1514e6045f1SJohannes Berg __create_workqueue_key(const char *name, int singlethread,
152eb13ba87SJohannes Berg 		       int freezeable, struct lock_class_key *key,
153eb13ba87SJohannes Berg 		       const char *lock_name);
1544e6045f1SJohannes Berg 
1554e6045f1SJohannes Berg #ifdef CONFIG_LOCKDEP
1564e6045f1SJohannes Berg #define __create_workqueue(name, singlethread, freezeable)	\
1574e6045f1SJohannes Berg ({								\
1584e6045f1SJohannes Berg 	static struct lock_class_key __key;			\
159eb13ba87SJohannes Berg 	const char *__lock_name;				\
160eb13ba87SJohannes Berg 								\
161eb13ba87SJohannes Berg 	if (__builtin_constant_p(name))				\
162eb13ba87SJohannes Berg 		__lock_name = (name);				\
163eb13ba87SJohannes Berg 	else							\
164eb13ba87SJohannes Berg 		__lock_name = #name;				\
1654e6045f1SJohannes Berg 								\
1664e6045f1SJohannes Berg 	__create_workqueue_key((name), (singlethread),		\
167eb13ba87SJohannes Berg 			       (freezeable), &__key,		\
168eb13ba87SJohannes Berg 			       __lock_name);			\
1694e6045f1SJohannes Berg })
1704e6045f1SJohannes Berg #else
1714e6045f1SJohannes Berg #define __create_workqueue(name, singlethread, freezeable)	\
172eb13ba87SJohannes Berg 	__create_workqueue_key((name), (singlethread), (freezeable), NULL, NULL)
1734e6045f1SJohannes Berg #endif
1744e6045f1SJohannes Berg 
175341a5958SRafael J. Wysocki #define create_workqueue(name) __create_workqueue((name), 0, 0)
176e3dfd296SOleg Nesterov #define create_freezeable_workqueue(name) __create_workqueue((name), 1, 1)
177341a5958SRafael J. Wysocki #define create_singlethread_workqueue(name) __create_workqueue((name), 1, 0)
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds extern void destroy_workqueue(struct workqueue_struct *wq);
1801da177e4SLinus Torvalds 
181b3c97528SHarvey Harrison extern int queue_work(struct workqueue_struct *wq, struct work_struct *work);
182c1a220e7SZhang Rui extern int queue_work_on(int cpu, struct workqueue_struct *wq,
183c1a220e7SZhang Rui 			struct work_struct *work);
184b3c97528SHarvey Harrison extern int queue_delayed_work(struct workqueue_struct *wq,
185b3c97528SHarvey Harrison 			struct delayed_work *work, unsigned long delay);
1867a6bc1cdSVenkatesh Pallipadi extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
18752bad64dSDavid Howells 			struct delayed_work *work, unsigned long delay);
18828e53bddSOleg Nesterov 
189b3c97528SHarvey Harrison extern void flush_workqueue(struct workqueue_struct *wq);
19028e53bddSOleg Nesterov extern void flush_scheduled_work(void);
1911da177e4SLinus Torvalds 
192b3c97528SHarvey Harrison extern int schedule_work(struct work_struct *work);
193c1a220e7SZhang Rui extern int schedule_work_on(int cpu, struct work_struct *work);
194b3c97528SHarvey Harrison extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay);
19528e53bddSOleg Nesterov extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
19628e53bddSOleg Nesterov 					unsigned long delay);
19765f27f38SDavid Howells extern int schedule_on_each_cpu(work_func_t func);
1981da177e4SLinus Torvalds extern int current_is_keventd(void);
1991da177e4SLinus Torvalds extern int keventd_up(void);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds extern void init_workqueues(void);
20265f27f38SDavid Howells int execute_in_process_context(work_func_t fn, struct execute_work *);
2031da177e4SLinus Torvalds 
2041f1f642eSOleg Nesterov extern int cancel_work_sync(struct work_struct *work);
20528e53bddSOleg Nesterov 
2061da177e4SLinus Torvalds /*
2071da177e4SLinus Torvalds  * Kill off a pending schedule_delayed_work().  Note that the work callback
208071b6386SOleg Nesterov  * function may still be running on return from cancel_delayed_work(), unless
209071b6386SOleg Nesterov  * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or
21028e53bddSOleg Nesterov  * cancel_work_sync() to wait on it.
2111da177e4SLinus Torvalds  */
21252bad64dSDavid Howells static inline int cancel_delayed_work(struct delayed_work *work)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	int ret;
2151da177e4SLinus Torvalds 
216223a10a9SOleg Nesterov 	ret = del_timer_sync(&work->timer);
2171da177e4SLinus Torvalds 	if (ret)
21823b2e599SOleg Nesterov 		work_clear_pending(&work->work);
2191da177e4SLinus Torvalds 	return ret;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221f1f642eSOleg Nesterov extern int cancel_delayed_work_sync(struct delayed_work *work);
2231634c48fSOleg Nesterov 
224f5a421a4SOleg Nesterov /* Obsolete. use cancel_delayed_work_sync() */
2251634c48fSOleg Nesterov static inline
2261634c48fSOleg Nesterov void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
2271634c48fSOleg Nesterov 					struct delayed_work *work)
2281634c48fSOleg Nesterov {
229f5a421a4SOleg Nesterov 	cancel_delayed_work_sync(work);
230f5a421a4SOleg Nesterov }
231f5a421a4SOleg Nesterov 
232f5a421a4SOleg Nesterov /* Obsolete. use cancel_delayed_work_sync() */
233f5a421a4SOleg Nesterov static inline
234f5a421a4SOleg Nesterov void cancel_rearming_delayed_work(struct delayed_work *work)
235f5a421a4SOleg Nesterov {
236f5a421a4SOleg Nesterov 	cancel_delayed_work_sync(work);
2371634c48fSOleg Nesterov }
2381634c48fSOleg Nesterov 
2391da177e4SLinus Torvalds #endif
240