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)
@@ -71,27 +69,12 @@ void GraphListener::init_wait_set()
7169void
7270GraphListener::start_if_not_started ()
7371{
72+ std::lock_guard<std::mutex> shutdown_lock (shutdown_mutex_);
7473 if (is_shutdown ()) {
7574 throw GraphListenerShutdownError ();
7675 }
7776
78- // Register an on_shutdown hook to shutdown the graph listener.
79- // This is important to ensure that the wait set is finalized before
80- // destruction of static objects occurs.
81- auto parent_context = weak_parent_context_.lock ();
82- if (!is_started_ && parent_context) {
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- });
92- }
93- std::lock_guard<std::mutex> shutdown_lock (shutdown_mutex_);
94- if (!is_started_) {
77+ if (!is_started ()) {
9578 // Initialize the wait set before starting.
9679 init_wait_set ();
9780 // Start the listener thread.
@@ -335,11 +318,11 @@ GraphListener::__shutdown()
335318{
336319 std::lock_guard<std::mutex> shutdown_lock (shutdown_mutex_);
337320 if (!is_shutdown_.exchange (true )) {
338- if (is_started_ ) {
321+ if (is_started () ) {
339322 interrupt_ (&interrupt_guard_condition_);
340323 listener_thread_.join ();
341324 }
342- if (is_started_ ) {
325+ if (is_started () ) {
343326 cleanup_wait_set ();
344327 }
345328 }
@@ -368,6 +351,12 @@ GraphListener::shutdown(const std::nothrow_t &) noexcept
368351 }
369352}
370353
354+ bool
355+ GraphListener::is_started ()
356+ {
357+ return is_started_;
358+ }
359+
371360bool
372361GraphListener::is_shutdown ()
373362{
0 commit comments