CmCommandQueue.cpp 5.2 KB

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