From bc46089ab0ca7963ca3b5507969a3df0f2afca6c Mon Sep 17 00:00:00 2001 From: Sam Belliveau Date: Fri, 30 Dec 2022 14:56:08 -0500 Subject: [PATCH] PerformanceTracker: Use shared_mutex instead of mutex so multiple threads can read at the same time. --- .../Core/VideoCommon/PerformanceTracker.cpp | 20 ++++++++++--------- Source/Core/VideoCommon/PerformanceTracker.h | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Source/Core/VideoCommon/PerformanceTracker.cpp b/Source/Core/VideoCommon/PerformanceTracker.cpp index ffde157883..305fb35598 100644 --- a/Source/Core/VideoCommon/PerformanceTracker.cpp +++ b/Source/Core/VideoCommon/PerformanceTracker.cpp @@ -5,8 +5,10 @@ #include #include -#include #include +#include + +#include #include "Common/CommonTypes.h" #include "Common/FileUtil.h" @@ -36,7 +38,7 @@ PerformanceTracker::~PerformanceTracker() void PerformanceTracker::Reset() { - std::lock_guard lock{m_mutex}; + std::unique_lock lock{m_mutex}; QueueClear(); m_last_time = Clock::now(); @@ -47,7 +49,7 @@ void PerformanceTracker::Reset() void PerformanceTracker::Count() { - std::lock_guard lock{m_mutex}; + std::unique_lock lock{m_mutex}; if (m_paused) return; @@ -97,19 +99,19 @@ DT PerformanceTracker::GetSampleWindow() const double PerformanceTracker::GetHzAvg() const { - std::lock_guard lock{m_mutex}; + std::shared_lock lock{m_mutex}; return m_hz_avg; } DT PerformanceTracker::GetDtAvg() const { - std::lock_guard lock{m_mutex}; + std::shared_lock lock{m_mutex}; return m_dt_avg; } DT PerformanceTracker::GetDtStd() const { - std::lock_guard lock{m_mutex}; + std::unique_lock lock{m_mutex}; if (m_dt_std) return *m_dt_std; @@ -130,7 +132,7 @@ DT PerformanceTracker::GetDtStd() const DT PerformanceTracker::GetLastRawDt() const { - std::lock_guard lock{m_mutex}; + std::shared_lock lock{m_mutex}; if (QueueEmpty()) return DT::zero(); @@ -142,7 +144,7 @@ void PerformanceTracker::ImPlotPlotLines(const char* label) const { static std::array x, y; - std::lock_guard lock{m_mutex}; + std::shared_lock lock{m_mutex}; if (QueueEmpty()) return; @@ -238,7 +240,7 @@ void PerformanceTracker::LogRenderTimeToFile(DT val) void PerformanceTracker::SetPaused(bool paused) { - std::lock_guard lock{m_mutex}; + std::unique_lock lock{m_mutex}; m_paused = paused; if (m_paused) diff --git a/Source/Core/VideoCommon/PerformanceTracker.h b/Source/Core/VideoCommon/PerformanceTracker.h index e253f83352..4136664981 100644 --- a/Source/Core/VideoCommon/PerformanceTracker.h +++ b/Source/Core/VideoCommon/PerformanceTracker.h @@ -6,8 +6,8 @@ #include #include #include -#include #include +#include #include "Common/CommonTypes.h" @@ -100,5 +100,5 @@ private: // Functions for managing dt queue mutable std::optional
m_dt_std = std::nullopt; // Used to enable thread safety with the performance tracker - mutable std::mutex m_mutex; + mutable std::shared_mutex m_mutex; };