From ff051cbe041d27ebbe5517bfbea62f2ce7ebf916 Mon Sep 17 00:00:00 2001 From: e3ndr <33337309+e3ndr@users.noreply.github.com> Date: Wed, 16 Oct 2024 06:47:03 -0500 Subject: [PATCH] fix(app): Threading. saucer_application_thread_safe won't work when called before run(), which causes problems! So we do thread checking in Java and just queue the dispatch. --- .../java/co/casterlabs/saucer/utils/SaucerApp.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/co/casterlabs/saucer/utils/SaucerApp.java b/api/src/main/java/co/casterlabs/saucer/utils/SaucerApp.java index 16469e0..357fa16 100644 --- a/api/src/main/java/co/casterlabs/saucer/utils/SaucerApp.java +++ b/api/src/main/java/co/casterlabs/saucer/utils/SaucerApp.java @@ -17,6 +17,7 @@ public class SaucerApp { private static final _Native N = _SaucerNative.load(_Native.class); private static Pointer $instance; + private static Thread mainThread; @Deprecated @InternalUseOnly @@ -39,11 +40,14 @@ public static void initialize(@NonNull String appId, @NonNull Runnable continueW if ($instance != null) return; // Silently fail if the app has already been initialized. $instance = N.saucer_application_acquire(appId); - new Thread(() -> SaucerApp.dispatch(continueWith)).start(); // Start executing continueWith ASAP. + mainThread = Thread.currentThread(); + new Thread(() -> SaucerApp.dispatch(continueWith)).start(); // Start executing continueWith ASAP. N.saucer_application_run($instance); + N.saucer_application_free($instance); // After run() returns, the app is done. So we free and set null. $instance = null; + mainThread = null; } public static void quit() { @@ -55,11 +59,12 @@ public static void quit() { N.saucer_application_free(old_$instance); }); $instance = null; + mainThread = null; } public static boolean isInMainThread() { check(); - return N.saucer_application_thread_safe($instance); + return Thread.currentThread() == mainThread; } /** @@ -118,8 +123,6 @@ static interface _Native extends Library { void saucer_application_free(Pointer $instance); - boolean saucer_application_thread_safe(Pointer $instance); - /** * @implNote Do not inline this. The JVM needs this to always be accessible * otherwise it will garbage collect and ruin our day.