WorkQueue.cpp 5.4 KB

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