setAutoSync() -> startSyncing() stopSyncing()

This commit is contained in:
woodser
2019-07-26 12:19:47 -04:00
parent 801e0f4528
commit b60d8da400
2 changed files with 44 additions and 36 deletions
+31 -26
View File
@@ -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();
}
}
+13 -10
View File
@@ -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<bool> rescanOnSync; // whether or not to rescan on sync
std::atomic<bool> autoSyncEnabled; // whether or not auto sync is enabled
std::atomic<int> autoSyncInterval; // auto sync loop interval in milliseconds
boost::thread autoSyncThread; // thread for auto sync loop
boost::mutex autoSyncMutex; // synchronize auto sync loop
std::atomic<bool> autoSyncThreadDone; // whether or not auto sync loop is done (cannot be re-started)
void autoSyncThreadFunc(); // function to run auto sync loop thread
std::atomic<bool> syncingEnabled; // whether or not auto sync is enabled
std::atomic<int> syncingInterval; // auto sync loop interval in milliseconds
boost::thread syncingThread; // thread for auto sync loop
boost::mutex syncingMutex; // synchronize auto sync loop
std::atomic<bool> syncingThreadDone; // whether or not the syncing thread is shut down
void syncingThreadFunc(); // function to run syncing loop thread
MoneroSyncResult lockAndSync(boost::optional<uint64_t> startHeight = boost::none, boost::optional<MoneroSyncListener&> listener = boost::none); // internal function to synchronize request to sync and rescan
MoneroSyncResult syncAux(boost::optional<uint64_t> startHeight = boost::none, boost::optional<MoneroSyncListener&> listener = boost::none); // internal function to immediately block, sync, and report progress
};