More Race-Condition fixes

This commit is contained in:
Frank 2023-05-30 18:21:53 +02:00
parent 457673d721
commit 3b2d8529e2
3 changed files with 19 additions and 61 deletions

View file

@ -60,7 +60,8 @@ public abstract class RegistriesDataProvider implements DataProvider {
cachedOutput, cachedOutput,
registriesProvider, registriesProvider,
dynamicOps, dynamicOps,
registryData registryData,
registries
)) ))
.toArray(CompletableFuture<?>[]::new); .toArray(CompletableFuture<?>[]::new);
@ -71,12 +72,19 @@ public abstract class RegistriesDataProvider implements DataProvider {
); );
} }
private <T> CompletableFuture<?> serializeRegistry( private <T> CompletableFuture<?> serializeRegistry(
CachedOutput cachedOutput, CachedOutput cachedOutput,
HolderLookup.Provider registryAccess, HolderLookup.Provider registryAccess,
DynamicOps<JsonElement> dynamicOps, DynamicOps<JsonElement> dynamicOps,
RegistrySupplier.RegistryInfo<T> registryData RegistrySupplier.RegistryInfo<T> registryData,
RegistrySupplier registries
) { ) {
try {
registries.MAIN_LOCK.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
final List<Holder<T>> elements = registryData.allElements(registryAccess); final List<Holder<T>> elements = registryData.allElements(registryAccess);
final HolderLookup.RegistryLookup<T> registry = registryAccess.lookupOrThrow(registryData.key()); final HolderLookup.RegistryLookup<T> registry = registryAccess.lookupOrThrow(registryData.key());
LOGGER.info("Serializing " LOGGER.info("Serializing "
@ -86,6 +94,7 @@ public abstract class RegistriesDataProvider implements DataProvider {
+ " elements from " + " elements from "
+ registryData.data.key() + registryData.data.key()
); );
registries.MAIN_LOCK.release();
if (!elements.isEmpty()) { if (!elements.isEmpty()) {
PackOutput.PathProvider pathProvider = this.output.createPathProvider( PackOutput.PathProvider pathProvider = this.output.createPathProvider(

View file

@ -7,13 +7,10 @@ import net.minecraft.core.Holder;
import net.minecraft.core.HolderLookup; import net.minecraft.core.HolderLookup;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.core.RegistrySetBuilder; import net.minecraft.core.RegistrySetBuilder;
import net.minecraft.data.DataProvider;
import net.minecraft.data.worldgen.BootstapContext; import net.minecraft.data.worldgen.BootstapContext;
import net.minecraft.resources.RegistryDataLoader; import net.minecraft.resources.RegistryDataLoader;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
@ -23,6 +20,7 @@ import org.jetbrains.annotations.Nullable;
public abstract class RegistrySupplier { public abstract class RegistrySupplier {
private static final int MAX_PERMITS = 2000; private static final int MAX_PERMITS = 2000;
private final Semaphore BOOTSTRAP_LOCK = new Semaphore(MAX_PERMITS); private final Semaphore BOOTSTRAP_LOCK = new Semaphore(MAX_PERMITS);
public final Semaphore MAIN_LOCK = new Semaphore(1);
final List<RegistrySupplier.RegistryInfo<?>> allRegistries; final List<RegistrySupplier.RegistryInfo<?>> allRegistries;
@Nullable List<String> defaultModIDs; @Nullable List<String> defaultModIDs;
@ -60,55 +58,6 @@ public abstract class RegistrySupplier {
BOOTSTRAP_LOCK.release(MAX_PERMITS); BOOTSTRAP_LOCK.release(MAX_PERMITS);
} }
public <T> void addWithLock(
RegistrySetBuilder registryBuilder,
ResourceKey<? extends Registry<T>> resourceKey,
RegistrySetBuilder.RegistryBootstrap<T> registryBootstrap
) {
try {
BOOTSTRAP_LOCK.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
registryBuilder.add(resourceKey, (ctx) -> {
registryBootstrap.run(ctx);
BOOTSTRAP_LOCK.release();
});
}
public <T extends DataProvider> void addProviderWithLock(
FabricDataGenerator.Pack pack,
FabricDataGenerator.Pack.RegistryDependentFactory<T> factory
) {
try {
BOOTSTRAP_LOCK.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
pack.addProvider((output, future) -> {
final T res = factory.create(output, future);
BOOTSTRAP_LOCK.release();
return res;
});
}
public <T extends DataProvider> void addProviderWithLock(
FabricDataGenerator.Pack pack,
FabricDataGenerator.Pack.Factory<T> factory
) {
try {
BOOTSTRAP_LOCK.acquire();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
pack.addProvider((output, future) -> {
final T res = factory.create(output);
BOOTSTRAP_LOCK.release();
return res;
});
}
public class InfoList extends LinkedList<RegistrySupplier.RegistryInfo<?>> { public class InfoList extends LinkedList<RegistrySupplier.RegistryInfo<?>> {
public <T> void add(ResourceKey<? extends Registry<T>> key, Codec<T> elementCodec) { public <T> void add(ResourceKey<? extends Registry<T>> key, Codec<T> elementCodec) {
this.add(new RegistrySupplier.RegistryInfo<T>(key, elementCodec)); this.add(new RegistrySupplier.RegistryInfo<T>(key, elementCodec));

View file

@ -25,17 +25,17 @@ public class BCLibDatagen implements DataGeneratorEntrypoint {
if (BCLib.ADD_TEST_DATA) { if (BCLib.ADD_TEST_DATA) {
TestBiomes.ensureStaticallyLoaded(); TestBiomes.ensureStaticallyLoaded();
BCLRegistrySupplier.INSTANCE.addProviderWithLock(pack, TestWorldgenProvider::new); pack.addProvider(TestWorldgenProvider::new);
BCLRegistrySupplier.INSTANCE.addProviderWithLock(pack, TestBiomes::new); pack.addProvider(TestBiomes::new);
RecipeDataProvider.createTestRecipes(); RecipeDataProvider.createTestRecipes();
} else { } else {
BCLRegistrySupplier.INSTANCE.addProviderWithLock(pack, NullscapeBiomes::new); pack.addProvider(NullscapeBiomes::new);
} }
BCLRegistrySupplier.INSTANCE.addProviderWithLock(pack, RecipeDataProvider::new); pack.addProvider(RecipeDataProvider::new);
BCLRegistrySupplier.INSTANCE.addProviderWithLock(pack, WorldPresetDataProvider::new); pack.addProvider(WorldPresetDataProvider::new);
BCLRegistrySupplier.INSTANCE.addProviderWithLock(pack, BCLibRegistriesDataProvider::new); pack.addProvider(BCLibRegistriesDataProvider::new);
BCLRegistrySupplier.INSTANCE.addProviderWithLock(pack, BCLAdvancementDataProvider::new); pack.addProvider(BCLAdvancementDataProvider::new);
} }