CmCommandQueue.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. command.callbackWithReturnValue(*command.asyncOp);
  89. if(!command.asyncOp->hasCompleted())
  90. {
  91. LOGDBG("Async operation return value wasn't resolved properly. Resolving automatically to nullptr. " \
  92. "Make sure to complete the operation before returning from the command callback method.");
  93. command.asyncOp->completeOperation(nullptr);
  94. }
  95. }
  96. else
  97. {
  98. command.callback();
  99. }
  100. if(command.notifyWhenComplete && !notifyCallback.empty())
  101. {
  102. notifyCallback(command.callbackId);
  103. }
  104. commands->pop();
  105. }
  106. mEmptyCommandQueues.push(commands);
  107. }
  108. void CommandQueueBase::playback(CamelotFramework::Queue<QueuedCommand>::type* commands)
  109. {
  110. playback(commands, boost::function<void(UINT32)>());
  111. }
  112. void CommandQueueBase::cancelAll()
  113. {
  114. CamelotFramework::Queue<QueuedCommand>::type* commands = flush();
  115. while(!commands->empty())
  116. commands->pop();
  117. mEmptyCommandQueues.push(commands);
  118. }
  119. bool CommandQueueBase::isEmpty()
  120. {
  121. if(mCommands != nullptr && mCommands->size() > 0)
  122. return false;
  123. return true;
  124. }
  125. void CommandQueueBase::throwInvalidThreadException(const String& message) const
  126. {
  127. CM_EXCEPT(InternalErrorException, message);
  128. }
  129. #if CM_DEBUG_MODE
  130. CM_STATIC_MUTEX_CLASS_INSTANCE(CommandQueueBreakpointMutex, CommandQueueBase);
  131. UINT32 CommandQueueBase::MaxCommandQueueIdx = 0;
  132. UnorderedSet<CommandQueueBase::QueueBreakpoint, CommandQueueBase::QueueBreakpoint::HashFunction,
  133. CommandQueueBase::QueueBreakpoint::EqualFunction>::type CommandQueueBase::SetBreakpoints;
  134. inline size_t CommandQueueBase::QueueBreakpoint::HashFunction::operator()(const QueueBreakpoint& v) const
  135. {
  136. size_t seed = 0;
  137. hash_combine(seed, v.queueIdx);
  138. hash_combine(seed, v.commandIdx);
  139. return seed;
  140. }
  141. inline bool CommandQueueBase::QueueBreakpoint::EqualFunction::operator()(const QueueBreakpoint &a, const QueueBreakpoint &b) const
  142. {
  143. return a.queueIdx == b.queueIdx && a.commandIdx == b.commandIdx;
  144. }
  145. void CommandQueueBase::addBreakpoint(UINT32 queueIdx, UINT32 commandIdx)
  146. {
  147. CM_LOCK_MUTEX(CommandQueueBreakpointMutex);
  148. SetBreakpoints.insert(QueueBreakpoint(queueIdx, commandIdx));
  149. }
  150. void CommandQueueBase::breakIfNeeded(UINT32 queueIdx, UINT32 commandIdx)
  151. {
  152. // I purposely don't use a mutex here, as this gets called very often. Generally breakpoints
  153. // will only be added at the start of the application, so race conditions should not occur.
  154. auto iterFind = SetBreakpoints.find(QueueBreakpoint(queueIdx, commandIdx));
  155. if(iterFind != SetBreakpoints.end())
  156. {
  157. assert(false && "Command queue breakpoint triggered!");
  158. }
  159. }
  160. #else
  161. void CommandQueueBase::addBreakpoint(UINT32 queueIdx, UINT32 commandIdx)
  162. {
  163. // Do nothing, no breakpoints in release
  164. }
  165. #endif
  166. }