From 1b43bf0d3ae7b1fcde0c0e20c23c341540f4c8d2 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Thu, 7 Mar 2013 14:25:21 -0500 Subject: [PATCH] Rename util.h Sleep --> MilliSleep Two reasons for this change: 1. Need to always use boost::thread's sleep, even on Windows, so the sleeps can be interrupted (prior code used Windows' built-in Sleep). 2. I always forgot what units the old Sleep took. --- src/bitcoinrpc.cpp | 2 +- src/db.cpp | 2 +- src/init.cpp | 10 +++++----- src/main.cpp | 2 +- src/net.cpp | 34 +++++++++++++++++----------------- src/rpcwallet.cpp | 2 +- src/util.h | 15 +++++++++------ src/walletdb.cpp | 4 ++-- 8 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 84478398c..c7219920c 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -1003,7 +1003,7 @@ void ThreadRPCServer3(void* parg) If this results in a DOS the user really shouldn't have their RPC port exposed.*/ if (mapArgs["-rpcpassword"].size() < 20) - Sleep(250); + MilliSleep(250); conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush; break; diff --git a/src/db.cpp b/src/db.cpp index e51d5d2cb..87c4b5c7e 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -413,7 +413,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip) return fSuccess; } } - Sleep(100); + MilliSleep(100); } return false; } diff --git a/src/init.cpp b/src/init.cpp index a5015adc4..d61bfa892 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -43,7 +43,7 @@ enum BindFlags { void ExitTimeout(void* parg) { #ifdef WIN32 - Sleep(5000); + MilliSleep(5000); ExitProcess(0); #endif } @@ -105,7 +105,7 @@ void Shutdown(void* parg) UnregisterWallet(pwalletMain); delete pwalletMain; NewThread(ExitTimeout, NULL); - Sleep(50); + MilliSleep(50); printf("Bitcoin exited\n\n"); fExit = true; #ifndef QT_GUI @@ -116,8 +116,8 @@ void Shutdown(void* parg) else { while (!fExit) - Sleep(500); - Sleep(100); + MilliSleep(500); + MilliSleep(100); ExitThread(0); } } @@ -1061,7 +1061,7 @@ bool AppInit2(boost::thread_group& threadGroup) // Loop until process is exit()ed from shutdown() function, // called from ThreadRPCServer thread when a "stop" command is received. while (1) - Sleep(5000); + MilliSleep(5000); #endif return true; diff --git a/src/main.cpp b/src/main.cpp index df9ea4443..53ec55461 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4520,7 +4520,7 @@ void static BitcoinMiner(CWallet *pwallet) try { loop { while (vNodes.empty()) - Sleep(1000); + MilliSleep(1000); // // Create new block diff --git a/src/net.cpp b/src/net.cpp index 097f480b8..fe0dcd41b 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -165,7 +165,7 @@ bool RecvLine(SOCKET hSocket, string& strLine) continue; if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS) { - Sleep(10); + MilliSleep(10); continue; } } @@ -909,7 +909,7 @@ void ThreadSocketHandler2(void* parg) } FD_ZERO(&fdsetSend); FD_ZERO(&fdsetError); - Sleep(timeout.tv_usec/1000); + MilliSleep(timeout.tv_usec/1000); } @@ -1076,7 +1076,7 @@ void ThreadSocketHandler2(void* parg) pnode->Release(); } - Sleep(10); + MilliSleep(10); } } @@ -1197,7 +1197,7 @@ void ThreadMapPort2(void* parg) else printf("UPnP Port Mapping successful.\n");; } - Sleep(2000); + MilliSleep(2000); i++; } } else { @@ -1208,7 +1208,7 @@ void ThreadMapPort2(void* parg) loop { if (fShutdown || !fUseUPnP) return; - Sleep(2000); + MilliSleep(2000); } } } @@ -1418,7 +1418,7 @@ void ThreadDumpAddress2(void* parg) { DumpAddresses(); vnThreadsRunning[THREAD_DUMPADDRESS]--; - Sleep(100000); + MilliSleep(100000); vnThreadsRunning[THREAD_DUMPADDRESS]++; } vnThreadsRunning[THREAD_DUMPADDRESS]--; @@ -1494,12 +1494,12 @@ void ThreadOpenConnections2(void* parg) OpenNetworkConnection(addr, NULL, strAddr.c_str()); for (int i = 0; i < 10 && i < nLoop; i++) { - Sleep(500); + MilliSleep(500); if (fShutdown) return; } } - Sleep(500); + MilliSleep(500); } } @@ -1510,7 +1510,7 @@ void ThreadOpenConnections2(void* parg) ProcessOneShot(); vnThreadsRunning[THREAD_OPENCONNECTIONS]--; - Sleep(500); + MilliSleep(500); vnThreadsRunning[THREAD_OPENCONNECTIONS]++; if (fShutdown) return; @@ -1642,12 +1642,12 @@ void ThreadOpenAddedConnections2(void* parg) CAddress addr; CSemaphoreGrant grant(*semOutbound); OpenNetworkConnection(addr, &grant, strAddNode.c_str()); - Sleep(500); + MilliSleep(500); if (fShutdown) return; } vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--; - Sleep(120000); // Retry every 2 minutes + MilliSleep(120000); // Retry every 2 minutes vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++; } return; @@ -1694,14 +1694,14 @@ void ThreadOpenAddedConnections2(void* parg) { CSemaphoreGrant grant(*semOutbound); OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant); - Sleep(500); + MilliSleep(500); if (fShutdown) return; } if (fShutdown) return; vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--; - Sleep(120000); // Retry every 2 minutes + MilliSleep(120000); // Retry every 2 minutes vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++; if (fShutdown) return; @@ -1821,7 +1821,7 @@ void ThreadMessageHandler2(void* parg) // Reduce vnThreadsRunning so StopNode has permission to exit while // we're sleeping, but we must always check fShutdown after doing this. vnThreadsRunning[THREAD_MESSAGEHANDLER]--; - Sleep(100); + MilliSleep(100); if (fRequestShutdown) StartShutdown(); vnThreadsRunning[THREAD_MESSAGEHANDLER]++; @@ -2068,7 +2068,7 @@ bool StopNode() break; if (GetTime() - nStart > 20) break; - Sleep(20); + MilliSleep(20); } while(true); if (vnThreadsRunning[THREAD_SOCKETHANDLER] > 0) printf("ThreadSocketHandler still running\n"); if (vnThreadsRunning[THREAD_OPENCONNECTIONS] > 0) printf("ThreadOpenConnections still running\n"); @@ -2082,8 +2082,8 @@ bool StopNode() if (vnThreadsRunning[THREAD_ADDEDCONNECTIONS] > 0) printf("ThreadOpenAddedConnections still running\n"); if (vnThreadsRunning[THREAD_DUMPADDRESS] > 0) printf("ThreadDumpAddresses still running\n"); while (vnThreadsRunning[THREAD_MESSAGEHANDLER] > 0 || vnThreadsRunning[THREAD_RPCHANDLER] > 0) - Sleep(20); - Sleep(50); + MilliSleep(20); + MilliSleep(50); DumpAddresses(); return true; diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 21eb2fd1a..526a7b5b3 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -1289,7 +1289,7 @@ void ThreadCleanWalletPassphrase(void* parg) break; LEAVE_CRITICAL_SECTION(cs_nWalletUnlockTime); - Sleep(nToSleep); + MilliSleep(nToSleep); ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime); } while(1); diff --git a/src/util.h b/src/util.h index 2c2612033..5173686ed 100644 --- a/src/util.h +++ b/src/util.h @@ -100,14 +100,17 @@ T* alignup(T* p) #endif #else #define MAX_PATH 1024 -inline void Sleep(int64 n) -{ - /*Boost has a year 2038 problem— if the request sleep time is past epoch+2^31 seconds the sleep returns instantly. - So we clamp our sleeps here to 10 years and hope that boost is fixed by 2028.*/ - boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n>315576000000LL?315576000000LL:n)); -} #endif +inline void MilliSleep(int64 n) +{ +#if BOOST_VERSION >= 105000 + boost::this_thread::sleep_for(boost::chrono::milliseconds(n)); +#else + boost::this_thread::sleep(boost::posix_time::milliseconds(n)); +#endif +} + /* This GNU C extension enables the compiler to check the format string against the parameters provided. * X is the number of the "format string" parameter, and Y is the number of the first variadic parameter. * Parameters count from 1. diff --git a/src/walletdb.cpp b/src/walletdb.cpp index fe9bce21e..96bc05b11 100644 --- a/src/walletdb.cpp +++ b/src/walletdb.cpp @@ -500,7 +500,7 @@ void ThreadFlushWalletDB(void* parg) int64 nLastWalletUpdate = GetTime(); while (!fShutdown) { - Sleep(500); + MilliSleep(500); if (nLastSeen != nWalletDBUpdated) { @@ -579,7 +579,7 @@ bool BackupWallet(const CWallet& wallet, const string& strDest) } } } - Sleep(100); + MilliSleep(100); } return false; }