Skip to content

Commit

Permalink
AlsaSoundStream: Convert volatile variables to atomics
Browse files Browse the repository at this point in the history
  • Loading branch information
lioncash committed May 10, 2015
1 parent 9415dc9 commit fda054f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 5 additions & 5 deletions Source/Core/AudioCommon/AlsaSoundStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ AlsaSound::~AlsaSound()
bool AlsaSound::Start()
{
thread = std::thread(&AlsaSound::SoundLoop, this);
thread_data = 0;
thread_data.store(0);
return true;
}

void AlsaSound::Stop()
{
thread_data = 1;
thread_data.store(1);
thread.join();
}

Expand All @@ -42,11 +42,11 @@ void AlsaSound::Update()
void AlsaSound::SoundLoop()
{
if (!AlsaInit()) {
thread_data = 2;
thread_data.store(2);
return;
}
Common::SetCurrentThreadName("Audio thread - alsa");
while (!thread_data)
while (thread_data.load() == 0)
{
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);
Expand All @@ -61,7 +61,7 @@ void AlsaSound::SoundLoop()
}
}
AlsaShutdown();
thread_data = 2;
thread_data.store(2);
}

bool AlsaSound::AlsaInit()
Expand Down
6 changes: 4 additions & 2 deletions Source/Core/AudioCommon/AlsaSoundStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

#pragma once

#include <atomic>
#include <thread>

#if defined(HAVE_ALSA) && HAVE_ALSA
#include <alsa/asoundlib.h>
#endif

#include "AudioCommon/SoundStream.h"
#include "Common/CommonTypes.h"
#include "Common/Thread.h"

class AlsaSound final : public SoundStream
{
Expand Down Expand Up @@ -39,7 +41,7 @@ class AlsaSound final : public SoundStream
// 0 = continue
// 1 = shutdown
// 2 = done shutting down.
volatile int thread_data;
std::atomic<int> thread_data;

snd_pcm_t *handle;
int frames_to_deliver;
Expand Down

0 comments on commit fda054f

Please sign in to comment.