From f50e964d4f53e36bb7f65852e8a695da02fc3ece Mon Sep 17 00:00:00 2001 From: Julian Krings Date: Mon, 21 Jul 2025 17:14:45 +0200 Subject: [PATCH] hopefully fix unsafe mantle chunk operations --- .../com/volmit/iris/util/mantle/Mantle.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/com/volmit/iris/util/mantle/Mantle.java b/core/src/main/java/com/volmit/iris/util/mantle/Mantle.java index 6e1e56e54..6851c4cea 100644 --- a/core/src/main/java/com/volmit/iris/util/mantle/Mantle.java +++ b/core/src/main/java/com/volmit/iris/util/mantle/Mantle.java @@ -421,19 +421,19 @@ public class Mantle { throw new RuntimeException("The Mantle is closed"); } - adjustedIdleDuration.set(baseIdleDuration); + double idleDuration = baseIdleDuration; if (loadedRegions.size() > tectonicLimit) { // todo update this correctly and maybe do something when its above a 100% - adjustedIdleDuration.set(Math.max(adjustedIdleDuration.get() - (1000 * (((loadedRegions.size() - tectonicLimit) / (double) tectonicLimit) * 100) * 0.4), 4000)); + idleDuration = Math.max(idleDuration - (1000 * (((loadedRegions.size() - tectonicLimit) / (double) tectonicLimit) * 100) * 0.4), 4000); } + adjustedIdleDuration.set(idleDuration); ioTrim.set(true); try { - double adjustedIdleDuration = this.adjustedIdleDuration.get(); - Iris.debug("Trimming Tectonic Plates older than " + Form.duration(adjustedIdleDuration, 0)); + Iris.debug("Trimming Tectonic Plates older than " + Form.duration(idleDuration, 0)); if (lastUse.isEmpty()) return; - double unloadTime = M.ms() - adjustedIdleDuration; + double unloadTime = M.ms() - idleDuration; for (long id : lastUse.keySet()) { hyperLock.withLong(id, () -> { Long lastUseTime = lastUse.get(id); @@ -462,6 +462,7 @@ public class Mantle { ioTectonicUnload.set(true); try { for (long id : toUnload) { + double unloadTime = M.ms() - adjustedIdleDuration.get(); burst.queue(() -> hyperLock.withLong(id, () -> { TectonicPlate m = loadedRegions.get(id); if (m == null) { @@ -470,6 +471,11 @@ public class Mantle { return; } + var used = lastUse.getOrDefault(id, 0L); + if (!toUnload.contains(id) || used >= unloadTime) { + return; + } + if (m.inUse()) { Iris.debug("Tectonic Plate was added to unload while in use " + C.DARK_GREEN + m.getX() + " " + m.getZ()); lastUse.put(id, M.ms()); @@ -522,9 +528,11 @@ public class Mantle { } } - TectonicPlate p = loadedRegions.get(key(x, z)); + Long key = key(x, z); + TectonicPlate p = loadedRegions.get(key); if (p != null) { + use(key); return p; } @@ -556,12 +564,12 @@ public class Mantle { TectonicPlate p = loadedRegions.get(k); if (p != null) { - lastUse.put(k, M.ms()); + use(k); return CompletableFuture.completedFuture(p); } return ioBurst.completeValue(() -> hyperLock.withResult(x, z, () -> { - lastUse.put(k, M.ms()); + use(k); TectonicPlate region = loadedRegions.get(k); if (region != null) { @@ -602,6 +610,11 @@ public class Mantle { })); } + private void use(Long key) { + lastUse.put(key, M.ms()); + toUnload.remove(key); + } + public void saveAll() { }