workqueue.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. struct workqueue_struct;
  10. typedef void (*work_func_t)(void *data);
  11. struct work_struct {
  12. /* the first word is the work queue pointer and the pending flag
  13. * rolled into one */
  14. unsigned long management;
  15. #define WORK_STRUCT_PENDING 0 /* T if work item pending execution */
  16. #define WORK_STRUCT_FLAG_MASK (3UL)
  17. #define WORK_STRUCT_WQ_DATA_MASK (~WORK_STRUCT_FLAG_MASK)
  18. struct list_head entry;
  19. work_func_t func;
  20. void *data;
  21. };
  22. struct delayed_work {
  23. struct work_struct work;
  24. struct timer_list timer;
  25. };
  26. struct execute_work {
  27. struct work_struct work;
  28. };
  29. #define __WORK_INITIALIZER(n, f, d) { \
  30. .entry = { &(n).entry, &(n).entry }, \
  31. .func = (f), \
  32. .data = (d), \
  33. }
  34. #define __DELAYED_WORK_INITIALIZER(n, f, d) { \
  35. .work = __WORK_INITIALIZER((n).work, (f), (d)), \
  36. .timer = TIMER_INITIALIZER(NULL, 0, 0), \
  37. }
  38. #define DECLARE_WORK(n, f, d) \
  39. struct work_struct n = __WORK_INITIALIZER(n, f, d)
  40. #define DECLARE_DELAYED_WORK(n, f, d) \
  41. struct delayed_work n = __DELAYED_WORK_INITIALIZER(n, f, d)
  42. /*
  43. * initialize a work item's function and data pointers
  44. */
  45. #define PREPARE_WORK(_work, _func, _data) \
  46. do { \
  47. (_work)->func = (_func); \
  48. (_work)->data = (_data); \
  49. } while (0)
  50. #define PREPARE_DELAYED_WORK(_work, _func, _data) \
  51. PREPARE_WORK(&(_work)->work, (_func), (_data))
  52. /*
  53. * initialize all of a work item in one go
  54. */
  55. #define INIT_WORK(_work, _func, _data) \
  56. do { \
  57. INIT_LIST_HEAD(&(_work)->entry); \
  58. (_work)->management = 0; \
  59. PREPARE_WORK((_work), (_func), (_data)); \
  60. } while (0)
  61. #define INIT_DELAYED_WORK(_work, _func, _data) \
  62. do { \
  63. INIT_WORK(&(_work)->work, (_func), (_data)); \
  64. init_timer(&(_work)->timer); \
  65. } while (0)
  66. /**
  67. * work_pending - Find out whether a work item is currently pending
  68. * @work: The work item in question
  69. */
  70. #define work_pending(work) \
  71. test_bit(WORK_STRUCT_PENDING, &(work)->management)
  72. /**
  73. * delayed_work_pending - Find out whether a delayable work item is currently
  74. * pending
  75. * @work: The work item in question
  76. */
  77. #define delayed_work_pending(work) \
  78. test_bit(WORK_STRUCT_PENDING, &(work)->work.management)
  79. extern struct workqueue_struct *__create_workqueue(const char *name,
  80. int singlethread);
  81. #define create_workqueue(name) __create_workqueue((name), 0)
  82. #define create_singlethread_workqueue(name) __create_workqueue((name), 1)
  83. extern void destroy_workqueue(struct workqueue_struct *wq);
  84. extern int FASTCALL(queue_work(struct workqueue_struct *wq, struct work_struct *work));
  85. extern int FASTCALL(queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay));
  86. extern int queue_delayed_work_on(int cpu, struct workqueue_struct *wq,
  87. struct delayed_work *work, unsigned long delay);
  88. extern void FASTCALL(flush_workqueue(struct workqueue_struct *wq));
  89. extern int FASTCALL(schedule_work(struct work_struct *work));
  90. extern int FASTCALL(schedule_delayed_work(struct delayed_work *work, unsigned long delay));
  91. extern int schedule_delayed_work_on(int cpu, struct delayed_work *work, unsigned long delay);
  92. extern int schedule_on_each_cpu(work_func_t func, void *info);
  93. extern void flush_scheduled_work(void);
  94. extern int current_is_keventd(void);
  95. extern int keventd_up(void);
  96. extern void init_workqueues(void);
  97. void cancel_rearming_delayed_work(struct delayed_work *work);
  98. void cancel_rearming_delayed_workqueue(struct workqueue_struct *,
  99. struct delayed_work *);
  100. int execute_in_process_context(work_func_t fn, void *, struct execute_work *);
  101. /*
  102. * Kill off a pending schedule_delayed_work(). Note that the work callback
  103. * function may still be running on return from cancel_delayed_work(). Run
  104. * flush_scheduled_work() to wait on it.
  105. */
  106. static inline int cancel_delayed_work(struct delayed_work *work)
  107. {
  108. int ret;
  109. ret = del_timer_sync(&work->timer);
  110. if (ret)
  111. clear_bit(WORK_STRUCT_PENDING, &work->work.management);
  112. return ret;
  113. }
  114. #endif