From 07a1cde8f94c306921a26c312f581b3102f2ea7d Mon Sep 17 00:00:00 2001 From: cryptozoidberg Date: Tue, 19 May 2020 16:28:50 +0200 Subject: [PATCH] optimized shutdown process for wallets manager --- src/wallet/wallets_manager.cpp | 74 +++++++++++++++++++++++++--------- src/wallet/wallets_manager.h | 6 ++- 2 files changed, 61 insertions(+), 19 deletions(-) diff --git a/src/wallet/wallets_manager.cpp b/src/wallet/wallets_manager.cpp index 4db0a77c..26459184 100644 --- a/src/wallet/wallets_manager.cpp +++ b/src/wallet/wallets_manager.cpp @@ -4,7 +4,7 @@ // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. - +#include #include "wallets_manager.h" #include "currency_core/alias_helper.h" #ifndef MOBILE_WALLET_BUILD @@ -33,7 +33,12 @@ return API_RETURN_CODE_WALLET_WRONG_ID; \ auto& name = it->second.w; -#define DAEMON_IDLE_UPDATE_TIME_MS 2000 +#ifdef MOBILE_WALLET_BUILD + #define DAEMON_IDLE_UPDATE_TIME_MS 10000 +#else + #define DAEMON_IDLE_UPDATE_TIME_MS 2000 +#endif + #define HTTP_PROXY_TIMEOUT 2000 #define HTTP_PROXY_ATTEMPTS_COUNT 1 @@ -282,13 +287,6 @@ bool wallets_manager::start() CATCH_ENTRY_L0("main", false); } - - -bool wallets_manager::send_stop_signal() -{ - m_stop_singal_sent = true; - return true; -} bool wallets_manager::stop() @@ -504,6 +502,25 @@ void wallets_manager::main_worker(const po::variables_map& m_vm) SHARED_CRITICAL_REGION_BEGIN(m_wallets_lock); + //first send stop signal to all wallets + for (auto& wo : m_wallets) + { + try + { + wo.second.stop(false); + } + catch (const std::exception& e) + { + LOG_ERROR("Exception rised at storing wallet: " << e.what()); + continue; + } + catch (...) + { + LOG_ERROR("Exception rised at storing wallet"); + continue; + } + } + for (auto& wo : m_wallets) { LOG_PRINT_L0("Storing wallet data..."); @@ -511,10 +528,7 @@ void wallets_manager::main_worker(const po::variables_map& m_vm) //m_pview->update_daemon_status(dsi); try { - wo.second.major_stop = true; - wo.second.stop_for_refresh = true; wo.second.stop(); - if(!dont_save_wallet_at_stop) wo.second.w->get()->store(); } @@ -613,13 +627,34 @@ void wallets_manager::toggle_pos_mining() //update_wallets_info(); } +bool wallets_manager::send_stop_signal() +{ + m_stop_singal_sent = true; + m_stop_singal_sent_mutex_cv.notify_one(); + return true; +} + + void wallets_manager::loop() { - while(!m_stop_singal_sent) + + while (!m_stop_singal_sent) { - update_state_info(); - std::this_thread::sleep_for(std::chrono::milliseconds(DAEMON_IDLE_UPDATE_TIME_MS)); + { + std::unique_lock lk(m_stop_singal_sent_mutex); + m_stop_singal_sent_mutex_cv.wait_for(lk, std::chrono::microseconds(DAEMON_IDLE_UPDATE_TIME_MS), [&] {return m_stop_singal_sent.load(); }); + } + if (!m_stop_singal_sent) + { + update_state_info(); + } + } +// while(!m_stop_singal_sent) +// { +// update_state_info(); +// std::this_thread::sleep_for(std::chrono::milliseconds(DAEMON_IDLE_UPDATE_TIME_MS)); +// } } void wallets_manager::init_wallet_entry(wallet_vs_options& wo, uint64_t id) @@ -1824,15 +1859,18 @@ void wallets_manager::wallet_vs_options::worker_func() } LOG_PRINT_GREEN("[WALLET_HANDLER] Wallet thread thread stopped", LOG_LEVEL_0); } -void wallets_manager::wallet_vs_options::stop() +void wallets_manager::wallet_vs_options::stop(bool wait) { w.unlocked_get()->stop(); do_mining = false; major_stop = true; stop_for_refresh = true; break_mining_loop = true; - if (miner_thread.joinable()) - miner_thread.join(); + if (wait) + { + if (miner_thread.joinable()) + miner_thread.join(); + } } wallets_manager::wallet_vs_options::~wallet_vs_options() { diff --git a/src/wallet/wallets_manager.h b/src/wallet/wallets_manager.h index 0d75447c..5b545f32 100644 --- a/src/wallet/wallets_manager.h +++ b/src/wallet/wallets_manager.h @@ -80,7 +80,7 @@ public: std::thread miner_thread; void worker_func(); - void stop(); + void stop(bool wait = true); 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(); }; @@ -174,7 +174,11 @@ private: virtual void on_transfer_canceled(size_t wallet_id, const tools::wallet_public::wallet_transfer_info& wti); std::thread m_main_worker_thread; + std::atomic m_stop_singal_sent; + std::mutex m_stop_singal_sent_mutex; + std::condition_variable m_stop_singal_sent_mutex_cv; + view::i_view m_view_stub; view::i_view* m_pview; std::shared_ptr m_rpc_proxy;