summaryrefslogtreecommitdiff
path: root/Source/Core
diff options
context:
space:
mode:
authorEmptyChaos <EmptyChaos@users.noreply.github.com>2016-05-12 01:18:30 +0000
committerEmptyChaos <EmptyChaos@users.noreply.github.com>2016-05-13 09:23:44 +1000
commitc1944f623b5918ab45152795dea9e49f57878138 (patch)
treeb11e913d08eb36eabdeef57f45b94bedc2e2da40 /Source/Core
parente8dfc8e654f7a8a92b126bdcff9e4b86b7463ba9 (diff)
Core/Movie: Add ability to run code in Host context
EndPlayInput runs on the CPU thread so it can't directly call UpdateWantDeterminism. PlayController also tries to ChangeDisc from the CPU Thread which is also invalid. It now just pauses execution and posts a request to the Host to fix it instead. The Core itself also did dodgy things like PauseAndLock-ing from the CPU Thread and SetState from EmuThread which have been removed.
Diffstat (limited to 'Source/Core')
-rw-r--r--Source/Core/Common/Common.h1
-rw-r--r--Source/Core/Core/Core.cpp104
-rw-r--r--Source/Core/Core/Core.h16
-rw-r--r--Source/Core/Core/HW/DVDInterface.cpp7
-rw-r--r--Source/Core/Core/HW/DVDInterface.h2
-rw-r--r--Source/Core/Core/Movie.cpp35
-rw-r--r--Source/Core/DolphinQt2/Host.cpp10
-rw-r--r--Source/Core/DolphinQt2/Main.cpp7
-rw-r--r--Source/Core/DolphinWX/Main.cpp13
-rw-r--r--Source/Core/DolphinWX/Main.h1
-rw-r--r--Source/Core/DolphinWX/MainNoGUI.cpp33
11 files changed, 200 insertions, 29 deletions
diff --git a/Source/Core/Common/Common.h b/Source/Core/Common/Common.h
index 36a1bc13bf..7f8e6ce639 100644
--- a/Source/Core/Common/Common.h
+++ b/Source/Core/Common/Common.h
@@ -83,6 +83,7 @@ enum HOST_COMM
WM_USER_STOP = 10,
WM_USER_CREATE,
WM_USER_SETCURSOR,
+ WM_USER_JOB_DISPATCH,
};
// Used for notification on emulation state
diff --git a/Source/Core/Core/Core.cpp b/Source/Core/Core/Core.cpp
index b5fbf14663..ab888de322 100644
--- a/Source/Core/Core/Core.cpp
+++ b/Source/Core/Core/Core.cpp
@@ -5,6 +5,9 @@
#include <atomic>
#include <cctype>
#include <cstring>
+#include <mutex>
+#include <queue>
+#include <utility>
#ifdef _WIN32
#include <windows.h>
@@ -102,6 +105,7 @@ void EmuThread();
static bool s_is_stopping = false;
static bool s_hardware_initialized = false;
static bool s_is_started = false;
+static std::atomic<bool> s_is_booting{ false };
static void* s_window_handle = nullptr;
static std::string s_state_filename;
static std::thread s_emu_thread;
@@ -112,6 +116,14 @@ static bool s_request_refresh_info = false;
static int s_pause_and_lock_depth = 0;
static bool s_is_throttler_temp_disabled = false;
+struct HostJob
+{
+ std::function<void()> job;
+ bool run_after_stop;
+};
+static std::mutex s_host_jobs_lock;
+static std::queue<HostJob> s_host_jobs_queue;
+
#ifdef ThreadLocalStorage
static ThreadLocalStorage bool tls_is_cpu_thread = false;
#else
@@ -225,6 +237,9 @@ bool Init()
s_emu_thread.join();
}
+ // Drain any left over jobs
+ HostDispatchJobs();
+
Core::UpdateWantDeterminism(/*initial*/ true);
INFO_LOG(OSREPORT, "Starting core = %s mode",
@@ -260,6 +275,9 @@ void Stop() // - Hammertime!
s_is_stopping = true;
+ // Dump left over jobs
+ HostDispatchJobs();
+
Fifo::EmulatorState(false);
INFO_LOG(CONSOLE, "Stop [Main Thread]\t\t---- Shutting down ----");
@@ -310,6 +328,16 @@ void UndeclareAsCPUThread()
#endif
}
+// For the CPU Thread only.
+static void CPUSetInitialExecutionState()
+{
+ QueueHostJob([]
+ {
+ SetState(SConfig::GetInstance().bBootToPause ? CORE_PAUSE : CORE_RUN);
+ Host_UpdateMainFrame();
+ });
+}
+
// Create the CPU thread, which is a CPU + Video thread in Single Core mode.
static void CpuThread()
{
@@ -331,10 +359,20 @@ static void CpuThread()
EMM::InstallExceptionHandler(); // Let's run under memory watch
if (!s_state_filename.empty())
- State::LoadAs(s_state_filename);
+ {
+ // Needs to PauseAndLock the Core
+ // NOTE: EmuThread should have left us in CPU_STEPPING so nothing will happen
+ // until after the job is serviced.
+ QueueHostJob([]
+ {
+ // Recheck in case Movie cleared it since.
+ if (!s_state_filename.empty())
+ State::LoadAs(s_state_filename);
+ });
+ }
s_is_started = true;
-
+ CPUSetInitialExecutionState();
#ifdef USE_GDBSTUB
#ifndef _WIN32
@@ -393,7 +431,10 @@ static void FifoPlayerThread()
{
PowerPC::InjectExternalCPUCore(cpu_core.get());
s_is_started = true;
+
+ CPUSetInitialExecutionState();
CPU::Run();
+
s_is_started = false;
PowerPC::InjectExternalCPUCore(nullptr);
}
@@ -427,6 +468,7 @@ static void FifoPlayerThread()
void EmuThread()
{
const SConfig& core_parameter = SConfig::GetInstance();
+ s_is_booting.store(true);
Common::SetCurrentThreadName("Emuthread - Starting");
@@ -445,6 +487,7 @@ void EmuThread()
if (!g_video_backend->Initialize(s_window_handle))
{
+ s_is_booting.store(false);
PanicAlert("Failed to initialize video backend!");
Host_Message(WM_USER_STOP);
return;
@@ -459,6 +502,7 @@ void EmuThread()
if (!DSP::GetDSPEmulator()->Initialize(core_parameter.bWii, core_parameter.bDSPThread))
{
+ s_is_booting.store(false);
HW::Shutdown();
g_video_backend->Shutdown();
PanicAlert("Failed to initialize DSP emulation!");
@@ -499,12 +543,10 @@ void EmuThread()
// The hardware is initialized.
s_hardware_initialized = true;
+ s_is_booting.store(false);
- // Boot to pause or not
- // NOTE: This violates the Host Thread requirement for SetState but we should
- // not race the Host because the UI should have the buttons disabled until
- // Host_UpdateMainFrame enables them.
- Core::SetState(core_parameter.bBootToPause ? Core::CORE_PAUSE : Core::CORE_RUN);
+ // Set execution state to known values (CPU/FIFO/Audio Paused)
+ CPU::Break();
// Load GCM/DOL/ELF whatever ... we boot with the interpreter core
PowerPC::SetMode(PowerPC::MODE_INTERPRETER);
@@ -640,6 +682,10 @@ void EmuThread()
void SetState(EState state)
{
+ // State cannot be controlled until the CPU Thread is operational
+ if (!IsRunningAndStarted())
+ return;
+
switch (state)
{
case CORE_PAUSE:
@@ -904,6 +950,9 @@ void Shutdown()
// on MSDN.
if (s_emu_thread.joinable())
s_emu_thread.join();
+
+ // Make sure there's nothing left over in case we're about to exit.
+ HostDispatchJobs();
}
void SetOnStoppedCallback(StoppedCallbackFunc callback)
@@ -937,4 +986,45 @@ void UpdateWantDeterminism(bool initial)
}
}
+void QueueHostJob(std::function<void()> job, bool run_during_stop)
+{
+ if (!job)
+ return;
+
+ bool send_message = false;
+ {
+ std::lock_guard<std::mutex> guard(s_host_jobs_lock);
+ send_message = s_host_jobs_queue.empty();
+ s_host_jobs_queue.emplace(HostJob{ std::move(job), run_during_stop });
+ }
+ // If the the queue was empty then kick the Host to come and get this job.
+ if (send_message)
+ Host_Message(WM_USER_JOB_DISPATCH);
+}
+
+void HostDispatchJobs()
+{
+ // WARNING: This should only run on the Host Thread.
+ // NOTE: This function is potentially re-entrant. If a job calls
+ // Core::Stop for instance then we'll enter this a second time.
+ std::unique_lock<std::mutex> guard(s_host_jobs_lock);
+ while (!s_host_jobs_queue.empty())
+ {
+ HostJob job = std::move(s_host_jobs_queue.front());
+ s_host_jobs_queue.pop();
+
+ // NOTE: Memory ordering is important. The booting flag needs to be
+ // checked first because the state transition is:
+ // CORE_UNINITIALIZED: s_is_booting -> s_hardware_initialized
+ // We need to check variables in the same order as the state
+ // transition, otherwise we race and get transient failures.
+ if (!job.run_after_stop && !s_is_booting.load() && !IsRunning())
+ continue;
+
+ guard.unlock();
+ job.job();
+ guard.lock();
+ }
+}
+
} // Core
diff --git a/Source/Core/Core/Core.h b/Source/Core/Core/Core.h
index b5de60fd5e..f09f89bce2 100644
--- a/Source/Core/Core/Core.h
+++ b/Source/Core/Core/Core.h
@@ -11,6 +11,7 @@
#pragma once
+#include <functional>
#include <string>
#include <vector>
@@ -91,4 +92,19 @@ void SetOnStoppedCallback(StoppedCallbackFunc callback);
// Run on the Host thread when the factors change. [NOT THREADSAFE]
void UpdateWantDeterminism(bool initial = false);
+// Queue an arbitrary function to asynchronously run once on the Host thread later.
+// Threadsafe. Can be called by any thread, including the Host itself.
+// Jobs will be executed in RELATIVE order. If you queue 2 jobs from the same thread
+// then they will be executed in the order they were queued; however, there is no
+// global order guarantee across threads - jobs from other threads may execute in
+// between.
+// NOTE: Make sure the jobs check the global state instead of assuming everything is
+// still the same as when the job was queued.
+// NOTE: Jobs that are not set to run during stop will be discarded instead.
+void QueueHostJob(std::function<void()> job, bool run_during_stop = false);
+
+// Should be called periodically by the Host to run pending jobs.
+// WM_USER_JOB_DISPATCH will be sent when something is added to the queue.
+void HostDispatchJobs();
+
} // namespace
diff --git a/Source/Core/Core/HW/DVDInterface.cpp b/Source/Core/Core/HW/DVDInterface.cpp
index 2276150d27..c3626cd428 100644
--- a/Source/Core/Core/HW/DVDInterface.cpp
+++ b/Source/Core/Core/HW/DVDInterface.cpp
@@ -478,8 +478,8 @@ static void InsertDiscCallback(u64 userdata, s64 cyclesLate)
void ChangeDisc(const std::string& newFileName)
{
- bool is_cpu = Core::IsCPUThread();
- bool was_unpaused = is_cpu ? false : Core::PauseAndLock(true);
+ // WARNING: Can only run on Host Thread
+ bool was_unpaused = Core::PauseAndLock(true);
std::string* _FileName = new std::string(newFileName);
CoreTiming::ScheduleEvent(0, s_eject_disc);
CoreTiming::ScheduleEvent(500000000, s_insert_disc, (u64)_FileName);
@@ -495,8 +495,7 @@ void ChangeDisc(const std::string& newFileName)
}
Movie::g_discChange = fileName.substr(sizeofpath);
}
- if (!is_cpu)
- Core::PauseAndLock(false, was_unpaused);
+ Core::PauseAndLock(false, was_unpaused);
}
void SetLidOpen(bool open)
diff --git a/Source/Core/Core/HW/DVDInterface.h b/Source/Core/Core/HW/DVDInterface.h
index 8c26b3b7c8..96b065132e 100644
--- a/Source/Core/Core/HW/DVDInterface.h
+++ b/Source/Core/Core/HW/DVDInterface.h
@@ -102,7 +102,7 @@ bool VolumeIsValid();
// Disc detection and swapping
void SetDiscInside(bool _DiscInside);
bool IsDiscInside();
-void ChangeDisc(const std::string& fileName);
+void ChangeDisc(const std::string& fileName); // [NOT THREADSAFE] Host only
// DVD Access Functions
bool ChangePartition(u64 offset);
diff --git a/Source/Core/Core/Movie.cpp b/Source/Core/Core/Movie.cpp
index 35bbae2762..176ef5b445 100644
--- a/Source/Core/Core/Movie.cpp
+++ b/Source/Core/Core/Movie.cpp
@@ -415,7 +415,7 @@ bool IsNetPlayRecording()
return s_bNetPlay;
}
-// NOTE: Host / CPU Thread
+// NOTE: Host Thread
void ChangePads(bool instantly)
{
if (!Core::IsRunning())
@@ -824,7 +824,7 @@ void RecordWiimote(int wiimote, u8 *data, u8 size)
s_totalBytes = s_currentByte;
}
-// NOTE: CPU / EmuThread / Host Thread
+// NOTE: EmuThread / Host Thread
void ReadHeader()
{
s_numPads = tmpHeader.numControllers;
@@ -934,7 +934,7 @@ void DoState(PointerWrap &p)
// other variables (such as s_totalBytes and g_totalFrames) are set in LoadInput
}
-// NOTE: Host / CPU Thread
+// NOTE: Host Thread
void LoadInput(const std::string& filename)
{
File::IOFile t_record;
@@ -1152,7 +1152,7 @@ void PlayController(GCPadStatus* PadStatus, int controllerID)
{
// This implementation assumes the disc change will only happen once. Trying to change more than that will cause
// it to load the last disc every time. As far as i know though, there are no 3+ disc games, so this should be fine.
- Core::SetState(Core::CORE_PAUSE);
+ CPU::Break();
bool found = false;
std::string path;
for (size_t i = 0; i < SConfig::GetInstance().m_ISOFolder.size(); ++i)
@@ -1166,8 +1166,16 @@ void PlayController(GCPadStatus* PadStatus, int controllerID)
}
if (found)
{
- DVDInterface::ChangeDisc(path + '/' + g_discChange);
- Core::SetState(Core::CORE_RUN);
+ path += '/' + g_discChange;
+
+ Core::QueueHostJob([=]
+ {
+ if (!Movie::IsPlayingInput())
+ return;
+
+ DVDInterface::ChangeDisc(path);
+ CPU::EnableStepping(false);
+ });
}
else
{
@@ -1235,10 +1243,13 @@ void EndPlayInput(bool cont)
}
else if (s_playMode != MODE_NONE)
{
+ // We can be called by EmuThread during boot (CPU_POWERDOWN)
+ bool was_running = Core::IsRunningAndStarted() && !CPU::IsStepping();
+ if (was_running)
+ CPU::Break();
s_rerecords = 0;
s_currentByte = 0;
s_playMode = MODE_NONE;
- Core::UpdateWantDeterminism();
Core::DisplayMessage("Movie End.", 2000);
s_bRecordingFromSaveState = false;
// we don't clear these things because otherwise we can't resume playback if we load a movie state later
@@ -1246,8 +1257,12 @@ void EndPlayInput(bool cont)
//delete tmpInput;
//tmpInput = nullptr;
- if (SConfig::GetInstance().m_PauseMovie)
- Core::SetState(Core::CORE_PAUSE);
+ Core::QueueHostJob([=]
+ {
+ Core::UpdateWantDeterminism();
+ if (was_running && !SConfig::GetInstance().m_PauseMovie)
+ CPU::EnableStepping(false);
+ });
}
}
@@ -1353,7 +1368,7 @@ void SetGraphicsConfig()
g_Config.bUseRealXFB = tmpHeader.bUseRealXFB;
}
-// NOTE: CPU / EmuThread / Host Thread
+// NOTE: EmuThread / Host Thread
void GetSettings()
{
s_bSaveConfig = true;
diff --git a/Source/Core/DolphinQt2/Host.cpp b/Source/Core/DolphinQt2/Host.cpp
index 389e0508b8..db74d88b39 100644
--- a/Source/Core/DolphinQt2/Host.cpp
+++ b/Source/Core/DolphinQt2/Host.cpp
@@ -2,6 +2,8 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
+#include <QAbstractEventDispatcher>
+#include <QApplication>
#include <QMutexLocker>
#include "Common/Common.h"
@@ -57,7 +59,15 @@ void Host::SetRenderFullscreen(bool fullscreen)
void Host_Message(int id)
{
if (id == WM_USER_STOP)
+ {
emit Host::GetInstance()->RequestStop();
+ }
+ else if (id == WM_USER_JOB_DISPATCH)
+ {
+ // Just poke the main thread to get it to wake up, job dispatch
+ // will happen automatically before it goes back to sleep again.
+ QAbstractEventDispatcher::instance(qApp->thread())->wakeUp();
+ }
}
void Host_UpdateTitle(const std::string& title)
diff --git a/Source/Core/DolphinQt2/Main.cpp b/Source/Core/DolphinQt2/Main.cpp
index 639f68a4b4..12963caf59 100644
--- a/Source/Core/DolphinQt2/Main.cpp
+++ b/Source/Core/DolphinQt2/Main.cpp
@@ -2,6 +2,7 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
+#include <QAbstractEventDispatcher>
#include <QApplication>
#include "Core/BootManager.h"
@@ -20,6 +21,12 @@ int main(int argc, char* argv[])
UICommon::Init();
Resources::Init();
+ // Whenever the event loop is about to go to sleep, dispatch the jobs
+ // queued in the Core first.
+ QObject::connect(QAbstractEventDispatcher::instance(),
+ &QAbstractEventDispatcher::aboutToBlock,
+ &app, &Core::HostDispatchJobs);
+
MainWindow win;
win.show();
int retval = app.exec();
diff --git a/Source/Core/DolphinWX/Main.cpp b/Source/Core/DolphinWX/Main.cpp
index e085a94bb2..5398bf244c 100644
--- a/Source/Core/DolphinWX/Main.cpp
+++ b/Source/Core/DolphinWX/Main.cpp
@@ -101,6 +101,7 @@ bool DolphinApp::OnInit()
Bind(wxEVT_QUERY_END_SESSION, &DolphinApp::OnEndSession, this);
Bind(wxEVT_END_SESSION, &DolphinApp::OnEndSession, this);
+ Bind(wxEVT_IDLE, &DolphinApp::OnIdle, this);
// Register message box and translation handlers
RegisterMsgAlertHandler(&wxMsgAlert);
@@ -359,6 +360,12 @@ void DolphinApp::OnFatalException()
WiimoteReal::Shutdown();
}
+void DolphinApp::OnIdle(wxIdleEvent& ev)
+{
+ ev.Skip();
+ Core::HostDispatchJobs();
+}
+
// ------------
// Talk to GUI
@@ -395,6 +402,12 @@ CFrame* DolphinApp::GetCFrame()
void Host_Message(int Id)
{
+ if (Id == WM_USER_JOB_DISPATCH)
+ {
+ // Trigger a wxEVT_IDLE
+ wxWakeUpIdle();
+ return;
+ }
wxCommandEvent event(wxEVT_HOST_COMMAND, Id);
main_frame->GetEventHandler()->AddPendingEvent(event);
}
diff --git a/Source/Core/DolphinWX/Main.h b/Source/Core/DolphinWX/Main.h
index 9b3bf2fdd0..7e9e3f56ff 100644
--- a/Source/Core/DolphinWX/Main.h
+++ b/Source/Core/DolphinWX/Main.h
@@ -33,6 +33,7 @@ private:
void OnEndSession(wxCloseEvent& event);
void InitLanguageSupport();
void AfterInit();
+ void OnIdle(wxIdleEvent&);
bool m_batch_mode = false;
bool m_confirm_stop = false;
diff --git a/Source/Core/DolphinWX/MainNoGUI.cpp b/Source/Core/DolphinWX/MainNoGUI.cpp
index bf2089a5c8..aaf207ade3 100644
--- a/Source/Core/DolphinWX/MainNoGUI.cpp
+++ b/Source/Core/DolphinWX/MainNoGUI.cpp
@@ -7,6 +7,7 @@
#include <cstring>
#include <getopt.h>
#include <string>
+#include <thread>
#include <unistd.h>
#include "Common/CommonTypes.h"
@@ -36,7 +37,14 @@ class Platform
public:
virtual void Init() {}
virtual void SetTitle(const std::string &title) {}
- virtual void MainLoop() { while(running) {} }
+ virtual void MainLoop()
+ {
+ while (running)
+ {
+ Core::HostDispatchJobs();
+ std::this_thread::sleep_for(std::chrono::milliseconds(100));
+ }
+ }
virtual void Shutdown() {}
virtual ~Platform() {}
};
@@ -50,7 +58,10 @@ static Common::Event updateMainFrameEvent;
void Host_Message(int Id)
{
if (Id == WM_USER_STOP)
+ {
running = false;
+ updateMainFrameEvent.Set();
+ }
}
static void* s_window_handle = nullptr;
@@ -101,10 +112,13 @@ void Host_ConnectWiimote(int wm_idx, bool connect)
{
if (Core::IsRunning() && SConfig::GetInstance().bWii)
{
- bool was_unpaused = Core::PauseAndLock(true);
- GetUsbPointer()->AccessWiiMote(wm_idx | 0x100)->Activate(connect);
- Host_UpdateMainFrame();
- Core::PauseAndLock(false, was_unpaused);
+ Core::QueueHostJob([=]
+ {
+ bool was_unpaused = Core::PauseAndLock(true);
+ GetUsbPointer()->AccessWiiMote(wm_idx | 0x100)->Activate(connect);
+ Host_UpdateMainFrame();
+ Core::PauseAndLock(false, was_unpaused);
+ });
}
}
@@ -270,6 +284,7 @@ class PlatformX11 : public Platform
&borderDummy, &depthDummy);
rendererIsFullscreen = false;
}
+ Core::HostDispatchJobs();
usleep(100000);
}
}
@@ -353,10 +368,14 @@ int main(int argc, char* argv[])
return 1;
}
- while (!Core::IsRunning())
+ while (!Core::IsRunning() && running)
+ {
+ Core::HostDispatchJobs();
updateMainFrameEvent.Wait();
+ }
- platform->MainLoop();
+ if (running)
+ platform->MainLoop();
Core::Stop();
Core::Shutdown();