Class LatestFrameSlot

Lock-free, allocation-free "latest value wins" handoff for one multi-channel frame, for the latest-only monitors (spider, heatmap) that render only the newest sample each flush.

Why: these monitors push a frame per sample but the flush keeps only the last one, so a queue is pure waste. This slot keeps exactly the newest frame in pre-allocated buffers — the producer overwrites, the consumer reads the latest. Steady-state allocates nothing.

SPSC triple buffer: one producer calls Write(ReadOnlySpan<double>); one consumer calls TryReadInto(Span<double>)/HasData/Clear(). Three buffers — the producer always owns one (back), the consumer always owns one (front), and a shared "ready" slot (middle) is swapped via a single atomic. The producer and consumer never touch the same buffer, so reads are tear-free and wait-free; intermediate frames are simply skipped. NOT safe for multiple producers.

Inheritance
LatestFrameSlot
Namespace: MOSAIC.Components.Basics
Assembly: MOSAIC.dll
Syntax
public sealed class LatestFrameSlot

Constructors

LatestFrameSlot(int)

Declaration
public LatestFrameSlot(int channels)
Parameters
Type Name Description
int channels

Fields

_buffers

The three reusable frame buffers.

Declaration
private readonly double[][] _buffers
Field Value
Type Description
double[][]

_channels

Declaration
private readonly int _channels
Field Value
Type Description
int

_flags

Packed indices in one atomic int: bits 0-1 = front (consumer's), bits 2-3 = middle (ready/spare), bits 4-5 = back (producer's), bit 6 (0x40) = dirty (new frame ready).

Declaration
private int _flags
Field Value
Type Description
int

_readIndex

Declaration
private int _readIndex
Field Value
Type Description
int

_writeIndex

Declaration
private int _writeIndex
Field Value
Type Description
int

Properties

Channels

Channels (doubles) per frame.

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

HasData

True if a frame newer than the last read is waiting. Consumer thread only.

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

Methods

Clear()

Discards the pending frame so the next TryReadInto(Span<double>) returns false until a new Write(ReadOnlySpan<double>) arrives. Consumer thread only (e.g. on pause/reset).

Declaration
public void Clear()

TryReadInto(Span<double>)

Copies the latest frame into dest (≥ Channels long) and returns true, or returns false if nothing new since the last read. Consumer thread only.

Declaration
public bool TryReadInto(Span<double> dest)
Parameters
Type Name Description
Span<double> dest
Returns
Type Description
bool

Write(ReadOnlySpan<double>)

Writes one frame, becoming the new "latest". Producer thread only.

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

Search documentation

Start typing to search the documentation.