cworkqueue.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include "workqueue.hpp"
  39. using namespace cpp_freertos;
  40. WorkItem::WorkItem(bool freeAfterComplete)
  41. : FreeItemAfterCompleted(freeAfterComplete)
  42. {
  43. }
  44. WorkItem::~WorkItem()
  45. {
  46. }
  47. bool WorkItem::FreeAfterRun()
  48. {
  49. return FreeItemAfterCompleted;
  50. }
  51. WorkQueue::WorkQueue( const char * const Name,
  52. uint16_t StackDepth,
  53. UBaseType_t Priority,
  54. UBaseType_t maxWorkItems)
  55. {
  56. //
  57. // Build the Queue first, since the Thread is going to access
  58. // it as soon as it can, maybe before we leave this ctor.
  59. //
  60. WorkItemQueue = new Queue(maxWorkItems, sizeof(WorkItem *));
  61. ThreadComplete = new BinarySemaphore();
  62. WorkerThread = new CWorkerThread(Name, StackDepth, Priority, this);
  63. //
  64. // Our ctor chain is complete, we can start.
  65. //
  66. WorkerThread->Start();
  67. }
  68. WorkQueue::WorkQueue( uint16_t StackDepth,
  69. UBaseType_t Priority,
  70. UBaseType_t maxWorkItems)
  71. {
  72. //
  73. // Build the Queue first, since the Thread is going to access
  74. // it as soon as it can, maybe before we leave this ctor.
  75. //
  76. WorkItemQueue = new Queue(maxWorkItems, sizeof(WorkItem *));
  77. ThreadComplete = new BinarySemaphore();
  78. WorkerThread = new CWorkerThread(StackDepth, Priority, this);
  79. //
  80. // Our ctor chain is complete, we can start.
  81. //
  82. WorkerThread->Start();
  83. }
  84. #if (INCLUDE_vTaskDelete == 1)
  85. WorkQueue::~WorkQueue()
  86. {
  87. //
  88. // This dtor is tricky, because of the multiple objects in
  89. // play, and the multithreaded nature of this specific object.
  90. //
  91. //
  92. // Note that we cannot flush the queue. If there are items
  93. // in the queue maked freeAfterComplete, we would leak the
  94. // memory.
  95. //
  96. //
  97. // Send a message that it's time to cleanup.
  98. //
  99. WorkItem *work = NULL;
  100. WorkItemQueue->Enqueue(&work);
  101. //
  102. // Wait until the thread has run enough to signal that it's done.
  103. //
  104. ThreadComplete->Take();
  105. //
  106. // Then delete the queue and thread. Order doesn't matter here.
  107. //
  108. delete WorkItemQueue;
  109. delete WorkerThread;
  110. delete ThreadComplete;
  111. }
  112. #endif
  113. bool WorkQueue::QueueWork(WorkItem *work)
  114. {
  115. return WorkItemQueue->Enqueue(&work);
  116. }
  117. WorkQueue::CWorkerThread::CWorkerThread(const char * const Name,
  118. uint16_t StackDepth,
  119. UBaseType_t Priority,
  120. WorkQueue *Parent)
  121. : Thread(Name, StackDepth, Priority), ParentWorkQueue(Parent)
  122. {
  123. }
  124. WorkQueue::CWorkerThread::CWorkerThread(uint16_t StackDepth,
  125. UBaseType_t Priority,
  126. WorkQueue *Parent)
  127. : Thread(StackDepth, Priority), ParentWorkQueue(Parent)
  128. {
  129. }
  130. WorkQueue::CWorkerThread::~CWorkerThread()
  131. {
  132. }
  133. void WorkQueue::CWorkerThread::Run()
  134. {
  135. while (true) {
  136. WorkItem *work;
  137. //
  138. // Wait forever for work.
  139. //
  140. ParentWorkQueue->WorkItemQueue->Dequeue(&work);
  141. //
  142. // If we dequeue a NULL item, its our sign to exit.
  143. // We are being deconstructed.
  144. //
  145. if (work == NULL) {
  146. //
  147. // Exit the task loop.
  148. //
  149. break;
  150. }
  151. //
  152. // Else we have an item, run it.
  153. //
  154. work->Run();
  155. //
  156. // If this was a dynamic, fire and forget item and we were
  157. // requested to clean it up, do so.
  158. //
  159. if (work->FreeAfterRun()) {
  160. delete work;
  161. }
  162. }
  163. //
  164. // Signal the dtor that the thread is exiting.
  165. //
  166. ParentWorkQueue->ThreadComplete->Give();
  167. }