From 685b3ee41baf37ee84258064ab5a3e1673611dae Mon Sep 17 00:00:00 2001 From: kd-11 Date: Sat, 25 Nov 2023 02:56:22 +0300 Subject: [PATCH] rsx: Code quality and readability improvements --- rpcs3/Emu/RSX/Core/RSXIOMap.hpp | 45 +++++++++++-- rpcs3/Emu/RSX/Core/RSXReservationLock.hpp | 81 ++++++++++++++--------- 2 files changed, 89 insertions(+), 37 deletions(-) diff --git a/rpcs3/Emu/RSX/Core/RSXIOMap.hpp b/rpcs3/Emu/RSX/Core/RSXIOMap.hpp index b4ea4b5bc8..4632160bfd 100644 --- a/rpcs3/Emu/RSX/Core/RSXIOMap.hpp +++ b/rpcs3/Emu/RSX/Core/RSXIOMap.hpp @@ -6,6 +6,43 @@ namespace rsx { + namespace iomap_helper + { + template + struct io_lock + { + shared_mutex& ref; + + io_lock(shared_mutex& obj) + : ref(obj) + {} + + bool try_lock() + { + if constexpr (Shared) + { + return ref.try_lock_shared(); + } + else + { + return ref.try_lock(); + } + } + + void lock() + { + if constexpr (Shared) + { + ref.lock_shared(); + } + else + { + ref.lock(); + } + } + }; + } + struct rsx_iomap_table { static constexpr u32 c_lock_stride = 8192; @@ -33,9 +70,9 @@ namespace rsx for (u32 block = addr / c_lock_stride; block <= (end / c_lock_stride); block += Stride) { - auto& mutex_ = rs[block]; + auto mutex_ = iomap_helper::io_lock(rs[block]); - if (IsFullLock ? !mutex_.try_lock() : !mutex_.try_lock_shared()) [[ unlikely ]] + if (!mutex_.try_lock()) [[ unlikely ]] { if (self) { @@ -44,11 +81,11 @@ namespace rsx if (!self || self->id_type() != 0x55u) { - IsFullLock ? mutex_.lock() : mutex_.lock_shared(); + mutex_.lock(); } else { - while (IsFullLock ? !mutex_.try_lock() : !mutex_.try_lock_shared()) + while (!mutex_.try_lock()) { self->cpu_wait({}); } diff --git a/rpcs3/Emu/RSX/Core/RSXReservationLock.hpp b/rpcs3/Emu/RSX/Core/RSXReservationLock.hpp index 2e7df6a9a5..2f83ffa50b 100644 --- a/rpcs3/Emu/RSX/Core/RSXReservationLock.hpp +++ b/rpcs3/Emu/RSX/Core/RSXReservationLock.hpp @@ -44,26 +44,28 @@ namespace rsx // If ranges do not overlap, the first range that is in main memory will be acquired. reservation_lock(u32 dst_addr, u32 dst_length, u32 src_addr, u32 src_length) { - if (g_cfg.core.rsx_accurate_res_access) + if (!g_cfg.core.rsx_accurate_res_access) { - const auto range1 = utils::address_range::start_length(dst_addr, dst_length); - const auto range2 = utils::address_range::start_length(src_addr, src_length); - utils::address_range target_range; + return; + } - if (!range1.overlaps(range2)) [[likely]] - { - target_range = (dst_addr < constants::local_mem_base) ? range1 : range2; - } - else - { - // Very unlikely - target_range = range1.get_min_max(range2); - } + const auto range1 = utils::address_range::start_length(dst_addr, dst_length); + const auto range2 = utils::address_range::start_length(src_addr, src_length); + utils::address_range target_range; - if (target_range.start < constants::local_mem_base) - { - lock_range(target_range.start, target_range.length()); - } + if (!range1.overlaps(range2)) [[likely]] + { + target_range = (dst_addr < constants::local_mem_base) ? range1 : range2; + } + else + { + // Very unlikely + target_range = range1.get_min_max(range2); + } + + if (target_range.start < constants::local_mem_base) + { + lock_range(target_range.start, target_range.length()); } } @@ -71,30 +73,43 @@ namespace rsx template void update_if_enabled(u32 addr, u32 _length, const std::add_pointer_t& lock_release = std::add_pointer_t{}) { - // This check is not perfect but it covers the important cases fast (this check is only an optimization - forcing true disables it) - if (length && (this->addr / rsx_iomap_table::c_lock_stride != addr / rsx_iomap_table::c_lock_stride || (addr % rsx_iomap_table::c_lock_stride + _length) > rsx_iomap_table::c_lock_stride) && _length > 1) + if (!length || _length <= 1) { - if constexpr (!std::is_void_v) - { - // See SPUThread.cpp - lock_release->release(0); - } - - unlock(); - lock_range(addr, _length); + return; } + + // This check is not perfect but it covers the important cases fast (this check is only an optimization - forcing true disables it) + const bool should_update = + (this->addr / rsx_iomap_table::c_lock_stride) != (addr / rsx_iomap_table::c_lock_stride) || // Lock-addr and test-addr have different locks, update + (addr % rsx_iomap_table::c_lock_stride + _length) > rsx_iomap_table::c_lock_stride; // Test range spills beyond our base section + + if (!should_update) + { + return; + } + + if constexpr (!std::is_void_v) + { + // See SPUThread.cpp + lock_release->release(0); + } + + unlock(); + lock_range(addr, _length); } void unlock(bool destructor = false) { - if (length) + if (!length) { - get_current_renderer()->iomap_table.unlock(addr, length); + return; + } - if (!destructor) - { - length = 0; - } + get_current_renderer()->iomap_table.unlock(addr, length); + + if (!destructor) + { + length = 0; } }