CmCommandQueue.cpp 5.5 KB

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