SaveStates: Fix Gem Thread Reboot

This commit is contained in:
Elad 2025-10-12 09:36:50 +03:00
parent a5b5ac1ed5
commit f3f6186a75
6 changed files with 28 additions and 6 deletions

View file

@ -106,6 +106,11 @@ thread_local u64 g_tls_wait_fail = 0;
thread_local bool g_tls_access_violation_recovered = false; thread_local bool g_tls_access_violation_recovered = false;
extern thread_local std::string(*g_tls_log_prefix)(); extern thread_local std::string(*g_tls_log_prefix)();
namespace stx
{
atomic_t<u32> g_launch_retainer{0};
}
// Report error and call std::abort(), defined in main.cpp // Report error and call std::abort(), defined in main.cpp
[[noreturn]] void report_fatal_error(std::string_view text, bool is_html = false, bool include_help_text = true); [[noreturn]] void report_fatal_error(std::string_view text, bool is_html = false, bool include_help_text = true);

View file

@ -465,6 +465,8 @@ public:
namespace stx namespace stx
{ {
struct launch_retainer; struct launch_retainer;
extern atomic_t<u32> g_launch_retainer;
} }
// Derived from the callable object Context, possibly a lambda // Derived from the callable object Context, possibly a lambda
@ -481,6 +483,11 @@ class named_thread final : public Context, result_storage<Context>, thread_base
u64 entry_point2() u64 entry_point2()
{ {
while (u32 value = stx::g_launch_retainer)
{
stx::g_launch_retainer.wait(value);
}
thread::initialize([]() thread::initialize([]()
{ {
if constexpr (!result::empty) if constexpr (!result::empty)

View file

@ -361,7 +361,7 @@ public:
case move_handler::mouse: case move_handler::mouse:
case move_handler::raw_mouse: case move_handler::raw_mouse:
{ {
auto& handler = g_fxo->get<MouseHandlerBase>(); auto& handler = *ensure(g_fxo->try_get<MouseHandlerBase>());
std::lock_guard mouse_lock(handler.mutex); std::lock_guard mouse_lock(handler.mutex);
const MouseInfo& info = handler.GetInfo(); const MouseInfo& info = handler.GetInfo();
@ -374,7 +374,7 @@ public:
#ifdef HAVE_LIBEVDEV #ifdef HAVE_LIBEVDEV
case move_handler::gun: case move_handler::gun:
{ {
gun_thread& gun = g_fxo->get<gun_thread>(); gun_thread& gun = *ensure(g_fxo->try_get<gun_thread>());
std::scoped_lock lock(gun.handler.mutex); std::scoped_lock lock(gun.handler.mutex);
gun.num_devices = gun.handler.init() ? gun.handler.get_num_guns() : 0; gun.num_devices = gun.handler.init() ? gun.handler.get_num_guns() : 0;
@ -505,7 +505,7 @@ public:
case move_handler::mouse: case move_handler::mouse:
case move_handler::raw_mouse: case move_handler::raw_mouse:
{ {
auto& handler = g_fxo->get<MouseHandlerBase>(); auto& handler = *ensure(g_fxo->try_get<MouseHandlerBase>());
std::lock_guard mouse_lock(handler.mutex); std::lock_guard mouse_lock(handler.mutex);
// Make sure that the mouse handler is initialized // Make sure that the mouse handler is initialized
@ -522,7 +522,7 @@ public:
#ifdef HAVE_LIBEVDEV #ifdef HAVE_LIBEVDEV
case move_handler::gun: case move_handler::gun:
{ {
gun_thread& gun = g_fxo->get<gun_thread>(); gun_thread& gun = *ensure(g_fxo->try_get<gun_thread>());
std::scoped_lock lock(gun.handler.mutex); std::scoped_lock lock(gun.handler.mutex);
gun.num_devices = gun.handler.init() ? gun.handler.get_num_guns() : 0; gun.num_devices = gun.handler.init() ? gun.handler.get_num_guns() : 0;
connected_controllers = std::min<u32>(std::min<u32>(attribute.max_connect, CELL_GEM_MAX_NUM), gun.num_devices); connected_controllers = std::min<u32>(std::min<u32>(attribute.max_connect, CELL_GEM_MAX_NUM), gun.num_devices);

View file

@ -837,7 +837,7 @@ namespace rsx
{ {
while (Emu.IsReady()) while (Emu.IsReady())
{ {
thread_ctrl::wait_for(1000); Emu.WaitReady();
} }
do do

View file

@ -221,13 +221,19 @@ void init_fxo_for_exec(utils::serial* ar, bool full = false)
Emu.ConfigurePPUCache(); Emu.ConfigurePPUCache();
stx::g_launch_retainer = 1;
g_fxo->init(false, ar, [](){ Emu.ExecPostponedInitCode(); }); g_fxo->init(false, ar, [](){ Emu.ExecPostponedInitCode(); });
Emu.GetCallbacks().init_gs_render(ar); Emu.GetCallbacks().init_gs_render(ar);
Emu.GetCallbacks().init_pad_handler(Emu.GetTitleID());
Emu.GetCallbacks().init_kb_handler(); Emu.GetCallbacks().init_kb_handler();
Emu.GetCallbacks().init_mouse_handler(); Emu.GetCallbacks().init_mouse_handler();
stx::g_launch_retainer = 0;
stx::g_launch_retainer.notify_all();
Emu.GetCallbacks().init_pad_handler(Emu.GetTitleID());
usz pos = 0; usz pos = 0;
if (ar) if (ar)
@ -859,6 +865,7 @@ bool Emulator::BootRsxCapture(const std::string& path)
GetCallbacks().on_run(false); GetCallbacks().on_run(false);
m_state = system_state::starting; m_state = system_state::starting;
m_state.notify_all();
ensure(g_fxo->init<named_thread<rsx::rsx_replay_thread>>("RSX Replay", std::move(frame))); ensure(g_fxo->init<named_thread<rsx::rsx_replay_thread>>("RSX Replay", std::move(frame)));
@ -2472,6 +2479,7 @@ void Emulator::Run(bool start_playtime)
rpcs3::utils::configure_logs(); rpcs3::utils::configure_logs();
m_state = system_state::starting; m_state = system_state::starting;
m_state.notify_all();
if (g_cfg.misc.prevent_display_sleep) if (g_cfg.misc.prevent_display_sleep)
{ {
@ -3254,6 +3262,7 @@ void Emulator::Kill(bool allow_autoexit, bool savestate, savestate_stage* save_s
} }
// Signal threads // Signal threads
m_state.notify_all();
if (auto rsx = g_fxo->try_get<rsx::thread>()) if (auto rsx = g_fxo->try_get<rsx::thread>())
{ {

View file

@ -439,6 +439,7 @@ public:
bool IsStopped(bool test_fully = false) const { return test_fully ? m_state == system_state::stopped : m_state <= system_state::stopping; } bool IsStopped(bool test_fully = false) const { return test_fully ? m_state == system_state::stopped : m_state <= system_state::stopping; }
bool IsReady() const { return m_state == system_state::ready; } bool IsReady() const { return m_state == system_state::ready; }
bool IsStarting() const { return m_state == system_state::starting; } bool IsStarting() const { return m_state == system_state::starting; }
void WaitReady() const { m_state.wait(system_state::ready); }
auto GetStatus(bool fixup = true) const { system_state state = m_state; return fixup && state == system_state::frozen ? system_state::paused : fixup && state == system_state::stopping ? system_state::stopped : state; } auto GetStatus(bool fixup = true) const { system_state state = m_state; return fixup && state == system_state::frozen ? system_state::paused : fixup && state == system_state::stopping ? system_state::stopped : state; }
bool HasGui() const { return m_has_gui; } bool HasGui() const { return m_has_gui; }