Skip to content

Commit 240bad1

Browse files
committed
Reentrant
1 parent 8dc48c1 commit 240bad1

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

simple_node/include/simple_node/node.hpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,12 @@ class Node : public rclcpp::Node {
3434
const std::shared_ptr<const typename ActionT::Feedback>)>
3535
feedback_cb = nullptr) {
3636

37-
rclcpp::CallbackGroup::SharedPtr group = nullptr;
38-
3937
std::shared_ptr<actions::ActionClient<ActionT>> action_client(
4038
new actions::ActionClient<ActionT>(this, action_name, feedback_cb),
41-
this->create_action_deleter<rclcpp_action::Client<ActionT>>(group));
39+
this->create_action_deleter<rclcpp_action::Client<ActionT>>());
4240

43-
this->get_node_waitables_interface()->add_waitable(action_client, group);
41+
this->get_node_waitables_interface()->add_waitable(action_client,
42+
this->group);
4443
return action_client;
4544
}
4645

@@ -52,14 +51,13 @@ class Node : public rclcpp::Node {
5251
void(std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>>)>
5352
execute_callback) {
5453

55-
rclcpp::CallbackGroup::SharedPtr group = nullptr;
56-
5754
std::shared_ptr<actions::ActionSingleServer<ActionT>> action_server(
5855
new actions::ActionSingleServer<ActionT>(this, action_name,
5956
execute_callback),
60-
this->create_action_deleter<rclcpp_action::Server<ActionT>>(group));
57+
this->create_action_deleter<rclcpp_action::Server<ActionT>>());
6158

62-
this->get_node_waitables_interface()->add_waitable(action_server, group);
59+
this->get_node_waitables_interface()->add_waitable(action_server,
60+
this->group);
6361
return action_server;
6462
}
6563

@@ -72,14 +70,13 @@ class Node : public rclcpp::Node {
7270
execute_callback,
7371
std::function<void()> cancel_callback) {
7472

75-
rclcpp::CallbackGroup::SharedPtr group = nullptr;
76-
7773
std::shared_ptr<actions::ActionSingleServer<ActionT>> action_server(
7874
new actions::ActionSingleServer<ActionT>(
7975
this, action_name, execute_callback, cancel_callback),
80-
this->create_action_deleter<rclcpp_action::Server<ActionT>>(group));
76+
this->create_action_deleter<rclcpp_action::Server<ActionT>>());
8177

82-
this->get_node_waitables_interface()->add_waitable(action_server, group);
78+
this->get_node_waitables_interface()->add_waitable(action_server,
79+
this->group);
8380
return action_server;
8481
}
8582

@@ -91,14 +88,13 @@ class Node : public rclcpp::Node {
9188
void(std::shared_ptr<rclcpp_action::ServerGoalHandle<ActionT>>)>
9289
execute_callback) {
9390

94-
rclcpp::CallbackGroup::SharedPtr group = nullptr;
95-
9691
std::shared_ptr<actions::ActionQueueServer<ActionT>> action_server(
9792
new actions::ActionQueueServer<ActionT>(this, action_name,
9893
execute_callback),
99-
this->create_action_deleter<rclcpp_action::Server<ActionT>>(group));
94+
this->create_action_deleter<rclcpp_action::Server<ActionT>>());
10095

101-
this->get_node_waitables_interface()->add_waitable(action_server, group);
96+
this->get_node_waitables_interface()->add_waitable(action_server,
97+
this->group);
10298
return action_server;
10399
}
104100

@@ -111,34 +107,33 @@ class Node : public rclcpp::Node {
111107
execute_callback,
112108
std::function<void()> cancel_callback) {
113109

114-
rclcpp::CallbackGroup::SharedPtr group = nullptr;
115-
116110
std::shared_ptr<actions::ActionQueueServer<ActionT>> action_server(
117111
new actions::ActionQueueServer<ActionT>(
118112
this, action_name, execute_callback, cancel_callback),
119-
this->create_action_deleter<rclcpp_action::Server<ActionT>>(group));
113+
this->create_action_deleter<rclcpp_action::Server<ActionT>>());
120114

121-
this->get_node_waitables_interface()->add_waitable(action_server, group);
115+
this->get_node_waitables_interface()->add_waitable(action_server,
116+
this->group);
122117
return action_server;
123118
}
124119

125120
private:
121+
rclcpp::CallbackGroup::SharedPtr group;
126122
rclcpp::Executor *executor;
127123
std::thread *spin_thread;
128124

129125
void run_executor();
130126

131127
template <typename TypeT>
132-
std::function<void(TypeT *ptr)>
133-
create_action_deleter(rclcpp::CallbackGroup::SharedPtr group) {
128+
std::function<void(TypeT *ptr)> create_action_deleter() {
134129

135130
rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr
136131
node_waitables_interface = this->get_node_waitables_interface();
137132

138133
std::weak_ptr<rclcpp::node_interfaces::NodeWaitablesInterface> weak_node =
139134
node_waitables_interface;
140-
std::weak_ptr<rclcpp::CallbackGroup> weak_group = group;
141-
bool group_is_null = (nullptr == group.get());
135+
std::weak_ptr<rclcpp::CallbackGroup> weak_group = this->group;
136+
bool group_is_null = (nullptr == this->group.get());
142137

143138
auto deleter = [weak_node, weak_group, group_is_null](TypeT *ptr) {
144139
if (nullptr == ptr) {

simple_node/src/simple_node/node.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ Node::Node(std::string name, rclcpp::Executor *executor)
1515
Node::Node(std::string name, std::string _namespace, rclcpp::Executor *executor)
1616
: rclcpp::Node(name, _namespace) {
1717

18+
rclcpp::CallbackGroup::SharedPtr group =
19+
this->create_callback_group(rclcpp::CallbackGroupType::Reentrant);
20+
1821
this->executor = executor;
1922
this->spin_thread = new std::thread(&Node::run_executor, this);
2023
}
@@ -25,13 +28,11 @@ Node::~Node() {
2528
}
2629

2730
void Node::join_spin() {
28-
2931
this->spin_thread->join();
3032
RCLCPP_INFO(this->get_logger(), "Destroying node %s", this->get_name());
3133
}
3234

3335
void Node::run_executor() {
34-
3536
this->executor->add_node(this->get_node_base_interface());
3637
this->executor->spin();
3738
}

0 commit comments

Comments
 (0)