WorkQueue.cpp 5.6 KB

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