workqueue.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * workqueue.h --- work queue handling for Linux.
  3. */
  4. #ifndef _LINUX_WORKQUEUE_H
  5. #define _LINUX_WORKQUEUE_H
  6. #include <linux/timer.h>
  7. #include <linux/linkage.h>
  8. #include <linux/bitops.h>
  9. #include <linux/lockdep.h>
  10. #include <linux/threads.h>
  11. #include <asm/atomic.h>
  12. struct workqueue_struct;
  13. struct work_struct;
  14. typedef void (*work_func_t)(struct work_struct *work);
  15. /*
  16. * The first word is the work queue pointer and the flags rolled into
  17. * one
  18. */
  19. #define work_data_bits(work) ((unsigned long *)(&(work)->data))
  20. enum {
  21. WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */
  22. WORK_STRUCT_LINKED_BIT = 1, /* next work is linked to this one */
  23. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  24. WORK_STRUCT_STATIC_BIT = 2, /* static initializer (debugobjects) */
  25. WORK_STRUCT_COLOR_SHIFT = 3, /* color for workqueue flushing */
  26. #else
  27. WORK_STRUCT_COLOR_SHIFT = 2, /* color for workqueue flushing */
  28. #endif
  29. WORK_STRUCT_COLOR_BITS = 4,
  30. WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT,
  31. WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT,
  32. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  33. WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT,
  34. #else
  35. WORK_STRUCT_STATIC = 0,
  36. #endif
  37. /*
  38. * The last color is no color used for works which don't
  39. * participate in workqueue flushing.
  40. */
  41. WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS) - 1,
  42. WORK_NO_COLOR = WORK_NR_COLORS,
  43. /*
  44. * Reserve 6 bits off of cwq pointer w/ debugobjects turned
  45. * off. This makes cwqs aligned to 64 bytes which isn't too
  46. * excessive while allowing 15 workqueue flush colors.
  47. */
  48. WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT +
  49. WORK_STRUCT_COLOR_BITS,
  50. WORK_STRUCT_FLAG_MASK = (1UL << WORK_STRUCT_FLAG_BITS) - 1,
  51. WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
  52. WORK_STRUCT_NO_CPU = NR_CPUS << WORK_STRUCT_FLAG_BITS,
  53. };
  54. struct work_struct {
  55. atomic_long_t data;
  56. struct list_head entry;
  57. work_func_t func;
  58. #ifdef CONFIG_LOCKDEP
  59. struct lockdep_map lockdep_map;
  60. #endif
  61. };
  62. #define WORK_DATA_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU)
  63. #define WORK_DATA_STATIC_INIT() \
  64. ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU | WORK_STRUCT_STATIC)
  65. struct delayed_work {
  66. struct work_struct work;
  67. struct timer_list timer;
  68. };
  69. static inline struct delayed_work *to_delayed_work(struct work_struct *work)
  70. {
  71. return container_of(work, struct delayed_work, work);
  72. }
  73. struct execute_work {
  74. struct work_struct work;
  75. };
  76. #ifdef CONFIG_LOCKDEP
  77. /*
  78. * NB: because we have to copy the lockdep_map, setting _key
  79. * here is required, otherwise it could get initialised to the
  80. * copy of the lockdep_map!
  81. */
  82. #define __WORK_INIT_LOCKDEP_MAP(n, k) \
  83. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
  84. #else
  85. #define __WORK_INIT_LOCKDEP_MAP(n, k)
  86. #endif
  87. #define __WORK_INITIALIZER(n, f) { \
  88. .data = WORK_DATA_STATIC_INIT(), \
  89. .entry = { &(n).entry, &(n).entry }, \
  90. .func = (f), \
  91. __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \
  92. }
  93. #define __DELAYED_WORK_INITIALIZER(n, f) { \
  94. .work = __WORK_INITIALIZER((n).work, (f)), \
  95. .timer = TIMER_INITIALIZER(NULL, 0, 0), \
  96. }
  97. #define DECLARE_WORK(n, f) \
  98. struct work_struct n = __WORK_INITIALIZER(n, f)
  99. #define DECLARE_DELAYED_WORK(n, f) \
  100. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
  101. /*
  102. * initialize a work item's function pointer
  103. */
  104. #define PREPARE_WORK(_work, _func) \
  105. do { \
  106. (_work)->func = (_func); \
  107. } while (0)
  108. #define PREPARE_DELAYED_WORK(_work, _func) \
  109. PREPARE_WORK(&(_work)->work, (_func))
  110. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  111. extern void __init_work(struct work_struct *work, int onstack);
  112. extern void destroy_work_on_stack(struct work_struct *work);
  113. static inline unsigned int work_static(struct work_struct *work)
  114. {
  115. return *work_data_bits(work) & WORK_STRUCT_STATIC;
  116. }
  117. #else
  118. static inline void __init_work(struct work_struct *work, int onstack) { }
  119. static inline void destroy_work_on_stack(struct work_struct *work) { }
  120. static inline unsigned int work_static(struct work_struct *work) { return 0; }
  121. #endif
  122. /*
  123. * initialize all of a work item in one go
  124. *
  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.
  128. */
  129. #ifdef CONFIG_LOCKDEP
  130. #define __INIT_WORK(_work, _func, _onstack) \
  131. do { \
  132. static struct lock_class_key __key; \
  133. \
  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)); \
  139. } while (0)
  140. #else
  141. #define __INIT_WORK(_work, _func, _onstack) \
  142. do { \
  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)); \
  147. } while (0)
  148. #endif
  149. #define INIT_WORK(_work, _func) \
  150. do { \
  151. __INIT_WORK((_work), (_func), 0); \
  152. } while (0)
  153. #define INIT_WORK_ON_STACK(_work, _func) \
  154. do { \
  155. __INIT_WORK((_work), (_func), 1); \
  156. } while (0)
  157. #define INIT_DELAYED_WORK(_work, _func) \
  158. do { \
  159. INIT_WORK(&(_work)->work, (_func)); \
  160. init_timer(&(_work)->timer); \
  161. } while (0)
  162. #define INIT_DELAYED_WORK_ON_STACK(_work, _func) \
  163. do { \
  164. INIT_WORK_ON_STACK(&(_work)->work, (_func)); \
  165. init_timer_on_stack(&(_work)->timer); \
  166. } while (0)
  167. #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func) \
  168. do { \
  169. INIT_WORK(&(_work)->work, (_func)); \
  170. init_timer_deferrable(&(_work)->timer); \
  171. } while (0)
  172. /**
  173. * work_pending - Find out whether a work item is currently pending
  174. * @work: The work item in question
  175. */
  176. #define work_pending(work) \
  177. test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
  178. /**
  179. * delayed_work_pending - Find out whether a delayable work item is currently
  180. * pending
  181. * @work: The work item in question
  182. */
  183. #define delayed_work_pending(w) \
  184. work_pending(&(w)->work)
  185. /**
  186. * work_clear_pending - for internal use only, mark a work item as not pending
  187. * @work: The work item in question
  188. */
  189. #define work_clear_pending(work) \
  190. clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
  191. enum {
  192. WQ_FREEZEABLE = 1 << 0, /* freeze during suspend */
  193. WQ_SINGLE_CPU = 1 << 1, /* only single cpu at a time */
  194. WQ_NON_REENTRANT = 1 << 2, /* guarantee non-reentrance */
  195. WQ_RESCUER = 1 << 3, /* has an rescue worker */
  196. WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */
  197. WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2,
  198. };
  199. extern struct workqueue_struct *
  200. __create_workqueue_key(const char *name, unsigned int flags, int max_active,
  201. struct lock_class_key *key, const char *lock_name);
  202. #ifdef CONFIG_LOCKDEP
  203. #define __create_workqueue(name, flags, max_active) \
  204. ({ \
  205. static struct lock_class_key __key; \
  206. const char *__lock_name; \
  207. \
  208. if (__builtin_constant_p(name)) \
  209. __lock_name = (name); \
  210. else \
  211. __lock_name = #name; \
  212. \
  213. __create_workqueue_key((name), (flags), (max_active), \
  214. &__key, __lock_name); \
  215. })
  216. #else
  217. #define __create_workqueue(name, flags, max_active) \
  218. __create_workqueue_key((name), (flags), (max_active), NULL, NULL)
  219. #endif
  220. #define create_workqueue(name) \
  221. __create_workqueue((name), WQ_RESCUER, 1)
  222. #define create_freezeable_workqueue(name) \
  223. __create_workqueue((name), \
  224. WQ_FREEZEABLE | WQ_SINGLE_CPU | WQ_RESCUER, 1)
  225. #define create_singlethread_workqueue(name) \
  226. __create_workqueue((name), WQ_SINGLE_CPU | WQ_RESCUER, 1)
  227. extern void destroy_workqueue(struct workqueue_struct *wq);
  228. extern int queue_work(struct workqueue_struct *wq, struct work_struct *work);
  229. extern int queue_work_on(int cpu, struct workqueue_struct *wq,
  230. struct work_struct *work);
  231. extern int queue_delayed_work(struct workqueue_struct *wq,
  232. struct delayed_work *work, unsigned long delay);
  233. extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  234. struct delayed_work *work, unsigned long delay);
  235. extern void flush_workqueue(struct workqueue_struct *wq);
  236. extern void flush_scheduled_work(void);
  237. extern void flush_delayed_work(struct delayed_work *work);
  238. extern int schedule_work(struct work_struct *work);
  239. extern int schedule_work_on(int cpu, struct work_struct *work);
  240. extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay);
  241. extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
  242. unsigned long delay);
  243. extern int schedule_on_each_cpu(work_func_t func);
  244. extern int keventd_up(void);
  245. extern void init_workqueues(void);
  246. int execute_in_process_context(work_func_t fn, struct execute_work *);
  247. extern int flush_work(struct work_struct *work);
  248. extern int cancel_work_sync(struct work_struct *work);
  249. /*
  250. * Kill off a pending schedule_delayed_work(). Note that the work callback
  251. * function may still be running on return from cancel_delayed_work(), unless
  252. * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or
  253. * cancel_work_sync() to wait on it.
  254. */
  255. static inline int cancel_delayed_work(struct delayed_work *work)
  256. {
  257. int ret;
  258. ret = del_timer_sync(&work->timer);
  259. if (ret)
  260. work_clear_pending(&work->work);
  261. return ret;
  262. }
  263. /*
  264. * Like above, but uses del_timer() instead of del_timer_sync(). This means,
  265. * if it returns 0 the timer function may be running and the queueing is in
  266. * progress.
  267. */
  268. static inline int __cancel_delayed_work(struct delayed_work *work)
  269. {
  270. int ret;
  271. ret = del_timer(&work->timer);
  272. if (ret)
  273. work_clear_pending(&work->work);
  274. return ret;
  275. }
  276. extern int cancel_delayed_work_sync(struct delayed_work *work);
  277. /* Obsolete. use cancel_delayed_work_sync() */
  278. static inline
  279. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  280. struct delayed_work *work)
  281. {
  282. cancel_delayed_work_sync(work);
  283. }
  284. /* Obsolete. use cancel_delayed_work_sync() */
  285. static inline
  286. void cancel_rearming_delayed_work(struct delayed_work *work)
  287. {
  288. cancel_delayed_work_sync(work);
  289. }
  290. #ifndef CONFIG_SMP
  291. static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  292. {
  293. return fn(arg);
  294. }
  295. #else
  296. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg);
  297. #endif /* CONFIG_SMP */
  298. #ifdef CONFIG_FREEZER
  299. extern void freeze_workqueues_begin(void);
  300. extern bool freeze_workqueues_busy(void);
  301. extern void thaw_workqueues(void);
  302. #endif /* CONFIG_FREEZER */
  303. #endif