From bf93f9f9874df43c165c02a79b862c88793b0c84 Mon Sep 17 00:00:00 2001 From: Eladash Date: Tue, 22 Aug 2023 07:18:44 +0300 Subject: [PATCH] Progress Dialog: Fix race that could lead to ever-inaccurate results --- rpcs3/Emu/system_progress.cpp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/rpcs3/Emu/system_progress.cpp b/rpcs3/Emu/system_progress.cpp index 522dbc79fe..9c868b497b 100644 --- a/rpcs3/Emu/system_progress.cpp +++ b/rpcs3/Emu/system_progress.cpp @@ -120,12 +120,22 @@ void progress_dialog_server::operator()() // Update progress while (!g_system_progress_stopping && thread_ctrl::state() != thread_state::aborting) { - const auto text_new = +g_progr.load(); + auto whole_state = std::make_tuple(+g_progr.load(), +g_progr_ftotal, +g_progr_fdone, +g_progr_ptotal, +g_progr_pdone); - const u32 ftotal_new = g_progr_ftotal; - const u32 fdone_new = g_progr_fdone; - const u32 ptotal_new = g_progr_ptotal; - const u32 pdone_new = g_progr_pdone; + while (true) + { + const auto new_state = std::make_tuple(+g_progr.load(), +g_progr_ftotal, +g_progr_fdone, +g_progr_ptotal, +g_progr_pdone); + + if (new_state == whole_state) + { + // Only leave while it has a complete (atomic) state + break; + } + + whole_state = new_state; + } + + const auto [text_new, ftotal_new, fdone_new, ptotal_new, pdone_new] = whole_state; if (ftotal != ftotal_new || fdone != fdone_new || ptotal != ptotal_new || pdone != pdone_new || text_new != text1) { @@ -137,8 +147,8 @@ void progress_dialog_server::operator()() if (!text_new) { - // Close dialog - break; + // Incomplete state + continue; } if (show_overlay_message) @@ -182,6 +192,12 @@ void progress_dialog_server::operator()() }); } } + // Leave only if total count is equal to done count + else if (ftotal == fdone && ptotal == pdone && !text_new) + { + // Complete state, empty message: close dialog + break; + } if (show_overlay_message) {