From 53063fb4135228961abaca7276fd9021146f8bee Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Tue, 15 Feb 2022 15:30:34 -0600 Subject: [PATCH] Fix configuration sequence Signed-off-by: Michael Carroll --- src/SimulationRunner.cc | 68 ++++++++++++++++++++------------------- src/SystemManager.cc | 20 +++++++----- src/SystemManager.hh | 3 ++ src/SystemManager_TEST.cc | 4 ++- 4 files changed, 53 insertions(+), 42 deletions(-) diff --git a/src/SimulationRunner.cc b/src/SimulationRunner.cc index 83916320eb8..0589bb8b500 100644 --- a/src/SimulationRunner.cc +++ b/src/SimulationRunner.cc @@ -461,6 +461,7 @@ void SimulationRunner::AddSystem(const SystemPluginPtr &_system, auto sdf = _sdf.has_value() ? _sdf.value() : this->sdfWorld->Element(); this->systemMgr->AddSystem(_system, entity, sdf); + this->systemMgr->ConfigurePendingSystems(this->entityCompMgr, this->eventMgr); } ////////////////////////////////////////////////// @@ -474,6 +475,7 @@ void SimulationRunner::AddSystem( auto sdf = _sdf.has_value() ? _sdf.value() : this->sdfWorld->Element(); this->systemMgr->AddSystem(_system, entity, sdf); + this->systemMgr->ConfigurePendingSystems(this->entityCompMgr, this->eventMgr); } ///////////////////////////////////////////////// @@ -481,49 +483,47 @@ void SimulationRunner::ProcessSystemQueue() { auto pending = this->systemMgr->PendingCount(); - if (pending > 0) - { - // If additional systems are to be added, stop the existing threads. - this->StopWorkerThreads(); + if (0 == pending) + return; - this->systemMgr->ConfigurePendingSystems( - this->entityCompMgr, this->eventMgr); - this->systemMgr->ActivatePendingSystems(); + // If additional systems are to be added, stop the existing threads. + this->StopWorkerThreads(); - auto threadCount = this->systemMgr->SystemsPostUpdate().size() + 1u; + this->systemMgr->ActivatePendingSystems(); - igndbg << "Creating PostUpdate worker threads: " - << threadCount << std::endl; + auto threadCount = this->systemMgr->SystemsPostUpdate().size() + 1u; - this->postUpdateStartBarrier = std::make_unique(threadCount); - this->postUpdateStopBarrier = std::make_unique(threadCount); + igndbg << "Creating PostUpdate worker threads: " + << threadCount << std::endl; - this->postUpdateThreadsRunning = true; - int id = 0; + this->postUpdateStartBarrier = std::make_unique(threadCount); + this->postUpdateStopBarrier = std::make_unique(threadCount); - for (auto &system : this->systemMgr->SystemsPostUpdate()) - { - igndbg << "Creating postupdate worker thread (" << id << ")" << std::endl; + this->postUpdateThreadsRunning = true; + int id = 0; - this->postUpdateThreads.push_back(std::thread([&, id]() + for (auto &system : this->systemMgr->SystemsPostUpdate()) + { + igndbg << "Creating postupdate worker thread (" << id << ")" << std::endl; + + this->postUpdateThreads.push_back(std::thread([&, id]() + { + std::stringstream ss; + ss << "PostUpdateThread: " << id; + IGN_PROFILE_THREAD_NAME(ss.str().c_str()); + while (this->postUpdateThreadsRunning) { - std::stringstream ss; - ss << "PostUpdateThread: " << id; - IGN_PROFILE_THREAD_NAME(ss.str().c_str()); - while (this->postUpdateThreadsRunning) + this->postUpdateStartBarrier->Wait(); + if (this->postUpdateThreadsRunning) { - this->postUpdateStartBarrier->Wait(); - if (this->postUpdateThreadsRunning) - { - system->PostUpdate(this->currentInfo, this->entityCompMgr); - } - this->postUpdateStopBarrier->Wait(); + system->PostUpdate(this->currentInfo, this->entityCompMgr); } - igndbg << "Exiting postupdate worker thread (" - << id << ")" << std::endl; - })); - id++; - } + this->postUpdateStopBarrier->Wait(); + } + igndbg << "Exiting postupdate worker thread (" + << id << ")" << std::endl; + })); + id++; } } @@ -866,6 +866,8 @@ void SimulationRunner::LoadPlugin(const Entity _entity, const sdf::ElementPtr &_sdf) { this->systemMgr->LoadPlugin(_entity, _fname, _name, _sdf); + this->systemMgr->ConfigurePendingSystems( + this->entityCompMgr, this->eventMgr); } ////////////////////////////////////////////////// diff --git a/src/SystemManager.cc b/src/SystemManager.cc index 999870464b5..b3dfa2b392c 100644 --- a/src/SystemManager.cc +++ b/src/SystemManager.cc @@ -73,17 +73,19 @@ void SystemManager::ConfigurePendingSystems(EntityComponentManager &_ecm, EventManager &_eventMgr) { std::lock_guard lock(this->systemsMutex); - for (const auto& system : this->pendingSystems) + for (size_t ii = 0; ii < this->pendingSystems.size(); ++ii) { + if (this->pendingSystemsConfigured[ii]) + continue; + + const auto& system = this->pendingSystems[ii]; + if (system.configure) { - if (system.configureEntity != kNullEntity && - system.configureSdf != nullptr) - { - system.configure->Configure(system.configureEntity, - system.configureSdf, - _ecm, _eventMgr); - } + system.configure->Configure(system.configureEntity, + system.configureSdf, + _ecm, _eventMgr); + this->pendingSystemsConfigured[ii] = true; } } } @@ -113,6 +115,7 @@ size_t SystemManager::ActivatePendingSystems() } this->pendingSystems.clear(); + this->pendingSystemsConfigured.clear(); return count; } @@ -145,6 +148,7 @@ void SystemManager::AddSystemImpl( // Update callbacks will be handled later, add to queue std::lock_guard lock(this->systemsMutex); this->pendingSystems.push_back(_system); + this->pendingSystemsConfigured.push_back(false); } ////////////////////////////////////////////////// diff --git a/src/SystemManager.hh b/src/SystemManager.hh index 16849331c49..ad67eae1427 100644 --- a/src/SystemManager.hh +++ b/src/SystemManager.hh @@ -120,6 +120,9 @@ namespace ignition /// \brief Pending systems to be added to systems. private: std::vector pendingSystems; + /// \brief Mark if a pending system has been configured + private: std::vector pendingSystemsConfigured; + /// \brief Mutex to protect pendingSystems private: mutable std::mutex systemsMutex; diff --git a/src/SystemManager_TEST.cc b/src/SystemManager_TEST.cc index 64447b2e10f..ac0a292aeb8 100644 --- a/src/SystemManager_TEST.cc +++ b/src/SystemManager_TEST.cc @@ -37,7 +37,9 @@ class System_WithConfigure: const Entity &, const std::shared_ptr &, EntityComponentManager &, - EventManager &) override {}; + EventManager &) override { configured++; }; + + public: int configured = 0; }; /////////////////////////////////////////////////