diff --git a/awviz_common/include/awviz_common/transformation/transformation_manager.hpp b/awviz_common/include/awviz_common/transformation/transformation_manager.hpp index 2711c59..3bfab04 100644 --- a/awviz_common/include/awviz_common/transformation/transformation_manager.hpp +++ b/awviz_common/include/awviz_common/transformation/transformation_manager.hpp @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -50,8 +51,9 @@ class TransformationManager * * @return Shared pointer of the entity paths map. */ - const std::shared_ptr> entities() const + const std::shared_ptr> entities() { + std::lock_guard lock(entities_mtx_); return entities_; } @@ -91,6 +93,9 @@ class TransformationManager std::shared_ptr> entities_; //!< Map stores a entity path of a corresponding frame ID. std::unordered_map last_log_stamps_; //!< Map stores last log timestamps. + + std::mutex tf_tree_mtx_; //!< Mutex of tf_tree_; + std::mutex entities_mtx_; //!< Mutex of entities_; }; } // namespace awviz_common #endif // AWVIZ_COMMON__TRANSFORMATION_MANAGER_HPP_ diff --git a/awviz_common/src/transformation/transformation_manager.cpp b/awviz_common/src/transformation/transformation_manager.cpp index 34ee2bd..bcb1558 100644 --- a/awviz_common/src/transformation/transformation_manager.cpp +++ b/awviz_common/src/transformation/transformation_manager.cpp @@ -18,7 +18,11 @@ #include +#include #include +#include +#include +#include namespace { @@ -76,6 +80,8 @@ void TransformationManager::update_tree() try { YAML::Node frames = YAML::Load(yaml); for (YAML::const_iterator itr = frames.begin(); itr != frames.end(); ++itr) { + std::lock_guard lock(entities_mtx_); + const auto id = itr->first.as(); const auto parent = itr->second["parent"].as(); @@ -90,6 +96,7 @@ void TransformationManager::update_tree() void TransformationManager::update_entity(const TfFrame & frame) { + std::scoped_lock lock(tf_tree_mtx_, entities_mtx_); if (!tf_tree_->can_link_to(frame, TF_ROOT)) { return; } @@ -99,6 +106,7 @@ void TransformationManager::update_entity(const TfFrame & frame) void TransformationManager::log_transform(const TfFrame & frame) { + std::lock_guard lock(entities_mtx_); // Root frame is skipped to log transformation if (frame.is_root() || entities_->count(frame.id()) == 0) { return;