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 around step callback #69

Merged
merged 5 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
"ms-vscode.cpptools-extension-pack",
"ms-python.pylint",
"ms-python.autopep8",
"ms-python.isort",
"eamodio.gitlens"
"ms-python.isort"
]
}
}
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Release Versions:

## Upcoming changes (in development)

- chore: fix links in readme (#68)
- chore: fix links in readme (#68)
- feat: add mutex around step callback (#67)

## 4.0.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class ComponentInterface {
friend class ComponentInterfacePublicInterface;

/**
* @brief Virtual default destructor.
* @brief Destructor
domire8 marked this conversation as resolved.
Show resolved Hide resolved
domire8 marked this conversation as resolved.
Show resolved Hide resolved
*/
virtual ~ComponentInterface() = default;
~ComponentInterface();
domire8 marked this conversation as resolved.
Show resolved Hide resolved

protected:
/**
Expand Down Expand Up @@ -536,6 +536,8 @@ class ComponentInterface {
const tf2::Duration& duration
);

std::mutex step_mutex_; ///< Mutex for step callback

std::map<std::string, utilities::PredicateVariant> predicates_; ///< Map of predicates
std::shared_ptr<rclcpp::Publisher<modulo_component_interfaces::msg::Predicate>>
predicate_publisher_; ///< Predicate publisher
Expand Down
11 changes: 10 additions & 1 deletion source/modulo_components/src/ComponentInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ ComponentInterface::ComponentInterface(

this->step_timer_ = rclcpp::create_wall_timer(
std::chrono::nanoseconds(static_cast<int64_t>(this->get_parameter_value<double>("period") * 1e9)),
[this] { this->step(); }, nullptr, this->node_base_.get(), this->node_timers_.get());
[this] {
std::unique_lock<std::mutex> lock(this->step_mutex_, std::try_to_lock);
this->step();
this->step_mutex_.unlock();
},
nullptr, this->node_base_.get(), this->node_timers_.get());
}

ComponentInterface::~ComponentInterface() {
std::lock_guard<std::mutex> lock(this->step_mutex_);
}

void ComponentInterface::step() {}
Expand Down
Loading