workqueue.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <asm/atomic.h>
  10. struct workqueue_struct;
  11. struct work_struct;
  12. typedef void (*work_func_t)(struct work_struct *work);
  13. /*
  14. * The first word is the work queue pointer and the flags rolled into
  15. * one
  16. */
  17. #define work_data_bits(work) ((unsigned long *)(&(work)->data))
  18. struct work_struct {
  19. atomic_long_t data;
  20. #define WORK_STRUCT_PENDING 0 /* T if work item pending execution */
  21. #define WORK_STRUCT_NOAUTOREL 1 /* F if work item automatically released on exec */
  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. };
  27. #define WORK_DATA_INIT(autorelease) \
  28. ATOMIC_LONG_INIT((autorelease) << WORK_STRUCT_NOAUTOREL)
  29. struct delayed_work {
  30. struct work_struct work;
  31. struct timer_list timer;
  32. };
  33. struct execute_work {
  34. struct work_struct work;
  35. };
  36. #define __WORK_INITIALIZER(n, f) { \
  37. .data = WORK_DATA_INIT(0), \
  38. .entry = { &(n).entry, &(n).entry }, \
  39. .func = (f), \
  40. }
  41. #define __WORK_INITIALIZER_NAR(n, f) { \
  42. .data = WORK_DATA_INIT(1), \
  43. .entry = { &(n).entry, &(n).entry }, \
  44. .func = (f), \
  45. }
  46. #define __DELAYED_WORK_INITIALIZER(n, f) { \
  47. .work = __WORK_INITIALIZER((n).work, (f)), \
  48. .timer = TIMER_INITIALIZER(NULL, 0, 0), \
  49. }
  50. #define __DELAYED_WORK_INITIALIZER_NAR(n, f) { \
  51. .work = __WORK_INITIALIZER_NAR((n).work, (f)), \
  52. .timer = TIMER_INITIALIZER(NULL, 0, 0), \
  53. }
  54. #define DECLARE_WORK(n, f) \
  55. struct work_struct n = __WORK_INITIALIZER(n, f)
  56. #define DECLARE_WORK_NAR(n, f) \
  57. struct work_struct n = __WORK_INITIALIZER_NAR(n, f)
  58. #define DECLARE_DELAYED_WORK(n, f) \
  59. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
  60. #define DECLARE_DELAYED_WORK_NAR(n, f) \
  61. struct dwork_struct n = __DELAYED_WORK_INITIALIZER_NAR(n, f)
  62. /*
  63. * initialize a work item's function pointer
  64. */
  65. #define PREPARE_WORK(_work, _func) \
  66. do { \
  67. (_work)->func = (_func); \
  68. } while (0)
  69. #define PREPARE_DELAYED_WORK(_work, _func) \
  70. PREPARE_WORK(&(_work)->work, (_func))
  71. /*
  72. * initialize all of a work item in one go
  73. *
  74. * NOTE! No point in using "atomic_long_set()": useing a direct
  75. * assignment of the work data initializer allows the compiler
  76. * to generate better code.
  77. */
  78. #define INIT_WORK(_work, _func) \
  79. do { \
  80. (_work)->data = (atomic_long_t) WORK_DATA_INIT(0); \
  81. INIT_LIST_HEAD(&(_work)->entry); \
  82. PREPARE_WORK((_work), (_func)); \
  83. } while (0)
  84. #define INIT_WORK_NAR(_work, _func) \
  85. do { \
  86. (_work)->data = (atomic_long_t) WORK_DATA_INIT(1); \
  87. INIT_LIST_HEAD(&(_work)->entry); \
  88. PREPARE_WORK((_work), (_func)); \
  89. } while (0)
  90. #define INIT_DELAYED_WORK(_work, _func) \
  91. do { \
  92. INIT_WORK(&(_work)->work, (_func)); \
  93. init_timer(&(_work)->timer); \
  94. } while (0)
  95. #define INIT_DELAYED_WORK_NAR(_work, _func) \
  96. do { \
  97. INIT_WORK_NAR(&(_work)->work, (_func)); \
  98. init_timer(&(_work)->timer); \
  99. } while (0)
  100. /**
  101. * work_pending - Find out whether a work item is currently pending
  102. * @work: The work item in question
  103. */
  104. #define work_pending(work) \
  105. test_bit(WORK_STRUCT_PENDING, work_data_bits(work))
  106. /**
  107. * delayed_work_pending - Find out whether a delayable work item is currently
  108. * pending
  109. * @work: The work item in question
  110. */
  111. #define delayed_work_pending(w) \
  112. work_pending(&(w)->work)
  113. /**
  114. * work_release - Release a work item under execution
  115. * @work: The work item to release
  116. *
  117. * This is used to release a work item that has been initialised with automatic
  118. * release mode disabled (WORK_STRUCT_NOAUTOREL is set). This gives the work
  119. * function the opportunity to grab auxiliary data from the container of the
  120. * work_struct before clearing the pending bit as the work_struct may be
  121. * subject to deallocation the moment the pending bit is cleared.
  122. *
  123. * In such a case, this should be called in the work function after it has
  124. * fetched any data it may require from the containter of the work_struct.
  125. * After this function has been called, the work_struct may be scheduled for
  126. * further execution or it may be deallocated unless other precautions are
  127. * taken.
  128. *
  129. * This should also be used to release a delayed work item.
  130. */
  131. #define work_release(work) \
  132. clear_bit(WORK_STRUCT_PENDING, work_data_bits(work))
  133. extern struct workqueue_struct *__create_workqueue(const char *name,
  134. int singlethread,
  135. int freezeable);
  136. #define create_workqueue(name) __create_workqueue((name), 0, 0)
  137. #define create_freezeable_workqueue(name) __create_workqueue((name), 1, 1)
  138. #define create_singlethread_workqueue(name) __create_workqueue((name), 1, 0)
  139. extern void destroy_workqueue(struct workqueue_struct *wq);
  140. extern int FASTCALL(queue_work(struct workqueue_struct *wq, struct work_struct *work));
  141. extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay));
  142. extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  143. struct delayed_work *work, unsigned long delay);
  144. extern void FASTCALL(flush_workqueue(struct workqueue_struct *wq));
  145. extern int FASTCALL(schedule_work(struct work_struct *work));
  146. extern int FASTCALL(run_scheduled_work(struct work_struct *work));
  147. extern int FASTCALL(schedule_delayed_work(struct delayed_work *work, unsigned long delay));
  148. extern int schedule_delayed_work_on(int cpu, struct delayed_work *work, unsigned long delay);
  149. extern int schedule_on_each_cpu(work_func_t func);
  150. extern void flush_scheduled_work(void);
  151. extern int current_is_keventd(void);
  152. extern int keventd_up(void);
  153. extern void init_workqueues(void);
  154. void cancel_rearming_delayed_work(struct delayed_work *work);
  155. void cancel_rearming_delayed_workqueue(struct workqueue_struct *,
  156. struct delayed_work *);
  157. int execute_in_process_context(work_func_t fn, struct execute_work *);
  158. /*
  159. * Kill off a pending schedule_delayed_work(). Note that the work callback
  160. * function may still be running on return from cancel_delayed_work(). Run
  161. * flush_scheduled_work() to wait on it.
  162. */
  163. static inline int cancel_delayed_work(struct delayed_work *work)
  164. {
  165. int ret;
  166. ret = del_timer_sync(&work->timer);
  167. if (ret)
  168. work_release(&work->work);
  169. return ret;
  170. }
  171. #endif