Fix FlaxStorage refs counting to be atomic

This commit is contained in:
Wojciech Figat
2022-08-16 12:36:44 +02:00
parent 31fb4f6a15
commit 0b15c369d6
2 changed files with 21 additions and 26 deletions

View File

@@ -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

View File

@@ -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:
/// <summary>
@@ -222,10 +232,7 @@ public:
/// <summary>
/// Gets the references count.
/// </summary>
FORCE_INLINE uint32 GetRefCount() const
{
return _refCount;
}
uint32 GetRefCount() const;
/// <summary>
/// Checks if storage container should be disposed (it's not used anymore).