From 14c6648c71b4b8a12ac0905ff23fcd4de7f0556f Mon Sep 17 00:00:00 2001 From: Oliver Reiche Date: Mon, 13 Jun 2022 13:30:13 +0200 Subject: multithreading: Add AtomicValue to atomically set/get value ... and use it to replace the commonly used pattern in Expression, LinkedMap, and GitTreeEntry. Furthermore, remove assignment operators for Expression and LinkedMap as those are considered to be used in an immutable manner anyway. --- .../build_engine/expression/linked_map.hpp | 49 ++++------------------ 1 file changed, 8 insertions(+), 41 deletions(-) (limited to 'src/buildtool/build_engine/expression/linked_map.hpp') diff --git a/src/buildtool/build_engine/expression/linked_map.hpp b/src/buildtool/build_engine/expression/linked_map.hpp index 5c6da558..6266c2ce 100644 --- a/src/buildtool/build_engine/expression/linked_map.hpp +++ b/src/buildtool/build_engine/expression/linked_map.hpp @@ -10,6 +10,7 @@ #include #include "fmt/core.h" +#include "src/buildtool/multithreading/atomic_value.hpp" #include "src/utils/cpp/atomic.hpp" #include "src/utils/cpp/hash_combine.hpp" @@ -110,32 +111,12 @@ class LinkedMap { } LinkedMap() noexcept = default; - LinkedMap(LinkedMap const& other) noexcept - : next_{other.next_}, - content_{other.content_}, - map_{other.map_}, - items_{other.items_.load()} {} - LinkedMap(LinkedMap&& other) noexcept - : next_{std::move(other.next_)}, - content_{std::move(other.content_)}, - map_{std::move(other.map_)}, - items_{other.items_.load()} {} + LinkedMap(LinkedMap const& other) noexcept = default; + LinkedMap(LinkedMap&& other) noexcept = default; ~LinkedMap() noexcept = default; - auto operator=(LinkedMap const& other) noexcept -> LinkedMap& { - next_ = other.next_; - content_ = other.content_; - map_ = other.map_; - items_ = other.items_.load(); - return *this; - } - auto operator=(LinkedMap&& other) noexcept -> LinkedMap& { - next_ = std::move(other.next_); - content_ = std::move(other.content_); - map_ = std::move(other.map_); - items_ = other.items_.load(); - return *this; - } + auto operator=(LinkedMap const& other) noexcept = delete; + auto operator=(LinkedMap&& other) noexcept = delete; [[nodiscard]] auto contains(K const& key) const noexcept -> bool { return static_cast(Find(key)); @@ -276,23 +257,10 @@ class LinkedMap { // NOTE: Expensive, needs to compute sorted items. [[nodiscard]] auto Items() const& -> items_t const& { - if (items_.load() == nullptr) { - if (not items_loading_.exchange(true)) { - items_ = std::make_shared(ComputeSortedItems()); - items_.notify_all(); - } - else { - items_.wait(nullptr); - } - } - return *items_.load(); + return items_.SetOnceAndGet([this] { return ComputeSortedItems(); }); } - // NOTE: Expensive, needs to compute sorted items. - [[nodiscard]] auto Items() && -> items_t { - return items_.load() == nullptr ? ComputeSortedItems() - : std::move(*items_.load()); - } + [[nodiscard]] auto Items() && = delete; // NOTE: Expensive, needs to compute sorted items. [[nodiscard]] auto Keys() const -> keys_t { @@ -323,8 +291,7 @@ class LinkedMap { Ptr content_{}; // content of this map if set underlying_map_t map_{}; // content of this map if content_ is not set - mutable atomic_shared_ptr items_{}; - mutable std::atomic items_loading_{}; + AtomicValue items_{}; [[nodiscard]] auto ComputeSortedItems() const noexcept -> items_t { auto size = -- cgit v1.2.3