workqueue.h 8.5 KB

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