From b30c8ea4138d8c23aa5f80d50b1e46314d7e34cd Mon Sep 17 00:00:00 2001 From: James Seibel Date: Sat, 9 Sep 2023 16:28:56 -0500 Subject: [PATCH] Fix crashing due to Chunk Wrapper concurrency --- .../common/wrappers/chunk/ChunkWrapper.java | 19 ++++++------------- coreSubProjects | 2 +- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/chunk/ChunkWrapper.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/chunk/ChunkWrapper.java index a72e06004..684784153 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/chunk/ChunkWrapper.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/chunk/ChunkWrapper.java @@ -42,6 +42,7 @@ import net.minecraft.world.level.chunk.LevelChunk; import net.minecraft.world.level.levelgen.Heightmap; import java.util.*; +import java.util.concurrent.ConcurrentLinkedQueue; #if POST_MC_1_17_1 import net.minecraft.core.QuartPos; @@ -89,9 +90,7 @@ public class ChunkWrapper implements IChunkWrapper * visible when a chunk is re-wrapped later.
* (Also, thread safety done via a reader writer lock) */ - private static ArrayList chunksToUpdateClientLightReadyWriter = new ArrayList<>(300); - /** two arrays are used to prevent concurrency issues and the need to use a read/write lock. */ - private static ArrayList chunksToUpdateClientLightReadyReader = new ArrayList<>(300); + private static final ConcurrentLinkedQueue chunksNeedingClientLightUpdating = new ConcurrentLinkedQueue<>(); @@ -115,7 +114,7 @@ public class ChunkWrapper implements IChunkWrapper this.blockLightArray = new byte[LodUtil.CHUNK_WIDTH * LodUtil.CHUNK_WIDTH * (this.getHeight() + 1)]; this.skyLightArray = new byte[LodUtil.CHUNK_WIDTH * LodUtil.CHUNK_WIDTH * (this.getHeight() + 1)]; - chunksToUpdateClientLightReadyWriter.add(this); + chunksNeedingClientLightUpdating.add(this); } @@ -404,19 +403,13 @@ public class ChunkWrapper implements IChunkWrapper // TODO: Check what to do in 1.18.1 and older #else - // swap the buffers - ArrayList temp = chunksToUpdateClientLightReadyReader; - chunksToUpdateClientLightReadyReader = chunksToUpdateClientLightReadyWriter; - chunksToUpdateClientLightReadyWriter = temp; - // update the chunks client lighting - for (ChunkWrapper chunkWrapper : chunksToUpdateClientLightReadyReader) + ChunkWrapper chunkWrapper = chunksNeedingClientLightUpdating.poll(); + while (chunkWrapper != null) { chunkWrapper.updateIsClientLightingCorrect(); + chunkWrapper = chunksNeedingClientLightUpdating.poll(); } - // remove the processed chunks. - // FIXME sometimes chunks will be processed slightly early and will be removed even if they have valid lighting, this can cause holes in the world when flying around. - chunksToUpdateClientLightReadyReader.clear(); #endif } diff --git a/coreSubProjects b/coreSubProjects index ae8c95250..d73c0b953 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit ae8c95250cfaba6ed4742b1c5ea2750234726be6 +Subproject commit d73c0b9531cac62315befa94f0234f5e60c631f6