Skip to content

Commit

Permalink
feat: add mutex around step callback (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
domire8 authored Feb 25, 2024
1 parent fb7e68e commit 59b4b6e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 7 deletions.
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 Virtual destructor
*/
virtual ~ComponentInterface() = default;
virtual ~ComponentInterface();

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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from functools import partial
from typing import Callable, Dict, List, Optional, TypeVar, Union, Iterable
from threading import Lock

import clproto
import modulo_core.translators.message_readers as modulo_readers
Expand Down Expand Up @@ -45,6 +46,7 @@ def __init__(self, node_name: str, *args, **kwargs):
if "parameter_overrides" in node_kwargs.keys():
node_kwargs["parameter_overrides"] = modify_parameter_overrides(node_kwargs["parameter_overrides"])
super().__init__(node_name, *args, **node_kwargs)
self.__step_lock = Lock()
self._parameter_dict: Dict[str, Union[str, sr.Parameter]] = {}
self._predicates: Dict[str, Union[bool, Callable[[], bool]]] = {}
self._triggers: Dict[str, bool] = {}
Expand All @@ -69,7 +71,15 @@ def __init__(self, node_name: str, *args, **kwargs):
self._predicate_publisher = self.create_publisher(Predicate, "/predicates", self._qos)
self.add_predicate("in_error_state", False)

self.create_timer(self.get_parameter_value("period"), self._step)
self.create_timer(self.get_parameter_value("period"), self.__step_with_mutex)

def __del__(self):
self.__step_lock.acquire()

def __step_with_mutex(self):
if self.__step_lock.acquire(blocking=False):
self._step()
self.__step_lock.release()

def _step(self) -> None:
"""
Expand Down
12 changes: 11 additions & 1 deletion source/modulo_components/src/ComponentInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ 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] {
if (this->step_mutex_.try_lock()) {
this->step();
this->step_mutex_.unlock();
}
},
nullptr, this->node_base_.get(), this->node_timers_.get());
}

ComponentInterface::~ComponentInterface() {
this->step_mutex_.lock();
}

void ComponentInterface::step() {}
Expand Down

0 comments on commit 59b4b6e

Please sign in to comment.