CmCoreThread.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "CmCommandQueue.h"
  5. namespace CamelotFramework
  6. {
  7. /**
  8. * @brief Manager for the core thread. Takes care of starting, running, queuing commands
  9. * and shutting down the core thread.
  10. */
  11. class CM_EXPORT CoreThread : public Module<CoreThread>
  12. {
  13. public:
  14. CoreThread();
  15. ~CoreThread();
  16. /**
  17. * @brief Returns the id of the core thread. If a separate core thread
  18. * is not used, then it returns the id of the thread RenderSystem
  19. * was initialized on.
  20. */
  21. CM_THREAD_ID_TYPE getCoreThreadId() { return mCoreThreadId; }
  22. /**
  23. * @brief Creates an accessor that you can use for executing commands on the core thread from
  24. * a non-core thread. You can have as many of these as you wish, the only limitation
  25. * is that you do not use a single instance on more than one thread. Each thread
  26. * requires its own accessor. The accessor will be bound to the thread you call this method on.
  27. */
  28. CoreAccessorPtr createAccessor();
  29. /**
  30. * @brief Retrieves an accessor that you can use for executing commands on the core thread from
  31. * a non-core thread. There is only one synchronized accessor and you may access it from any thread you wish.
  32. * Note however that it is much more efficient to create a separate non-synchronized accessor using
  33. * "createCoreAccessor" for each thread you will be using it on.
  34. */
  35. SyncedCoreAccessor& getSyncedAccessor();
  36. /**
  37. * @brief Queues a new command that will be added to the global command queue. You are allowed to call this from any thread,
  38. * however be aware that it involves possibly slow synchronization primitives, so limit your usage.
  39. *
  40. * @param blockUntilComplete If true the thread will be blocked until the command executes. Be aware that there be many commands queued before it
  41. * and they all need to be executed in order before the current command is reached, which might take a long time.
  42. *
  43. * @see CommandQueue::queueReturn
  44. */
  45. AsyncOp queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete = false);
  46. /**
  47. * @brief Queues a new command that will be added to the global command queue.You are allowed to call this from any thread,
  48. * however be aware that it involves possibly slow synchronization primitives, so limit your usage.
  49. *
  50. * @param blockUntilComplete If true the thread will be blocked until the command executes. Be aware that there be many commands queued before it
  51. * and they all need to be executed in order before the current command is reached, which might take a long time.
  52. * @see CommandQueue::queue
  53. */
  54. void queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete = false);
  55. /**
  56. * @brief Called once every frame.
  57. *
  58. * @note Must be called before sim thread schedules any CoreThread operations that frame.
  59. */
  60. void update();
  61. /**
  62. * @brief Returns a frame allocator that should be used for allocating temporary data being passed to the
  63. * core thread. As the name implies the data only lasts one frame, so you need to be careful not
  64. * to use it for longer than that.
  65. *
  66. * @note Sim thread only.
  67. */
  68. FrameAlloc* getFrameAlloc() const;
  69. private:
  70. class CoreThreadWorkerFunc CM_THREAD_WORKER_INHERIT
  71. {
  72. public:
  73. CoreThreadWorkerFunc(CoreThread* owner);
  74. void operator()();
  75. private:
  76. CoreThread* mOwner;
  77. };
  78. // Double buffered frame allocators - Means sim thread cannot be more than 1 frame ahead of core thread
  79. // (If that changes you should be able to easily add more)
  80. FrameAlloc* mFrameAllocs[2];
  81. UINT32 mActiveFrameAlloc;
  82. CoreThreadWorkerFunc* mCoreThreadFunc;
  83. volatile bool mCoreThreadStarted;
  84. volatile bool mCoreThreadShutdown;
  85. CM_THREAD_ID_TYPE mCoreThreadId;
  86. CM_THREAD_SYNCHRONISER(mCoreThreadStartCondition)
  87. CM_MUTEX(mCoreThreadStartMutex)
  88. CM_MUTEX(mCommandQueueMutex)
  89. CM_THREAD_SYNCHRONISER(mCommandReadyCondition)
  90. CM_MUTEX(mCommandNotifyMutex)
  91. CM_THREAD_SYNCHRONISER(mCommandCompleteCondition)
  92. #if CM_THREAD_SUPPORT
  93. CM_THREAD_TYPE* mCoreThread;
  94. #endif
  95. CommandQueue<CommandQueueSync>* mCommandQueue;
  96. UINT32 mMaxCommandNotifyId; // ID that will be assigned to the next command with a notifier callback
  97. Vector<UINT32>::type mCommandsCompleted; // Completed commands that have notifier callbacks set up
  98. SyncedCoreAccessor* mSyncedCoreAccessor;
  99. /**
  100. * @brief Initializes a separate core thread. Should only be called once.
  101. */
  102. void initCoreThread();
  103. /**
  104. * @brief Main function of the core thread. Called once thread is started.
  105. */
  106. void runCoreThread();
  107. /**
  108. * @brief Shutdowns the core thread. It will complete all ready commands
  109. * before shutdown.
  110. */
  111. void shutdownCoreThread();
  112. /**
  113. * @brief Blocks the calling thread until the command with the specified ID completes.
  114. * Make sure that the specified ID actually exists, otherwise this will block forever.
  115. */
  116. void blockUntilCommandCompleted(UINT32 commandId);
  117. /**
  118. * @brief Callback called by the command list when a specific command finishes executing.
  119. * This is only called on commands that have a special notify on complete flag set.
  120. *
  121. * @param commandId Identifier for the command.
  122. */
  123. void commandCompletedNotify(UINT32 commandId);
  124. };
  125. CM_EXPORT CoreThread& gCoreThread();
  126. /**
  127. * @brief Throws an exception if current thread isn't the core thread;
  128. */
  129. CM_EXPORT void throwIfNotCoreThread();
  130. /**
  131. * @brief Throws an exception if current thread is the core thread;
  132. */
  133. CM_EXPORT void throwIfCoreThread();
  134. #if CM_DEBUG_MODE
  135. #define THROW_IF_NOT_CORE_THREAD throwIfNotCoreThread();
  136. #define THROW_IF_CORE_THREAD throwIfCoreThread();
  137. #else
  138. #define THROW_IF_NOT_CORE_THREAD
  139. #define THROW_IF_CORE_THREAD
  140. #endif
  141. }