summaryrefslogtreecommitdiff
path: root/src/buildtool/file_system/git_tree.cpp
diff options
context:
space:
mode:
authorOliver Reiche <oliver.reiche@huawei.com>2022-06-13 13:30:13 +0200
committerOliver Reiche <oliver.reiche@huawei.com>2022-06-13 15:39:47 +0200
commit14c6648c71b4b8a12ac0905ff23fcd4de7f0556f (patch)
tree265b9b616486e77ad69820d1fe5fc7feb088bb97 /src/buildtool/file_system/git_tree.cpp
parent1ad1906f2ac3f73ccf2283e4c1cc992557d91161 (diff)
downloadjustbuild-14c6648c71b4b8a12ac0905ff23fcd4de7f0556f.tar.gz
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.
Diffstat (limited to 'src/buildtool/file_system/git_tree.cpp')
-rw-r--r--src/buildtool/file_system/git_tree.cpp26
1 files changed, 7 insertions, 19 deletions
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::optional<GitTree>>(std::nullopt);
-
[[nodiscard]] auto PermToType(std::string const& perm_str) noexcept
-> std::optional<ObjectType> {
constexpr auto kPermBase = 8;
@@ -154,26 +151,17 @@ auto GitTreeEntry::Blob() const noexcept -> std::optional<std::string> {
}
auto GitTreeEntry::Tree() const& noexcept -> std::optional<GitTree> const& {
- auto ptr = tree_cached_.load();
- if (not ptr) {
- if (not tree_loading_.exchange(true)) {
- ptr = kLoadTreeError;
- std::optional<std::string> obj{};
- if (IsTree() and (obj = cas_->ReadObject(raw_id_))) {
+ return tree_cached_.SetOnceAndGet([this]() -> std::optional<GitTree> {
+ std::optional<std::string> obj{};
+ if (IsTree()) {
+ if (auto obj = cas_->ReadObject(raw_id_)) {
if (auto entries = ParseRawTreeObject(cas_, *obj)) {
- ptr = std::make_shared<std::optional<GitTree>>(
- 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<std::size_t> {