Core, VideoCommon: Fix crash at shutdown due to destructor ordering

Previously, PerformanceTracker registered a callback to be updated on
emulation state changes. PerformanceTrackers live in a global variable
(g_perf_metrics) within libvideocommon. The callback was stored in a
global variable in libcore. This created a race condition at shutdown
between these libraries, when the PerfTracker's destructor tried to
unregister the callback.
Notify the PerfTracker directly from libcore, without callbacks, since
Core.cpp already references g_perf_metrics explicitly. Also rename
Core::CallOnStateChangedCallbacks to NotifyStateChanged to better
reflect what it's doing.
This commit is contained in:
Mihai Brodschi
2025-04-27 17:33:23 +03:00
parent 8ee64a84c7
commit bad78cfed4
8 changed files with 25 additions and 20 deletions

View File

@ -35,6 +35,12 @@ void PerformanceMetrics::CountVBlank()
m_vps_counter.Count();
}
void PerformanceMetrics::OnEmulationStateChanged([[maybe_unused]] Core::State state)
{
m_fps_counter.InvalidateLastTime();
m_vps_counter.InvalidateLastTime();
}
void PerformanceMetrics::CountThrottleSleep(DT sleep)
{
m_time_sleeping += sleep;

View File

@ -7,6 +7,7 @@
#include <deque>
#include "Common/CommonTypes.h"
#include "Core/Core.h"
#include "VideoCommon/PerformanceTracker.h"
namespace Core
@ -29,6 +30,7 @@ public:
void CountFrame();
void CountVBlank();
void OnEmulationStateChanged(Core::State state);
// Call from CPU thread.
void CountThrottleSleep(DT sleep);

View File

@ -23,17 +23,9 @@ PerformanceTracker::PerformanceTracker(const std::optional<std::string> log_name
const std::optional<DT> sample_window_duration)
: m_log_name{log_name}, m_sample_window_duration{sample_window_duration}
{
m_on_state_changed_handle =
Core::AddOnStateChangedCallback([this](Core::State state) { m_is_last_time_sane = false; });
Reset();
}
PerformanceTracker::~PerformanceTracker()
{
Core::RemoveOnStateChangedCallback(&m_on_state_changed_handle);
}
void PerformanceTracker::Reset()
{
m_raw_dts.Clear();
@ -135,6 +127,11 @@ DT PerformanceTracker::GetLastRawDt() const
return m_last_raw_dt;
}
void PerformanceTracker::InvalidateLastTime()
{
m_is_last_time_sane = false;
}
void PerformanceTracker::ImPlotPlotLines(const char* label) const
{
// "quality" graph uses twice as many points.

View File

@ -16,7 +16,7 @@ class PerformanceTracker
public:
PerformanceTracker(const std::optional<std::string> log_name = std::nullopt,
const std::optional<DT> sample_window_duration = std::nullopt);
~PerformanceTracker();
~PerformanceTracker() = default;
PerformanceTracker(const PerformanceTracker&) = delete;
PerformanceTracker& operator=(const PerformanceTracker&) = delete;
@ -39,6 +39,7 @@ public:
DT GetDtAvg() const;
DT GetDtStd() const;
DT GetLastRawDt() const;
void InvalidateLastTime();
private:
void LogRenderTimeToFile(DT val);
@ -47,8 +48,6 @@ private:
void PushFront(DT value);
void PopBack();
int m_on_state_changed_handle;
// Name of log file and file stream
std::optional<std::string> m_log_name;
std::ofstream m_bench_file;