2
0

BsCoreThreadAccessor.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. Provides a set of common
  45. * methods you may want to execute on the core thread, as well as a general command queuing methods.
  46. *
  47. * @note Queued commands are only executed after the call to submitToCoreThread(), in the order they were submitted.
  48. */
  49. template <class CommandQueueSyncPolicy = CommandQueueNoSync>
  50. class BS_CORE_EXPORT CoreThreadAccessor : public CoreThreadAccessorBase
  51. {
  52. public:
  53. /**
  54. * Constructor.
  55. *
  56. * @param[in] threadId Identifier for the thread that created the accessor.
  57. */
  58. CoreThreadAccessor(ThreadId threadId)
  59. :CoreThreadAccessorBase(bs_new<CommandQueue<CommandQueueSyncPolicy>>(threadId))
  60. {
  61. }
  62. };
  63. /** @} */
  64. }