-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfe3e4a
commit 2e0b78f
Showing
7 changed files
with
75 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
common/src/main/java/mod/chloeprime/aaaparticles/api/client/effekseer/SafeFinalized.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package mod.chloeprime.aaaparticles.api.client.effekseer; | ||
|
||
import net.minecraft.client.Minecraft; | ||
|
||
import java.io.Closeable; | ||
import java.util.Collections; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* 避免GC线程回收Effekseer对象导致nv驱动报错 | ||
*/ | ||
abstract class SafeFinalized<T> implements Closeable { | ||
private static final Set<Object> KEEPER = Collections.newSetFromMap(new ConcurrentHashMap<>()); | ||
private final AtomicReference<T> kept; | ||
private final Consumer<T> closer; | ||
|
||
protected SafeFinalized(T kept, Consumer<T> closer) { | ||
this.kept = new AtomicReference<>(kept); | ||
this.closer = closer; | ||
KEEPER.add(kept); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("deprecation") | ||
protected void finalize() throws Throwable { | ||
super.finalize(); | ||
var kept = this.kept.get(); | ||
if (kept != null) { | ||
Minecraft.getInstance().tell(this::close); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
T removed = this.kept.getAndSet(null); | ||
if (removed == null) { | ||
return; | ||
} | ||
try { | ||
closer.accept(removed); | ||
} finally { | ||
KEEPER.remove(removed); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters