1
1
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2
2
From: Aikar <aikar@aikar.co>
3
3
Date: Fri, 29 May 2020 20:29:02 -0400
4
- Subject: [PATCH] Synchronize PalettedContainer instead of ReentrantLock
4
+ Subject: [PATCH] Synchronize PalettedContainer instead of
5
+ ThreadingDetector/Semaphore
5
6
6
7
Mojang has flaws in their logic about chunks being concurrently
7
8
wrote to. So we constantly see crashes around multiple threads writing.
8
9
9
10
Additionally, java has optimized synchronization so well that its
10
- in many times faster than trying to manage read wrote locks for low
11
+ in many times faster than trying to manage read write locks for low
11
12
contention situations.
12
13
13
14
And this is extremely a low contention situation.
14
15
15
16
diff --git a/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java b/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java
16
- index 45a969f97b26e328e34f3d576557999c32b954a2..83dc9550f4ed2a138d4ece2765273e555cda994a 100644
17
+ index 45a969f97b26e328e34f3d576557999c32b954a2..277b75940d0424051919889b2d0045f313027234 100644
17
18
--- a/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java
18
19
+++ b/src/main/java/net/minecraft/world/level/chunk/PalettedContainer.java
19
- @@ -35,11 +35,11 @@ public class PalettedContainer<T> implements PaletteResize<T> {
20
- private final ThreadingDetector threadingDetector = new ThreadingDetector("PalettedContainer");
20
+ @@ -32,14 +32,14 @@ public class PalettedContainer<T> implements PaletteResize<T> {
21
+ private final T @org.jetbrains.annotations.Nullable [] presetValues; // Paper - Anti-Xray - Add preset values
22
+ private volatile PalettedContainer.Data<T> data;
23
+ private final PalettedContainer.Strategy strategy;
24
+ - private final ThreadingDetector threadingDetector = new ThreadingDetector("PalettedContainer");
25
+ + // private final ThreadingDetector threadingDetector = new ThreadingDetector("PalettedContainer"); // Paper - unused
21
26
22
27
public void acquire() {
23
28
- this.threadingDetector.checkAndLock();
@@ -30,24 +35,33 @@ index 45a969f97b26e328e34f3d576557999c32b954a2..83dc9550f4ed2a138d4ece2765273e55
30
35
}
31
36
32
37
// Paper start - Anti-Xray - Add preset values
33
- @@ -143 ,7 +143 ,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
38
+ @@ -113 ,7 +113 ,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
34
39
}
35
- // Paper end
36
40
37
- - public T getAndSet(int x, int y, int z, T value) {
38
- + public synchronized T getAndSet(int x, int y, int z, T value) { // Paper - synchronize
39
- this.acquire();
41
+ @Override
42
+ - public int onResize(int newBits, T object) {
43
+ + public synchronized int onResize(int newBits, T object) { // Paper - synchronize
44
+ PalettedContainer.Data<T> data = this.data;
40
45
41
- Object var5;
42
- @@ -166 ,7 +166 ,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
43
- return this.data.palette.valueFor(j );
46
+ // Paper start - Anti-Xray - Add preset values
47
+ @@ -160 ,7 +160 ,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
48
+ return this.getAndSet(this.strategy.getIndex(x, y, z), value );
44
49
}
45
50
46
- - public void set(int x, int y, int z, T value) {
47
- + public synchronized void set(int x, int y, int z, T value) { // Paper - synchronize
48
- this.acquire();
51
+ - private T getAndSet(int index, T value) {
52
+ + private synchronized T getAndSet(int index, T value) { // Paper - synchronize
53
+ int i = this.data.palette.idFor(value);
54
+ int j = this.data.storage.getAndSet(index, i);
55
+ return this.data.palette.valueFor(j);
56
+ @@ -177,7 +177,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
49
57
50
- try {
58
+ }
59
+
60
+ - private void set(int index, T value) {
61
+ + private synchronized void set(int index, T value) { // Paper - synchronize
62
+ int i = this.data.palette.idFor(value);
63
+ this.data.storage.set(index, i);
64
+ }
51
65
@@ -200,7 +200,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
52
66
});
53
67
}
@@ -66,15 +80,6 @@ index 45a969f97b26e328e34f3d576557999c32b954a2..83dc9550f4ed2a138d4ece2765273e55
66
80
this.acquire();
67
81
68
82
try {
69
- @@ -236,7 +236,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
70
-
71
- }
72
-
73
- - private static <T> DataResult<PalettedContainer<T>> read(IdMap<T> idList, PalettedContainer.Strategy provider, PalettedContainer.DiscData<T> serialized, T defaultValue, T @org.jetbrains.annotations.Nullable [] presetValues) { // Paper - Anti-Xray - Add preset values
74
- + private synchronized static <T> DataResult<PalettedContainer<T>> read(IdMap<T> idList, PalettedContainer.Strategy provider, PalettedContainer.DiscData<T> serialized, T defaultValue, T @org.jetbrains.annotations.Nullable [] presetValues) { // Paper - Anti-Xray - Add preset values // Paper - synchronize
75
- List<T> list = serialized.paletteEntries();
76
- int i = provider.size();
77
- int j = provider.calculateBitsForSerialization(idList, list.size());
78
83
@@ -275,7 +275,7 @@ public class PalettedContainer<T> implements PaletteResize<T> {
79
84
return DataResult.success(new PalettedContainer<>(idList, provider, configuration, bitStorage, list, defaultValue, presetValues)); // Paper - Anti-Xray - Add preset values
80
85
}
0 commit comments