From d68a63e40439850cfc66f8525b35986a6371fa47 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Sun, 29 Nov 2015 13:02:01 +0000 Subject: [PATCH] wallet: cancellable refresh ^C while in manual refresh will cancel the refresh, since that's often an annoying thing to have to wait for. Also, a manual refresh command will interrupt any running background refresh and take over, rather than wait for the background refresh to be done, and look to be hanging. --- src/common/util.cpp | 2 +- src/common/util.h | 12 +++---- src/simplewallet/simplewallet.cpp | 54 +++++++++++++++++++++++++++---- src/simplewallet/simplewallet.h | 3 ++ src/wallet/wallet2.cpp | 1 + 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/src/common/util.cpp b/src/common/util.cpp index 4c1b44004..371b55add 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -48,7 +48,7 @@ using namespace epee; namespace tools { - std::function signal_handler::m_handler; + std::function signal_handler::m_handler; #ifdef WIN32 std::string get_windows_version_display_string() diff --git a/src/common/util.h b/src/common/util.h index 236a0b6f0..937f7e44d 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -130,7 +130,7 @@ namespace tools { if (CTRL_C_EVENT == type || CTRL_BREAK_EVENT == type) { - handle_signal(); + handle_signal(type); } else { @@ -141,21 +141,21 @@ namespace tools } #else /*! \brief handler for NIX */ - static void posix_handler(int /*type*/) + static void posix_handler(int type) { - handle_signal(); + handle_signal(type); } #endif /*! \brief calles m_handler */ - static void handle_signal() + static void handle_signal(int type) { static std::mutex m_mutex; std::unique_lock lock(m_mutex); - m_handler(); + m_handler(type); } /*! \brief where the installed handler is stored */ - static std::function m_handler; + static std::function m_handler; }; } diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index f44eb33d0..1e8d02cec 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -468,6 +468,7 @@ simple_wallet::simple_wallet() , m_refresh_progress_reporter(*this) , m_auto_refresh_run(false) , m_auto_refresh_refreshing(false) + , m_in_manual_refresh(false) { m_cmd_binder.set_handler("start_mining", boost::bind(&simple_wallet::start_mining, this, _1), tr("start_mining [] - Start mining in daemon")); m_cmd_binder.set_handler("stop_mining", boost::bind(&simple_wallet::stop_mining, this, _1), tr("Stop mining in daemon")); @@ -1071,7 +1072,6 @@ bool simple_wallet::open_wallet(const string &wallet_file, const std::string& pa m_wallet->init(m_daemon_address); - refresh(std::vector()); success_msg_writer() << "**********************************************************************\n" << tr("Use \"help\" command to see the list of available commands.\n") << @@ -1084,6 +1084,11 @@ bool simple_wallet::close_wallet() if (m_auto_refresh_run.load(std::memory_order_relaxed)) { m_auto_refresh_run.store(false, std::memory_order_relaxed); + m_wallet->stop(); + { + std::unique_lock lock(m_auto_refresh_mutex); + m_auto_refresh_cond.notify_one(); + } m_auto_refresh_thread.join(); } @@ -1276,11 +1281,16 @@ void simple_wallet::on_skip_transaction(uint64_t height, const cryptonote::trans //---------------------------------------------------------------------------------------------------- bool simple_wallet::refresh(const std::vector& args) { - std::unique_lock lock(m_auto_refresh_mutex); - if (!try_connect_to_daemon()) return true; + bool auto_refresh_run = m_auto_refresh_run.load(std::memory_order_relaxed); + m_auto_refresh_run.store(false, std::memory_order_relaxed); + // stop any background refresh, and take over + m_wallet->stop(); + std::unique_lock lock(m_auto_refresh_mutex); + m_auto_refresh_cond.notify_one(); + message_writer() << tr("Starting refresh..."); uint64_t fetched_blocks = 0; @@ -1300,7 +1310,9 @@ bool simple_wallet::refresh(const std::vector& args) std::ostringstream ss; try { + m_in_manual_refresh.store(true, std::memory_order_relaxed); m_wallet->refresh(start_height, fetched_blocks); + m_in_manual_refresh.store(false, std::memory_order_relaxed); ok = true; // Clear line "Height xxx of xxx" std::cout << "\r \r"; @@ -1346,6 +1358,8 @@ bool simple_wallet::refresh(const std::vector& args) fail_msg_writer() << tr("refresh failed: ") << ss.str() << ". " << tr("Blocks received: ") << fetched_blocks; } + m_in_manual_refresh.store(false, std::memory_order_relaxed); + m_auto_refresh_run.store(auto_refresh_run, std::memory_order_relaxed); return true; } //---------------------------------------------------------------------------------------------------- @@ -2222,6 +2236,8 @@ void simple_wallet::wallet_refresh_thread() while (true) { std::unique_lock lock(m_auto_refresh_mutex); + if (!m_auto_refresh_run.load(std::memory_order_relaxed)) + break; m_auto_refresh_refreshing = true; try { @@ -2230,9 +2246,9 @@ void simple_wallet::wallet_refresh_thread() } catch(...) {} m_auto_refresh_refreshing = false; - m_auto_refresh_cond.wait_for(lock, chrono::seconds(90)); if (!m_auto_refresh_run.load(std::memory_order_relaxed)) break; + m_auto_refresh_cond.wait_for(lock, chrono::seconds(90)); } } //---------------------------------------------------------------------------------------------------- @@ -2244,6 +2260,10 @@ bool simple_wallet::run() { m_auto_refresh_thread = std::thread([&]{wallet_refresh_thread();}); } + else + { + refresh(std::vector()); + } return m_cmd_binder.run_handling(std::string("[") + tr("wallet") + " " + addr_start + "]: ", ""); } //---------------------------------------------------------------------------------------------------- @@ -2306,6 +2326,18 @@ bool simple_wallet::process_command(const std::vector &args) return m_cmd_binder.process_command_vec(args); } //---------------------------------------------------------------------------------------------------- +void simple_wallet::interrupt() +{ + if (m_in_manual_refresh.load(std::memory_order_relaxed)) + { + m_wallet->stop(); + } + else + { + stop(); + } +} +//---------------------------------------------------------------------------------------------------- int main(int argc, char* argv[]) { #ifdef WIN32 @@ -2480,7 +2512,7 @@ int main(int argc, char* argv[]) bool r = wrpc.init(vm); CHECK_AND_ASSERT_MES(r, 1, sw::tr("Failed to initialize wallet rpc server")); - tools::signal_handler::install([&wrpc, &wal] { + tools::signal_handler::install([&wrpc, &wal](int) { wrpc.send_stop_signal(); wal.store(); }); @@ -2513,8 +2545,16 @@ int main(int argc, char* argv[]) } else { - tools::signal_handler::install([&w] { - w.stop(); + tools::signal_handler::install([&w](int type) { + if (type == SIGINT) + { + // if we're pressing ^C when refreshing, just stop refreshing + w.interrupt(); + } + else + { + w.stop(); + } }); w.run(); diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index be510ffb2..dd1e59d6c 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -68,6 +68,7 @@ namespace cryptonote bool deinit(); bool run(); void stop(); + void interrupt(); //wallet *create_wallet(); bool process_command(const std::vector &args); @@ -134,6 +135,7 @@ namespace cryptonote uint64_t get_daemon_blockchain_height(std::string& err); bool try_connect_to_daemon(); bool ask_wallet_create_if_needed(); + /*! * \brief Prints the seed with a nice message * \param seed seed to print @@ -238,5 +240,6 @@ namespace cryptonote std::thread m_auto_refresh_thread; std::mutex m_auto_refresh_mutex; std::condition_variable m_auto_refresh_cond; + std::atomic m_in_manual_refresh; }; } diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index c4df78ba9..ba7fe93b3 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -680,6 +680,7 @@ void wallet2::refresh(uint64_t start_height, uint64_t & blocks_fetched, bool& re get_short_chain_history(short_chain_history); pull_blocks(start_height, blocks_start_height, short_chain_history, blocks); + m_run.store(true, std::memory_order_relaxed); while(m_run.load(std::memory_order_relaxed)) { try