From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers 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 f423f6322b6cbb7b73074f84debc8333ad4e64b3..77e39c713a4d022f05ac71e27abd9799587356f0 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -1081,9 +1081,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 @@ -1093,6 +1090,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(null, () -> 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 7e24854f1e727e5e40108c68933466d0845bdca1..fb185e7f6344f21ed861e56c137ce470e891e766 100644 --- a/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueueTier.java +++ b/src/main/java/org/galemc/gale/executor/queue/BaseTaskQueueTier.java @@ -75,7 +75,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}. *
* This queue may contain tasks of every {@link TaskSpan}. *
@@ -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}. *
* This queue may contain tasks of every {@link TaskSpan}. *
@@ -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. + *
+ * This queue may contain tasks of every {@link TaskSpan}. + */ + public static final SimpleTaskQueue scheduledAsync = SimpleTaskQueue.allSpans("ScheduledAsync"); + }