audio_stream: remove unreachable wait on never-notified condvar
AudioStream::BeginWrite() used to wait on m_buffer_draining_cv when the FIFO didn't have enough free space for the requested batch. Nothing in the codebase ever calls notify_one()/notify_all() on that condition variable, so the wait could only return spuriously, and on implementations without spurious wakeups the SPU would deadlock the moment the buffer truly filled up. It does not fill up in practice today - the libretro path drains the buffer in full once per retro_run via UploadToFrontend(), and m_max_samples is set to 2 * audio_buffer_size (default 4096), several times smaller than the FIFO capacity (32 768). But 'cannot happen today' is not a safe contract to leave in production code, especially when the failure mode is a hard hang. Drop the CV entirely and rewrite BeginWrite() to be non-blocking. It now hands the SPU whatever contiguous space the FIFO currently exposes (capped at m_buffer_size); EndWrite() records the count the caller actually used, exactly as before. The SPU's outer loop in spu.cpp already calls BeginWrite/EndWrite repeatedly until all generated frames are consumed, so a short return is benign. The mutex is kept: in libretro mode producer and consumer run on the same thread (retro_run_frame), so it's always uncontended and near-free, but other hosts that drive audio output from a separate thread still rely on it for safety. Comment added accordingly. Also drops the now-unused #include <condition_variable> and the m_buffer_draining_cv member.
This commit is contained in:
parent
5e17e70de0
commit
8bebcfb6d9
2 changed files with 20 additions and 17 deletions
|
|
@ -33,23 +33,24 @@ void AudioStream::Shutdown()
|
|||
|
||||
void AudioStream::BeginWrite(SampleType** buffer_ptr, u32* num_frames)
|
||||
{
|
||||
// The SPU calls BeginWrite/EndWrite from the same thread that drains
|
||||
// the buffer (in libretro mode, that is retro_run_frame()). The mutex
|
||||
// is therefore always uncontended in single-threaded use; it remains
|
||||
// here so that backends with their own audio output threads stay
|
||||
// safe.
|
||||
//
|
||||
// Previously this routine waited on a condition variable whenever
|
||||
// `buffer_space < requested_size`. Nothing in the codebase ever
|
||||
// notified that CV (it was wait-only), so the SPU could deadlock if
|
||||
// the buffer ever filled up. A non-blocking design is safer: in
|
||||
// single-threaded operation the buffer never fills (m_max_samples is
|
||||
// 2 * buffer_size, currently 8x smaller than the FIFO capacity, and
|
||||
// UploadToFrontend drains it every retro_run); on the rare overrun
|
||||
// path we simply give the SPU what space is contiguously available
|
||||
// and let it write a smaller batch. EndWrite passes the actual
|
||||
// count.
|
||||
m_buffer_mutex.lock();
|
||||
|
||||
const u32 requested_frames = std::min(*num_frames, m_buffer_size);
|
||||
u32 size = requested_frames * AUDIO_CHANNELS;
|
||||
u32 buffer_space = m_max_samples - m_buffer.GetSize();
|
||||
if (buffer_space < size)
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(m_buffer_mutex, std::adopt_lock);
|
||||
m_buffer_draining_cv.wait(lock, [this, size]()
|
||||
{
|
||||
u32 buffer_space = m_max_samples - m_buffer.GetSize();
|
||||
return buffer_space >= size;
|
||||
}
|
||||
);
|
||||
lock.release();
|
||||
}
|
||||
|
||||
*buffer_ptr = m_buffer.GetWritePointer();
|
||||
*num_frames = std::min(m_buffer_size, m_buffer.GetContiguousSpace() / AUDIO_CHANNELS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
#include "fifo_queue.h"
|
||||
#include "types.h"
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
|
|
@ -45,8 +44,11 @@ protected:
|
|||
u32 m_buffer_size = 0;
|
||||
|
||||
HeapFIFOQueue<SampleType, MaxSamples> m_buffer;
|
||||
// Held while BeginWrite/EndWrite are in flight. In libretro mode the
|
||||
// producer (SPU) and consumer (UploadToFrontend) run on the same
|
||||
// thread, so this is always uncontended; it stays in place so other
|
||||
// hosts that drive audio from a separate output thread remain safe.
|
||||
mutable std::mutex m_buffer_mutex;
|
||||
std::condition_variable m_buffer_draining_cv;
|
||||
|
||||
u32 m_max_samples = 0;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue