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
Namespace: MOSAIC.Components.Basics
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
Type Description
double[]

_capacity

Declaration
private readonly int _capacity
Field Value
Type Description
int

_channels

Declaration
private readonly int _channels
Field Value
Type Description
int

_dropped

Declaration
private long _dropped
Field Value
Type Description
long

_guard

Declaration
private readonly int _guard
Field Value
Type Description
int

_head

Declaration
private long _head
Field Value
Type Description
long

_mask

Declaration
private readonly int _mask
Field Value
Type Description
int

_readHead

Declaration
private long _readHead
Field Value
Type Description
long

_tail

Declaration
private long _tail
Field Value
Type Description
long

Properties

Capacity

Capacity in frames (rounded up to a power of two at construction).

Declaration
public int Capacity { get; }
Property Value
Type Description
int

Channels

Number of channels (doubles) per frame.

Declaration
public int Channels { get; }
Property Value
Type Description
int

DroppedFrames

Total frames dropped as overwritten-oldest because the consumer fell too far behind.

Declaration
public long DroppedFrames { get; }
Property Value
Type Description
long

GuardFrames

Frames held back from the oldest edge of each read to guarantee tear-free reads.

Declaration
public int GuardFrames { get; }
Property Value
Type Description
int

HasData

True if at least one fresh frame is available. Consumer thread only.

Declaration
public bool HasData { get; }
Property Value
Type Description
bool

Methods

BeginRead()

Opens a read batch; returns the number of fresh frames readable at [0, count) via Peek(int, int)/CopyFrame(int, Span<double>). Consumer thread only. Skips overwritten oldest frames so the batch ends at the newest. Call CommitRead() when done.

Declaration
public int BeginRead()
Returns
Type Description
int

Clear()

Discards all buffered frames. Consumer thread only.

Declaration
public void Clear()

CommitRead()

Marks the current batch consumed. Consumer thread only.

Declaration
public void CommitRead()

CopyFrame(int, Span<double>)

Copies the frame at logical frameIndex (0 = oldest) into dest.

Declaration
public void CopyFrame(int frameIndex, Span<double> dest)
Parameters
Type Name Description
int frameIndex
Span<double> dest

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
Type Description
double

Write(ReadOnlySpan<double>)

Copies one frame in. Producer thread only. Never blocks; values stored verbatim.

Declaration
public void Write(ReadOnlySpan<double> frame)
Parameters
Type Name Description
ReadOnlySpan<double> frame

Search documentation

Start typing to search the documentation.