Skip to content

Commit

Permalink
GH-2102 Add a copyable atomic type
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Apr 8, 2024
1 parent 8e98c18 commit a9058a1
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions libraries/chain/include/eosio/chain/thread_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,36 @@ namespace eosio { namespace chain {
auto make_accessor() { return accessor{mtx, value}; }
};

template <typename T>
class copyable_atomic {
std::atomic<T> 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<typename DS>
friend DS& operator<<(DS& ds, const copyable_atomic& ca) {
fc::raw::pack(ds, ca.load(std::memory_order_relaxed));
return ds;
}

template<typename DS>
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.
Expand Down

0 comments on commit a9058a1

Please sign in to comment.