workqueue.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #else
  82. static inline void __init_work(struct work_struct *work, int onstack) { }
  83. static inline void destroy_work_on_stack(struct work_struct *work) { }
  84. #endif
  85. /*
  86. * initialize all of a work item in one go
  87. *
  88. * NOTE! No point in using "atomic_long_set()": using a direct
  89. * assignment of the work data initializer allows the compiler
  90. * to generate better code.
  91. */
  92. #ifdef CONFIG_LOCKDEP
  93. #define __INIT_WORK(_work, _func, _onstack) \
  94. do { \
  95. static struct lock_class_key __key; \
  96. \
  97. __init_work((_work), _onstack); \
  98. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  99. lockdep_init_map(&(_work)->lockdep_map, #_work, &__key, 0);\
  100. INIT_LIST_HEAD(&(_work)->entry); \
  101. PREPARE_WORK((_work), (_func)); \
  102. } while (0)
  103. #else
  104. #define __INIT_WORK(_work, _func, _onstack) \
  105. do { \
  106. __init_work((_work), _onstack); \
  107. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  108. INIT_LIST_HEAD(&(_work)->entry); \
  109. PREPARE_WORK((_work), (_func)); \
  110. } while (0)
  111. #endif
  112. #define INIT_WORK(_work, _func) \
  113. do { \
  114. __INIT_WORK((_work), (_func), 0); \
  115. } while (0)
  116. #define INIT_WORK_ON_STACK(_work, _func) \
  117. do { \
  118. __INIT_WORK((_work), (_func), 1); \
  119. } while (0)
  120. #define INIT_DELAYED_WORK(_work, _func) \
  121. do { \
  122. INIT_WORK(&(_work)->work, (_func)); \
  123. init_timer(&(_work)->timer); \
  124. } while (0)
  125. #define INIT_DELAYED_WORK_ON_STACK(_work, _func) \
  126. do { \
  127. INIT_WORK_ON_STACK(&(_work)->work, (_func)); \
  128. init_timer_on_stack(&(_work)->timer); \
  129. } while (0)
  130. #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func) \
  131. do { \
  132. INIT_WORK(&(_work)->work, (_func)); \
  133. init_timer_deferrable(&(_work)->timer); \
  134. } while (0)
  135. /**
  136. * work_pending - Find out whether a work item is currently pending
  137. * @work: The work item in question
  138. */
  139. #define work_pending(work) \
  140. test_bit(WORK_STRUCT_PENDING, work_data_bits(work))
  141. /**
  142. * delayed_work_pending - Find out whether a delayable work item is currently
  143. * pending
  144. * @work: The work item in question
  145. */
  146. #define delayed_work_pending(w) \
  147. work_pending(&(w)->work)
  148. /**
  149. * work_clear_pending - for internal use only, mark a work item as not pending
  150. * @work: The work item in question
  151. */
  152. #define work_clear_pending(work) \
  153. clear_bit(WORK_STRUCT_PENDING, work_data_bits(work))
  154. extern struct workqueue_struct *
  155. __create_workqueue_key(const char *name, int singlethread, int freezeable,
  156. struct lock_class_key *key, const char *lock_name);
  157. #ifdef CONFIG_LOCKDEP
  158. #define __create_workqueue(name, singlethread, freezeable) \
  159. ({ \
  160. static struct lock_class_key __key; \
  161. const char *__lock_name; \
  162. \
  163. if (__builtin_constant_p(name)) \
  164. __lock_name = (name); \
  165. else \
  166. __lock_name = #name; \
  167. \
  168. __create_workqueue_key((name), (singlethread), \
  169. (freezeable), &__key, \
  170. __lock_name); \
  171. })
  172. #else
  173. #define __create_workqueue(name, singlethread, freezeable) \
  174. __create_workqueue_key((name), (singlethread), (freezeable), \
  175. NULL, NULL)
  176. #endif
  177. #define create_workqueue(name) __create_workqueue((name), 0, 0)
  178. #define create_freezeable_workqueue(name) __create_workqueue((name), 1, 1)
  179. #define create_singlethread_workqueue(name) __create_workqueue((name), 1, 0)
  180. extern void destroy_workqueue(struct workqueue_struct *wq);
  181. extern int queue_work(struct workqueue_struct *wq, struct work_struct *work);
  182. extern int queue_work_on(int cpu, struct workqueue_struct *wq,
  183. struct work_struct *work);
  184. extern int queue_delayed_work(struct workqueue_struct *wq,
  185. struct delayed_work *work, unsigned long delay);
  186. extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  187. struct delayed_work *work, unsigned long delay);
  188. extern void flush_workqueue(struct workqueue_struct *wq);
  189. extern void flush_scheduled_work(void);
  190. extern void flush_delayed_work(struct delayed_work *work);
  191. extern int schedule_work(struct work_struct *work);
  192. extern int schedule_work_on(int cpu, struct work_struct *work);
  193. extern int schedule_delayed_work(struct delayed_work *work, unsigned long delay);
  194. extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
  195. unsigned long delay);
  196. extern int schedule_on_each_cpu(work_func_t func);
  197. extern int current_is_keventd(void);
  198. extern int keventd_up(void);
  199. extern void init_workqueues(void);
  200. int execute_in_process_context(work_func_t fn, struct execute_work *);
  201. extern int flush_work(struct work_struct *work);
  202. extern int cancel_work_sync(struct work_struct *work);
  203. /*
  204. * Kill off a pending schedule_delayed_work(). Note that the work callback
  205. * function may still be running on return from cancel_delayed_work(), unless
  206. * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or
  207. * cancel_work_sync() to wait on it.
  208. */
  209. static inline int cancel_delayed_work(struct delayed_work *work)
  210. {
  211. int ret;
  212. ret = del_timer_sync(&work->timer);
  213. if (ret)
  214. work_clear_pending(&work->work);
  215. return ret;
  216. }
  217. /*
  218. * Like above, but uses del_timer() instead of del_timer_sync(). This means,
  219. * if it returns 0 the timer function may be running and the queueing is in
  220. * progress.
  221. */
  222. static inline int __cancel_delayed_work(struct delayed_work *work)
  223. {
  224. int ret;
  225. ret = del_timer(&work->timer);
  226. if (ret)
  227. work_clear_pending(&work->work);
  228. return ret;
  229. }
  230. extern int cancel_delayed_work_sync(struct delayed_work *work);
  231. /* Obsolete. use cancel_delayed_work_sync() */
  232. static inline
  233. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  234. struct delayed_work *work)
  235. {
  236. cancel_delayed_work_sync(work);
  237. }
  238. /* Obsolete. use cancel_delayed_work_sync() */
  239. static inline
  240. void cancel_rearming_delayed_work(struct delayed_work *work)
  241. {
  242. cancel_delayed_work_sync(work);
  243. }
  244. #ifndef CONFIG_SMP
  245. static inline long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
  246. {
  247. return fn(arg);
  248. }
  249. #else
  250. long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg);
  251. #endif /* CONFIG_SMP */
  252. #endif