BsCommandQueue.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "CoreThread/BsCommandQueue.h"
  4. #include "Error/BsException.h"
  5. #include "CoreThread/BsCoreThread.h"
  6. #include "Debug/BsDebug.h"
  7. namespace bs
  8. {
  9. #if BS_DEBUG_MODE
  10. CommandQueueBase::CommandQueueBase(ThreadId threadId)
  11. :mMyThreadId(threadId), mMaxDebugIdx(0)
  12. {
  13. mAsyncOpSyncData = bs_shared_ptr_new<AsyncOpSyncData>();
  14. mCommands = bs_new<bs::Queue<QueuedCommand>>();
  15. {
  16. Lock lock(CommandQueueBreakpointMutex);
  17. mCommandQueueIdx = MaxCommandQueueIdx++;
  18. }
  19. }
  20. #else
  21. CommandQueueBase::CommandQueueBase(ThreadId threadId)
  22. :mMyThreadId(threadId)
  23. {
  24. mAsyncOpSyncData = bs_shared_ptr_new<AsyncOpSyncData>();
  25. mCommands = bs_new<bs::Queue<QueuedCommand>>();
  26. }
  27. #endif
  28. CommandQueueBase::~CommandQueueBase()
  29. {
  30. if(mCommands != nullptr)
  31. bs_delete(mCommands);
  32. while(!mEmptyCommandQueues.empty())
  33. {
  34. bs_delete(mEmptyCommandQueues.top());
  35. mEmptyCommandQueues.pop();
  36. }
  37. }
  38. AsyncOp CommandQueueBase::queueReturn(std::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete, UINT32 _callbackId)
  39. {
  40. #if BS_DEBUG_MODE
  41. breakIfNeeded(mCommandQueueIdx, mMaxDebugIdx);
  42. QueuedCommand newCommand(commandCallback, mMaxDebugIdx++, mAsyncOpSyncData, _notifyWhenComplete, _callbackId);
  43. #else
  44. QueuedCommand newCommand(commandCallback, mAsyncOpSyncData, _notifyWhenComplete, _callbackId);
  45. #endif
  46. mCommands->push(newCommand);
  47. #if BS_FORCE_SINGLETHREADED_RENDERING
  48. Queue<QueuedCommand>* commands = flush();
  49. playback(commands);
  50. #endif
  51. return newCommand.asyncOp;
  52. }
  53. void CommandQueueBase::queue(std::function<void()> commandCallback, bool _notifyWhenComplete, UINT32 _callbackId)
  54. {
  55. #if BS_DEBUG_MODE
  56. breakIfNeeded(mCommandQueueIdx, mMaxDebugIdx);
  57. QueuedCommand newCommand(commandCallback, mMaxDebugIdx++, _notifyWhenComplete, _callbackId);
  58. #else
  59. QueuedCommand newCommand(commandCallback, _notifyWhenComplete, _callbackId);
  60. #endif
  61. mCommands->push(newCommand);
  62. #if BS_FORCE_SINGLETHREADED_RENDERING
  63. Queue<QueuedCommand>* commands = flush();
  64. playback(commands);
  65. #endif
  66. }
  67. bs::Queue<QueuedCommand>* CommandQueueBase::flush()
  68. {
  69. bs::Queue<QueuedCommand>* oldCommands = mCommands;
  70. if(!mEmptyCommandQueues.empty())
  71. {
  72. mCommands = mEmptyCommandQueues.top();
  73. mEmptyCommandQueues.pop();
  74. }
  75. else
  76. {
  77. mCommands = bs_new<bs::Queue<QueuedCommand>>();
  78. }
  79. return oldCommands;
  80. }
  81. void CommandQueueBase::playbackWithNotify(bs::Queue<QueuedCommand>* commands, std::function<void(UINT32)> notifyCallback)
  82. {
  83. THROW_IF_NOT_CORE_THREAD;
  84. if(commands == nullptr)
  85. return;
  86. while(!commands->empty())
  87. {
  88. QueuedCommand& command = commands->front();
  89. if(command.returnsValue)
  90. {
  91. AsyncOp& op = command.asyncOp;
  92. command.callbackWithReturnValue(op);
  93. if(!command.asyncOp.hasCompleted())
  94. {
  95. LOGDBG("Async operation return value wasn't resolved properly. Resolving automatically to nullptr. " \
  96. "Make sure to complete the operation before returning from the command callback method.");
  97. command.asyncOp._completeOperation(nullptr);
  98. }
  99. }
  100. else
  101. {
  102. command.callback();
  103. }
  104. if(command.notifyWhenComplete && notifyCallback != nullptr)
  105. {
  106. notifyCallback(command.callbackId);
  107. }
  108. commands->pop();
  109. }
  110. mEmptyCommandQueues.push(commands);
  111. }
  112. void CommandQueueBase::playback(bs::Queue<QueuedCommand>* commands)
  113. {
  114. playbackWithNotify(commands, std::function<void(UINT32)>());
  115. }
  116. void CommandQueueBase::cancelAll()
  117. {
  118. bs::Queue<QueuedCommand>* commands = flush();
  119. while(!commands->empty())
  120. commands->pop();
  121. mEmptyCommandQueues.push(commands);
  122. }
  123. bool CommandQueueBase::isEmpty()
  124. {
  125. if(mCommands != nullptr && mCommands->size() > 0)
  126. return false;
  127. return true;
  128. }
  129. void CommandQueueBase::throwInvalidThreadException(const String& message) const
  130. {
  131. BS_EXCEPT(InternalErrorException, message);
  132. }
  133. #if BS_DEBUG_MODE
  134. Mutex CommandQueueBase::CommandQueueBreakpointMutex;
  135. UINT32 CommandQueueBase::MaxCommandQueueIdx = 0;
  136. UnorderedSet<CommandQueueBase::QueueBreakpoint, CommandQueueBase::QueueBreakpoint::HashFunction,
  137. CommandQueueBase::QueueBreakpoint::EqualFunction> CommandQueueBase::SetBreakpoints;
  138. inline size_t CommandQueueBase::QueueBreakpoint::HashFunction::operator()(const QueueBreakpoint& v) const
  139. {
  140. size_t seed = 0;
  141. hash_combine(seed, v.queueIdx);
  142. hash_combine(seed, v.commandIdx);
  143. return seed;
  144. }
  145. inline bool CommandQueueBase::QueueBreakpoint::EqualFunction::operator()(const QueueBreakpoint &a, const QueueBreakpoint &b) const
  146. {
  147. return a.queueIdx == b.queueIdx && a.commandIdx == b.commandIdx;
  148. }
  149. void CommandQueueBase::addBreakpoint(UINT32 queueIdx, UINT32 commandIdx)
  150. {
  151. Lock lock(CommandQueueBreakpointMutex);
  152. SetBreakpoints.insert(QueueBreakpoint(queueIdx, commandIdx));
  153. }
  154. void CommandQueueBase::breakIfNeeded(UINT32 queueIdx, UINT32 commandIdx)
  155. {
  156. // I purposely don't use a mutex here, as this gets called very often. Generally breakpoints
  157. // will only be added at the start of the application, so race conditions should not occur.
  158. auto iterFind = SetBreakpoints.find(QueueBreakpoint(queueIdx, commandIdx));
  159. if(iterFind != SetBreakpoints.end())
  160. {
  161. assert(false && "Command queue breakpoint triggered!");
  162. }
  163. }
  164. #else
  165. void CommandQueueBase::addBreakpoint(UINT32 queueIdx, UINT32 commandIdx)
  166. {
  167. // Do nothing, no breakpoints in release
  168. }
  169. #endif
  170. }