WorkQueue.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. Resume();
  60. for (unsigned i = 0; i < threads_.Size(); ++i)
  61. threads_[i]->Stop();
  62. }
  63. void WorkQueue::CreateThreads(unsigned numThreads)
  64. {
  65. // Other subsystems may initialize themselves according to the number of threads.
  66. // Therefore allow creating the threads only once, after which the amount is fixed
  67. if (!threads_.Empty())
  68. return;
  69. // Start threads in paused mode
  70. Pause();
  71. for (unsigned i = 0; i < numThreads; ++i)
  72. {
  73. SharedPtr<WorkerThread> thread(new WorkerThread(this, i + 1));
  74. thread->Start();
  75. threads_.Push(thread);
  76. }
  77. }
  78. void WorkQueue::AddWorkItem(const WorkItem& item)
  79. {
  80. if (threads_.Size())
  81. {
  82. if (paused_)
  83. {
  84. queue_.Push(item);
  85. queueMutex_.Release();
  86. paused_ = false;
  87. }
  88. else
  89. {
  90. queueMutex_.Acquire();
  91. queue_.Push(item);
  92. queueMutex_.Release();
  93. }
  94. }
  95. else
  96. item.workFunction_(&item, 0);
  97. }
  98. void WorkQueue::Pause()
  99. {
  100. if (!paused_)
  101. {
  102. queueMutex_.Acquire();
  103. paused_ = true;
  104. }
  105. }
  106. void WorkQueue::Resume()
  107. {
  108. if (paused_)
  109. {
  110. queueMutex_.Release();
  111. paused_ = false;
  112. }
  113. }
  114. void WorkQueue::Complete()
  115. {
  116. if (threads_.Size())
  117. {
  118. Resume();
  119. for (;;)
  120. {
  121. queueMutex_.Acquire();
  122. if (!queue_.Empty())
  123. {
  124. WorkItem item = queue_.Front();
  125. queue_.PopFront();
  126. queueMutex_.Release();
  127. item.workFunction_(&item, 0);
  128. }
  129. else
  130. {
  131. if (numActive_)
  132. queueMutex_.Release();
  133. else
  134. {
  135. // All work items are done. Leave the mutex locked and re-enter pause mode
  136. paused_ = true;
  137. return;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. bool WorkQueue::IsCompleted() const
  144. {
  145. if (threads_.Size())
  146. return !numActive_ && queue_.Empty();
  147. else
  148. return true;
  149. }
  150. void WorkQueue::ProcessItems(unsigned threadIndex)
  151. {
  152. bool wasActive = false;
  153. for (;;)
  154. {
  155. if (shutDown_)
  156. return;
  157. queueMutex_.Acquire();
  158. if (!queue_.Empty())
  159. {
  160. if (!wasActive)
  161. {
  162. ++numActive_;
  163. wasActive = true;
  164. }
  165. WorkItem item = queue_.Front();
  166. queue_.PopFront();
  167. queueMutex_.Release();
  168. item.workFunction_(&item, threadIndex);
  169. }
  170. else
  171. {
  172. if (wasActive)
  173. {
  174. --numActive_;
  175. wasActive = false;
  176. }
  177. queueMutex_.Release();
  178. Time::Sleep(0);
  179. }
  180. }
  181. }