Browse Source

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.
miguelfreitas
Gavin Andresen 12 years ago
parent
commit
1b43bf0d3a
  1. 2
      src/bitcoinrpc.cpp
  2. 2
      src/db.cpp
  3. 10
      src/init.cpp
  4. 2
      src/main.cpp
  5. 34
      src/net.cpp
  6. 2
      src/rpcwallet.cpp
  7. 13
      src/util.h
  8. 4
      src/walletdb.cpp

2
src/bitcoinrpc.cpp

@ -1003,7 +1003,7 @@ void ThreadRPCServer3(void* parg)
If this results in a DOS the user really If this results in a DOS the user really
shouldn't have their RPC port exposed.*/ shouldn't have their RPC port exposed.*/
if (mapArgs["-rpcpassword"].size() < 20) if (mapArgs["-rpcpassword"].size() < 20)
Sleep(250); MilliSleep(250);
conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush; conn->stream() << HTTPReply(HTTP_UNAUTHORIZED, "", false) << std::flush;
break; break;

2
src/db.cpp

@ -413,7 +413,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
return fSuccess; return fSuccess;
} }
} }
Sleep(100); MilliSleep(100);
} }
return false; return false;
} }

10
src/init.cpp

@ -43,7 +43,7 @@ enum BindFlags {
void ExitTimeout(void* parg) void ExitTimeout(void* parg)
{ {
#ifdef WIN32 #ifdef WIN32
Sleep(5000); MilliSleep(5000);
ExitProcess(0); ExitProcess(0);
#endif #endif
} }
@ -105,7 +105,7 @@ void Shutdown(void* parg)
UnregisterWallet(pwalletMain); UnregisterWallet(pwalletMain);
delete pwalletMain; delete pwalletMain;
NewThread(ExitTimeout, NULL); NewThread(ExitTimeout, NULL);
Sleep(50); MilliSleep(50);
printf("Bitcoin exited\n\n"); printf("Bitcoin exited\n\n");
fExit = true; fExit = true;
#ifndef QT_GUI #ifndef QT_GUI
@ -116,8 +116,8 @@ void Shutdown(void* parg)
else else
{ {
while (!fExit) while (!fExit)
Sleep(500); MilliSleep(500);
Sleep(100); MilliSleep(100);
ExitThread(0); ExitThread(0);
} }
} }
@ -1061,7 +1061,7 @@ bool AppInit2(boost::thread_group& threadGroup)
// Loop until process is exit()ed from shutdown() function, // Loop until process is exit()ed from shutdown() function,
// called from ThreadRPCServer thread when a "stop" command is received. // called from ThreadRPCServer thread when a "stop" command is received.
while (1) while (1)
Sleep(5000); MilliSleep(5000);
#endif #endif
return true; return true;

2
src/main.cpp

@ -4520,7 +4520,7 @@ void static BitcoinMiner(CWallet *pwallet)
try { loop { try { loop {
while (vNodes.empty()) while (vNodes.empty())
Sleep(1000); MilliSleep(1000);
// //
// Create new block // Create new block

34
src/net.cpp

@ -165,7 +165,7 @@ bool RecvLine(SOCKET hSocket, string& strLine)
continue; continue;
if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS) if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
{ {
Sleep(10); MilliSleep(10);
continue; continue;
} }
} }
@ -909,7 +909,7 @@ void ThreadSocketHandler2(void* parg)
} }
FD_ZERO(&fdsetSend); FD_ZERO(&fdsetSend);
FD_ZERO(&fdsetError); FD_ZERO(&fdsetError);
Sleep(timeout.tv_usec/1000); MilliSleep(timeout.tv_usec/1000);
} }
@ -1076,7 +1076,7 @@ void ThreadSocketHandler2(void* parg)
pnode->Release(); pnode->Release();
} }
Sleep(10); MilliSleep(10);
} }
} }
@ -1197,7 +1197,7 @@ void ThreadMapPort2(void* parg)
else else
printf("UPnP Port Mapping successful.\n");; printf("UPnP Port Mapping successful.\n");;
} }
Sleep(2000); MilliSleep(2000);
i++; i++;
} }
} else { } else {
@ -1208,7 +1208,7 @@ void ThreadMapPort2(void* parg)
loop { loop {
if (fShutdown || !fUseUPnP) if (fShutdown || !fUseUPnP)
return; return;
Sleep(2000); MilliSleep(2000);
} }
} }
} }
@ -1418,7 +1418,7 @@ void ThreadDumpAddress2(void* parg)
{ {
DumpAddresses(); DumpAddresses();
vnThreadsRunning[THREAD_DUMPADDRESS]--; vnThreadsRunning[THREAD_DUMPADDRESS]--;
Sleep(100000); MilliSleep(100000);
vnThreadsRunning[THREAD_DUMPADDRESS]++; vnThreadsRunning[THREAD_DUMPADDRESS]++;
} }
vnThreadsRunning[THREAD_DUMPADDRESS]--; vnThreadsRunning[THREAD_DUMPADDRESS]--;
@ -1494,12 +1494,12 @@ void ThreadOpenConnections2(void* parg)
OpenNetworkConnection(addr, NULL, strAddr.c_str()); OpenNetworkConnection(addr, NULL, strAddr.c_str());
for (int i = 0; i < 10 && i < nLoop; i++) for (int i = 0; i < 10 && i < nLoop; i++)
{ {
Sleep(500); MilliSleep(500);
if (fShutdown) if (fShutdown)
return; return;
} }
} }
Sleep(500); MilliSleep(500);
} }
} }
@ -1510,7 +1510,7 @@ void ThreadOpenConnections2(void* parg)
ProcessOneShot(); ProcessOneShot();
vnThreadsRunning[THREAD_OPENCONNECTIONS]--; vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
Sleep(500); MilliSleep(500);
vnThreadsRunning[THREAD_OPENCONNECTIONS]++; vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
if (fShutdown) if (fShutdown)
return; return;
@ -1642,12 +1642,12 @@ void ThreadOpenAddedConnections2(void* parg)
CAddress addr; CAddress addr;
CSemaphoreGrant grant(*semOutbound); CSemaphoreGrant grant(*semOutbound);
OpenNetworkConnection(addr, &grant, strAddNode.c_str()); OpenNetworkConnection(addr, &grant, strAddNode.c_str());
Sleep(500); MilliSleep(500);
if (fShutdown) if (fShutdown)
return; return;
} }
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--; vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--;
Sleep(120000); // Retry every 2 minutes MilliSleep(120000); // Retry every 2 minutes
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++; vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++;
} }
return; return;
@ -1694,14 +1694,14 @@ void ThreadOpenAddedConnections2(void* parg)
{ {
CSemaphoreGrant grant(*semOutbound); CSemaphoreGrant grant(*semOutbound);
OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant); OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant);
Sleep(500); MilliSleep(500);
if (fShutdown) if (fShutdown)
return; return;
} }
if (fShutdown) if (fShutdown)
return; return;
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--; vnThreadsRunning[THREAD_ADDEDCONNECTIONS]--;
Sleep(120000); // Retry every 2 minutes MilliSleep(120000); // Retry every 2 minutes
vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++; vnThreadsRunning[THREAD_ADDEDCONNECTIONS]++;
if (fShutdown) if (fShutdown)
return; return;
@ -1821,7 +1821,7 @@ void ThreadMessageHandler2(void* parg)
// Reduce vnThreadsRunning so StopNode has permission to exit while // Reduce vnThreadsRunning so StopNode has permission to exit while
// we're sleeping, but we must always check fShutdown after doing this. // we're sleeping, but we must always check fShutdown after doing this.
vnThreadsRunning[THREAD_MESSAGEHANDLER]--; vnThreadsRunning[THREAD_MESSAGEHANDLER]--;
Sleep(100); MilliSleep(100);
if (fRequestShutdown) if (fRequestShutdown)
StartShutdown(); StartShutdown();
vnThreadsRunning[THREAD_MESSAGEHANDLER]++; vnThreadsRunning[THREAD_MESSAGEHANDLER]++;
@ -2068,7 +2068,7 @@ bool StopNode()
break; break;
if (GetTime() - nStart > 20) if (GetTime() - nStart > 20)
break; break;
Sleep(20); MilliSleep(20);
} while(true); } while(true);
if (vnThreadsRunning[THREAD_SOCKETHANDLER] > 0) printf("ThreadSocketHandler still running\n"); if (vnThreadsRunning[THREAD_SOCKETHANDLER] > 0) printf("ThreadSocketHandler still running\n");
if (vnThreadsRunning[THREAD_OPENCONNECTIONS] > 0) printf("ThreadOpenConnections 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_ADDEDCONNECTIONS] > 0) printf("ThreadOpenAddedConnections still running\n");
if (vnThreadsRunning[THREAD_DUMPADDRESS] > 0) printf("ThreadDumpAddresses still running\n"); if (vnThreadsRunning[THREAD_DUMPADDRESS] > 0) printf("ThreadDumpAddresses still running\n");
while (vnThreadsRunning[THREAD_MESSAGEHANDLER] > 0 || vnThreadsRunning[THREAD_RPCHANDLER] > 0) while (vnThreadsRunning[THREAD_MESSAGEHANDLER] > 0 || vnThreadsRunning[THREAD_RPCHANDLER] > 0)
Sleep(20); MilliSleep(20);
Sleep(50); MilliSleep(50);
DumpAddresses(); DumpAddresses();
return true; return true;

2
src/rpcwallet.cpp

@ -1289,7 +1289,7 @@ void ThreadCleanWalletPassphrase(void* parg)
break; break;
LEAVE_CRITICAL_SECTION(cs_nWalletUnlockTime); LEAVE_CRITICAL_SECTION(cs_nWalletUnlockTime);
Sleep(nToSleep); MilliSleep(nToSleep);
ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime); ENTER_CRITICAL_SECTION(cs_nWalletUnlockTime);
} while(1); } while(1);

13
src/util.h

@ -100,13 +100,16 @@ T* alignup(T* p)
#endif #endif
#else #else
#define MAX_PATH 1024 #define MAX_PATH 1024
inline void Sleep(int64 n) #endif
inline void MilliSleep(int64 n)
{ {
/*Boost has a year 2038 problem— if the request sleep time is past epoch+2^31 seconds the sleep returns instantly. #if BOOST_VERSION >= 105000
So we clamp our sleeps here to 10 years and hope that boost is fixed by 2028.*/ boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n>315576000000LL?315576000000LL:n)); #else
} boost::this_thread::sleep(boost::posix_time::milliseconds(n));
#endif #endif
}
/* This GNU C extension enables the compiler to check the format string against the parameters provided. /* 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. * X is the number of the "format string" parameter, and Y is the number of the first variadic parameter.

4
src/walletdb.cpp

@ -500,7 +500,7 @@ void ThreadFlushWalletDB(void* parg)
int64 nLastWalletUpdate = GetTime(); int64 nLastWalletUpdate = GetTime();
while (!fShutdown) while (!fShutdown)
{ {
Sleep(500); MilliSleep(500);
if (nLastSeen != nWalletDBUpdated) if (nLastSeen != nWalletDBUpdated)
{ {
@ -579,7 +579,7 @@ bool BackupWallet(const CWallet& wallet, const string& strDest)
} }
} }
} }
Sleep(100); MilliSleep(100);
} }
return false; return false;
} }

Loading…
Cancel
Save