WorkQueue.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_cond_wait(&condition_, &mutex_);
  73. #endif
  74. }
  75. private:
  76. #ifdef WIN32
  77. /// Event for signaling the worker threads.
  78. HANDLE eventHandle_;
  79. #else
  80. /// Condition variable for signaling the worker threads.
  81. pthread_cond_t condition_;
  82. /// Mutex for the condition variable.
  83. pthread_mutex_t mutex_;
  84. #endif
  85. };
  86. OBJECTTYPESTATIC(WorkQueue);
  87. WorkQueue::WorkQueue(Context* context) :
  88. Object(context),
  89. impl_(new WorkQueueImpl()),
  90. started_(false),
  91. shutDown_(false)
  92. {
  93. // Create worker threads and start them
  94. unsigned numCores = GetNumCPUCores();
  95. for (unsigned i = 1; i < numCores; ++i)
  96. {
  97. SharedPtr<WorkerThread> thread(new WorkerThread(this, i));
  98. thread->Start();
  99. threads_.Push(thread);
  100. }
  101. }
  102. WorkQueue::~WorkQueue()
  103. {
  104. // Stop the worker threads. First make sure they are not waiting for work items.
  105. if (!threads_.Empty())
  106. {
  107. shutDown_ = true;
  108. started_ = false;
  109. for (unsigned i = 0; i < threads_.Size(); ++i)
  110. impl_->Signal();
  111. for (unsigned i = 0; i < threads_.Size(); ++i)
  112. threads_[i]->Stop();
  113. }
  114. delete impl_;
  115. impl_ = 0;
  116. }
  117. void WorkQueue::AddWorkItem(WorkItem* item)
  118. {
  119. if (!started_)
  120. queue_.Push(item);
  121. else
  122. {
  123. queueLock_.Acquire();
  124. queue_.Push(item);
  125. queueLock_.Release();
  126. impl_->Signal();
  127. }
  128. }
  129. void WorkQueue::Start()
  130. {
  131. if (!threads_.Empty() && !started_)
  132. {
  133. started_ = true;
  134. for (unsigned i = 0; i < threads_.Size(); ++i)
  135. impl_->Signal();
  136. }
  137. }
  138. void WorkQueue::Finish()
  139. {
  140. if (threads_.Empty())
  141. {
  142. // If no worker threads, do all work now in the main thread
  143. for (List<WorkItem*>::Iterator i = queue_.Begin(); i != queue_.End(); ++i)
  144. (*i)->Process(0);
  145. queue_.Clear();
  146. }
  147. else
  148. {
  149. // Wait for work to finish while also taking work items in the main thread
  150. for (;;)
  151. {
  152. WorkItem* item = 0;
  153. bool allIdle = false;
  154. queueLock_.Acquire();
  155. if (!queue_.Empty())
  156. {
  157. item = queue_.Front();
  158. queue_.PopFront();
  159. }
  160. else
  161. allIdle = CheckIdle();
  162. queueLock_.Release();
  163. if (item)
  164. item->Process(0);
  165. else if (allIdle)
  166. break;
  167. }
  168. }
  169. }
  170. void WorkQueue::FinishAndStop()
  171. {
  172. Finish();
  173. started_ = false;
  174. }
  175. bool WorkQueue::IsCompleted()
  176. {
  177. queueLock_.Acquire();
  178. bool queueEmpty = queue_.Empty();
  179. bool allIdle = false;
  180. if (queueEmpty)
  181. allIdle = CheckIdle();
  182. queueLock_.Release();
  183. return queueEmpty && allIdle;
  184. }
  185. WorkItem* WorkQueue::GetNextWorkItem(WorkerThread* thread)
  186. {
  187. for (;;)
  188. {
  189. if (shutDown_)
  190. return 0;
  191. WorkItem* item = 0;
  192. queueLock_.Acquire();
  193. if (started_ && !queue_.Empty())
  194. {
  195. item = queue_.Front();
  196. queue_.PopFront();
  197. thread->SetWorking(true);
  198. }
  199. queueLock_.Release();
  200. if (item)
  201. return item;
  202. else
  203. {
  204. thread->SetWorking(false);
  205. impl_->Wait();
  206. }
  207. }
  208. }
  209. bool WorkQueue::CheckIdle()
  210. {
  211. bool allIdle = true;
  212. for (unsigned i = 0; i < threads_.Size(); ++i)
  213. {
  214. if (threads_[i]->IsWorking())
  215. {
  216. allIdle = false;
  217. break;
  218. }
  219. }
  220. return allIdle;
  221. }