workqueue.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_FLAG_MASK (3UL)
  22. #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)
  23. struct list_head entry;
  24. work_func_t func;
  25. };
  26. #define WORK_DATA_INIT() ATOMIC_LONG_INIT(0)
  27. struct delayed_work {
  28. struct work_struct work;
  29. struct timer_list timer;
  30. };
  31. struct execute_work {
  32. struct work_struct work;
  33. };
  34. #define __WORK_INITIALIZER(n, f) { \
  35. .data = WORK_DATA_INIT(), \
  36. .entry = { &(n).entry, &(n).entry }, \
  37. .func = (f), \
  38. }
  39. #define __DELAYED_WORK_INITIALIZER(n, f) { \
  40. .work = __WORK_INITIALIZER((n).work, (f)), \
  41. .timer = TIMER_INITIALIZER(NULL, 0, 0), \
  42. }
  43. #define DECLARE_WORK(n, f) \
  44. struct work_struct n = __WORK_INITIALIZER(n, f)
  45. #define DECLARE_DELAYED_WORK(n, f) \
  46. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f)
  47. /*
  48. * initialize a work item's function pointer
  49. */
  50. #define PREPARE_WORK(_work, _func) \
  51. do { \
  52. (_work)->func = (_func); \
  53. } while (0)
  54. #define PREPARE_DELAYED_WORK(_work, _func) \
  55. PREPARE_WORK(&(_work)->work, (_func))
  56. /*
  57. * initialize all of a work item in one go
  58. *
  59. * NOTE! No point in using "atomic_long_set()": useing a direct
  60. * assignment of the work data initializer allows the compiler
  61. * to generate better code.
  62. */
  63. #define INIT_WORK(_work, _func) \
  64. do { \
  65. (_work)->data = (atomic_long_t) WORK_DATA_INIT(); \
  66. INIT_LIST_HEAD(&(_work)->entry); \
  67. PREPARE_WORK((_work), (_func)); \
  68. } while (0)
  69. #define INIT_DELAYED_WORK(_work, _func) \
  70. do { \
  71. INIT_WORK(&(_work)->work, (_func)); \
  72. init_timer(&(_work)->timer); \
  73. } while (0)
  74. #define INIT_DELAYED_WORK_DEFERRABLE(_work, _func) \
  75. do { \
  76. INIT_WORK(&(_work)->work, (_func)); \
  77. init_timer_deferrable(&(_work)->timer); \
  78. } while (0)
  79. /**
  80. * work_pending - Find out whether a work item is currently pending
  81. * @work: The work item in question
  82. */
  83. #define work_pending(work) \
  84. test_bit(WORK_STRUCT_PENDING, work_data_bits(work))
  85. /**
  86. * delayed_work_pending - Find out whether a delayable work item is currently
  87. * pending
  88. * @work: The work item in question
  89. */
  90. #define delayed_work_pending(w) \
  91. work_pending(&(w)->work)
  92. /**
  93. * work_clear_pending - for internal use only, mark a work item as not pending
  94. * @work: The work item in question
  95. */
  96. #define work_clear_pending(work) \
  97. clear_bit(WORK_STRUCT_PENDING, work_data_bits(work))
  98. extern struct workqueue_struct *__create_workqueue(const char *name,
  99. int singlethread,
  100. int freezeable);
  101. #define create_workqueue(name) __create_workqueue((name), 0, 0)
  102. #define create_freezeable_workqueue(name) __create_workqueue((name), 1, 1)
  103. #define create_singlethread_workqueue(name) __create_workqueue((name), 1, 0)
  104. extern void destroy_workqueue(struct workqueue_struct *wq);
  105. extern int FASTCALL(queue_work(struct workqueue_struct *wq, struct work_struct *work));
  106. extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq,
  107. struct delayed_work *work, unsigned long delay));
  108. extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  109. struct delayed_work *work, unsigned long delay);
  110. extern void FASTCALL(flush_workqueue(struct workqueue_struct *wq));
  111. extern void flush_scheduled_work(void);
  112. extern int FASTCALL(schedule_work(struct work_struct *work));
  113. extern int FASTCALL(schedule_delayed_work(struct delayed_work *work,
  114. unsigned long delay));
  115. extern int schedule_delayed_work_on(int cpu, struct delayed_work *work,
  116. unsigned long delay);
  117. extern int schedule_on_each_cpu(work_func_t func);
  118. extern int current_is_keventd(void);
  119. extern int keventd_up(void);
  120. extern void init_workqueues(void);
  121. int execute_in_process_context(work_func_t fn, struct execute_work *);
  122. extern void cancel_work_sync(struct work_struct *work);
  123. /*
  124. * Kill off a pending schedule_delayed_work(). Note that the work callback
  125. * function may still be running on return from cancel_delayed_work(), unless
  126. * it returns 1 and the work doesn't re-arm itself. Run flush_workqueue() or
  127. * cancel_work_sync() to wait on it.
  128. */
  129. static inline int cancel_delayed_work(struct delayed_work *work)
  130. {
  131. int ret;
  132. ret = del_timer(&work->timer);
  133. if (ret)
  134. work_clear_pending(&work->work);
  135. return ret;
  136. }
  137. extern void cancel_rearming_delayed_work(struct delayed_work *work);
  138. /* Obsolete. use cancel_rearming_delayed_work() */
  139. static inline
  140. void cancel_rearming_delayed_workqueue(struct workqueue_struct *wq,
  141. struct delayed_work *work)
  142. {
  143. cancel_rearming_delayed_work(work);
  144. }
  145. #endif