From daf43989fcefe8c8e5436909758fb70f4c5f788d Mon Sep 17 00:00:00 2001 From: Eladash Date: Thu, 25 Aug 2022 12:12:29 +0300 Subject: [PATCH] Thread.h: Improve thread abort performance --- Utilities/Thread.h | 10 +++++++++- rpcs3/Emu/CPU/CPUThread.cpp | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Utilities/Thread.h b/Utilities/Thread.h index c378b872f9..ae4d625f1e 100644 --- a/Utilities/Thread.h +++ b/Utilities/Thread.h @@ -635,9 +635,11 @@ public: // Join thread by thread_state::finished named_thread& operator=(thread_state s) { + bool notify_sync = false; + if (s >= thread_state::aborting && thread::m_sync.fetch_op([](u64& v){ return !(v & 3) && (v |= 1); }).second) { - thread::m_sync.notify_one(1); + notify_sync = true; } if constexpr (std::is_assignable_v) @@ -645,6 +647,12 @@ public: static_cast(*this) = s; } + if (notify_sync) + { + // Notify after context abortion has been made so all conditions for wake-up be satisfied by the time of notification + thread::m_sync.notify_one(1); + } + if (s == thread_state::finished) { // This participates in emulation stopping, use destruction-alike semantics diff --git a/rpcs3/Emu/CPU/CPUThread.cpp b/rpcs3/Emu/CPU/CPUThread.cpp index d48f902f63..211e5b2924 100644 --- a/rpcs3/Emu/CPU/CPUThread.cpp +++ b/rpcs3/Emu/CPU/CPUThread.cpp @@ -881,8 +881,19 @@ void cpu_thread::notify() cpu_thread& cpu_thread::operator=(thread_state) { - state += cpu_flag::exit; - state.notify_one(cpu_flag::exit); + if (state & cpu_flag::exit) + { + // Must be notified elsewhere or self-raised + return *this; + } + + const auto old = state.fetch_add(cpu_flag::exit); + + if (old & cpu_flag::wait && old.none_of(cpu_flag::again + cpu_flag::exit)) + { + state.notify_one(cpu_flag::exit); + } + return *this; }