Browse Source

Don't lock inside Engine::fromJson() until all modules are instantiated. Fix recursive lock when calling getModule().

tags/v2.5.0
Andrew Belt 1 year ago
parent
commit
e6744e5c3e
1 changed files with 13 additions and 7 deletions
  1. +13
    -7
      src/engine/Engine.cpp

+ 13
- 7
src/engine/Engine.cpp View File

@@ -1201,10 +1201,8 @@ json_t* Engine::toJson() {


void Engine::fromJson(json_t* rootJ) {
std::lock_guard<SharedMutex> lock(internal->mutex);

clear_NoLock();
// modules
std::vector<Module*> modules;
json_t* modulesJ = json_object_get(rootJ, "modules");
if (!modulesJ)
return;
@@ -1228,15 +1226,12 @@ void Engine::fromJson(json_t* rootJ) {
assert(module);

try {
// This doesn't need a lock because the Module is not added to the Engine yet.
module->fromJson(moduleJ);

// Before 1.0, the module ID was the index in the "modules" array
if (module->id < 0) {
module->id = moduleIndex;
}

addModule_NoLock(module);
}
catch (Exception& e) {
WARN("Cannot load module: %s", e.what());
@@ -1244,6 +1239,17 @@ void Engine::fromJson(json_t* rootJ) {
delete module;
continue;
}

modules.push_back(module);
}

std::lock_guard<SharedMutex> lock(internal->mutex);

clear_NoLock();

// Add modules
for (Module* module : modules) {
addModule_NoLock(module);
}

// cables
@@ -1280,7 +1286,7 @@ void Engine::fromJson(json_t* rootJ) {
// masterModule
json_t* masterModuleIdJ = json_object_get(rootJ, "masterModuleId");
if (masterModuleIdJ) {
Module* masterModule = getModule(json_integer_value(masterModuleIdJ));
Module* masterModule = getModule_NoLock(json_integer_value(masterModuleIdJ));
setMasterModule_NoLock(masterModule);
}
}


Loading…
Cancel
Save