WorkQueue.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 "Timer.h"
  25. #include "WorkerThread.h"
  26. #include "WorkQueue.h"
  27. #ifdef WIN32
  28. #include <windows.h>
  29. #else
  30. #include <pthread.h>
  31. #endif
  32. OBJECTTYPESTATIC(WorkQueue);
  33. WorkQueue::WorkQueue(Context* context) :
  34. Object(context),
  35. numActive_(0),
  36. shutDown_(false),
  37. paused_(false)
  38. {
  39. }
  40. WorkQueue::~WorkQueue()
  41. {
  42. // Stop the worker threads. First make sure they are not waiting for work items
  43. shutDown_ = true;
  44. if (paused_)
  45. {
  46. queueMutex_.Release();
  47. paused_ = false;
  48. }
  49. for (unsigned i = 0; i < threads_.Size(); ++i)
  50. threads_[i]->Stop();
  51. }
  52. void WorkQueue::CreateThreads(unsigned numThreads)
  53. {
  54. // Other subsystems may initialize themselves according to the number of threads.
  55. // Therefore allow creating the threads only once, after which the amount is fixed
  56. if (!threads_.Empty())
  57. return;
  58. // Start threads in paused mode
  59. Pause();
  60. for (unsigned i = 0; i < numThreads; ++i)
  61. {
  62. SharedPtr<WorkerThread> thread(new WorkerThread(this, i + 1));
  63. thread->Start();
  64. threads_.Push(thread);
  65. }
  66. }
  67. void WorkQueue::AddWorkItem(const WorkItem& item)
  68. {
  69. if (threads_.Size())
  70. {
  71. if (paused_)
  72. {
  73. queue_.Push(item);
  74. queueMutex_.Release();
  75. paused_ = false;
  76. }
  77. else
  78. {
  79. queueMutex_.Acquire();
  80. queue_.Push(item);
  81. queueMutex_.Release();
  82. }
  83. }
  84. else
  85. item.workFunction_(&item, 0);
  86. }
  87. void WorkQueue::Pause()
  88. {
  89. if (!paused_)
  90. {
  91. queueMutex_.Acquire();
  92. paused_ = true;
  93. }
  94. }
  95. void WorkQueue::Resume()
  96. {
  97. if (paused_)
  98. {
  99. queueMutex_.Release();
  100. paused_ = false;
  101. }
  102. }
  103. void WorkQueue::Complete()
  104. {
  105. if (threads_.Size())
  106. {
  107. Resume();
  108. for (;;)
  109. {
  110. queueMutex_.Acquire();
  111. if (!queue_.Empty())
  112. {
  113. WorkItem item = queue_.Front();
  114. queue_.PopFront();
  115. queueMutex_.Release();
  116. item.workFunction_(&item, 0);
  117. }
  118. else
  119. {
  120. if (numActive_)
  121. queueMutex_.Release();
  122. else
  123. {
  124. // All work items are done. Leave the mutex locked and re-enter pause mode
  125. paused_ = true;
  126. return;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. bool WorkQueue::IsCompleted()
  133. {
  134. if (threads_.Size())
  135. return !numActive_ && queue_.Empty();
  136. else
  137. return true;
  138. }
  139. void WorkQueue::ProcessItems(unsigned threadIndex)
  140. {
  141. bool wasActive = false;
  142. for (;;)
  143. {
  144. if (shutDown_)
  145. return;
  146. queueMutex_.Acquire();
  147. if (!queue_.Empty())
  148. {
  149. if (!wasActive)
  150. {
  151. ++numActive_;
  152. wasActive = true;
  153. }
  154. WorkItem item = queue_.Front();
  155. queue_.PopFront();
  156. queueMutex_.Release();
  157. item.workFunction_(&item, threadIndex);
  158. }
  159. else
  160. {
  161. if (wasActive)
  162. {
  163. --numActive_;
  164. wasActive = false;
  165. }
  166. queueMutex_.Release();
  167. // When transitioning from working to waiting, give up the time slice so that it is easier
  168. // for the main thread to re-lock the mutex
  169. Time::Sleep(0);
  170. }
  171. }
  172. }