AlsaSoundStream: Convert volatile variables to atomics

This commit is contained in:
Lioncash 2015-05-10 00:20:27 -04:00
parent 7b376abd3b
commit 353205132c
2 changed files with 9 additions and 7 deletions

View File

@ -23,13 +23,13 @@ AlsaSound::~AlsaSound()
bool AlsaSound::Start() bool AlsaSound::Start()
{ {
thread = std::thread(&AlsaSound::SoundLoop, this); thread = std::thread(&AlsaSound::SoundLoop, this);
thread_data = 0; thread_data.store(0);
return true; return true;
} }
void AlsaSound::Stop() void AlsaSound::Stop()
{ {
thread_data = 1; thread_data.store(1);
thread.join(); thread.join();
} }
@ -42,11 +42,11 @@ void AlsaSound::Update()
void AlsaSound::SoundLoop() void AlsaSound::SoundLoop()
{ {
if (!AlsaInit()) { if (!AlsaInit()) {
thread_data = 2; thread_data.store(2);
return; return;
} }
Common::SetCurrentThreadName("Audio thread - alsa"); Common::SetCurrentThreadName("Audio thread - alsa");
while (!thread_data) while (thread_data.load() == 0)
{ {
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 +61,7 @@ void AlsaSound::SoundLoop()
} }
} }
AlsaShutdown(); AlsaShutdown();
thread_data = 2; thread_data.store(2);
} }
bool AlsaSound::AlsaInit() bool AlsaSound::AlsaInit()

View File

@ -4,13 +4,15 @@
#pragma once #pragma once
#include <atomic>
#include <thread>
#if defined(HAVE_ALSA) && HAVE_ALSA #if defined(HAVE_ALSA) && HAVE_ALSA
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#endif #endif
#include "AudioCommon/SoundStream.h" #include "AudioCommon/SoundStream.h"
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/Thread.h"
class AlsaSound final : public SoundStream class AlsaSound final : public SoundStream
{ {
@ -39,7 +41,7 @@ private:
// 0 = continue // 0 = continue
// 1 = shutdown // 1 = shutdown
// 2 = done shutting down. // 2 = done shutting down.
volatile int thread_data; std::atomic<int> thread_data;
snd_pcm_t *handle; snd_pcm_t *handle;
int frames_to_deliver; int frames_to_deliver;