1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-09-29 23:40:46 +00:00

Fix ComputerThread not updating the minimumVirtualRuntime

We attempted to simplify this 0bfb7049b0,
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.
This commit is contained in:
SquidDev 2019-03-21 14:04:13 +00:00
parent e46f09a939
commit bd28955c8e

View File

@ -10,6 +10,7 @@ import dan200.computercraft.ComputerCraft;
import dan200.computercraft.shared.util.ThreadUtils; import dan200.computercraft.shared.util.ThreadUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -205,7 +206,7 @@ public class ComputerThread
if( executor.onComputerQueue ) throw new IllegalStateException( "Cannot queue already queued executor" ); if( executor.onComputerQueue ) throw new IllegalStateException( "Cannot queue already queued executor" );
executor.onComputerQueue = true; executor.onComputerQueue = true;
updateRuntimes(); updateRuntimes( null );
// We're not currently on the queue, so update its current execution time to // We're not currently on the queue, so update its current execution time to
// ensure its at least as high as the minimum. // ensure its at least as high as the minimum.
@ -241,7 +242,7 @@ public class ComputerThread
* *
* This is called before queueing tasks, to ensure that {@link #minimumVirtualRuntime} is up-to-date. * 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; long minRuntime = Long.MAX_VALUE;
@ -249,12 +250,11 @@ public class ComputerThread
if( !computerQueue.isEmpty() ) minRuntime = computerQueue.first().virtualRuntime; if( !computerQueue.isEmpty() ) minRuntime = computerQueue.first().virtualRuntime;
// Update all the currently executing tasks // Update all the currently executing tasks
long now = System.nanoTime();
int tasks = 1 + computerQueue.size();
TaskRunner[] currentRunners = runners; TaskRunner[] currentRunners = runners;
if( currentRunners != null ) if( currentRunners != null )
{ {
long now = System.nanoTime();
int tasks = 1 + computerQueue.size();
for( TaskRunner runner : currentRunners ) for( TaskRunner runner : currentRunners )
{ {
if( runner == null ) continue; if( runner == null ) continue;
@ -268,6 +268,12 @@ public class ComputerThread
} }
} }
// 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 ) if( minRuntime > minimumVirtualRuntime && minRuntime < Long.MAX_VALUE )
{ {
minimumVirtualRuntime = minRuntime; minimumVirtualRuntime = minRuntime;
@ -296,9 +302,7 @@ public class ComputerThread
computerLock.lock(); computerLock.lock();
try try
{ {
// Update the virtual runtime of this task. updateRuntimes( executor );
long now = System.nanoTime();
executor.virtualRuntime += (now - executor.vRuntimeStart) / (1 + computerQueue.size());
// If we've no more tasks, just return. // If we've no more tasks, just return.
if( !executor.afterWork() ) return; if( !executor.afterWork() ) return;