MCPcopy Create free account
hub / github.com/bitcoinxt/bitcoinxt / ThreadFlushWalletDB

Function ThreadFlushWalletDB

src/wallet/walletdb.cpp:798–858  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

796}
797
798void ThreadFlushWalletDB(const string& strFile)
799{
800 // Make this thread recognisable as the wallet flushing thread
801 RenameThread("bitcoin-wallet");
802
803 static bool fOneThread;
804 if (fOneThread)
805 return;
806 fOneThread = true;
807 if (!GetBoolArg("-flushwallet", true))
808 return;
809
810 unsigned int nLastSeen = nWalletDBUpdated;
811 unsigned int nLastFlushed = nWalletDBUpdated;
812 int64_t nLastWalletUpdate = GetTime();
813 while (true)
814 {
815 MilliSleep(500);
816
817 if (nLastSeen != nWalletDBUpdated)
818 {
819 nLastSeen = nWalletDBUpdated;
820 nLastWalletUpdate = GetTime();
821 }
822
823 if (nLastFlushed != nWalletDBUpdated && GetTime() - nLastWalletUpdate >= 2)
824 {
825 TRY_LOCK(bitdb.cs_db,lockDb);
826 if (lockDb)
827 {
828 // Don't do this if any databases are in use
829 int nRefCount = 0;
830 map<string, int>::iterator mi = bitdb.mapFileUseCount.begin();
831 while (mi != bitdb.mapFileUseCount.end())
832 {
833 nRefCount += (*mi).second;
834 mi++;
835 }
836
837 if (nRefCount == 0)
838 {
839 boost::this_thread::interruption_point();
840 map<string, int>::iterator mi = bitdb.mapFileUseCount.find(strFile);
841 if (mi != bitdb.mapFileUseCount.end())
842 {
843 LogPrint("db", "Flushing wallet.dat\n");
844 nLastFlushed = nWalletDBUpdated;
845 int64_t nStart = GetTimeMillis();
846
847 // Flush wallet.dat so it's self contained
848 bitdb.CloseDb(strFile);
849 bitdb.CheckpointLSN(strFile);
850
851 bitdb.mapFileUseCount.erase(mi++);
852 LogPrint("db", "Flushed wallet.dat %dms\n", GetTimeMillis() - nStart);
853 }
854 }
855 }

Callers

nothing calls this directly

Calls 11

RenameThreadFunction · 0.85
GetBoolArgFunction · 0.85
GetTimeFunction · 0.85
MilliSleepFunction · 0.85
GetTimeMillisFunction · 0.85
findMethod · 0.80
CloseDbMethod · 0.80
CheckpointLSNMethod · 0.80
beginMethod · 0.45
endMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected