Debugger: Fixed deadlocks when switching games/etc with debugger tools opened

This commit is contained in:
Sour 2019-10-22 19:53:18 -04:00
parent 1a7be287f7
commit fa98a0ff4e
8 changed files with 27 additions and 35 deletions

View file

@ -95,6 +95,7 @@ void Console::Run()
}
auto emulationLock = _emulationLock.AcquireSafe();
auto lock = _runLock.AcquireSafe();
DebugStats stats(this);
Timer lastFrameTimer;
@ -112,7 +113,6 @@ void Console::Run()
_memoryManager->IncMasterClockStartup();
_controlManager->UpdateInputState();
auto lock = _runLock.AcquireSafe();
while(!_stopFlag) {
_cpu->Exec();
@ -202,6 +202,11 @@ void Console::Stop(bool sendNotification)
_emulationLock.WaitForRelease();
if(_emuThread) {
_emuThread->join();
_emuThread.release();
}
if(_cart && !_settings->GetPreferences().DisableGameSelectionScreen) {
RomInfo romInfo = _cart->GetRomInfo();
_saveStateManager->SaveRecentGame(romInfo.RomFile.GetFileName(), romInfo.RomFile, romInfo.PatchFile);
@ -300,11 +305,11 @@ bool Console::LoadRom(VirtualFile romFile, VirtualFile patchFile, bool stopRom)
shared_ptr<BaseCartridge> cart = BaseCartridge::CreateCartridge(this, romFile, patchFile);
if(cart) {
_cheatManager->ClearCheats(false);
if(stopRom) {
Stop(false);
}
_cheatManager->ClearCheats(false);
_cart = cart;
_batteryManager->Initialize(FolderUtilities::GetFilename(romFile.GetFileName(), false));
@ -355,6 +360,12 @@ bool Console::LoadRom(VirtualFile romFile, VirtualFile patchFile, bool stopRom)
string messageTitle = MessageManager::Localize("GameLoaded") + " (" + modelName + ")";
MessageManager::DisplayMessage(messageTitle, FolderUtilities::GetFilename(GetRomInfo().RomFile.GetFileName(), false));
if(stopRom) {
#ifndef LIBRETRO
_emuThread.reset(new thread(&Console::Run, this));
#endif
}
return true;
}

View file

@ -35,6 +35,8 @@ enum class ConsoleRegion;
class Console : public std::enable_shared_from_this<Console>
{
private:
unique_ptr<thread> _emuThread;
shared_ptr<Cpu> _cpu;
shared_ptr<Ppu> _ppu;
shared_ptr<Spc> _spc;

View file

@ -203,6 +203,7 @@ int32_t RecordedRomTest::Run(string filename)
emuCfg.RamPowerOnState = RamState::AllZeros;
_console->GetSettings()->SetEmulationConfig(emuCfg);
_console->Lock();
//Start playing movie
if(_console->LoadRom(testRom, VirtualFile(""))) {
settings->SetFlag(EmulationFlags::MaximumSpeed);
@ -211,16 +212,13 @@ int32_t RecordedRomTest::Run(string filename)
_ppu = _console->GetPpu().get();
_runningTest = true;
_runThread = std::thread([=]() {
_console->Run();
});
_console->Unlock();
_signal.Wait();
_console->Stop(false);
//_console->Release();
_runThread.join();
_runningTest = false;
} else {
//Something went wrong when loading the rom
_console->Unlock();
return -2;
}

View file

@ -23,7 +23,6 @@ private:
std::deque<uint8_t*> _screenshotHashes;
std::deque<uint8_t> _repetitionCount;
uint8_t _currentCount = 0;
std::thread _runThread;
string _filename;
ofstream _file;

View file

@ -153,9 +153,9 @@ extern "C" {
return _returnString.c_str();
}
DllExport void __stdcall Run()
DllExport bool __stdcall IsRunning()
{
_console->Run();
return _console->IsRunning();
}
DllExport void __stdcall Stop()

View file

@ -13,9 +13,6 @@ namespace Mesen.GUI.Emulation
{
public static class EmuRunner
{
private static Thread _emuThread = null;
private static ResourcePath? _romPath = null;
public static void LoadRom(ResourcePath romPath, ResourcePath? patchPath = null)
{
if(!frmSelectRom.SelectRom(ref romPath)) {
@ -32,8 +29,7 @@ namespace Mesen.GUI.Emulation
}
}
}
_romPath = romPath;
if(EmuApi.LoadRom(romPath, patchPath)) {
ConfigManager.Config.RecentFiles.AddRecentFile(romPath, patchPath);
}
@ -54,7 +50,7 @@ namespace Mesen.GUI.Emulation
//There is a single rom in the same folder as the IPS/BPS patch, use it automatically
LoadRom(romsInFolder[0], patchFile);
} else {
if(!IsRunning() || !_romPath.HasValue) {
if(!IsRunning()) {
//Prompt the user for a rom to load
if(MesenMsgBox.Show("SelectRomIps", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) {
using(OpenFileDialog ofd = new OpenFileDialog()) {
@ -70,7 +66,7 @@ namespace Mesen.GUI.Emulation
}
} else if(MesenMsgBox.Show("PatchAndReset", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) {
//Confirm that the user wants to patch the current rom and reset
LoadRom(_romPath.Value, patchFile);
LoadRom(EmuApi.GetRomInfo().RomPath, patchFile);
}
}
}
@ -78,7 +74,6 @@ namespace Mesen.GUI.Emulation
public static void LoadRecentGame(string recentGameArchivePath)
{
EmuApi.LoadRecentGame(recentGameArchivePath, false /* TODO , ConfigManager.Config.Preferences.GameSelectionScreenResetGame */);
StartEmulation();
}
private static bool IsPatchFile(string filename)
@ -107,19 +102,10 @@ namespace Mesen.GUI.Emulation
}
}
}
public static void StartEmulation()
{
_emuThread = new Thread(() => {
EmuApi.Run();
_emuThread = null;
});
_emuThread.Start();
}
public static bool IsRunning()
{
return _emuThread != null;
return EmuApi.IsRunning();
}
}
}

View file

@ -160,10 +160,6 @@ namespace Mesen.GUI.Forms
CheatCodes.ApplyCheats();
this.BeginInvoke((Action)(() => {
if(!EmuRunner.IsRunning()) {
EmuRunner.StartEmulation();
}
UpdateDebuggerMenu();
ctrlRecentGames.Visible = false;
SaveStateManager.UpdateStateMenu(mnuLoadState, false);

View file

@ -34,7 +34,7 @@ namespace Mesen.GUI
[DllImport(DllPath)] public static extern void InitializeEmu([MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))]string homeFolder, IntPtr windowHandle, IntPtr dxViewerHandle, [MarshalAs(UnmanagedType.I1)]bool noAudio, [MarshalAs(UnmanagedType.I1)]bool noVideo, [MarshalAs(UnmanagedType.I1)]bool noInput);
[DllImport(DllPath)] public static extern void Release();
[DllImport(DllPath)] public static extern void Run();
[DllImport(DllPath)] [return: MarshalAs(UnmanagedType.I1)] public static extern bool IsRunning();
[DllImport(DllPath)] public static extern void Stop();
[DllImport(DllPath)] public static extern void Reset();