decrease locking for mantle generation

This commit is contained in:
Julian Krings
2026-01-05 14:58:09 +01:00
parent dbafe84fa5
commit 01a2999e03
3 changed files with 14 additions and 11 deletions

View File

@@ -24,6 +24,10 @@ public class AtomicBooleanArray implements Serializable {
AA.setVolatile(array, index, newValue);
}
public final boolean getAndSet(int index, boolean newValue) {
return (boolean) AA.getAndSet(array, index, newValue);
}
public final boolean compareAndSet(int index, boolean expectedValue, boolean newValue) {
return (boolean) AA.compareAndSet(array, index, expectedValue, newValue);
}

View File

@@ -34,8 +34,11 @@ interface MatterGenerator {
return@radius
for (c in pair.a) {
if (mc.isFlagged(c.flag))
continue
launch(multicore) {
mc.raiseFlagSuspend(MantleFlag.PLANNED, c.flag) {
mc.raiseFlagSuspend(c.flag) {
c.generateLayer(writer, x, z, context)
}
}

View File

@@ -49,19 +49,15 @@ abstract class FlaggedChunk() {
flags.set(flag.ordinal(), value)
}
suspend fun raiseFlagSuspend(guard: MantleFlag?, flag: MantleFlag, task: suspend () -> Unit) {
suspend fun raiseFlagSuspend(flag: MantleFlag, task: suspend () -> Unit) {
if (isClosed()) throw IllegalStateException("Chunk is closed!")
if (guard != null && isFlagged(guard)) return
if (isFlagged(flag)) return
locks[flag.ordinal()].withLock {
if (flags.compareAndSet(flag.ordinal(), false, true)) {
try {
if (isFlagged(flag)) return
task()
} catch (e: Throwable) {
flags.set(flag.ordinal(), false)
throw e
}
}
if (flags.getAndSet(flag.ordinal(), true))
throw IllegalStateException("Flag ${flag.name()} was already set after task ran!")
}
}