From a0c524774386c4543c710958bf1c607a91d6f28c Mon Sep 17 00:00:00 2001 From: Moncef Mechri Date: Sun, 9 Aug 2015 00:37:48 +0200 Subject: [PATCH] Initialize ALSA before starting the audio thread This fixes a race condition: Before this commit, there was a race condition when starting a game: Core::EmuThread(), after having started (but not necessarily completed) the initialization of the audio thread, calls Core::SetState() which calls CCPU::EnableStepping(), which in turns calls AudioCommon::ClearAudioBuffer(). This means that SoundStream::Clear() can be called before AlsaSound::AlsaInit() has completed. --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index 5902edba94..ae888564b3 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -26,6 +26,12 @@ AlsaSound::~AlsaSound() bool AlsaSound::Start() { m_thread_status.store(ALSAThreadStatus::RUNNING); + if (!AlsaInit()) + { + m_thread_status.store(ALSAThreadStatus::STOPPED); + return false; + } + thread = std::thread(&AlsaSound::SoundLoop, this); return true; } @@ -44,10 +50,6 @@ void AlsaSound::Update() // Called on audio thread. void AlsaSound::SoundLoop() { - if (!AlsaInit()) { - m_thread_status.store(ALSAThreadStatus::STOPPED); - return; - } Common::SetCurrentThreadName("Audio thread - alsa"); while (m_thread_status.load() == ALSAThreadStatus::RUNNING) {