workqueue.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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_CWQ_BIT = 1, /* data points to cwq */
  23. WORK_STRUCT_LINKED_BIT = 2, /* next work is linked to this one */
  24. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  25. WORK_STRUCT_STATIC_BIT = 3, /* static initializer (debugobjects) */
  26. WORK_STRUCT_COLOR_SHIFT = 4, /* color for workqueue flushing */
  27. #else
  28. WORK_STRUCT_COLOR_SHIFT = 3, /* color for workqueue flushing */
  29. #endif
  30. WORK_STRUCT_COLOR_BITS = 4,
  31. WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT,
  32. WORK_STRUCT_CWQ = 1 << WORK_STRUCT_CWQ_BIT,
  33. WORK_STRUCT_LINKED = 1 << WORK_STRUCT_LINKED_BIT,
  34. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  35. WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT,
  36. #else
  37. WORK_STRUCT_STATIC = 0,
  38. #endif
  39. /*
  40. * The last color is no color used for works which don't
  41. * participate in workqueue flushing.
  42. */
  43. WORK_NR_COLORS = (1 << WORK_STRUCT_COLOR_BITS) - 1,
  44. WORK_NO_COLOR = WORK_NR_COLORS,
  45. /* special cpu IDs */
  46. WORK_CPU_UNBOUND = NR_CPUS,
  47. WORK_CPU_NONE = NR_CPUS + 1,
  48. WORK_CPU_LAST = WORK_CPU_NONE,
  49. /*
  50. * Reserve 7 bits off of cwq pointer w/ debugobjects turned
  51. * off. This makes cwqs aligned to 128 bytes which isn't too
  52. * excessive while allowing 15 workqueue flush colors.
  53. */
  54. WORK_STRUCT_FLAG_BITS = WORK_STRUCT_COLOR_SHIFT +
  55. WORK_STRUCT_COLOR_BITS,
  56. WORK_STRUCT_FLAG_MASK = (1UL << WORK_STRUCT_FLAG_BITS) - 1,
  57. WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
  58. WORK_STRUCT_NO_CPU = WORK_CPU_NONE << WORK_STRUCT_FLAG_BITS,
  59. /* bit mask for work_busy() return values */
  60. WORK_BUSY_PENDING = 1 << 0,
  61. WORK_BUSY_RUNNING = 1 << 1,
  62. };
  63. struct work_struct {
  64. atomic_long_t data;
  65. struct list_head entry;
  66. work_func_t func;
  67. #ifdef CONFIG_LOCKDEP
  68. struct lockdep_map lockdep_map;
  69. #endif
  70. };
  71. #define WORK_DATA_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU)
  72. #define WORK_DATA_STATIC_INIT() \
  73. ATOMIC_LONG_INIT(WORK_STRUCT_NO_CPU | WORK_STRUCT_STATIC)
  74. struct delayed_work {
  75. struct work_struct work;
  76. struct timer_list timer;
  77. };
  78. static inline struct delayed_work *to_delayed_work(struct work_struct *work)
  79. {
  80. return container_of(work, struct delayed_work, work);
  81. }
  82. struct execute_work {
  83. struct work_struct work;
  84. };
  85. #ifdef CONFIG_LOCKDEP
  86. /*
  87. * NB: because we have to copy the lockdep_map, setting _key
  88. * here is required, otherwise it could get initialised to the
  89. * copy of the lockdep_map!
  90. */
  91. #define __WORK_INIT_LOCKDEP_MAP(n, k) \
  92. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
  93. #else
  94. #define __WORK_INIT_LOCKDEP_MAP(n, k)
  95. #endif
  96. #define __WORK_INITIALIZER(n, f) { \
  97. .data = WORK_DATA_STATIC_INIT(), \
  98. .entry = { &(n).entry, &(n).entry }, \
  99. .func = (f), \
  100. __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \
  101. }
  102. #define __DELAYED_WORK_INITIALIZER(n, f) { \
  103. .work = __WORK_INITIALIZER((n).work, (f)), \
  104. .timer = TIMER_INITIALIZER(NULL, 0, 0), \
  105. }
  106. #define DECLARE_WORK(n, f) \
  107. struct work_struct n = __WORK_INITIALIZER(n, f)
  108. #define DECLARE_DELAYED_WORK(n, f) \
  109. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
  110. /*
  111. * initialize a work item's function pointer
  112. */
  113. #define PREPARE_WORK(_work, _func) \
  114. do { \
  115. (_work)->func = (_func); \
  116. } while (0)
  117. #define PREPARE_DELAYED_WORK(_work, _func) \
  118. PREPARE_WORK(&(_work)->work, (_func))
  119. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  120. extern void __init_work(struct work_struct *work, int onstack);
  121. extern void destroy_work_on_stack(struct work_struct *work);
  122. static inline unsigned int work_static(struct work_struct *work)
  123. {
  124. return *work_data_bits(work) & WORK_STRUCT_STATIC;
  125. }
  126. #else
  127. static inline void __init_work(struct work_struct *work, int onstack) { }
  128. static inline void destroy_work_on_stack(struct work_struct *work) { }
  129. static inline unsigned int work_static(struct work_struct *work) { return 0; }
  130. #endif
  131. /*
  132. * initialize all of a work item in one go
  133. *
  134. * NOTE! No point in using "atomic_long_set()": using a direct
  135. * assignment of the work data initializer allows the compiler
  136. * to generate better code.
  137. */
  138. #ifdef CONFIG_LOCKDEP
  139. #define __INIT_WORK(_work, _func, _onstack) \
  140. do { \
  141. static struct lock_class_key __key; \
  142. \
  143. __init_work((_work), _onstack); \
  144. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  145. lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0);\
  146. INIT_LIST_HEAD(&(_work)->entry); \
  147. PREPARE_WORK((_work), (_func)); \
  148. } while (0)
  149. #else
  150. #define __INIT_WORK(_work, _func, _onstack) \
  151. do { \
  152. __init_work((_work), _onstack); \
  153. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  154. INIT_LIST_HEAD(&(_work)->entry); \
  155. PREPARE_WORK((_work), (_func)); \
  156. } while (0)
  157. #endif
  158. #define INIT_WORK(_work, _func) \
  159. do { \
  160. __INIT_WORK((_work), (_func), 0); \
  161. } while (0)
  162. #define INIT_WORK_ON_STACK(_work, _func) \
  163. do { \
  164. __INIT_WORK((_work), (_func), 1); \
  165. } while (0)
  166. #define INIT_DELAYED_WORK(_work, _func) \
  167. do { \
  168. INIT_WORK(&(_work)->work, (_func)); \
  169. init_timer(&(_work)->timer); \
  170. } while (0)
  171. #define INIT_DELAYED_WORK_ON_STACK(_work, _func) \
  172. do { \
  173. INIT_WORK_ON_STACK(&(_work)->work, (_func)); \
  174. init_timer_on_stack(&(_work)->timer); \
  175. } while (0)
  176. #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func) \
  177. do { \
  178. INIT_WORK(&(_work)->work, (_func)); \
  179. init_timer_deferrable(&(_work)->timer); \
  180. } while (0)
  181. /**
  182. * work_pending - Find out whether a work item is currently pending
  183. * @work: The work item in question
  184. */
  185. #define work_pending(work) \
  186. test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
  187. /**
  188. * delayed_work_pending - Find out whether a delayable work item is currently
  189. * pending
  190. * @work: The work item in question
  191. */
  192. #define delayed_work_pending(w) \
  193. work_pending(&(w)->work)
  194. /**
  195. * work_clear_pending - for internal use only, mark a work item as not pending
  196. * @work: The work item in question
  197. */
  198. #define work_clear_pending(work) \
  199. clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
  200. enum {
  201. WQ_NON_REENTRANT = 1 << 0, /* guarantee non-reentrance */
  202. WQ_UNBOUND = 1 << 1, /* not bound to any cpu */
  203. WQ_FREEZEABLE = 1 << 2, /* freeze during suspend */
  204. WQ_RESCUER = 1 << 3, /* has an rescue worker */
  205. WQ_HIGHPRI = 1 << 4, /* high priority */
  206. WQ_CPU_INTENSIVE = 1 << 5, /* cpu instensive workqueue */
  207. WQ_DYING = 1 << 6, /* internal: workqueue is dying */
  208. WQ_MAX_ACTIVE = 512, /* I like 512, better ideas? */
  209. WQ_MAX_UNBOUND_PER_CPU = 4, /* 4 * #cpus for unbound wq */
  210. WQ_DFL_ACTIVE = WQ_MAX_ACTIVE / 2,
  211. };
  212. /* unbound wq's aren't per-cpu, scale max_active according to #cpus */
  213. #define WQ_UNBOUND_MAX_ACTIVE \
  214. max_t(int, WQ_MAX_ACTIVE, num_possible_cpus() * WQ_MAX_UNBOUND_PER_CPU)
  215. /*
  216. * System-wide workqueues which are always present.
  217. *
  218. * system_wq is the one used by schedule[_delayed]_work[_on]().
  219. * Multi-CPU multi-threaded. There are users which expect relatively
  220. * short queue flush time. Don't queue works which can run for too
  221. * long.
  222. *
  223. * system_long_wq is similar to system_wq but may host long running
  224. * works. Queue flushing might take relatively long.
  225. *
  226. * system_nrt_wq is non-reentrant and guarantees that any given work
  227. * item is never executed in parallel by multiple CPUs. Queue
  228. * flushing might take relatively long.
  229. *
  230. * system_unbound_wq is unbound workqueue. Workers are not bound to
  231. * any specific CPU, not concurrency managed, and all queued works are
  232. * executed immediately as long as max_active limit is not reached and
  233. * resources are available.
  234. */
  235. extern struct workqueue_struct *system_wq;
  236. extern struct workqueue_struct *system_long_wq;
  237. extern struct workqueue_struct *system_nrt_wq;
  238. extern struct workqueue_struct *system_unbound_wq;
  239. extern struct workqueue_struct *
  240. __alloc_workqueue_key(const char *name, unsigned int flags, int max_active,
  241. struct lock_class_key *key, const char *lock_name);
  242. #ifdef CONFIG_LOCKDEP
  243. #define alloc_workqueue(name, flags, max_active) \
  244. ({ \
  245. static struct lock_class_key __key; \
  246. const char *__lock_name; \
  247. \
  248. if (__builtin_constant_p(name)) \
  249. __lock_name = (name); \
  250. else \
  251. __lock_name = #name; \
  252. \
  253. __alloc_workqueue_key((name), (flags), (max_active), \
  254. &__key, __lock_name); \
  255. })
  256. #else
  257. #define alloc_workqueue(name, flags, max_active) \
  258. __alloc_workqueue_key((name), (flags), (max_active), NULL, NULL)
  259. #endif
  260. #define create_workqueue(name) \
  261. alloc_workqueue((name), WQ_RESCUER, 1)
  262. #define create_freezeable_workqueue(name) \
  263. alloc_workqueue((name), WQ_FREEZEABLE | WQ_UNBOUND | WQ_RESCUER, 1)
  264. #define create_singlethread_workqueue(name) \
  265. alloc_workqueue((name), WQ_UNBOUND | WQ_RESCUER, 1)
  266. extern void destroy_workqueue(struct workqueue_struct *wq);
  267. extern int queue_work(struct workqueue_struct *wq, struct work_struct *work);
  268. extern int queue_work_on(int cpu, struct workqueue_struct *wq,
  269. struct work_struct *work);
  270. extern int queue_delayed_work(struct workqueue_struct *wq,
  271. struct delayed_work *work, unsigned long delay);
  272. extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  273. struct delayed_work *work, unsigned long delay);
  274. extern void flush_workqueue(struct workqueue_struct *wq);
  275. extern void flush_scheduled_work(void);
  276. extern void flush_delayed_work(struct delayed_work *work);
  277. extern int schedule_work(struct work_struct *work);
  278. extern int schedule_work_on(int cpu, struct work_struct *work);
  279. extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay);
  280. extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
  281. unsigned long delay);
  282. extern int schedule_on_each_cpu(work_func_t func);
  283. extern int keventd_up(void);
  284. int execute_in_process_context(work_func_t fn, struct execute_work *);
  285. extern int flush_work(struct work_struct *work);
  286. extern int cancel_work_sync(struct work_struct *work);
  287. extern void workqueue_set_max_active(struct workqueue_struct *wq,
  288. int max_active);
  289. extern bool workqueue_congested(unsigned int cpu, struct workqueue_struct *wq);
  290. extern unsigned int work_cpu(struct work_struct *work);
  291. extern unsigned int work_busy(struct work_struct *work);
  292. /*
  293. * Kill off a pending schedule_delayed_work(). Note that the work callback
  294. * function may still be running on return from cancel_delayed_work(), unless
  295. * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or
  296. * cancel_work_sync() to wait on it.
  297. */
  298. static inline int cancel_delayed_work(struct delayed_work *work)
  299. {
  300. int ret;
  301. ret = del_timer_sync(&work->timer);
  302. if (ret)
  303. work_clear_pending(&work->work);
  304. return ret;
  305. }
  306. /*
  307. * Like above, but uses del_timer() instead of del_timer_sync(). This means,
  308. * if it returns 0 the timer function may be running and the queueing is in
  309. * progress.
  310. */
  311. static inline int __cancel_delayed_work(struct delayed_work *work)
  312. {
  313. int ret;
  314. ret = del_timer(&work->timer);
  315. if (ret)
  316. work_clear_pending(&work->work);
  317. return ret;
  318. }
  319. extern int cancel_delayed_work_sync(struct delayed_work *work);
  320. /* Obsolete. use cancel_delayed_work_sync() */
  321. static inline
  322. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  323. struct delayed_work *work)
  324. {
  325. cancel_delayed_work_sync(work);
  326. }
  327. /* Obsolete. use cancel_delayed_work_sync() */
  328. static inline
  329. void cancel_rearming_delayed_work(struct delayed_work *work)
  330. {
  331. cancel_delayed_work_sync(work);
  332. }
  333. #ifndef CONFIG_SMP
  334. static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  335. {
  336. return fn(arg);
  337. }
  338. #else
  339. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg);
  340. #endif /* CONFIG_SMP */
  341. #ifdef CONFIG_FREEZER
  342. extern void freeze_workqueues_begin(void);
  343. extern bool freeze_workqueues_busy(void);
  344. extern void thaw_workqueues(void);
  345. #endif /* CONFIG_FREEZER */
  346. #ifdef CONFIG_LOCKDEP
  347. int in_workqueue_context(struct workqueue_struct *wq);
  348. #endif
  349. #endif