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 "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. For now use a maximum of 4 worker threads, and leave cores free for the main thread
  95. // and the GPU driver + audio processing
  96. int numCores = GetNumCPUCores();
  97. if (numCores == 1)
  98. return;
  99. int numThreads = Clamp(numCores - 2, 1, 4);
  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. queueMutex_.Acquire();
  126. queue_.Push(item);
  127. bool isWaiting = numWaiting_ > 0;
  128. queueMutex_.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. queueMutex_.Acquire();
  143. if (!queue_.Empty())
  144. {
  145. WorkItem item = queue_.Front();
  146. queue_.PopFront();
  147. queueMutex_.Release();
  148. item.workFunction_(&item, 0);
  149. }
  150. else
  151. {
  152. queueMutex_.Release();
  153. if (numWaiting_ == threads_.Size())
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. bool WorkQueue::IsCompleted()
  160. {
  161. if (threads_.Size())
  162. {
  163. queueMutex_.Acquire();
  164. bool completed = queue_.Empty() && numWaiting_ == threads_.Size();
  165. queueMutex_.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. queueMutex_.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. queueMutex_.Release();
  189. item.workFunction_(&item, threadIndex);
  190. }
  191. else
  192. {
  193. ++numWaiting_;
  194. queueMutex_.Release();
  195. impl_->Wait();
  196. wasWaiting = true;
  197. }
  198. }
  199. }