9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-23 00:49:31 +00:00

shutdown mob spawning thread on exit

This commit is contained in:
hayanesuru
2025-06-04 00:39:42 +09:00
parent 8e48e51e76
commit 2cd4838b7e
2 changed files with 39 additions and 39 deletions

View File

@@ -22,7 +22,7 @@ and, in my opinion, worth the low risk of minor mob-spawning-related
inconsistencies.
diff --git a/net/minecraft/server/MinecraftServer.java b/net/minecraft/server/MinecraftServer.java
index 4ba85d704ffebae38f7a76a97a182e3674730c6f..a76b67a846b12a7b3d0c41b6ac4833d4f0372531 100644
index 4ba85d704ffebae38f7a76a97a182e3674730c6f..d19e31d3b280325defcaf46e7168eafb3d6587d2 100644
--- a/net/minecraft/server/MinecraftServer.java
+++ b/net/minecraft/server/MinecraftServer.java
@@ -286,6 +286,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
@@ -33,6 +33,21 @@ index 4ba85d704ffebae38f7a76a97a182e3674730c6f..a76b67a846b12a7b3d0c41b6ac4833d4
public static <S extends MinecraftServer> S spin(Function<Thread, S> threadFunction) {
ca.spottedleaf.dataconverter.minecraft.datatypes.MCTypeRegistry.init(); // Paper - rewrite data converter system
@@ -1055,6 +1056,14 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
io.papermc.paper.log.CustomLogManager.forceReset(); // Paper - Reset loggers after shutdown
this.onServerExit();
// Paper end - Improved watchdog support - move final shutdown items here
+ // Leaf start - Async mob spawning
+ if (this.mobSpawnExecutor != null) {
+ try {
+ this.mobSpawnExecutor.kill();
+ } catch (java.lang.InterruptedException ignored) {
+ }
+ }
+ // Leaf end - Async mob spawning
}
public String getLocalIp() {
diff --git a/net/minecraft/server/dedicated/DedicatedServer.java b/net/minecraft/server/dedicated/DedicatedServer.java
index bd7f792c45059f0652e530608ef0c77c5caf7cfa..649403ef1d5d898052412d6d47783769f291b94f 100644
--- a/net/minecraft/server/dedicated/DedicatedServer.java

View File

@@ -1,74 +1,59 @@
package gg.pufferfish.pufferfish.util;
import com.google.common.collect.Queues;
import it.unimi.dsi.fastutil.PriorityQueue;
import it.unimi.dsi.fastutil.PriorityQueues;
import it.unimi.dsi.fastutil.objects.ObjectArrayFIFOQueue;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Queue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.NoSuchElementException;
import java.util.concurrent.locks.LockSupport;
public class AsyncExecutor implements Runnable {
private final Logger LOGGER = LogManager.getLogger("Leaf");
private final Queue<Runnable> jobs = Queues.newArrayDeque();
private final Lock mutex = new ReentrantLock();
private final Condition cond = mutex.newCondition();
private final PriorityQueue<Runnable> jobs = PriorityQueues.synchronize(new ObjectArrayFIFOQueue<>());
private final Thread thread;
private volatile boolean killswitch = false;
public AsyncExecutor(String threadName) {
this.thread = new Thread(this, threadName);
this.thread = Thread.ofPlatform()
.name(threadName)
.priority(Thread.NORM_PRIORITY - 1)
.daemon(false)
.unstarted(this);
}
public void start() {
thread.start();
}
public void kill() {
public void kill() throws InterruptedException {
killswitch = true;
cond.signalAll();
LockSupport.unpark(thread);
thread.join();
}
public void submit(Runnable runnable) {
mutex.lock();
try {
jobs.offer(runnable);
cond.signalAll();
} finally {
mutex.unlock();
}
jobs.enqueue(runnable);
LockSupport.unpark(thread);
}
@Override
public void run() {
while (!killswitch) {
try {
Runnable runnable = takeRunnable();
if (runnable != null) {
runnable.run();
Runnable runnable;
try {
runnable = jobs.dequeue();
} catch (NoSuchElementException e) {
LockSupport.park();
continue;
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
runnable.run();
} catch (Exception e) {
LOGGER.error("Failed to execute async job for thread {}", thread.getName(), e);
}
}
}
private Runnable takeRunnable() throws InterruptedException {
mutex.lock();
try {
while (jobs.isEmpty() && !killswitch) {
cond.await();
}
if (jobs.isEmpty()) return null; // We've set killswitch
return jobs.remove();
} finally {
mutex.unlock();
}
}
}