WorkQueue.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. started_(false),
  93. shutDown_(false),
  94. numWaiting_(0)
  95. {
  96. // Create worker threads and start them. Leave one core free for the main thread
  97. unsigned numCores = GetNumCPUCores();
  98. for (unsigned i = 1; i < numCores; ++i)
  99. {
  100. SharedPtr<WorkerThread> thread(new WorkerThread(this, i));
  101. thread->Start();
  102. threads_.Push(thread);
  103. }
  104. }
  105. WorkQueue::~WorkQueue()
  106. {
  107. // Stop the worker threads. First make sure they are not waiting for work items.
  108. if (!threads_.Empty())
  109. {
  110. shutDown_ = true;
  111. started_ = false;
  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(WorkItem* item)
  121. {
  122. if (!started_)
  123. queue_.Push(item);
  124. else
  125. {
  126. queueLock_.Acquire();
  127. queue_.Push(item);
  128. bool isWaiting = numWaiting_ > 0;
  129. queueLock_.Release();
  130. if (isWaiting)
  131. impl_->Signal();
  132. }
  133. }
  134. void WorkQueue::Start()
  135. {
  136. if (!threads_.Empty() && !started_)
  137. {
  138. started_ = true;
  139. unsigned numSignals = numWaiting_;
  140. for (unsigned i = 0; i < numSignals; ++i)
  141. impl_->Signal();
  142. }
  143. }
  144. void WorkQueue::Finish()
  145. {
  146. if (threads_.Empty())
  147. {
  148. // If no worker threads, do all work now in the main thread
  149. for (List<WorkItem*>::Iterator i = queue_.Begin(); i != queue_.End(); ++i)
  150. (*i)->Process(0);
  151. queue_.Clear();
  152. }
  153. else
  154. {
  155. // Wait for work to finish while also taking work items in the main thread
  156. for (;;)
  157. {
  158. WorkItem* item = 0;
  159. queueLock_.Acquire();
  160. if (!queue_.Empty())
  161. {
  162. item = queue_.Front();
  163. queue_.PopFront();
  164. }
  165. queueLock_.Release();
  166. if (item)
  167. item->Process(0);
  168. else if (numWaiting_ == threads_.Size())
  169. break;
  170. }
  171. }
  172. }
  173. void WorkQueue::FinishAndStop()
  174. {
  175. Finish();
  176. started_ = false;
  177. }
  178. bool WorkQueue::IsCompleted()
  179. {
  180. queueLock_.Acquire();
  181. bool completed = queue_.Empty() && numWaiting_ == threads_.Size();
  182. queueLock_.Release();
  183. return completed;
  184. }
  185. WorkItem* WorkQueue::GetNextWorkItem()
  186. {
  187. bool wasWaiting = false;
  188. for (;;)
  189. {
  190. if (shutDown_)
  191. return 0;
  192. WorkItem* item = 0;
  193. queueLock_.Acquire();
  194. if (wasWaiting)
  195. {
  196. --numWaiting_;
  197. wasWaiting = false;
  198. }
  199. if (started_ && !queue_.Empty())
  200. {
  201. item = queue_.Front();
  202. queue_.PopFront();
  203. }
  204. if (item)
  205. {
  206. queueLock_.Release();
  207. return item;
  208. }
  209. else
  210. {
  211. ++numWaiting_;
  212. queueLock_.Release();
  213. impl_->Wait();
  214. wasWaiting = true;
  215. }
  216. }
  217. }