Skip to content

Commit d94c966

Browse files
mauropasseMauro Passerino
authored andcommitted
Use rclcpp::guard_condition (ros2#1612)
* Use rclcpp::GuardCondition Signed-off-by: Mauro Passerino <mpasserino@irobot.com> * Pass GuardCondition ptr instead of ref to remove_guard_condition Before the api was taking a reference to a guard condition, then getting the address of it. But if a node had expired, we can't get the orig gc dereferencing a pointer, nor can we get an address of an out-of-scope guard condition. Signed-off-by: Mauro Passerino <mpasserino@irobot.com> * Address PR comments Signed-off-by: Mauro Passerino <mpasserino@irobot.com> Co-authored-by: Mauro Passerino <mpasserino@irobot.com>
1 parent 9383cbc commit d94c966

50 files changed

Lines changed: 309 additions & 350 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

rclcpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set(${PROJECT_NAME}_SRCS
4141
src/rclcpp/clock.cpp
4242
src/rclcpp/context.cpp
4343
src/rclcpp/contexts/default_context.cpp
44+
src/rclcpp/detail/add_guard_condition_to_rcl_wait_set.cpp
4445
src/rclcpp/detail/resolve_parameter_overrides.cpp
4546
src/rclcpp/detail/rmw_implementation_specific_payload.cpp
4647
src/rclcpp/detail/rmw_implementation_specific_publisher_payload.cpp
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2021 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef RCLCPP__DETAIL__ADD_GUARD_CONDITION_TO_RCL_WAIT_SET_HPP_
16+
#define RCLCPP__DETAIL__ADD_GUARD_CONDITION_TO_RCL_WAIT_SET_HPP_
17+
18+
#include "rclcpp/guard_condition.hpp"
19+
20+
namespace rclcpp
21+
{
22+
namespace detail
23+
{
24+
25+
/// Adds the guard condition to a waitset
26+
/**
27+
* \param[in] wait_set reference to a wait set where to add the guard condition
28+
* \param[in] guard_condition reference to the guard_condition to be added
29+
*/
30+
RCLCPP_PUBLIC
31+
void
32+
add_guard_condition_to_rcl_wait_set(
33+
rcl_wait_set_t & wait_set,
34+
const rclcpp::GuardCondition & guard_condition);
35+
36+
} // namespace detail
37+
} // namespace rclcpp
38+
39+
#endif // RCLCPP__DETAIL__ADD_GUARD_CONDITION_TO_RCL_WAIT_SET_HPP_

rclcpp/include/rclcpp/executor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ class Executor
525525
std::atomic_bool spinning;
526526

527527
/// Guard condition for signaling the rmw layer to wake up for special events.
528-
rcl_guard_condition_t interrupt_guard_condition_ = rcl_get_zero_initialized_guard_condition();
528+
rclcpp::GuardCondition interrupt_guard_condition_;
529529

530530
std::shared_ptr<rclcpp::GuardCondition> shutdown_guard_condition_;
531531

@@ -549,7 +549,7 @@ class Executor
549549
spin_once_impl(std::chrono::nanoseconds timeout);
550550

551551
typedef std::map<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr,
552-
const rcl_guard_condition_t *,
552+
const rclcpp::GuardCondition *,
553553
std::owner_less<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr>>
554554
WeakNodesToGuardConditionsMap;
555555

rclcpp/include/rclcpp/executors/static_executor_entities_collector.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class StaticExecutorEntitiesCollector final
102102
* \throws std::runtime_error if it couldn't add guard condition to wait set
103103
*/
104104
RCLCPP_PUBLIC
105-
bool
105+
void
106106
add_to_wait_set(rcl_wait_set_t * wait_set) override;
107107

108108
RCLCPP_PUBLIC
@@ -328,7 +328,7 @@ class StaticExecutorEntitiesCollector final
328328
WeakCallbackGroupsToNodesMap weak_groups_to_nodes_associated_with_executor_;
329329

330330
typedef std::map<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr,
331-
const rcl_guard_condition_t *,
331+
const rclcpp::GuardCondition *,
332332
std::owner_less<rclcpp::node_interfaces::NodeBaseInterface::WeakPtr>>
333333
WeakNodesToGuardConditionsMap;
334334
WeakNodesToGuardConditionsMap weak_nodes_to_guard_conditions_;

rclcpp/include/rclcpp/experimental/subscription_intra_process_base.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "rcl/error_handling.h"
2727

28+
#include "rclcpp/guard_condition.hpp"
2829
#include "rclcpp/qos.hpp"
2930
#include "rclcpp/type_support_decl.hpp"
3031
#include "rclcpp/waitable.hpp"
@@ -41,9 +42,10 @@ class SubscriptionIntraProcessBase : public rclcpp::Waitable
4142

4243
RCLCPP_PUBLIC
4344
SubscriptionIntraProcessBase(
45+
rclcpp::Context::SharedPtr context,
4446
const std::string & topic_name,
4547
const rclcpp::QoS & qos_profile)
46-
: topic_name_(topic_name), qos_profile_(qos_profile)
48+
: gc_(context), topic_name_(topic_name), qos_profile_(qos_profile)
4749
{}
4850

4951
virtual ~SubscriptionIntraProcessBase() = default;
@@ -53,7 +55,7 @@ class SubscriptionIntraProcessBase : public rclcpp::Waitable
5355
get_number_of_ready_guard_conditions() {return 1;}
5456

5557
RCLCPP_PUBLIC
56-
bool
58+
void
5759
add_to_wait_set(rcl_wait_set_t * wait_set);
5860

5961
virtual bool
@@ -79,7 +81,7 @@ class SubscriptionIntraProcessBase : public rclcpp::Waitable
7981

8082
protected:
8183
std::recursive_mutex reentrant_mutex_;
82-
rcl_guard_condition_t gc_;
84+
rclcpp::GuardCondition gc_;
8385

8486
private:
8587
virtual void

rclcpp/include/rclcpp/experimental/subscription_intra_process_buffer.hpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,36 +67,13 @@ class SubscriptionIntraProcessBuffer : public SubscriptionIntraProcessBase
6767
const std::string & topic_name,
6868
const rclcpp::QoS & qos_profile,
6969
rclcpp::IntraProcessBufferType buffer_type)
70-
: SubscriptionIntraProcessBase(topic_name, qos_profile)
70+
: SubscriptionIntraProcessBase(context, topic_name, qos_profile)
7171
{
7272
// Create the intra-process buffer.
7373
buffer_ = rclcpp::experimental::create_intra_process_buffer<MessageT, Alloc, Deleter>(
7474
buffer_type,
7575
qos_profile,
7676
allocator);
77-
78-
// Create the guard condition.
79-
rcl_guard_condition_options_t guard_condition_options =
80-
rcl_guard_condition_get_default_options();
81-
82-
gc_ = rcl_get_zero_initialized_guard_condition();
83-
rcl_ret_t ret = rcl_guard_condition_init(
84-
&gc_, context->get_rcl_context().get(), guard_condition_options);
85-
86-
if (RCL_RET_OK != ret) {
87-
throw std::runtime_error(
88-
"SubscriptionIntraProcessBuffer init error initializing guard condition");
89-
}
90-
}
91-
92-
virtual ~SubscriptionIntraProcessBuffer()
93-
{
94-
if (rcl_guard_condition_fini(&gc_) != RCL_RET_OK) {
95-
RCUTILS_LOG_ERROR_NAMED(
96-
"rclcpp",
97-
"Failed to destroy guard condition: %s",
98-
rcutils_get_error_string().str);
99-
}
10077
}
10178

10279
bool
@@ -130,8 +107,7 @@ class SubscriptionIntraProcessBuffer : public SubscriptionIntraProcessBase
130107
void
131108
trigger_guard_condition()
132109
{
133-
rcl_ret_t ret = rcl_trigger_guard_condition(&gc_);
134-
(void)ret;
110+
gc_.trigger();
135111
}
136112

137113
BufferUniquePtr buffer_;

rclcpp/include/rclcpp/graph_listener.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "rcl/guard_condition.h"
2525
#include "rcl/wait.h"
2626
#include "rclcpp/context.hpp"
27+
#include "rclcpp/guard_condition.hpp"
2728
#include "rclcpp/macros.hpp"
2829
#include "rclcpp/node_interfaces/node_graph_interface.hpp"
2930
#include "rclcpp/visibility_control.hpp"
@@ -187,7 +188,7 @@ class GraphListener : public std::enable_shared_from_this<GraphListener>
187188
mutable std::mutex node_graph_interfaces_mutex_;
188189
std::vector<rclcpp::node_interfaces::NodeGraphInterface *> node_graph_interfaces_;
189190

190-
rcl_guard_condition_t interrupt_guard_condition_ = rcl_get_zero_initialized_guard_condition();
191+
rclcpp::GuardCondition interrupt_guard_condition_;
191192
rcl_wait_set_t wait_set_ = rcl_get_zero_initialized_wait_set();
192193
};
193194

rclcpp/include/rclcpp/guard_condition.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class GuardCondition
4747
RCLCPP_PUBLIC
4848
explicit GuardCondition(
4949
rclcpp::Context::SharedPtr context =
50-
rclcpp::contexts::get_global_default_context());
50+
rclcpp::contexts::get_global_default_context(),
51+
rcl_guard_condition_options_t guard_condition_options =
52+
rcl_guard_condition_get_default_options());
5153

5254
RCLCPP_PUBLIC
5355
virtual
@@ -58,6 +60,11 @@ class GuardCondition
5860
rclcpp::Context::SharedPtr
5961
get_context() const;
6062

63+
/// Return the underlying rcl guard condition structure.
64+
RCLCPP_PUBLIC
65+
rcl_guard_condition_t &
66+
get_rcl_guard_condition();
67+
6168
/// Return the underlying rcl guard condition structure.
6269
RCLCPP_PUBLIC
6370
const rcl_guard_condition_t &

rclcpp/include/rclcpp/memory_strategy.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ class RCLCPP_PUBLIC MemoryStrategy
6464
virtual void clear_handles() = 0;
6565
virtual void remove_null_handles(rcl_wait_set_t * wait_set) = 0;
6666

67-
virtual void add_guard_condition(const rcl_guard_condition_t * guard_condition) = 0;
67+
virtual void
68+
add_guard_condition(const rclcpp::GuardCondition & guard_condition) = 0;
6869

69-
virtual void remove_guard_condition(const rcl_guard_condition_t * guard_condition) = 0;
70+
virtual void
71+
remove_guard_condition(const rclcpp::GuardCondition * guard_condition) = 0;
7072

7173
virtual void
7274
get_next_subscription(

rclcpp/include/rclcpp/node_interfaces/node_base.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,9 @@ class NodeBase : public NodeBaseInterface
114114
get_associated_with_executor_atomic() override;
115115

116116
RCLCPP_PUBLIC
117-
rcl_guard_condition_t *
117+
rclcpp::GuardCondition &
118118
get_notify_guard_condition() override;
119119

120-
RCLCPP_PUBLIC
121-
std::unique_lock<std::recursive_mutex>
122-
acquire_notify_guard_condition_lock() const override;
123-
124120
RCLCPP_PUBLIC
125121
bool
126122
get_use_intra_process_default() const override;
@@ -149,7 +145,7 @@ class NodeBase : public NodeBaseInterface
149145

150146
/// Guard condition for notifying the Executor of changes to this node.
151147
mutable std::recursive_mutex notify_guard_condition_mutex_;
152-
rcl_guard_condition_t notify_guard_condition_ = rcl_get_zero_initialized_guard_condition();
148+
rclcpp::GuardCondition notify_guard_condition_;
153149
bool notify_guard_condition_is_valid_;
154150
};
155151

0 commit comments

Comments
 (0)