From d34a9afe049d9b800a4ce70158091427cdb958be Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 6 Apr 2019 17:39:25 -0500 Subject: [PATCH] Minor changes to usages of std::condition_variable. --- Source/Core/AudioCommon/AlsaSoundStream.cpp | 10 +++++++++- Source/Core/Common/Event.h | 13 ++++++++++++- Source/Core/VideoCommon/RenderBase.h | 1 - 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Source/Core/AudioCommon/AlsaSoundStream.cpp b/Source/Core/AudioCommon/AlsaSoundStream.cpp index 3d20331abd..d249640d1b 100644 --- a/Source/Core/AudioCommon/AlsaSoundStream.cpp +++ b/Source/Core/AudioCommon/AlsaSoundStream.cpp @@ -19,6 +19,9 @@ AlsaSound::~AlsaSound() { m_thread_status.store(ALSAThreadStatus::STOPPING); + // Immediately lock and unlock mutex to prevent cv race. + std::unique_lock{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{cv_m}; + + // Notify thread that status has changed + cv.notify_one(); return true; } diff --git a/Source/Core/Common/Event.h b/Source/Core/Common/Event.h index a85f3326a7..85c2040e0f 100644 --- a/Source/Core/Common/Event.h +++ b/Source/Core/Common/Event.h @@ -31,7 +31,18 @@ public: { if (m_flag.TestAndSet()) { - std::lock_guard 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 lk(m_mutex); + } + m_condvar.notify_one(); } } diff --git a/Source/Core/VideoCommon/RenderBase.h b/Source/Core/VideoCommon/RenderBase.h index af9c1a8972..48e0c5d62a 100644 --- a/Source/Core/VideoCommon/RenderBase.h +++ b/Source/Core/VideoCommon/RenderBase.h @@ -15,7 +15,6 @@ #pragma once #include -#include #include #include #include