Skip to content

Commit

Permalink
Optimize render updates and use of thread mutexes in Sensors system (#…
Browse files Browse the repository at this point in the history
…1938)

This PR optimizes the threading and render updates in the sensors system by:

* reducing the scope of the renderMutex lock in Sensors::Update function
* changing a few bool variables to atomic, also to reduce the need to lock mutexes
* performing scene tree updates only when necessary

Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 authored Apr 25, 2023
1 parent df231cb commit 224a6ce
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 104 deletions.
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 gz
}
}

/// \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
{
gzerr << "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
9 changes: 9 additions & 0 deletions src/EventManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ 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 +51,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 +64,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

0 comments on commit 224a6ce

Please sign in to comment.