WorkQueue.cpp 5.4 KB

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