XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
namespace XFESystem.NativeRuntime.Application;

/// <summary>
/// Latest complete diagnostic snapshot per channel. Publish never performs I/O
/// or waits for the consumer. Intermediate diagnostic states may be coalesced;
/// input reports, terminal commands and timing samples do not use this queue.
/// </summary>
public sealed class GuiDiagnosticQueue
{
    private readonly string?[] latest;
    private readonly string?[] trace = new string?[128];
    private readonly object traceGate = new();
    private int head, tail, count, traceCharacters;
    private long lostTrace;
    private bool traceTurn = true;
    private int next;
    public GuiDiagnosticQueue(int channels)
    {
        if (channels is < 1 or > 32) throw new ArgumentOutOfRangeException(nameof(channels));
        latest = new string?[channels];
    }
    public void Publish(int channel, string snapshot)
    {
        ArgumentNullException.ThrowIfNull(snapshot);
        if ((uint)channel >= (uint)latest.Length) throw new ArgumentOutOfRangeException(nameof(channel));
        Interlocked.Exchange(ref latest[channel], snapshot);
    }
    // Event records cannot be coalesced. The short queue lock never includes
    // encoding or serial I/O. Overflow invalidates diagnostic evidence explicitly.
    public void PublishTrace(string record)
    {
        ArgumentNullException.ThrowIfNull(record);
        lock (traceGate)
        {
            if (count == trace.Length || record.Length > 1_048_576 - traceCharacters)
            { Interlocked.Increment(ref lostTrace); return; }
            trace[tail] = record; tail = (tail + 1) % trace.Length;
            count++; traceCharacters += record.Length;
        }
    }
    private bool TryReadTrace(out string record)
    {
        lock (traceGate)
        {
            if (count == 0) { record = ""; return false; }
            record = trace[head]!; trace[head] = null; head = (head + 1) % trace.Length;
            count--; traceCharacters -= record.Length; return true;
        }
    }
    // Exactly one consumer owns round-robin selection. A busy channel cannot
    // starve terminal completion or another window's diagnostic snapshot.
    public bool TryRead(out string snapshot)
    {
        long lost = Interlocked.Exchange(ref lostTrace, 0);
        if (lost != 0) { snapshot = "XFE_DIAGNOSTIC_EVIDENCE_LOST|Records|" + lost + "\n"; return true; }
        if (traceTurn && TryReadTrace(out snapshot)) { traceTurn = false; return true; }
        for (int scanned = 0; scanned < latest.Length; scanned++)
        {
            int channel = next;
            next = (next + 1) % latest.Length;
            string? value = Interlocked.Exchange(ref latest[channel], null);
            if (value is null) continue;
            snapshot = value; traceTurn = true; return true;
        }
        traceTurn = false;
        return TryReadTrace(out snapshot);
    }
}