BsCoreObjectCore.h 3.0 KB

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