From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 8 Jul 2021 15:03:15 -0400 Subject: [PATCH] Use LinkedBlockingDeque in IAsyncTaskHandler Original code by Titaniumtown, licensed under GNU General Public License v3.0 You can find the original code on https://gitlab.com/Titaniumtown/JettPack diff --git a/src/main/java/net/minecraft/util/thread/BlockableEventLoop.java b/src/main/java/net/minecraft/util/thread/BlockableEventLoop.java index 540aa5e12bb6b44d510c701f2867f541b07ebcc4..62b5f9daff2d45433fc294aec38489a44d03278c 100644 --- a/src/main/java/net/minecraft/util/thread/BlockableEventLoop.java +++ b/src/main/java/net/minecraft/util/thread/BlockableEventLoop.java @@ -1,12 +1,9 @@ package net.minecraft.util.thread; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Queues; import java.util.List; -import java.util.Queue; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; -import java.util.concurrent.locks.LockSupport; import java.util.function.BooleanSupplier; import java.util.function.Supplier; import net.minecraft.util.profiling.metrics.MetricCategory; @@ -15,12 +12,15 @@ import net.minecraft.util.profiling.metrics.MetricsRegistry; import net.minecraft.util.profiling.metrics.ProfilerMeasured; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.concurrent.LinkedBlockingDeque; // Jettpack +import java.util.concurrent.TimeUnit; // Jettpack public abstract class BlockableEventLoop implements ProfilerMeasured, ProcessorHandle, Executor { private final String name; private static final Logger LOGGER = LogManager.getLogger(); - private final Queue pendingRunnables = Queues.newConcurrentLinkedQueue(); + private final LinkedBlockingDeque pendingRunnables = new LinkedBlockingDeque(); // Jettpack private int blockingCount; + private R next = null; // Jettpack protected BlockableEventLoop(String name) { this.name = name; @@ -89,7 +89,7 @@ public abstract class BlockableEventLoop implements Profiler @Override public void tell(R runnable) { this.pendingRunnables.add(runnable); - LockSupport.unpark(this.getRunningThread()); + //LockSupport.unpark(this.getRunningThread()); // Jettpack } @Override @@ -113,15 +113,20 @@ public abstract class BlockableEventLoop implements Profiler } public boolean pollTask() { - R runnable = this.pendingRunnables.peek(); - if (runnable == null) { - return false; - } else if (this.blockingCount == 0 && !true/*this.shouldRun(runnable)*/) { // Patina + // Jettpack start + if (this.next == null && !this.pendingRunnables.isEmpty()) { + this.waitForTasks(); + } + + if (this.next == null) { return false; } else { - this.doRunTask(this.pendingRunnables.remove()); + R r2 = this.next; + this.next = null; + this.doRunTask(r2); return true; } + // Jettpack end } public void managedBlock(BooleanSupplier stopCondition) { @@ -140,8 +145,18 @@ public abstract class BlockableEventLoop implements Profiler } protected void waitForTasks() { - Thread.yield(); - LockSupport.parkNanos("waiting for tasks", 100000L); + // Jettpack start + if (this.next != null) { + throw new IllegalStateException("next != null"); + } + try { + this.next = this.pendingRunnables.poll(100L, TimeUnit.MICROSECONDS); + return; + } + catch (InterruptedException interruptedException) { + return; + } + // Jettpack end } protected void doRunTask(R task) {