Fix Cache Deadlock

This commit is contained in:
Zoe Gidiere 2024-10-12 15:38:51 -06:00
parent 1e5a1e20c2
commit 65d026a130
2 changed files with 40 additions and 32 deletions

View File

@ -24,6 +24,7 @@ public class CacheSampler implements NoiseSampler {
public CacheSampler(NoiseSampler sampler, int dimensions) {
this.sampler = sampler;
if (dimensions == 2) {
this.cache2D = ThreadLocal.withInitial(() -> {
LoadingCache<DoubleSeededVector2Key, Double> cache = Caffeine
.newBuilder()
.executor(CACHE_EXECUTOR)
@ -31,9 +32,11 @@ public class CacheSampler implements NoiseSampler {
.initialCapacity(256)
.maximumSize(256)
.build(this::sampleNoise);
this.cache2D = ThreadLocal.withInitial(() -> Pair.of(new DoubleSeededVector2Key(0, 0, 0), cache).mutable());
return Pair.of(new DoubleSeededVector2Key(0, 0, 0), cache).mutable();
});
this.cache3D = null;
} else {
this.cache3D = ThreadLocal.withInitial(() -> {
LoadingCache<DoubleSeededVector3Key, Double> cache = Caffeine
.newBuilder()
.executor(CACHE_EXECUTOR)
@ -41,7 +44,8 @@ public class CacheSampler implements NoiseSampler {
.initialCapacity(981504)
.maximumSize(981504)
.build(this::sampleNoise);
this.cache3D = ThreadLocal.withInitial(() -> Pair.of(new DoubleSeededVector3Key(0, 0, 0, 0), cache).mutable());
return Pair.of(new DoubleSeededVector3Key(0, 0, 0, 0), cache).mutable();
});
this.cache2D = null;
}
}

View File

@ -33,6 +33,7 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
this.delegate = delegate;
this.res = delegate.resolution();
this.baseCache = ThreadLocal.withInitial(() -> {
LoadingCache<SeededVector2Key, Optional<Biome>> cache = Caffeine
.newBuilder()
.executor(CACHE_EXECUTOR)
@ -40,8 +41,10 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
.initialCapacity(256)
.maximumSize(256)
.build(this::sampleBiome);
this.baseCache = ThreadLocal.withInitial(() -> Pair.of(new SeededVector2Key(0, 0, 0), cache).mutable());
return Pair.of(new SeededVector2Key(0, 0, 0), cache).mutable();
});
this.cache = ThreadLocal.withInitial(() -> {
LoadingCache<SeededVector3Key, Biome> cache3D = Caffeine
.newBuilder()
.executor(CACHE_EXECUTOR)
@ -49,7 +52,8 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
.initialCapacity(981504)
.maximumSize(981504)
.build(this::sampleBiome);
this.cache = ThreadLocal.withInitial(() -> Pair.of(new SeededVector3Key(0, 0, 0, 0), cache3D).mutable());
return Pair.of(new SeededVector3Key(0, 0, 0, 0), cache3D).mutable();
});