Skip to content

Commit

Permalink
Rename util.h Sleep --> MilliSleep
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gavinandresen committed Apr 3, 2013
1 parent c8c2fbe commit 1b43bf0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/bitcoinrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
return fSuccess;
}
}
Sleep(100);
MilliSleep(100);
}
return false;
}
Expand Down
10 changes: 5 additions & 5 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ enum BindFlags {
void ExitTimeout(void* parg)
{
#ifdef WIN32
Sleep(5000);
MilliSleep(5000);
ExitProcess(0);
#endif
}
Expand Down Expand Up @@ -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
Expand All @@ -116,8 +116,8 @@ void Shutdown(void* parg)
else
{
while (!fExit)
Sleep(500);
Sleep(100);
MilliSleep(500);
MilliSleep(100);
ExitThread(0);
}
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4520,7 +4520,7 @@ void static BitcoinMiner(CWallet *pwallet)

try { loop {
while (vNodes.empty())
Sleep(1000);
MilliSleep(1000);

//
// Create new block
Expand Down
34 changes: 17 additions & 17 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ bool RecvLine(SOCKET hSocket, string& strLine)
continue;
if (nErr == WSAEWOULDBLOCK || nErr == WSAEINTR || nErr == WSAEINPROGRESS)
{
Sleep(10);
MilliSleep(10);
continue;
}
}
Expand Down Expand Up @@ -909,7 +909,7 @@ void ThreadSocketHandler2(void* parg)
}
FD_ZERO(&fdsetSend);
FD_ZERO(&fdsetError);
Sleep(timeout.tv_usec/1000);
MilliSleep(timeout.tv_usec/1000);
}


Expand Down Expand Up @@ -1076,7 +1076,7 @@ void ThreadSocketHandler2(void* parg)
pnode->Release();
}

Sleep(10);
MilliSleep(10);
}
}

Expand Down Expand Up @@ -1197,7 +1197,7 @@ void ThreadMapPort2(void* parg)
else
printf("UPnP Port Mapping successful.\n");;
}
Sleep(2000);
MilliSleep(2000);
i++;
}
} else {
Expand All @@ -1208,7 +1208,7 @@ void ThreadMapPort2(void* parg)
loop {
if (fShutdown || !fUseUPnP)
return;
Sleep(2000);
MilliSleep(2000);
}
}
}
Expand Down Expand Up @@ -1418,7 +1418,7 @@ void ThreadDumpAddress2(void* parg)
{
DumpAddresses();
vnThreadsRunning[THREAD_DUMPADDRESS]--;
Sleep(100000);
MilliSleep(100000);
vnThreadsRunning[THREAD_DUMPADDRESS]++;
}
vnThreadsRunning[THREAD_DUMPADDRESS]--;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -1510,7 +1510,7 @@ void ThreadOpenConnections2(void* parg)
ProcessOneShot();

vnThreadsRunning[THREAD_OPENCONNECTIONS]--;
Sleep(500);
MilliSleep(500);
vnThreadsRunning[THREAD_OPENCONNECTIONS]++;
if (fShutdown)
return;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]++;
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,16 @@ T* alignup(T* p)
#endif
#else
#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.
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));
}
#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.
Expand Down
4 changes: 2 additions & 2 deletions src/walletdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ void ThreadFlushWalletDB(void* parg)
int64 nLastWalletUpdate = GetTime();
while (!fShutdown)
{
Sleep(500);
MilliSleep(500);

if (nLastSeen != nWalletDBUpdated)
{
Expand Down Expand Up @@ -579,7 +579,7 @@ bool BackupWallet(const CWallet& wallet, const string& strDest)
}
}
}
Sleep(100);
MilliSleep(100);
}
return false;
}
Expand Down

0 comments on commit 1b43bf0

Please sign in to comment.