Emu: Run Qt events while calling Emu.Cleanup

This commit is contained in:
Elad 2025-05-20 18:05:44 +03:00
parent cb5fd17635
commit 7cb2977ede
2 changed files with 46 additions and 2 deletions

View file

@ -3895,6 +3895,40 @@ bool Emulator::Quit(bool force_quit)
void Emulator::CleanUp()
{
// Signal threads
if (auto rsx = g_fxo->try_get<rsx::thread>())
{
*static_cast<cpu_thread*>(rsx) = thread_state::aborting;
}
for (const auto& [type, data] : *g_fxo)
{
if (type.thread_op)
{
type.thread_op(data, thread_state::aborting);
}
}
// Join threads
qt_events_aware_op(50, [&]()
{
bool has_running = false;
for (const auto& [type, data] : *g_fxo)
{
if (type.thread_op)
{
if (type.thread_op(data, thread_state::aborting) != thread_state::finished)
{
has_running = true;
break;
}
}
}
return has_running == false;
});
// Deinitialize object manager to prevent any hanging objects at program exit
g_fxo->clear();
}

View file

@ -71,7 +71,7 @@ namespace stx
struct typeinfo
{
bool(*create)(uchar* ptr, manual_typemap&, utils::serial*, std::string_view) noexcept = nullptr;
void(*thread_op)(void* ptr, thread_state) noexcept = nullptr;
thread_state(*thread_op)(void* ptr, thread_state) noexcept = nullptr;
void(*save)(void* ptr, utils::serial&) noexcept = nullptr;
bool(*saveable)(bool) noexcept = nullptr;
void(*destroy)(void* ptr) noexcept = nullptr;
@ -137,10 +137,20 @@ namespace stx
}
template <typename T>
static void call_thread_op(void* ptr, thread_state state) noexcept
static thread_state call_thread_op(void* ptr, thread_state state) noexcept
{
// Abort and/or join (expected thread_state::aborting or thread_state::finished)
*std::launder(static_cast<T*>(ptr)) = state;
if constexpr (std::is_convertible_v<const T&, thread_state>)
{
return *std::launder(static_cast<T*>(ptr));
}
else
{
constexpr thread_state context_finished{3};
return context_finished;
}
}
template <typename T>