Skip to content

Commit ab3d5ab

Browse files
committed
[#25513] Solved more data races
Signed-off-by: danipiza <dpizarrogallego@gmail.com>
1 parent 2ac2039 commit ab3d5ab

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

ddspipe_participants/include/ddspipe_participants/participant/dds/CommonParticipant.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@ class CommonParticipant : public core::IParticipant
319319
//! <Topics <Writer_guid, Partitions set>>
320320
std::map<std::string, std::map<std::string, std::string>> partition_names;
321321

322+
//! Protects concurrent access to \c partition_names, which is written from the DDS discovery
323+
//! thread (add/update/delete/clear_topic_partition) and read from other threads (topic_partitions).
324+
mutable std::mutex partition_names_mutex_;
325+
322326
// Filter partitions set
323327
std::set<std::string> partition_filter_set_;
324328
// Filter content_topicfilter dict

ddspipe_participants/src/cpp/participant/dds/CommonParticipant.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ core::types::TopicQoS CommonParticipant::topic_qos() const noexcept
115115

116116
std::map<std::string, std::map<std::string, std::string>> CommonParticipant::topic_partitions() const noexcept
117117
{
118+
std::lock_guard<std::mutex> lock(partition_names_mutex_);
118119
return partition_names;
119120
}
120121

@@ -578,6 +579,7 @@ bool CommonParticipant::add_topic_partition(
578579
const std::string& writer_guid,
579580
const std::string& partition)
580581
{
582+
std::lock_guard<std::mutex> lock(partition_names_mutex_);
581583
if (partition_names.find(topic_name) != partition_names.end())
582584
{
583585
// the topic exists
@@ -604,6 +606,7 @@ bool CommonParticipant::update_topic_partition(
604606
const std::string& writer_guid,
605607
const std::string& partition)
606608
{
609+
std::lock_guard<std::mutex> lock(partition_names_mutex_);
607610
if (partition_names.find(topic_name) == partition_names.end())
608611
{
609612
// the topic dont exists
@@ -627,6 +630,7 @@ bool CommonParticipant::delete_topic_partition(
627630
const std::string& writer_guid,
628631
const std::string& partition)
629632
{
633+
std::lock_guard<std::mutex> lock(partition_names_mutex_);
630634
if (partition_names.find(topic_name) == partition_names.end())
631635
{
632636
// the topic dont exists
@@ -646,6 +650,7 @@ bool CommonParticipant::delete_topic_partition(
646650

647651
void CommonParticipant::clear_topic_partitions()
648652
{
653+
std::lock_guard<std::mutex> lock(partition_names_mutex_);
649654
partition_names.clear();
650655
}
651656

0 commit comments

Comments
 (0)