From e67be6b3dddcabadd0afc02d21a0df8e9b4e5fa9 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Fri, 8 Sep 2023 04:56:19 -0400 Subject: [PATCH] Make Engine::fromJson() lock in entire method, so modules and cables aren't added on different engine frames. Make Cable::fromJson() call non-locking Engine methods. --- src/engine/Cable.cpp | 4 ++-- src/engine/Engine.cpp | 13 +++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/engine/Cable.cpp b/src/engine/Cable.cpp index a86bc7a1..a079b659 100644 --- a/src/engine/Cable.cpp +++ b/src/engine/Cable.cpp @@ -33,7 +33,7 @@ void Cable::fromJson(json_t* rootJ) { if (!inputModuleIdJ) throw Exception("Input module ID not found for cable %lld", (long long) id); int64_t inputModuleId = json_integer_value(inputModuleIdJ); - inputModule = APP->engine->getModule(inputModuleId); + inputModule = APP->engine->getModule_NoLock(inputModuleId); if (!inputModule) throw Exception("Input module %lld not found for cable %lld", (long long) inputModuleId, (long long) id); @@ -48,7 +48,7 @@ void Cable::fromJson(json_t* rootJ) { if (!outputModuleIdJ) throw Exception("Output module ID not found for cable %lld", (long long) id); int64_t outputModuleId = json_integer_value(outputModuleIdJ); - outputModule = APP->engine->getModule(outputModuleId); + outputModule = APP->engine->getModule_NoLock(outputModuleId); if (!outputModule) throw Exception("Output module %lld not found for cable %lld", (long long) outputModuleId, (long long) id); diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 5bb43b78..f75c841b 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -1201,10 +1201,9 @@ json_t* Engine::toJson() { void Engine::fromJson(json_t* rootJ) { - // Don't write-lock the entire method because most of it doesn't need it. + std::lock_guard lock(internal->mutex); - // Write-locks - clear(); + clear_NoLock(); // modules json_t* modulesJ = json_object_get(rootJ, "modules"); if (!modulesJ) @@ -1237,8 +1236,7 @@ void Engine::fromJson(json_t* rootJ) { module->id = moduleIndex; } - // Write-locks - addModule(module); + addModule_NoLock(module); } catch (Exception& e) { WARN("Cannot load module: %s", e.what()); @@ -1269,8 +1267,7 @@ void Engine::fromJson(json_t* rootJ) { cable->id = cableIndex; } - // Write-locks - addCable(cable); + addCable_NoLock(cable); } catch (Exception& e) { WARN("Cannot load cable: %s", e.what()); @@ -1284,7 +1281,7 @@ void Engine::fromJson(json_t* rootJ) { json_t* masterModuleIdJ = json_object_get(rootJ, "masterModuleId"); if (masterModuleIdJ) { Module* masterModule = getModule(json_integer_value(masterModuleIdJ)); - setMasterModule(masterModule); + setMasterModule_NoLock(masterModule); } }