From e6362ff924b756a4e24100dbe5f4cda9c8ca76b6 Mon Sep 17 00:00:00 2001 From: Martijn Reicher Date: Thu, 17 Mar 2022 10:16:12 +0100 Subject: [PATCH] Improve get_type_props function thread safety Signed-off-by: Martijn Reicher --- .../eclipse/cyclonedds/core/cdr/entity_properties.hpp | 1 + src/idlcxx/src/streamers.c | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ddscxx/include/org/eclipse/cyclonedds/core/cdr/entity_properties.hpp b/src/ddscxx/include/org/eclipse/cyclonedds/core/cdr/entity_properties.hpp index 519ca1bc..1fccb313 100644 --- a/src/ddscxx/include/org/eclipse/cyclonedds/core/cdr/entity_properties.hpp +++ b/src/ddscxx/include/org/eclipse/cyclonedds/core/cdr/entity_properties.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include "cdr_enums.hpp" diff --git a/src/idlcxx/src/streamers.c b/src/idlcxx/src/streamers.c index 3badb3f9..714ad2bd 100644 --- a/src/idlcxx/src/streamers.c +++ b/src/idlcxx/src/streamers.c @@ -1022,10 +1022,12 @@ print_constructed_type_open(struct streams *streams, const idl_node_t *node) " {\n" " static std::mutex mtx;\n" " static entity_properties_t props;\n" - " static bool initialized = false;\n" - " std::lock_guard lock(mtx);\n\n" - " if (initialized)\n" + " static std::atomic_bool initialized {false};\n\n" + " if (initialized.load(std::memory_order_relaxed))\n" " return props;\n\n" + " std::lock_guard lock(mtx);\n" + " if (initialized.load(std::memory_order_relaxed))\n" + " return props;\n" " props.clear();\n" " key_endpoint keylist;\n\n"; static const char *sfmt = @@ -1085,7 +1087,7 @@ print_constructed_type_close( static const char *pfmt = "\n props.m_members_by_seq.push_back(final_entry());\n\n" " props.finish(keylist);\n" - " initialized = true;\n" + " initialized.store(true, std::memory_order::memory_order_release);\n" " return props;\n" "}\n\n";