Add thread-safety to various scripting methods

This commit is contained in:
Wojtek Figat
2023-10-01 10:57:51 +02:00
parent 075f40b93a
commit d10d52ec0c
5 changed files with 23 additions and 3 deletions

View File

@@ -647,6 +647,8 @@ ScriptingTypeInitializer::ScriptingTypeInitializer(BinaryModule* module, const S
module->TypeNameToTypeIndex[fullname] = TypeIndex;
}
CriticalSection BinaryModule::Locker;
BinaryModule::BinaryModulesList& BinaryModule::GetModules()
{
static BinaryModulesList modules;

View File

@@ -60,6 +60,9 @@ public:
/// <returns>The found binary module or null if missing.</returns>
static BinaryModule* GetModule(const StringAnsiView& name);
// Global scripting locker for cached data.
static CriticalSection Locker;
protected:
/// <summary>

View File

@@ -35,7 +35,6 @@ private:
mutable int32 _hasCachedClasses : 1;
mutable ClassesDictionary _classes;
CriticalSection _locker;
int32 _reloadCount;
StringAnsi _name;

View File

@@ -25,6 +25,7 @@
#include "Engine/Scripting/ManagedCLR/MException.h"
#include "Engine/Scripting/ManagedCLR/MUtils.h"
#include "Engine/Scripting/Scripting.h"
#include "Engine/Scripting/BinaryModule.h"
#include "Engine/Engine/Globals.h"
#include "Engine/Profiler/ProfilerCPU.h"
#include "Engine/Threading/Threading.h"
@@ -636,7 +637,7 @@ const MAssembly::ClassesDictionary& MAssembly::GetClasses() const
#if TRACY_ENABLE
ZoneText(*_name, _name.Length());
#endif
ScopeLock lock(_locker);
ScopeLock lock(BinaryModule::Locker);
if (_hasCachedClasses)
return _classes;
ASSERT(_classes.IsEmpty());
@@ -939,6 +940,9 @@ MMethod* MClass::GetMethod(const char* name, int32 numParams) const
const Array<MMethod*>& MClass::GetMethods() const
{
if (_hasCachedMethods)
return _methods;
ScopeLock lock(BinaryModule::Locker);
if (_hasCachedMethods)
return _methods;
@@ -972,6 +976,9 @@ MField* MClass::GetField(const char* name) const
const Array<MField*>& MClass::GetFields() const
{
if (_hasCachedFields)
return _fields;
ScopeLock lock(BinaryModule::Locker);
if (_hasCachedFields)
return _fields;
@@ -1016,6 +1023,9 @@ MProperty* MClass::GetProperty(const char* name) const
const Array<MProperty*>& MClass::GetProperties() const
{
if (_hasCachedProperties)
return _properties;
ScopeLock lock(BinaryModule::Locker);
if (_hasCachedProperties)
return _properties;
@@ -1038,6 +1048,9 @@ const Array<MProperty*>& MClass::GetProperties() const
const Array<MClass*>& MClass::GetInterfaces() const
{
if (_hasCachedInterfaces)
return _interfaces;
ScopeLock lock(BinaryModule::Locker);
if (_hasCachedInterfaces)
return _interfaces;
@@ -1073,6 +1086,9 @@ MObject* MClass::GetAttribute(const MClass* monoClass) const
const Array<MObject*>& MClass::GetAttributes() const
{
if (_hasCachedAttributes)
return _attributes;
ScopeLock lock(BinaryModule::Locker);
if (_hasCachedAttributes)
return _attributes;

View File

@@ -1044,7 +1044,7 @@ const MAssembly::ClassesDictionary& MAssembly::GetClasses() const
const StringAnsiView monoImageName(mono_image_get_name(_monoImage));
ZoneText(*monoImageName, monoImageName.Length());
#endif
ScopeLock lock(_locker);
ScopeLock lock(BinaryModule::Locker);
if (_hasCachedClasses)
return _classes;
ASSERT(_classes.IsEmpty());