Skip to content
Permalink
Browse files

Minor changes to usages of std::condition_variable.

  • Loading branch information...
jordan-woyak committed Apr 6, 2019
1 parent 75e7431 commit d34a9afe049d9b800a4ce70158091427cdb958be
Showing with 21 additions and 3 deletions.
  1. +9 −1 Source/Core/AudioCommon/AlsaSoundStream.cpp
  2. +12 −1 Source/Core/Common/Event.h
  3. +0 −1 Source/Core/VideoCommon/RenderBase.h
@@ -19,6 +19,9 @@ AlsaSound::~AlsaSound()
{
m_thread_status.store(ALSAThreadStatus::STOPPING);

// Immediately lock and unlock mutex to prevent cv race.
std::unique_lock<std::mutex>{cv_m};

// Give the opportunity to the audio thread
// to realize we are stopping the emulation
cv.notify_one();
@@ -81,7 +84,12 @@ void AlsaSound::SoundLoop()
bool AlsaSound::SetRunning(bool running)
{
m_thread_status.store(running ? ALSAThreadStatus::RUNNING : ALSAThreadStatus::PAUSED);
cv.notify_one(); // Notify thread that status has changed

// Immediately lock and unlock mutex to prevent cv race.
std::unique_lock<std::mutex>{cv_m};

// Notify thread that status has changed
cv.notify_one();
return true;
}

@@ -31,7 +31,18 @@ class Event final
{
if (m_flag.TestAndSet())
{
std::lock_guard<std::mutex> lk(m_mutex);
// Lock and immediately unlock m_mutex.
{
// Holding the lock at any time between the change of our flag and notify call
// is sufficient to prevent a race where both of these actions
// happen between the other thread's predicate test and wait call
// which would cause wait to block until the next spurious wakeup or timeout.

// Unlocking before notification is a micro-optimization to prevent
// the notified thread from immediately blocking on the mutex.
std::lock_guard<std::mutex> lk(m_mutex);
}

m_condvar.notify_one();
}
}
@@ -15,7 +15,6 @@
#pragma once

#include <array>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <string>

0 comments on commit d34a9af

Please sign in to comment.
You can’t perform that action at this time.