CmCoreThread.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. AsyncOp CoreThread::queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete)
  129. {
  130. AsyncOp op;
  131. if(CM_THREAD_CURRENT_ID == getCoreThreadId())
  132. {
  133. commandCallback(op); // Execute immediately
  134. return op;
  135. }
  136. UINT32 commandId = -1;
  137. {
  138. CM_LOCK_MUTEX(mCommandQueueMutex);
  139. if(blockUntilComplete)
  140. {
  141. commandId = mMaxCommandNotifyId++;
  142. op = mCommandQueue->queueReturn(commandCallback, true, commandId);
  143. }
  144. else
  145. op = mCommandQueue->queueReturn(commandCallback);
  146. }
  147. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  148. if(blockUntilComplete)
  149. blockUntilCommandCompleted(commandId);
  150. return op;
  151. }
  152. void CoreThread::queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete)
  153. {
  154. if(CM_THREAD_CURRENT_ID == getCoreThreadId())
  155. {
  156. commandCallback(); // Execute immediately
  157. return;
  158. }
  159. UINT32 commandId = -1;
  160. {
  161. CM_LOCK_MUTEX(mCommandQueueMutex);
  162. if(blockUntilComplete)
  163. {
  164. commandId = mMaxCommandNotifyId++;
  165. mCommandQueue->queue(commandCallback, true, commandId);
  166. }
  167. else
  168. mCommandQueue->queue(commandCallback);
  169. }
  170. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  171. if(blockUntilComplete)
  172. blockUntilCommandCompleted(commandId);
  173. }
  174. void CoreThread::update()
  175. {
  176. mActiveFrameAlloc = (mActiveFrameAlloc + 1) % 2;
  177. mFrameAllocs[mActiveFrameAlloc]->clear();
  178. }
  179. FrameAlloc* CoreThread::getFrameAlloc() const
  180. {
  181. return mFrameAllocs[mActiveFrameAlloc];
  182. }
  183. void CoreThread::blockUntilCommandCompleted(UINT32 commandId)
  184. {
  185. #if !CM_FORCE_SINGLETHREADED_RENDERING
  186. CM_LOCK_MUTEX_NAMED(mCommandNotifyMutex, lock);
  187. while(true)
  188. {
  189. // TODO - This might be causing a deadlock in Release mode. I'm thinking because mCommandsCompleted isn't marked as volatile.
  190. // Check if our command id is in the completed list
  191. auto iter = mCommandsCompleted.begin();
  192. for(; iter != mCommandsCompleted.end(); ++iter)
  193. {
  194. if(*iter == commandId)
  195. break;
  196. }
  197. if(iter != mCommandsCompleted.end())
  198. {
  199. mCommandsCompleted.erase(iter);
  200. break;
  201. }
  202. CM_THREAD_WAIT(mCommandCompleteCondition, mCommandNotifyMutex, lock);
  203. }
  204. #endif
  205. }
  206. void CoreThread::commandCompletedNotify(UINT32 commandId)
  207. {
  208. {
  209. CM_LOCK_MUTEX(mCommandNotifyMutex);
  210. mCommandsCompleted.push_back(commandId);
  211. }
  212. CM_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
  213. }
  214. CoreThread& gCoreThread()
  215. {
  216. return CoreThread::instance();
  217. }
  218. void throwIfNotCoreThread()
  219. {
  220. #if !CM_FORCE_SINGLETHREADED_RENDERING
  221. if(CM_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
  222. CM_EXCEPT(InternalErrorException, "This method can only be accessed from the core thread.");
  223. #endif
  224. }
  225. void throwIfCoreThread()
  226. {
  227. #if !CM_FORCE_SINGLETHREADED_RENDERING
  228. if(CM_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  229. CM_EXCEPT(InternalErrorException, "This method cannot be accessed from the core thread.");
  230. #endif
  231. }
  232. /************************************************************************/
  233. /* THREAD WORKER */
  234. /************************************************************************/
  235. CoreThread::CoreThreadWorkerFunc::CoreThreadWorkerFunc(CoreThread* owner)
  236. :mOwner(owner)
  237. {
  238. assert(mOwner != nullptr);
  239. }
  240. void CoreThread::CoreThreadWorkerFunc::operator()()
  241. {
  242. mOwner->runCoreThread();
  243. }
  244. }