workqueue.h 14 KB

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