AlsaSoundStream: Use an enum class for signifying ALSA thread state

This commit is contained in:
Lioncash 2015-05-10 02:30:24 -04:00
parent 353205132c
commit f907e5cace
2 changed files with 18 additions and 10 deletions

View File

@ -10,7 +10,11 @@
#define BUFFER_SIZE_MAX 8192 #define BUFFER_SIZE_MAX 8192
#define BUFFER_SIZE_BYTES (BUFFER_SIZE_MAX*2*2) #define BUFFER_SIZE_BYTES (BUFFER_SIZE_MAX*2*2)
AlsaSound::AlsaSound(CMixer *mixer) : SoundStream(mixer), thread_data(0), handle(nullptr), frames_to_deliver(FRAME_COUNT_MIN) AlsaSound::AlsaSound(CMixer *mixer)
: SoundStream(mixer)
, m_thread_status(ALSAThreadStatus::STOPPED)
, handle(nullptr)
, frames_to_deliver(FRAME_COUNT_MIN)
{ {
mix_buffer = new u8[BUFFER_SIZE_BYTES]; mix_buffer = new u8[BUFFER_SIZE_BYTES];
} }
@ -22,14 +26,14 @@ AlsaSound::~AlsaSound()
bool AlsaSound::Start() bool AlsaSound::Start()
{ {
m_thread_status.store(ALSAThreadStatus::RUNNING);
thread = std::thread(&AlsaSound::SoundLoop, this); thread = std::thread(&AlsaSound::SoundLoop, this);
thread_data.store(0);
return true; return true;
} }
void AlsaSound::Stop() void AlsaSound::Stop()
{ {
thread_data.store(1); m_thread_status.store(ALSAThreadStatus::STOPPING);
thread.join(); thread.join();
} }
@ -42,11 +46,11 @@ void AlsaSound::Update()
void AlsaSound::SoundLoop() void AlsaSound::SoundLoop()
{ {
if (!AlsaInit()) { if (!AlsaInit()) {
thread_data.store(2); m_thread_status.store(ALSAThreadStatus::STOPPED);
return; return;
} }
Common::SetCurrentThreadName("Audio thread - alsa"); Common::SetCurrentThreadName("Audio thread - alsa");
while (thread_data.load() == 0) while (m_thread_status.load() == ALSAThreadStatus::RUNNING)
{ {
m_mixer->Mix(reinterpret_cast<short *>(mix_buffer), frames_to_deliver); 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 = m_muted ? 1337 : snd_pcm_writei(handle, mix_buffer, frames_to_deliver);
@ -61,7 +65,7 @@ void AlsaSound::SoundLoop()
} }
} }
AlsaShutdown(); AlsaShutdown();
thread_data.store(2); m_thread_status.store(ALSAThreadStatus::STOPPED);
} }
bool AlsaSound::AlsaInit() bool AlsaSound::AlsaInit()

View File

@ -33,15 +33,19 @@ public:
virtual void Update() override; virtual void Update() override;
private: private:
enum class ALSAThreadStatus
{
RUNNING,
STOPPING,
STOPPED,
};
bool AlsaInit(); bool AlsaInit();
void AlsaShutdown(); void AlsaShutdown();
u8 *mix_buffer; u8 *mix_buffer;
std::thread thread; std::thread thread;
// 0 = continue std::atomic<ALSAThreadStatus> m_thread_status;
// 1 = shutdown
// 2 = done shutting down.
std::atomic<int> thread_data;
snd_pcm_t *handle; snd_pcm_t *handle;
int frames_to_deliver; int frames_to_deliver;