From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: wangxyper Date: Fri, 27 Jan 2023 19:55:14 +0800 Subject: [PATCH] MikuServer: Code error fix Original license: MIT Original project: https://github.com/MikuMC/MikuServer diff --git a/src/main/java/io/papermc/paper/chunk/system/scheduling/ChunkHolderManager.java b/src/main/java/io/papermc/paper/chunk/system/scheduling/ChunkHolderManager.java index 15eeea40bb7a44470f6f3f0e2473cb451812eec1..c256b6175f8b859883b849c6cd623f680b78be32 100644 --- a/src/main/java/io/papermc/paper/chunk/system/scheduling/ChunkHolderManager.java +++ b/src/main/java/io/papermc/paper/chunk/system/scheduling/ChunkHolderManager.java @@ -2,7 +2,6 @@ package io.papermc.paper.chunk.system.scheduling; import ca.spottedleaf.concurrentutil.executor.standard.PrioritisedExecutor; import ca.spottedleaf.concurrentutil.map.SWMRLong2ObjectHashTable; -import co.aikar.timings.Timing; import com.google.common.collect.ImmutableList; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -29,8 +28,6 @@ import net.minecraft.server.level.TicketType; import net.minecraft.util.SortedArraySet; import net.minecraft.util.Unit; import net.minecraft.world.level.ChunkPos; -import net.minecraft.world.level.chunk.ChunkAccess; -import net.minecraft.world.level.chunk.ChunkStatus; import org.bukkit.plugin.Plugin; import org.slf4j.Logger; import java.io.IOException; diff --git a/src/main/java/io/papermc/paper/util/misc/Delayed26WayDistancePropagator3D.java b/src/main/java/io/papermc/paper/util/misc/Delayed26WayDistancePropagator3D.java index 470402573bc31106d5a63e415b958fb7f9c36aa9..e831738a2988746fe4e065f6ded811a8bdf5dabe 100644 --- a/src/main/java/io/papermc/paper/util/misc/Delayed26WayDistancePropagator3D.java +++ b/src/main/java/io/papermc/paper/util/misc/Delayed26WayDistancePropagator3D.java @@ -94,24 +94,42 @@ public final class Delayed26WayDistancePropagator3D { protected final void addToIncreaseWorkQueue(final long coordinate, final byte level) { final Delayed8WayDistancePropagator2D.WorkQueue queue = this.levelIncreaseWorkQueues[level]; - queue.queuedCoordinates.enqueue(coordinate); - queue.queuedLevels.enqueue(level); + + final long id = queue.lock.writeLock(); + try { + queue.queuedCoordinates.add(coordinate); + queue.queuedLevels.add(level); + }finally { + queue.lock.unlockWrite(id); + } this.levelIncreaseWorkQueueBitset |= (1L << level); } protected final void addToIncreaseWorkQueue(final long coordinate, final byte index, final byte level) { final Delayed8WayDistancePropagator2D.WorkQueue queue = this.levelIncreaseWorkQueues[index]; - queue.queuedCoordinates.enqueue(coordinate); - queue.queuedLevels.enqueue(level); + + final long id = queue.lock.writeLock(); + try { + queue.queuedCoordinates.add(coordinate); + queue.queuedLevels.add(level); + }finally { + queue.lock.unlockWrite(id); + } this.levelIncreaseWorkQueueBitset |= (1L << index); } protected final void addToRemoveWorkQueue(final long coordinate, final byte level) { final Delayed8WayDistancePropagator2D.WorkQueue queue = this.levelRemoveWorkQueues[level]; - queue.queuedCoordinates.enqueue(coordinate); - queue.queuedLevels.enqueue(level); + + final long id = queue.lock.writeLock(); + try { + queue.queuedCoordinates.add(coordinate); + queue.queuedLevels.add(level); + }finally { + queue.lock.unlockWrite(id); + } this.levelRemoveWorkQueueBitset |= (1L << level); } @@ -163,9 +181,20 @@ public final class Delayed26WayDistancePropagator3D { this.levelIncreaseWorkQueueBitset ^= (1L << queueIndex), queueIndex = 63 ^ Long.numberOfLeadingZeros(this.levelIncreaseWorkQueueBitset)) { final Delayed8WayDistancePropagator2D.WorkQueue queue = this.levelIncreaseWorkQueues[queueIndex]; - while (!queue.queuedLevels.isEmpty()) { - final long coordinate = queue.queuedCoordinates.removeFirstLong(); - byte level = queue.queuedLevels.removeFirstByte(); + while (true) { + + long coordinate; + byte level; + final long id = queue.lock.writeLock(); + try { + if (queue.queuedLevels.isEmpty()){ + break; + } + coordinate = queue.queuedCoordinates.removeFirst(); + level = queue.queuedLevels.removeFirst(); + }finally { + queue.lock.unlockWrite(id); + } final boolean neighbourCheck = level < 0; @@ -232,9 +261,19 @@ public final class Delayed26WayDistancePropagator3D { this.levelRemoveWorkQueueBitset ^= (1L << queueIndex), queueIndex = 63 ^ Long.numberOfLeadingZeros(this.levelRemoveWorkQueueBitset)) { final Delayed8WayDistancePropagator2D.WorkQueue queue = this.levelRemoveWorkQueues[queueIndex]; - while (!queue.queuedLevels.isEmpty()) { - final long coordinate = queue.queuedCoordinates.removeFirstLong(); - final byte level = queue.queuedLevels.removeFirstByte(); + while (true) { + long coordinate; + byte level; + final long id = queue.lock.writeLock(); + try { + if (queue.queuedLevels.isEmpty()){ + break; + } + coordinate = queue.queuedCoordinates.removeFirst(); + level = queue.queuedLevels.removeFirst(); + }finally { + queue.lock.unlockWrite(id); + } final byte currentLevel = this.levels.removeIfGreaterOrEqual(coordinate, level); if (currentLevel == 0) { diff --git a/src/main/java/io/papermc/paper/util/misc/Delayed8WayDistancePropagator2D.java b/src/main/java/io/papermc/paper/util/misc/Delayed8WayDistancePropagator2D.java index 808d1449ac44ae86a650932365081fbaf178d141..0fa95d81bafc7fe5c1bede7a0608b54795a78fa0 100644 --- a/src/main/java/io/papermc/paper/util/misc/Delayed8WayDistancePropagator2D.java +++ b/src/main/java/io/papermc/paper/util/misc/Delayed8WayDistancePropagator2D.java @@ -1,12 +1,14 @@ package io.papermc.paper.util.misc; +import io.papermc.paper.util.MCUtil; import it.unimi.dsi.fastutil.HashCommon; -import it.unimi.dsi.fastutil.bytes.ByteArrayFIFOQueue; import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongArrayFIFOQueue; import it.unimi.dsi.fastutil.longs.LongIterator; import it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet; -import io.papermc.paper.util.MCUtil; + +import java.util.Deque; +import java.util.concurrent.ConcurrentLinkedDeque; +import java.util.concurrent.locks.StampedLock; public final class Delayed8WayDistancePropagator2D { @@ -356,24 +358,42 @@ public final class Delayed8WayDistancePropagator2D { protected final void addToIncreaseWorkQueue(final long coordinate, final byte level) { final WorkQueue queue = this.levelIncreaseWorkQueues[level]; - queue.queuedCoordinates.enqueue(coordinate); - queue.queuedLevels.enqueue(level); + + final long id = queue.lock.writeLock(); + try { + queue.queuedCoordinates.add(coordinate); + queue.queuedLevels.add(level); + }finally { + queue.lock.unlockWrite(id); + } this.levelIncreaseWorkQueueBitset |= (1L << level); } protected final void addToIncreaseWorkQueue(final long coordinate, final byte index, final byte level) { final WorkQueue queue = this.levelIncreaseWorkQueues[index]; - queue.queuedCoordinates.enqueue(coordinate); - queue.queuedLevels.enqueue(level); + + final long id = queue.lock.writeLock(); + try { + queue.queuedCoordinates.add(coordinate); + queue.queuedLevels.add(level); + }finally { + queue.lock.unlockWrite(id); + } this.levelIncreaseWorkQueueBitset |= (1L << index); } protected final void addToRemoveWorkQueue(final long coordinate, final byte level) { final WorkQueue queue = this.levelRemoveWorkQueues[level]; - queue.queuedCoordinates.enqueue(coordinate); - queue.queuedLevels.enqueue(level); + + final long id = queue.lock.writeLock(); + try { + queue.queuedCoordinates.add(coordinate); + queue.queuedLevels.add(level); + }finally { + queue.lock.unlockWrite(id); + } this.levelRemoveWorkQueueBitset |= (1L << level); } @@ -425,9 +445,19 @@ public final class Delayed8WayDistancePropagator2D { this.levelIncreaseWorkQueueBitset ^= (1L << queueIndex), queueIndex = 63 ^ Long.numberOfLeadingZeros(this.levelIncreaseWorkQueueBitset)) { final WorkQueue queue = this.levelIncreaseWorkQueues[queueIndex]; - while (!queue.queuedLevels.isEmpty()) { - final long coordinate = queue.queuedCoordinates.removeFirstLong(); - byte level = queue.queuedLevels.removeFirstByte(); + while (true) { + byte level; + long coordinate; + final long id = queue.lock.writeLock(); + try { + if (queue.queuedLevels.isEmpty()){ + break; + } + coordinate = queue.queuedCoordinates.removeFirst(); + level = queue.queuedLevels.removeFirst(); + }finally { + queue.lock.unlockWrite(id); + } final boolean neighbourCheck = level < 0; @@ -491,9 +521,20 @@ public final class Delayed8WayDistancePropagator2D { this.levelRemoveWorkQueueBitset ^= (1L << queueIndex), queueIndex = 63 ^ Long.numberOfLeadingZeros(this.levelRemoveWorkQueueBitset)) { final WorkQueue queue = this.levelRemoveWorkQueues[queueIndex]; - while (!queue.queuedLevels.isEmpty()) { - final long coordinate = queue.queuedCoordinates.removeFirstLong(); - final byte level = queue.queuedLevels.removeFirstByte(); + while (true) { + long coordinate; + byte level; + + final long id = queue.lock.writeLock(); + try { + if (queue.queuedLevels.isEmpty()){ + break; + } + coordinate = queue.queuedCoordinates.removeFirst(); + level = queue.queuedLevels.removeFirst(); + }finally { + queue.lock.unlockWrite(id); + } final byte currentLevel = this.levels.removeIfGreaterOrEqual(coordinate, level); if (currentLevel == 0) { @@ -678,41 +719,9 @@ public final class Delayed8WayDistancePropagator2D { } protected static final class WorkQueue { - - public final NoResizeLongArrayFIFODeque queuedCoordinates = new NoResizeLongArrayFIFODeque(); - public final NoResizeByteArrayFIFODeque queuedLevels = new NoResizeByteArrayFIFODeque(); - + public final Deque queuedCoordinates = new ConcurrentLinkedDeque<>(); + public final Deque queuedLevels = new ConcurrentLinkedDeque<>(); + public final StampedLock lock = new StampedLock(); } - protected static final class NoResizeLongArrayFIFODeque extends LongArrayFIFOQueue { - - /** - * Assumes non-empty. If empty, undefined behaviour. - */ - public long removeFirstLong() { - // copied from superclass - long t = this.array[this.start]; - if (++this.start == this.length) { - this.start = 0; - } - - return t; - } - } - - protected static final class NoResizeByteArrayFIFODeque extends ByteArrayFIFOQueue { - - /** - * Assumes non-empty. If empty, undefined behaviour. - */ - public byte removeFirstByte() { - // copied from superclass - byte t = this.array[this.start]; - if (++this.start == this.length) { - this.start = 0; - } - - return t; - } - } } diff --git a/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java b/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java index 8950b220b9a3512cd4667beb7bdec0e82e07edc6..91fe5898270495aa89586b74ebfd649ef1e0b342 100644 --- a/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java +++ b/src/main/java/net/minecraft/world/entity/ai/village/poi/PoiManager.java @@ -4,8 +4,7 @@ import com.mojang.datafixers.DataFixer; import com.mojang.datafixers.util.Pair; import it.unimi.dsi.fastutil.longs.Long2ByteMap; import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap; -import it.unimi.dsi.fastutil.longs.LongOpenHashSet; -import it.unimi.dsi.fastutil.longs.LongSet; + import java.nio.file.Path; import java.util.Comparator; import java.util.List;