WorkQueue.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "ProcessUtils.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. /// Work queue implementation.
  33. class WorkQueueImpl
  34. {
  35. public:
  36. /// Construct.
  37. WorkQueueImpl()
  38. {
  39. #ifdef WIN32
  40. eventHandle_ = CreateEvent(0, FALSE, FALSE, 0);
  41. #else
  42. pthread_cond_init(&condition_, 0);
  43. pthread_mutex_init(&mutex_, 0);
  44. #endif
  45. }
  46. /// Destruct.
  47. ~WorkQueueImpl()
  48. {
  49. #ifdef WIN32
  50. CloseHandle(eventHandle_);
  51. #else
  52. pthread_cond_destroy(&condition_);
  53. pthread_mutex_destroy(&mutex_);
  54. #endif
  55. }
  56. /// Signal one worker thread for available work.
  57. void Signal()
  58. {
  59. #ifdef WIN32
  60. SetEvent(eventHandle_);
  61. #else
  62. pthread_cond_signal(&condition_);
  63. #endif
  64. }
  65. /// Wait for available work.
  66. void Wait()
  67. {
  68. #ifdef WIN32
  69. WaitForSingleObject(eventHandle_, INFINITE);
  70. #else
  71. pthread_mutex_lock(&mutex_);
  72. pthread_cond_wait(&condition_, &mutex_);
  73. pthread_mutex_unlock(&mutex_);
  74. #endif
  75. }
  76. private:
  77. #ifdef WIN32
  78. /// Event for signaling the worker threads.
  79. HANDLE eventHandle_;
  80. #else
  81. /// Condition variable for signaling the worker threads.
  82. pthread_cond_t condition_;
  83. /// Mutex for the condition variable.
  84. pthread_mutex_t mutex_;
  85. #endif
  86. };
  87. OBJECTTYPESTATIC(WorkQueue);
  88. WorkQueue::WorkQueue(Context* context) :
  89. Object(context),
  90. impl_(new WorkQueueImpl()),
  91. shutDown_(false),
  92. numWaiting_(0)
  93. {
  94. // Create worker threads and start them. Leave one core free for the main thread, and another for GPU & audio.
  95. int numCores = GetNumCPUCores();
  96. if (numCores == 1)
  97. return;
  98. int numThreads = Clamp(numCores - 2, 1, 4);
  99. for (int i = 0; i < numThreads; ++i)
  100. {
  101. SharedPtr<WorkerThread> thread(new WorkerThread(this, i + 1));
  102. thread->Start();
  103. threads_.Push(thread);
  104. }
  105. }
  106. WorkQueue::~WorkQueue()
  107. {
  108. // Stop the worker threads. First make sure they are not waiting for work items.
  109. if (!threads_.Empty())
  110. {
  111. shutDown_ = true;
  112. for (unsigned i = 0; i < threads_.Size(); ++i)
  113. impl_->Signal();
  114. for (unsigned i = 0; i < threads_.Size(); ++i)
  115. threads_[i]->Stop();
  116. }
  117. delete impl_;
  118. impl_ = 0;
  119. }
  120. void WorkQueue::AddWorkItem(const WorkItem& item)
  121. {
  122. if (threads_.Size())
  123. {
  124. queueLock_.Acquire();
  125. queue_.Push(item);
  126. bool isWaiting = numWaiting_ > 0;
  127. queueLock_.Release();
  128. if (isWaiting)
  129. impl_->Signal();
  130. }
  131. else
  132. item.workFunction_(&item, 0);
  133. }
  134. void WorkQueue::Complete()
  135. {
  136. if (threads_.Size())
  137. {
  138. // Wait for work to finish while also taking work items in the main thread
  139. for (;;)
  140. {
  141. queueLock_.Acquire();
  142. if (!queue_.Empty())
  143. {
  144. WorkItem item = queue_.Front();
  145. queue_.PopFront();
  146. queueLock_.Release();
  147. item.workFunction_(&item, 0);
  148. }
  149. else
  150. {
  151. queueLock_.Release();
  152. if (numWaiting_ == threads_.Size())
  153. break;
  154. }
  155. }
  156. }
  157. }
  158. bool WorkQueue::IsCompleted()
  159. {
  160. if (threads_.Size())
  161. {
  162. queueLock_.Acquire();
  163. bool completed = queue_.Empty() && numWaiting_ == threads_.Size();
  164. queueLock_.Release();
  165. return completed;
  166. }
  167. else
  168. return true;
  169. }
  170. void WorkQueue::ProcessItems(unsigned threadIndex)
  171. {
  172. bool wasWaiting = false;
  173. for (;;)
  174. {
  175. if (shutDown_)
  176. return;
  177. queueLock_.Acquire();
  178. if (wasWaiting)
  179. {
  180. --numWaiting_;
  181. wasWaiting = false;
  182. }
  183. if (!queue_.Empty())
  184. {
  185. WorkItem item = queue_.Front();
  186. queue_.PopFront();
  187. queueLock_.Release();
  188. item.workFunction_(&item, threadIndex);
  189. }
  190. else
  191. {
  192. ++numWaiting_;
  193. queueLock_.Release();
  194. impl_->Wait();
  195. wasWaiting = true;
  196. }
  197. }
  198. }