merge #16117: Replace boost sleep with std sleep

This commit is contained in:
Kittywhiskers Van Gogh 2021-07-13 15:01:17 +05:30
parent 600c9f891b
commit cbc4a74250
18 changed files with 26 additions and 103 deletions

View File

@ -1207,57 +1207,6 @@ AC_LINK_IFELSE([AC_LANG_PROGRAM([[
LIBS="$TEMP_LIBS"
CPPFLAGS="$TEMP_CPPFLAGS"
dnl Boost >= 1.50 uses sleep_for rather than the now-deprecated sleep, however
dnl it was broken from 1.50 to 1.52 when backed by nanosleep. Use sleep_for if
dnl a working version is available, else fall back to sleep. sleep was removed
dnl after 1.56.
dnl If neither is available, abort.
TEMP_LIBS="$LIBS"
LIBS="$BOOST_LIBS $LIBS"
TEMP_CPPFLAGS="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
#include <boost/thread/thread.hpp>
#include <boost/version.hpp>
]],[[
#if BOOST_VERSION >= 105000 && (!defined(BOOST_HAS_NANOSLEEP) || BOOST_VERSION >= 105200)
boost::this_thread::sleep_for(boost::chrono::milliseconds(0));
#else
choke me
#endif
]])],
[boost_sleep=yes;
AC_DEFINE(HAVE_WORKING_BOOST_SLEEP_FOR, 1, [Define this symbol if boost sleep_for works])],
[boost_sleep=no])
LIBS="$TEMP_LIBS"
CPPFLAGS="$TEMP_CPPFLAGS"
if test x$boost_sleep != xyes; then
TEMP_LIBS="$LIBS"
LIBS="$BOOST_LIBS $LIBS"
TEMP_CPPFLAGS="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
AC_LINK_IFELSE([AC_LANG_PROGRAM([[
#include <boost/version.hpp>
#include <boost/thread.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
]],[[
#if BOOST_VERSION <= 105600
boost::this_thread::sleep(boost::posix_time::milliseconds(0));
#else
choke me
#endif
]])],
[boost_sleep=yes; AC_DEFINE(HAVE_WORKING_BOOST_SLEEP, 1, [Define this symbol if boost sleep works])],
[boost_sleep=no])
LIBS="$TEMP_LIBS"
CPPFLAGS="$TEMP_CPPFLAGS"
fi
if test x$boost_sleep != xyes; then
AC_MSG_ERROR(No working boost sleep implementation found.)
fi
fi
if test x$use_pkgconfig = xyes; then

View File

@ -356,7 +356,7 @@ static void BLS_Verify_BatchedParallel(benchmark::Bench& bench)
cancel = true;
while (blsWorker.IsAsyncVerifyInProgress())
{
MilliSleep(100);
UninterruptibleSleep(std::chrono::milliseconds{100});
}
}

View File

@ -293,7 +293,7 @@ void CCoinJoinClientSession::UnlockCoins()
while (true) {
TRY_LOCK(mixingWallet.cs_wallet, lockWallet);
if (!lockWallet) {
MilliSleep(50);
UninterruptibleSleep(std::chrono::milliseconds{50});
continue;
}
for (const auto& outpoint : vecOutPointLocked)

View File

@ -519,7 +519,7 @@ static int CommandLineRPC(int argc, char *argv[])
}
catch (const CConnectionFailed&) {
if (fWait)
MilliSleep(1000);
UninterruptibleSleep(std::chrono::milliseconds{1000});
else
throw;
}

View File

@ -48,7 +48,7 @@ static void WaitForShutdown()
{
while (!ShutdownRequested())
{
MilliSleep(200);
UninterruptibleSleep(std::chrono::milliseconds{200});
}
Interrupt();
}

View File

@ -170,7 +170,7 @@ static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
/* Deter brute-forcing
If this results in a DoS the user really
shouldn't have their RPC port exposed. */
MilliSleep(250);
UninterruptibleSleep(std::chrono::milliseconds{250});
req->WriteHeader("WWW-Authenticate", WWW_AUTH_HEADER_DATA);
req->WriteReply(HTTP_UNAUTHORIZED);

View File

@ -208,7 +208,7 @@ void CDKGSessionHandler::WaitForNextPhase(QuorumPhase curPhase,
throw AbortPhaseException();
}
if (!runWhileWaiting()) {
MilliSleep(100);
UninterruptibleSleep(std::chrono::milliseconds{100});
}
}
@ -238,7 +238,7 @@ void CDKGSessionHandler::WaitForNewQuorum(const uint256& oldQuorumHash) const
if (p.second != oldQuorumHash) {
break;
}
MilliSleep(100);
UninterruptibleSleep(std::chrono::milliseconds{100});
}
LogPrint(BCLog::LLMQ_DKG, "CDKGSessionManager::%s -- %s - done\n", __func__, params.name);
@ -305,7 +305,7 @@ void CDKGSessionHandler::SleepBeforePhase(QuorumPhase curPhase,
}
}
if (!runWhileWaiting()) {
MilliSleep(100);
UninterruptibleSleep(std::chrono::milliseconds{100});
}
}

View File

@ -309,7 +309,7 @@ UniValue stop(const JSONRPCRequest& jsonRequest)
// this reply will get back to the client.
StartShutdown();
if (jsonRequest.params[0].isNum()) {
MilliSleep(jsonRequest.params[0].get_int());
UninterruptibleSleep(std::chrono::milliseconds{jsonRequest.params[0].get_int()});
}
return "Dash Core server stopping";
}

View File

@ -372,7 +372,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueueControl_Locks)
CCheckQueueControl<FakeCheck> control(queue.get());
// While sleeping, no other thread should execute to this point
auto observed = ++nThreads;
MilliSleep(10);
UninterruptibleSleep(std::chrono::milliseconds{10});
fails += observed != nThreads;
});
}

View File

@ -4,6 +4,7 @@
#include <random.h>
#include <scheduler.h>
#include <util/time.h>
#include <test/test_dash.h>
@ -26,18 +27,6 @@ static void microTask(CScheduler& s, boost::mutex& mutex, int& counter, int delt
}
}
static void MicroSleep(uint64_t n)
{
#if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
boost::this_thread::sleep_for(boost::chrono::microseconds(n));
#elif defined(HAVE_WORKING_BOOST_SLEEP)
boost::this_thread::sleep(boost::posix_time::microseconds(n));
#else
//should never get here
#error missing boost sleep implementation
#endif
}
BOOST_AUTO_TEST_CASE(manythreads)
{
// Stress test: hundreds of microsecond-scheduled tasks,
@ -84,7 +73,7 @@ BOOST_AUTO_TEST_CASE(manythreads)
for (int i = 0; i < 5; i++)
microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, &microTasks));
MicroSleep(600);
UninterruptibleSleep(std::chrono::microseconds{600});
now = boost::chrono::system_clock::now();
// More threads and more tasks:

View File

@ -151,7 +151,7 @@ TestChainSetup::TestChainSetup(int blockCount) : TestingSetup(CBaseChainParams::
int64_t time_start = GetTimeMillis();
while (!g_txindex->BlockUntilSyncedToCurrentChain()) {
assert(time_start + timeout_ms > GetTimeMillis());
MilliSleep(100);
UninterruptibleSleep(std::chrono::milliseconds{100});
}
}

View File

@ -36,7 +36,7 @@ BOOST_FIXTURE_TEST_CASE(txindex_initial_sync, TestChain100Setup)
int64_t time_start = GetTimeMillis();
while (!txindex.BlockUntilSyncedToCurrentChain()) {
BOOST_REQUIRE(time_start + timeout_ms > GetTimeMillis());
MilliSleep(100);
UninterruptibleSleep(std::chrono::milliseconds{100});
}
// Check that txindex excludes genesis block transactions.

View File

@ -757,7 +757,7 @@ BOOST_AUTO_TEST_CASE(util_time_GetTime)
SetMockTime(111);
// Check that mock time does not change after a sleep
for (const auto& num_sleep : {0, 1}) {
MilliSleep(num_sleep);
UninterruptibleSleep(std::chrono::milliseconds{num_sleep});
BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
@ -768,7 +768,7 @@ BOOST_AUTO_TEST_CASE(util_time_GetTime)
// Check that system time changes after a sleep
const auto ms_0 = GetTime<std::chrono::milliseconds>();
const auto us_0 = GetTime<std::chrono::microseconds>();
MilliSleep(1);
UninterruptibleSleep(std::chrono::milliseconds{1});
BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
}

View File

@ -169,7 +169,7 @@ BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
threads.join_all();
while (GetMainSignals().CallbacksPending() > 0) {
MilliSleep(100);
UninterruptibleSleep(std::chrono::milliseconds{100});
}
UnregisterValidationInterface(&sub);

View File

@ -1246,7 +1246,7 @@ void RenameThreadPool(ctpl::thread_pool& tp, const char* baseName)
do {
// Always sleep to let all threads acquire locks
MilliSleep(10);
UninterruptibleSleep(std::chrono::milliseconds{10});
// `doneCnt` should be at least `futures.size()` if tp size was increased (for whatever reason),
// or at least `tp.size()` if tp size was decreased and queue was cleared
// (which can happen on `stop()` if we were not fast enough to get all jobs to their threads).

View File

@ -11,10 +11,13 @@
#include <atomic>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <ctime>
#include <thread>
#include <tinyformat.h>
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
static std::atomic<int64_t> nMockTime(0); //!< For unit testing
int64_t GetTime()
@ -70,24 +73,6 @@ int64_t GetSystemTimeInSeconds()
return GetTimeMicros()/1000000;
}
void MilliSleep(int64_t n)
{
/**
* Boost's sleep_for was uninterruptible when backed by nanosleep from 1.50
* until fixed in 1.52. Use the deprecated sleep method for the broken case.
* See: https://svn.boost.org/trac/boost/ticket/7238
*/
#if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
#elif defined(HAVE_WORKING_BOOST_SLEEP)
boost::this_thread::sleep(boost::posix_time::milliseconds(n));
#else
//should never get here
#error missing boost sleep implementation
#endif
}
std::string FormatISO8601DateTime(int64_t nTime) {
struct tm ts;
time_t time_val = nTime;

View File

@ -10,6 +10,8 @@
#include <string>
#include <chrono>
void UninterruptibleSleep(const std::chrono::microseconds& n);
/**
* DEPRECATED
* Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
@ -28,8 +30,6 @@ void SetMockTime(int64_t nMockTimeIn);
/** For testing */
int64_t GetMockTime();
void MilliSleep(int64_t n);
/** Return system time (or mocked time, if set) */
template <typename T>
T GetTime();

View File

@ -700,7 +700,7 @@ bool BerkeleyBatch::Rewrite(BerkeleyDatabase& database, const char* pszSkip)
return fSuccess;
}
}
MilliSleep(100);
UninterruptibleSleep(std::chrono::milliseconds{100});
}
}
@ -830,7 +830,7 @@ bool BerkeleyDatabase::Backup(const std::string& strDest)
}
}
}
MilliSleep(100);
UninterruptibleSleep(std::chrono::milliseconds{100});
}
}