Skip to content

Commit

Permalink
Switch to recursive_mutex in rmw_context_impl_s
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Sep 5, 2024
1 parent 0c091ee commit 5295774
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
28 changes: 14 additions & 14 deletions rmw_zenoh_cpp/src/detail/rmw_context_impl_s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> lock(data_ptr->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_ptr->mutex_);
if (data_ptr->is_shutdown_) {
return;
}
Expand Down Expand Up @@ -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<std::mutex> lock(mutex_);
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (is_shutdown_) {
return RMW_RET_OK;
}
Expand Down Expand Up @@ -378,35 +378,35 @@ rmw_context_impl_s::rmw_context_impl_s(
///=============================================================================
std::string rmw_context_impl_s::enclave() const
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
return data_->enclave_;
}

///=============================================================================
z_session_t rmw_context_impl_s::session() const
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
return z_loan(data_->session_);
}

///=============================================================================
std::optional<zc_owned_shm_manager_t> & rmw_context_impl_s::shm_manager()
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
return data_->shm_manager_;
}

///=============================================================================
rmw_guard_condition_t * rmw_context_impl_s::graph_guard_condition()
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
return data_->graph_guard_condition_;
}

///=============================================================================
std::size_t rmw_context_impl_s::get_next_entity_id()
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
return data_->next_entity_id_++;
}

Expand All @@ -419,21 +419,21 @@ rmw_ret_t rmw_context_impl_s::shutdown()
///=============================================================================
bool rmw_context_impl_s::is_shutdown() const
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
return data_->is_shutdown_;
}

///=============================================================================
bool rmw_context_impl_s::session_is_valid() const
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
return z_check(data_->session_);
}

///=============================================================================
std::shared_ptr<rmw_zenoh_cpp::GraphCache> rmw_context_impl_s::graph_cache()
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
return data_->graph_cache_;
}

Expand All @@ -443,7 +443,7 @@ bool rmw_context_impl_s::create_node_data(
const std::string & ns,
const std::string & node_name)
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
auto node_insertion = data_->nodes_.insert(std::make_pair(node, nullptr));
if (!node_insertion.second) {
// Node already exists.
Expand All @@ -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),
Expand Down Expand Up @@ -507,7 +507,7 @@ bool rmw_context_impl_s::create_node_data(
std::shared_ptr<rmw_zenoh_cpp::NodeData> rmw_context_impl_s::get_node_data(
const rmw_node_t * const node)
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
auto node_it = data_->nodes_.find(node);
if (node_it == data_->nodes_.end()) {
return nullptr;
Expand All @@ -518,6 +518,6 @@ std::shared_ptr<rmw_zenoh_cpp::NodeData> rmw_context_impl_s::get_node_data(
///=============================================================================
void rmw_context_impl_s::delete_node_data(const rmw_node_t * const node)
{
std::lock_guard<std::mutex> lock(data_->mutex_);
std::lock_guard<std::recursive_mutex> lock(data_->mutex_);
data_->nodes_.erase(node);
}
2 changes: 1 addition & 1 deletion rmw_zenoh_cpp/src/detail/rmw_context_impl_s.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions rmw_zenoh_cpp/src/rmw_zenoh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5295774

Please sign in to comment.