diff --git a/contrib/epee/include/misc_log_ex.h b/contrib/epee/include/misc_log_ex.h index f1d846ad..37b2400f 100644 --- a/contrib/epee/include/misc_log_ex.h +++ b/contrib/epee/include/misc_log_ex.h @@ -1205,7 +1205,7 @@ namespace log_space //get_enabled_channels not thread-safe, at the moment leave it like this because it's configured in main, before other threads started static std::set& get_enabled_channels() { - static std::set genabled_channels; + static epee::static_helpers::wrapper> genabled_channels; return genabled_channels; } diff --git a/contrib/epee/include/singleton.h b/contrib/epee/include/singleton.h index f510c9a6..a770ab28 100644 --- a/contrib/epee/include/singleton.h +++ b/contrib/epee/include/singleton.h @@ -31,7 +31,7 @@ #pragma once #include - +#include "static_helpers.h" template class abstract_singleton @@ -39,7 +39,7 @@ class abstract_singleton static std::shared_ptr get_set_instance_internal(bool is_need_set = false, owned_object_t* pnew_obj = nullptr) { - static std::shared_ptr val_pobj; + static epee::static_helpers::wrapper> val_pobj; if (is_need_set) val_pobj.reset(pnew_obj); diff --git a/contrib/epee/include/static_helpers.h b/contrib/epee/include/static_helpers.h index 1a3dce05..bba92d51 100644 --- a/contrib/epee/include/static_helpers.h +++ b/contrib/epee/include/static_helpers.h @@ -33,6 +33,10 @@ #include #include "include_base_utils.h" #include "auto_val_init.h" + +#define DEFINE_SECURE_STATIC_VAR(type, var) static epee::static_helpers::wrapper var##inst; \ + static type& var = var##inst; + namespace epee { namespace static_helpers diff --git a/src/common/util.h b/src/common/util.h index 87138ef0..1f31d263 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -259,14 +259,14 @@ namespace tools static void handle_signal() { - static std::mutex m_mutex; + static epee::static_helpers::wrapper m_mutex; std::unique_lock lock(m_mutex); m_handler(); } static void handle_fatal_signal(int sig_number, void* address) { - static std::mutex m_mutex_fatal; + static epee::static_helpers::wrapper m_mutex_fatal; std::unique_lock lock(m_mutex_fatal); m_fatal_handler(sig_number, address); uninstall_fatal(); diff --git a/src/wallet/plain_wallet_api.cpp b/src/wallet/plain_wallet_api.cpp index 82880631..fbb1c477 100644 --- a/src/wallet/plain_wallet_api.cpp +++ b/src/wallet/plain_wallet_api.cpp @@ -83,7 +83,7 @@ namespace plain_wallet std::string get_set_working_dir(bool need_to_set = false, const std::string val = "") { - static std::string working_dir; + DEFINE_SECURE_STATIC_VAR(std::string, working_dir); if (need_to_set) working_dir = val; return working_dir; diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 06adc3d6..4db0a77c 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -513,7 +513,7 @@ void wallets_manager::main_worker(const po::variables_map& m_vm) { wo.second.major_stop = true; wo.second.stop_for_refresh = true; - wo.second.w.unlocked_get()->stop(); + wo.second.stop(); if(!dont_save_wallet_at_stop) wo.second.w->get()->store(); @@ -1824,8 +1824,9 @@ void wallets_manager::wallet_vs_options::worker_func() } LOG_PRINT_GREEN("[WALLET_HANDLER] Wallet thread thread stopped", LOG_LEVEL_0); } -wallets_manager::wallet_vs_options::~wallet_vs_options() +void wallets_manager::wallet_vs_options::stop() { + w.unlocked_get()->stop(); do_mining = false; major_stop = true; stop_for_refresh = true; @@ -1833,6 +1834,10 @@ wallets_manager::wallet_vs_options::~wallet_vs_options() if (miner_thread.joinable()) miner_thread.join(); } +wallets_manager::wallet_vs_options::~wallet_vs_options() +{ + stop(); +} std::string wallets_manager::get_wallet_log_prefix(size_t wallet_id) const { diff --git a/src/wallet/wallets_manager.h b/src/wallet/wallets_manager.h index aa900d93..0d75447c 100644 --- a/src/wallet/wallets_manager.h +++ b/src/wallet/wallets_manager.h @@ -80,6 +80,7 @@ public: std::thread miner_thread; void worker_func(); + void stop(); std::string get_log_prefix() const { return std::string("[") + epee::string_tools::num_to_string_fast(wallet_id) + ":" + w->get()->get_log_prefix() + "]"; } ~wallet_vs_options(); };