diff options
author | Jonas Schnelli <dev@jonasschnelli.ch> | 2017-12-08 11:07:37 -1000 |
---|---|---|
committer | Jonas Schnelli <dev@jonasschnelli.ch> | 2018-01-23 20:23:57 -1000 |
commit | dbf8556b4d6a2484ad4c03d0b4e41c1db0133997 (patch) | |
tree | c955e1bea937c2901267cdbc191bde93db725e22 /src/wallet/wallet.h | |
parent | 8d0b610fe8d0916404aa9158c525b80b1c581c0e (diff) | |
download | bitcoin-dbf8556b4d6a2484ad4c03d0b4e41c1db0133997.tar.xz |
Add RAII wallet rescan reserver
Diffstat (limited to 'src/wallet/wallet.h')
-rw-r--r-- | src/wallet/wallet.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 53a2c6b9d5..5d782faa60 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -669,6 +669,9 @@ private: static std::atomic<bool> fFlushScheduled; std::atomic<bool> fAbortRescan; std::atomic<bool> fScanningWallet; + std::mutex mutexScanning; + friend class WalletRescanReserver; + /** * Select a set of coins such that nValueRet >= nTargetValue and at least @@ -1263,4 +1266,34 @@ CTxDestination GetDestinationForKey(const CPubKey& key, OutputType); /** Get all destinations (potentially) supported by the wallet for the given key. */ std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key); +/** RAII object to check and reserve a wallet rescan */ +class WalletRescanReserver +{ +private: + CWalletRef m_wallet; + bool m_could_reserve; +public: + explicit WalletRescanReserver(CWalletRef w) : m_wallet(w), m_could_reserve(false) {} + + bool reserve() + { + assert(!m_could_reserve); + std::lock_guard<std::mutex> lock(m_wallet->mutexScanning); + if (m_wallet->fScanningWallet) { + return false; + } + m_wallet->fScanningWallet = true; + m_could_reserve = true; + return true; + } + + ~WalletRescanReserver() + { + std::lock_guard<std::mutex> lock(m_wallet->mutexScanning); + if (m_could_reserve) { + m_wallet->fScanningWallet = false; + } + } +}; + #endif // BITCOIN_WALLET_WALLET_H |