1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 07:03:22 +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.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;