CmCoreThread.cpp 7.5 KB

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