Skip to content

Commit

Permalink
refactor!: unify the initialized_ flag
Browse files Browse the repository at this point in the history
  • Loading branch information
YuanYuYuan committed Dec 4, 2024
1 parent 4ee266c commit 4c63e8d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 9 deletions.
6 changes: 4 additions & 2 deletions rmw_zenoh_cpp/src/detail/rmw_client_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ std::shared_ptr<ClientData> ClientData::make(
return nullptr;
}

client_data->initialized_ = true;
return client_data;
}

Expand All @@ -230,6 +231,7 @@ ClientData::ClientData(
wait_set_data_(nullptr),
sequence_number_(1),
is_shutdown_(false),
initialized_(false),
num_in_flight_(0)
{
// Do nothing.
Expand Down Expand Up @@ -265,7 +267,6 @@ bool ClientData::init(std::shared_ptr<ZenohSession> session)
}

free_ros_keyexpr.cancel();

return true;
}

Expand Down Expand Up @@ -512,7 +513,7 @@ bool ClientData::detach_condition_and_queue_is_empty()
///=============================================================================
void ClientData::_shutdown()
{
if (is_shutdown_) {
if (is_shutdown_ || !initialized_) {
return;
}

Expand All @@ -523,6 +524,7 @@ void ClientData::_shutdown()

sess_.reset();
is_shutdown_ = true;
initialized_ = false;
}

///=============================================================================
Expand Down
2 changes: 2 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_client_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class ClientData final : public std::enable_shared_from_this<ClientData>
size_t sequence_number_;
// Shutdown flag.
bool is_shutdown_;
// Whether the object has ever successfully been initialized.
bool initialized_;
size_t num_in_flight_;
};
using ClientDataPtr = std::shared_ptr<ClientData>;
Expand Down
8 changes: 6 additions & 2 deletions rmw_zenoh_cpp/src/detail/rmw_node_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ std::shared_ptr<NodeData> NodeData::make(
return nullptr;
}

return std::shared_ptr<NodeData>(
auto node_data = std::shared_ptr<NodeData>(
new NodeData{
node,
id,
std::move(entity),
std::move(token)
});
node_data->initialized_ = true;
return node_data;
}

///=============================================================================
Expand All @@ -88,6 +90,7 @@ NodeData::NodeData(
entity_(std::move(entity)),
token_(std::move(token)),
is_shutdown_(false),
initialized_(false),
pubs_({})
{
// Do nothing.
Expand Down Expand Up @@ -392,14 +395,15 @@ rmw_ret_t NodeData::shutdown()
{
std::lock_guard<std::recursive_mutex> lock(mutex_);
rmw_ret_t ret = RMW_RET_OK;
if (is_shutdown_) {
if (is_shutdown_ || !initialized_) {
return ret;
}

// Unregister this node from the ROS graph.
z_liveliness_undeclare_token(z_move(token_));

is_shutdown_ = true;
initialized_ = false;
return ret;
}

Expand Down
2 changes: 2 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_node_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ class NodeData final
z_owned_liveliness_token_t token_;
// Shutdown flag.
bool is_shutdown_;
// Whether the object has ever successfully been initialized.
bool initialized_;
// Map of publishers.
std::unordered_map<const rmw_publisher_t *, PublisherDataPtr> pubs_;
// Map of subscriptions.
Expand Down
10 changes: 7 additions & 3 deletions rmw_zenoh_cpp/src/detail/rmw_publisher_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ std::shared_ptr<PublisherData> PublisherData::make(
undeclare_z_publisher_cache.cancel();
undeclare_z_publisher.cancel();

return std::shared_ptr<PublisherData>(
auto pub_data = std::shared_ptr<PublisherData>(
new PublisherData{
node,
std::move(entity),
Expand All @@ -198,6 +198,8 @@ std::shared_ptr<PublisherData> PublisherData::make(
type_support->data,
std::move(message_type_support)
});
pub_data->initialized_ = true;
return pub_data;
}

///=============================================================================
Expand All @@ -219,7 +221,8 @@ PublisherData::PublisherData(
type_support_impl_(type_support_impl),
type_support_(std::move(type_support)),
sequence_number_(1),
is_shutdown_(false)
is_shutdown_(false),
initialized_(false)
{
events_mgr_ = std::make_shared<EventsManager>();
}
Expand Down Expand Up @@ -426,7 +429,7 @@ PublisherData::~PublisherData()
rmw_ret_t PublisherData::shutdown()
{
std::lock_guard<std::mutex> lock(mutex_);
if (is_shutdown_) {
if (is_shutdown_ || !initialized_) {
return RMW_RET_OK;
}

Expand All @@ -443,6 +446,7 @@ rmw_ret_t PublisherData::shutdown()

sess_.reset();
is_shutdown_ = true;
initialized_ = false;
return RMW_RET_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_publisher_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class PublisherData final
size_t sequence_number_;
// Shutdown flag.
bool is_shutdown_;
// Whether the object has ever successfully been initialized.
bool initialized_;
};
using PublisherDataPtr = std::shared_ptr<PublisherData>;
using PublisherDataConstPtr = std::shared_ptr<const PublisherData>;
Expand Down
7 changes: 5 additions & 2 deletions rmw_zenoh_cpp/src/detail/rmw_service_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ std::shared_ptr<ServiceData> ServiceData::make(
undeclare_z_queryable.cancel();
free_token.cancel();

service_data->initialized_ = true;
return service_data;
}

Expand All @@ -229,7 +230,8 @@ ServiceData::ServiceData(
request_type_support_(std::move(request_type_support)),
response_type_support_(std::move(response_type_support)),
wait_set_data_(nullptr),
is_shutdown_(false)
is_shutdown_(false),
initialized_(false)
{
// Do nothing.
}
Expand Down Expand Up @@ -493,7 +495,7 @@ rmw_ret_t ServiceData::shutdown()
{
rmw_ret_t ret = RMW_RET_OK;
std::lock_guard<std::mutex> lock(mutex_);
if (is_shutdown_) {
if (is_shutdown_ || !initialized_) {
return ret;
}

Expand All @@ -503,6 +505,7 @@ rmw_ret_t ServiceData::shutdown()

sess_.reset();
is_shutdown_ = true;
initialized_ = false;
return RMW_RET_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions rmw_zenoh_cpp/src/detail/rmw_service_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class ServiceData final
DataCallbackManager data_callback_mgr_;
// Shutdown flag.
bool is_shutdown_;
// Whether the object has ever successfully been initialized.
bool initialized_;
};
using ServiceDataPtr = std::shared_ptr<ServiceData>;
using ServiceDataConstPtr = std::shared_ptr<const ServiceData>;
Expand Down

0 comments on commit 4c63e8d

Please sign in to comment.