CmCommandQueue.h 12 KB

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