From bf07964fea766120f88b8f86a38bcca84422169c Mon Sep 17 00:00:00 2001 From: Markus Pilman Date: Mon, 13 Jul 2020 11:38:00 -0600 Subject: [PATCH] Make ThreadSpinLock large to prevent false sharing This fixes #3161 --- flow/ThreadPrimitives.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/flow/ThreadPrimitives.h b/flow/ThreadPrimitives.h index 6e43b8f1ab..35d3f4c287 100644 --- a/flow/ThreadPrimitives.h +++ b/flow/ThreadPrimitives.h @@ -23,6 +23,7 @@ #pragma once #include +#include #include "flow/Error.h" #include "flow/Trace.h" @@ -44,6 +45,10 @@ #include #endif +// TODO: We should make this dependent on the CPU. Maybe cmake +// can set this variable properly? +constexpr size_t CACHE_LINE_SIZE = 64; + class ThreadSpinLock { public: // #ifdef _WIN32 @@ -83,6 +88,9 @@ private: ThreadSpinLock(const ThreadSpinLock&); void operator=(const ThreadSpinLock&); std::atomic_flag isLocked = ATOMIC_FLAG_INIT; + // We want a spin lock to occupy a cache line in order to + // prevent false sharing. + std::array padding; }; class ThreadSpinLockHolder {