using System;
using System.Threading;
namespace ytLive.Services;
///
/// An atomic "publish only if strictly newer" gate for the capture's latest-wins
/// hand-out. Screen capture conversions may now overlap (slice 17), so two
/// conversions can finish briefly out of order; without a monotonic gate the slower
/// (older) completion would overwrite the faster (newer) one's LatestFrame and the
/// compositor would render STALE content — a time hole in the other direction.
/// Sequence must be a strictly increasing per-source counter
/// (Interlocked.Increment in ScreenCaptureFrameSource).
///
internal sealed class MonotonicGate
{
private long _last;
/// Returns true iff is strictly newer than every
/// seq accepted so far (and records it).
public bool TryPublish(long seq)
{
while (true)
{
var current = Interlocked.Read(ref _last);
if (seq <= current) return false;
if (Interlocked.CompareExchange(ref _last, seq, current) == current) return true;
}
}
}