From 30b6bce74ee98480b72d4a23328c0b30c28b4566 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Thu, 2 Apr 2020 13:58:37 +0000 Subject: Improve thread visibility. --- vespalib/src/vespa/vespalib/util/gencnt.cpp | 2 +- vespalib/src/vespa/vespalib/util/gencnt.h | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/vespalib/src/vespa/vespalib/util/gencnt.cpp b/vespalib/src/vespa/vespalib/util/gencnt.cpp index 5adbf14a757..ad82cf2e67c 100644 --- a/vespalib/src/vespa/vespalib/util/gencnt.cpp +++ b/vespalib/src/vespa/vespalib/util/gencnt.cpp @@ -58,7 +58,7 @@ GenCnt::distance(const GenCnt &other) const GenCnt & GenCnt::operator=(const GenCnt &src) { - _val = src._val; + _val = src.getAsInt(); return *this; } diff --git a/vespalib/src/vespa/vespalib/util/gencnt.h b/vespalib/src/vespa/vespalib/util/gencnt.h index 7bfc5a7e49b..cac868a8adb 100644 --- a/vespalib/src/vespa/vespalib/util/gencnt.h +++ b/vespalib/src/vespa/vespalib/util/gencnt.h @@ -2,6 +2,7 @@ #pragma once #include +#include namespace vespalib { @@ -16,7 +17,7 @@ namespace vespalib { class GenCnt { private: - uint32_t _val; + std::atomic _val; public: /** @@ -31,12 +32,12 @@ public: **/ GenCnt(uint32_t val) : _val(val) {} - GenCnt(const GenCnt &rhs) = default; + GenCnt(const GenCnt &rhs) : _val(rhs.getAsInt()) {} /** * @brief empty destructor **/ - ~GenCnt() {} + ~GenCnt() = default; /** * @brief Increase the generation count held by this object @@ -95,7 +96,7 @@ public: * * @return generation counter **/ - uint32_t getAsInt() const { return _val; } + uint32_t getAsInt() const { return _val.load(std::memory_order_relaxed); } /** * @brief Set the generation counter from an integer -- cgit v1.2.3