workqueue.h 14 KB

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