From 80a11e8cbef249b5c2f347acd692419a0b4fd72a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 12 May 2025 13:33:27 +0200 Subject: [PATCH] Add sleep_random function for debugging purposes --- Common/TimeUtil.cpp | 9 +++++++++ Common/TimeUtil.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/Common/TimeUtil.cpp b/Common/TimeUtil.cpp index 1f16898d18..8522109d66 100644 --- a/Common/TimeUtil.cpp +++ b/Common/TimeUtil.cpp @@ -4,6 +4,7 @@ #include #include "Common/TimeUtil.h" +#include "Common/Data/Random/Rng.h" #include "Common/Log.h" #ifdef HAVE_LIBNX @@ -339,3 +340,11 @@ void GetCurrentTimeFormatted(char formattedTime[13]) { // Now tack on the milliseconds snprintf(formattedTime, 11, "%s:%03u", tmp, milliseconds % 1000); } + +// We don't even bother synchronizing this, it's fine if threads stomp a bit. +static GMRng g_sleepRandom; + +void sleep_random(double minSeconds, double maxSeconds) { + const double waitSeconds = minSeconds + (maxSeconds - minSeconds) * g_sleepRandom.F(); + sleep_precise(waitSeconds); +} diff --git a/Common/TimeUtil.h b/Common/TimeUtil.h index 2d33b5b954..f300cbdea5 100644 --- a/Common/TimeUtil.h +++ b/Common/TimeUtil.h @@ -25,6 +25,9 @@ void sleep_ms(int ms, const char *reason); // Precise sleep. Can consume a little bit of CPU on Windows at least. void sleep_precise(double seconds); +// Random sleep, used for debugging. +void sleep_random(double minSeconds, double maxSeconds); + // Yield. Signals that this thread is busy-waiting but wants to allow other hyperthreads to run. void yield();