work.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2013-2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. *
  18. * Deferred work queues.
  19. *
  20. * Works, like threads, are scheduled activities, but they are much shorter
  21. * and (usually) consume a lot less resources. They are allowed to block
  22. * and must run in thread context. This module provides thread pools to
  23. * concurrently handle queued works.
  24. */
  25. #ifndef KERN_WORK_H
  26. #define KERN_WORK_H
  27. #include <kern/init.h>
  28. /*
  29. * Work scheduling flags.
  30. */
  31. #define WORK_HIGHPRIO 0x1 /* Use a high priority worker thread */
  32. /*
  33. * Deferred work.
  34. *
  35. * This structure should be embedded in objects related to the work. It
  36. * stores the work function and is passed to it as its only parameter.
  37. * The function can then find the containing object with the structof macro.
  38. */
  39. struct work;
  40. /*
  41. * Queue of deferred works for batch scheduling.
  42. */
  43. struct work_queue;
  44. /*
  45. * Type for work functions.
  46. */
  47. typedef void (*work_fn_t)(struct work *);
  48. #include <kern/work_i.h>
  49. static inline void
  50. work_queue_init(struct work_queue *queue)
  51. {
  52. queue->first = NULL;
  53. queue->last = NULL;
  54. queue->nr_works = 0;
  55. }
  56. static inline unsigned int
  57. work_queue_nr_works(const struct work_queue *queue)
  58. {
  59. return queue->nr_works;
  60. }
  61. static inline void
  62. work_queue_push(struct work_queue *queue, struct work *work)
  63. {
  64. work->next = NULL;
  65. if (queue->last == NULL) {
  66. queue->first = work;
  67. } else {
  68. queue->last->next = work;
  69. }
  70. queue->last = work;
  71. queue->nr_works++;
  72. }
  73. static inline struct work *
  74. work_queue_pop(struct work_queue *queue)
  75. {
  76. struct work *work;
  77. work = queue->first;
  78. queue->first = work->next;
  79. if (queue->last == work) {
  80. queue->last = NULL;
  81. }
  82. queue->nr_works--;
  83. return work;
  84. }
  85. static inline void
  86. work_queue_transfer(struct work_queue *dest, struct work_queue *src)
  87. {
  88. *dest = *src;
  89. }
  90. static inline void
  91. work_queue_concat(struct work_queue *queue1, struct work_queue *queue2)
  92. {
  93. if (queue2->nr_works == 0) {
  94. return;
  95. }
  96. if (queue1->nr_works == 0) {
  97. *queue1 = *queue2;
  98. return;
  99. }
  100. queue1->last->next = queue2->first;
  101. queue1->last = queue2->last;
  102. queue1->nr_works += queue2->nr_works;
  103. }
  104. static inline void
  105. work_init(struct work *work, work_fn_t fn)
  106. {
  107. work->fn = fn;
  108. }
  109. /*
  110. * Schedule work for deferred processing.
  111. *
  112. * After being scheduled, a work queue must be reinitialized before
  113. * it can be reused.
  114. *
  115. * This function may be called from interrupt context.
  116. */
  117. void work_schedule(struct work *work, int flags);
  118. void work_queue_schedule(struct work_queue *queue, int flags);
  119. /*
  120. * Report a periodic event (normally the periodic timer interrupt) on the
  121. * current processor.
  122. *
  123. * Periodic events are used internally for optimizations.
  124. *
  125. * Interrupts and preemption must be disabled when calling this function.
  126. */
  127. void work_report_periodic_event(void);
  128. /*
  129. * This init operation provides :
  130. * - work / work queue initialization and scheduling
  131. */
  132. INIT_OP_DECLARE(work_bootstrap);
  133. #endif /* KERN_WORK_H */