Skip to content

Commit

Permalink
Core/Movie: Add ability to run code in Host context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EmptyChaos committed May 4, 2016
1 parent 6b5537a commit 8db0f40
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 28 deletions.
27 changes: 26 additions & 1 deletion Source/Android/jni/MainAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdlib>
#include <jni.h>
#include <memory>
#include <mutex>
#include <thread>
#include <android/log.h>
#include <android/native_window_jni.h>
Expand Down Expand Up @@ -66,13 +67,23 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved)
void Host_NotifyMapLoaded() {}
void Host_RefreshDSPDebuggerWindow() {}

// The Core only supports using a single Host thread.
// If multiple threads want to call host functions then they need to queue
// sequentially for access.
static std::mutex s_host_identity_lock;
Common::Event updateMainFrameEvent;
static bool s_have_wm_user_stop = false;
void Host_Message(int Id)
{
if (Id == WM_USER_STOP)
if (Id == WM_USER_JOB_DISPATCH)
{
updateMainFrameEvent.Set();
}
else if (Id == WM_USER_STOP)
{
s_have_wm_user_stop = true;
if (Core::IsRunning())
Core::QueueHostJob(&Core::Stop);
}
}

Expand Down Expand Up @@ -394,15 +405,18 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SurfaceDestr

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_UnPauseEmulation(JNIEnv *env, jobject obj)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
Core::SetState(Core::CORE_RUN);
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_PauseEmulation(JNIEnv *env, jobject obj)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
Core::SetState(Core::CORE_PAUSE);
}

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_StopEmulation(JNIEnv *env, jobject obj)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
Core::SaveScreenShot("thumb");
Renderer::s_screenshotCompleted.WaitFor(std::chrono::seconds(2));
Core::Stop();
Expand Down Expand Up @@ -491,6 +505,7 @@ JNIEXPORT jboolean JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_Supports

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SaveScreenShot(JNIEnv *env, jobject obj)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
Core::SaveScreenShot();
}

Expand Down Expand Up @@ -535,11 +550,13 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetFilename(

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SaveState(JNIEnv *env, jobject obj, jint slot)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
State::Save(slot);
}

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_LoadState(JNIEnv *env, jobject obj, jint slot)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
State::Load(slot);
}

Expand Down Expand Up @@ -578,6 +595,7 @@ JNIEXPORT jstring JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_GetUserDi

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetProfiling(JNIEnv *env, jobject obj, jboolean enable)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
Core::SetState(Core::CORE_PAUSE);
JitInterface::ClearCache();
Profiler::g_ProfileBlocks = enable;
Expand All @@ -586,6 +604,7 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SetProfiling

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_WriteProfileResults(JNIEnv *env, jobject obj)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
std::string filename = File::GetUserPath(D_DUMP_IDX) + "Debug/profiler.txt";
File::CreateFullPath(filename);
JitInterface::WriteProfileResults(filename);
Expand Down Expand Up @@ -644,6 +663,7 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_SurfaceDestr
}
JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_RefreshWiimotes(JNIEnv *env, jobject obj)
{
std::lock_guard<std::mutex> guard(s_host_identity_lock);
WiimoteReal::Refresh();
}

Expand All @@ -657,6 +677,7 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_Run(JNIEnv *

RegisterMsgAlertHandler(&MsgAlert);

std::unique_lock<std::mutex> guard(s_host_identity_lock);
UICommon::SetUserDirectory(g_set_userpath);
UICommon::Init();

Expand All @@ -679,13 +700,17 @@ JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_NativeLibrary_Run(JNIEnv *
{
while (PowerPC::GetState() != PowerPC::CPU_POWERDOWN)
{
guard.unlock();
updateMainFrameEvent.Wait();
guard.lock();
Core::HostDispatchJobs();
}
}
}

Core::Shutdown();
UICommon::Shutdown();
guard.unlock();

if (surf)
{
Expand Down
1 change: 1 addition & 0 deletions Source/Core/Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ enum HOST_COMM
WM_USER_STOP = 10,
WM_USER_CREATE,
WM_USER_SETCURSOR,
WM_USER_JOB_DISPATCH,
};

// Used for notification on emulation state
Expand Down
104 changes: 97 additions & 7 deletions Source/Core/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <atomic>
#include <cctype>
#include <cstring>
#include <mutex>
#include <queue>
#include <utility>

#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 ----");
Expand Down Expand Up @@ -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()
{
Expand All @@ -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
Expand Down Expand Up @@ -395,7 +433,10 @@ static void FifoPlayerThread()
did_run = true;
PowerPC::InjectExternalCPUCore(cpu_core.get());
s_is_started = true;

CPUSetInitialExecutionState();
CPU::Run();

s_is_started = false;
PowerPC::InjectExternalCPUCore(nullptr);
}
Expand Down Expand Up @@ -429,6 +470,7 @@ static void FifoPlayerThread()
void EmuThread()
{
const SConfig& core_parameter = SConfig::GetInstance();
s_is_booting.store(true);

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

Expand All @@ -447,6 +489,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;
Expand All @@ -461,6 +504,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!");
Expand Down Expand Up @@ -501,12 +545,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);
Expand Down Expand Up @@ -642,6 +684,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:
Expand Down Expand Up @@ -890,6 +936,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)
Expand Down Expand Up @@ -923,4 +972,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
16 changes: 16 additions & 0 deletions Source/Core/Core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#pragma once

#include <functional>
#include <string>
#include <vector>

Expand Down Expand Up @@ -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
7 changes: 3 additions & 4 deletions Source/Core/Core/HW/DVDInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 8db0f40

Please sign in to comment.