diff --git a/src/cpp/rtps/builtin/discovery/participant/simple/PDPStatelessWriter.cpp b/src/cpp/rtps/builtin/discovery/participant/simple/PDPStatelessWriter.cpp index 5fb37b2898c..c505632f3be 100644 --- a/src/cpp/rtps/builtin/discovery/participant/simple/PDPStatelessWriter.cpp +++ b/src/cpp/rtps/builtin/discovery/participant/simple/PDPStatelessWriter.cpp @@ -55,6 +55,7 @@ PDPStatelessWriter::PDPStatelessWriter( bool PDPStatelessWriter::matched_reader_add_edp( const ReaderProxyData& data) { + std::lock_guard guard(mp_mutex); bool ret = StatelessWriter::matched_reader_add_edp(data); if (ret) { @@ -69,6 +70,7 @@ bool PDPStatelessWriter::matched_reader_add_edp( bool PDPStatelessWriter::matched_reader_remove( const GUID_t& reader_guid) { + std::lock_guard guard(mp_mutex); bool ret = StatelessWriter::matched_reader_remove(reader_guid); if (ret) { @@ -82,6 +84,7 @@ void PDPStatelessWriter::unsent_change_added_to_history( CacheChange_t* change, const std::chrono::time_point& max_blocking_time) { + std::lock_guard guard(mp_mutex); mark_all_readers_interested(); StatelessWriter::unsent_change_added_to_history(change, max_blocking_time); } @@ -89,7 +92,7 @@ void PDPStatelessWriter::unsent_change_added_to_history( void PDPStatelessWriter::set_initial_peers( const LocatorList& locator_list) { - std::lock_guard guard(mp_mutex); + std::lock_guard guard(interested_readers_mutex_); initial_peers_.push_back(locator_list); mp_RTPSParticipant->createSenderResources(initial_peers_); @@ -106,6 +109,8 @@ bool PDPStatelessWriter::send_to_fixed_locators( const uint32_t& total_bytes, std::chrono::steady_clock::time_point& max_blocking_time_point) const { + std::lock_guard guard(interested_readers_mutex_); + bool ret = true; if (should_reach_all_destinations_) @@ -133,6 +138,7 @@ bool PDPStatelessWriter::is_relevant( const fastdds::rtps::CacheChange_t& /* change */, const fastdds::rtps::GUID_t& reader_guid) const { + std::lock_guard guard(interested_readers_mutex_); return interested_readers_.end() != std::find(interested_readers_.begin(), interested_readers_.end(), reader_guid); } @@ -140,6 +146,7 @@ bool PDPStatelessWriter::is_relevant( void PDPStatelessWriter::mark_all_readers_interested() { std::lock_guard guard(mp_mutex); + std::lock_guard _guard(interested_readers_mutex_); should_reach_all_destinations_ = true; interested_readers_.clear(); fixed_locators_.clear(); @@ -151,6 +158,7 @@ void PDPStatelessWriter::add_interested_reader( const GUID_t& reader_guid) { std::lock_guard guard(mp_mutex); + std::lock_guard _guard(interested_readers_mutex_); if (!should_reach_all_destinations_) { auto it = std::find(interested_readers_.begin(), interested_readers_.end(), reader_guid); @@ -165,7 +173,7 @@ void PDPStatelessWriter::add_interested_reader( void PDPStatelessWriter::remove_interested_reader( const GUID_t& reader_guid) { - std::lock_guard guard(mp_mutex); + std::lock_guard guard(interested_readers_mutex_); interested_readers_.remove(reader_guid); } diff --git a/src/cpp/rtps/builtin/discovery/participant/simple/PDPStatelessWriter.hpp b/src/cpp/rtps/builtin/discovery/participant/simple/PDPStatelessWriter.hpp index 7d21b6a41bc..7804661c58d 100644 --- a/src/cpp/rtps/builtin/discovery/participant/simple/PDPStatelessWriter.hpp +++ b/src/cpp/rtps/builtin/discovery/participant/simple/PDPStatelessWriter.hpp @@ -20,6 +20,7 @@ #define FASTDDS_RTPS_BUILTIN_DISCOVERY_PARTICIPANT_SIMPLE__PDPSTATELESSWRITER_HPP #include +#include #include #include @@ -144,6 +145,8 @@ class PDPStatelessWriter : public StatelessWriter, private IReaderDataFilter mutable ResourceLimitedVector interested_readers_; //! Whether we have set that all destinations are interested mutable bool should_reach_all_destinations_ = false; + //! Mutex to protect attributes of this class + mutable std::mutex interested_readers_mutex_; }; diff --git a/test/blackbox/common/RTPSWithRegistrationWriter.hpp b/test/blackbox/common/RTPSWithRegistrationWriter.hpp index 58e9464f73c..b6a461df737 100644 --- a/test/blackbox/common/RTPSWithRegistrationWriter.hpp +++ b/test/blackbox/common/RTPSWithRegistrationWriter.hpp @@ -673,7 +673,7 @@ class RTPSWithRegistrationWriter bool initialized_; std::mutex mutex_; std::condition_variable cv_; - uint32_t matched_; + std::atomic matched_; eprosima::fastdds::rtps::EntityId_t custom_entity_id_ = eprosima::fastdds::rtps::c_EntityId_Unknown; type_support type_; std::shared_ptr payload_pool_;