From 1417f9b7de037865e5a2f4e3a7325e52a5050747 Mon Sep 17 00:00:00 2001 From: Elad <18193363+elad335@users.noreply.github.com> Date: Sat, 2 Nov 2024 08:27:36 +0200 Subject: [PATCH] utils/atomic.cpp: Fixup utils::get_unique_tsc() It was reading s_min_tsc again inside the atomic operation. Also optimize it a bit. --- rpcs3/util/atomic.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/rpcs3/util/atomic.cpp b/rpcs3/util/atomic.cpp index e154a7a4c9..06b61c7927 100644 --- a/rpcs3/util/atomic.cpp +++ b/rpcs3/util/atomic.cpp @@ -674,19 +674,28 @@ u64 utils::get_unique_tsc() { const u64 stamp0 = utils::get_tsc(); - return s_min_tsc.atomic_op([&](u64& tsc) + if (!s_min_tsc.fetch_op([=](u64& tsc) { - if (stamp0 <= s_min_tsc) + if (stamp0 <= tsc) { // Add 1 if new stamp is too old - return ++tsc; + return false; } else { // Update last tsc with new stamp otherwise - return ((tsc = stamp0)); + tsc = stamp0; + return true; } - }); + }).second) + { + // Add 1 if new stamp is too old + // Avoid doing it in the atomic operaion because, if it gets here it means there is already much cntention + // So break the race (at least on x86) + return s_min_tsc.add_fetch(1); + } + + return stamp0; } atomic_t* root_info::slot_alloc(uptr ptr) noexcept