aboutsummaryrefslogtreecommitdiffhomepage
path: root/Spigot-Server-Patches/0466-Allow-multiple-callbacks-to-schedule-for-Callback-Ex.patch
blob: d339b0cec659e5392d96e72563409a3be3ee0b6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 21 Apr 2020 03:51:53 -0400
Subject: [PATCH] Allow multiple callbacks to schedule for Callback Executor

ChunkMapDistance polls multiple entries for pendingChunkUpdates

Each of these have the potential to move a chunk in and out of
"Loaded" state, which will result in multiple callbacks being
needed within a single tick of ChunkMapDistance

Use an ArrayDeque to store this Queue

We make sure to also implement a pattern that is recursion safe too.

diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
index 987d4129d7ec9c8be4f6a20376923702e895f231..f2bbb61c7f54fabd9ee087eb4f7ec56aa3df6e99 100644
--- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
+++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
@@ -112,24 +112,32 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
     public final CallbackExecutor callbackExecutor = new CallbackExecutor();
     public static final class CallbackExecutor implements java.util.concurrent.Executor, Runnable {
 
-        private Runnable queued;
+        // Paper start - replace impl with recursive safe multi entry queue
+        // it's possible to schedule multiple tasks currently, so it's vital we change this impl
+        // If we recurse into the executor again, we will append to another queue, ensuring task order consistency
+        private java.util.ArrayDeque<Runnable> queued = new java.util.ArrayDeque<>();
 
         @Override
         public void execute(Runnable runnable) {
-            if (queued != null) {
-                throw new IllegalStateException("Already queued");
+            if (queued == null) {
+                queued = new java.util.ArrayDeque<>();
             }
-            queued = runnable;
+            queued.add(runnable);
         }
 
         @Override
         public void run() {
-            Runnable task = queued;
+            if (queued == null) {
+                return;
+            }
+            java.util.ArrayDeque<Runnable> queue = queued;
             queued = null;
-            if (task != null) {
+            Runnable task;
+            while ((task = queue.pollFirst()) != null) {
                 task.run();
             }
         }
+        // Paper end
     };
     // CraftBukkit end