mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-03 23:59:25 +08:00
With shared memory, the worker's PCM goes to the owner through a single-producer, single-consumer SharedArrayBuffer ring (sound-ring.ts) instead of a message per chunk: the owner drains it into the sink's onChunk on coalesced "sound-wake" messages, or hands it to the new SoundSink.onRing for a reader of the sink's own (an AudioWorklet). State changes are records in the ring as well as messages, numbered alike, so a sink sees states and PCM in the kernel's order however late the messages are. The delay the kernel reports is the sink's answer plus the frames still in the ring, reset with each stream. A full ring drops the newest PCM, which the owner logs. shared: false keeps the "sound-chunk" and "sound-queued" messages. yieldToEventLoop() without setImmediate (browsers) reuses one MessageChannel, with one message, so one task, per waiting yield. Idle waits of 1 to 4 ms also wait on a timed Atomics.waitAsync where a SharedArrayBuffer exists: browsers raise a nested setTimeout below 4 ms to 4 ms (about 5 ms between wakes in Chrome and Safari workers, against 2.5 ms, measured 2026-11-24). Co-authored-by: Adam Meghji <566793+adammeghji@users.noreply.github.com>