From 9f2c703137661473c7362f9c9d6a9df8bedce862 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 12 Feb 2020 15:07:15 -0500 Subject: [PATCH 1/2] address_arbiter: Simplify GetThreadsWaitingOnAddress() Simplifies the overall function and also allows for it to become a const-qualified member function. --- src/core/hle/kernel/address_arbiter.cpp | 17 ++++++++--------- src/core/hle/kernel/address_arbiter.h | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp index 2ea3dcb61..a1287de93 100644 --- a/src/core/hle/kernel/address_arbiter.cpp +++ b/src/core/hle/kernel/address_arbiter.cpp @@ -228,15 +228,14 @@ void AddressArbiter::RemoveThread(std::shared_ptr thread) { UNREACHABLE(); } -std::vector> AddressArbiter::GetThreadsWaitingOnAddress(VAddr address) { - std::vector> result; - std::list>& thread_list = arb_threads[address]; - auto it = thread_list.begin(); - while (it != thread_list.end()) { - std::shared_ptr current_thread = *it; - result.push_back(std::move(current_thread)); - ++it; +std::vector> AddressArbiter::GetThreadsWaitingOnAddress( + VAddr address) const { + const auto iter = arb_threads.find(address); + if (iter == arb_threads.cend()) { + return {}; } - return result; + + const std::list>& thread_list = iter->second; + return {thread_list.cbegin(), thread_list.cend()}; } } // namespace Kernel diff --git a/src/core/hle/kernel/address_arbiter.h b/src/core/hle/kernel/address_arbiter.h index 386983e54..f958eee5a 100644 --- a/src/core/hle/kernel/address_arbiter.h +++ b/src/core/hle/kernel/address_arbiter.h @@ -86,7 +86,7 @@ private: void RemoveThread(std::shared_ptr thread); // Gets the threads waiting on an address. - std::vector> GetThreadsWaitingOnAddress(VAddr address); + std::vector> GetThreadsWaitingOnAddress(VAddr address) const; /// List of threads waiting for a address arbiter std::unordered_map>> arb_threads; From be269e21a53cb317534477657ca232bb3f51aa26 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Wed, 12 Feb 2020 15:26:31 -0500 Subject: [PATCH 2/2] address_arbiter: Collapse loops in InsertThread() and RemoveThread() Same behavior, but without the need to explicitly loop through everything manually. --- src/core/hle/kernel/address_arbiter.cpp | 36 ++++++++++++------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/core/hle/kernel/address_arbiter.cpp b/src/core/hle/kernel/address_arbiter.cpp index a1287de93..8475b698c 100644 --- a/src/core/hle/kernel/address_arbiter.cpp +++ b/src/core/hle/kernel/address_arbiter.cpp @@ -201,31 +201,29 @@ void AddressArbiter::HandleWakeupThread(std::shared_ptr thread) { void AddressArbiter::InsertThread(std::shared_ptr thread) { const VAddr arb_addr = thread->GetArbiterWaitAddress(); std::list>& thread_list = arb_threads[arb_addr]; - auto it = thread_list.begin(); - while (it != thread_list.end()) { - const std::shared_ptr& current_thread = *it; - if (current_thread->GetPriority() >= thread->GetPriority()) { - thread_list.insert(it, thread); - return; - } - ++it; + + const auto iter = + std::find_if(thread_list.cbegin(), thread_list.cend(), [&thread](const auto& entry) { + return entry->GetPriority() >= thread->GetPriority(); + }); + + if (iter == thread_list.cend()) { + thread_list.push_back(std::move(thread)); + } else { + thread_list.insert(iter, std::move(thread)); } - thread_list.push_back(std::move(thread)); } void AddressArbiter::RemoveThread(std::shared_ptr thread) { const VAddr arb_addr = thread->GetArbiterWaitAddress(); std::list>& thread_list = arb_threads[arb_addr]; - auto it = thread_list.begin(); - while (it != thread_list.end()) { - const std::shared_ptr& current_thread = *it; - if (current_thread.get() == thread.get()) { - thread_list.erase(it); - return; - } - ++it; - } - UNREACHABLE(); + + const auto iter = std::find_if(thread_list.cbegin(), thread_list.cend(), + [&thread](const auto& entry) { return thread == entry; }); + + ASSERT(iter != thread_list.cend()); + + thread_list.erase(iter); } std::vector> AddressArbiter::GetThreadsWaitingOnAddress(