Skip to content

Commit

Permalink
lock just once during initialization of LifecycleNodeInterfaceImpl.
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
  • Loading branch information
fujitatomoya committed Dec 20, 2021
1 parent d88adf8 commit e28ab16
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
28 changes: 12 additions & 16 deletions rclcpp_lifecycle/src/lifecycle_node_interface_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,25 @@ class LifecycleNode::LifecycleNodeInterfaceImpl
// The publisher takes a C-Typesupport since the publishing (i.e. creating
// the message) is done fully in RCL.
// Services are handled in C++, so that it needs a C++ typesupport structure.
rcl_ret_t ret;
{
std::lock_guard<std::recursive_mutex> lock(state_machine_mutex_);
state_machine_ = rcl_lifecycle_get_zero_initialized_state_machine();
ret = rcl_lifecycle_state_machine_init(
&state_machine_,
node_handle,
ROSIDL_GET_MSG_TYPE_SUPPORT(lifecycle_msgs, msg, TransitionEvent),
rosidl_typesupport_cpp::get_service_type_support_handle<ChangeStateSrv>(),
rosidl_typesupport_cpp::get_service_type_support_handle<GetStateSrv>(),
rosidl_typesupport_cpp::get_service_type_support_handle<GetAvailableStatesSrv>(),
rosidl_typesupport_cpp::get_service_type_support_handle<GetAvailableTransitionsSrv>(),
rosidl_typesupport_cpp::get_service_type_support_handle<GetAvailableTransitionsSrv>(),
&state_machine_options);
}
std::lock_guard<std::recursive_mutex> lock(state_machine_mutex_);
state_machine_ = rcl_lifecycle_get_zero_initialized_state_machine();
rcl_ret_t ret = rcl_lifecycle_state_machine_init(
&state_machine_,
node_handle,
ROSIDL_GET_MSG_TYPE_SUPPORT(lifecycle_msgs, msg, TransitionEvent),
rosidl_typesupport_cpp::get_service_type_support_handle<ChangeStateSrv>(),
rosidl_typesupport_cpp::get_service_type_support_handle<GetStateSrv>(),
rosidl_typesupport_cpp::get_service_type_support_handle<GetAvailableStatesSrv>(),
rosidl_typesupport_cpp::get_service_type_support_handle<GetAvailableTransitionsSrv>(),
rosidl_typesupport_cpp::get_service_type_support_handle<GetAvailableTransitionsSrv>(),
&state_machine_options);
if (ret != RCL_RET_OK) {
throw std::runtime_error(
std::string("Couldn't initialize state machine for node ") +
node_base_interface_->get_name());
}

if (enable_communication_interface) {
std::lock_guard<std::recursive_mutex> lock(state_machine_mutex_);
{ // change_state
auto cb = std::bind(
&LifecycleNodeInterfaceImpl::on_change_state, this,
Expand Down
1 change: 0 additions & 1 deletion rclcpp_lifecycle/src/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ State::operator=(const State & rhs)

// hold the lock until state_handle_ is reconstructed
std::lock_guard<std::recursive_mutex> lock(state_handle_mutex_);

// reset all currently used resources
reset();

Expand Down

0 comments on commit e28ab16

Please sign in to comment.