CmCoreObject.cpp 5.8 KB

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