BsCoreThreadQueue.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "CoreThread/BsCommandQueue.h"
  6. #include "Threading/BsAsyncOp.h"
  7. namespace bs
  8. {
  9. /** @addtogroup CoreThread
  10. * @{
  11. */
  12. /** Contains base functionality used for CoreThreadQueue. */
  13. class BS_CORE_EXPORT CoreThreadQueueBase
  14. {
  15. public:
  16. CoreThreadQueueBase(CommandQueueBase* commandQueue);
  17. virtual ~CoreThreadQueueBase();
  18. /**
  19. * Queues a new generic command that will be added to the command queue. Returns an async operation object that you
  20. * may use to check if the operation has finished, and to retrieve the return value once finished.
  21. */
  22. AsyncOp queueReturnCommand(std::function<void(AsyncOp&)> commandCallback);
  23. /** Queues a new generic command that will be added to the command queue. */
  24. void queueCommand(std::function<void()> commandCallback);
  25. /**
  26. * Makes all the currently queued commands available to the core thread. They will be executed as soon as the core
  27. * thread is ready. All queued commands are removed from the queue.
  28. *
  29. * @param[in] blockUntilComplete If true, the calling thread will block until the core thread finished executing
  30. * all currently queued commands. This is usually very expensive and should only be
  31. * used in non-performance critical code.
  32. */
  33. void submitToCoreThread(bool blockUntilComplete = false);
  34. /** Cancels all commands in the queue. */
  35. void cancelAll();
  36. private:
  37. CommandQueueBase* mCommandQueue;
  38. };
  39. /**
  40. * Queue that allows the calling thread to queue commands for execution on the core thread. Commands will only be
  41. * executed after they have been submitted to the core thread.
  42. *
  43. * @note Queued commands are only executed after the call to submitToCoreThread(), in the order they were submitted.
  44. */
  45. template <class CommandQueueSyncPolicy = CommandQueueNoSync>
  46. class BS_CORE_EXPORT TCoreThreadQueue : public CoreThreadQueueBase
  47. {
  48. public:
  49. /**
  50. * Constructor.
  51. *
  52. * @param[in] threadId Identifier for the thread that created the queue.
  53. */
  54. TCoreThreadQueue(ThreadId threadId)
  55. :CoreThreadQueueBase(bs_new<CommandQueue<CommandQueueSyncPolicy>>(threadId))
  56. { }
  57. };
  58. /** @} */
  59. }