Skip to content

Commit

Permalink
iox-eclipse-iceoryx#2301 Assert that all atomics in the hot path are …
Browse files Browse the repository at this point in the history
…lock-free
  • Loading branch information
elBoberido committed Jun 7, 2024
1 parent 835bcae commit 528e4b9
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class MpmcLockFreeQueue
UninitializedArray<ElementType, Capacity> m_buffer;

std::atomic<uint64_t> m_size{0U};
static_assert(std::atomic<uint64_t>::is_always_lock_free, "std::atomic<uint64_t> must be lock-free!");

// template is needed to distinguish between lvalue and rvalue T references
// (universal reference type deduction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class MpmcIndexQueue
// note: there is a way with is_always_lock_free in c++17 (which we cannot use here)
using Index = CyclicIndex<Capacity>;
using Cell = std::atomic<Index>;
static_assert(std::atomic<Index>::is_always_lock_free, "std::atomic<MpmcIndexQueue::Index> must be lock-free!");

/// the array entries have to be initialized explicitly in the constructor since
/// the default atomic constructor does not call the default constructor of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ class MpmcLoFFLi
uint32_t abaCounter;
};

static_assert(sizeof(Node) <= NODE_SIZE,
"The size of 'Node' must not exceed 8 bytes in order to be lock-free on 64 bit systems!");

/// @todo iox-#680 introduce typesafe indices with the properties listed below
/// id is required that not two loefflis with the same properties
/// mix up the id
Expand All @@ -72,6 +69,7 @@ class MpmcLoFFLi
uint32_t m_size{0U};
Index_t m_invalidIndex{0U};
std::atomic<Node> m_head{{0U, 1U}};
static_assert(std::atomic<Node>::is_always_lock_free, "std::atomic<MpmcLoFFLi::Node> must be lock-free!");
iox::RelativePointer<Index_t> m_nextFreeIndex;

public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class MpmcResizeableLockFreeQueue : protected MpmcLockFreeQueue<ElementType, Max
private:
using BufferIndex = uint64_t;
std::atomic<uint64_t> m_capacity{MaxCapacity};
static_assert(std::atomic<uint64_t>::is_always_lock_free, "std::atomic<uint64_t> must be lock-free!");
// must be operator= otherwise it is undefined, see https://en.cppreference.com/w/cpp/atomic/ATOMIC_FLAG_INIT
std::atomic_flag m_resizeInProgress = ATOMIC_FLAG_INIT;
iox::vector<BufferIndex, MaxCapacity> m_unusedIndices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class SpscFifo
UninitializedArray<ValueType, Capacity> m_data;
std::atomic<uint64_t> m_writePos{0};
std::atomic<uint64_t> m_readPos{0};
static_assert(std::atomic<uint64_t>::is_always_lock_free, "std::atomic<uint64_t> must be lock-free!");
};

} // namespace concurrent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ class SpscSofi
/// reordered (read or written too late)
std::atomic<uint64_t> m_readPosition{0};
std::atomic<uint64_t> m_writePosition{0};
static_assert(std::atomic<uint64_t>::is_always_lock_free, "std::atomic<uint64_t> must be lock-free!");
};

} // namespace concurrent
Expand Down
1 change: 0 additions & 1 deletion iceoryx_hoofs/concurrent/buffer/source/mpmc_loffli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ void MpmcLoFFLi::init(not_null<Index_t*> freeIndicesMemory, const uint32_t capac
constexpr uint32_t INTERNALLY_RESERVED_INDICES{1U};
IOX_ENFORCE(capacity < (std::numeric_limits<Index_t>::max() - INTERNALLY_RESERVED_INDICES),
"Requested capacity exceeds limits!");
IOX_ENFORCE(m_head.is_lock_free(), "std::atomic<MpmcLoFFLi::Node> must be lock-free!");

m_nextFreeIndex = freeIndicesMemory;
m_size = capacity;
Expand Down

0 comments on commit 528e4b9

Please sign in to comment.