Merge pull request #2719 from moncefmechri/bugfix-7729

Don't busy wait in the audio thread (ALSA)
This commit is contained in:
Markus Wick 2015-08-11 13:33:29 +02:00
commit ccf14e1ba4
2 changed files with 39 additions and 5 deletions

View File

@ -2,6 +2,8 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <mutex>
#include "AudioCommon/AlsaSoundStream.h"
#include "Common/CommonTypes.h"
#include "Common/Thread.h"
@ -26,6 +28,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;
}
@ -33,6 +41,10 @@ bool AlsaSound::Start()
void AlsaSound::Stop()
{
m_thread_status.store(ALSAThreadStatus::STOPPING);
//Give the opportunity to the audio thread
//to realize we are stopping the emulation
cv.notify_one();
thread.join();
}
@ -44,15 +56,14 @@ 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)
{
std::unique_lock<std::mutex> lock(cv_m);
cv.wait(lock, [this]{return !m_muted || m_thread_status.load() != ALSAThreadStatus::RUNNING;});
m_mixer->Mix(reinterpret_cast<short *>(mix_buffer), frames_to_deliver);
int rc = m_muted ? 1337 : snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
int rc = snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
if (rc == -EPIPE)
{
// Underrun
@ -67,6 +78,24 @@ void AlsaSound::SoundLoop()
m_thread_status.store(ALSAThreadStatus::STOPPED);
}
void AlsaSound::Clear(bool muted)
{
m_muted = muted;
if (m_muted)
{
std::lock_guard<std::mutex> lock(cv_m);
snd_pcm_drop(handle);
}
else
{
std::unique_lock<std::mutex> lock(cv_m);
snd_pcm_prepare(handle);
lock.unlock();
cv.notify_one();
}
}
bool AlsaSound::AlsaInit()
{
unsigned int sample_rate = m_mixer->GetSampleRate();

View File

@ -5,6 +5,8 @@
#pragma once
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#if defined(HAVE_ALSA) && HAVE_ALSA
@ -25,6 +27,7 @@ public:
void SoundLoop() override;
void Stop() override;
void Update() override;
void Clear(bool) override;
static bool isValid()
{
@ -45,6 +48,8 @@ private:
u8 *mix_buffer;
std::thread thread;
std::atomic<ALSAThreadStatus> m_thread_status;
std::condition_variable cv;
std::mutex cv_m;
snd_pcm_t *handle;
int frames_to_deliver;