CmCommandQueue.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 CM_EXPORT 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 CM_EXPORT 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. * @param allowAllThreads Only matters for debug purposes. If false, then the queue
  85. * will throw an exception if accessed outside of the creation thread
  86. * (If in debug mode).
  87. *
  88. * When you want to allow multiple threads to access it, set this to true,
  89. * and also make sure you sync access to the queue properly.
  90. *
  91. */
  92. CommandQueueBase(CM_THREAD_ID_TYPE threadId, bool allowAllThreads = false);
  93. virtual ~CommandQueueBase();
  94. CM_THREAD_ID_TYPE getThreadId() const { return mMyThreadId; }
  95. /**
  96. * @brief Plays all provided commands. To get the commands call flush().
  97. *
  98. * @param notifyCallback Callback that will be called if a command that has "notifyOnComplete" flag set.
  99. * The callback will receive "callbackId" of the command.
  100. */
  101. void playback(Queue<QueuedCommand>::type* commands, boost::function<void(UINT32)> notifyCallback);
  102. /**
  103. * @brief Plays all provided commands. To get the commands call flush().
  104. */
  105. void playback(Queue<QueuedCommand>::type* commands);
  106. /**
  107. * @brief Allows you to set a breakpoint that will trigger when the specified command is executed.
  108. *
  109. * @note This is helpful when you receive an error on the executing thread and you cannot tell from where was
  110. * the command that caused the error queued from. However you can make a note of the queue and command index
  111. * and set a breakpoint so that it gets triggered next time you run the program. At that point you can know
  112. * exactly which part of code queued the command by examining the stack trace.
  113. *
  114. * @param queueIdx Zero-based index of the queue the command was queued on.
  115. * @param commandIdx Zero-based index of the command.
  116. */
  117. static void addBreakpoint(UINT32 queueIdx, UINT32 commandIdx);
  118. protected:
  119. /**
  120. * @brief Queue up a new command to execute. Make sure the provided function has all of its
  121. * parameters properly bound. Last parameter must be unbound and of AsyncOp&amp; type.
  122. * This is used to signal that the command is completed, and also for storing the return
  123. * value.
  124. *
  125. * @note Callback method also needs to call AsyncOp::markAsResolved once it is done
  126. * processing. (If it doesn't it will still be called automatically, but the return
  127. * value will default to nullptr)
  128. *
  129. * @param _notifyWhenComplete (optional) Call the notify method (provided in the call to CommandQueue::playback)
  130. * when the command is complete.
  131. * @param _callbackId (optional) Identifier for the callback so you can then later find it
  132. * if needed.
  133. *
  134. * @return Async operation object you can continuously check until the command completes. After
  135. * it completes AsyncOp::isResolved will return true and return data will be valid (if
  136. * the callback provided any).
  137. */
  138. AsyncOp queueReturn(boost::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0);
  139. /**
  140. * @brief Queue up a new command to execute. Make sure the provided function has all of its
  141. * parameters properly bound. Provided command is not expected to return a value. If you
  142. * wish to return a value from the callback use the other overload of queueCommand which
  143. * accepts AsyncOp parameter.
  144. *
  145. * @param _notifyWhenComplete (optional) Call the notify method (provided in the call to CommandQueue::playback)
  146. * when the command is complete.
  147. * @param _callbackId (optional) Identifier for the callback so you can then later find
  148. * it if needed.
  149. */
  150. void queue(boost::function<void()> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0);
  151. /**
  152. * @brief Returns a copy of all queued commands and makes room for new ones. Must be called from the thread
  153. * that created the command queue. Returned commands MUST be passed to "playback" method.
  154. */
  155. CamelotFramework::Queue<QueuedCommand>::type* flush();
  156. /**
  157. * @brief Cancels all currently queued commands.
  158. */
  159. void cancelAll();
  160. /**
  161. * @brief Returns true if no commands are queued.
  162. */
  163. bool isEmpty();
  164. protected:
  165. void throwInvalidThreadException(const String& message) const;
  166. private:
  167. CamelotFramework::Queue<QueuedCommand>::type* mCommands;
  168. bool mAllowAllThreads;
  169. CM_THREAD_ID_TYPE mMyThreadId;
  170. // Various variables that allow for easier debugging by allowing us to trigger breakpoints
  171. // when a certain command was queued.
  172. #if CM_DEBUG_MODE
  173. struct QueueBreakpoint
  174. {
  175. class HashFunction
  176. {
  177. public:
  178. size_t operator()(const QueueBreakpoint &key) const;
  179. };
  180. class EqualFunction
  181. {
  182. public:
  183. bool operator()(const QueueBreakpoint &a, const QueueBreakpoint &b) const;
  184. };
  185. QueueBreakpoint(UINT32 _queueIdx, UINT32 _commandIdx)
  186. :queueIdx(_queueIdx), commandIdx(_commandIdx)
  187. { }
  188. UINT32 queueIdx;
  189. UINT32 commandIdx;
  190. inline size_t operator()(const QueueBreakpoint& v) const;
  191. };
  192. UINT32 mMaxDebugIdx;
  193. UINT32 mCommandQueueIdx;
  194. static UINT32 MaxCommandQueueIdx;
  195. static UnorderedSet<QueueBreakpoint, QueueBreakpoint::HashFunction, QueueBreakpoint::EqualFunction>::type SetBreakpoints;
  196. CM_STATIC_MUTEX(CommandQueueBreakpointMutex);
  197. static void breakIfNeeded(UINT32 queueIdx, UINT32 commandIdx);
  198. #endif
  199. };
  200. /**
  201. * @copydoc CommandQueue
  202. */
  203. template<class SyncPolicy = CommandQueueNoSync>
  204. class CM_EXPORT CommandQueue : public CommandQueueBase, public SyncPolicy
  205. {
  206. public:
  207. /**
  208. * @copydoc CommandQueueBase::CommandQueueBase
  209. */
  210. CommandQueue(CM_THREAD_ID_TYPE threadId, bool allowAllThreads = false)
  211. :CommandQueueBase(threadId, allowAllThreads)
  212. { }
  213. ~CommandQueue()
  214. {
  215. #if CM_DEBUG_MODE
  216. #if CM_THREAD_SUPPORT != 0
  217. if(!isValidThread(getThreadId()))
  218. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  219. #endif
  220. #endif
  221. }
  222. /**
  223. * @copydoc CommandQueueBase::queueReturn
  224. */
  225. AsyncOp queueReturn(boost::function<void(AsyncOp&)> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  226. {
  227. #if CM_DEBUG_MODE
  228. #if CM_THREAD_SUPPORT != 0
  229. if(!isValidThread(getThreadId()))
  230. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  231. #endif
  232. #endif
  233. lock();
  234. AsyncOp asyncOp = CommandQueueBase::queueReturn(commandCallback, _notifyWhenComplete, _callbackId);
  235. unlock();
  236. return asyncOp;
  237. }
  238. /**
  239. * @copydoc CommandQueueBase::queue
  240. */
  241. void queue(boost::function<void()> commandCallback, bool _notifyWhenComplete = false, UINT32 _callbackId = 0)
  242. {
  243. #if CM_DEBUG_MODE
  244. #if CM_THREAD_SUPPORT != 0
  245. if(!isValidThread(getThreadId()))
  246. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  247. #endif
  248. #endif
  249. lock();
  250. CommandQueueBase::queue(commandCallback, _notifyWhenComplete, _callbackId);
  251. unlock();
  252. }
  253. /**
  254. * @copydoc CommandQueueBase::flush
  255. */
  256. CamelotFramework::Queue<QueuedCommand>::type* flush()
  257. {
  258. #if CM_DEBUG_MODE
  259. #if CM_THREAD_SUPPORT != 0
  260. if(!isValidThread(getThreadId()))
  261. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  262. #endif
  263. #endif
  264. lock();
  265. CamelotFramework::Queue<QueuedCommand>::type* commands = CommandQueueBase::flush();
  266. unlock();
  267. return commands;
  268. }
  269. /**
  270. * @copydoc CommandQueueBase::cancelAll
  271. */
  272. void cancelAll()
  273. {
  274. #if CM_DEBUG_MODE
  275. #if CM_THREAD_SUPPORT != 0
  276. if(!isValidThread(getThreadId()))
  277. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  278. #endif
  279. #endif
  280. lock();
  281. CommandQueueBase::cancelAll();
  282. unlock();
  283. }
  284. /**
  285. * @copydoc CommandQueueBase::isEmpty
  286. */
  287. bool isEmpty()
  288. {
  289. #if CM_DEBUG_MODE
  290. #if CM_THREAD_SUPPORT != 0
  291. if(!isValidThread(getThreadId()))
  292. throwInvalidThreadException("Command queue accessed outside of its creation thread.");
  293. #endif
  294. #endif
  295. lock();
  296. bool empty = CommandQueueBase::isEmpty();
  297. unlock();
  298. return empty;
  299. }
  300. };
  301. }