WorkQueue.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "ProcessUtils.h"
  25. #include "Thread.h"
  26. #include "Timer.h"
  27. #include "WorkQueue.h"
  28. /// Worker thread managed by the work queue.
  29. class WorkerThread : public Thread, public RefCounted
  30. {
  31. public:
  32. /// Construct.
  33. WorkerThread(WorkQueue* owner, unsigned index) :
  34. owner_(owner),
  35. index_(index)
  36. {
  37. }
  38. /// Process work items until stopped.
  39. virtual void ThreadFunction()
  40. {
  41. // Init FPU state first
  42. InitFPU();
  43. owner_->ProcessItems(index_);
  44. }
  45. /// Return thread index.
  46. unsigned GetIndex() const { return index_; }
  47. private:
  48. /// Work queue.
  49. WorkQueue* owner_;
  50. /// Thread index.
  51. unsigned index_;
  52. };
  53. OBJECTTYPESTATIC(WorkQueue);
  54. WorkQueue::WorkQueue(Context* context) :
  55. Object(context),
  56. numActive_(0),
  57. shutDown_(false),
  58. pausing_(false),
  59. paused_(false)
  60. {
  61. }
  62. WorkQueue::~WorkQueue()
  63. {
  64. // Stop the worker threads. First make sure they are not waiting for work items
  65. shutDown_ = true;
  66. Resume();
  67. for (unsigned i = 0; i < threads_.Size(); ++i)
  68. threads_[i]->Stop();
  69. }
  70. void WorkQueue::CreateThreads(unsigned numThreads)
  71. {
  72. // Other subsystems may initialize themselves according to the number of threads.
  73. // Therefore allow creating the threads only once, after which the amount is fixed
  74. if (!threads_.Empty())
  75. return;
  76. // Start threads in paused mode
  77. Pause();
  78. for (unsigned i = 0; i < numThreads; ++i)
  79. {
  80. SharedPtr<WorkerThread> thread(new WorkerThread(this, i + 1));
  81. thread->Start();
  82. threads_.Push(thread);
  83. }
  84. }
  85. void WorkQueue::AddWorkItem(const WorkItem& item)
  86. {
  87. if (threads_.Size())
  88. {
  89. if (paused_)
  90. {
  91. queue_.Push(item);
  92. queueMutex_.Release();
  93. paused_ = false;
  94. }
  95. else
  96. {
  97. queueMutex_.Acquire();
  98. queue_.Push(item);
  99. queueMutex_.Release();
  100. }
  101. }
  102. else
  103. item.workFunction_(&item, 0);
  104. }
  105. void WorkQueue::Pause()
  106. {
  107. if (!paused_)
  108. {
  109. pausing_ = true;
  110. queueMutex_.Acquire();
  111. paused_ = true;
  112. pausing_ = false;
  113. }
  114. }
  115. void WorkQueue::Resume()
  116. {
  117. if (paused_)
  118. {
  119. queueMutex_.Release();
  120. paused_ = false;
  121. }
  122. }
  123. void WorkQueue::Complete()
  124. {
  125. if (threads_.Size())
  126. {
  127. Resume();
  128. // Take work items in the main thread until queue empty
  129. while (!queue_.Empty())
  130. {
  131. queueMutex_.Acquire();
  132. if (!queue_.Empty())
  133. {
  134. WorkItem item = queue_.Front();
  135. queue_.PopFront();
  136. queueMutex_.Release();
  137. item.workFunction_(&item, 0);
  138. }
  139. else
  140. queueMutex_.Release();
  141. }
  142. // Wait for all work to finish
  143. while (!IsCompleted())
  144. {
  145. }
  146. // Pause worker threads by leaving the mutex locked
  147. Pause();
  148. }
  149. }
  150. bool WorkQueue::IsCompleted() const
  151. {
  152. if (threads_.Size())
  153. return !numActive_ && queue_.Empty();
  154. else
  155. return true;
  156. }
  157. void WorkQueue::ProcessItems(unsigned threadIndex)
  158. {
  159. bool wasActive = false;
  160. for (;;)
  161. {
  162. if (shutDown_)
  163. return;
  164. if (pausing_ && !wasActive)
  165. Time::Sleep(0);
  166. else
  167. {
  168. queueMutex_.Acquire();
  169. if (!queue_.Empty())
  170. {
  171. if (!wasActive)
  172. {
  173. ++numActive_;
  174. wasActive = true;
  175. }
  176. WorkItem item = queue_.Front();
  177. queue_.PopFront();
  178. queueMutex_.Release();
  179. item.workFunction_(&item, threadIndex);
  180. }
  181. else
  182. {
  183. if (wasActive)
  184. {
  185. --numActive_;
  186. wasActive = false;
  187. }
  188. queueMutex_.Release();
  189. Time::Sleep(0);
  190. }
  191. }
  192. }
  193. }