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.
This commit is contained in:
Moncef Mechri 2015-08-09 00:37:48 +02:00
parent e27caf2e37
commit a0c5247743
1 changed files with 6 additions and 4 deletions

View File

@ -26,6 +26,12 @@ AlsaSound::~AlsaSound()
bool AlsaSound::Start() bool AlsaSound::Start()
{ {
m_thread_status.store(ALSAThreadStatus::RUNNING); m_thread_status.store(ALSAThreadStatus::RUNNING);
if (!AlsaInit())
{
m_thread_status.store(ALSAThreadStatus::STOPPED);
return false;
}
thread = std::thread(&AlsaSound::SoundLoop, this); thread = std::thread(&AlsaSound::SoundLoop, this);
return true; return true;
} }
@ -44,10 +50,6 @@ void AlsaSound::Update()
// Called on audio thread. // Called on audio thread.
void AlsaSound::SoundLoop() void AlsaSound::SoundLoop()
{ {
if (!AlsaInit()) {
m_thread_status.store(ALSAThreadStatus::STOPPED);
return;
}
Common::SetCurrentThreadName("Audio thread - alsa"); Common::SetCurrentThreadName("Audio thread - alsa");
while (m_thread_status.load() == ALSAThreadStatus::RUNNING) while (m_thread_status.load() == ALSAThreadStatus::RUNNING)
{ {