From 33b8924384bad254972a16dd511d148bdc481238 Mon Sep 17 00:00:00 2001 From: flightlessmango Date: Wed, 10 Apr 2024 09:44:59 +0200 Subject: [PATCH] config: add a mutex for config We want to wait to render before config has been fully loaded. Otherwise this can sometimes cause a crash when we access config options while they are being assigned. --- src/overlay.cpp | 2 ++ src/overlay_params.cpp | 9 +++++++++ src/overlay_params.h | 6 +++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/overlay.cpp b/src/overlay.cpp index 7ae2fae4..311dac4b 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -666,6 +666,8 @@ void horizontal_separator(struct overlay_params& params) { void render_imgui(swapchain_stats& data, struct overlay_params& params, ImVec2& window_size, bool is_vulkan) { + std::unique_lock lock(config_mtx); + config_cv.wait(lock, []{ return config_ready; }); // data.engine = EngineTypes::GAMESCOPE; HUDElements.sw_stats = &data; HUDElements.params = ¶ms; HUDElements.is_vulkan = is_vulkan; diff --git a/src/overlay_params.cpp b/src/overlay_params.cpp index e6fa28a8..eb1d4c18 100644 --- a/src/overlay_params.cpp +++ b/src/overlay_params.cpp @@ -41,6 +41,9 @@ #include "fps_metrics.h" std::unique_ptr fpsmetrics; +std::mutex config_mtx; +std::condition_variable config_cv; +bool config_ready = false; #if __cplusplus >= 201703L @@ -1001,6 +1004,12 @@ parse_overlay_config(struct overlay_params *params, #endif if (HUDElements.net) HUDElements.net->should_reset = true; + + { + std::lock_guard lock(config_mtx); + config_ready = true; + config_cv.notify_one(); + } } bool parse_preset_config(int preset, struct overlay_params *params){ diff --git a/src/overlay_params.h b/src/overlay_params.h index bfebeb9c..dbf26838 100644 --- a/src/overlay_params.h +++ b/src/overlay_params.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include #ifdef __cplusplus extern "C" { @@ -330,9 +332,11 @@ void parse_overlay_config(struct overlay_params *params, void presets(int preset, struct overlay_params *params, bool inherit=false); bool parse_preset_config(int preset, struct overlay_params *params); void add_to_options(struct overlay_params *params, std::string option, std::string value); - #ifdef __cplusplus } #endif +extern std::mutex config_mtx; +extern std::condition_variable config_cv; +extern bool config_ready; #endif /* MANGOHUD_OVERLAY_PARAMS_H */