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. --- src/buildtool/file_system/git_tree.cpp | 26 +++++++------------------- 1 file changed, 7 insertions(+), 19 deletions(-) (limited to 'src/buildtool/file_system/git_tree.cpp') diff --git a/src/buildtool/file_system/git_tree.cpp b/src/buildtool/file_system/git_tree.cpp index 98281707..f4912a7b 100644 --- a/src/buildtool/file_system/git_tree.cpp +++ b/src/buildtool/file_system/git_tree.cpp @@ -13,9 +13,6 @@ namespace { constexpr auto kOIDRawSize{GIT_OID_RAWSZ}; -auto const kLoadTreeError = - std::make_shared>(std::nullopt); - [[nodiscard]] auto PermToType(std::string const& perm_str) noexcept -> std::optional { constexpr auto kPermBase = 8; @@ -154,26 +151,17 @@ auto GitTreeEntry::Blob() const noexcept -> std::optional { } auto GitTreeEntry::Tree() const& noexcept -> std::optional const& { - auto ptr = tree_cached_.load(); - if (not ptr) { - if (not tree_loading_.exchange(true)) { - ptr = kLoadTreeError; - std::optional obj{}; - if (IsTree() and (obj = cas_->ReadObject(raw_id_))) { + return tree_cached_.SetOnceAndGet([this]() -> std::optional { + std::optional obj{}; + if (IsTree()) { + if (auto obj = cas_->ReadObject(raw_id_)) { if (auto entries = ParseRawTreeObject(cas_, *obj)) { - ptr = std::make_shared>( - GitTree{cas_, std::move(*entries), raw_id_}); + return GitTree{cas_, std::move(*entries), raw_id_}; } } - tree_cached_.store(ptr); - tree_cached_.notify_all(); - } - else { - tree_cached_.wait(nullptr); - ptr = tree_cached_.load(); } - } - return *ptr; + return std::nullopt; + }); } auto GitTreeEntry::Size() const noexcept -> std::optional { -- cgit v1.2.3