Make an Iris world's running world and structure host once

running() and the structure host were created lazily without
synchronisation, so two first callers got two instances. Two running
worlds mean two SavedWorldStates and one of them is never saved. Both are
now final fields built with the world. A concurrency test reproduces the
split on the old code.
This commit is contained in:
piexel committed 2026-11-06 12:46:29 +01:00
1 parent 4f772e2b48
commit 96532f9a22
2 files changed
+81 -22

No files matched your search

@@ -40,9 +40,9 @@ public final class IrisPackWorld implements PackWorld {
private final IrisPackResources resources;
private final WorldStates states;
private final ModuleLog log;
private volatile IrisRunningWorld running;
private final IrisRunningWorld running;
private final Placements placements;
private volatile IrisStructureHost structureHost;
private final IrisStructureHost structureHost;
IrisPackWorld(Engine engine) {
this.engine = engine;
@@ -50,6 +50,8 @@ public final class IrisPackWorld implements PackWorld {
this.states = new WorldStates();
this.log = IrisApiLogger.hostLog();
this.placements = new IrisPlacements(engine.getData(), engine, () -> this);
this.running = new IrisRunningWorld(engine, log);
this.structureHost = new IrisStructureHost(engine, log);
}
@Override
@@ -85,20 +87,12 @@ public final class IrisPackWorld implements PackWorld {
@Override
public Optional<RunningWorld> running() {
IrisRunningWorld known = running;
if (known == null) {
known = new IrisRunningWorld(engine, log);
running = known;
}
return Optional.of(known);
return Optional.of(running);
}
/** Writes what modules saved with this world, if any asked for it. */
void saveState() {
IrisRunningWorld known = running;
if (known != null) {
known.saveState();
}
running.saveState();
}
@Override
@@ -122,20 +116,11 @@ public final class IrisPackWorld implements PackWorld {
return engine;
}
if (type == StructureHost.class) {
return structures();
return structureHost;
}
return null;
}
private StructureHost structures() {
IrisStructureHost host = structureHost;
if (host == null) {
host = new IrisStructureHost(engine, log);
structureHost = host;
}
return host;
}
/** Whether the pack's resources were reloaded since this world was made, so it generates with an old snapshot. */
boolean isStale() {
return engine.getData().getPackResources() != resources;
@@ -0,0 +1,74 @@
package art.arcane.iris.core.modules;
import art.arcane.iris.builtin.structure.StructureHost;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.engine.framework.Engine;
import com.volmit.world.PackWorld;
import org.junit.Test;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.function.Function;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** Threads asking an Iris world for its running world or structure host at once all get the same instance. */
public class IrisPackWorldConcurrencyTest {
private static final int THREADS = 16;
private static final int ROUNDS = 200;
@Test
public void concurrentFirstCallersShareOneRunningWorld() throws Exception {
assertEveryCallerGetsOneInstance(world -> world.running().orElseThrow());
}
@Test
public void concurrentFirstCallersShareOneStructureHost() throws Exception {
assertEveryCallerGetsOneInstance(world -> world.extension(StructureHost.class).orElseThrow());
}
private static void assertEveryCallerGetsOneInstance(Function<PackWorld, Object> ask) throws Exception {
ExecutorService pool = Executors.newFixedThreadPool(THREADS);
try {
for (int round = 0; round < ROUNDS; round++) {
PackWorld world = new IrisPackWorld(engine());
assertEquals(1, distinctAnswers(pool, world, ask));
}
} finally {
pool.shutdownNow();
}
}
private static int distinctAnswers(ExecutorService pool, PackWorld world, Function<PackWorld, Object> ask) throws Exception {
CountDownLatch go = new CountDownLatch(1);
List<Future<Object>> answers = new ArrayList<>();
for (int caller = 0; caller < THREADS; caller++) {
Callable<Object> firstCall = () -> {
go.await();
return ask.apply(world);
};
answers.add(pool.submit(firstCall));
}
go.countDown();
Set<Object> distinct = java.util.Collections.newSetFromMap(new IdentityHashMap<>());
for (Future<Object> answer : answers) {
distinct.add(answer.get());
}
return distinct.size();
}
private static Engine engine() {
Engine engine = mock(Engine.class);
when(engine.getData()).thenReturn(mock(IrisData.class));
return engine;
}
}