From 82e0c1c8ca38b6dc691f90d9b6f051968a4cb24b Mon Sep 17 00:00:00 2001 From: lizzie Date: Sun, 24 May 2026 04:55:42 +0000 Subject: [PATCH] OH FUCK THE TIMINGS --- src/common/thread.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/common/thread.cpp b/src/common/thread.cpp index 19dd8db58e..fb06487f0c 100644 --- a/src/common/thread.cpp +++ b/src/common/thread.cpp @@ -183,15 +183,19 @@ bool Event::WaitFor(const std::chrono::nanoseconds time) { _mm_monitorx(reinterpret_cast(std::addressof(is_set)), 0, 0); if (!is_set.load()) { // RDTSC may be fenced here due to atomic load - u32 const cycles = std::min(std::numeric_limits::max(), s64(end) - s64(start)); - // See here: https://github.com/torvalds/linux/blob/948a64995aca6820abefd17f1a4258f5835c5ad9/arch/x86/lib/delay.c#L93 - // MWAITX accepts a 32-bit input timer which determines the total number of cycles to wait for - // NOT THE TOTAL ABSOLUTE TSC VALUE, it's just a delta - // BIT[1] = use a timer - // Hint = 0: Use C1 state when sleepy (means faster wakeup but less power saving) - _mm_mwaitx(1 << 1, 0u, cycles); - if (!is_set.load()) - return false; + auto const now = _rdtsc(); + if (end > now) { + u32 const cycles = std::min(std::numeric_limits::max(), s64(end) - s64(now)); + // See here: https://github.com/torvalds/linux/blob/948a64995aca6820abefd17f1a4258f5835c5ad9/arch/x86/lib/delay.c#L93 + // MWAITX accepts a 32-bit input timer which determines the total number of cycles to wait for + // NOT THE TOTAL ABSOLUTE TSC VALUE, it's just a delta + // BIT[1] = use a timer + // Hint = 0: Use C1 state when sleepy (means faster wakeup but less power saving) + _mm_mwaitx(1 << 1, 0u, cycles); + if (!is_set.load()) + return false; + } else + return false; //timeout } bool expected = true; if (is_set.compare_exchange_weak(expected, false, std::memory_order_release))