From ba5fb78c5a81a18c9434ff88bf41fd97e2690702 Mon Sep 17 00:00:00 2001 From: psucien Date: Thu, 11 Jul 2024 09:09:37 +0200 Subject: [PATCH] fix for large delays precision + Linux build --- src/common/logging/backend.cpp | 2 +- src/core/libraries/kernel/event_queue.cpp | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 460b7376..0b03c86b 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -170,7 +170,7 @@ public: const char* function, std::string message) { // Propagate important log messages to the profiler if (IsProfilerConnected()) { - const auto& msg_str = std::format("[{}] {}", GetLogClassName(log_class), message); + const auto& msg_str = fmt::format("[{}] {}", GetLogClassName(log_class), message); switch (log_level) { case Level::Warning: TRACE_WARN(msg_str); diff --git a/src/core/libraries/kernel/event_queue.cpp b/src/core/libraries/kernel/event_queue.cpp index 0705fded..18561d6b 100644 --- a/src/core/libraries/kernel/event_queue.cpp +++ b/src/core/libraries/kernel/event_queue.cpp @@ -11,7 +11,7 @@ EqueueInternal::~EqueueInternal() = default; bool EqueueInternal::AddEvent(EqueueEvent& event) { std::scoped_lock lock{m_mutex}; - event.time_added = std::chrono::high_resolution_clock::now(); + event.time_added = std::chrono::steady_clock::now(); const auto& it = std::ranges::find(m_events, event); if (it != m_events.cend()) { @@ -52,16 +52,22 @@ int EqueueInternal::WaitForEvents(SceKernelEvent* ev, int num, u32 micros) { m_cond.wait_for(lock, std::chrono::microseconds(micros), predicate); } + if (HasSmallTimer()) { + if (count > 0) { + const auto time_waited = std::chrono::duration_cast( + std::chrono::steady_clock::now() - m_events[0].time_added) + .count(); + count = WaitForSmallTimer(ev, num, std::max(0l, long(micros - time_waited))); + } + small_timer_event.event.data = 0; + } + if (ev->flags & SceKernelEvent::Flags::OneShot) { for (auto ev_id = 0u; ev_id < count; ++ev_id) { RemoveEvent(ev->ident); } } - if (HasSmallTimer()) { - count = WaitForSmallTimer(ev, num, micros); - } - return count; } @@ -108,7 +114,7 @@ bool EqueueInternal::AddSmallTimer(EqueueEvent& ev) { // can be posted to the queue, based on observations so far. In the opposite case, // the small timer storage and wait logic should be reworked. ASSERT(!HasSmallTimer() || small_timer_event.event.ident == ev.event.ident); - ev.time_added = std::chrono::high_resolution_clock::now(); + ev.time_added = std::chrono::steady_clock::now(); small_timer_event = std::move(ev); return true; } @@ -118,11 +124,11 @@ int EqueueInternal::WaitForSmallTimer(SceKernelEvent* ev, int num, u32 micros) { ASSERT(num == 1); - auto curr_clock = std::chrono::high_resolution_clock::now(); + auto curr_clock = std::chrono::steady_clock::now(); const auto wait_end_us = curr_clock + std::chrono::microseconds{micros}; do { - curr_clock = std::chrono::high_resolution_clock::now(); + curr_clock = std::chrono::steady_clock::now(); { std::unique_lock lock{m_mutex};