From 53cf91d151d1e3d289917b63cf17ca254674f1ce Mon Sep 17 00:00:00 2001
From: Fernando Sahmkow <fsahmkow27@gmail.com>
Date: Sat, 16 Oct 2021 00:20:19 +0200
Subject: [PATCH] NvHost/Core: Address Feedback.

---
 src/core/core.cpp                             | 34 +++++++++++--------
 src/core/core.h                               |  4 ++-
 .../hle/service/nvdrv/devices/nvhost_ctrl.cpp |  8 +++--
 3 files changed, 27 insertions(+), 19 deletions(-)

diff --git a/src/core/core.cpp b/src/core/core.cpp
index 4abf037e29..3042d611b6 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -163,19 +163,19 @@ struct System::Impl {
         return status;
     }
 
-    void stallForGPU(bool pause) {
-        if (pause) {
-            suspend_guard.lock();
-            kernel.Suspend(pause);
-            core_timing.SyncPause(pause);
-            cpu_manager.Pause(pause);
-        } else {
-            if (!is_paused) {
-                core_timing.SyncPause(pause);
-                kernel.Suspend(pause);
-                cpu_manager.Pause(pause);
-            }
-            suspend_guard.unlock();
+    std::unique_lock<std::mutex> StallCPU() {
+        std::unique_lock<std::mutex> lk(suspend_guard);
+        kernel.Suspend(true);
+        core_timing.SyncPause(true);
+        cpu_manager.Pause(true);
+        return lk;
+    }
+
+    void UnstallCPU() {
+        if (!is_paused) {
+            core_timing.SyncPause(false);
+            kernel.Suspend(false);
+            cpu_manager.Pause(false);
         }
     }
 
@@ -487,8 +487,12 @@ void System::Shutdown() {
     impl->Shutdown();
 }
 
-void System::stallForGPU(bool pause) {
-    impl->stallForGPU(pause);
+std::unique_lock<std::mutex> System::StallCPU() {
+    return impl->StallCPU();
+}
+
+void System::UnstallCPU() {
+    impl->UnstallCPU();
 }
 
 SystemResultStatus System::Load(Frontend::EmuWindow& emu_window, const std::string& filepath,
diff --git a/src/core/core.h b/src/core/core.h
index 8b21816cce..1cfe1bba6e 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -7,6 +7,7 @@
 #include <cstddef>
 #include <functional>
 #include <memory>
+#include <mutex>
 #include <string>
 #include <vector>
 
@@ -160,7 +161,8 @@ public:
     /// Shutdown the emulated system.
     void Shutdown();
 
-    void stallForGPU(bool pause);
+    std::unique_lock<std::mutex> StallCPU();
+    void UnstallCPU();
 
     /**
      * Load an executable application.
diff --git a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp
index b59eae55cd..f9b82b5047 100644
--- a/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp
+++ b/src/core/hle/service/nvdrv/devices/nvhost_ctrl.cpp
@@ -150,9 +150,11 @@ NvResult nvhost_ctrl::IocCtrlEventWait(const std::vector<u8>& input, std::vector
     params.value |= event_id;
     event.event->GetWritableEvent().Clear();
     if (events_interface.failed[event_id]) {
-        system.stallForGPU(true);
-        gpu.WaitFence(params.syncpt_id, target_value);
-        system.stallForGPU(false);
+        {
+            auto lk = system.StallCPU();
+            gpu.WaitFence(params.syncpt_id, target_value);
+            system.UnstallCPU();
+        }
         std::memcpy(output.data(), &params, sizeof(params));
         events_interface.failed[event_id] = false;
         return NvResult::Success;