CmCoreThread.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmModule.h"
  4. #include "CmCommandQueue.h"
  5. #include "CmCoreThreadAccessor.h"
  6. namespace CamelotFramework
  7. {
  8. /**
  9. * @brief Manager for the core thread. Takes care of starting, running, queuing commands
  10. * and shutting down the core thread.
  11. *
  12. * @note How threading works:
  13. * - This class contains a queue which is filled by commands from other threads via queueCommand and queueReturnCommand
  14. * - Commands are executed on the core thread as soon as they are queued (if core thread is not busy with previous commands)
  15. * - Core thread accessors are helpers for queuing commands. They serve two purposes:
  16. * - They contain helper methods for various common Core thread commands.
  17. * - They perform better than queuing each command directly using queueCommand or queueReturnCommand
  18. * - Accessors contain a command queue of their own, and queuing commands in them will not automatically start executing the commands
  19. * like with queueCommand or queueReturnCommand. Instead you must manually call "submitAccessors" when you are ready to send their
  20. * commands to the core thread.
  21. * - Synced accessor is a special type of accessor which may be accessed from any thread. Its commands are always executed after all other
  22. * non-synced accessors. It is primarily useful when multiple threads are managing the same resource and you must ensure proper order of operations.
  23. * You should use normal accessors whenever possible as synced accessors involve potentially slow synchronization operations.
  24. */
  25. class CoreThread : public Module<CoreThread>
  26. {
  27. struct AccessorContainer
  28. {
  29. CoreAccessorPtr accessor;
  30. };
  31. public:
  32. CM_EXPORT CoreThread();
  33. CM_EXPORT ~CoreThread();
  34. /**
  35. * @brief Returns the id of the core thread. If a separate core thread
  36. * is not used, then it returns the id of the thread RenderSystem
  37. * was initialized on.
  38. */
  39. CM_EXPORT CM_THREAD_ID_TYPE getCoreThreadId() { return mCoreThreadId; }
  40. /**
  41. * @brief Creates or retrieves an accessor that you can use for executing commands on the core thread from
  42. * a non-core thread. The accessor will be bound to the thread you call this method on.
  43. *
  44. * @note Accessors contain their own command queue and their commands will only start to get executed once that queue is submitted
  45. * to the core thread via "submitAccessors" method.
  46. */
  47. CM_EXPORT CoreAccessorPtr getAccessor();
  48. /**
  49. * @brief Retrieves an accessor that you can use for executing commands on the core thread from
  50. * a non-core thread. There is only one synchronized accessor and you may access it from any thread you wish.
  51. * Note however that it is much more efficient to create a separate non-synchronized accessor using
  52. * "createCoreAccessor" for each thread you will be using it on.
  53. *
  54. * @note Accessors contain their own command queue and their commands will only start to get executed once that queue is submitted
  55. * to the core thread via "submitAccessors" method.
  56. *
  57. * Synced accessor commands are sent after all non-synced accessor commands are sent.
  58. */
  59. CM_EXPORT SyncedCoreAccessor& getSyncedAccessor();
  60. /**
  61. * @brief Queues all the accessor commands and starts executing them on the core thread.
  62. */
  63. CM_EXPORT void submitAccessors(bool blockUntilComplete = false);
  64. /**
  65. * @brief Queues a new command that will be added to the global command queue. You are allowed to call this from any thread,
  66. * however be aware that it involves possibly slow synchronization primitives, so limit your usage.
  67. *
  68. * @param blockUntilComplete If true the thread will be blocked until the command executes. Be aware that there may be many commands queued before it
  69. * and they all need to be executed in order before the current command is reached, which might take a long time.
  70. *
  71. * @see CommandQueue::queueReturn
  72. */
  73. CM_EXPORT AsyncOp queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete = false);
  74. /**
  75. * @brief Queues a new command that will be added to the global command queue.You are allowed to call this from any thread,
  76. * however be aware that it involves possibly slow synchronization primitives, so limit your usage.
  77. *
  78. * @param blockUntilComplete If true the thread will be blocked until the command executes. Be aware that there may be many commands queued before it
  79. * and they all need to be executed in order before the current command is reached, which might take a long time.
  80. * @see CommandQueue::queue
  81. */
  82. CM_EXPORT void queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete = false);
  83. /**
  84. * @brief Called once every frame.
  85. *
  86. * @note Must be called before sim thread schedules any CoreThread operations that frame.
  87. */
  88. CM_EXPORT void update();
  89. /**
  90. * @brief Returns a frame allocator that should be used for allocating temporary data being passed to the
  91. * core thread. As the name implies the data only lasts one frame, so you need to be careful not
  92. * to use it for longer than that.
  93. *
  94. * @note Sim thread only.
  95. */
  96. CM_EXPORT FrameAlloc* getFrameAlloc() const;
  97. private:
  98. class CoreThreadWorkerFunc CM_THREAD_WORKER_INHERIT
  99. {
  100. public:
  101. CoreThreadWorkerFunc(CoreThread* owner);
  102. void operator()();
  103. private:
  104. CoreThread* mOwner;
  105. };
  106. // Double buffered frame allocators - Means sim thread cannot be more than 1 frame ahead of core thread
  107. // (If that changes you should be able to easily add more)
  108. FrameAlloc* mFrameAllocs[2];
  109. UINT32 mActiveFrameAlloc;
  110. static CM_THREADLOCAL AccessorContainer* mAccessor;
  111. Vector<AccessorContainer*>::type mAccessors;
  112. CoreThreadWorkerFunc* mCoreThreadFunc;
  113. volatile bool mCoreThreadStarted;
  114. volatile bool mCoreThreadShutdown;
  115. CM_THREAD_ID_TYPE mCoreThreadId;
  116. CM_THREAD_SYNCHRONISER(mCoreThreadStartCondition)
  117. CM_MUTEX(mCoreThreadStartMutex)
  118. CM_MUTEX(mCommandQueueMutex)
  119. CM_MUTEX(mAccessorMutex)
  120. CM_THREAD_SYNCHRONISER(mCommandReadyCondition)
  121. CM_MUTEX(mCommandNotifyMutex)
  122. CM_THREAD_SYNCHRONISER(mCommandCompleteCondition)
  123. #if CM_THREAD_SUPPORT
  124. CM_THREAD_TYPE* mCoreThread;
  125. #endif
  126. CommandQueue<CommandQueueSync>* mCommandQueue;
  127. UINT32 mMaxCommandNotifyId; // ID that will be assigned to the next command with a notifier callback
  128. Vector<UINT32>::type mCommandsCompleted; // Completed commands that have notifier callbacks set up
  129. SyncedCoreAccessor* mSyncedCoreAccessor;
  130. /**
  131. * @brief Initializes a separate core thread. Should only be called once.
  132. */
  133. void initCoreThread();
  134. /**
  135. * @brief Main function of the core thread. Called once thread is started.
  136. */
  137. void runCoreThread();
  138. /**
  139. * @brief Shutdowns the core thread. It will complete all ready commands
  140. * before shutdown.
  141. */
  142. void shutdownCoreThread();
  143. /**
  144. * @brief Blocks the calling thread until the command with the specified ID completes.
  145. * Make sure that the specified ID actually exists, otherwise this will block forever.
  146. */
  147. void blockUntilCommandCompleted(UINT32 commandId);
  148. /**
  149. * @brief Callback called by the command list when a specific command finishes executing.
  150. * This is only called on commands that have a special notify on complete flag set.
  151. *
  152. * @param commandId Identifier for the command.
  153. */
  154. void commandCompletedNotify(UINT32 commandId);
  155. };
  156. CM_EXPORT CoreThread& gCoreThread();
  157. CM_EXPORT CoreThreadAccessor<CommandQueueNoSync>& gCoreAccessor();
  158. CM_EXPORT CoreThreadAccessor<CommandQueueSync>& gSyncedCoreAccessor();
  159. /**
  160. * @brief Throws an exception if current thread isn't the core thread;
  161. */
  162. CM_EXPORT void throwIfNotCoreThread();
  163. /**
  164. * @brief Throws an exception if current thread is the core thread;
  165. */
  166. CM_EXPORT void throwIfCoreThread();
  167. #if CM_DEBUG_MODE
  168. #define THROW_IF_NOT_CORE_THREAD throwIfNotCoreThread();
  169. #define THROW_IF_CORE_THREAD throwIfCoreThread();
  170. #else
  171. #define THROW_IF_NOT_CORE_THREAD
  172. #define THROW_IF_CORE_THREAD
  173. #endif
  174. }