CmCoreObject.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. if(CM_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  39. mThis.lock()->destroy_internal();
  40. else
  41. queueDestroyGpuCommand(mThis.lock());
  42. }
  43. else
  44. {
  45. destroy_internal();
  46. }
  47. }
  48. void CoreObject::destroy_internal()
  49. {
  50. #if CM_DEBUG_MODE
  51. if(!isInitialized())
  52. CM_EXCEPT(InternalErrorException, "Trying to destroy an object that is already destroyed (or it never was initialized).");
  53. #endif
  54. setIsInitialized(false);
  55. }
  56. void CoreObject::initialize()
  57. {
  58. #if CM_DEBUG_MODE
  59. if(isInitialized() || isScheduledToBeInitialized())
  60. CM_EXCEPT(InternalErrorException, "Trying to initialize an object that is already initialized.");
  61. #endif
  62. if(requiresInitOnRenderThread())
  63. {
  64. setScheduledToBeInitialized(true);
  65. if(CM_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  66. mThis.lock()->initialize_internal();
  67. else
  68. queueInitializeGpuCommand(mThis.lock());
  69. }
  70. else
  71. {
  72. initialize_internal();
  73. }
  74. }
  75. void CoreObject::initialize_internal()
  76. {
  77. if(requiresInitOnRenderThread())
  78. {
  79. {
  80. CM_LOCK_MUTEX(mCoreGpuObjectLoadedMutex);
  81. setIsInitialized(true);
  82. }
  83. setScheduledToBeInitialized(false);
  84. CM_THREAD_NOTIFY_ALL(mCoreGpuObjectLoadedCondition);
  85. }
  86. else
  87. {
  88. setIsInitialized(true);
  89. }
  90. }
  91. void CoreObject::synchronize()
  92. {
  93. if(!isInitialized())
  94. {
  95. if(requiresInitOnRenderThread())
  96. {
  97. #if CM_DEBUG_MODE
  98. if(CM_THREAD_CURRENT_ID == CoreThread::instance().getCoreThreadId())
  99. CM_EXCEPT(InternalErrorException, "You cannot call this method on the core thread. It will cause a deadlock!");
  100. #endif
  101. CM_LOCK_MUTEX_NAMED(mCoreGpuObjectLoadedMutex, lock);
  102. while(!isInitialized())
  103. {
  104. if(!isScheduledToBeInitialized())
  105. CM_EXCEPT(InternalErrorException, "Attempting to wait until initialization finishes but object is not scheduled to be initialized.");
  106. CM_THREAD_WAIT(mCoreGpuObjectLoadedCondition, mCoreGpuObjectLoadedMutex, lock);
  107. }
  108. }
  109. else
  110. {
  111. CM_EXCEPT(InternalErrorException, "Attempting to wait until initialization finishes but object is not scheduled to be initialized.");
  112. }
  113. }
  114. }
  115. void CoreObject::setThisPtr(std::shared_ptr<CoreObject> ptrThis)
  116. {
  117. mThis = ptrThis;
  118. }
  119. void CoreObject::_deleteDelayedInternal(CoreObject* obj)
  120. {
  121. assert(obj != nullptr);
  122. // This method usually gets called automatically by the shared pointer when all references are released. The process:
  123. // - If the object wasn't initialized delete it right away
  124. // - Otherwise:
  125. // - We re-create the reference to the object by setting mThis pointer
  126. // - We queue the object to be destroyed so all of its GPU resources may be released on the core thread
  127. // - destroy() makes sure it keeps a reference of mThis so object isn't deleted
  128. // - Once the destroy() finishes the reference is removed and the default shared_ptr deleter is called
  129. #if CM_DEBUG_MODE
  130. if(obj->isScheduledToBeInitialized())
  131. {
  132. CM_EXCEPT(InternalErrorException, "Object scheduled to be initialized, yet it's being deleted. " \
  133. "By design objects queued in the command queue should always have a reference count >= 1, therefore never be deleted " \
  134. "while still in the queue.");
  135. }
  136. #endif
  137. }
  138. void CoreObject::queueGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void()> func)
  139. {
  140. // We call another internal method and go through an additional layer of abstraction in order to keep an active
  141. // reference to the obj (saved in the bound function).
  142. // We could have called the function directly using "this" pointer but then we couldn't have used a shared_ptr for the object,
  143. // in which case there is a possibility that the object would be released and deleted while still being in the command queue.
  144. gCoreAccessor().queueCommand(boost::bind(&CoreObject::executeGpuCommand, obj, func));
  145. }
  146. AsyncOp CoreObject::queueReturnGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void(AsyncOp&)> func)
  147. {
  148. // See queueGpuCommand
  149. return gCoreAccessor().queueReturnCommand(boost::bind(&CoreObject::executeReturnGpuCommand, obj, func, _1));
  150. }
  151. void CoreObject::queueInitializeGpuCommand(std::shared_ptr<CoreObject>& obj)
  152. {
  153. boost::function<void()> func = boost::bind(&CoreObject::initialize_internal, obj.get());
  154. CoreThread::instance().queueCommand(boost::bind(&CoreObject::executeGpuCommand, obj, func));
  155. }
  156. void CoreObject::queueDestroyGpuCommand(std::shared_ptr<CoreObject>& obj)
  157. {
  158. boost::function<void()> func = boost::bind(&CoreObject::destroy_internal, obj.get());
  159. gCoreAccessor().queueCommand(boost::bind(&CoreObject::executeGpuCommand, obj, func));
  160. }
  161. void CoreObject::executeGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void()> func)
  162. {
  163. volatile std::shared_ptr<CoreObject> objParam = obj; // Makes sure obj isn't optimized out?
  164. func();
  165. }
  166. void CoreObject::executeReturnGpuCommand(std::shared_ptr<CoreObject>& obj, boost::function<void(AsyncOp&)> func, AsyncOp& op)
  167. {
  168. volatile std::shared_ptr<CoreObject> objParam = obj; // Makes sure obj isn't optimized out?
  169. func(op);
  170. }
  171. }