From ad3e723437ccb90a10ea8c5ed7775f012f76b775 Mon Sep 17 00:00:00 2001 From: themode Date: Fri, 4 Mar 2022 05:31:50 +0100 Subject: [PATCH] CAS to reset acquiring time --- .../minestom/server/ServerProcessImpl.java | 3 +-- .../minestom/server/thread/Acquirable.java | 22 +++++++------------ 2 files changed, 9 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/minestom/server/ServerProcessImpl.java b/src/main/java/net/minestom/server/ServerProcessImpl.java index d63fcad67..b471db108 100644 --- a/src/main/java/net/minestom/server/ServerProcessImpl.java +++ b/src/main/java/net/minestom/server/ServerProcessImpl.java @@ -312,11 +312,10 @@ final class ServerProcessImpl implements ServerProcess { // Monitoring { - final double acquisitionTimeMs = Acquirable.getAcquiringTime() / 1e6D; + final double acquisitionTimeMs = Acquirable.resetAcquiringTime() / 1e6D; final double tickTimeMs = (System.nanoTime() - nanoTime) / 1e6D; final TickMonitor tickMonitor = new TickMonitor(tickTimeMs, acquisitionTimeMs); EventDispatcher.call(new ServerTickMonitorEvent(tickMonitor)); - Acquirable.resetAcquiringTime(); } } diff --git a/src/main/java/net/minestom/server/thread/Acquirable.java b/src/main/java/net/minestom/server/thread/Acquirable.java index 9526241d0..9bbf0a64d 100644 --- a/src/main/java/net/minestom/server/thread/Acquirable.java +++ b/src/main/java/net/minestom/server/thread/Acquirable.java @@ -33,22 +33,16 @@ public sealed interface Acquirable permits AcquirableImpl { } /** - * Gets the time spent acquiring since last tick. - * - * @return the acquiring time - */ - static long getAcquiringTime() { - return AcquirableImpl.WAIT_COUNTER_NANO.get(); - } - - /** - * Resets {@link #getAcquiringTime()}. - *

- * Mostly for internal use. + * Retrieve and reset acquiring time. */ @ApiStatus.Internal - static void resetAcquiringTime() { - AcquirableImpl.WAIT_COUNTER_NANO.set(0); + static long resetAcquiringTime() { + do { + final long acquiringTime = AcquirableImpl.WAIT_COUNTER_NANO.get(); + if (AcquirableImpl.WAIT_COUNTER_NANO.compareAndSet(acquiringTime, 0)) { + return acquiringTime; + } + } while (true); } /**