Skip to content

Commit

Permalink
feat: add mutex to member variables of TF manager
Browse files Browse the repository at this point in the history
Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
  • Loading branch information
ktro2828 committed Dec 27, 2024
1 parent 40351f7 commit 64550ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <tf2_ros/transform_listener.h>

#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <unordered_map>
Expand All @@ -50,8 +51,9 @@ class TransformationManager
*
* @return Shared pointer of the entity paths map.
*/
const std::shared_ptr<std::unordered_map<std::string, std::string>> entities() const
const std::shared_ptr<std::unordered_map<std::string, std::string>> entities()
{
std::lock_guard lock(entities_mtx_);
return entities_;
}

Expand Down Expand Up @@ -91,6 +93,9 @@ class TransformationManager
std::shared_ptr<std::unordered_map<std::string, std::string>>
entities_; //!< Map stores a entity path of a corresponding frame ID.
std::unordered_map<std::string, double> 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_
8 changes: 8 additions & 0 deletions awviz_common/src/transformation/transformation_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

#include <yaml-cpp/yaml.h>

#include <algorithm>
#include <chrono>
#include <cmath>
#include <mutex>
#include <string>

namespace
{
Expand Down Expand Up @@ -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<std::string>();
const auto parent = itr->second["parent"].as<std::string>();

Expand All @@ -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;
}
Expand All @@ -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;
Expand Down

0 comments on commit 64550ef

Please sign in to comment.