BsCoreThreadAccessor.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsCommandQueue.h"
  4. #include "BsAsyncOp.h"
  5. namespace BansheeEngine
  6. {
  7. /** @addtogroup CoreThread
  8. * @{
  9. */
  10. /**
  11. * Contains base functionality used for CoreThreadAccessor.
  12. *
  13. * @see CoreThreadAccesor
  14. */
  15. class BS_CORE_EXPORT CoreThreadAccessorBase
  16. {
  17. public:
  18. CoreThreadAccessorBase(CommandQueueBase* commandQueue);
  19. virtual ~CoreThreadAccessorBase();
  20. /**
  21. * Queues a new generic command that will be added to the command queue. Returns an async operation object that you
  22. * may use to check if the operation has finished, and to retrieve the return value once finished.
  23. */
  24. AsyncOp queueReturnCommand(std::function<void(AsyncOp&)> commandCallback);
  25. /** Queues a new generic command that will be added to the command queue. */
  26. void queueCommand(std::function<void()> commandCallback);
  27. /**
  28. * Makes all the currently queued commands available to the core thread. They will be executed as soon as the core
  29. * thread is ready. All queued commands are removed from the accessor.
  30. *
  31. * @param[in] blockUntilComplete If true, the calling thread will block until the core thread finished executing
  32. * all currently queued commands. This is usually very expensive and should only be
  33. * used in performance non-critical code.
  34. */
  35. void submitToCoreThread(bool blockUntilComplete = false);
  36. /** Cancels all commands in the queue. */
  37. void cancelAll();
  38. private:
  39. CommandQueueBase* mCommandQueue;
  40. };
  41. /**
  42. * Core thread accessor allows you to schedule core commands outside of the core thread. Provides a set of common
  43. * methods you may want to execute on the core thread, as well as a general command queuing methods.
  44. *
  45. * @note Queued commands are only executed after the call to submitToCoreThread(), in the order they were submitted.
  46. */
  47. template <class CommandQueueSyncPolicy = CommandQueueNoSync>
  48. class BS_CORE_EXPORT CoreThreadAccessor : public CoreThreadAccessorBase
  49. {
  50. public:
  51. /**
  52. * Constructor.
  53. *
  54. * @param[in] threadId Identifier for the thread that created the accessor.
  55. */
  56. CoreThreadAccessor(BS_THREAD_ID_TYPE threadId)
  57. :CoreThreadAccessorBase(bs_new<CommandQueue<CommandQueueSyncPolicy>>(threadId))
  58. {
  59. }
  60. };
  61. /** @} */
  62. }