BsCoreObjectManager.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsCoreObjectCore.h"
  4. #include "BsModule.h"
  5. namespace BansheeEngine
  6. {
  7. // TODO Low priority - Add debug option that would remember a call stack for each resource initialization,
  8. // so when we fail to release one we know which one it is.
  9. /**
  10. * @brief Determines type of synchronization to perform when syncing
  11. * dirty data between core and sim threads.
  12. */
  13. enum CoreObjectSync
  14. {
  15. Sim, /** Syncing sim thread data to core thread. */
  16. Core /** Syncing core thread data to sim thread. */
  17. };
  18. /**
  19. * @brief Manager that keeps track of all active CoreObjects.
  20. *
  21. * @note Internal class. Thread safe unless specified otherwise.
  22. */
  23. class BS_CORE_EXPORT CoreObjectManager : public Module<CoreObjectManager>
  24. {
  25. /**
  26. * @brief Stores dirty data that is to be transferred from core
  27. * thread to sim thread part of a CoreObject.
  28. */
  29. struct SimStoredSyncData
  30. {
  31. SimStoredSyncData()
  32. :destinationObj(nullptr)
  33. { }
  34. SimStoredSyncData(CoreObject* destObj, const CoreSyncData& syncData)
  35. :destinationObj(destObj), syncData(syncData)
  36. { }
  37. CoreObject* destinationObj;
  38. CoreSyncData syncData;
  39. };
  40. /**
  41. * @brief Stores dirty data that is to be transferred from sim
  42. * thread to core thread part of a CoreObject.
  43. */
  44. struct CoreStoredSyncData
  45. {
  46. CoreStoredSyncData()
  47. :destinationObj(nullptr)
  48. { }
  49. CoreStoredSyncData(CoreObjectCore* destObj, const CoreSyncData& syncData)
  50. :destinationObj(destObj), syncData(syncData)
  51. { }
  52. CoreObjectCore* destinationObj;
  53. CoreSyncData syncData;
  54. };
  55. public:
  56. CoreObjectManager();
  57. ~CoreObjectManager();
  58. /**
  59. * @brief Registers a new CoreObject notifying the manager the object
  60. * is created.
  61. */
  62. UINT64 registerObject(CoreObject* object);
  63. /**
  64. * @brief Unregisters a CoreObject notifying the manager the object
  65. * is destroyed.
  66. */
  67. void unregisterObject(CoreObject* object);
  68. /**
  69. * @brief Stores all syncable data from dirty core objects into memory allocated
  70. * by the provided allocator. Additional meta-data is stored internally to be
  71. * used by call to syncUpload.
  72. *
  73. * @param type Determines where to copy the dirty data from. If set to Sim the data copied
  74. * will be data accessible by the sim thread, and if set to Core the data copied
  75. * will be data accessible by the core thread.
  76. * @param allocator Allocator to use for allocating memory for stored data.
  77. *
  78. * @note Not thread safe. If used with Sim type it should only be called from
  79. * sim thread, and if used with Core type it should only be called from
  80. * core thread.
  81. *
  82. * Must be followed by a call to syncUpload with the same type.
  83. */
  84. void syncDownload(CoreObjectSync type, FrameAlloc* allocator);
  85. /**
  86. * @brief Copies all the data stored by previous call to "syncDownload"
  87. * into CoreObjects or their core thread versions.
  88. *
  89. * @param type Determines where to copy the stored data to. This will be the opposite
  90. * data source than used by "syncDownload". If set to Sim the data will be
  91. * copied to data accessible by core thread, and if set to Core the data
  92. * will be copied to data accessible by the sim thread.
  93. *
  94. * @note Not thread safe. If used with Sim type it should only be called from
  95. * core thread, and if used with Core type it should only be called from
  96. * sim thread.
  97. */
  98. void syncUpload(CoreObjectSync type);
  99. private:
  100. UINT64 mNextAvailableID;
  101. Map<UINT64, CoreObject*> mObjects;
  102. FrameAlloc* mSimSyncDataAlloc;
  103. Map<UINT64, SimStoredSyncData> mSimSyncData;
  104. FrameAlloc* mCoreSyncDataAlloc;
  105. Map<UINT64, CoreStoredSyncData> mCoreSyncData;
  106. BS_MUTEX(mObjectsMutex);
  107. };
  108. }