Skip to content

Commit

Permalink
Merge pull request #5517 from leoetlino/more-reliable-shutdown
Browse files Browse the repository at this point in the history
More reliable shutdown
  • Loading branch information
leoetlino committed Jun 3, 2017
2 parents bcc85f9 + 546181c commit 227bc38
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
69 changes: 36 additions & 33 deletions Source/Core/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Common/Logging/LogManager.h"
#include "Common/MemoryUtil.h"
#include "Common/MsgHandler.h"
#include "Common/ScopeGuard.h"
#include "Common/StringUtil.h"
#include "Common/Thread.h"
#include "Common/Timer.h"
Expand Down Expand Up @@ -453,6 +454,19 @@ static void EmuThread()
{
const SConfig& core_parameter = SConfig::GetInstance();
s_is_booting.Set();
Common::ScopeGuard flag_guard{[] {
s_is_booting.Clear();
s_is_started = false;
s_is_stopping = false;

if (s_on_stopped_callback)
s_on_stopped_callback();

INFO_LOG(CONSOLE, "Stop\t\t---- Shutdown complete ----");
}};

// Prevent the UI from getting stuck whenever an error occurs.
Common::ScopeGuard stop_message_guard{[] { Host_Message(WM_USER_STOP); }};

Common::SetCurrentThreadName("Emuthread - Starting");

Expand All @@ -466,16 +480,23 @@ static void EmuThread()
DeclareAsCPUThread();

Movie::Init();
Common::ScopeGuard movie_guard{Movie::Shutdown};

HW::Init();
Common::ScopeGuard hw_guard{[] {
// We must set up this flag before executing HW::Shutdown()
s_hardware_initialized = false;
INFO_LOG(CONSOLE, "%s", StopMessage(false, "Shutting down HW").c_str());
HW::Shutdown();
INFO_LOG(CONSOLE, "%s", StopMessage(false, "HW shutdown").c_str());
}};

if (!g_video_backend->Initialize(s_window_handle))
{
s_is_booting.Clear();
PanicAlert("Failed to initialize video backend!");
Host_Message(WM_USER_STOP);
return;
}
Common::ScopeGuard video_guard{[] { g_video_backend->Shutdown(); }};

OSD::AddMessage("Dolphin " + g_video_backend->GetName() + " Video Backend.", 5000);

Expand All @@ -486,11 +507,7 @@ static void EmuThread()

if (!DSP::GetDSPEmulator()->Initialize(core_parameter.bWii, core_parameter.bDSPThread))
{
s_is_booting.Clear();
HW::Shutdown();
g_video_backend->Shutdown();
PanicAlert("Failed to initialize DSP emulation!");
Host_Message(WM_USER_STOP);
return;
}

Expand Down Expand Up @@ -520,7 +537,18 @@ static void EmuThread()
Wiimote::LoadConfig();
}

Common::ScopeGuard controller_guard{[init_controllers] {
if (!init_controllers)
return;

Wiimote::Shutdown();
Keyboard::Shutdown();
Pad::Shutdown();
g_controller_interface.Shutdown();
}};

AudioCommon::InitSoundStream();
Common::ScopeGuard audio_guard{AudioCommon::ShutdownSoundStream};

// The hardware is initialized.
s_hardware_initialized = true;
Expand Down Expand Up @@ -617,40 +645,15 @@ static void EmuThread()
if (core_parameter.bCPUThread)
g_video_backend->Video_Cleanup();

// We must set up this flag before executing HW::Shutdown()
s_hardware_initialized = false;
INFO_LOG(CONSOLE, "%s", StopMessage(false, "Shutting down HW").c_str());
HW::Shutdown();
INFO_LOG(CONSOLE, "%s", StopMessage(false, "HW shutdown").c_str());

if (init_controllers)
{
Wiimote::Shutdown();
Keyboard::Shutdown();
Pad::Shutdown();
g_controller_interface.Shutdown();
init_controllers = false;
}

g_video_backend->Shutdown();
AudioCommon::ShutdownSoundStream();

INFO_LOG(CONSOLE, "%s", StopMessage(true, "Main Emu thread stopped").c_str());

// Clear on screen messages that haven't expired
OSD::ClearMessages();

BootManager::RestoreConfig();

INFO_LOG(CONSOLE, "Stop [Video Thread]\t\t---- Shutdown complete ----");
Movie::Shutdown();
PatchEngine::Shutdown();
HLE::Clear();

s_is_stopping = false;

if (s_on_stopped_callback)
s_on_stopped_callback();
// If we shut down normally, the stop message does not need to be triggered.
stop_message_guard.Dismiss();
}

// Set or get the running state
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/DolphinWX/FrameTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,6 @@ void CFrame::DoStop()
// don't let this function run again until it finishes, or is aborted.
m_confirm_stop = true;

m_is_game_loading = false;
if (Core::GetState() != Core::State::Uninitialized || m_render_parent != nullptr)
{
#if defined __WXGTK__
Expand Down Expand Up @@ -911,6 +910,7 @@ bool CFrame::TriggerSTMPowerEvent()
void CFrame::OnStopped()
{
m_confirm_stop = false;
m_is_game_loading = false;
m_tried_graceful_shutdown = false;

UninhibitScreensaver();
Expand Down

0 comments on commit 227bc38

Please sign in to comment.