Fix asset references to use separate lightweight locking instead of full asset mutex

This commit is contained in:
Wojtek Figat
2025-06-29 19:16:23 +02:00
parent f126a83b79
commit 43d11264f8
2 changed files with 6 additions and 4 deletions

View File

@@ -242,9 +242,8 @@ void Asset::AddReference(IAssetReference* ref, bool week)
if (ref) if (ref)
{ {
//PROFILE_MEM(EngineDelegate); // Include references tracking memory within Delegate memory //PROFILE_MEM(EngineDelegate); // Include references tracking memory within Delegate memory
Locker.Lock(); ScopeLock lock(_referencesLocker);
_references.Add(ref); _references.Add(ref);
Locker.Unlock();
} }
} }
@@ -257,9 +256,8 @@ void Asset::RemoveReference(IAssetReference* ref, bool week)
{ {
if (ref) if (ref)
{ {
Locker.Lock(); ScopeLock lock(_referencesLocker);
_references.Remove(ref); _references.Remove(ref);
Locker.Unlock();
} }
if (!week) if (!week)
Platform::InterlockedDecrement(&_refCount); Platform::InterlockedDecrement(&_refCount);
@@ -681,6 +679,7 @@ void Asset::onLoaded_MainThread()
ASSERT(IsInMainThread()); ASSERT(IsInMainThread());
// Send event // Send event
ScopeLock lock(_referencesLocker);
for (const auto& e : _references) for (const auto& e : _references)
e.Item->OnAssetLoaded(this, this); e.Item->OnAssetLoaded(this, this);
OnLoaded(this); OnLoaded(this);
@@ -696,6 +695,7 @@ void Asset::onUnload_MainThread()
CancelStreaming(); CancelStreaming();
// Send event // Send event
ScopeLock lock(_referencesLocker);
for (const auto& e : _references) for (const auto& e : _references)
e.Item->OnAssetUnloaded(this, this); e.Item->OnAssetUnloaded(this, this);
OnUnloaded(this); OnUnloaded(this);

View File

@@ -7,6 +7,7 @@
#include "Engine/Core/Types/String.h" #include "Engine/Core/Types/String.h"
#include "Engine/Platform/CriticalSection.h" #include "Engine/Platform/CriticalSection.h"
#include "Engine/Scripting/ScriptingObject.h" #include "Engine/Scripting/ScriptingObject.h"
#include "Engine/Threading/ConcurrentSystemLocker.h"
#include "Config.h" #include "Config.h"
#include "Types.h" #include "Types.h"
@@ -63,6 +64,7 @@ protected:
int8 _isVirtual : 1; // Indicates that asset is pure virtual (generated or temporary, has no storage so won't be saved) int8 _isVirtual : 1; // Indicates that asset is pure virtual (generated or temporary, has no storage so won't be saved)
HashSet<IAssetReference*> _references; HashSet<IAssetReference*> _references;
CriticalSection _referencesLocker; // TODO: convert into a single interlocked exchange for the current thread owning lock
public: public:
/// <summary> /// <summary>