From 62077765c402197bea2aa73bdb65ba5671c777c8 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Wed, 19 Jun 2024 16:03:45 +0200 Subject: [PATCH] Refs #21082. Move datasharing pool initialization to `RTPSWriter`. Signed-off-by: Miguel Company --- src/cpp/fastdds/publisher/DataWriterImpl.cpp | 10 +--------- src/cpp/rtps/writer/PersistentWriter.cpp | 11 +++++++++++ src/cpp/rtps/writer/RTPSWriter.cpp | 10 ++++++++++ .../rtps/DataSharing/WriterPool.hpp | 5 +++++ 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/cpp/fastdds/publisher/DataWriterImpl.cpp b/src/cpp/fastdds/publisher/DataWriterImpl.cpp index 9c1fdda5196..1ffc2c77242 100644 --- a/src/cpp/fastdds/publisher/DataWriterImpl.cpp +++ b/src/cpp/fastdds/publisher/DataWriterImpl.cpp @@ -369,20 +369,12 @@ ReturnCode_t DataWriterImpl::enable() w_att.endpoint.data_sharing_configuration().kind() != DataSharingKind::OFF) { auto writer_pool = std::dynamic_pointer_cast(pool); - if (!writer_pool || !writer_pool->init_shared_memory( - writer, w_att.endpoint.data_sharing_configuration().shm_directory())) + if (!writer_pool || !writer_pool->is_initialized()) { EPROSIMA_LOG_ERROR(DATA_WRITER, "Could not initialize DataSharing writer pool"); RTPSDomain::removeRTPSWriter(writer); writer = nullptr; } - else - { - for (auto it = history_->changesBegin(); it != history_->changesEnd(); ++it) - { - writer_pool->add_to_shared_history(*it); - } - } } if (writer == nullptr && diff --git a/src/cpp/rtps/writer/PersistentWriter.cpp b/src/cpp/rtps/writer/PersistentWriter.cpp index 497160ea060..f1fb2d91415 100644 --- a/src/cpp/rtps/writer/PersistentWriter.cpp +++ b/src/cpp/rtps/writer/PersistentWriter.cpp @@ -49,6 +49,17 @@ PersistentWriter::PersistentWriter( hist->m_isHistoryFull = hist->m_att.maximumReservedCaches > 0 && static_cast(hist->m_changes.size()) == hist->m_att.maximumReservedCaches; + + // Prepare the changes for datasharing if compatible + if (att.endpoint.data_sharing_configuration().kind() != dds::DataSharingKind::OFF) + { + auto pool = std::dynamic_pointer_cast(hist->get_payload_pool()); + assert(pool != nullptr); + for (auto change : hist->m_changes) + { + pool->add_to_shared_history(change); + } + } } PersistentWriter::~PersistentWriter() diff --git a/src/cpp/rtps/writer/RTPSWriter.cpp b/src/cpp/rtps/writer/RTPSWriter.cpp index b9bf6ca2a1d..c8818a56c5d 100644 --- a/src/cpp/rtps/writer/RTPSWriter.cpp +++ b/src/cpp/rtps/writer/RTPSWriter.cpp @@ -19,6 +19,7 @@ #include +#include #include #include @@ -84,6 +85,15 @@ void RTPSWriter::init( fixed_payload_size_ = mp_history->m_att.payloadMaxSize; } + if (att.endpoint.data_sharing_configuration().kind() != dds::OFF) + { + std::shared_ptr pool = std::dynamic_pointer_cast(mp_history->get_payload_pool()); + if (!pool || !pool->init_shared_memory(this, att.endpoint.data_sharing_configuration().shm_directory())) + { + EPROSIMA_LOG_ERROR(RTPS_WRITER, "Could not initialize DataSharing writer pool"); + } + } + mp_history->mp_writer = this; mp_history->mp_mutex = &mp_mutex; diff --git a/test/mock/rtps/DataSharingPayloadPool/rtps/DataSharing/WriterPool.hpp b/test/mock/rtps/DataSharingPayloadPool/rtps/DataSharing/WriterPool.hpp index 7223e3f8ba4..7b8205a9c00 100644 --- a/test/mock/rtps/DataSharingPayloadPool/rtps/DataSharing/WriterPool.hpp +++ b/test/mock/rtps/DataSharingPayloadPool/rtps/DataSharing/WriterPool.hpp @@ -42,6 +42,11 @@ class WriterPool : public DataSharingPayloadPool return true; } + bool is_initialized() const + { + return writer_guid_ != GUID_t::unknown(); + } + void add_to_shared_history( const CacheChange_t* /*cache_change*/) {