CmCommandQueue.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. CM_THREAD_ID_TYPE mMyThreadId;
  162. // Various variables that allow for easier debugging by allowing us to trigger breakpoints
  163. // when a certain command was queued.
  164. #if CM_DEBUG_MODE
  165. struct QueueBreakpoint
  166. {
  167. class HashFunction
  168. {
  169. public:
  170. size_t operator()(const QueueBreakpoint &key) const;
  171. };
  172. class EqualFunction
  173. {
  174. public:
  175. bool operator()(const QueueBreakpoint &a, const QueueBreakpoint &b) const;
  176. };
  177. QueueBreakpoint(UINT32 _queueIdx, UINT32 _commandIdx)
  178. :queueIdx(_queueIdx), commandIdx(_commandIdx)
  179. { }
  180. UINT32 queueIdx;
  181. UINT32 commandIdx;
  182. inline size_t operator()(const QueueBreakpoint& v) const;
  183. };
  184. UINT32 mMaxDebugIdx;
  185. UINT32 mCommandQueueIdx;
  186. static UINT32 MaxCommandQueueIdx;
  187. static UnorderedSet<QueueBreakpoint, QueueBreakpoint::HashFunction, QueueBreakpoint::EqualFunction>::type SetBreakpoints;
  188. CM_STATIC_MUTEX(CommandQueueBreakpointMutex);
  189. static void breakIfNeeded(UINT32 queueIdx, UINT32 commandIdx);
  190. #endif
  191. };
  192. /**
  193. * @copydoc CommandQueueBase
  194. */
  195. template<class SyncPolicy = CommandQueueNoSync>
  196. class CommandQueue : public CommandQueueBase, public SyncPolicy
  197. {
  198. public:
  199. /**
  200. * @copydoc CommandQueueBase::CommandQueueBase
  201. */
  202. CommandQueue(CM_THREAD_ID_TYPE threadId)
  203. :CommandQueueBase(threadId)
  204. { }
  205. ~CommandQueue()
  206. {
  207. #if CM_DEBUG_MODE
  208. #if CM_THREAD_SUPPORT != 0
  209. if(!isValidThread(getThreadId()))
  210. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  211. #endif
  212. #endif
  213. }
  214. /**
  215. * @copydoc CommandQueueBase::queueReturn
  216. */
  217. AsyncOp queueReturn(boost::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  218. {
  219. #if CM_DEBUG_MODE
  220. #if CM_THREAD_SUPPORT != 0
  221. if(!isValidThread(getThreadId()))
  222. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  223. #endif
  224. #endif
  225. lock();
  226. AsyncOp asyncOp = CommandQueueBase::queueReturn(commandCallback, _notifyWhenComplete, _callbackId);
  227. unlock();
  228. return asyncOp;
  229. }
  230. /**
  231. * @copydoc CommandQueueBase::queue
  232. */
  233. void queue(boost::function<void()> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  234. {
  235. #if CM_DEBUG_MODE
  236. #if CM_THREAD_SUPPORT != 0
  237. if(!isValidThread(getThreadId()))
  238. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  239. #endif
  240. #endif
  241. lock();
  242. CommandQueueBase::queue(commandCallback, _notifyWhenComplete, _callbackId);
  243. unlock();
  244. }
  245. /**
  246. * @copydoc CommandQueueBase::flush
  247. */
  248. CamelotFramework::Queue<QueuedCommand>::type* flush()
  249. {
  250. #if CM_DEBUG_MODE
  251. #if CM_THREAD_SUPPORT != 0
  252. if(!isValidThread(getThreadId()))
  253. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  254. #endif
  255. #endif
  256. lock();
  257. CamelotFramework::Queue<QueuedCommand>::type* commands = CommandQueueBase::flush();
  258. unlock();
  259. return commands;
  260. }
  261. /**
  262. * @copydoc CommandQueueBase::cancelAll
  263. */
  264. void cancelAll()
  265. {
  266. #if CM_DEBUG_MODE
  267. #if CM_THREAD_SUPPORT != 0
  268. if(!isValidThread(getThreadId()))
  269. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  270. #endif
  271. #endif
  272. lock();
  273. CommandQueueBase::cancelAll();
  274. unlock();
  275. }
  276. /**
  277. * @copydoc CommandQueueBase::isEmpty
  278. */
  279. bool isEmpty()
  280. {
  281. #if CM_DEBUG_MODE
  282. #if CM_THREAD_SUPPORT != 0
  283. if(!isValidThread(getThreadId()))
  284. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  285. #endif
  286. #endif
  287. lock();
  288. bool empty = CommandQueueBase::isEmpty();
  289. unlock();
  290. return empty;
  291. }
  292. };
  293. }