CmCoreThread.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. private:
  56. class CoreThreadWorkerFunc CM_THREAD_WORKER_INHERIT
  57. {
  58. public:
  59. CoreThreadWorkerFunc(CoreThread* owner);
  60. void operator()();
  61. private:
  62. CoreThread* mOwner;
  63. };
  64. CoreThreadWorkerFunc* mCoreThreadFunc;
  65. volatile bool mCoreThreadStarted;
  66. volatile bool mCoreThreadShutdown;
  67. CM_THREAD_ID_TYPE mCoreThreadId;
  68. CM_THREAD_SYNCHRONISER(mCoreThreadStartCondition)
  69. CM_MUTEX(mCoreThreadStartMutex)
  70. CM_MUTEX(mCommandQueueMutex)
  71. CM_THREAD_SYNCHRONISER(mCommandReadyCondition)
  72. CM_MUTEX(mCommandNotifyMutex)
  73. CM_THREAD_SYNCHRONISER(mCommandCompleteCondition)
  74. #if CM_THREAD_SUPPORT
  75. CM_THREAD_TYPE* mCoreThread;
  76. #endif
  77. CommandQueue<CommandQueueSync>* mCommandQueue;
  78. UINT32 mMaxCommandNotifyId; // ID that will be assigned to the next command with a notifier callback
  79. Vector<UINT32>::type mCommandsCompleted; // Completed commands that have notifier callbacks set up
  80. SyncedCoreAccessor* mSyncedCoreAccessor;
  81. /**
  82. * @brief Initializes a separate core thread. Should only be called once.
  83. */
  84. void initCoreThread();
  85. /**
  86. * @brief Main function of the core thread. Called once thread is started.
  87. */
  88. void runCoreThread();
  89. /**
  90. * @brief Shutdowns the core thread. It will complete all ready commands
  91. * before shutdown.
  92. */
  93. void shutdownCoreThread();
  94. /**
  95. * @brief Blocks the calling thread until the command with the specified ID completes.
  96. * Make sure that the specified ID actually exists, otherwise this will block forever.
  97. */
  98. void blockUntilCommandCompleted(UINT32 commandId);
  99. /**
  100. * @brief Callback called by the command list when a specific command finishes executing.
  101. * This is only called on commands that have a special notify on complete flag set.
  102. *
  103. * @param commandId Identifier for the command.
  104. */
  105. void commandCompletedNotify(UINT32 commandId);
  106. };
  107. CM_EXPORT CoreThread& gCoreThread();
  108. /**
  109. * @brief Throws an exception if current thread isn't the core thread;
  110. */
  111. CM_EXPORT void throwIfNotCoreThread();
  112. #if CM_DEBUG_MODE
  113. #define THROW_IF_NOT_CORE_THREAD throwIfNotCoreThread();
  114. #else
  115. #define THROW_IF_NOT_CORE_THREAD
  116. #endif
  117. }