Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a std::promise/std::future to avoid busy waiting the step ack messages in NetworkManagerPrimary #470

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/network/NetworkManagerPrimary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "NetworkManagerPrimary.hh"

#include <algorithm>
#include <future>
#include <set>
#include <string>
#include <utility>
Expand All @@ -41,6 +42,7 @@

using namespace ignition;
using namespace gazebo;
using namespace std::chrono_literals;

//////////////////////////////////////////////////
NetworkManagerPrimary::NetworkManagerPrimary(
Expand Down Expand Up @@ -142,21 +144,17 @@ bool NetworkManagerPrimary::Step(const UpdateInfo &_info)

// Send step to all secondaries
this->secondaryStates.clear();
this->secondaryStatesPromise = std::promise<void>{};
auto future = this->secondaryStatesPromise.get_future();
this->simStepPub.Publish(step);

// Block until all secondaries are done
{
IGN_PROFILE("Waiting for secondaries");

int sleep = 0;
int maxSleep = 10 * 1000 * 1000;
for (; sleep < maxSleep &&
(this->secondaryStates.size() < this->secondaries.size()); ++sleep)
{
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
}
auto result = future.wait_for(10s);

if (sleep == maxSleep)
if (std::future_status::ready != result)
{
ignerr << "Waited 10 s and got only [" << this->secondaryStates.size()
<< " / " << this->secondaries.size()
Expand Down Expand Up @@ -202,6 +200,9 @@ std::map<std::string, SecondaryControl::Ptr>
void NetworkManagerPrimary::OnStepAck(const msgs::SerializedStateMap &_msg)
{
this->secondaryStates.push_back(_msg);
if (this->secondaryStates.size() == this->secondaries.size()) {
this->secondaryStatesPromise.set_value();
}
}

//////////////////////////////////////////////////
Expand Down
4 changes: 4 additions & 0 deletions src/network/NetworkManagerPrimary.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define IGNITION_GAZEBO_NETWORK_NETWORKMANAGERPRIMARY_HH_

#include <atomic>
#include <future>
#include <map>
#include <memory>
#include <string>
Expand Down Expand Up @@ -117,6 +118,9 @@ namespace ignition

/// \brief Keep track of states received from secondaries.
private: std::vector<msgs::SerializedStateMap> secondaryStates;

/// \brief Promise used to notify when all secondaryStates where received.
private: std::promise<void> secondaryStatesPromise;
};
}
} // namespace gazebo
Expand Down