Added mutex locks on static variables such as static scope and userdata, that could be shared around threads

This commit is contained in:
Deukhoofd 2019-08-24 15:11:53 +02:00
parent 18ffe00bc4
commit 1fcde396c3
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 6 additions and 0 deletions

View File

@ -23,6 +23,7 @@ namespace Porygon::StandardLibraries{
public:
map<Utilities::HashedString, Binder::BoundVariable *> _boundVariables;
map<Utilities::HashedString, Evaluation::EvalValue*> _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<ScriptType> type, Evaluation::EvalValue* value){
std::lock_guard<std::mutex> 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<std::mutex> guard(GetScope()->_mutex);
auto hash = Utilities::HashedString::CreateLookup(identifier);
GetScope()->_boundVariables.insert({hash, new Binder::BoundVariable(shared_ptr<ScriptType>(type))});
GetScope()->_variables.insert({hash, value});

View File

@ -3,12 +3,14 @@
#include <utility>
#include <unordered_map>
#include <mutex>
#include "UserDataField.hpp"
#include "UserDataOperation.hpp"
namespace Porygon::UserData {
class UserData {
std::unordered_map<uint32_t, unique_ptr<UserDataField>> _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<std::mutex> guard(_mutex);
_fields.insert({fieldId, unique_ptr<UserDataField>(field)});
}