From b285c2a4ed29a126b5bcfe46e2784bd1870bdf82 Mon Sep 17 00:00:00 2001
From: Yuri Kunde Schlesner <yuriks@yuriks.net>
Date: Mon, 20 Feb 2017 13:56:58 -0800
Subject: [PATCH] Core: Make PerfStats internally locked

More ergonomic to use and will be required for upcoming changes.
---
 src/core/core.cpp                                  |  5 ++---
 src/core/core.h                                    |  3 +--
 src/core/frontend/emu_window.cpp                   |  2 +-
 src/core/hle/service/gsp_gpu.cpp                   |  3 +--
 src/core/perf_stats.cpp                            | 11 +++++++++++
 src/core/perf_stats.h                              |  7 +++++++
 src/video_core/renderer_opengl/renderer_opengl.cpp | 10 ++--------
 7 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/src/core/core.cpp b/src/core/core.cpp
index ca2c28ce4..140ff6451 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -110,8 +110,7 @@ void System::PrepareReschedule() {
 }
 
 PerfStats::Results System::GetAndResetPerfStats() {
-    auto perf_stats = this->perf_stats.Lock();
-    return perf_stats->GetAndResetStats(CoreTiming::GetGlobalTimeUs());
+    return perf_stats.GetAndResetStats(CoreTiming::GetGlobalTimeUs());
 }
 
 void System::Reschedule() {
@@ -147,7 +146,7 @@ System::ResultStatus System::Init(EmuWindow* emu_window, u32 system_mode) {
 
     // Reset counters and set time origin to current frame
     GetAndResetPerfStats();
-    perf_stats.Lock()->BeginSystemFrame();
+    perf_stats.BeginSystemFrame();
 
     return ResultStatus::Success;
 }
diff --git a/src/core/core.h b/src/core/core.h
index 3efc20c3d..db3b98a05 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -7,7 +7,6 @@
 #include <memory>
 #include <string>
 #include "common/common_types.h"
-#include "common/synchronized_wrapper.h"
 #include "core/memory.h"
 #include "core/perf_stats.h"
 
@@ -94,7 +93,7 @@ public:
         return *cpu_core;
     }
 
-    Common::SynchronizedWrapper<PerfStats> perf_stats;
+    PerfStats perf_stats;
 
 private:
     /**
diff --git a/src/core/frontend/emu_window.cpp b/src/core/frontend/emu_window.cpp
index b65d6ff58..a155b657d 100644
--- a/src/core/frontend/emu_window.cpp
+++ b/src/core/frontend/emu_window.cpp
@@ -104,7 +104,7 @@ void EmuWindow::AccelerometerChanged(float x, float y, float z) {
 void EmuWindow::GyroscopeChanged(float x, float y, float z) {
     constexpr float FULL_FPS = 60;
     float coef = GetGyroscopeRawToDpsCoefficient();
-    float stretch = Core::System::GetInstance().perf_stats.Lock()->GetLastFrameTimeScale();
+    float stretch = Core::System::GetInstance().perf_stats.GetLastFrameTimeScale();
     std::lock_guard<std::mutex> lock(gyro_mutex);
     gyro_x = static_cast<s16>(x * coef * stretch);
     gyro_y = static_cast<s16>(y * coef * stretch);
diff --git a/src/core/hle/service/gsp_gpu.cpp b/src/core/hle/service/gsp_gpu.cpp
index 67bab38da..097ed87e4 100644
--- a/src/core/hle/service/gsp_gpu.cpp
+++ b/src/core/hle/service/gsp_gpu.cpp
@@ -281,8 +281,7 @@ ResultCode SetBufferSwap(u32 screen_id, const FrameBufferInfo& info) {
 
     if (screen_id == 0) {
         MicroProfileFlip();
-        auto perf_stats = Core::System::GetInstance().perf_stats.Lock();
-        perf_stats->EndGameFrame();
+        Core::System::GetInstance().perf_stats.EndGameFrame();
     }
 
     return RESULT_SUCCESS;
diff --git a/src/core/perf_stats.cpp b/src/core/perf_stats.cpp
index 8d9e521a3..06bc788bd 100644
--- a/src/core/perf_stats.cpp
+++ b/src/core/perf_stats.cpp
@@ -3,6 +3,7 @@
 // Refer to the license.txt file included.
 
 #include <chrono>
+#include <mutex>
 #include "core/hw/gpu.h"
 #include "core/perf_stats.h"
 
@@ -12,10 +13,14 @@ using std::chrono::duration_cast;
 namespace Core {
 
 void PerfStats::BeginSystemFrame() {
+    std::lock_guard<std::mutex> lock(object_mutex);
+
     frame_begin = Clock::now();
 }
 
 void PerfStats::EndSystemFrame() {
+    std::lock_guard<std::mutex> lock(object_mutex);
+
     auto frame_end = Clock::now();
     accumulated_frametime += frame_end - frame_begin;
     system_frames += 1;
@@ -25,10 +30,14 @@ void PerfStats::EndSystemFrame() {
 }
 
 void PerfStats::EndGameFrame() {
+    std::lock_guard<std::mutex> lock(object_mutex);
+
     game_frames += 1;
 }
 
 PerfStats::Results PerfStats::GetAndResetStats(u64 current_system_time_us) {
+    std::lock_guard<std::mutex> lock(object_mutex);
+
     auto now = Clock::now();
     // Walltime elapsed since stats were reset
     auto interval = duration_cast<DoubleSecs>(now - reset_point).count();
@@ -54,6 +63,8 @@ PerfStats::Results PerfStats::GetAndResetStats(u64 current_system_time_us) {
 }
 
 double PerfStats::GetLastFrameTimeScale() {
+    std::lock_guard<std::mutex> lock(object_mutex);
+
     constexpr double FRAME_LENGTH = 1.0 / GPU::SCREEN_REFRESH_RATE;
     return duration_cast<DoubleSecs>(previous_frame_length).count() / FRAME_LENGTH;
 }
diff --git a/src/core/perf_stats.h b/src/core/perf_stats.h
index 8a03c511a..4098fc1f2 100644
--- a/src/core/perf_stats.h
+++ b/src/core/perf_stats.h
@@ -5,10 +5,15 @@
 #pragma once
 
 #include <chrono>
+#include <mutex>
 #include "common/common_types.h"
 
 namespace Core {
 
+/**
+ * Class to manage and query performance/timing statistics. All public functions of this class are
+ * thread-safe unless stated otherwise.
+ */
 class PerfStats {
 public:
     using Clock = std::chrono::high_resolution_clock;
@@ -37,6 +42,8 @@ public:
     double GetLastFrameTimeScale();
 
 private:
+    std::mutex object_mutex;
+
     Clock::time_point reset_point = Clock::now();
 
     Clock::time_point frame_begin = reset_point;
diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp
index 6bc142148..b3604106c 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.cpp
+++ b/src/video_core/renderer_opengl/renderer_opengl.cpp
@@ -145,10 +145,7 @@ void RendererOpenGL::SwapBuffers() {
 
     DrawScreens();
 
-    {
-        auto perf_stats = Core::System::GetInstance().perf_stats.Lock();
-        perf_stats->EndSystemFrame();
-    }
+    Core::System::GetInstance().perf_stats.EndSystemFrame();
 
     // Swap buffers
     render_window->PollEvents();
@@ -156,10 +153,7 @@ void RendererOpenGL::SwapBuffers() {
 
     prev_state.Apply();
 
-    {
-        auto perf_stats = Core::System::GetInstance().perf_stats.Lock();
-        perf_stats->BeginSystemFrame();
-    }
+    Core::System::GetInstance().perf_stats.BeginSystemFrame();
 
     RefreshRasterizerSetting();