workqueue.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * File : workqueue.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-02-27 bernard fix the re-work issue.
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #ifdef RT_USING_HEAP
  28. static void _workqueue_thread_entry(void* parameter)
  29. {
  30. rt_base_t level;
  31. struct rt_work* work;
  32. struct rt_workqueue* queue;
  33. queue = (struct rt_workqueue*) parameter;
  34. RT_ASSERT(queue != RT_NULL);
  35. while (1)
  36. {
  37. if (rt_list_isempty(&(queue->work_list)))
  38. {
  39. /* no software timer exist, suspend self. */
  40. rt_thread_suspend(rt_thread_self());
  41. rt_schedule();
  42. }
  43. /* we have work to do with. */
  44. level = rt_hw_interrupt_disable();
  45. work = rt_list_entry(queue->work_list.next, struct rt_work, list);
  46. rt_list_remove(&(work->list));
  47. queue->work_current = work;
  48. rt_hw_interrupt_enable(level);
  49. /* do work */
  50. work->work_func(work, work->work_data);
  51. level = rt_hw_interrupt_disable();
  52. /* clean current work */
  53. queue->work_current = RT_NULL;
  54. rt_hw_interrupt_enable(level);
  55. }
  56. }
  57. struct rt_workqueue *rt_workqueue_create(const char* name, rt_uint16_t stack_size, rt_uint8_t priority)
  58. {
  59. struct rt_workqueue *queue = RT_NULL;
  60. queue = (struct rt_workqueue*)RT_KERNEL_MALLOC(sizeof(struct rt_workqueue));
  61. if (queue != RT_NULL)
  62. {
  63. /* initialize work list */
  64. rt_list_init(&(queue->work_list));
  65. queue->work_current = RT_NULL;
  66. /* create the work thread */
  67. queue->work_thread = rt_thread_create(name, _workqueue_thread_entry, queue, stack_size, priority, 10);
  68. if (queue->work_thread == RT_NULL)
  69. {
  70. RT_KERNEL_FREE(queue);
  71. return RT_NULL;
  72. }
  73. rt_thread_startup(queue->work_thread);
  74. }
  75. return queue;
  76. }
  77. rt_err_t rt_workqueue_destroy(struct rt_workqueue* queue)
  78. {
  79. RT_ASSERT(queue != RT_NULL);
  80. rt_thread_delete(queue->work_thread);
  81. RT_KERNEL_FREE(queue);
  82. return RT_EOK;
  83. }
  84. rt_err_t rt_workqueue_dowork(struct rt_workqueue* queue, struct rt_work* work)
  85. {
  86. rt_base_t level;
  87. RT_ASSERT(queue != RT_NULL);
  88. RT_ASSERT(work != RT_NULL);
  89. level = rt_hw_interrupt_disable();
  90. if (queue->work_current == work)
  91. {
  92. rt_hw_interrupt_enable(level);
  93. return -RT_EBUSY;
  94. }
  95. /* NOTE: the work MUST be initialized firstly */
  96. rt_list_remove(&(work->list));
  97. rt_list_insert_after(queue->work_list.prev, &(work->list));
  98. /* whether the workqueue is doing work */
  99. if (queue->work_current == RT_NULL)
  100. {
  101. rt_hw_interrupt_enable(level);
  102. /* resume work thread */
  103. rt_thread_resume(queue->work_thread);
  104. rt_schedule();
  105. }
  106. else rt_hw_interrupt_enable(level);
  107. return RT_EOK;
  108. }
  109. rt_err_t rt_workqueue_critical_work(struct rt_workqueue* queue, struct rt_work* work)
  110. {
  111. rt_base_t level;
  112. RT_ASSERT(queue != RT_NULL);
  113. RT_ASSERT(work != RT_NULL);
  114. level = rt_hw_interrupt_disable();
  115. if (queue->work_current == work)
  116. {
  117. rt_hw_interrupt_enable(level);
  118. return -RT_EBUSY;
  119. }
  120. /* NOTE: the work MUST be initialized firstly */
  121. rt_list_remove(&(work->list));
  122. rt_list_insert_after(queue->work_list.prev, &(work->list));
  123. if (queue->work_current == RT_NULL)
  124. {
  125. rt_hw_interrupt_enable(level);
  126. /* resume work thread */
  127. rt_thread_resume(queue->work_thread);
  128. rt_schedule();
  129. }
  130. else rt_hw_interrupt_enable(level);
  131. return RT_EOK;
  132. }
  133. rt_err_t rt_workqueue_cancel_work(struct rt_workqueue* queue, struct rt_work* work)
  134. {
  135. rt_base_t level;
  136. RT_ASSERT(queue != RT_NULL);
  137. RT_ASSERT(work != RT_NULL);
  138. level = rt_hw_interrupt_disable();
  139. if (queue->work_current == work)
  140. {
  141. rt_hw_interrupt_enable(level);
  142. return -RT_EBUSY;
  143. }
  144. rt_list_remove(&(work->list));
  145. rt_hw_interrupt_enable(level);
  146. return RT_EOK;
  147. }
  148. rt_err_t rt_workqueue_cancel_all_work(struct rt_workqueue* queue)
  149. {
  150. struct rt_list_node *node, *next;
  151. RT_ASSERT(queue != RT_NULL);
  152. rt_enter_critical();
  153. for (node = queue->work_list.next; node != &(queue->work_list); node = next)
  154. {
  155. next = node->next;
  156. rt_list_remove(node);
  157. }
  158. rt_exit_critical();
  159. return RT_EOK;
  160. }
  161. #endif