[1.13] Make Caps, TESR, Entity renderers, and keybinds thread safe to call during parallel init (#5359)

This commit is contained in:
Vincent Lee authored and LexManos committed 2019-01-25 13:14:49 -08:00
1 parent 2c8ab76240
commit fa939a890c
4 files changed
+49 -40

No files matched your search

@@ -28,17 +28,23 @@ import java.lang.annotation.*;
* of 'Capability'
*
* Example:
* @CapabilityInject(IExampleCapability.class)
* private static final Capability<IExampleCapability> TEST_CAP = null;
* <pre>
* {@literal @}CapabilityInject(IExampleCapability.class)
* private static final Capability<IExampleCapability> TEST_CAP = null;
* </pre>
*
* When placed on a METHOD, the method will be invoked once the
* capability is registered. This allows you to have a 'enable features'
* callback. It MUST have one parameter of type 'Capability;
*
* Example:
* @CapabilityInject(IExampleCapability.class)
* private static void capRegistered(Capability<IExampleCapability> cap) {}
* <pre>
* {@literal @}CapabilityInject(IExampleCapability.class)
* private static void capRegistered(Capability<IExampleCapability> cap) {}
* </pre>
*
* <b>Warning</b>: Capability injections are run in the thread that the capablity is registered.
* Due to parallel mod loading, this can potentially be off of the main thread.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@@ -27,6 +27,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;
@@ -50,6 +51,7 @@ public enum CapabilityManager
* Registers a capability to be consumed by others.
* APIs who define the capability should call this.
* To retrieve the Capability instance, use the @CapabilityInject annotation.
* This method is safe to call during parallel mod loading.
*
* @param type The Interface to be registered
* @param storage A default implementation of the storage handler.
@@ -61,20 +63,25 @@ public enum CapabilityManager
Objects.requireNonNull(storage,"Attempted to register a capability with no storage implementation");
Objects.requireNonNull(factory,"Attempted to register a capability with no default implementation factory");
String realName = type.getName().intern();
if (providers.containsKey(realName)) {
LOGGER.error(CAPABILITIES, "Cannot register capability implementation multiple times : {}", realName);
throw new IllegalArgumentException("Cannot register a capability implementation multiple times : "+ realName);
}
Capability<T> cap;
Capability<T> cap = new Capability<>(realName, storage, factory);
providers.put(realName, cap);
synchronized (providers)
{
if (providers.containsKey(realName)) {
LOGGER.error(CAPABILITIES, "Cannot register capability implementation multiple times : {}", realName);
throw new IllegalArgumentException("Cannot register a capability implementation multiple times : "+ realName);
}
cap = new Capability<>(realName, storage, factory);
providers.put(realName, cap);
}
callbacks.getOrDefault(realName, Collections.emptyList()).forEach(func -> func.apply(cap));
}
// INTERNAL
private IdentityHashMap<String, Capability<?>> providers = new IdentityHashMap<>();
private IdentityHashMap<String, List<Function<Capability<?>, Object>>> callbacks = new IdentityHashMap<>();
private final IdentityHashMap<String, Capability<?>> providers = new IdentityHashMap<>();
private volatile IdentityHashMap<String, List<Function<Capability<?>, Object>>> callbacks;
public void injectCapabilities(List<ModFileScanData> data)
{
final List<ModFileScanData.AnnotationData> capabilities = data.stream()
@@ -82,10 +89,12 @@ public enum CapabilityManager
.flatMap(Collection::stream)
.filter(a -> CAP_INJECT.equals(a.getAnnotationType()))
.collect(Collectors.toList());
capabilities.forEach(this::attachCapabilityToMethod);
final IdentityHashMap<String, List<Function<Capability<?>, Object>>> m = new IdentityHashMap<>();
capabilities.forEach(entry -> attachCapabilityToMethod(m, entry));
callbacks = m;
}
private void attachCapabilityToMethod(ModFileScanData.AnnotationData entry)
private static void attachCapabilityToMethod(Map<String, List<Function<Capability<?>, Object>>> cbs, ModFileScanData.AnnotationData entry)
{
final String targetClass = entry.getClassType().getClassName();
final String targetName = entry.getMemberName();
@@ -97,7 +106,7 @@ public enum CapabilityManager
}
final String capabilityName = type.getInternalName().replace('/', '.').intern();
List<Function<Capability<?>, Object>> list = callbacks.computeIfAbsent(capabilityName, k -> new ArrayList<>());
List<Function<Capability<?>, Object>> list = cbs.computeIfAbsent(capabilityName, k -> new ArrayList<>());
if (entry.getMemberName().indexOf('(') > 0)
{