CmCoreObject.cpp 5.9 KB

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