mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-01 17:01:56 +01:00
Ensure chunks never load async
Force operation to main thread if it occurs async
This commit is contained in:
parent
ff2c1ee38e
commit
e9c7cca230
2 changed files with 80 additions and 1 deletions
|
@ -0,0 +1,46 @@
|
||||||
|
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Aikar <aikar@aikar.co>
|
||||||
|
Date: Fri, 27 May 2016 21:41:26 -0400
|
||||||
|
Subject: [PATCH] Ensure Chunks never ever load async
|
||||||
|
|
||||||
|
Safely pushes the operation to main thread, then back to the posting thread
|
||||||
|
|
||||||
|
diff --git a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java
|
||||||
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||||
|
--- a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java
|
||||||
|
+++ b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOExecutor.java
|
||||||
|
@@ -0,0 +0,0 @@ import com.destroystokyo.paper.PaperConfig;
|
||||||
|
import net.minecraft.server.Chunk;
|
||||||
|
import net.minecraft.server.ChunkProviderServer;
|
||||||
|
import net.minecraft.server.ChunkRegionLoader;
|
||||||
|
+import net.minecraft.server.MCUtil; // Paper
|
||||||
|
import net.minecraft.server.World;
|
||||||
|
import org.bukkit.craftbukkit.util.AsynchronousExecutor;
|
||||||
|
|
||||||
|
@@ -0,0 +0,0 @@ public class ChunkIOExecutor {
|
||||||
|
private static final AsynchronousExecutor<QueuedChunk, Chunk, Runnable, RuntimeException> instance = new AsynchronousExecutor<QueuedChunk, Chunk, Runnable, RuntimeException>(new ChunkIOProvider(), BASE_THREADS);
|
||||||
|
|
||||||
|
public static Chunk syncChunkLoad(World world, ChunkRegionLoader loader, ChunkProviderServer provider, int x, int z) {
|
||||||
|
- return instance.getSkipQueue(new QueuedChunk(x, z, loader, world, provider));
|
||||||
|
+ return MCUtil.ensureMain("Async Chunk Load", () -> instance.getSkipQueue(new QueuedChunk(x, z, loader, world, provider))); // Paper
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void queueChunkLoad(World world, ChunkRegionLoader loader, ChunkProviderServer provider, int x, int z, Runnable runnable) {
|
||||||
|
diff --git a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java
|
||||||
|
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
|
||||||
|
--- a/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java
|
||||||
|
+++ b/src/main/java/org/bukkit/craftbukkit/chunkio/ChunkIOProvider.java
|
||||||
|
@@ -0,0 +0,0 @@ class ChunkIOProvider implements AsynchronousExecutor.CallBackProvider<QueuedChu
|
||||||
|
|
||||||
|
// sync stuff
|
||||||
|
public void callStage2(QueuedChunk queuedChunk, Chunk chunk) throws RuntimeException {
|
||||||
|
- if (chunk == null) {
|
||||||
|
- // If the chunk loading failed just do it synchronously (may generate)
|
||||||
|
+ if (chunk == null || queuedChunk.provider.chunks.containsKey(ChunkCoordIntPair.a(queuedChunk.x, queuedChunk.z))) { // Paper - also call original if it was already loaded
|
||||||
|
+ // If the chunk loading failed (or was already loaded for some reason) just do it synchronously (may generate)
|
||||||
|
queuedChunk.provider.originalGetChunkAt(queuedChunk.x, queuedChunk.z);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
--
|
||||||
|
2.7.4 (Apple Git-66)
|
||||||
|
|
|
@ -15,10 +15,14 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||||
+
|
+
|
||||||
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||||
+import org.bukkit.Location;
|
+import org.bukkit.Location;
|
||||||
|
+import org.bukkit.craftbukkit.util.Waitable;
|
||||||
|
+import org.spigotmc.AsyncCatcher;
|
||||||
+
|
+
|
||||||
+import javax.annotation.Nullable;
|
+import javax.annotation.Nullable;
|
||||||
|
+import java.util.concurrent.ExecutionException;
|
||||||
+import java.util.concurrent.Executor;
|
+import java.util.concurrent.Executor;
|
||||||
+import java.util.concurrent.Executors;
|
+import java.util.concurrent.Executors;
|
||||||
|
+import java.util.function.Supplier;
|
||||||
+import java.util.regex.Pattern;
|
+import java.util.regex.Pattern;
|
||||||
+
|
+
|
||||||
+public final class MCUtil {
|
+public final class MCUtil {
|
||||||
|
@ -38,6 +42,33 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||||
+ }
|
+ }
|
||||||
+
|
+
|
||||||
+ /**
|
+ /**
|
||||||
|
+ * Ensures the target code is running on the main thread
|
||||||
|
+ * @param reason
|
||||||
|
+ * @param run
|
||||||
|
+ * @param <T>
|
||||||
|
+ * @return
|
||||||
|
+ */
|
||||||
|
+ public static <T> T ensureMain(String reason, Supplier<T> run) {
|
||||||
|
+ if (AsyncCatcher.enabled && Thread.currentThread() != MinecraftServer.getServer().primaryThread) {
|
||||||
|
+ new IllegalStateException( "Asynchronous " + reason + "! Blocking thread until it returns ").printStackTrace();
|
||||||
|
+ Waitable<T> wait = new Waitable<T>() {
|
||||||
|
+ @Override
|
||||||
|
+ protected T evaluate() {
|
||||||
|
+ return run.get();
|
||||||
|
+ }
|
||||||
|
+ };
|
||||||
|
+ MinecraftServer.getServer().processQueue.add(wait);
|
||||||
|
+ try {
|
||||||
|
+ return wait.get();
|
||||||
|
+ } catch (InterruptedException | ExecutionException e) {
|
||||||
|
+ e.printStackTrace();
|
||||||
|
+ }
|
||||||
|
+ return null;
|
||||||
|
+ }
|
||||||
|
+ return run.get();
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ /**
|
||||||
+ * Calculates distance between 2 entities
|
+ * Calculates distance between 2 entities
|
||||||
+ * @param e1
|
+ * @param e1
|
||||||
+ * @param e2
|
+ * @param e2
|
||||||
|
@ -205,4 +236,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
|
||||||
private byte type = 0;
|
private byte type = 0;
|
||||||
|
|
||||||
public NBTTagList() {}
|
public NBTTagList() {}
|
||||||
--
|
--
|
||||||
|
2.7.4 (Apple Git-66)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue