Emu: Implement BlockingCallFromMainThread

Reduces some copy-paste clutter throughout the project
This commit is contained in:
Megamouse 2022-06-24 19:58:26 +02:00 committed by Ivan
parent 7842812e24
commit 9cf7a63c77
13 changed files with 95 additions and 223 deletions

View file

@ -130,6 +130,37 @@ void fmt_class_string<cfg_mode>::format(std::string& out, u64 arg)
});
}
void Emulator::CallFromMainThread(std::function<void()>&& func, atomic_t<bool>* wake_up, bool track_emu_state, u64 stop_ctr) const
{
if (!track_emu_state)
{
m_cb.call_from_main_thread(std::move(func), wake_up);
return;
}
std::function<void()> final_func = [this, before = IsStopped(), count = (stop_ctr == umax ? +m_stop_ctr : stop_ctr), func = std::move(func)]
{
if (count == m_stop_ctr && before == IsStopped())
{
func();
}
};
m_cb.call_from_main_thread(std::move(final_func), wake_up);
}
void Emulator::BlockingCallFromMainThread(std::function<void()>&& func) const
{
atomic_t<bool> wake_up = false;
CallFromMainThread(std::move(func), &wake_up);
while (!wake_up && !IsStopped())
{
thread_ctrl::wait_on(wake_up, false);
}
}
void Emulator::Init(bool add_only)
{
jit_runtime::initialize();