BsCoreThread.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #include "BsCoreThread.h"
  2. #include "BsThreadPool.h"
  3. #include "BsTaskScheduler.h"
  4. #include "BsFrameAlloc.h"
  5. using namespace std::placeholders;
  6. namespace BansheeEngine
  7. {
  8. BS_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] = bs_new<FrameAlloc>();
  17. mFrameAllocs[1] = bs_new<FrameAlloc>();
  18. mCoreThreadId = BS_THREAD_CURRENT_ID;
  19. mCommandQueue = bs_new<CommandQueue<CommandQueueSync>>(BS_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. BS_LOCK_MUTEX(mAccessorMutex);
  28. for(auto& accessor : mAccessors)
  29. {
  30. bs_delete(accessor);
  31. }
  32. mAccessors.clear();
  33. }
  34. if(mCommandQueue != nullptr)
  35. {
  36. bs_delete(mCommandQueue);
  37. mCommandQueue = nullptr;
  38. }
  39. bs_delete(mFrameAllocs[0]);
  40. bs_delete(mFrameAllocs[1]);
  41. }
  42. void CoreThread::initCoreThread()
  43. {
  44. #if !BS_FORCE_SINGLETHREADED_RENDERING
  45. #if BS_THREAD_SUPPORT
  46. mCoreThread = ThreadPool::instance().run("Core", std::bind(&CoreThread::runCoreThread, this));
  47. #else
  48. BS_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 !BS_FORCE_SINGLETHREADED_RENDERING
  55. TaskScheduler::instance().removeWorker(); // One less worker because we are reserving one core for this thread
  56. mCoreThreadId = BS_THREAD_CURRENT_ID;
  57. mSyncedCoreAccessor = bs_new<CoreThreadAccessor<CommandQueueSync>>(BS_THREAD_CURRENT_ID);
  58. while(true)
  59. {
  60. // Wait until we get some ready commands
  61. Queue<QueuedCommand>* commands = nullptr;
  62. {
  63. BS_LOCK_MUTEX_NAMED(mCommandQueueMutex, lock)
  64. while(mCommandQueue->isEmpty())
  65. {
  66. if(mCoreThreadShutdown)
  67. {
  68. bs_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. BS_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 !BS_FORCE_SINGLETHREADED_RENDERING
  86. {
  87. BS_LOCK_MUTEX(mCommandQueueMutex);
  88. mCoreThreadShutdown = true;
  89. }
  90. // Wake all threads. They will quit after they see the shutdown flag
  91. BS_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  92. mCoreThreadId = BS_THREAD_CURRENT_ID;
  93. mCoreThread.blockUntilComplete();
  94. #endif
  95. }
  96. CoreAccessorPtr CoreThread::getAccessor()
  97. {
  98. if(mAccessor == nullptr)
  99. {
  100. CoreAccessorPtr newAccessor = bs_shared_ptr<CoreThreadAccessor<CommandQueueNoSync>>(BS_THREAD_CURRENT_ID);
  101. mAccessor = bs_new<AccessorContainer>();
  102. mAccessor->accessor = newAccessor;
  103. BS_LOCK_MUTEX(mAccessorMutex);
  104. mAccessors.push_back(mAccessor);
  105. }
  106. return mAccessor->accessor;
  107. }
  108. SyncedCoreAccessor& CoreThread::getSyncedAccessor()
  109. {
  110. return *mSyncedCoreAccessor;
  111. }
  112. void CoreThread::submitAccessors(bool blockUntilComplete)
  113. {
  114. Vector<AccessorContainer*> accessorCopies;
  115. {
  116. BS_LOCK_MUTEX(mAccessorMutex);
  117. accessorCopies = mAccessors;
  118. }
  119. for(auto& accessor : accessorCopies)
  120. accessor->accessor->submitToCoreThread(blockUntilComplete);
  121. mSyncedCoreAccessor->submitToCoreThread(blockUntilComplete);
  122. }
  123. AsyncOp CoreThread::queueReturnCommand(std::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete)
  124. {
  125. AsyncOp op;
  126. if(BS_THREAD_CURRENT_ID == getCoreThreadId())
  127. {
  128. commandCallback(op); // Execute immediately
  129. return op;
  130. }
  131. UINT32 commandId = -1;
  132. {
  133. BS_LOCK_MUTEX(mCommandQueueMutex);
  134. if(blockUntilComplete)
  135. {
  136. commandId = mMaxCommandNotifyId++;
  137. op = mCommandQueue->queueReturn(commandCallback, true, commandId);
  138. }
  139. else
  140. op = mCommandQueue->queueReturn(commandCallback);
  141. }
  142. BS_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  143. if(blockUntilComplete)
  144. blockUntilCommandCompleted(commandId);
  145. return op;
  146. }
  147. void CoreThread::queueCommand(std::function<void()> commandCallback, bool blockUntilComplete)
  148. {
  149. if(BS_THREAD_CURRENT_ID == getCoreThreadId())
  150. {
  151. commandCallback(); // Execute immediately
  152. return;
  153. }
  154. UINT32 commandId = -1;
  155. {
  156. BS_LOCK_MUTEX(mCommandQueueMutex);
  157. if(blockUntilComplete)
  158. {
  159. commandId = mMaxCommandNotifyId++;
  160. mCommandQueue->queue(commandCallback, true, commandId);
  161. }
  162. else
  163. mCommandQueue->queue(commandCallback);
  164. }
  165. BS_THREAD_NOTIFY_ALL(mCommandReadyCondition);
  166. if(blockUntilComplete)
  167. blockUntilCommandCompleted(commandId);
  168. }
  169. void CoreThread::update()
  170. {
  171. mActiveFrameAlloc = (mActiveFrameAlloc + 1) % 2;
  172. mFrameAllocs[mActiveFrameAlloc]->clear();
  173. }
  174. FrameAlloc* CoreThread::getFrameAlloc() const
  175. {
  176. return mFrameAllocs[mActiveFrameAlloc];
  177. }
  178. void CoreThread::blockUntilCommandCompleted(UINT32 commandId)
  179. {
  180. #if !BS_FORCE_SINGLETHREADED_RENDERING
  181. BS_LOCK_MUTEX_NAMED(mCommandNotifyMutex, lock);
  182. while(true)
  183. {
  184. // TODO - This might be causing a deadlock in Release mode. I'm thinking because mCommandsCompleted isn't marked as volatile.
  185. // Check if our command id is in the completed list
  186. auto iter = mCommandsCompleted.begin();
  187. for(; iter != mCommandsCompleted.end(); ++iter)
  188. {
  189. if(*iter == commandId)
  190. break;
  191. }
  192. if(iter != mCommandsCompleted.end())
  193. {
  194. mCommandsCompleted.erase(iter);
  195. break;
  196. }
  197. BS_THREAD_WAIT(mCommandCompleteCondition, mCommandNotifyMutex, lock);
  198. }
  199. #endif
  200. }
  201. void CoreThread::commandCompletedNotify(UINT32 commandId)
  202. {
  203. {
  204. BS_LOCK_MUTEX(mCommandNotifyMutex);
  205. mCommandsCompleted.push_back(commandId);
  206. }
  207. BS_THREAD_NOTIFY_ALL(mCommandCompleteCondition);
  208. }
  209. CoreThread& gCoreThread()
  210. {
  211. return CoreThread::instance();
  212. }
  213. CoreThreadAccessor<CommandQueueNoSync>& gCoreAccessor()
  214. {
  215. return *CoreThread::instance().getAccessor();
  216. }
  217. void throwIfNotCoreThread()
  218. {
  219. #if !BS_FORCE_SINGLETHREADED_RENDERING
  220. if(BS_THREAD_CURRENT_ID != CoreThread::instance().getCoreThreadId())
  221. BS_EXCEPT(InternalErrorException, "This method can only be accessed from the core thread.");
  222. #endif
  223. }
  224. void throwIfCoreThread()
  225. {
  226. #if !BS_FORCE_SINGLETHREADED_RENDERING
  227. if(BS_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  228. BS_EXCEPT(InternalErrorException, "This method cannot be accessed from the core thread.");
  229. #endif
  230. }
  231. }