BsCoreObject.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #include "BsCoreObject.h"
  2. #include "BsCoreThread.h"
  3. #include "BsCoreObjectManager.h"
  4. #include "BsCoreThreadAccessor.h"
  5. #include "BsDebug.h"
  6. using namespace std::placeholders;
  7. namespace BansheeEngine
  8. {
  9. BS_STATIC_THREAD_SYNCHRONISER_CLASS_INSTANCE(mCoreGpuObjectLoadedCondition, CoreObject)
  10. BS_STATIC_MUTEX_CLASS_INSTANCE(mCoreGpuObjectLoadedMutex, CoreObject)
  11. CoreObject::CoreObject(bool initializeOnRenderThread)
  12. : mFlags(0), mInternalID(0), mCoreDirtyFlags(0xFFFFFFFF), mCoreSpecific(nullptr)
  13. {
  14. mInternalID = CoreObjectManager::instance().registerObject(this);
  15. mFlags = initializeOnRenderThread ? mFlags | CGO_INIT_ON_CORE_THREAD : mFlags;
  16. }
  17. CoreObject::~CoreObject()
  18. {
  19. if(isInitialized())
  20. {
  21. // Object must be released with destroy() otherwise engine can still try to use it, even if it was destructed
  22. // (e.g. if an object has one of its methods queued in a command queue, and is destructed, you will be accessing invalid memory)
  23. BS_EXCEPT(InternalErrorException, "Destructor called but object is not destroyed. This will result in nasty issues.");
  24. }
  25. #if BS_DEBUG_MODE
  26. if(!mThis.expired())
  27. {
  28. BS_EXCEPT(InternalErrorException, "Shared pointer to this object still has active references but " \
  29. "the object is being deleted? You shouldn't delete CoreObjects manually.");
  30. }
  31. #endif
  32. if (mCoreSpecific != nullptr)
  33. {
  34. bs_delete(mCoreSpecific);
  35. mCoreSpecific = nullptr;
  36. }
  37. CoreObjectManager::instance().unregisterObject(this);
  38. }
  39. void CoreObject::destroy()
  40. {
  41. if(requiresInitOnCoreThread())
  42. {
  43. setScheduledToBeDeleted(true);
  44. if(BS_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  45. mThis.lock()->destroy_internal();
  46. else
  47. queueDestroyGpuCommand(mThis.lock());
  48. }
  49. else
  50. {
  51. destroy_internal();
  52. }
  53. }
  54. void CoreObject::destroy_internal()
  55. {
  56. #if BS_DEBUG_MODE
  57. if(!isInitialized())
  58. BS_EXCEPT(InternalErrorException, "Trying to destroy an object that is already destroyed (or it never was initialized).");
  59. #endif
  60. if (mCoreSpecific != nullptr)
  61. mCoreSpecific->destroy();
  62. setIsInitialized(false);
  63. }
  64. void CoreObject::initialize()
  65. {
  66. #if BS_DEBUG_MODE
  67. if(isInitialized() || isScheduledToBeInitialized())
  68. BS_EXCEPT(InternalErrorException, "Trying to initialize an object that is already initialized.");
  69. #endif
  70. mCoreSpecific = createCore();
  71. if(requiresInitOnCoreThread())
  72. {
  73. setScheduledToBeInitialized(true);
  74. if(BS_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  75. mThis.lock()->initialize_internal();
  76. else
  77. queueInitializeGpuCommand(mThis.lock());
  78. }
  79. else
  80. {
  81. initialize_internal();
  82. }
  83. }
  84. void CoreObject::initialize_internal()
  85. {
  86. if (mCoreSpecific != nullptr)
  87. mCoreSpecific->initialize();
  88. if(requiresInitOnCoreThread())
  89. {
  90. {
  91. BS_LOCK_MUTEX(mCoreGpuObjectLoadedMutex);
  92. setIsInitialized(true);
  93. }
  94. setScheduledToBeInitialized(false);
  95. BS_THREAD_NOTIFY_ALL(mCoreGpuObjectLoadedCondition);
  96. }
  97. else
  98. {
  99. setIsInitialized(true);
  100. }
  101. }
  102. void CoreObject::synchronize()
  103. {
  104. if(!isInitialized())
  105. {
  106. if(requiresInitOnCoreThread())
  107. {
  108. #if BS_DEBUG_MODE
  109. if(BS_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  110. BS_EXCEPT(InternalErrorException, "You cannot call this method on the core thread. It will cause a deadlock!");
  111. #endif
  112. BS_LOCK_MUTEX_NAMED(mCoreGpuObjectLoadedMutex, lock);
  113. while(!isInitialized())
  114. {
  115. if(!isScheduledToBeInitialized())
  116. BS_EXCEPT(InternalErrorException, "Attempting to wait until initialization finishes but object is not scheduled to be initialized.");
  117. BS_THREAD_WAIT(mCoreGpuObjectLoadedCondition, mCoreGpuObjectLoadedMutex, lock);
  118. }
  119. }
  120. else
  121. {
  122. BS_EXCEPT(InternalErrorException, "Attempting to wait until initialization finishes but object is not scheduled to be initialized.");
  123. }
  124. }
  125. }
  126. void CoreObject::_setThisPtr(std::shared_ptr<CoreObject> ptrThis)
  127. {
  128. mThis = ptrThis;
  129. }
  130. void CoreObject::_deleteDelayedInternal(CoreObject* obj)
  131. {
  132. assert(obj != nullptr);
  133. // This method usually gets called automatically by the shared pointer when all references are released. The process:
  134. // - If the object wasn't initialized delete it right away
  135. // - Otherwise:
  136. // - We re-create the reference to the object by setting mThis pointer
  137. // - We queue the object to be destroyed so all of its GPU resources may be released on the core thread
  138. // - destroy() makes sure it keeps a reference of mThis so object isn't deleted
  139. // - Once the destroy() finishes the reference is removed and the default shared_ptr deleter is called
  140. #if BS_DEBUG_MODE
  141. if(obj->isScheduledToBeInitialized())
  142. {
  143. BS_EXCEPT(InternalErrorException, "Object scheduled to be initialized, yet it's being deleted. " \
  144. "By design objects queued in the command queue should always have a reference count >= 1, therefore never be deleted " \
  145. "while still in the queue.");
  146. }
  147. #endif
  148. }
  149. void CoreObject::queueGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void()> func)
  150. {
  151. // We call another internal method and go through an additional layer of abstraction in order to keep an active
  152. // reference to the obj (saved in the bound function).
  153. // We could have called the function directly using "this" pointer but then we couldn't have used a shared_ptr for the object,
  154. // in which case there is a possibility that the object would be released and deleted while still being in the command queue.
  155. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  156. }
  157. AsyncOp CoreObject::queueReturnGpuCommand(std::shared_ptr<CoreObject>& obj, std::function<void(AsyncOp&)> func)
  158. {
  159. // See queueGpuCommand
  160. return gCoreAccessor().queueReturnCommand(std::bind(&CoreObject::executeReturnGpuCommand, obj, func, _1));
  161. }
  162. void CoreObject::queueInitializeGpuCommand(std::shared_ptr<CoreObject>& obj)
  163. {
  164. std::function<void()> func = std::bind(&CoreObject::initialize_internal, obj.get());
  165. CoreThread::instance().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  166. }
  167. void CoreObject::queueDestroyGpuCommand(std::shared_ptr<CoreObject>& obj)
  168. {
  169. std::function<void()> func = std::bind(&CoreObject::destroy_internal, obj.get());
  170. gCoreAccessor().queueCommand(std::bind(&CoreObject::executeGpuCommand, obj, func));
  171. }
  172. void CoreObject::executeGpuCommand(std::shared_ptr<CoreObject> obj, std::function<void()> func)
  173. {
  174. volatile std::shared_ptr<CoreObject> objParam = obj; // Makes sure obj isn't optimized out?
  175. func();
  176. }
  177. void CoreObject::executeReturnGpuCommand(std::shared_ptr<CoreObject> obj, std::function<void(AsyncOp&)> func, AsyncOp& op)
  178. {
  179. volatile std::shared_ptr<CoreObject> objParam = obj; // Makes sure obj isn't optimized out?
  180. func(op);
  181. }
  182. }