BsCoreThreadAccessor.h 2.4 KB

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