Lock-free sample ring for the audio callback
C++ · C++ · advanced · modification
Replaces the mutex in `SampleRing` with a lock-free single-producer single-consumer design, so the audio callback never waits on the decoder. `sample_ring.hpp` swaps the mutex, head and count for two free-running atomic counters, `write_` advanced by `push` and `read_` advanced by `pop`, kept on separate cache lines; the fill level is their difference and a sample's slot is its counter masked by the capacity, which is now asserted to be a power of two (the atomics are asserted to be lock-free). In `sample_ring.cpp` each side reads the other side's counter to see how much it may copy, copies the samples, then advances its own counter; both functions are now `noexcept`. `tests/sample_ring_test.cpp` keeps the order and full-ring tests and adds empty, partial and wraparound cases.
Desktop audio player in C++20, standard library only, built with CMake (build files unchanged) by Apple clang 17 locally and on the macOS CI runner and GCC 13 on the Linux CI runner, with `-Wall -Wextra -Wconversion` treated as errors; 64-bit targets only, so `std::size_t` is 64 bits. Samples are mono `float` at 48 kHz. `Player` (unchanged, not in the diff) owns the ring through a `std::unique_ptr`, starts the decoder thread and the audio stream, and on shutdown stops the stream and joins the decoder thread before the ring is destroyed. The decoder thread pushes each decoded block and, when `push` copies fewer samples than it was given, sleeps 2 ms and retries with the rest. The audio callback asks for 256 samples per call, pops into the device buffer and fills whatever `pop` did not supply with silence. Tests use the repo's existing helper `tests/check.hpp` (unchanged): `TEST(name)` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure without stopping, and `tests/check_main.cpp` runs every registered test and exits non-zero on any failure.
Requirements
- `audio::SampleRing` (existing) carries decoded samples from the decoder thread to the audio callback. Exactly one thread ever calls `push` (the decoder thread) and exactly one ever calls `pop` (the audio callback, which the audio API always runs on the same real-time thread); the two run at the same time on different cores.
- `pop` runs on the real-time audio thread and must not block, take a lock, allocate or make a system call: with the current mutex, the callback sometimes waited for a decoder thread that had been preempted while holding it, which was heard as a dropout. `push` must not block either.
- The ring stays a correct FIFO for any interleaving of the two threads: every sample `pop` returns is exactly the value `push` was given for that position, in push order, each one once. No sample may be lost, duplicated or replaced by an older value, and the code must be free of data races under the C++ memory model (the player ships on Apple Silicon Macs and on x86-64 and arm64 Linux).
- Unchanged interface: `push` copies as many samples as fit and returns how many it copied (0 when the ring is full); `pop` copies up to `out.size()` of the oldest samples and returns how many (0 when the ring is empty); the capacity stays 4096 samples.
Files touched
- src/audio/sample_ring.hpp
- src/audio/sample_ring.cpp
- tests/sample_ring_test.cpp