Przeglądaj źródła

core: Support session close with multicore.

bunnei 8 lat temu
rodzic
commit
cba69fdcd4
4 zmienionych plików z 47 dodań i 16 usunięć
  1. 11 4
      src/core/core.cpp
  2. 1 1
      src/core/core.h
  3. 28 1
      src/core/core_cpu.cpp
  4. 7 10
      src/core/core_cpu.h

+ 11 - 4
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");

+ 1 - 1
src/core/core.h

@@ -92,7 +92,7 @@ public:
      * @returns True if the emulated system is powered on, otherwise false.
      */
     bool IsPoweredOn() const {
-        return cpu_cores[0] != nullptr;
+        return cpu_barrier && cpu_barrier->IsAlive();
     }
 
     /**

+ 28 - 1
src/core/core_cpu.cpp

@@ -19,6 +19,30 @@
 
 namespace Core {
 
+void CpuBarrier::NotifyEnd() {
+    std::unique_lock<std::mutex> lock(mutex);
+    end = true;
+    condition.notify_all();
+}
+
+bool CpuBarrier::Rendezvous() {
+    if (end) {
+        return false;
+    } else {
+        std::unique_lock<std::mutex> lock(mutex);
+
+        --cores_waiting;
+        if (!cores_waiting) {
+            cores_waiting = NUM_CPU_CORES;
+            condition.notify_all();
+            return true;
+        }
+
+        condition.wait(lock);
+        return true;
+    }
+}
+
 Cpu::Cpu(std::shared_ptr<CpuBarrier> cpu_barrier, size_t core_index)
     : cpu_barrier{std::move(cpu_barrier)}, core_index{core_index} {
 
@@ -38,7 +62,10 @@ Cpu::Cpu(std::shared_ptr<CpuBarrier> cpu_barrier, size_t core_index)
 
 void Cpu::RunLoop(bool tight_loop) {
     // Wait for all other CPU cores to complete the previous slice, such that they run in lock-step
-    cpu_barrier->Rendezvous();
+    if (!cpu_barrier->Rendezvous()) {
+        // If rendezvous failed, session has been killed
+        return;
+    }
 
     // If we don't have a currently active thread then don't execute instructions,
     // instead advance to the next event and try to yield to the next thread

+ 7 - 10
src/core/core_cpu.h

@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <atomic>
 #include <condition_variable>
 #include <memory>
 #include <mutex>
@@ -22,23 +23,19 @@ constexpr unsigned NUM_CPU_CORES{4};
 
 class CpuBarrier {
 public:
-    void Rendezvous() {
-        std::unique_lock<std::mutex> lock(mutex);
+    bool IsAlive() const {
+        return !end;
+    }
 
-        --cores_waiting;
-        if (!cores_waiting) {
-            cores_waiting = NUM_CPU_CORES;
-            condition.notify_all();
-            return;
-        }
+    void NotifyEnd();
 
-        condition.wait(lock);
-    }
+    bool Rendezvous();
 
 private:
     unsigned cores_waiting{NUM_CPU_CORES};
     std::mutex mutex;
     std::condition_variable condition;
+    std::atomic<bool> end{};
 };
 
 class Cpu {