From 1fcde396c37ec6c20a8db03e263a66c7faf1afd5 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 24 Aug 2019 15:11:53 +0200 Subject: [PATCH] Added mutex locks on static variables such as static scope and userdata, that could be shared around threads --- src/StandardLibraries/StaticScope.hpp | 3 +++ src/UserData/UserData.hpp | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/StandardLibraries/StaticScope.hpp b/src/StandardLibraries/StaticScope.hpp index cdf90db..27cddc3 100644 --- a/src/StandardLibraries/StaticScope.hpp +++ b/src/StandardLibraries/StaticScope.hpp @@ -23,6 +23,7 @@ namespace Porygon::StandardLibraries{ public: map _boundVariables; map _variables; + std::mutex _mutex; InternalScope(){ BasicLibrary::RegisterVariables(&_boundVariables, &_variables); @@ -58,11 +59,13 @@ namespace Porygon::StandardLibraries{ static void RegisterVariable(const Utilities::HashedString& identifier, shared_ptr type, Evaluation::EvalValue* value){ + std::lock_guard guard(GetScope()->_mutex); GetScope()->_boundVariables.insert({identifier, new Binder::BoundVariable(std::move(type))}); GetScope()->_variables.insert({identifier, value}); } static void RegisterVariableRaw(uint32_t identifier, ScriptType* type, Evaluation::EvalValue* value){ + std::lock_guard guard(GetScope()->_mutex); auto hash = Utilities::HashedString::CreateLookup(identifier); GetScope()->_boundVariables.insert({hash, new Binder::BoundVariable(shared_ptr(type))}); GetScope()->_variables.insert({hash, value}); diff --git a/src/UserData/UserData.hpp b/src/UserData/UserData.hpp index 9da5e97..de9a803 100644 --- a/src/UserData/UserData.hpp +++ b/src/UserData/UserData.hpp @@ -3,12 +3,14 @@ #include #include +#include #include "UserDataField.hpp" #include "UserDataOperation.hpp" namespace Porygon::UserData { class UserData { std::unordered_map> _fields; + std::mutex _mutex; // Binary operations UserDataBinaryOperation* _addition = nullptr; @@ -62,6 +64,7 @@ namespace Porygon::UserData { } inline void CreateField(uint32_t fieldId, UserDataField *field) { + std::lock_guard guard(_mutex); _fields.insert({fieldId, unique_ptr(field)}); }