From a9058a1c34e35adf217f881e3135c05913e13b3b Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Mon, 8 Apr 2024 09:04:17 -0500 Subject: [PATCH] GH-2102 Add a copyable atomic type --- .../include/eosio/chain/thread_utils.hpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/libraries/chain/include/eosio/chain/thread_utils.hpp b/libraries/chain/include/eosio/chain/thread_utils.hpp index f687096aaa..b4e4d5a673 100644 --- a/libraries/chain/include/eosio/chain/thread_utils.hpp +++ b/libraries/chain/include/eosio/chain/thread_utils.hpp @@ -50,6 +50,36 @@ namespace eosio { namespace chain { auto make_accessor() { return accessor{mtx, value}; } }; + template + class copyable_atomic { + std::atomic value; + public: + copyable_atomic() = default; + copyable_atomic(T v) noexcept + : value(v) {} + copyable_atomic(const copyable_atomic& rhs) + : value(rhs.value.load(std::memory_order_relaxed)) {} + copyable_atomic(copyable_atomic&& rhs) noexcept + : value(rhs.value.load(std::memory_order_relaxed)) {} + + T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept { return value.load(mo); } + void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept { value.store(v, mo); } + + template + friend DS& operator<<(DS& ds, const copyable_atomic& ca) { + fc::raw::pack(ds, ca.load(std::memory_order_relaxed)); + return ds; + } + + template + friend DS& operator>>(DS& ds, copyable_atomic& ca) { + T v; + fc::raw::unpack(ds, v); + ca.store(v, std::memory_order_relaxed); + return ds; + } + }; + /** * Wrapper class for thread pool of boost asio io_context run. * Also names threads so that tools like htop can see thread name.