From 5f8233e936a72bf430ab04fed2dfcda97ec40c48 Mon Sep 17 00:00:00 2001 From: m0dB Date: Sun, 17 Nov 2024 21:32:47 +0100 Subject: [PATCH] use std::atomic for ControlValueAtomicBase --- src/control/controlvalue.h | 41 +++++++++++++------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/src/control/controlvalue.h b/src/control/controlvalue.h index 0361cbafb78..149b6aac573 100644 --- a/src/control/controlvalue.h +++ b/src/control/controlvalue.h @@ -120,48 +120,35 @@ class ControlValueAtomicBase { QAtomicInt m_writeIndex; }; -// Specialized template for types that are deemed to be atomic on the target -// architecture. Instead of using a read/write ring to guarantee atomicity, -// direct assignment/read of an aligned member variable is used. +// Specialized template for types that are deemed to be lock free atomic +// on the target architecture. Instead of using a read/write ring to +// guarantee atomicity, direct assignment/read of an std::atomic member +// variable is used. template class ControlValueAtomicBase { public: inline T getValue() const { - return m_value; - } - - inline T getValueOnce() { - return std::move(m_value); + return m_value.load(); } inline void setValue(const T& value) { - m_value = value; + m_value.store(value); } protected: ControlValueAtomicBase() = default; private: -#if defined(__GNUC__) - T m_value __attribute__((aligned(sizeof(void*)))); -#elif defined(_MSC_VER) -#ifdef _WIN64 - T __declspec(align(8)) m_value; -#else - T __declspec(align(4)) m_value; -#endif -#else - T m_value; -#endif + std::atomic m_value; }; -// ControlValueAtomic is a wrapper around ControlValueAtomicBase which uses the -// sizeof(T) to determine which underlying implementation of -// ControlValueAtomicBase to use. For types where sizeof(T) <= sizeof(void*), -// the specialized implementation of ControlValueAtomicBase for types that are -// atomic on the architecture is used. -template -class ControlValueAtomic : public ControlValueAtomicBase { +// ControlValueAtomic is a wrapper around ControlValueAtomicBase which uses +// std::atomic::is_always_lock_free to determine which underlying +// implementation of ControlValueAtomicBase to use. +template +class ControlValueAtomic : public ControlValueAtomicBase::is_always_lock_free> { public: ControlValueAtomic() = default; };