Skip to content

Commit

Permalink
Fix configuration sequence
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carroll <[email protected]>
  • Loading branch information
mjcarroll committed Feb 15, 2022
1 parent cb111a2 commit 53063fb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 42 deletions.
68 changes: 35 additions & 33 deletions src/SimulationRunner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

//////////////////////////////////////////////////
Expand All @@ -474,56 +475,55 @@ 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);
}

/////////////////////////////////////////////////
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<Barrier>(threadCount);
this->postUpdateStopBarrier = std::make_unique<Barrier>(threadCount);
igndbg << "Creating PostUpdate worker threads: "
<< threadCount << std::endl;

this->postUpdateThreadsRunning = true;
int id = 0;
this->postUpdateStartBarrier = std::make_unique<Barrier>(threadCount);
this->postUpdateStopBarrier = std::make_unique<Barrier>(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++;
}
}

Expand Down Expand Up @@ -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);
}

//////////////////////////////////////////////////
Expand Down
20 changes: 12 additions & 8 deletions src/SystemManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,19 @@ void SystemManager::ConfigurePendingSystems(EntityComponentManager &_ecm,
EventManager &_eventMgr)
{
std::lock_guard<std::mutex> 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;
}
}
}
Expand Down Expand Up @@ -113,6 +115,7 @@ size_t SystemManager::ActivatePendingSystems()
}

this->pendingSystems.clear();
this->pendingSystemsConfigured.clear();
return count;
}

Expand Down Expand Up @@ -145,6 +148,7 @@ void SystemManager::AddSystemImpl(
// Update callbacks will be handled later, add to queue
std::lock_guard<std::mutex> lock(this->systemsMutex);
this->pendingSystems.push_back(_system);
this->pendingSystemsConfigured.push_back(false);
}

//////////////////////////////////////////////////
Expand Down
3 changes: 3 additions & 0 deletions src/SystemManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ namespace ignition
/// \brief Pending systems to be added to systems.
private: std::vector<SystemInternal> pendingSystems;

/// \brief Mark if a pending system has been configured
private: std::vector<bool> pendingSystemsConfigured;

/// \brief Mutex to protect pendingSystems
private: mutable std::mutex systemsMutex;

Expand Down
4 changes: 3 additions & 1 deletion src/SystemManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class System_WithConfigure:
const Entity &,
const std::shared_ptr<const sdf::Element> &,
EntityComponentManager &,
EventManager &) override {};
EventManager &) override { configured++; };

public: int configured = 0;
};

/////////////////////////////////////////////////
Expand Down

0 comments on commit 53063fb

Please sign in to comment.