From cba69fdcd439c5f225bbddf1dad70e6326edd0dc Mon Sep 17 00:00:00 2001
From: bunnei <bunneidev@gmail.com>
Date: Thu, 3 May 2018 00:16:12 -0400
Subject: core: Support session close with multicore.

---
 src/core/core.cpp | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

(limited to 'src/core/core.cpp')

diff --git a/src/core/core.cpp b/src/core/core.cpp
index 066423f234..1e6be34c88 100644
--- a/src/core/core.cpp
+++ b/src/core/core.cpp
@@ -37,6 +37,9 @@ static void RunCpuCore(std::shared_ptr<Cpu> cpu_state) {
 System::ResultStatus System::RunLoop(bool tight_loop) {
     status = ResultStatus::Success;
 
+    // Update thread_to_cpu in case Core 0 is run from a different host thread
+    thread_to_cpu[std::this_thread::get_id()] = cpu_cores[0];
+
     if (GDBStub::IsServerEnabled()) {
         GDBStub::HandlePacket();
 
@@ -186,17 +189,21 @@ void System::Shutdown() {
     gpu_core.reset();
 
     // Close all CPU/threading state
-    thread_to_cpu.clear();
-    for (auto& cpu_core : cpu_cores) {
-        cpu_core.reset();
-    }
+    cpu_barrier->NotifyEnd();
     for (auto& thread : cpu_core_threads) {
         thread->join();
         thread.reset();
     }
+    thread_to_cpu.clear();
+    for (auto& cpu_core : cpu_cores) {
+        cpu_core.reset();
+    }
+    cpu_barrier.reset();
 
+    // Close core timing
     CoreTiming::Shutdown();
 
+    // Close app loader
     app_loader.reset();
 
     NGLOG_DEBUG(Core, "Shutdown OK");
-- 
cgit v1.2.3-70-g09d2