CmCoreThread.cpp 7.7 KB

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