diff --git a/Source/Engine/Content/Storage/FlaxStorage.cpp b/Source/Engine/Content/Storage/FlaxStorage.cpp index 1469753f6..47610be77 100644 --- a/Source/Engine/Content/Storage/FlaxStorage.cpp +++ b/Source/Engine/Content/Storage/FlaxStorage.cpp @@ -201,23 +201,6 @@ FlaxStorage::FlaxStorage(const StringView& path) { } -void FlaxStorage::AddRef() -{ - _refCount++; -} - -void FlaxStorage::RemoveRef() -{ - if (_refCount > 0) - { - _refCount--; - if (_refCount == 0) - { - _lastRefLostTime = DateTime::NowUTC(); - } - } -} - FlaxStorage::~FlaxStorage() { // Validate if has been disposed @@ -237,9 +220,14 @@ FlaxStorage::LockData FlaxStorage::LockSafe() return lock; } +uint32 FlaxStorage::GetRefCount() const +{ + return (uint32)Platform::AtomicRead((int64*)&_refCount); +} + bool FlaxStorage::ShouldDispose() const { - return _refCount == 0 && Platform::AtomicRead((int64*)&_chunksLock) == 0 && DateTime::NowUTC() - _lastRefLostTime >= TimeSpan::FromMilliseconds(500); + return Platform::AtomicRead((int64*)&_refCount) == 0 && Platform::AtomicRead((int64*)&_chunksLock) == 0 && DateTime::NowUTC() - _lastRefLostTime >= TimeSpan::FromMilliseconds(500); } uint32 FlaxStorage::GetMemoryUsage() const diff --git a/Source/Engine/Content/Storage/FlaxStorage.h b/Source/Engine/Content/Storage/FlaxStorage.h index 62cb524bc..01c7c7d32 100644 --- a/Source/Engine/Content/Storage/FlaxStorage.h +++ b/Source/Engine/Content/Storage/FlaxStorage.h @@ -88,9 +88,9 @@ public: protected: // State - uint32 _refCount; - DateTime _lastRefLostTime; + int64 _refCount; int64 _chunksLock; + DateTime _lastRefLostTime; CriticalSection _loadLocker; // Storage @@ -106,8 +106,18 @@ protected: private: // Used by FlaxStorageReference: - void AddRef(); - void RemoveRef(); + void AddRef() + { + Platform::InterlockedIncrement(&_refCount); + } + void RemoveRef() + { + Platform::InterlockedDecrement(&_refCount); + if (Platform::AtomicRead(&_refCount) == 0) + { + _lastRefLostTime = DateTime::NowUTC(); + } + } public: /// @@ -222,10 +232,7 @@ public: /// /// Gets the references count. /// - FORCE_INLINE uint32 GetRefCount() const - { - return _refCount; - } + uint32 GetRefCount() const; /// /// Checks if storage container should be disposed (it's not used anymore).