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

Files touched

Review this PR

C++ practice