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 9321e3a8b2f6f2eaebfa55679f287429d009e834..a270169efccfdd92b2c18af37477b9020746861c 100644 --- a/src/main/java/net/minecraft/util/thread/BlockableEventLoop.java +++ b/src/main/java/net/minecraft/util/thread/BlockableEventLoop.java @@ -1,13 +1,10 @@ package net.minecraft.util.thread; import com.google.common.collect.ImmutableList; -import com.google.common.collect.Queues; import com.mojang.logging.LogUtils; 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.MetricSampler; import net.minecraft.util.profiling.metrics.MetricsRegistry; import net.minecraft.util.profiling.metrics.ProfilerMeasured; import org.slf4j.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 = LogUtils.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 @@ -117,15 +117,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) { @@ -144,8 +149,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) {