From a3ca81498e6726d27cfb3267857fe186e0be577a Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Fri, 30 Aug 2024 03:06:36 +0200 Subject: [PATCH] Replace Thread#sleep calls with Thread#onSpinWait --- .../sosengine/base/logging/Logger.java | 7 +++---- .../windowing/classes/api/ApiManagementClass.java | 12 +++++------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java b/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java index 0409b540..4dac9148 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java +++ b/base/src/main/java/de/staropensource/sosengine/base/logging/Logger.java @@ -137,10 +137,9 @@ public final class Logger { flushLogMessages(); // Sleep for whatever has been configured - try { - //noinspection BusyWait - Thread.sleep(EngineConfiguration.getInstance().getLoggerPollingSpeed()); - } catch (Exception ignored) {} + long sleepDuration = System.currentTimeMillis() + EngineConfiguration.getInstance().getLoggerPollingSpeed(); + while (System.currentTimeMillis() < sleepDuration) + Thread.onSpinWait(); } }; diff --git a/windowing/src/main/java/de/staropensource/sosengine/windowing/classes/api/ApiManagementClass.java b/windowing/src/main/java/de/staropensource/sosengine/windowing/classes/api/ApiManagementClass.java index 480940c2..5fa7c815 100644 --- a/windowing/src/main/java/de/staropensource/sosengine/windowing/classes/api/ApiManagementClass.java +++ b/windowing/src/main/java/de/staropensource/sosengine/windowing/classes/api/ApiManagementClass.java @@ -113,13 +113,11 @@ public abstract class ApiManagementClass { splitDeltaTime.add(renderTime + sleepDuration); // Busy wait unless V-Sync is enabled - if (WindowingSubsystemConfiguration.getInstance().getVsyncMode() == VsyncMode.OFF) - try { - //noinspection BusyWait // true, true, but there's no other way to do it - Thread.sleep(sleepDuration); - } catch (InterruptedException exception) { - logger.crash("Rendering loop got interrupted. This is unsupported behaviour", exception); - } + if (WindowingSubsystemConfiguration.getInstance().getVsyncMode() == VsyncMode.OFF && WindowingSubsystemConfiguration.getInstance().getMaximumFramesPerSecond() >= 1) { + sleepDuration += System.currentTimeMillis(); + while (System.currentTimeMillis() < sleepDuration) + Thread.onSpinWait(); + } // Calculate delta time and frame count every second if (System.currentTimeMillis() >= reportDuration + 1000) {