From bd28955c8e884832f5d86c37b995d75e1e9545ed Mon Sep 17 00:00:00 2001 From: SquidDev Date: Thu, 21 Mar 2019 14:04:13 +0000 Subject: [PATCH] Fix ComputerThread not updating the minimumVirtualRuntime We attempted to simplify this 0bfb7049b077ce8df0f316049069c0cb2e50a862, but that change now means that minimumVirtualRuntime is not updated. As a result, new tasks will have a runtime of 0 when the queue is empty. --- .../core/computer/ComputerThread.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/dan200/computercraft/core/computer/ComputerThread.java b/src/main/java/dan200/computercraft/core/computer/ComputerThread.java index 165530321..16cc6a294 100644 --- a/src/main/java/dan200/computercraft/core/computer/ComputerThread.java +++ b/src/main/java/dan200/computercraft/core/computer/ComputerThread.java @@ -10,6 +10,7 @@ import dan200.computercraft.shared.util.ThreadUtils; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.util.TreeSet; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; @@ -205,7 +206,7 @@ static void queue( @Nonnull ComputerExecutor executor ) if( executor.onComputerQueue ) throw new IllegalStateException( "Cannot queue already queued executor" ); executor.onComputerQueue = true; - updateRuntimes(); + updateRuntimes( null ); // We're not currently on the queue, so update its current execution time to // ensure its at least as high as the minimum. @@ -241,7 +242,7 @@ static void queue( @Nonnull ComputerExecutor executor ) * * This is called before queueing tasks, to ensure that {@link #minimumVirtualRuntime} is up-to-date. */ - private static void updateRuntimes() + private static void updateRuntimes( @Nullable ComputerExecutor current ) { long minRuntime = Long.MAX_VALUE; @@ -249,12 +250,11 @@ private static void updateRuntimes() if( !computerQueue.isEmpty() ) minRuntime = computerQueue.first().virtualRuntime; // Update all the currently executing tasks + long now = System.nanoTime(); + int tasks = 1 + computerQueue.size(); TaskRunner[] currentRunners = runners; if( currentRunners != null ) { - long now = System.nanoTime(); - int tasks = 1 + computerQueue.size(); - for( TaskRunner runner : currentRunners ) { if( runner == null ) continue; @@ -268,6 +268,12 @@ private static void updateRuntimes() } } + // And update the most recently executed one (if set). + if( current != null ) + { + minRuntime = Math.min( minRuntime, current.virtualRuntime += (now - current.vRuntimeStart) / tasks ); + } + if( minRuntime > minimumVirtualRuntime && minRuntime < Long.MAX_VALUE ) { minimumVirtualRuntime = minRuntime; @@ -296,9 +302,7 @@ private static void afterWork( TaskRunner runner, ComputerExecutor executor ) computerLock.lock(); try { - // Update the virtual runtime of this task. - long now = System.nanoTime(); - executor.virtualRuntime += (now - executor.vRuntimeStart) / (1 + computerQueue.size()); + updateRuntimes( executor ); // If we've no more tasks, just return. if( !executor.afterWork() ) return;