Skip to content

Commit

Permalink
Fix Emulator::IsPaused() to allow measurements during module compilation
Browse files Browse the repository at this point in the history
Also fix a potential deadlock in access violation handler for non-cpu_thread
  • Loading branch information
elad335 committed Jan 23, 2025
1 parent 6209c1e commit 3c3b3ba
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 16 deletions.
49 changes: 42 additions & 7 deletions Utilities/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1703,23 +1703,58 @@ bool handle_access_violation(u32 addr, bool is_writing, ucontext_t* context) noe
cpu->state += cpu_flag::wait;
}

Emu.Pause(true);

// Note: a thread may access violate more than once after hack_alloc recovery
// Do not log any further access violations in this case.
if (!g_tls_access_violation_recovered)
{
vm_log.notice("\n%s", dump_useful_thread_info());
vm_log.fatal("Access violation %s location 0x%x (%s)", is_writing ? "writing" : (cpu && cpu->get_class() == thread_class::ppu && cpu->get_pc() == addr ? "executing" : "reading"), addr, (is_writing && vm::check_addr(addr)) ? "read-only memory" : "unmapped memory");
}

// Note: a thread may access violate more than once after hack_alloc recovery
// Do not log any further access violations in this case.
if (!g_tls_access_violation_recovered)
while (Emu.IsPausedOrReady())
{
vm_log.fatal("Access violation %s location 0x%x (%s)", is_writing ? "writing" : (cpu && cpu->get_class() == thread_class::ppu && cpu->get_pc() == addr ? "executing" : "reading"), addr, (is_writing && vm::check_addr(addr)) ? "read-only memory" : "unmapped memory");
if (cpu)
{
auto state = +cpu->state;

if (::is_paused(state) && !::is_stopped(state))
{
thread_ctrl::wait_on(cpu->state, state);
}
else
{
// Temporary until Emulator updates state
std::this_thread::yield();
}
}
else
{
thread_ctrl::wait_for(1000);
}
}

Emu.Pause(true);

while (Emu.IsPaused())
{
thread_ctrl::wait();
if (cpu)
{
auto state = +cpu->state;

if (::is_paused(state) && !::is_stopped(state))
{
thread_ctrl::wait_on(cpu->state, state);
}
else
{
// Temporary until Emulator updates state
std::this_thread::yield();
}
}
else
{
thread_ctrl::wait_for(1000);
}
}

if (Emu.IsStopped() && !hack_alloc())
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/Cell/Modules/cellAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ void cell_audio_thread::operator()()

thread_ctrl::scoped_priority high_prio(+1);

while (Emu.IsPaused())
while (Emu.IsPausedOrReady())
{
thread_ctrl::wait_for(5000);
}
Expand Down
4 changes: 2 additions & 2 deletions rpcs3/Emu/Cell/Modules/cellCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1640,14 +1640,14 @@ void camera_context::operator()()
while (thread_ctrl::state() != thread_state::aborting && !Emu.IsStopped())
{
// send ATTACH event
if (init && is_attached_dirty && !Emu.IsPaused())
if (init && is_attached_dirty && !Emu.IsPausedOrReady())
{
send_attach_state(is_attached);
}

const s32 fps = info.framerate;

if (!init || !fps || Emu.IsPaused() || g_cfg.io.camera == camera_handler::null)
if (!init || !fps || Emu.IsPausedOrReady() || g_cfg.io.camera == camera_handler::null)
{
thread_ctrl::wait_for(1000); // hack
continue;
Expand Down
4 changes: 2 additions & 2 deletions rpcs3/Emu/Cell/lv2/sys_rsxaudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ void rsxaudio_data_thread::extract_audio_data()
return rsxaudio_obj_ptr;
}();

if (Emu.IsPaused() || !rsxaudio_obj)
if (Emu.IsPausedOrReady() || !rsxaudio_obj)
{
advance_all_timers();
return;
Expand Down Expand Up @@ -1533,7 +1533,7 @@ void rsxaudio_backend_thread::operator()()
backend_failed = false;
}

if (!Emu.IsPaused() || !use_aux_ringbuf) // Don't pause if thread is in direct mode
if (!Emu.IsPausedOrReady() || !use_aux_ringbuf) // Don't pause if thread is in direct mode
{
if (!backend_playing())
{
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/Cell/lv2/sys_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void lv2_timer_thread::operator()()

sleep_time = umax;

if (Emu.IsPaused())
if (Emu.IsPausedOrReady())
{
sleep_time = 10000;
continue;
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/Cell/lv2/sys_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1968,7 +1968,7 @@ void vuart_av_thread::operator()()
{
while (thread_ctrl::state() != thread_state::aborting)
{
if (Emu.IsPaused())
if (Emu.IsPausedOrReady())
{
thread_ctrl::wait_for(5000);
continue;
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/Emu/GDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ bool gdb_thread::cmd_vcont(gdb_cmd& cmd)
{
Emu.Run(true);
}
if (Emu.IsPaused())
else if (Emu.IsPaused())
{
Emu.Resume();
}
Expand Down
3 changes: 2 additions & 1 deletion rpcs3/Emu/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ class Emulator final
static void CleanUp();

bool IsRunning() const { return m_state == system_state::running; }
bool IsPaused() const { return m_state >= system_state::paused; } // ready/starting are also considered paused by this function
bool IsPaused() const { system_state state = m_state; return state >= system_state::paused && state <= system_state::frozen; }
bool IsPausedOrReady() const { return m_state >= system_state::paused; }
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 IsStarting() const { return m_state == system_state::starting; }
Expand Down

0 comments on commit 3c3b3ba

Please sign in to comment.