Signet Forge 0.1.0
C++20 Parquet library with AI-native extensions
DEMO
Loading...
Searching...
No Matches
signet::forge::MpmcRing< T > Class Template Reference

Lock-free bounded multi-producer multi-consumer ring buffer. More...

#include <mpmc_ring.hpp>

Public Member Functions

 MpmcRing (size_t capacity)
 Construct a ring buffer with the given capacity.
 
 MpmcRing (const MpmcRing &)=delete
 
MpmcRingoperator= (const MpmcRing &)=delete
 
 MpmcRing (MpmcRing &&)=delete
 
MpmcRingoperator= (MpmcRing &&)=delete
 
bool push (T val) noexcept(std::is_nothrow_move_assignable_v< T >)
 Push an element into the ring (non-blocking).
 
bool pop (T &out) noexcept(std::is_nothrow_move_assignable_v< T >)
 Pop an element from the ring (non-blocking).
 
size_t capacity () const noexcept
 Return the actual ring capacity (always a power of two).
 
size_t size () const noexcept
 Approximate occupancy (not linearizable without external synchronization).
 
bool empty () const noexcept
 Check whether the ring appears empty (approximate).
 

Detailed Description

template<typename T>
class signet::forge::MpmcRing< T >

Lock-free bounded multi-producer multi-consumer ring buffer.

Based on Dmitry Vyukov's bounded MPMC queue algorithm (public domain). Multiple producers and consumers call push()/pop() concurrently without external locking. O(1) amortized push/pop, no allocation after construction, ABA-safe via per-slot sequence numbers.

Each slot carries a sequence number encoding its lifecycle phase:

  • seq == pos : slot is empty; a producer at pos may claim it.
  • seq == pos + 1 : slot is full; a consumer at pos may drain it.
  • seq == pos + cap: slot has been drained and is ready for the next cycle.
Template Parameters
TElement type. Must be move-assignable.
Note
Non-copyable and non-movable (contains atomics).
Runtime capacity is always rounded up to the next power of two (minimum 2).
See also
EventBus

Definition at line 46 of file mpmc_ring.hpp.

Constructor & Destructor Documentation

◆ MpmcRing() [1/3]

template<typename T >
signet::forge::MpmcRing< T >::MpmcRing ( size_t  capacity)
inlineexplicit

Construct a ring buffer with the given capacity.

The actual capacity is rounded up to the next power of two (minimum 2). Power-of-two capacity is required for correct index masking (pos & mask_).

Parameters
capacityRequested capacity; will be rounded up to a power of two. Must be >= 1.
Exceptions
std::invalid_argumentif capacity is 0.
Note
Wraparound safety: the enqueue_/dequeue_ cursors are size_t (64-bit on modern platforms). At 1 GHz sustained push rate, UINT64_MAX wraps after ~584 years — a theoretical-only concern. The masking arithmetic (pos & mask_) remains correct across the wraparound boundary because capacity is always a power of two.

Definition at line 66 of file mpmc_ring.hpp.

◆ MpmcRing() [2/3]

template<typename T >
signet::forge::MpmcRing< T >::MpmcRing ( const MpmcRing< T > &  )
delete

◆ MpmcRing() [3/3]

template<typename T >
signet::forge::MpmcRing< T >::MpmcRing ( MpmcRing< T > &&  )
delete

Member Function Documentation

◆ capacity()

template<typename T >
size_t signet::forge::MpmcRing< T >::capacity ( ) const
inlinenoexcept

Return the actual ring capacity (always a power of two).

Definition at line 165 of file mpmc_ring.hpp.

◆ empty()

template<typename T >
bool signet::forge::MpmcRing< T >::empty ( ) const
inlinenoexcept

Check whether the ring appears empty (approximate).

Definition at line 176 of file mpmc_ring.hpp.

◆ operator=() [1/2]

template<typename T >
MpmcRing & signet::forge::MpmcRing< T >::operator= ( const MpmcRing< T > &  )
delete

◆ operator=() [2/2]

template<typename T >
MpmcRing & signet::forge::MpmcRing< T >::operator= ( MpmcRing< T > &&  )
delete

◆ pop()

template<typename T >
bool signet::forge::MpmcRing< T >::pop ( T &  out)
inlinenoexcept

Pop an element from the ring (non-blocking).

Parameters
outReceives the dequeued element on success (via move assignment).
Returns
true if an element was dequeued; false if the ring is empty.

Definition at line 132 of file mpmc_ring.hpp.

◆ push()

template<typename T >
bool signet::forge::MpmcRing< T >::push ( val)
inlinenoexcept

Push an element into the ring (non-blocking).

Parameters
valThe element to enqueue (moved into the ring).
Returns
true if the element was enqueued; false if the ring is full.

Definition at line 97 of file mpmc_ring.hpp.

◆ size()

template<typename T >
size_t signet::forge::MpmcRing< T >::size ( ) const
inlinenoexcept

Approximate occupancy (not linearizable without external synchronization).

Returns
An estimate of the number of elements currently in the ring.

Definition at line 169 of file mpmc_ring.hpp.


The documentation for this class was generated from the following file: