Skip to content

Commit

Permalink
Use a stored SerializedStepMap message instead of passing it as a f…
Browse files Browse the repository at this point in the history
…unction argument

Signed-off-by: Addisu Z. Taddese <[email protected]>
  • Loading branch information
azeey committed Feb 22, 2025
1 parent 6b7df49 commit b2ffebc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
48 changes: 25 additions & 23 deletions src/gui/GuiRunner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*
*/

#include <optional>
#include <memory>
#include <mutex>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -45,10 +47,6 @@
using namespace gz;
using namespace sim;

// Register SerializedStepMap to the Qt meta type system so we can pass objects
// of this type in QMetaObject::invokeMethod
Q_DECLARE_METATYPE(msgs::SerializedStepMap)

/////////////////////////////////////////////////
class gz::sim::GuiRunner::Implementation
{
Expand All @@ -64,15 +62,6 @@ class gz::sim::GuiRunner::Implementation
/// \brief Latest update info
public: UpdateInfo updateInfo;

/// \brief Flag used to end the updateThread.
public: bool running{false};

/// \brief Mutex to protect the plugin update.
public: std::mutex updateMutex;

/// \brief The plugin update thread..
public: std::thread updateThread;

/// \brief True if the initial state has been received and processed.
public: bool receivedInitialState{false};

Expand Down Expand Up @@ -103,14 +92,15 @@ class gz::sim::GuiRunner::Implementation

/// \brief Manager of all events.
public: EventManager eventMgr;

public: std::optional<msgs::SerializedStepMap> lastStateMsg;
public: std::mutex stateMsgMutex;
};

/////////////////////////////////////////////////
GuiRunner::GuiRunner(const std::string &_worldName)
: dataPtr(utils::MakeUniqueImpl<Implementation>())
{
qRegisterMetaType<msgs::SerializedStepMap>();

this->setProperty("worldName", QString::fromStdString(_worldName));

// Allow for creation of entities on GUI side.
Expand Down Expand Up @@ -262,8 +252,11 @@ void GuiRunner::OnStateAsyncService(const msgs::SerializedStepMap &_res)
// OnStateQt function to the queue so that its called from the Qt thread. This
// ensures that only one thread has access to the ecm and updateInfo
// variables.
QMetaObject::invokeMethod(this, "OnStateQt", Qt::QueuedConnection,
Q_ARG(msgs::SerializedStepMap, _res));
{
std::lock_guard<std::mutex> lock(this->dataPtr->stateMsgMutex);
this->dataPtr->lastStateMsg = _res;
}
QMetaObject::invokeMethod(this, "OnStateQt", Qt::QueuedConnection);
this->dataPtr->receivedInitialState = true;

// todo(anyone) store reqSrv string in a member variable and use it here
Expand All @@ -284,23 +277,32 @@ void GuiRunner::OnState(const msgs::SerializedStepMap &_msg)
if (!this->dataPtr->receivedInitialState)
return;

{
std::lock_guard<std::mutex> lock(this->dataPtr->stateMsgMutex);
this->dataPtr->lastStateMsg = _msg;
}
// Since this function may be called from a transport thread, we push the
// OnStateQt function to the queue so that its called from the Qt thread. This
// ensures that only one thread has access to the ecm and updateInfo
// variables.
QMetaObject::invokeMethod(this, "OnStateQt", Qt::QueuedConnection,
Q_ARG(msgs::SerializedStepMap, _msg));
QMetaObject::invokeMethod(this, "OnStateQt", Qt::QueuedConnection);
}

/////////////////////////////////////////////////
void GuiRunner::OnStateQt(const msgs::SerializedStepMap &_msg)
void GuiRunner::OnStateQt()
{
GZ_PROFILE_THREAD_NAME("Qt thread");
GZ_PROFILE("GuiRunner::Update");
this->dataPtr->ecm.SetState(_msg.state());
{
std::lock_guard<std::mutex> lock(this->dataPtr->stateMsgMutex);
if (this->dataPtr->lastStateMsg.has_value())
{
this->dataPtr->ecm.SetState(this->dataPtr->lastStateMsg->state());
}

// Update all plugins
this->dataPtr->updateInfo = convert<UpdateInfo>(_msg.stats());
// Update all plugins
this->dataPtr->updateInfo = convert<UpdateInfo>(this->dataPtr->lastStateMsg->stats());
}
this->UpdatePlugins();
}

Expand Down
4 changes: 3 additions & 1 deletion src/gui/GuiRunner.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
#include "gz/sim/EventManager.hh"
#include "gz/sim/gui/Export.hh"


namespace gz
{
namespace sim
{

// Inline bracket to help doxygen filtering.
inline namespace GZ_SIM_VERSION_NAMESPACE {
/// \brief Responsible for running GUI systems as new states are received from
Expand Down Expand Up @@ -67,7 +69,7 @@ class GZ_SIM_GUI_VISIBLE GuiRunner : public QObject

/// \brief Called by the Qt thread to update the ECM with new state
/// \param[in] _msg New state message.
private: Q_INVOKABLE void OnStateQt(const msgs::SerializedStepMap &_msg);
private: Q_INVOKABLE void OnStateQt();

/// \brief Update the plugins.
private: Q_INVOKABLE void UpdatePlugins();
Expand Down

0 comments on commit b2ffebc

Please sign in to comment.