CmCoreThread.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "CmCoreThread.h"
  2. #include "BsThreadPool.h"
  3. using namespace std::placeholders;
  4. namespace BansheeEngine
  5. {
  6. CM_THREADLOCAL CoreThread::AccessorContainer* CoreThread::mAccessor = nullptr;
  7. CoreThread::CoreThread()
  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. #if CM_THREAD_SUPPORT
  44. ThreadPool::instance().run("Core", std::bind(&CoreThread::runCoreThread, this));
  45. #else
  46. CM_EXCEPT(InternalErrorException, "Attempting to start a core thread but application isn't compiled with thread support.");
  47. #endif
  48. #endif
  49. }
  50. void CoreThread::runCoreThread()
  51. {
  52. #if !CM_FORCE_SINGLETHREADED_RENDERING
  53. mCoreThreadId = CM_THREAD_CURRENT_ID;
  54. mSyncedCoreAccessor = cm_new<CoreThreadAccessor<CommandQueueSync>>(CM_THREAD_CURRENT_ID);
  55. while(true)
  56. {
  57. // Wait until we get some ready commands
  58. Queue<QueuedCommand>::type* commands = nullptr;
  59. {
  60. CM_LOCK_MUTEX_NAMED(mCommandQueueMutex, lock)
  61. while(mCommandQueue->isEmpty())
  62. {
  63. if(mCoreThreadShutdown)
  64. {
  65. cm_delete(mSyncedCoreAccessor);
  66. return;
  67. }
  68. CM_THREAD_WAIT(mCommandReadyCondition, mCommandQueueMutex, lock);
  69. }
  70. commands = mCommandQueue->flush();
  71. }
  72. // Play commands
  73. mCommandQueue->playbackWithNotify(commands, std::bind(&CoreThread::commandCompletedNotify, this, _1));
  74. }
  75. #endif
  76. }
  77. void CoreThread::shutdownCoreThread()
  78. {
  79. #if !CM_FORCE_SINGLETHREADED_RENDERING
  80. {
  81. CM_LOCK_MUTEX(mCommandQueueMutex);
  82. mCoreThreadShutdown = true;
  83. }
  84. // Wake all threads. They will quit after they see the shutdown flag
  85. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  86. mCoreThreadId = CM_THREAD_CURRENT_ID;
  87. #endif
  88. }
  89. CoreAccessorPtr CoreThread::getAccessor()
  90. {
  91. if(mAccessor == nullptr)
  92. {
  93. CoreAccessorPtr newAccessor = cm_shared_ptr<CoreThreadAccessor<CommandQueueNoSync>>(CM_THREAD_CURRENT_ID);
  94. mAccessor = cm_new<AccessorContainer>();
  95. mAccessor->accessor = newAccessor;
  96. CM_LOCK_MUTEX(mAccessorMutex);
  97. mAccessors.push_back(mAccessor);
  98. }
  99. return mAccessor->accessor;
  100. }
  101. SyncedCoreAccessor& CoreThread::getSyncedAccessor()
  102. {
  103. return *mSyncedCoreAccessor;
  104. }
  105. void CoreThread::submitAccessors(bool blockUntilComplete)
  106. {
  107. Vector<AccessorContainer*>::type accessorCopies;
  108. {
  109. CM_LOCK_MUTEX(mAccessorMutex);
  110. accessorCopies = mAccessors;
  111. }
  112. for(auto& accessor : accessorCopies)
  113. accessor->accessor->submitToCoreThread(blockUntilComplete);
  114. mSyncedCoreAccessor->submitToCoreThread(blockUntilComplete);
  115. }
  116. AsyncOp CoreThread::queueReturnCommand(std::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete)
  117. {
  118. AsyncOp op;
  119. if(CM_THREAD_CURRENT_ID == getCoreThreadId())
  120. {
  121. commandCallback(op); // Execute immediately
  122. return op;
  123. }
  124. UINT32 commandId = -1;
  125. {
  126. CM_LOCK_MUTEX(mCommandQueueMutex);
  127. if(blockUntilComplete)
  128. {
  129. commandId = mMaxCommandNotifyId++;
  130. op = mCommandQueue->queueReturn(commandCallback, true, commandId);
  131. }
  132. else
  133. op = mCommandQueue->queueReturn(commandCallback);
  134. }
  135. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  136. if(blockUntilComplete)
  137. blockUntilCommandCompleted(commandId);
  138. return op;
  139. }
  140. void CoreThread::queueCommand(std::function<void()> commandCallback, bool blockUntilComplete)
  141. {
  142. if(CM_THREAD_CURRENT_ID == getCoreThreadId())
  143. {
  144. commandCallback(); // Execute immediately
  145. return;
  146. }
  147. UINT32 commandId = -1;
  148. {
  149. CM_LOCK_MUTEX(mCommandQueueMutex);
  150. if(blockUntilComplete)
  151. {
  152. commandId = mMaxCommandNotifyId++;
  153. mCommandQueue->queue(commandCallback, true, commandId);
  154. }
  155. else
  156. mCommandQueue->queue(commandCallback);
  157. }
  158. CM_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  159. if(blockUntilComplete)
  160. blockUntilCommandCompleted(commandId);
  161. }
  162. void CoreThread::update()
  163. {
  164. mActiveFrameAlloc = (mActiveFrameAlloc + 1) % 2;
  165. mFrameAllocs[mActiveFrameAlloc]->clear();
  166. }
  167. FrameAlloc* CoreThread::getFrameAlloc() const
  168. {
  169. return mFrameAllocs[mActiveFrameAlloc];
  170. }
  171. void CoreThread::blockUntilCommandCompleted(UINT32 commandId)
  172. {
  173. #if !CM_FORCE_SINGLETHREADED_RENDERING
  174. CM_LOCK_MUTEX_NAMED(mCommandNotifyMutex, lock);
  175. while(true)
  176. {
  177. // TODO - This might be causing a deadlock in Release mode. I'm thinking because mCommandsCompleted isn't marked as volatile.
  178. // Check if our command id is in the completed list
  179. auto iter = mCommandsCompleted.begin();
  180. for(; iter != mCommandsCompleted.end(); ++iter)
  181. {
  182. if(*iter == commandId)
  183. break;
  184. }
  185. if(iter != mCommandsCompleted.end())
  186. {
  187. mCommandsCompleted.erase(iter);
  188. break;
  189. }
  190. CM_THREAD_WAIT(mCommandCompleteCondition, mCommandNotifyMutex, lock);
  191. }
  192. #endif
  193. }
  194. void CoreThread::commandCompletedNotify(UINT32 commandId)
  195. {
  196. {
  197. CM_LOCK_MUTEX(mCommandNotifyMutex);
  198. mCommandsCompleted.push_back(commandId);
  199. }
  200. CM_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
  201. }
  202. CoreThread& gCoreThread()
  203. {
  204. return CoreThread::instance();
  205. }
  206. CoreThreadAccessor<CommandQueueNoSync>& gCoreAccessor()
  207. {
  208. return *CoreThread::instance().getAccessor();
  209. }
  210. void throwIfNotCoreThread()
  211. {
  212. #if !CM_FORCE_SINGLETHREADED_RENDERING
  213. if(CM_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
  214. CM_EXCEPT(InternalErrorException, "This method can only be accessed from the core thread.");
  215. #endif
  216. }
  217. void throwIfCoreThread()
  218. {
  219. #if !CM_FORCE_SINGLETHREADED_RENDERING
  220. if(CM_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  221. CM_EXCEPT(InternalErrorException, "This method cannot be accessed from the core thread.");
  222. #endif
  223. }
  224. }