Skip to content

Commit

Permalink
GH-2102 Add a large_atomic wrapper around mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Apr 5, 2024
1 parent a74450c commit cafd0b6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
38 changes: 38 additions & 0 deletions libraries/chain/include/eosio/chain/thread_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,44 @@

namespace eosio { namespace chain {

// should be defined for c++17, but clang++16 still has not implemented it
#ifdef __cpp_lib_hardware_interference_size
using std::hardware_constructive_interference_size;
using std::hardware_destructive_interference_size;
#else
// 64 bytes on x86-64 │ L1_CACHE_BYTES │ L1_CACHE_SHIFT │ __cacheline_aligned │ ...
[[maybe_unused]] constexpr std::size_t hardware_constructive_interference_size = 64;
[[maybe_unused]] constexpr std::size_t hardware_destructive_interference_size = 64;
#endif

// Use instead of std::atomic when std::atomic does not support type
template <typename T>
class large_atomic {
alignas(hardware_destructive_interference_size)
mutable std::mutex mtx;
T value{};
public:
T load() const {
std::lock_guard g(mtx);
return value;
}
void store(const T& v) {
std::lock_guard g(mtx);
value = v;
}

class accessor {
std::lock_guard<std::mutex> g;
T& v;
public:
accessor(std::mutex& m, T& v)
: g(m), v(v) {}
T& value() { return v; }
};

auto make_accessor() { return accessor{mtx, value}; }
};

/**
* 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
10 changes: 0 additions & 10 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@
#include <new>
#include <regex>

// should be defined for c++17, but clang++16 still has not implemented it
#ifdef __cpp_lib_hardware_interference_size
using std::hardware_constructive_interference_size;
using std::hardware_destructive_interference_size;
#else
// 64 bytes on x86-64 │ L1_CACHE_BYTES │ L1_CACHE_SHIFT │ __cacheline_aligned │ ...
[[maybe_unused]] constexpr std::size_t hardware_constructive_interference_size = 64;
[[maybe_unused]] constexpr std::size_t hardware_destructive_interference_size = 64;
#endif

using namespace eosio::chain::plugin_interface;

using namespace std::chrono_literals;
Expand Down

0 comments on commit cafd0b6

Please sign in to comment.