workqueue.h 7.7 KB

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