From 19f414b7755142b4e312e4980224cddaf2d07fd4 Mon Sep 17 00:00:00 2001 From: lizzie Date: Sat, 23 May 2026 10:43:32 +0000 Subject: [PATCH] gido fix --- src/common/CMakeLists.txt | 2 -- src/common/thread.cpp | 28 +++++++++++++++++++---- src/common/x64/cpu_wait.cpp | 44 ------------------------------------- src/common/x64/cpu_wait.h | 13 ----------- src/core/core_timing.cpp | 4 ---- 5 files changed, 24 insertions(+), 67 deletions(-) delete mode 100644 src/common/x64/cpu_wait.cpp delete mode 100644 src/common/x64/cpu_wait.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index c5740bfb83..85bb9e5292 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -182,8 +182,6 @@ if(ARCHITECTURE_x86_64) common PRIVATE x64/cpu_detect.cpp x64/cpu_detect.h - x64/cpu_wait.cpp - x64/cpu_wait.h x64/rdtsc.cpp x64/rdtsc.h x64/xbyak.h) diff --git a/src/common/thread.cpp b/src/common/thread.cpp index 81685315f8..1617f5a4f9 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp @@ -19,29 +19,31 @@ #elif defined(_WIN32) #include #include "common/string_util.h" +#include "common/windows/timer_resolution.h" #else #if defined(__FreeBSD__) #include #include #include +// Compatibility with CPUset +#define cpu_set_t cpuset_t #elif defined(__DragonFly__) || defined(__OpenBSD__) || defined(__Bitrig__) #include #endif #include #include #endif + #ifndef _WIN32 #include #endif +#ifdef ARCHITECTURE_x86_64 #ifdef _MSC_VER #include #else #include #endif - -#ifdef __FreeBSD__ -# define cpu_set_t cpuset_t #endif namespace Common { @@ -151,6 +153,7 @@ void PinCurrentThreadToPerformanceCore(size_t core_id) { } } +#ifdef ARCHITECTURE_x86_64 // On Linux and UNIX systems, a futex would nominally be used to cover the costs // the idea is that it's intuitivelly cheaper to use a direct instruction as opposed to a full futex call // the underlying libc++ implementation uses pthread_cond_timedwait which MAY invoke a futex @@ -167,7 +170,7 @@ __attribute__((target("waitpkg,mwaitx"))) #endif bool Event::WaitFor(const std::chrono::nanoseconds& time) { auto const& caps = Common::GetCPUCaps(); - auto const ns_ratio = std::max(1, caps.max_frequency / 1'000); + auto const ns_ratio = std::max(1, caps.base_frequency / 1'000); auto const target_tsc = Common::X64::FencedRDTSC() + time.count() * ns_ratio; if (caps.monitorx) { while (true) { @@ -209,5 +212,22 @@ bool Event::WaitFor(const std::chrono::nanoseconds& time) { #endif } } +#else +bool Event::WaitFor(const std::chrono::nanoseconds& time) { +#ifdef _WIN32 + while (!IsSet() && _rdtsc() < target_tsc) + Common::Windows::SleepForOneTick(); + if (event.IsSet()) + event.Reset(); + return true; +#else + std::unique_lock lk{mutex}; + if (!condvar.wait_for(lk, time, [this] { return is_set.load(); })) + return false; + is_set = false; + return true; +#endif +} +#endif } // namespace Common diff --git a/src/common/x64/cpu_wait.cpp b/src/common/x64/cpu_wait.cpp deleted file mode 100644 index 96971a1219..0000000000 --- a/src/common/x64/cpu_wait.cpp +++ /dev/null @@ -1,44 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project -// SPDX-License-Identifier: GPL-3.0-or-later - -#include - -#ifdef _MSC_VER -#include -#else -#include -#endif - -#include "common/x64/cpu_detect.h" -#include "common/x64/cpu_wait.h" -#include "common/x64/rdtsc.h" - -namespace Common::X64 { - -#ifdef __clang__ -__attribute__((target("waitpkg,mwaitx"))) -#elif defined(__GNUC__) -#pragma GCC target("waitpkg") -#pragma GCC target("mwaitx") -#endif -void MicroSleep(const CPUCaps& caps, u64 cycles) { - do { - u64 start = FencedRDTSC(); - if (caps.waitpkg) { - constexpr auto RequestC02State = 0U; - _tpause(RequestC02State, start + cycles); - } else if (caps.monitorx) { - constexpr auto EnableWaitTimeFlag = 1U << 1; - constexpr auto RequestC1State = 0U; - // monitor_var should be aligned to a cache line. - alignas(64) static const u64 monitor_var{}; - _mm_monitorx(const_cast(std::addressof(monitor_var)), 0, 0); - _mm_mwaitx(EnableWaitTimeFlag, RequestC1State, cycles); - } else { - std::this_thread::yield(); - } - cycles -= FencedRDTSC() - start; - } while (cycles > 0); -} - -} // namespace Common::X64 diff --git a/src/common/x64/cpu_wait.h b/src/common/x64/cpu_wait.h deleted file mode 100644 index c445ac910a..0000000000 --- a/src/common/x64/cpu_wait.h +++ /dev/null @@ -1,13 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project -// SPDX-License-Identifier: GPL-3.0-or-later - -#pragma once - -#include "common/common_types.h" -#include "common/x64/cpu_detect.h" - -namespace Common::X64 { - -void MicroSleep(const CPUCaps& caps, u64 cycles); - -} // namespace Common::X64 diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index 5c2237ff6f..0c1a755d72 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -14,10 +14,6 @@ #include "common/windows/timer_resolution.h" #endif -#ifdef ARCHITECTURE_x86_64 -#include "common/x64/cpu_wait.h" -#endif - #include "common/settings.h" #include "core/core_timing.h" #include "core/hardware_properties.h"