From 43d11264f82ac2ffb3b4f94d77da427f32a05ac2 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sun, 29 Jun 2025 19:16:23 +0200 Subject: [PATCH] Fix asset references to use separate lightweight locking instead of full asset mutex --- Source/Engine/Content/Asset.cpp | 8 ++++---- Source/Engine/Content/Asset.h | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Content/Asset.cpp b/Source/Engine/Content/Asset.cpp index 5a9e62993..559a673da 100644 --- a/Source/Engine/Content/Asset.cpp +++ b/Source/Engine/Content/Asset.cpp @@ -242,9 +242,8 @@ void Asset::AddReference(IAssetReference* ref, bool week) if (ref) { //PROFILE_MEM(EngineDelegate); // Include references tracking memory within Delegate memory - Locker.Lock(); + ScopeLock lock(_referencesLocker); _references.Add(ref); - Locker.Unlock(); } } @@ -257,9 +256,8 @@ void Asset::RemoveReference(IAssetReference* ref, bool week) { if (ref) { - Locker.Lock(); + ScopeLock lock(_referencesLocker); _references.Remove(ref); - Locker.Unlock(); } if (!week) Platform::InterlockedDecrement(&_refCount); @@ -681,6 +679,7 @@ void Asset::onLoaded_MainThread() ASSERT(IsInMainThread()); // Send event + ScopeLock lock(_referencesLocker); for (const auto& e : _references) e.Item->OnAssetLoaded(this, this); OnLoaded(this); @@ -696,6 +695,7 @@ void Asset::onUnload_MainThread() CancelStreaming(); // Send event + ScopeLock lock(_referencesLocker); for (const auto& e : _references) e.Item->OnAssetUnloaded(this, this); OnUnloaded(this); diff --git a/Source/Engine/Content/Asset.h b/Source/Engine/Content/Asset.h index bb0fbe490..c838eddf8 100644 --- a/Source/Engine/Content/Asset.h +++ b/Source/Engine/Content/Asset.h @@ -7,6 +7,7 @@ #include "Engine/Core/Types/String.h" #include "Engine/Platform/CriticalSection.h" #include "Engine/Scripting/ScriptingObject.h" +#include "Engine/Threading/ConcurrentSystemLocker.h" #include "Config.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) HashSet _references; + CriticalSection _referencesLocker; // TODO: convert into a single interlocked exchange for the current thread owning lock public: ///