workqueue.hpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /****************************************************************************
  2. *
  3. * Copyright (c) 2017, Michael Becker (michael.f.becker@gmail.com)
  4. *
  5. * This file is part of the FreeRTOS Add-ons project.
  6. *
  7. * Source Code:
  8. * https://github.com/michaelbecker/freertos-addons
  9. *
  10. * Project Page:
  11. * http://michaelbecker.github.io/freertos-addons/
  12. *
  13. * On-line Documentation:
  14. * http://michaelbecker.github.io/freertos-addons/docs/html/index.html
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a
  17. * copy of this software and associated documentation files
  18. * (the "Software"), to deal in the Software without restriction, including
  19. * without limitation the rights to use, copy, modify, merge, publish,
  20. * distribute, sublicense, and/or sell copies of the Software, and to
  21. * permit persons to whom the Software is furnished to do so,subject to the
  22. * following conditions:
  23. *
  24. * + The above copyright notice and this permission notice shall be included
  25. * in all copies or substantial portions of the Software.
  26. * + Credit is appreciated, but not required, if you find this project
  27. * useful enough to include in your application, product, device, etc.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  32. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  33. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  34. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  35. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  36. *
  37. ***************************************************************************/
  38. #ifndef WORK_QUEUE_HPP_
  39. #define WORK_QUEUE_HPP_
  40. #include "thread.hpp"
  41. #include "queue.hpp"
  42. #include "semaphore.hpp"
  43. namespace cpp_freertos {
  44. #define DEFAULT_MAX_WORK_ITEMS 10
  45. #define DEFAULT_WORK_QUEUE_STACK_SIZE (configMINIMAL_STACK_SIZE * 2)
  46. #define DEFAULT_WORK_QUEUE_PRIORITY (tskIDLE_PRIORITY + 1)
  47. /**
  48. * This class encapsulates the idea of a discrete, non-repeating task.
  49. * Create a WorkItem when there is something you need to do on a different
  50. * Thread, but doesn't have to happen periodically. This is a great
  51. * construct for one off fire and forget tasks.
  52. *
  53. * This is an abstract base class.
  54. * To use this, you need to subclass it. All of your WorkItems should
  55. * be derived from this class. Then implement the virtual Run
  56. * function. This is a similar design to Java threading.
  57. */
  58. class WorkItem {
  59. /////////////////////////////////////////////////////////////////////////
  60. //
  61. // Public API
  62. //
  63. /////////////////////////////////////////////////////////////////////////
  64. public:
  65. /**
  66. * Our constructor.
  67. *
  68. * @param freeAfterComplete If you pass in a true, you are
  69. * requesing the WorkQueue itself to delete this WorkItem after
  70. * it has run it.
  71. * @note Only set freeAfterComplete = true if:
  72. * 1) You dynamically allocated it (i.e. used "new")
  73. * 2) After you call QueueWork() you promise never to touch
  74. * this object again.
  75. */
  76. WorkItem(bool freeAfterComplete = false);
  77. /**
  78. * Our destructor.
  79. */
  80. virtual ~WorkItem();
  81. /**
  82. * Allows a client to decide if this WorkItem is marked
  83. * for automatic deletion.
  84. */
  85. bool FreeAfterRun();
  86. /**
  87. * Implementation of your actual WorkItem function.
  88. * You must override this function.
  89. */
  90. virtual void Run() = 0;
  91. /////////////////////////////////////////////////////////////////////////
  92. //
  93. // Private API
  94. // The internals of this wrapper class.
  95. //
  96. /////////////////////////////////////////////////////////////////////////
  97. private:
  98. /**
  99. * Designates whether this WorkItem should be deleted
  100. * after the WorkQueue has run it.
  101. */
  102. const bool FreeItemAfterCompleted;
  103. };
  104. /**
  105. * This class is the "engine" for WorkItems. Create one or more WorkQueues
  106. * to accept WorkItems. WorkQueues pull WorkItems off of a FIFO queue and
  107. * run them sequentially.
  108. */
  109. class WorkQueue {
  110. /////////////////////////////////////////////////////////////////////////
  111. //
  112. // Public API
  113. //
  114. /////////////////////////////////////////////////////////////////////////
  115. public:
  116. /**
  117. * Constructor to create a named WorkQueue.
  118. *
  119. * @throws ThreadCreateException, QueueCreateException,
  120. * SemaphoreCreateException
  121. * @param Name Name of the thread internal to the WorkQueue.
  122. * Only useful for debugging.
  123. * @param StackDepth Number of "words" allocated for the Thread stack.
  124. * @param Priority FreeRTOS priority of this Thread.
  125. * @param MaxWorkItems Maximum number of WorkItems this WorkQueue can hold.
  126. */
  127. WorkQueue( const char * const Name,
  128. uint16_t StackDepth = DEFAULT_WORK_QUEUE_STACK_SIZE,
  129. UBaseType_t Priority = DEFAULT_WORK_QUEUE_PRIORITY,
  130. UBaseType_t MaxWorkItems = DEFAULT_MAX_WORK_ITEMS);
  131. /**
  132. * Constructor to create an unnamed WorkQueue.
  133. *
  134. * @throws ThreadCreateException, QueueCreateException,
  135. * SemaphoreCreateException
  136. * @param StackDepth Number of "words" allocated for the Thread stack.
  137. * @param Priority FreeRTOS priority of this Thread.
  138. * @param MaxWorkItems Maximum number of WorkItems this WorkQueue can hold.
  139. */
  140. WorkQueue( uint16_t StackDepth = DEFAULT_WORK_QUEUE_STACK_SIZE,
  141. UBaseType_t Priority = DEFAULT_WORK_QUEUE_PRIORITY,
  142. UBaseType_t MaxWorkItems = DEFAULT_MAX_WORK_ITEMS);
  143. #if (INCLUDE_vTaskDelete == 1)
  144. /**
  145. * Our destructor.
  146. *
  147. * @note Given the multithreaded nature of this class, the dtor
  148. * may block until the underlying Thread has had a chance to
  149. * clean up.
  150. */
  151. ~WorkQueue();
  152. #else
  153. //
  154. // If we are using C++11 or later, take advantage of the
  155. // newer features to find bugs.
  156. //
  157. #if __cplusplus >= 201103L
  158. /**
  159. * If we can't delete a task, it makes no sense to have a
  160. * destructor.
  161. */
  162. ~WorkQueue() = delete;
  163. #endif
  164. #endif
  165. /**
  166. * Send a WorkItem off to be executed.
  167. *
  168. * @param work Pointer to a WorkItem.
  169. * @return true if it was queued, false otherwise.
  170. * @note This function may block if the WorkQueue is presently full.
  171. */
  172. bool QueueWork(WorkItem *work);
  173. /////////////////////////////////////////////////////////////////////////
  174. //
  175. // Private API
  176. // The internals of this class.
  177. //
  178. /////////////////////////////////////////////////////////////////////////
  179. private:
  180. /**
  181. * An internal derived Thread class, in which we do our real work.
  182. */
  183. class CWorkerThread : public Thread {
  184. public:
  185. CWorkerThread( const char * const Name,
  186. uint16_t StackDepth,
  187. UBaseType_t Priority,
  188. WorkQueue *Parent);
  189. CWorkerThread( uint16_t StackDepth,
  190. UBaseType_t Priority,
  191. WorkQueue *Parent);
  192. virtual ~CWorkerThread();
  193. protected:
  194. virtual void Run();
  195. private:
  196. const WorkQueue *ParentWorkQueue;
  197. };
  198. /**
  199. * Pointer to our WorkerThread.
  200. */
  201. CWorkerThread *WorkerThread;
  202. /**
  203. * Pointer to our work queue itself.
  204. */
  205. Queue *WorkItemQueue;
  206. /**
  207. * Semaphore to support deconstruction without race conditions.
  208. */
  209. BinarySemaphore *ThreadComplete;
  210. };
  211. }
  212. #endif