Replace balanced Core::PauseAndLock calls with RunAsCPUThread

Core::PauseAndLock requires all calls to it to be balanced, like this:

    const bool was_unpaused = Core::PauseAndLock(true);
    // do stuff on the CPU thread
    Core::PauseAndLock(false, was_unpaused);

Aside from being a bit cumbersome, it turns out all callers really
don't need to know about was_unpaused at all. They just need to do
something on the CPU thread safely, including locking/unlocking.

So this commit replaces Core::PauseAndLock with a function that
makes both the purpose and the scope of what is being run on the
CPU thread visually clear. This makes it harder to accidentally run
something on the wrong thread, or forget the second call to
PauseAndLock to unpause, or forget that it needs to be passed
was_unpaused at the end.

We also don't need comments to indicate code X is being run on the
CPU thread anymore, as the function name makes it obvious.
This commit is contained in:
Léo Lam
2017-07-21 14:06:02 +08:00
parent 2f0bec93cb
commit f106a9637d
13 changed files with 300 additions and 317 deletions

View File

@ -759,7 +759,7 @@ void RequestRefreshInfo()
s_request_refresh_info = true;
}
bool PauseAndLock(bool do_lock, bool unpause_on_unlock)
static bool PauseAndLock(bool do_lock, bool unpause_on_unlock)
{
// WARNING: PauseAndLock is not fully threadsafe so is only valid on the Host Thread
if (!IsRunning())
@ -806,6 +806,19 @@ bool PauseAndLock(bool do_lock, bool unpause_on_unlock)
return was_unpaused;
}
void RunAsCPUThread(std::function<void()> function)
{
const bool is_cpu_thread = IsCPUThread();
bool was_unpaused = false;
if (!is_cpu_thread)
was_unpaused = PauseAndLock(true, true);
function();
if (!is_cpu_thread)
PauseAndLock(false, was_unpaused);
}
// Display FPS info
// This should only be called from VI
void VideoThrottle()
@ -955,22 +968,20 @@ void UpdateWantDeterminism(bool initial)
{
NOTICE_LOG(COMMON, "Want determinism <- %s", new_want_determinism ? "true" : "false");
bool was_unpaused = Core::PauseAndLock(true);
RunAsCPUThread([&] {
s_wants_determinism = new_want_determinism;
const auto ios = IOS::HLE::GetIOS();
if (ios)
ios->UpdateWantDeterminism(new_want_determinism);
Fifo::UpdateWantDeterminism(new_want_determinism);
// We need to clear the cache because some parts of the JIT depend on want_determinism,
// e.g. use of FMA.
JitInterface::ClearCache();
s_wants_determinism = new_want_determinism;
const auto ios = IOS::HLE::GetIOS();
if (ios)
ios->UpdateWantDeterminism(new_want_determinism);
Fifo::UpdateWantDeterminism(new_want_determinism);
// We need to clear the cache because some parts of the JIT depend on want_determinism, e.g. use
// of FMA.
JitInterface::ClearCache();
// Don't call InitializeWiiRoot during boot, because IOS already does it.
if (!initial)
Core::InitializeWiiRoot(s_wants_determinism);
Core::PauseAndLock(false, was_unpaused);
// Don't call InitializeWiiRoot during boot, because IOS already does it.
if (!initial)
Core::InitializeWiiRoot(s_wants_determinism);
});
}
}