Skip to content

Commit 48409fc

Browse files
committed
Fix concurrency issue in CoFH FlagManager
Related: #173
1 parent 373a10f commit 48409fc

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.embeddedt.modernfix.forge.mixin.bugfix.cofh_core_crash;
2+
3+
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
4+
import org.embeddedt.modernfix.annotation.RequiresMod;
5+
import org.spongepowered.asm.mixin.*;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Coerce;
8+
import org.spongepowered.asm.mixin.injection.Redirect;
9+
10+
import java.util.function.BooleanSupplier;
11+
12+
/**
13+
* Fix getOrCreateFlag accessing the FLAGS map without synchronization by wrapping all calls to it
14+
* in a synchronized block.
15+
*/
16+
@Pseudo
17+
@Mixin(targets = { "cofh/lib/util/flags/FlagManager" }, remap = false)
18+
@RequiresMod("cofh_core")
19+
public class FlagManagerMixin {
20+
@Shadow @Final
21+
private static Object2ObjectOpenHashMap<String, BooleanSupplier> FLAGS;
22+
23+
@Shadow
24+
private BooleanSupplier getOrCreateFlag(String flag) {
25+
throw new AssertionError();
26+
}
27+
28+
@Redirect(method = "*", at = @At(value = "INVOKE", target = "getOrCreateFlag"), require = 0)
29+
private BooleanSupplier getFlag(@Coerce Object flagHandler, String flag) {
30+
if(flagHandler != this)
31+
throw new AssertionError("Redirect targeted bad getOrCreateFlag invocation");
32+
synchronized (FLAGS) {
33+
return this.getOrCreateFlag(flag);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)