diff --git a/tests/unit/engine_stubs.cpp b/tests/unit/engine_stubs.cpp index 52a503ac5..fae34c5da 100644 --- a/tests/unit/engine_stubs.cpp +++ b/tests/unit/engine_stubs.cpp @@ -19,9 +19,12 @@ // settings --------------------------------------------------------------------------------------- -global_settings &GetGlobalSettings() { +std::atomic global_settings_detail::instance{nullptr}; + +global_settings &global_settings_detail::make() { // defaults, as before reading eu07.ini static global_settings settings; + instance.store(&settings, std::memory_order_release); return settings; } diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 4461e8278..260dad8e8 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -2966,8 +2966,11 @@ template <> void global_settings::export_as_text(std::ostream &Output, std::stri Output << Key << " " << (Value ? "yes" : "no") << "\n"; } -global_settings &GetGlobalSettings() +std::atomic global_settings_detail::instance{nullptr}; + +global_settings &global_settings_detail::make() { static global_settings global{}; + instance.store(&global, std::memory_order_release); return global; } diff --git a/utilities/Globals.h b/utilities/Globals.h index 0a47cd13d..9da132b8d 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "rendering/light.h" #include "utilities/utilities.h" #include "utilities/motiontelemetry.h" +#include #include #include @@ -874,6 +875,21 @@ inline std::string_view name_of( global_settings::derailment_model const Model ) } } -extern global_settings& GetGlobalSettings(); +// the settings are made on the first call of GetGlobalSettings(), from the static objects of the other files too, whatever +// the order they're made in. after that a pointer to them is kept, which the calls read: the guard of a function-local +// static took a call and a look at the thread's storage for each use of Global, many times in the step of every vehicle +namespace global_settings_detail +{ +// zero until the settings are made: set before any other thread starts +extern std::atomic instance; +// makes the settings on the first call, keeps the pointer to them +global_settings &make(); +} // namespace global_settings_detail + +inline global_settings &GetGlobalSettings() +{ + auto *const instance{global_settings_detail::instance.load(std::memory_order_acquire)}; + return instance != nullptr ? *instance : global_settings_detail::make(); +} #define Global (GetGlobalSettings())