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

Backport sensors system threading optimization changes #2058

Merged
merged 3 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 25 additions & 0 deletions include/gz/sim/EventManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ namespace ignition
}
}

/// \brief Get connection count for a particular event
/// Connection count for the event
public: template <typename E>
unsigned int
ConnectionCount()
{
if (this->events.find(typeid(E)) == this->events.end())
{
return 0u;
}

E *eventPtr = dynamic_cast<E *>(this->events[typeid(E)].get());
// All values in the map should be derived from Event,
// so this shouldn't be an issue, but it doesn't hurt to check.
if (eventPtr != nullptr)
{
return eventPtr->ConnectionCount();
}
else
{
ignerr << "Failed to get connection count for event: "
<< typeid(E).name() << std::endl;
return 0u;
}
}

/// \brief Convenience type for storing typeinfo references.
private: using TypeInfoRef = std::reference_wrapper<const std::type_info>;
Expand Down
10 changes: 10 additions & 0 deletions src/EventManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ TEST(EventManager, EmitConnectTest)
{
EventManager eventManager;

EXPECT_EQ(0u, eventManager.ConnectionCount<events::Pause>());

bool paused1 = false;
auto connection1 = eventManager.Connect<events::Pause>(
[&paused1](bool _paused) {
paused1 = _paused;
});


EXPECT_EQ(1u, eventManager.ConnectionCount<events::Pause>());

// Emitting events causes connection callbacks to be fired.
eventManager.Emit<events::Pause>(true);
EXPECT_EQ(true, paused1);
Expand All @@ -47,6 +52,8 @@ TEST(EventManager, EmitConnectTest)
paused2 = _paused;
});

EXPECT_EQ(2u, eventManager.ConnectionCount<events::Pause>());

// Multiple connections should each be fired.
eventManager.Emit<events::Pause>(true);
EXPECT_EQ(true, paused1);
Expand All @@ -58,9 +65,12 @@ TEST(EventManager, EmitConnectTest)

// Clearing the ConnectionPtr will cause it to no longer fire.
connection1.reset();

eventManager.Emit<events::Pause>(true);
EXPECT_EQ(false, paused1);
EXPECT_EQ(true, paused2);

EXPECT_EQ(1u, eventManager.ConnectionCount<events::Pause>());
}

/// Test that we are able to connect arbitrary events and signal them.
Expand Down
Loading