BsCoreObjectCore.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsAsyncOp.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup CoreThread
  7. * @{
  8. */
  9. /**
  10. * Represents counterpart of a CoreObject that is meant to be used specifically on the core thread.
  11. *
  12. * @note Core thread only.
  13. * @note Different CoreObject implementations should implement this class for their own needs.
  14. */
  15. class BS_CORE_EXPORT CoreObjectCore
  16. {
  17. protected:
  18. /** Values that represent current state of the object */
  19. enum Flags
  20. {
  21. CGCO_INITIALIZED = 0x01, /**< Object has been initialized and can be used. */
  22. CGCO_SCHEDULED_FOR_INIT = 0x02 /**< Object has been scheduled for initialization but core thread has not completed it yet. */
  23. };
  24. public:
  25. CoreObjectCore();
  26. virtual ~CoreObjectCore();
  27. /** Called on the core thread when the object is first created. */
  28. virtual void initialize();
  29. /** Returns a shared_ptr version of "this" pointer. */
  30. SPtr<CoreObjectCore> getThisPtr() const { return mThis.lock(); }
  31. /** @cond INTERNAL */
  32. /**
  33. * Sets a shared this pointer to this object. This MUST be called immediately after construction.
  34. *
  35. * @note Internal method.
  36. * @note Called automatically by the factory creation methods so user should not call this manually.
  37. */
  38. void _setThisPtr(std::shared_ptr<CoreObjectCore> ptrThis);
  39. /** @endcond */
  40. protected:
  41. friend class CoreObjectManager;
  42. friend class CoreObject;
  43. /**
  44. * Update internal data from provided memory buffer that was populated with data from the sim thread.
  45. *
  46. * @note
  47. * This generally happens at the start of a core thread frame. Data used was recorded on the previous sim thread
  48. * frame.
  49. */
  50. virtual void syncToCore(const CoreSyncData& data) { }
  51. /**
  52. * Blocks the current thread until the resource is fully initialized.
  53. *
  54. * @note
  55. * If you call this without calling initialize first a deadlock will occur. You should not call this from core thread.
  56. */
  57. void synchronize();
  58. /**
  59. * Returns true if the object has been properly initialized. Methods are not allowed to be called on the object
  60. * until it is initialized.
  61. */
  62. bool isInitialized() const { return (mFlags & CGCO_INITIALIZED) != 0; }
  63. bool isScheduledToBeInitialized() const { return (mFlags & CGCO_SCHEDULED_FOR_INIT) != 0; }
  64. void setIsInitialized(bool initialized) { mFlags = initialized ? mFlags | CGCO_INITIALIZED : mFlags & ~CGCO_INITIALIZED; }
  65. void setScheduledToBeInitialized(bool scheduled) { mFlags = scheduled ? mFlags | CGCO_SCHEDULED_FOR_INIT : mFlags & ~CGCO_SCHEDULED_FOR_INIT; }
  66. volatile UINT8 mFlags;
  67. std::weak_ptr<CoreObjectCore> mThis;
  68. BS_STATIC_THREAD_SYNCHRONISER(mCoreGpuObjectLoadedCondition)
  69. BS_STATIC_MUTEX(mCoreGpuObjectLoadedMutex)
  70. };
  71. /** @} */
  72. }