no more core timing

This commit is contained in:
lizzie 2026-05-24 00:52:15 +00:00
parent e0469ad46d
commit bd505c1621

View file

@ -174,7 +174,7 @@ __attribute__((target("waitpkg,mwaitx")))
bool Event::WaitFor(const std::chrono::nanoseconds time) { bool Event::WaitFor(const std::chrono::nanoseconds time) {
auto const start = Common::X64::FencedRDTSC(); auto const start = Common::X64::FencedRDTSC();
auto const& caps = Common::g_cpu_caps; auto const& caps = Common::g_cpu_caps;
auto const ns_ratio = std::max<u64>(1, caps.base_frequency / 1'000); auto const ns_ratio = std::max<u64>(1, caps.tsc_frequency / 1'000);
auto const end = start + time.count() * ns_ratio; auto const end = start + time.count() * ns_ratio;
if (caps.monitorx) { if (caps.monitorx) {
while (true) { while (true) {
@ -205,24 +205,22 @@ bool Event::WaitFor(const std::chrono::nanoseconds time) {
// #UD If CPUID.7.0:ECX.WAITPKG[bit 5]=0. // #UD If CPUID.7.0:ECX.WAITPKG[bit 5]=0.
while (true) { while (true) {
_umonitor(std::addressof(is_set)); _umonitor(std::addressof(is_set));
if (!is_set.load() && !_umwait(1, end)) if (!is_set.load()) {
return false; s32 const cycles = s64(_rdtsc()) - s64(start);
if (!_umwait(1, cycles))
return false;
}
bool expected = true; bool expected = true;
if (is_set.compare_exchange_weak(expected, false, std::memory_order_release)) if (is_set.compare_exchange_weak(expected, false, std::memory_order_release))
return true; return true;
} }
} else { } else {
#ifdef _WIN32 #ifdef _WIN32
while (!is_set.load() && _rdtsc() < end) while (!is_set.load() && end > _rdtsc())
Common::Windows::SleepForOneTick(); Common::Windows::SleepForOneTick();
if (is_set.load()) if (is_set.load())
Reset(); Reset();
return true; return true;
#elif defined(__FreeBSD__)
while (!is_set.load() && end > _rdtsc())
_mm_pause();
bool expected = true;
return is_set.compare_exchange_weak(expected, false, std::memory_order_release);
#else #else
std::unique_lock lk{mutex}; std::unique_lock lk{mutex};
if (!condvar.wait_for(lk, time, [this] { return is_set.load(); })) if (!condvar.wait_for(lk, time, [this] { return is_set.load(); }))
@ -234,22 +232,11 @@ bool Event::WaitFor(const std::chrono::nanoseconds time) {
} }
#else #else
bool Event::WaitFor(const std::chrono::nanoseconds time) { bool Event::WaitFor(const std::chrono::nanoseconds time) {
#ifdef _WIN32
s64 rem = s64(time.count()); //98 years
while (!is_set.load() && rem > 0) {
Common::Windows::SleepForOneTick();
rem = s64(GetGlobalTimeNs().count()) - s64(time.count());
}
if (is_set.load())
Reset();
return true;
#else
std::unique_lock lk{mutex}; std::unique_lock lk{mutex};
if (!condvar.wait_for(lk, time, [this] { return is_set.load(); })) if (!condvar.wait_for(lk, time, [this] { return is_set.load(); }))
return false; return false;
is_set = false; is_set = false;
return true; return true;
#endif
} }
#endif #endif