1919#include < memory>
2020#include < string>
2121#include < vector>
22-
2322#include " rcl/error_handling.h"
2423#include " rcl/types.h"
2524#include " rclcpp/detail/add_guard_condition_to_rcl_wait_set.hpp"
@@ -38,8 +37,7 @@ namespace graph_listener
3837{
3938
4039GraphListener::GraphListener (const std::shared_ptr<Context> & parent_context)
41- : weak_parent_context_(parent_context),
42- rcl_parent_context_ (parent_context->get_rcl_context ()),
40+ : rcl_parent_context_(parent_context->get_rcl_context ()),
4341 is_started_(false ),
4442 is_shutdown_(false ),
4543 interrupt_guard_condition_(parent_context)
7270GraphListener::start_if_not_started ()
7371{
7472 std::lock_guard<std::mutex> shutdown_lock (shutdown_mutex_);
75- if (is_shutdown_. load ()) {
73+ if (is_shutdown ()) {
7674 throw GraphListenerShutdownError ();
7775 }
78- auto parent_context = weak_parent_context_.lock ();
79- if (!is_started_ && parent_context) {
80- // Register an on_shutdown hook to shtudown the graph listener.
81- // This is important to ensure that the wait set is finalized before
82- // destruction of static objects occurs.
83- std::weak_ptr<GraphListener> weak_this = shared_from_this ();
84- parent_context->on_shutdown (
85- [weak_this]() {
86- auto shared_this = weak_this.lock ();
87- if (shared_this) {
88- // should not throw from on_shutdown if it can be avoided
89- shared_this->shutdown (std::nothrow);
90- }
91- });
76+
77+ if (!is_started ()) {
9278 // Initialize the wait set before starting.
9379 init_wait_set ();
9480 // Start the listener thread.
@@ -122,7 +108,7 @@ GraphListener::run_loop()
122108{
123109 while (true ) {
124110 // If shutdown() was called, exit.
125- if (is_shutdown_. load ()) {
111+ if (is_shutdown ()) {
126112 return ;
127113 }
128114 rcl_ret_t ret;
@@ -190,7 +176,7 @@ GraphListener::run_loop()
190176 if (graph_gc == wait_set_.guard_conditions [graph_gc_indexes[i]]) {
191177 node_ptr->notify_graph_change ();
192178 }
193- if (is_shutdown_ ) {
179+ if (is_shutdown () ) {
194180 // If shutdown, then notify the node of this as well.
195181 node_ptr->notify_shutdown ();
196182 }
@@ -257,7 +243,7 @@ GraphListener::add_node(rclcpp::node_interfaces::NodeGraphInterface * node_graph
257243 throw std::invalid_argument (" node is nullptr" );
258244 }
259245 std::lock_guard<std::mutex> shutdown_lock (shutdown_mutex_);
260- if (is_shutdown_. load ()) {
246+ if (is_shutdown ()) {
261247 throw GraphListenerShutdownError ();
262248 }
263249
@@ -332,11 +318,11 @@ GraphListener::__shutdown()
332318{
333319 std::lock_guard<std::mutex> shutdown_lock (shutdown_mutex_);
334320 if (!is_shutdown_.exchange (true )) {
335- if (is_started_ ) {
321+ if (is_started () ) {
336322 interrupt_ (&interrupt_guard_condition_);
337323 listener_thread_.join ();
338324 }
339- if (is_started_ ) {
325+ if (is_started () ) {
340326 cleanup_wait_set ();
341327 }
342328 }
@@ -365,6 +351,12 @@ GraphListener::shutdown(const std::nothrow_t &) noexcept
365351 }
366352}
367353
354+ bool
355+ GraphListener::is_started ()
356+ {
357+ return is_started_;
358+ }
359+
368360bool
369361GraphListener::is_shutdown ()
370362{
0 commit comments