From b60d8da400c89bd4a2feea9304e202c89c7c164a Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 26 Jul 2019 12:19:47 -0400 Subject: [PATCH] setAutoSync() -> startSyncing() stopSyncing() --- src/wallet/MoneroWallet.cpp | 57 ++++++++++++++++++++----------------- src/wallet/MoneroWallet.h | 23 ++++++++------- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index f3a45ae..6cfdecf 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -1051,17 +1051,22 @@ namespace monero { return lockAndSync(startHeight, listener); } - void MoneroWallet::setAutoSync(bool autoSync) { - cout << "setAutoSync(" << autoSync << ")" << endl; - if (autoSync) { - if (!autoSyncEnabled) { - autoSyncEnabled = true; - syncCV.notify_one(); - } - } else { - if (!autoSyncThreadDone) { - autoSyncEnabled = false; - } + /** + * Start automatic syncing as its own thread. + */ + void MoneroWallet::startSyncing() { + if (!syncingEnabled) { + syncingEnabled = true; + syncCV.notify_one(); + } + } + + /** + * Stop automatic syncing as its own thread. + */ + void MoneroWallet::stopSyncing() { + if (!syncingThreadDone) { + syncingEnabled = false; } } @@ -2289,10 +2294,10 @@ namespace monero { void MoneroWallet::close() { cout << "close()" << endl; - autoSyncEnabled = false; - autoSyncThreadDone = true; + syncingEnabled = false; + syncingThreadDone = true; syncCV.notify_one(); - autoSyncThread.join(); + syncingThread.join(); wallet2->stop(); wallet2->deinit(); } @@ -2305,28 +2310,28 @@ namespace monero { if (getDaemonConnection() == nullptr) isConnected = false; isSynced = false; rescanOnSync = false; - autoSyncEnabled = false; - autoSyncThreadDone = false; - autoSyncInterval = DEFAULT_SYNC_INTERVAL_MILLIS; + syncingEnabled = false; + syncingThreadDone = false; + syncingInterval = DEFAULT_SYNC_INTERVAL_MILLIS; // start auto sync loop - autoSyncThread = boost::thread([this]() { - this->autoSyncThreadFunc(); + syncingThread = boost::thread([this]() { + this->syncingThreadFunc(); }); } - void MoneroWallet::autoSyncThreadFunc() { - cout << "autoSyncThreadFunc()" << endl; + void MoneroWallet::syncingThreadFunc() { + cout << "syncingThreadFunc()" << endl; while (true) { - boost::mutex::scoped_lock lock(autoSyncMutex); - if (autoSyncThreadDone) break; - if (autoSyncEnabled) { - boost::posix_time::milliseconds wait_for_ms(autoSyncInterval.load()); + boost::mutex::scoped_lock lock(syncingMutex); + if (syncingThreadDone) break; + if (syncingEnabled) { + boost::posix_time::milliseconds wait_for_ms(syncingInterval.load()); syncCV.timed_wait(lock, wait_for_ms); } else { syncCV.wait(lock); } - if (autoSyncEnabled) { + if (syncingEnabled) { lockAndSync(); } } diff --git a/src/wallet/MoneroWallet.h b/src/wallet/MoneroWallet.h index 13be4f6..85e71bb 100644 --- a/src/wallet/MoneroWallet.h +++ b/src/wallet/MoneroWallet.h @@ -358,11 +358,14 @@ namespace monero { MoneroSyncResult sync(uint64_t startHeight, MoneroSyncListener& listener); /** - * Enable or disable automatic synchronization. - * - * @param autoSync specifies if automatic synchronization is enabled or disabled + * Start automatic syncing as its own thread. */ - void setAutoSync(bool autoSync); + void startSyncing(); + + /** + * Stop automatic syncing as its own thread. + */ + void stopSyncing(); /** * Rescan the blockchain from scratch, losing any information which cannot be recovered from @@ -1171,12 +1174,12 @@ namespace monero { boost::condition_variable syncCV; // to awaken sync threads boost::mutex syncMutex; // synchronize sync() and syncAsync() requests std::atomic rescanOnSync; // whether or not to rescan on sync - std::atomic autoSyncEnabled; // whether or not auto sync is enabled - std::atomic autoSyncInterval; // auto sync loop interval in milliseconds - boost::thread autoSyncThread; // thread for auto sync loop - boost::mutex autoSyncMutex; // synchronize auto sync loop - std::atomic autoSyncThreadDone; // whether or not auto sync loop is done (cannot be re-started) - void autoSyncThreadFunc(); // function to run auto sync loop thread + std::atomic syncingEnabled; // whether or not auto sync is enabled + std::atomic syncingInterval; // auto sync loop interval in milliseconds + boost::thread syncingThread; // thread for auto sync loop + boost::mutex syncingMutex; // synchronize auto sync loop + std::atomic syncingThreadDone; // whether or not the syncing thread is shut down + void syncingThreadFunc(); // function to run syncing loop thread MoneroSyncResult lockAndSync(boost::optional startHeight = boost::none, boost::optional listener = boost::none); // internal function to synchronize request to sync and rescan MoneroSyncResult syncAux(boost::optional startHeight = boost::none, boost::optional listener = boost::none); // internal function to immediately block, sync, and report progress };