Skip to content

Commit

Permalink
Try to fix the GUI thread getting "stuck" sometimes when doing fast f…
Browse files Browse the repository at this point in the history
…orward
  • Loading branch information
CasualPokePlayer committed May 12, 2024
1 parent 8a22507 commit 2dffe0a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions GSR.Emu/EmuManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public sealed class EmuManager : IDisposable
private volatile bool _disposing;
private volatile EmuThreadException _emuThreadException;

// hack due to wonky locking in NativeAOT
private volatile bool _wantEmuThreadLock;

private IEmuCore _emuCore;
private IEmuController _emuController;
private bool _emuPaused;
Expand Down Expand Up @@ -160,6 +163,14 @@ private void EmuThreadProc()
}
}
}

if (_wantEmuThreadLock)
{
// if someone else is trying to lock the emu thread, we need to wait a bit after unlocking it
// as it seems (at least under NativeAOT) if it's re-acquired too quickly, other threads have a hard time acquiring it
// (perhaps this is a .NET bug, perhaps it isn't)
Thread.Yield();
}
}
}
catch (Exception e)
Expand Down Expand Up @@ -286,8 +297,10 @@ public void DoFrameStep()

public void LoadRom(EmuLoadArgs loadArgs)
{
_wantEmuThreadLock = true;
lock (_emuThreadLock)
{
_wantEmuThreadLock = false;
UnloadRom();
try
{
Expand Down Expand Up @@ -321,8 +334,10 @@ public void LoadRom(EmuLoadArgs loadArgs)

public void UnloadRom()
{
_wantEmuThreadLock = true;
lock (_emuThreadLock)
{
_wantEmuThreadLock = false;
CheckEmuThreadException();
SetToNullCore();
}
Expand Down Expand Up @@ -474,8 +489,10 @@ public EmuVideoBuffer LoadStatePreview(string statePath)

public void SetSpeedFactor(int speedFactor)
{
_wantEmuThreadLock = true;
lock (_emuThreadLock)
{
_wantEmuThreadLock = false;
_speedFactor = speedFactor;
if (_speedFactor == 1)
{
Expand Down

0 comments on commit 2dffe0a

Please sign in to comment.