Skip to content

Commit

Permalink
refactor: add more static_assert(is_always_lock_free)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftb0y committed Aug 19, 2024
1 parent 42ae999 commit 23ba8f3
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/control/controlvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ class ControlRingValue {
private:
T m_value;
mutable std::atomic<std::size_t> m_readerSlots;
static_assert(std::atomic<std::size_t>::is_always_lock_free,
"atomics used for lock-free data storage are not lock-free");
};

// Ring buffer based implementation for all Types sizeof(T) > sizeof(void*)
Expand Down Expand Up @@ -123,6 +125,8 @@ class ControlValueAtomicBase {
ControlRingValue<T> m_ring[cRingSize];
std::atomic<std::size_t> m_readIndex;
std::atomic<std::size_t> m_writeIndex;
static_assert(std::atomic<std::size_t>::is_always_lock_free,
"atomics used for lock-free data storage are not lock-free");
};

// Specialized template for types that are deemed to be atomic on the target
Expand All @@ -146,6 +150,7 @@ class ControlValueAtomicBase<T, cRingSize, true> {

private:
std::atomic<T> m_value;
static_assert(std::atomic<T>::is_always_lock_free);
};

template<typename T, std::size_t cRingSize = kDefaultRingSize>
Expand All @@ -156,6 +161,12 @@ class ControlValueAtomic : public ControlValueAtomicBase<T,
// so this alias makes it a little more manageable.
using ParentT = ControlValueAtomicBase<T, cRingSize, std::atomic<T>::is_always_lock_free>;

static_assert(!(!std::atomic<T>::is_always_lock_free &&
sizeof(T) <= sizeof(void*)),
"T is not lock free even though it is smaller than void*! Consider "
"using `std::atomic<T>::is_lock_free()` to only fallback to the "
"eventually-consistent ControlValueAtomicBase when necessary");

public:
ControlValueAtomic() = default;
ControlValueAtomic(const T& value)
Expand Down

0 comments on commit 23ba8f3

Please sign in to comment.