2
0

CmCommandQueue.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmAsyncOp.h"
  4. #include "CmCommonEnums.h"
  5. #include "boost/function.hpp"
  6. #include <functional>
  7. namespace CamelotFramework
  8. {
  9. class CommandQueueNoSync
  10. {
  11. public:
  12. CommandQueueNoSync() {}
  13. virtual ~CommandQueueNoSync() {}
  14. bool isValidThread(CM_THREAD_ID_TYPE ownerThread) const
  15. {
  16. return CM_THREAD_CURRENT_ID == ownerThread;
  17. }
  18. void lock()
  19. {
  20. };
  21. void unlock()
  22. {
  23. }
  24. };
  25. class CommandQueueSync
  26. {
  27. public:
  28. CommandQueueSync()
  29. :mLock(mCommandQueueMutex, boost::defer_lock)
  30. { }
  31. virtual ~CommandQueueSync() {}
  32. bool isValidThread(CM_THREAD_ID_TYPE ownerThread) const
  33. {
  34. return true;
  35. }
  36. void lock()
  37. {
  38. mLock.lock();
  39. };
  40. void unlock()
  41. {
  42. mLock.unlock();
  43. }
  44. private:
  45. CM_MUTEX(mCommandQueueMutex);
  46. CM_LOCK_TYPE mLock;
  47. };
  48. struct QueuedCommand
  49. {
  50. #if CM_DEBUG_MODE
  51. QueuedCommand(boost::function<void(AsyncOp&)> _callback, UINT32 _debugId, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  52. :callbackWithReturnValue(_callback), debugId(_debugId), returnsValue(true), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId)
  53. { }
  54. QueuedCommand(boost::function<void()> _callback, UINT32 _debugId, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  55. :callback(_callback), debugId(_debugId), returnsValue(false), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId)
  56. { }
  57. UINT32 debugId;
  58. #else
  59. QueuedCommand(boost::function<void(AsyncOp&)> _callback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  60. :callbackWithReturnValue(_callback), returnsValue(true), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId)
  61. { }
  62. QueuedCommand(boost::function<void()> _callback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  63. :callback(_callback), returnsValue(false), notifyWhenComplete(_notifyWhenComplete), callbackId(_callbackId)
  64. { }
  65. #endif
  66. boost::function<void()> callback;
  67. boost::function<void(AsyncOp&)> callbackWithReturnValue;
  68. AsyncOp asyncOp;
  69. bool returnsValue;
  70. UINT32 callbackId;
  71. bool notifyWhenComplete;
  72. };
  73. /**
  74. * @brief Contains a list of commands that can be queued by one thread,
  75. * and executed by another.
  76. */
  77. class CM_EXPORT CommandQueueBase
  78. {
  79. public:
  80. /**
  81. * @brief Constructor.
  82. *
  83. * @param threadId Identifier for the thread the command queue will be used on.
  84. */
  85. CommandQueueBase(CM_THREAD_ID_TYPE threadId);
  86. virtual ~CommandQueueBase();
  87. CM_THREAD_ID_TYPE getThreadId() const { return mMyThreadId; }
  88. /**
  89. * @brief Plays all provided commands. To get the commands call flush().
  90. *
  91. * @param notifyCallback Callback that will be called if a command that has "notifyOnComplete" flag set.
  92. * The callback will receive "callbackId" of the command.
  93. */
  94. void playback(Queue<QueuedCommand>::type* commands, boost::function<void(UINT32)> notifyCallback);
  95. /**
  96. * @brief Plays all provided commands. To get the commands call flush().
  97. */
  98. void playback(Queue<QueuedCommand>::type* commands);
  99. /**
  100. * @brief Allows you to set a breakpoint that will trigger when the specified command is executed.
  101. *
  102. * @note This is helpful when you receive an error on the executing thread and you cannot tell from where was
  103. * the command that caused the error queued from. However you can make a note of the queue and command index
  104. * and set a breakpoint so that it gets triggered next time you run the program. At that point you can know
  105. * exactly which part of code queued the command by examining the stack trace.
  106. *
  107. * @param queueIdx Zero-based index of the queue the command was queued on.
  108. * @param commandIdx Zero-based index of the command.
  109. */
  110. static void addBreakpoint(UINT32 queueIdx, UINT32 commandIdx);
  111. protected:
  112. /**
  113. * @brief Queue up a new command to execute. Make sure the provided function has all of its
  114. * parameters properly bound. Last parameter must be unbound and of AsyncOp&amp; type.
  115. * This is used to signal that the command is completed, and also for storing the return
  116. * value.
  117. *
  118. * @note Callback method also needs to call AsyncOp::markAsResolved once it is done
  119. * processing. (If it doesn't it will still be called automatically, but the return
  120. * value will default to nullptr)
  121. *
  122. * @param _notifyWhenComplete (optional) Call the notify method (provided in the call to CommandQueue::playback)
  123. * when the command is complete.
  124. * @param _callbackId (optional) Identifier for the callback so you can then later find it
  125. * if needed.
  126. *
  127. * @return Async operation object you can continuously check until the command completes. After
  128. * it completes AsyncOp::isResolved will return true and return data will be valid (if
  129. * the callback provided any).
  130. */
  131. AsyncOp queueReturn(boost::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0);
  132. /**
  133. * @brief Queue up a new command to execute. Make sure the provided function has all of its
  134. * parameters properly bound. Provided command is not expected to return a value. If you
  135. * wish to return a value from the callback use the other overload of queueCommand which
  136. * accepts AsyncOp parameter.
  137. *
  138. * @param _notifyWhenComplete (optional) Call the notify method (provided in the call to CommandQueue::playback)
  139. * when the command is complete.
  140. * @param _callbackId (optional) Identifier for the callback so you can then later find
  141. * it if needed.
  142. */
  143. void queue(boost::function<void()> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0);
  144. /**
  145. * @brief Returns a copy of all queued commands and makes room for new ones. Must be called from the thread
  146. * that created the command queue. Returned commands MUST be passed to "playback" method.
  147. */
  148. CamelotFramework::Queue<QueuedCommand>::type* flush();
  149. /**
  150. * @brief Cancels all currently queued commands.
  151. */
  152. void cancelAll();
  153. /**
  154. * @brief Returns true if no commands are queued.
  155. */
  156. bool isEmpty();
  157. protected:
  158. void throwInvalidThreadException(const String& message) const;
  159. private:
  160. CamelotFramework::Queue<QueuedCommand>::type* mCommands;
  161. Stack<CamelotFramework::Queue<QueuedCommand>::type*>::type mEmptyCommandQueues; // List of empty queues for reuse
  162. CM_THREAD_ID_TYPE mMyThreadId;
  163. // Various variables that allow for easier debugging by allowing us to trigger breakpoints
  164. // when a certain command was queued.
  165. #if CM_DEBUG_MODE
  166. struct QueueBreakpoint
  167. {
  168. class HashFunction
  169. {
  170. public:
  171. size_t operator()(const QueueBreakpoint &key) const;
  172. };
  173. class EqualFunction
  174. {
  175. public:
  176. bool operator()(const QueueBreakpoint &a, const QueueBreakpoint &b) const;
  177. };
  178. QueueBreakpoint(UINT32 _queueIdx, UINT32 _commandIdx)
  179. :queueIdx(_queueIdx), commandIdx(_commandIdx)
  180. { }
  181. UINT32 queueIdx;
  182. UINT32 commandIdx;
  183. inline size_t operator()(const QueueBreakpoint& v) const;
  184. };
  185. UINT32 mMaxDebugIdx;
  186. UINT32 mCommandQueueIdx;
  187. static UINT32 MaxCommandQueueIdx;
  188. static UnorderedSet<QueueBreakpoint, QueueBreakpoint::HashFunction, QueueBreakpoint::EqualFunction>::type SetBreakpoints;
  189. CM_STATIC_MUTEX(CommandQueueBreakpointMutex);
  190. static void breakIfNeeded(UINT32 queueIdx, UINT32 commandIdx);
  191. #endif
  192. };
  193. /**
  194. * @copydoc CommandQueueBase
  195. */
  196. template<class SyncPolicy = CommandQueueNoSync>
  197. class CommandQueue : public CommandQueueBase, public SyncPolicy
  198. {
  199. public:
  200. /**
  201. * @copydoc CommandQueueBase::CommandQueueBase
  202. */
  203. CommandQueue(CM_THREAD_ID_TYPE threadId)
  204. :CommandQueueBase(threadId)
  205. { }
  206. ~CommandQueue()
  207. {
  208. #if CM_DEBUG_MODE
  209. #if CM_THREAD_SUPPORT != 0
  210. if(!isValidThread(getThreadId()))
  211. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  212. #endif
  213. #endif
  214. }
  215. /**
  216. * @copydoc CommandQueueBase::queueReturn
  217. */
  218. AsyncOp queueReturn(boost::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  219. {
  220. #if CM_DEBUG_MODE
  221. #if CM_THREAD_SUPPORT != 0
  222. if(!isValidThread(getThreadId()))
  223. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  224. #endif
  225. #endif
  226. lock();
  227. AsyncOp asyncOp = CommandQueueBase::queueReturn(commandCallback, _notifyWhenComplete, _callbackId);
  228. unlock();
  229. return asyncOp;
  230. }
  231. /**
  232. * @copydoc CommandQueueBase::queue
  233. */
  234. void queue(boost::function<void()> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  235. {
  236. #if CM_DEBUG_MODE
  237. #if CM_THREAD_SUPPORT != 0
  238. if(!isValidThread(getThreadId()))
  239. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  240. #endif
  241. #endif
  242. lock();
  243. CommandQueueBase::queue(commandCallback, _notifyWhenComplete, _callbackId);
  244. unlock();
  245. }
  246. /**
  247. * @copydoc CommandQueueBase::flush
  248. */
  249. CamelotFramework::Queue<QueuedCommand>::type* flush()
  250. {
  251. #if CM_DEBUG_MODE
  252. #if CM_THREAD_SUPPORT != 0
  253. if(!isValidThread(getThreadId()))
  254. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  255. #endif
  256. #endif
  257. lock();
  258. CamelotFramework::Queue<QueuedCommand>::type* commands = CommandQueueBase::flush();
  259. unlock();
  260. return commands;
  261. }
  262. /**
  263. * @copydoc CommandQueueBase::cancelAll
  264. */
  265. void cancelAll()
  266. {
  267. #if CM_DEBUG_MODE
  268. #if CM_THREAD_SUPPORT != 0
  269. if(!isValidThread(getThreadId()))
  270. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  271. #endif
  272. #endif
  273. lock();
  274. CommandQueueBase::cancelAll();
  275. unlock();
  276. }
  277. /**
  278. * @copydoc CommandQueueBase::isEmpty
  279. */
  280. bool isEmpty()
  281. {
  282. #if CM_DEBUG_MODE
  283. #if CM_THREAD_SUPPORT != 0
  284. if(!isValidThread(getThreadId()))
  285. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  286. #endif
  287. #endif
  288. lock();
  289. bool empty = CommandQueueBase::isEmpty();
  290. unlock();
  291. return empty;
  292. }
  293. };
  294. }