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

feat: add mutex to member variables of TF manager #65

Merged
merged 1 commit into from
Dec 27, 2024
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
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
Loading