workqueue.h 6.9 KB

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