From 52957748e2446bc974ab68879c2cb639f7b36e21 Mon Sep 17 00:00:00 2001 From: Yadunund Date: Thu, 5 Sep 2024 23:57:11 +0800 Subject: [PATCH] Switch to recursive_mutex in rmw_context_impl_s Signed-off-by: Yadunund --- .../src/detail/rmw_context_impl_s.cpp | 28 +++++++++---------- .../src/detail/rmw_context_impl_s.hpp | 2 +- rmw_zenoh_cpp/src/rmw_zenoh.cpp | 2 ++ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp index fc178bad..8575c672 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp @@ -51,7 +51,7 @@ void rmw_context_impl_s::graph_sub_data_handler(const z_sample_t * sample, void } // Update the graph cache. - std::lock_guard lock(data_ptr->mutex_); + std::lock_guard lock(data_ptr->mutex_); if (data_ptr->is_shutdown_) { return; } @@ -147,7 +147,7 @@ rmw_ret_t rmw_context_impl_s::Data::subscribe() ///============================================================================= rmw_ret_t rmw_context_impl_s::Data::shutdown() { - std::lock_guard lock(mutex_); + std::lock_guard lock(mutex_); if (is_shutdown_) { return RMW_RET_OK; } @@ -378,35 +378,35 @@ rmw_context_impl_s::rmw_context_impl_s( ///============================================================================= std::string rmw_context_impl_s::enclave() const { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); return data_->enclave_; } ///============================================================================= z_session_t rmw_context_impl_s::session() const { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); return z_loan(data_->session_); } ///============================================================================= std::optional & rmw_context_impl_s::shm_manager() { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); return data_->shm_manager_; } ///============================================================================= rmw_guard_condition_t * rmw_context_impl_s::graph_guard_condition() { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); return data_->graph_guard_condition_; } ///============================================================================= std::size_t rmw_context_impl_s::get_next_entity_id() { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); return data_->next_entity_id_++; } @@ -419,21 +419,21 @@ rmw_ret_t rmw_context_impl_s::shutdown() ///============================================================================= bool rmw_context_impl_s::is_shutdown() const { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); return data_->is_shutdown_; } ///============================================================================= bool rmw_context_impl_s::session_is_valid() const { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); return z_check(data_->session_); } ///============================================================================= std::shared_ptr rmw_context_impl_s::graph_cache() { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); return data_->graph_cache_; } @@ -443,7 +443,7 @@ bool rmw_context_impl_s::create_node_data( const std::string & ns, const std::string & node_name) { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); auto node_insertion = data_->nodes_.insert(std::make_pair(node, nullptr)); if (!node_insertion.second) { // Node already exists. @@ -456,7 +456,7 @@ bool rmw_context_impl_s::create_node_data( // Create the entity. z_session_t session = z_loan(data_->session_); - const size_t node_id = this->data_->next_entity_id_++; + const size_t node_id = this->get_next_entity_id(); auto entity = rmw_zenoh_cpp::liveliness::Entity::make( z_info_zid(session), std::to_string(node_id), @@ -507,7 +507,7 @@ bool rmw_context_impl_s::create_node_data( std::shared_ptr rmw_context_impl_s::get_node_data( const rmw_node_t * const node) { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); auto node_it = data_->nodes_.find(node); if (node_it == data_->nodes_.end()) { return nullptr; @@ -518,6 +518,6 @@ std::shared_ptr rmw_context_impl_s::get_node_data( ///============================================================================= void rmw_context_impl_s::delete_node_data(const rmw_node_t * const node) { - std::lock_guard lock(data_->mutex_); + std::lock_guard lock(data_->mutex_); data_->nodes_.erase(node); } diff --git a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp index 28545b79..5810a51a 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp +++ b/rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp @@ -119,7 +119,7 @@ class rmw_context_impl_s ~Data(); // Mutex to lock when accessing members. - mutable std::mutex mutex_; + mutable std::recursive_mutex mutex_; // RMW allocator. const rcutils_allocator_t * allocator_; // Enclave, name used to find security artifacts in a sros2 keystore. diff --git a/rmw_zenoh_cpp/src/rmw_zenoh.cpp b/rmw_zenoh_cpp/src/rmw_zenoh.cpp index 467f1f1d..80d776c9 100644 --- a/rmw_zenoh_cpp/src/rmw_zenoh.cpp +++ b/rmw_zenoh_cpp/src/rmw_zenoh.cpp @@ -238,6 +238,8 @@ rmw_create_node( return nullptr; } + printf("Created node %s\n", name); + node->implementation_identifier = rmw_zenoh_cpp::rmw_zenoh_identifier; node->context = context; // Store type erased rmw_context_impl_s in node->data so that the NodeData