WorkQueue.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Thread.h"
  25. #include "Timer.h"
  26. #include "WorkQueue.h"
  27. /// Worker thread managed by the work queue.
  28. class WorkerThread : public Thread, public RefCounted
  29. {
  30. public:
  31. /// Construct.
  32. WorkerThread(WorkQueue* owner, unsigned index) :
  33. owner_(owner),
  34. index_(index)
  35. {
  36. }
  37. /// Process work items until stopped.
  38. virtual void ThreadFunction() { owner_->ProcessItems(index_); }
  39. /// Return thread index.
  40. unsigned GetIndex() const { return index_; }
  41. private:
  42. /// Work queue.
  43. WorkQueue* owner_;
  44. /// Thread index.
  45. unsigned index_;
  46. };
  47. OBJECTTYPESTATIC(WorkQueue);
  48. WorkQueue::WorkQueue(Context* context) :
  49. Object(context),
  50. numActive_(0),
  51. shutDown_(false),
  52. paused_(false)
  53. {
  54. }
  55. WorkQueue::~WorkQueue()
  56. {
  57. // Stop the worker threads. First make sure they are not waiting for work items
  58. shutDown_ = true;
  59. if (paused_)
  60. {
  61. queueMutex_.Release();
  62. paused_ = false;
  63. }
  64. for (unsigned i = 0; i < threads_.Size(); ++i)
  65. threads_[i]->Stop();
  66. }
  67. void WorkQueue::CreateThreads(unsigned numThreads)
  68. {
  69. // Other subsystems may initialize themselves according to the number of threads.
  70. // Therefore allow creating the threads only once, after which the amount is fixed
  71. if (!threads_.Empty())
  72. return;
  73. // Start threads in paused mode
  74. Pause();
  75. for (unsigned i = 0; i < numThreads; ++i)
  76. {
  77. SharedPtr<WorkerThread> thread(new WorkerThread(this, i + 1));
  78. thread->Start();
  79. threads_.Push(thread);
  80. }
  81. }
  82. void WorkQueue::AddWorkItem(const WorkItem& item)
  83. {
  84. if (threads_.Size())
  85. {
  86. if (paused_)
  87. {
  88. queue_.Push(item);
  89. queueMutex_.Release();
  90. paused_ = false;
  91. }
  92. else
  93. {
  94. queueMutex_.Acquire();
  95. queue_.Push(item);
  96. queueMutex_.Release();
  97. }
  98. }
  99. else
  100. item.workFunction_(&item, 0);
  101. }
  102. void WorkQueue::Pause()
  103. {
  104. if (!paused_)
  105. {
  106. queueMutex_.Acquire();
  107. paused_ = true;
  108. }
  109. }
  110. void WorkQueue::Resume()
  111. {
  112. if (paused_)
  113. {
  114. queueMutex_.Release();
  115. paused_ = false;
  116. }
  117. }
  118. void WorkQueue::Complete()
  119. {
  120. if (threads_.Size())
  121. {
  122. Resume();
  123. for (;;)
  124. {
  125. queueMutex_.Acquire();
  126. if (!queue_.Empty())
  127. {
  128. WorkItem item = queue_.Front();
  129. queue_.PopFront();
  130. queueMutex_.Release();
  131. item.workFunction_(&item, 0);
  132. }
  133. else
  134. {
  135. if (numActive_)
  136. queueMutex_.Release();
  137. else
  138. {
  139. // All work items are done. Leave the mutex locked and re-enter pause mode
  140. paused_ = true;
  141. return;
  142. }
  143. }
  144. }
  145. }
  146. }
  147. bool WorkQueue::IsCompleted()
  148. {
  149. if (threads_.Size())
  150. return !numActive_ && queue_.Empty();
  151. else
  152. return true;
  153. }
  154. void WorkQueue::ProcessItems(unsigned threadIndex)
  155. {
  156. bool wasActive = false;
  157. for (;;)
  158. {
  159. if (shutDown_)
  160. return;
  161. if (queue_.Empty())
  162. Time::Sleep(0);
  163. queueMutex_.Acquire();
  164. if (!queue_.Empty())
  165. {
  166. if (!wasActive)
  167. {
  168. ++numActive_;
  169. wasActive = true;
  170. }
  171. WorkItem item = queue_.Front();
  172. queue_.PopFront();
  173. queueMutex_.Release();
  174. item.workFunction_(&item, threadIndex);
  175. }
  176. else
  177. {
  178. if (wasActive)
  179. {
  180. --numActive_;
  181. wasActive = false;
  182. }
  183. queueMutex_.Release();
  184. }
  185. }
  186. }