CmCoreThread.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. class CM_EXPORT CoreThread : public Module<CoreThread>
  13. {
  14. public:
  15. CoreThread();
  16. ~CoreThread();
  17. /**
  18. * @brief Returns the id of the core thread. If a separate core thread
  19. * is not used, then it returns the id of the thread RenderSystem
  20. * was initialized on.
  21. */
  22. CM_THREAD_ID_TYPE getCoreThreadId() { return mCoreThreadId; }
  23. /**
  24. * @brief Creates an accessor that you can use for executing commands on the core thread from
  25. * a non-core thread. You can have as many of these as you wish, the only limitation
  26. * is that you do not use a single instance on more than one thread. Each thread
  27. * requires its own accessor. The accessor will be bound to the thread you call this method on.
  28. */
  29. CoreAccessorPtr createAccessor();
  30. /**
  31. * @brief Retrieves an accessor that you can use for executing commands on the core thread from
  32. * a non-core thread. There is only one synchronized accessor and you may access it from any thread you wish.
  33. * Note however that it is much more efficient to create a separate non-synchronized accessor using
  34. * "createCoreAccessor" for each thread you will be using it on.
  35. */
  36. SyncedCoreAccessor& getSyncedAccessor();
  37. /**
  38. * @brief Queues a new command that will be added to the global command queue. You are allowed to call this from any thread,
  39. * however be aware that it involves possibly slow synchronization primitives, so limit your usage.
  40. *
  41. * @param blockUntilComplete If true the thread will be blocked until the command executes. Be aware that there be many commands queued before it
  42. * and they all need to be executed in order before the current command is reached, which might take a long time.
  43. *
  44. * @see CommandQueue::queueReturn
  45. */
  46. AsyncOp queueReturnCommand(boost::function<void(AsyncOp&)> commandCallback, bool blockUntilComplete = false);
  47. /**
  48. * @brief Queues a new command that will be added to the global command queue.You are allowed to call this from any thread,
  49. * however be aware that it involves possibly slow synchronization primitives, so limit your usage.
  50. *
  51. * @param blockUntilComplete If true the thread will be blocked until the command executes. Be aware that there be many commands queued before it
  52. * and they all need to be executed in order before the current command is reached, which might take a long time.
  53. * @see CommandQueue::queue
  54. */
  55. void queueCommand(boost::function<void()> commandCallback, bool blockUntilComplete = false);
  56. private:
  57. class CoreThreadWorkerFunc CM_THREAD_WORKER_INHERIT
  58. {
  59. public:
  60. CoreThreadWorkerFunc(CoreThread* owner);
  61. void operator()();
  62. private:
  63. CoreThread* mOwner;
  64. };
  65. CoreThreadWorkerFunc* mCoreThreadFunc;
  66. volatile bool mCoreThreadStarted;
  67. volatile bool mCoreThreadShutdown;
  68. CM_THREAD_ID_TYPE mCoreThreadId;
  69. CM_THREAD_SYNCHRONISER(mCoreThreadStartCondition)
  70. CM_MUTEX(mCoreThreadStartMutex)
  71. CM_MUTEX(mCommandQueueMutex)
  72. CM_THREAD_SYNCHRONISER(mCommandReadyCondition)
  73. CM_MUTEX(mCommandNotifyMutex)
  74. CM_THREAD_SYNCHRONISER(mCommandCompleteCondition)
  75. #if CM_THREAD_SUPPORT
  76. CM_THREAD_TYPE* mCoreThread;
  77. #endif
  78. CommandQueue<CommandQueueSync>* mCommandQueue;
  79. UINT32 mMaxCommandNotifyId; // ID that will be assigned to the next command with a notifier callback
  80. Vector<UINT32>::type mCommandsCompleted; // Completed commands that have notifier callbacks set up
  81. SyncedCoreAccessor* mSyncedCoreAccessor;
  82. /**
  83. * @brief Initializes a separate core thread. Should only be called once.
  84. */
  85. void initCoreThread();
  86. /**
  87. * @brief Main function of the core thread. Called once thread is started.
  88. */
  89. void runCoreThread();
  90. /**
  91. * @brief Shutdowns the core thread. It will complete all ready commands
  92. * before shutdown.
  93. */
  94. void shutdownCoreThread();
  95. /**
  96. * @brief Blocks the calling thread until the command with the specified ID completes.
  97. * Make sure that the specified ID actually exists, otherwise this will block forever.
  98. */
  99. void blockUntilCommandCompleted(UINT32 commandId);
  100. /**
  101. * @brief Callback called by the command list when a specific command finishes executing.
  102. * This is only called on commands that have a special notify on complete flag set.
  103. *
  104. * @param commandId Identifier for the command.
  105. */
  106. void commandCompletedNotify(UINT32 commandId);
  107. };
  108. CM_EXPORT CoreThread& gCoreThread();
  109. /**
  110. * @brief Throws an exception if current thread isn't the core thread;
  111. */
  112. CM_EXPORT void throwIfNotCoreThread();
  113. #if CM_DEBUG_MODE
  114. #define THROW_IF_NOT_CORE_THREAD throwIfNotCoreThread();
  115. #else
  116. #define THROW_IF_NOT_CORE_THREAD
  117. #endif
  118. }