CmCoreThread.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "CmCoreThread.h"
  2. #include "CmCoreThreadAccessor.h"
  3. namespace CamelotFramework
  4. {
  5. CoreThread::CoreThread()
  6. : mCoreThreadFunc(nullptr)
  7. , mCoreThreadStarted(false)
  8. , mCoreThreadShutdown(false)
  9. , mCommandQueue(nullptr)
  10. , mMaxCommandNotifyId(0)
  11. , mSyncedCoreAccessor(nullptr)
  12. , mActiveFrameAlloc(0)
  13. {
  14. mFrameAllocs[0] = cm_new<FrameAlloc>();
  15. mFrameAllocs[1] = cm_new<FrameAlloc>();
  16. mCoreThreadId = CM_THREAD_CURRENT_ID;
  17. mCommandQueue = cm_new<CommandQueue<CommandQueueSync>>(CM_THREAD_CURRENT_ID);
  18. initCoreThread();
  19. }
  20. CoreThread::~CoreThread()
  21. {
  22. // TODO - What if something gets queued between the queued call to destroy_internal and this!?
  23. shutdownCoreThread();
  24. if(mCommandQueue != nullptr)
  25. {
  26. cm_delete(mCommandQueue);
  27. mCommandQueue = nullptr;
  28. }
  29. cm_delete(mFrameAllocs[0]);
  30. cm_delete(mFrameAllocs[1]);
  31. }
  32. void CoreThread::initCoreThread()
  33. {
  34. #if !CM_FORCE_SINGLETHREADED_RENDERING
  35. mCoreThreadFunc = cm_new<CoreThreadWorkerFunc>(this);
  36. #if CM_THREAD_SUPPORT
  37. CM_THREAD_CREATE(t, *mCoreThreadFunc);
  38. mCoreThread = t;
  39. CM_LOCK_MUTEX_NAMED(mCoreThreadStartMutex, lock);
  40. while(!mCoreThreadStarted)
  41. CM_THREAD_WAIT(mCoreThreadStartCondition, mCoreThreadStartMutex, lock);
  42. #else
  43. CM_EXCEPT(InternalErrorException, "Attempting to start a core thread but Camelot isn't compiled with thread support.");
  44. #endif
  45. #endif
  46. }
  47. void CoreThread::runCoreThread()
  48. {
  49. #if !CM_FORCE_SINGLETHREADED_RENDERING
  50. MemStack::beginThread();
  51. mCoreThreadId = CM_THREAD_CURRENT_ID;
  52. mSyncedCoreAccessor = cm_new<CoreThreadAccessor<CommandQueueSync>>(CM_THREAD_CURRENT_ID);
  53. {
  54. CM_LOCK_MUTEX(mCoreThreadStartMutex);
  55. mCoreThreadStarted = true;
  56. }
  57. CM_THREAD_NOTIFY_ALL(mCoreThreadStartCondition)
  58. while(true)
  59. {
  60. // Wait until we get some ready commands
  61. Queue<QueuedCommand>::type* commands = nullptr;
  62. {
  63. CM_LOCK_MUTEX_NAMED(mCommandQueueMutex, lock)
  64. while(mCommandQueue->isEmpty())
  65. {
  66. if(mCoreThreadShutdown)
  67. {
  68. MemStack::endThread();
  69. return;
  70. }
  71. CM_THREAD_WAIT(mCommandReadyCondition, mCommandQueueMutex, lock);
  72. }
  73. commands = mCommandQueue->flush();
  74. }
  75. // Play commands
  76. mCommandQueue->playback(commands, boost::bind(&CoreThread::commandCompletedNotify, this, _1));
  77. }
  78. cm_delete(mSyncedCoreAccessor);
  79. MemStack::endThread();
  80. #endif
  81. }
  82. void CoreThread::shutdownCoreThread()
  83. {
  84. #if !CM_FORCE_SINGLETHREADED_RENDERING
  85. {
  86. CM_LOCK_MUTEX(mCommandQueueMutex);
  87. mCoreThreadShutdown = true;
  88. }
  89. // Wake all threads. They will quit after they see the shutdown flag
  90. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  91. mCoreThread->join();
  92. CM_THREAD_DESTROY(mCoreThread);
  93. mCoreThread = nullptr;
  94. mCoreThreadId = CM_THREAD_CURRENT_ID;
  95. if(mCoreThreadFunc != nullptr)
  96. {
  97. cm_delete(mCoreThreadFunc);
  98. mCoreThreadFunc = nullptr;
  99. }
  100. #endif
  101. mCoreThreadStarted = false;
  102. }
  103. CoreAccessorPtr CoreThread::createAccessor()
  104. {
  105. return cm_shared_ptr<CoreThreadAccessor<CommandQueueNoSync>>(CM_THREAD_CURRENT_ID);
  106. }
  107. SyncedCoreAccessor& CoreThread::getSyncedAccessor()
  108. {
  109. return *mSyncedCoreAccessor;
  110. }
  111. AsyncOp CoreThread::queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete)
  112. {
  113. AsyncOp op;
  114. if(CM_THREAD_CURRENT_ID == getCoreThreadId())
  115. {
  116. commandCallback(op); // Execute immediately
  117. return op;
  118. }
  119. UINT32 commandId = -1;
  120. {
  121. CM_LOCK_MUTEX(mCommandQueueMutex);
  122. if(blockUntilComplete)
  123. {
  124. commandId = mMaxCommandNotifyId++;
  125. op = mCommandQueue->queueReturn(commandCallback, true, commandId);
  126. }
  127. else
  128. op = mCommandQueue->queueReturn(commandCallback);
  129. }
  130. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  131. if(blockUntilComplete)
  132. blockUntilCommandCompleted(commandId);
  133. return op;
  134. }
  135. void CoreThread::queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete)
  136. {
  137. if(CM_THREAD_CURRENT_ID == getCoreThreadId())
  138. {
  139. commandCallback(); // Execute immediately
  140. return;
  141. }
  142. UINT32 commandId = -1;
  143. {
  144. CM_LOCK_MUTEX(mCommandQueueMutex);
  145. if(blockUntilComplete)
  146. {
  147. commandId = mMaxCommandNotifyId++;
  148. mCommandQueue->queue(commandCallback, true, commandId);
  149. }
  150. else
  151. mCommandQueue->queue(commandCallback);
  152. }
  153. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  154. if(blockUntilComplete)
  155. blockUntilCommandCompleted(commandId);
  156. }
  157. void CoreThread::update()
  158. {
  159. mActiveFrameAlloc = (mActiveFrameAlloc + 1) % 2;
  160. mFrameAllocs[mActiveFrameAlloc]->clear();
  161. }
  162. FrameAlloc* CoreThread::getFrameAlloc() const
  163. {
  164. return mFrameAllocs[mActiveFrameAlloc];
  165. }
  166. void CoreThread::blockUntilCommandCompleted(UINT32 commandId)
  167. {
  168. #if !CM_FORCE_SINGLETHREADED_RENDERING
  169. CM_LOCK_MUTEX_NAMED(mCommandNotifyMutex, lock);
  170. while(true)
  171. {
  172. // TODO - This might be causing a deadlock in Release mode. I'm thinking because mCommandsCompleted isn't marked as volatile.
  173. // Check if our command id is in the completed list
  174. auto iter = mCommandsCompleted.begin();
  175. for(; iter != mCommandsCompleted.end(); ++iter)
  176. {
  177. if(*iter == commandId)
  178. break;
  179. }
  180. if(iter != mCommandsCompleted.end())
  181. {
  182. mCommandsCompleted.erase(iter);
  183. break;
  184. }
  185. CM_THREAD_WAIT(mCommandCompleteCondition, mCommandNotifyMutex, lock);
  186. }
  187. #endif
  188. }
  189. void CoreThread::commandCompletedNotify(UINT32 commandId)
  190. {
  191. {
  192. CM_LOCK_MUTEX(mCommandNotifyMutex);
  193. mCommandsCompleted.push_back(commandId);
  194. }
  195. CM_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
  196. }
  197. CoreThread& gCoreThread()
  198. {
  199. return CoreThread::instance();
  200. }
  201. void throwIfNotCoreThread()
  202. {
  203. #if !CM_FORCE_SINGLETHREADED_RENDERING
  204. if(CM_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
  205. CM_EXCEPT(InternalErrorException, "This method can only be accessed from the core thread.");
  206. #endif
  207. }
  208. void throwIfCoreThread()
  209. {
  210. #if !CM_FORCE_SINGLETHREADED_RENDERING
  211. if(CM_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  212. CM_EXCEPT(InternalErrorException, "This method cannot be accessed from the core thread.");
  213. #endif
  214. }
  215. /************************************************************************/
  216. /* THREAD WORKER */
  217. /************************************************************************/
  218. CoreThread::CoreThreadWorkerFunc::CoreThreadWorkerFunc(CoreThread* owner)
  219. :mOwner(owner)
  220. {
  221. assert(mOwner != nullptr);
  222. }
  223. void CoreThread::CoreThreadWorkerFunc::operator()()
  224. {
  225. mOwner->runCoreThread();
  226. }
  227. }