From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Aikar Date: Fri, 29 May 2020 20:29:02 -0400 Subject: [PATCH] Synchronize DataPaletteBlock instead of ReentrantLock Mojang has flaws in their logic about chunks being concurrently wrote to. So we constantly see crashes around multiple threads writing. Additionally, java has optimized synchronization so well that its in many times faster than trying to manage read wrote locks for low contention situations. And this is extremely a low contention situation. diff --git a/src/main/java/net/minecraft/world/level/chunk/DataPaletteBlock.java b/src/main/java/net/minecraft/world/level/chunk/DataPaletteBlock.java index 8928157b01bb4f0dfe043732777b33708c23cda7..cc0c5995dc3840ce66ea849849f7c37555d3b5e6 100644 --- a/src/main/java/net/minecraft/world/level/chunk/DataPaletteBlock.java +++ b/src/main/java/net/minecraft/world/level/chunk/DataPaletteBlock.java @@ -32,7 +32,7 @@ public class DataPaletteBlock implements DataPaletteExpandable { private int i; private int getBitsPerObject() { return this.i; } // Paper - OBFHELPER private final ReentrantLock j = new ReentrantLock(); - public void a() { + public void a() { /* // Paper start - disable this - use proper synchronization if (this.j.isLocked() && !this.j.isHeldByCurrentThread()) { String s = (String) Thread.getAllStackTraces().keySet().stream().filter(Objects::nonNull).map((thread) -> { return thread.getName() + ": \n\tat " + (String) Arrays.stream(thread.getStackTrace()).map(Object::toString).collect(Collectors.joining("\n\tat ")); @@ -44,11 +44,11 @@ public class DataPaletteBlock implements DataPaletteExpandable { throw new ReportedException(crashreport); } else { this.j.lock(); - } + } */ // Paper end } public void b() { - this.j.unlock(); + //this.j.unlock(); // Paper - disable this } public DataPaletteBlock(DataPalette datapalette, RegistryBlockID registryblockid, Function function, Function function1, T t0) { @@ -84,7 +84,7 @@ public class DataPaletteBlock implements DataPaletteExpandable { } @Override - public int onResize(int i, T t0) { + public synchronized int onResize(int i, T t0) { // Paper - synchronize this.a(); DataBits databits = this.a; DataPalette datapalette = this.h; @@ -107,18 +107,18 @@ public class DataPaletteBlock implements DataPaletteExpandable { } public T setBlock(int i, int j, int k, T t0) { - this.a(); - T t1 = this.a(b(i, j, k), t0); + //this.a(); // Paper - remove to reduce ops - synchronize handled below + return this.a(b(i, j, k), t0); // Paper - this.b(); - return t1; + //this.b(); // Paper + //return t1; // PAper } public T b(int i, int j, int k, T t0) { return this.a(b(i, j, k), t0); } - protected T a(int i, T t0) { + protected synchronized T a(int i, T t0) { // Paper - synchronize - writes int j = this.h.a(t0); int k = this.a.a(i, j); T t1 = this.h.a(k); @@ -143,7 +143,7 @@ public class DataPaletteBlock implements DataPaletteExpandable { } public void writeDataPaletteBlock(PacketDataSerializer packetDataSerializer) { this.b(packetDataSerializer); } // Paper - OBFHELPER - public void b(PacketDataSerializer packetdataserializer) { + public synchronized void b(PacketDataSerializer packetdataserializer) { // Paper - synchronize this.a(); packetdataserializer.writeByte(this.i); this.h.b(packetdataserializer); @@ -151,7 +151,7 @@ public class DataPaletteBlock implements DataPaletteExpandable { this.b(); } - public void a(NBTTagList nbttaglist, long[] along) { + public synchronized void a(NBTTagList nbttaglist, long[] along) { // Paper - synchronize this.a(); int i = Math.max(4, MathHelper.e(nbttaglist.size())); @@ -184,7 +184,7 @@ public class DataPaletteBlock implements DataPaletteExpandable { this.b(); } - public void a(NBTTagCompound nbttagcompound, String s, String s1) { + public synchronized void a(NBTTagCompound nbttagcompound, String s, String s1) { // Paper - synchronize this.a(); DataPaletteHash datapalettehash = new DataPaletteHash<>(this.d, this.i, this.c, this.e, this.f); T t0 = this.g;