Fix PCM buffer thread safety with proper mutex#990
Merged
Conversation
kblaschke
approved these changes
Apr 10, 2026
Member
kblaschke
left a comment
There was a problem hiding this comment.
One of the low-priority, but recently more frequently reported issues to be fixed. LGTM, will merge.
The circular audio buffer was protected only by a std::atomic on the write index, which is insufficient: a multi-sample AddToBuffer() call can be interrupted mid-write by UpdateFrameAudioData() reading the buffer, producing torn waveform data. Replace the atomic index with a std::mutex that protects both the buffer writes in AddToBuffer() and the bulk copy in UpdateFrameAudioData(). The lock is held only for the buffer access, not for the downstream spectrum analysis. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
6d00417 to
16f40af
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The PCM circular input buffer is written by the audio thread (via
projectm_pcm_add_*) and read by the render thread (viaRenderFrame→UpdateFrameAudioData→CopyNewWaveformData). The previous design usedstd::atomic<size_t> m_startalone to coordinate, but this is insufficient — the buffer contents themselves are not atomic, so concurrent reads/writes are a data race in C++ and have been observed to cause audio corruption and crashes under sustained load.This adds a
std::mutexto protect the input buffer, locked inAddToBuffer(audio write path) and aroundCopyNewWaveformDatainUpdateFrameAudioData(render read path). The lock is released before the heavier spectrum/FFT work, so the audio thread is only blocked for the duration of a sample copy (microseconds).Changes
src/libprojectM/Audio/PCM.{cpp,hpp}(+13, -5)std::mutex m_pcmMutexto PCMAddToBufferand aroundCopyNewWaveformDatareadsstd::atomic<size_t> m_start(now protected by the mutex) and remove unused<atomic>includeDesign notes
A lock-free SPSC design was considered but rejected for this use case:
std::atomic<float>would kill SIMD and roughly double memoryTest plan