diff --git a/emper/Common.hpp b/emper/Common.hpp index 351a194b89456494b41741a9008fe79c82b50d11..6fd18884eaa8626bafd2fe859f8b02bfdc4ca353 100644 --- a/emper/Common.hpp +++ b/emper/Common.hpp @@ -3,7 +3,8 @@ #pragma once #include <functional> -#include <sstream> // IWYU pragma: keep +#include <sstream> // IWYU pragma: keep +#include <type_traits> // IWYU pragma: keep #include "emper-config.h" // IWYU pragma: keep @@ -30,6 +31,9 @@ using func_t = std::function<void()>; #define unlikely(x) __builtin_expect(!!(x), 0) #define ALIGN_TO_CACHE_LINE alignas(64) +#define CACHE_LINE_EXCLUSIVE(T, symbol) \ + std::aligned_storage<64, 64>::type __symbol_mem; \ + T& symbol = *new (&__symbol_mem) T() [[noreturn]] void die(const char* message, bool usePerror); diff --git a/emper/sleep_strategy/PipeSleepStrategy.hpp b/emper/sleep_strategy/PipeSleepStrategy.hpp index d96e69552bcf0e9ee20532a4aef1e29298012a99..6a67cf984956de3043be0660124513e0809e22bf 100644 --- a/emper/sleep_strategy/PipeSleepStrategy.hpp +++ b/emper/sleep_strategy/PipeSleepStrategy.hpp @@ -9,7 +9,6 @@ #include <cstdint> #include <iostream> #include <stdexcept> -#include <type_traits> #include <vector> #include "CallerEnvironment.hpp" @@ -128,8 +127,7 @@ class PipeSleepStrategy : AbstractWorkerSleepStrategy<PipeSleepStrategy>, Stats stats; // Make sure the shared counter lives in an exlusive cache line - std::aligned_storage<64, 64>::type sleepers_mem; - std::atomic<int64_t>& sleepers = *reinterpret_cast<std::atomic<int64_t>*>(&sleepers_mem); + CACHE_LINE_EXCLUSIVE(std::atomic<int64_t>, sleepers); template <CallerEnvironment callerEnvironment> [[nodiscard]] auto createHint() -> TaggedPtr;