From 7eebe0693189c931e493842dea69a0865943d4b1 Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Tue, 25 Feb 2020 11:43:04 +0300 Subject: [PATCH] Restore thread counter (world may be not ready yet) Remove dumb 1300ms timeout. --- Utilities/Thread.cpp | 3 +++ Utilities/Thread.h | 8 ++++++++ rpcs3/Emu/CPU/CPUThread.cpp | 3 --- rpcs3/Emu/System.cpp | 8 ++++++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Utilities/Thread.cpp b/Utilities/Thread.cpp index 6169605bd7..f22f33a63c 100644 --- a/Utilities/Thread.cpp +++ b/Utilities/Thread.cpp @@ -1722,6 +1722,8 @@ DECLARE(thread_ctrl::g_native_core_layout) { native_core_arrangement::undefined void thread_base::start(native_entry entry) { + thread_ctrl::g_thread_count++; + #ifdef _WIN32 m_thread = ::_beginthreadex(nullptr, 0, entry, this, CREATE_SUSPENDED, nullptr); verify("thread_ctrl::start" HERE), m_thread, ::ResumeThread(reinterpret_cast(+m_thread)) != -1; @@ -1863,6 +1865,7 @@ void thread_base::finalize() noexcept { g_tls_log_prefix = []() -> std::string { return {}; }; thread_ctrl::g_tls_this_thread = nullptr; + thread_ctrl::g_thread_count--; } void thread_ctrl::_wait_for(u64 usec, bool alert /* true */) diff --git a/Utilities/Thread.h b/Utilities/Thread.h index e31158047c..833c8c12b6 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -192,6 +192,9 @@ class thread_ctrl final // Target cpu core layout static atomic_t g_native_core_layout; + // Global thread counter + static inline atomic_t g_thread_count = 0; + // Internal waiting function, may throw. Infinite value is -1. static void _wait_for(u64 usec, bool alert); @@ -275,6 +278,11 @@ public: return g_tls_this_thread; } + static u64 get_count() + { + return g_thread_count.load(); + } + // Detect layout static void detect_cpu_layout(); diff --git a/rpcs3/Emu/CPU/CPUThread.cpp b/rpcs3/Emu/CPU/CPUThread.cpp index 86337ba551..186bc9d167 100644 --- a/rpcs3/Emu/CPU/CPUThread.cpp +++ b/rpcs3/Emu/CPU/CPUThread.cpp @@ -630,9 +630,6 @@ void cpu_thread::stop_all() noexcept std::this_thread::sleep_for(10ms); } - // Workaround for remaining threads (TODO) - std::this_thread::sleep_for(1300ms); - sys_log.notice("All CPU threads have been stopped."); } diff --git a/rpcs3/Emu/System.cpp b/rpcs3/Emu/System.cpp index 7d256b6738..d61225bd43 100644 --- a/rpcs3/Emu/System.cpp +++ b/rpcs3/Emu/System.cpp @@ -1600,6 +1600,14 @@ void Emulator::Stop(bool restart) cpu_thread::stop_all(); g_fxo->reset(); + + while (thread_ctrl::get_count()) + { + std::this_thread::sleep_for(10ms); + } + + sys_log.notice("All threads have been stopped."); + lv2_obj::cleanup(); idm::clear();