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,24 +24,28 @@ public class CacheSampler implements NoiseSampler {
public CacheSampler(NoiseSampler sampler, int dimensions) { public CacheSampler(NoiseSampler sampler, int dimensions) {
this.sampler = sampler; this.sampler = sampler;
if (dimensions == 2) { if (dimensions == 2) {
LoadingCache<DoubleSeededVector2Key, Double> cache = Caffeine this.cache2D = ThreadLocal.withInitial(() -> {
.newBuilder() LoadingCache<DoubleSeededVector2Key, Double> cache = Caffeine
.executor(CACHE_EXECUTOR) .newBuilder()
.scheduler(Scheduler.systemScheduler()) .executor(CACHE_EXECUTOR)
.initialCapacity(256) .scheduler(Scheduler.systemScheduler())
.maximumSize(256) .initialCapacity(256)
.build(this::sampleNoise); .maximumSize(256)
this.cache2D = ThreadLocal.withInitial(() -> Pair.of(new DoubleSeededVector2Key(0, 0, 0), cache).mutable()); .build(this::sampleNoise);
return Pair.of(new DoubleSeededVector2Key(0, 0, 0), cache).mutable();
});
this.cache3D = null; this.cache3D = null;
} else { } else {
LoadingCache<DoubleSeededVector3Key, Double> cache = Caffeine this.cache3D = ThreadLocal.withInitial(() -> {
.newBuilder() LoadingCache<DoubleSeededVector3Key, Double> cache = Caffeine
.executor(CACHE_EXECUTOR) .newBuilder()
.scheduler(Scheduler.systemScheduler()) .executor(CACHE_EXECUTOR)
.initialCapacity(981504) .scheduler(Scheduler.systemScheduler())
.maximumSize(981504) .initialCapacity(981504)
.build(this::sampleNoise); .maximumSize(981504)
this.cache3D = ThreadLocal.withInitial(() -> Pair.of(new DoubleSeededVector3Key(0, 0, 0, 0), cache).mutable()); .build(this::sampleNoise);
return Pair.of(new DoubleSeededVector3Key(0, 0, 0, 0), cache).mutable();
});
this.cache2D = null; this.cache2D = null;
} }
} }

View File

@ -33,23 +33,27 @@ public class CachingBiomeProvider implements BiomeProvider, Handle {
this.delegate = delegate; this.delegate = delegate;
this.res = delegate.resolution(); this.res = delegate.resolution();
LoadingCache<SeededVector2Key, Optional<Biome>> cache = Caffeine this.baseCache = ThreadLocal.withInitial(() -> {
.newBuilder() LoadingCache<SeededVector2Key, Optional<Biome>> cache = Caffeine
.executor(CACHE_EXECUTOR) .newBuilder()
.scheduler(Scheduler.systemScheduler()) .executor(CACHE_EXECUTOR)
.initialCapacity(256) .scheduler(Scheduler.systemScheduler())
.maximumSize(256) .initialCapacity(256)
.build(this::sampleBiome); .maximumSize(256)
this.baseCache = ThreadLocal.withInitial(() -> Pair.of(new SeededVector2Key(0, 0, 0), cache).mutable()); .build(this::sampleBiome);
return Pair.of(new SeededVector2Key(0, 0, 0), cache).mutable();
});
LoadingCache<SeededVector3Key, Biome> cache3D = Caffeine this.cache = ThreadLocal.withInitial(() -> {
.newBuilder() LoadingCache<SeededVector3Key, Biome> cache3D = Caffeine
.executor(CACHE_EXECUTOR) .newBuilder()
.scheduler(Scheduler.systemScheduler()) .executor(CACHE_EXECUTOR)
.initialCapacity(981504) .scheduler(Scheduler.systemScheduler())
.maximumSize(981504) .initialCapacity(981504)
.build(this::sampleBiome); .maximumSize(981504)
this.cache = ThreadLocal.withInitial(() -> Pair.of(new SeededVector3Key(0, 0, 0, 0), cache3D).mutable()); .build(this::sampleBiome);
return Pair.of(new SeededVector3Key(0, 0, 0, 0), cache3D).mutable();
});