Class SampleRingBuffer
Lock-free, allocation-free ring buffer of fixed-width sample frames for the real-time
visualization ingest path (one frame = Channels doubles).
SPSC: one producer calls Write(ReadOnlySpan<double>); one consumer calls
HasData/BeginRead()/Peek(int, int)/CopyFrame(int, Span<double>)/
CommitRead()/Clear(). Lock-free between the two roles. NOT safe for
multiple producers.
Overflow = overwrite-oldest: the producer writes circularly and never blocks, so
the ring always holds the most recent Capacity frames. If the consumer falls
behind, BeginRead() skips the overwritten-oldest frames (counted in
DroppedFrames) and returns a batch ENDING at the newest frame — the live signal
is never the data dropped. A guard band keeps reads tear-free.
Inheritance
SampleRingBuffer
Assembly: MOSAIC.dll
Syntax
public sealed class SampleRingBuffer
Constructors
SampleRingBuffer(int, int)
Creates a ring holding up to capacityFrames frames of
channels doubles. Capacity is rounded up to a power of two. Size it well
above the frames produced between two consumer flushes AND above the largest single batch.
Declaration
public SampleRingBuffer(int capacityFrames, int channels)
Parameters
| Type |
Name |
Description |
| int |
capacityFrames |
|
| int |
channels |
|
Fields
_buffer
Declaration
private readonly double[] _buffer
Field Value
_capacity
Declaration
private readonly int _capacity
Field Value
_channels
Declaration
private readonly int _channels
Field Value
_dropped
Declaration
Field Value
_guard
Declaration
private readonly int _guard
Field Value
_head
Declaration
Field Value
_mask
Declaration
private readonly int _mask
Field Value
_readHead
Declaration
Field Value
_tail
Declaration
Field Value
Properties
Capacity
Capacity in frames (rounded up to a power of two at construction).
Declaration
public int Capacity { get; }
Property Value
Channels
Number of channels (doubles) per frame.
Declaration
public int Channels { get; }
Property Value
DroppedFrames
Total frames dropped as overwritten-oldest because the consumer fell too far behind.
Declaration
public long DroppedFrames { get; }
Property Value
GuardFrames
Frames held back from the oldest edge of each read to guarantee tear-free reads.
Declaration
public int GuardFrames { get; }
Property Value
HasData
True if at least one fresh frame is available. Consumer thread only.
Declaration
public bool HasData { get; }
Property Value
Methods
BeginRead()
Declaration
Returns
Clear()
Discards all buffered frames. Consumer thread only.
Declaration
CommitRead()
Marks the current batch consumed. Consumer thread only.
Declaration
CopyFrame(int, Span<double>)
Copies the frame at logical frameIndex (0 = oldest) into dest.
Declaration
public void CopyFrame(int frameIndex, Span<double> dest)
Parameters
Peek(int, int)
Reads one channel value of the frame at logical frameIndex (0 = oldest).
Declaration
public double Peek(int frameIndex, int channel)
Parameters
| Type |
Name |
Description |
| int |
frameIndex |
|
| int |
channel |
|
Returns
Write(ReadOnlySpan<double>)
Copies one frame in. Producer thread only. Never blocks; values stored verbatim.
Declaration
public void Write(ReadOnlySpan<double> frame)
Parameters