CmCoreThread.cpp 6.9 KB

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