fix possible async access issue

This commit is contained in:
dags- 2020-03-01 21:25:26 +00:00
parent ee111a4005
commit 3b866e54a6
6 changed files with 16 additions and 10 deletions

View File

@ -31,7 +31,6 @@ import com.terraforged.core.world.heightmap.RegionExtent;
import me.dags.noise.util.NoiseUtil; import me.dags.noise.util.NoiseUtil;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class RegionCache implements RegionExtent { public class RegionCache implements RegionExtent {
@ -39,13 +38,12 @@ public class RegionCache implements RegionExtent {
private final boolean queuing; private final boolean queuing;
private final RegionGenerator renderer; private final RegionGenerator renderer;
private final Cache<Long, CompletableFuture<Region>> cache; private final Cache<Long, CompletableFuture<Region>> cache;
private final ThreadLocal<Region> cachedRegion = new ThreadLocal<>();
private Region cachedRegion = null;
public RegionCache(boolean queueNeighbours, RegionGenerator renderer) { public RegionCache(boolean queueNeighbours, RegionGenerator renderer) {
this.renderer = renderer; this.renderer = renderer;
this.queuing = queueNeighbours; this.queuing = queueNeighbours;
this.cache = new Cache<>(180, 60, TimeUnit.SECONDS, () -> new ConcurrentHashMap<>()); this.cache = Cache.concurrent(180, 60, TimeUnit.SECONDS);
} }
@Override @Override
@ -74,25 +72,28 @@ public class RegionCache implements RegionExtent {
@Override @Override
public Region getRegion(int regionX, int regionZ) { public Region getRegion(int regionX, int regionZ) {
if (cachedRegion != null && regionX == cachedRegion.getRegionX() && regionZ == cachedRegion.getRegionZ()) { Region cached = cachedRegion.get();
return cachedRegion; if (cached != null && regionX == cached.getRegionX() && regionZ == cached.getRegionZ()) {
return cached;
} }
long id = NoiseUtil.seed(regionX, regionZ); long id = NoiseUtil.seed(regionX, regionZ);
CompletableFuture<Region> futureRegion = cache.get(id); CompletableFuture<Region> futureRegion = cache.get(id);
if (futureRegion == null) { if (futureRegion == null) {
cachedRegion = renderer.generateRegion(regionX, regionZ); cached = renderer.generateRegion(regionX, regionZ);
cache.put(id, CompletableFuture.completedFuture(cachedRegion)); cache.put(id, CompletableFuture.completedFuture(cached));
} else { } else {
cachedRegion = futureRegion.join(); cached = futureRegion.join();
} }
if (queuing) { if (queuing) {
queueNeighbours(regionX, regionZ); queueNeighbours(regionX, regionZ);
} }
return cachedRegion; cachedRegion.set(cached);
return cached;
} }
private void queueNeighbours(int regionX, int regionZ) { private void queueNeighbours(int regionX, int regionZ) {

View File

@ -27,6 +27,7 @@ package com.terraforged.core.util;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -94,4 +95,8 @@ public class Cache<K, V> {
return value; return value;
} }
} }
public static <K, V> Cache<K, V> concurrent(long lifespan, long interval, TimeUnit unit) {
return new Cache<>(lifespan, interval, unit, () -> new ConcurrentHashMap<>(100));
}
} }