workqueue.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 <asm/atomic.h>
  11. struct workqueue_struct;
  12. struct work_struct;
  13. typedef void (*work_func_t)(struct work_struct *work);
  14. /*
  15. * The first word is the work queue pointer and the flags rolled into
  16. * one
  17. */
  18. #define work_data_bits(work) ((unsigned long *)(&(work)->data))
  19. enum {
  20. WORK_STRUCT_PENDING_BIT = 0, /* work item is pending execution */
  21. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  22. WORK_STRUCT_STATIC_BIT = 1, /* static initializer (debugobjects) */
  23. #endif
  24. WORK_STRUCT_PENDING = 1 << WORK_STRUCT_PENDING_BIT,
  25. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  26. WORK_STRUCT_STATIC = 1 << WORK_STRUCT_STATIC_BIT,
  27. #else
  28. WORK_STRUCT_STATIC = 0,
  29. #endif
  30. WORK_STRUCT_FLAG_MASK = 3UL,
  31. WORK_STRUCT_WQ_DATA_MASK = ~WORK_STRUCT_FLAG_MASK,
  32. };
  33. struct work_struct {
  34. atomic_long_t data;
  35. struct list_head entry;
  36. work_func_t func;
  37. #ifdef CONFIG_LOCKDEP
  38. struct lockdep_map lockdep_map;
  39. #endif
  40. };
  41. #define WORK_DATA_INIT() ATOMIC_LONG_INIT(0)
  42. #define WORK_DATA_STATIC_INIT() ATOMIC_LONG_INIT(WORK_STRUCT_STATIC)
  43. struct delayed_work {
  44. struct work_struct work;
  45. struct timer_list timer;
  46. };
  47. static inline struct delayed_work *to_delayed_work(struct work_struct *work)
  48. {
  49. return container_of(work, struct delayed_work, work);
  50. }
  51. struct execute_work {
  52. struct work_struct work;
  53. };
  54. #ifdef CONFIG_LOCKDEP
  55. /*
  56. * NB: because we have to copy the lockdep_map, setting _key
  57. * here is required, otherwise it could get initialised to the
  58. * copy of the lockdep_map!
  59. */
  60. #define __WORK_INIT_LOCKDEP_MAP(n, k) \
  61. .lockdep_map = STATIC_LOCKDEP_MAP_INIT(n, k),
  62. #else
  63. #define __WORK_INIT_LOCKDEP_MAP(n, k)
  64. #endif
  65. #define __WORK_INITIALIZER(n, f) { \
  66. .data = WORK_DATA_STATIC_INIT(), \
  67. .entry = { &(n).entry, &(n).entry }, \
  68. .func = (f), \
  69. __WORK_INIT_LOCKDEP_MAP(#n, &(n)) \
  70. }
  71. #define __DELAYED_WORK_INITIALIZER(n, f) { \
  72. .work = __WORK_INITIALIZER((n).work, (f)), \
  73. .timer = TIMER_INITIALIZER(NULL, 0, 0), \
  74. }
  75. #define DECLARE_WORK(n, f) \
  76. struct work_struct n = __WORK_INITIALIZER(n, f)
  77. #define DECLARE_DELAYED_WORK(n, f) \
  78. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
  79. /*
  80. * initialize a work item's function pointer
  81. */
  82. #define PREPARE_WORK(_work, _func) \
  83. do { \
  84. (_work)->func = (_func); \
  85. } while (0)
  86. #define PREPARE_DELAYED_WORK(_work, _func) \
  87. PREPARE_WORK(&(_work)->work, (_func))
  88. #ifdef CONFIG_DEBUG_OBJECTS_WORK
  89. extern void __init_work(struct work_struct *work, int onstack);
  90. extern void destroy_work_on_stack(struct work_struct *work);
  91. static inline unsigned int work_static(struct work_struct *work)
  92. {
  93. return *work_data_bits(work) & WORK_STRUCT_STATIC;
  94. }
  95. #else
  96. static inline void __init_work(struct work_struct *work, int onstack) { }
  97. static inline void destroy_work_on_stack(struct work_struct *work) { }
  98. static inline unsigned int work_static(struct work_struct *work) { return 0; }
  99. #endif
  100. /*
  101. * initialize all of a work item in one go
  102. *
  103. * NOTE! No point in using "atomic_long_set()": using a direct
  104. * assignment of the work data initializer allows the compiler
  105. * to generate better code.
  106. */
  107. #ifdef CONFIG_LOCKDEP
  108. #define __INIT_WORK(_work, _func, _onstack) \
  109. do { \
  110. static struct lock_class_key __key; \
  111. \
  112. __init_work((_work), _onstack); \
  113. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  114. lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0);\
  115. INIT_LIST_HEAD(&(_work)->entry); \
  116. PREPARE_WORK((_work), (_func)); \
  117. } while (0)
  118. #else
  119. #define __INIT_WORK(_work, _func, _onstack) \
  120. do { \
  121. __init_work((_work), _onstack); \
  122. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  123. INIT_LIST_HEAD(&(_work)->entry); \
  124. PREPARE_WORK((_work), (_func)); \
  125. } while (0)
  126. #endif
  127. #define INIT_WORK(_work, _func) \
  128. do { \
  129. __INIT_WORK((_work), (_func), 0); \
  130. } while (0)
  131. #define INIT_WORK_ON_STACK(_work, _func) \
  132. do { \
  133. __INIT_WORK((_work), (_func), 1); \
  134. } while (0)
  135. #define INIT_DELAYED_WORK(_work, _func) \
  136. do { \
  137. INIT_WORK(&(_work)->work, (_func)); \
  138. init_timer(&(_work)->timer); \
  139. } while (0)
  140. #define INIT_DELAYED_WORK_ON_STACK(_work, _func) \
  141. do { \
  142. INIT_WORK_ON_STACK(&(_work)->work, (_func)); \
  143. init_timer_on_stack(&(_work)->timer); \
  144. } while (0)
  145. #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func) \
  146. do { \
  147. INIT_WORK(&(_work)->work, (_func)); \
  148. init_timer_deferrable(&(_work)->timer); \
  149. } while (0)
  150. /**
  151. * work_pending - Find out whether a work item is currently pending
  152. * @work: The work item in question
  153. */
  154. #define work_pending(work) \
  155. test_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
  156. /**
  157. * delayed_work_pending - Find out whether a delayable work item is currently
  158. * pending
  159. * @work: The work item in question
  160. */
  161. #define delayed_work_pending(w) \
  162. work_pending(&(w)->work)
  163. /**
  164. * work_clear_pending - for internal use only, mark a work item as not pending
  165. * @work: The work item in question
  166. */
  167. #define work_clear_pending(work) \
  168. clear_bit(WORK_STRUCT_PENDING_BIT, work_data_bits(work))
  169. enum {
  170. WQ_FREEZEABLE = 1 << 0, /* freeze during suspend */
  171. WQ_SINGLE_THREAD = 1 << 1, /* no per-cpu worker */
  172. };
  173. extern struct workqueue_struct *
  174. __create_workqueue_key(const char *name, unsigned int flags,
  175. struct lock_class_key *key, const char *lock_name);
  176. #ifdef CONFIG_LOCKDEP
  177. #define __create_workqueue(name, flags) \
  178. ({ \
  179. static struct lock_class_key __key; \
  180. const char *__lock_name; \
  181. \
  182. if (__builtin_constant_p(name)) \
  183. __lock_name = (name); \
  184. else \
  185. __lock_name = #name; \
  186. \
  187. __create_workqueue_key((name), (flags), &__key, \
  188. __lock_name); \
  189. })
  190. #else
  191. #define __create_workqueue(name, flags) \
  192. __create_workqueue_key((name), (flags), NULL, NULL)
  193. #endif
  194. #define create_workqueue(name) \
  195. __create_workqueue((name), 0)
  196. #define create_freezeable_workqueue(name) \
  197. __create_workqueue((name), WQ_FREEZEABLE | WQ_SINGLE_THREAD)
  198. #define create_singlethread_workqueue(name) \
  199. __create_workqueue((name), WQ_SINGLE_THREAD)
  200. extern void destroy_workqueue(struct workqueue_struct *wq);
  201. extern int queue_work(struct workqueue_struct *wq, struct work_struct *work);
  202. extern int queue_work_on(int cpu, struct workqueue_struct *wq,
  203. struct work_struct *work);
  204. extern int queue_delayed_work(struct workqueue_struct *wq,
  205. struct delayed_work *work, unsigned long delay);
  206. extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  207. struct delayed_work *work, unsigned long delay);
  208. extern void flush_workqueue(struct workqueue_struct *wq);
  209. extern void flush_scheduled_work(void);
  210. extern void flush_delayed_work(struct delayed_work *work);
  211. extern int schedule_work(struct work_struct *work);
  212. extern int schedule_work_on(int cpu, struct work_struct *work);
  213. extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay);
  214. extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
  215. unsigned long delay);
  216. extern int schedule_on_each_cpu(work_func_t func);
  217. extern int current_is_keventd(void);
  218. extern int keventd_up(void);
  219. extern void init_workqueues(void);
  220. int execute_in_process_context(work_func_t fn, struct execute_work *);
  221. extern int flush_work(struct work_struct *work);
  222. extern int cancel_work_sync(struct work_struct *work);
  223. /*
  224. * Kill off a pending schedule_delayed_work(). Note that the work callback
  225. * function may still be running on return from cancel_delayed_work(), unless
  226. * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or
  227. * cancel_work_sync() to wait on it.
  228. */
  229. static inline int cancel_delayed_work(struct delayed_work *work)
  230. {
  231. int ret;
  232. ret = del_timer_sync(&work->timer);
  233. if (ret)
  234. work_clear_pending(&work->work);
  235. return ret;
  236. }
  237. /*
  238. * Like above, but uses del_timer() instead of del_timer_sync(). This means,
  239. * if it returns 0 the timer function may be running and the queueing is in
  240. * progress.
  241. */
  242. static inline int __cancel_delayed_work(struct delayed_work *work)
  243. {
  244. int ret;
  245. ret = del_timer(&work->timer);
  246. if (ret)
  247. work_clear_pending(&work->work);
  248. return ret;
  249. }
  250. extern int cancel_delayed_work_sync(struct delayed_work *work);
  251. /* Obsolete. use cancel_delayed_work_sync() */
  252. static inline
  253. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  254. struct delayed_work *work)
  255. {
  256. cancel_delayed_work_sync(work);
  257. }
  258. /* Obsolete. use cancel_delayed_work_sync() */
  259. static inline
  260. void cancel_rearming_delayed_work(struct delayed_work *work)
  261. {
  262. cancel_delayed_work_sync(work);
  263. }
  264. #ifndef CONFIG_SMP
  265. static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  266. {
  267. return fn(arg);
  268. }
  269. #else
  270. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg);
  271. #endif /* CONFIG_SMP */
  272. #endif