From 112e2429036973819f801054891ab7b59da4b0d1 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Wed, 3 Jan 2024 17:58:57 +0100 Subject: [PATCH] fix options being reset when gc thread is gcing --- src/support/include/Logger.h | 3 +++ src/support/include/Options.h | 4 +++- src/support/src/Logger.cpp | 22 ++++++++++++++++++---- src/support/src/Options.cpp | 9 ++++++++- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/support/include/Logger.h b/src/support/include/Logger.h index 79105ef..793898f 100644 --- a/src/support/include/Logger.h +++ b/src/support/include/Logger.h @@ -8,7 +8,9 @@ #include #include #include +#include #include +#include #include #include @@ -40,6 +42,7 @@ private: std::chrono::time_point _start_time = std::chrono::high_resolution_clock::now(); std::reference_wrapper _out = std::cout; std::reference_wrapper _out_err = std::cerr; + std::shared_mutex _mutex; }; diff --git a/src/support/include/Options.h b/src/support/include/Options.h index 95e6a6c..a415a57 100644 --- a/src/support/include/Options.h +++ b/src/support/include/Options.h @@ -6,10 +6,11 @@ #define PSIL_OPTIONS_H +#include +#include #include #include #include - class Options { public: static bool get_bool(const std::string &opt); @@ -25,6 +26,7 @@ private: {"default_log_level", 1U}}; std::unordered_map> _current = _defaults; + std::shared_mutex _mutex; }; diff --git a/src/support/src/Logger.cpp b/src/support/src/Logger.cpp index 99f87c1..3001aa6 100644 --- a/src/support/src/Logger.cpp +++ b/src/support/src/Logger.cpp @@ -14,6 +14,7 @@ Logger &Logger::get() { } void Logger::log(const std::string &tag, const std::string &what, int level) { + std::shared_lock l(get()._mutex); int en_level = Options::get_int("default_log_level"); if (get()._levels.find(tag) != get()._levels.end()) en_level = get()._levels.at(tag); @@ -31,6 +32,7 @@ void Logger::log(const std::string &tag, const std::string &what, int level) { } void Logger::log(const std::string &tag, const std::function &fn, int level) { + std::shared_lock l(get()._mutex); int en_level = Options::get_int("default_log_level"); if (get()._levels.find(tag) != get()._levels.end()) en_level = get()._levels.at(tag); @@ -41,7 +43,19 @@ void Logger::log(const std::string &tag, const std::function(_defaults.at(opt))) throw std::invalid_argument("Bad option type " + opt); return std::get(o._current.at(opt)); } size_t Options::get_int(const std::string &opt) { Options &o = get(); + std::shared_lock l(o._mutex); if (_defaults.find(opt) == _defaults.end()) throw std::invalid_argument("Unknown option " + opt); if (!std::holds_alternative(_defaults.at(opt))) throw std::invalid_argument("Bad option type " + opt); return std::get(o._current.at(opt)); } void Options::set_bool(const std::string &opt, bool val) { Options &o = get(); + std::lock_guard l(o._mutex); if (_defaults.find(opt) == _defaults.end()) throw std::invalid_argument("Unknown option " + opt); if (!std::holds_alternative(_defaults.at(opt))) throw std::invalid_argument("Bad option type " + opt); o._current[opt] = val; } void Options::set_int(const std::string &opt, size_t val) { Options &o = get(); + std::lock_guard l(o._mutex); if (_defaults.find(opt) == _defaults.end()) throw std::invalid_argument("Unknown option " + opt); if (!std::holds_alternative(_defaults.at(opt))) throw std::invalid_argument("Bad option type " + opt); o._current[opt] = val; } -void Options::reset() { get()._current = _defaults; } +void Options::reset() { + std::lock_guard l(get()._mutex); + get()._current = _defaults; +}