9
0
mirror of https://github.com/Dreeam-qwq/Gale.git synced 2025-12-22 16:29:26 +00:00
Files
Gale/patches/server/0152-Run-async-executor-tasks-on-base-thread-pool.patch
2023-02-05 21:58:32 +01:00

107 lines
6.2 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Martijn Muijsers <martijnmuijsers@live.nl>
Date: Sun, 29 Jan 2023 23:52:19 +0100
Subject: [PATCH] Run async executor tasks on base thread pool
License: AGPL-3.0 (https://www.gnu.org/licenses/agpl-3.0.html)
Gale - https://galemc.org
diff --git a/src/main/java/io/papermc/paper/util/MCUtil.java b/src/main/java/io/papermc/paper/util/MCUtil.java
index 4b8da38db72d7ebc2d498ec3d711d3b30911096c..80f9e70d5c4330e079feccc9a4b1b5957c79ef45 100644
--- a/src/main/java/io/papermc/paper/util/MCUtil.java
+++ b/src/main/java/io/papermc/paper/util/MCUtil.java
@@ -57,14 +57,7 @@ import java.util.function.Consumer;
import java.util.function.Supplier;
public final class MCUtil {
- public static final ThreadPoolExecutor asyncExecutor = new ThreadPoolExecutor(
- 0, 2, 60L, TimeUnit.SECONDS,
- new LinkedBlockingQueue<>(),
- new ThreadFactoryBuilder()
- .setNameFormat("Paper Async Task Handler Thread - %1$d")
- .setUncaughtExceptionHandler(new net.minecraft.DefaultUncaughtExceptionHandlerWithName(MinecraftServer.LOGGER))
- .build()
- );
+ public static final Executor asyncExecutor = BaseTaskQueues.scheduledAsync.yieldingExecutor; // Gale - base thread pool - remove Paper async executor
public static final ThreadPoolExecutor cleanerExecutor = new ThreadPoolExecutor(
1, 1, 0L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(),
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index ac12cabaf15bc3520ff74d09faa48a135c63f23c..0019e5eefc4b638526a75dd3706a54033dd9b811 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1080,9 +1080,6 @@ public abstract class MinecraftServer extends MinecraftServerBlockableEventLoop
MinecraftServer.LOGGER.error("Failed to unlock level {}", this.storageSource.getLevelId(), ioexception1);
}
// Spigot start
- io.papermc.paper.util.MCUtil.asyncExecutor.shutdown(); // Paper
- try { io.papermc.paper.util.MCUtil.asyncExecutor.awaitTermination(30, java.util.concurrent.TimeUnit.SECONDS); // Paper
- } catch (java.lang.InterruptedException ignored) {} // Paper
if (org.spigotmc.SpigotConfig.saveUserCacheOnStopOnly) {
MinecraftServer.LOGGER.info("Saving usercache.json");
this.getProfileCache().save(false); // Paper
@@ -1092,6 +1089,12 @@ public abstract class MinecraftServer extends MinecraftServerBlockableEventLoop
LOGGER.info("Flushing Chunk IO");
io.papermc.paper.chunk.system.io.RegionFileIOThread.close(true); // Paper // Paper - rewrite chunk system
LOGGER.info("Closing Thread Pool");
+ // Gale start - base thread pool - remove Paper async executor
+ long startTime = Util.getMillis();
+ LOGGER.info("Waiting 30 seconds for asynchronous tasks to finish...");
+ serverThread.runTasksUntil(() -> Util.getMillis() - startTime >= 30 || !BaseTaskQueues.scheduledAsync.hasTasks(), null); // Paper
+ LOGGER.info("Shutting down IO executor...");
+ // Gale end - base thread pool - remove Paper async executor
Util.shutdownExecutors(); // Paper
LOGGER.info("Closing Server");
try {
diff --git a/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueueTier.java b/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueueTier.java
index e4a0b3085cb22f25246010c43919129283a3b872..52c413990fd8aaca72e371c3bc8f1f145a172abc 100644
--- a/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueueTier.java
+++ b/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueueTier.java
@@ -73,7 +73,9 @@ public enum BaseTaskQueueTier {
* asynchronously with respect to the {@link ServerThread} and the ticking of the server.
* Execution of
*/
- ASYNC(new AbstractTaskQueue[0], Integer.getInteger("gale.thread.priority.async", 6)),
+ ASYNC(new AbstractTaskQueue[]{
+ BaseTaskQueues.scheduledAsync
+ }, Integer.getInteger("gale.thread.priority.async", 6)),
/**
* A tier for queues that contain tasks with the same considerations as {@link #ASYNC},
* but with a low priority.
diff --git a/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueues.java b/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueues.java
index cf8e2b42ecfc8205af5b105e19975c3e54ffec5f..2295ead9ddcb57be81f8b8bd0731f56c9f7f60b9 100644
--- a/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueues.java
+++ b/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueues.java
@@ -54,7 +54,7 @@ public final class BaseTaskQueues {
/**
* This queue explicitly stores tasks that represent steps or parts of steps in ticking the server and that must be
* executed on the main thread, and as such always have a higher priority in being started than pending tasks in
- * {@link #anyTickScheduledServerThread}.
+ * {@link #anyTickScheduledServerThread} and {@link #scheduledAsync}.
* <br>
* This queue may contain tasks of every {@link TaskSpan}.
* <br>
@@ -79,7 +79,8 @@ public final class BaseTaskQueues {
/**
* This queue explicitly stores tasks that represent steps or parts of steps in ticking the server that do not have
- * to be executed on the main thread (but must be executed on a {@link BaseThread}).
+ * to be executed on the main thread (but must be executed on a {@link BaseThread}), and have a higher priority
+ * in being started than pending tasks in {@link #scheduledAsync}.
* <br>
* This queue may contain tasks of every {@link TaskSpan}.
* <br>
@@ -89,4 +90,12 @@ public final class BaseTaskQueues {
*/
public static final SimpleTaskQueue tickAssist = SimpleTaskQueue.allSpans("TickAssist");
+ /**
+ * This queue stores the tasks scheduled to be executed on any thread, which would usually be stored in various
+ * executors with a specific purpose.
+ * <br>
+ * This queue may contain tasks of every {@link TaskSpan}.
+ */
+ public static final SimpleTaskQueue scheduledAsync = SimpleTaskQueue.allSpans("ScheduledAsync");
+
}