diff options
| author | JosJuice <josjuice@gmail.com> | 2024-04-05 22:40:24 +0200 |
|---|---|---|
| committer | JosJuice <josjuice@gmail.com> | 2024-04-05 22:46:48 +0200 |
| commit | 9ca9d073df98b0bc976eac68c3d708fec57f3389 (patch) | |
| tree | 3900da30ddf8d6cc27e18ee2d13af5bfd6ed372a /Source/Android/app/src/main/java/org | |
| parent | 4312840a4b8cd52ce1beee3a13ba12afe2b59e53 (diff) | |
Android: Fix race condition in displayAlertMsg
It was possible for sAlertMessageLock.notify() to be called before
sAlertMessageLock.wait(), causing Dolphin to deadlock. In particular,
this was guaranteed to happen if displayAlertMsg was called from the UI
thread while the emulation activity is being destroyed, because
runOnUiThread runs the passed-in anonymous function immediately when
called from the UI thread.
By replacing Object.wait/Object.notify with Semaphore.acquire/
Semaphore.release, it no longer matters what order the methods are
called in.
Diffstat (limited to 'Source/Android/app/src/main/java/org')
| -rw-r--r-- | Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java | 21 |
1 files changed, 8 insertions, 13 deletions
diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java index b83a489256..19a5c39d2c 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/NativeLibrary.java @@ -21,6 +21,7 @@ import org.dolphinemu.dolphinemu.utils.Log; import java.lang.ref.WeakReference; import java.util.LinkedHashMap; +import java.util.concurrent.Semaphore; /** * Class which contains methods that interact @@ -28,7 +29,7 @@ import java.util.LinkedHashMap; */ public final class NativeLibrary { - private static final Object sAlertMessageLock = new Object(); + private static final Semaphore sAlertMessageSemaphore = new Semaphore(0); private static boolean sIsShowingAlertMessage = false; private static WeakReference<EmulationActivity> sEmulationActivity = new WeakReference<>(null); @@ -492,15 +493,12 @@ public final class NativeLibrary }); // Wait for the lock to notify that it is complete. - synchronized (sAlertMessageLock) + try + { + sAlertMessageSemaphore.acquire(); + } + catch (InterruptedException ignored) { - try - { - sAlertMessageLock.wait(); - } - catch (Exception ignored) - { - } } if (yesNo) @@ -520,10 +518,7 @@ public final class NativeLibrary public static void NotifyAlertMessageLock() { - synchronized (sAlertMessageLock) - { - sAlertMessageLock.notify(); - } + sAlertMessageSemaphore.release(); } public static void setEmulationActivity(EmulationActivity emulationActivity) |
