Producers write; one merger orders; consumers read. Nothing on the producer path takes a lock or allocates.
source thread ──► SourceRing ─┐
source thread ──► SourceRing ─┼──► merger thread ──► TimelineIndex ──► Subscription (live)
source thread ──► SourceRing ─┘ └──► CaptureWindow (paused)
registerSource(SourceConfig) takes the lowest free id (0 to MAX_SOURCES - 1), so ids are reused after
deregisterSource(). A source's ring holds slot_count slots (rounded up to a power of two) of slot_bytes each.
Registration and deregistration happen only while the buffer is Idle or Paused; deregistration only while Paused.
Each slot has a 64-byte header holding a generation counter, the timestamp and the payload size. The producer
marks the generation odd, publishes the new write sequence, writes the payload, then makes the generation even
again on publish(). A reader samples the generation, copies, and samples it again; an odd value, or a change, means
the slot was overwritten while being read, and ReadHandle::validate() says so. The copy itself is therefore a
deliberate data race, which is why copyBytesRacy is named and why the TSan suppressions name exactly these
functions.
⚠ The write path needs a release fence after the generation goes odd, not only the release store of the write sequence: that store orders what comes before it and nothing after it. On x86 the fence costs nothing; on arm64 it is the difference between the seqlock working and a torn read that validates clean.
The merger drains each ring's next event into a per-source head, and emits the earliest head onto the timeline once
every source has written past it by reorder_window_us. A source that delivers slightly late therefore still lands in
timestamp order. A timestamp that goes backwards within one source is clamped to one microsecond after the previous
one and counted as a monotonicity violation. The merger wakes on a producer's publish() (WaitFlag), spins briefly
while there is work, and otherwise waits with a short timeout.
The timeline index must hold at least as many entries as all the registered rings together, or a window's start can
read as missing for every source while each ring still has it; registerSource() warns when it does not.
pause() stops producers (their acquireWriteSlot returns valid = false), lets the merger drain everything onto the
timeline, and parks it. The rings are then frozen.
captureWindow(t0, t1) snapshots the timeline's entries in that range and builds a CaptureWindow over the ring
backing. That backing pins every ring registered at that moment (a shared reference) and reads only those, so a
source deregistered or re-registered while the window is read cannot free or swap the memory being read; a
deregistered ring is freed when the last window holding it goes. While any ring-backed window is alive,
windowLive() is true and resume() refuses, because resuming clears the rings (resume_clear_rings).
makeRingSource() builds the backing on its own, for a host that must wait for something else before it can build
the window: the guard goes up when the backing is built, not when the window is, so a resume in between cannot clear
the rings about to be read.
A window sorts each source's entries by timestamp (stably). bracket(id, t) returns the first entry strictly after
t, and, as before, the first of the entries sharing the largest timestamp at or before t: not simply the one
before after, which would be the last of them. Equal timestamps cannot occur within one source on the ring path (the
merger enforces monotonicity) but can in a host's own backing.
payloadOf serialises fetches per source, so a streaming backing with one decoder per source is safe to read from
several threads; different sources still fetch concurrently. The bytes returned stay valid only until the next fetch
on that source.
WaitFlag is the one wait primitive. A timed wait is a short spin then a sched_yield loop on Apple platforms
(a timed condition-variable wait there is subject to timer coalescing: 500 µs routinely becomes 5 to 10 ms),
WaitOnAddress on Windows, and a futex-backed std::atomic::wait on Linux. increment() is a single atomic add,
so concurrent producers never lose a wake.
ThreadPolicy raises the merger's priority (a QoS class on Apple platforms, SCHED_FIFO where allowed on Linux,
thread priority on Windows) and can pin it to a core. On Windows the merger raises the timer resolution to 1 ms only
while capturing.
DeviceClockMapper carries a device's own capture instant onto the host clock. It fits the lower envelope of
(device time, arrival time), because arrival delay is one-sided; it fits the rate as well as the offset, restarts the
fit every window of device time while pooling the rate, and can take a latch (a device timestamp bracketed by two host
reads) to remove the constant transfer latency the envelope cannot see. Mapped stamps are strictly increasing and
never later than the arrival.