mirror of
https://github.com/GTNewHorizons/Angelica.git
synced 2026-11-21 21:02:15 +00:00
Opt in thread saftey (#254)
* Evolution of thread safe ISBRH * Add API package Co-authored-by: makamys <[email protected]>
This commit is contained in:
1 parent
a0d782a1a5
commit
6a7c2520d0
6 files changed
+97
-19
No files matched your search
@@ -0,0 +1,35 @@
|
||||
package com.gtnewhorizons.angelica.api;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.TYPE;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* Marks an ISBRH as thread-safe, allowing Angelica to run it off-thread.
|
||||
*
|
||||
* <p>Note: Any tessellator access <i>must</i> be done within the methods of the ISBRH and not cached in the class.</p>
|
||||
*/
|
||||
@Documented
|
||||
@Target({ TYPE })
|
||||
@Retention(RUNTIME)
|
||||
public @interface ThreadSafeISBRH {
|
||||
/**
|
||||
* Specifies whether Angelica should create a thread local instance of the class for each thread.
|
||||
* <ul>
|
||||
* <li>If this is set to <tt>true</tt>, a new instance will be created using the default constructor on each thread.</li>
|
||||
* <ul>
|
||||
* <li>Note: If your renderer requires arguments to the constructor, do <i>not</i> use this annotation;
|
||||
* instead use the {@link ThreadSafeISBRHFactory} interface and implement the {@link ThreadSafeISBRHFactory#newInstance} method. </li>
|
||||
* </ul>
|
||||
* <li>If this is set to <tt>false</tt>, the main thread instance will be used on all threads.
|
||||
* Use this option if your renderer class has no state, and can be safely used from multiple threads as is.</li>
|
||||
* <ul>
|
||||
* <li>No state/non final variables should be stored in the ISBRH as it is not guaranteed to be thread safe.</li>
|
||||
* </ul>
|
||||
* </ul>
|
||||
*/
|
||||
boolean perThread();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.gtnewhorizons.angelica.api;
|
||||
|
||||
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||
|
||||
/**
|
||||
* Marks an ISBRH as thread-safe using a thread local instance, allowing Angelica to run it off-thread.
|
||||
* Use this when your ISBRH uses a non-default constructor.
|
||||
* <p>To avoid a hard dependency on Angelica, you will likely want to mark this as an optional interface using:</p>
|
||||
* <pre>
|
||||
* @Optional.Interface(modid = "angelica", iface = "com.gtnewhorizons.angelica.api.ThreadSafeISBRHFactory")
|
||||
* </pre>
|
||||
*
|
||||
* <p><b>Notes</b></p>
|
||||
* <ul>
|
||||
* <li>Any tessellator access <i>must</i> be done within the methods of the ISBRH and not cached in the class.</li>
|
||||
* <li>If your class uses a default constructor or is thread-safe by default, consider using the simpler {@link ThreadSafeISBRH}
|
||||
* annotation instead.
|
||||
* </ul>
|
||||
*/
|
||||
public interface ThreadSafeISBRHFactory extends ISimpleBlockRenderingHandler {
|
||||
ThreadSafeISBRHFactory newInstance();
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.gtnewhorizons.angelica.interfaces;
|
||||
|
||||
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||
|
||||
public interface IThreadSafeISBRH extends ISimpleBlockRenderingHandler {
|
||||
IThreadSafeISBRH getThreadLocal();
|
||||
|
||||
}
|
||||
+27
-5
@@ -1,21 +1,26 @@
|
||||
package com.gtnewhorizons.angelica.mixins.early.sodium;
|
||||
|
||||
import com.gtnewhorizons.angelica.interfaces.IThreadSafeISBRH;
|
||||
import com.gtnewhorizons.angelica.api.ThreadSafeISBRH;
|
||||
import com.gtnewhorizons.angelica.api.ThreadSafeISBRHFactory;
|
||||
import com.gtnewhorizons.angelica.glsm.GLStateManager;
|
||||
import com.gtnewhorizons.angelica.mixins.interfaces.IRenderingRegistryExt;
|
||||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
|
||||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
|
||||
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Shadow;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Map;
|
||||
|
||||
@Mixin(value = RenderingRegistry.class, remap = false)
|
||||
public class MixinRenderingRegistry implements IRenderingRegistryExt {
|
||||
@Shadow private Map<Integer, ISimpleBlockRenderingHandler> blockRenderers;
|
||||
|
||||
private ThreadLocal<Map<Class<?>, Object>> THREAD_LOCAL_MAP = ThreadLocal.withInitial(Reference2ObjectOpenHashMap::new);
|
||||
@Override
|
||||
public ISimpleBlockRenderingHandler getISBRH(int modelId) {
|
||||
return this.blockRenderers.get(modelId);
|
||||
@@ -23,10 +28,27 @@ public class MixinRenderingRegistry implements IRenderingRegistryExt {
|
||||
|
||||
@WrapOperation(method = { "renderWorldBlock", "renderInventoryBlock", "renderItemAsFull3DBlock" }, at = @At(value="INVOKE", target="Ljava/util/Map;get(Ljava/lang/Object;)Ljava/lang/Object;"))
|
||||
private Object getWrapped(Map<Integer, ISimpleBlockRenderingHandler> instance, Object modelId, Operation<ISimpleBlockRenderingHandler> original) {
|
||||
final ISimpleBlockRenderingHandler res = original.call(instance, modelId);
|
||||
if(res instanceof IThreadSafeISBRH isbhr) {
|
||||
return isbhr.getThreadLocal();
|
||||
// TODO: Move this to BlockRenderer
|
||||
|
||||
// Get the main thread handler
|
||||
final ISimpleBlockRenderingHandler mainThreadHandler = original.call(instance, modelId);
|
||||
if(Thread.currentThread() != GLStateManager.getMainThread()) {
|
||||
ThreadSafeISBRH annotation = mainThreadHandler.getClass().getAnnotation(ThreadSafeISBRH.class);
|
||||
if (annotation != null && annotation.perThread()) {
|
||||
return THREAD_LOCAL_MAP.get().computeIfAbsent(mainThreadHandler.getClass(), k -> {
|
||||
try {
|
||||
// Won't work with non-default constructors, use ThreadSafeISBRHFactory instead
|
||||
return mainThreadHandler.getClass().getDeclaredConstructor().newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch(InvocationTargetException e) {
|
||||
throw new RuntimeException(e.getCause());
|
||||
}
|
||||
});
|
||||
} else if (mainThreadHandler.getClass().isInstance(ThreadSafeISBRHFactory.class)) {
|
||||
return THREAD_LOCAL_MAP.get().computeIfAbsent(mainThreadHandler.getClass(), k -> ((ThreadSafeISBRHFactory) mainThreadHandler).newInstance());
|
||||
}
|
||||
}
|
||||
return res;
|
||||
return mainThreadHandler;
|
||||
}
|
||||
}
|
||||
+12
-5
@@ -1,10 +1,10 @@
|
||||
package com.gtnewhorizons.angelica.rendering;
|
||||
|
||||
import com.gtnewhorizons.angelica.interfaces.IThreadSafeISBRH;
|
||||
import com.gtnewhorizons.angelica.api.ThreadSafeISBRH;
|
||||
import com.gtnewhorizons.angelica.api.ThreadSafeISBRHFactory;
|
||||
import com.gtnewhorizons.angelica.mixins.interfaces.IRenderingRegistryExt;
|
||||
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
|
||||
import cpw.mods.fml.client.registry.RenderingRegistry;
|
||||
import cpw.mods.fml.common.Loader;
|
||||
import cpw.mods.fml.common.versioning.ComparableVersion;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanMap;
|
||||
import it.unimi.dsi.fastutil.objects.Reference2BooleanOpenHashMap;
|
||||
import net.minecraft.block.Block;
|
||||
@@ -39,8 +39,15 @@ public class AngelicaBlockSafetyRegistry {
|
||||
}
|
||||
|
||||
private static boolean populateCanRenderOffThread(Block block, Reference2BooleanMap<Block> map) {
|
||||
@SuppressWarnings("deprecation")
|
||||
final boolean canBeOffThread = map == ISBRH_SAFETY_MAP ? ((IRenderingRegistryExt)RenderingRegistry.instance()).getISBRH(block.getRenderType()) instanceof IThreadSafeISBRH : !(block.getClass().getName().startsWith("gregtech."));
|
||||
final boolean canBeOffThread;
|
||||
if(map == ISBRH_SAFETY_MAP) {
|
||||
@SuppressWarnings("deprecation")
|
||||
final ISimpleBlockRenderingHandler isbrh = ((IRenderingRegistryExt)RenderingRegistry.instance()).getISBRH(block.getRenderType());
|
||||
canBeOffThread = isbrh.getClass().isAnnotationPresent(ThreadSafeISBRH.class)
|
||||
|| isbrh instanceof ThreadSafeISBRHFactory;
|
||||
} else {
|
||||
canBeOffThread = !(block.getClass().getName().startsWith("gregtech."));
|
||||
}
|
||||
|
||||
final long stamp = LOCK.writeLock();
|
||||
|
||||
|
||||
Reference in new issue
Block a user