Skip to content

Commit

Permalink
remove boost::chrono
Browse files Browse the repository at this point in the history
  • Loading branch information
Naviabheeman committed Jun 1, 2023
1 parent acd88fc commit 434d8d7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 224 deletions.
118 changes: 0 additions & 118 deletions build-aux/m4/ax_boost_chrono.m4

This file was deleted.

53 changes: 1 addition & 52 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,6 @@ fi
AX_BOOST_SYSTEM
AX_BOOST_FILESYSTEM
AX_BOOST_THREAD
AX_BOOST_CHRONO

dnl Boost 1.56 through 1.62 allow using std::atomic instead of its own atomic
dnl counter implementations. In 1.63 and later the std::atomic approach is default.
Expand Down Expand Up @@ -960,7 +959,7 @@ fi

if test x$use_boost = xyes; then

BOOST_LIBS="$BOOST_LDFLAGS $BOOST_SYSTEM_LIB $BOOST_FILESYSTEM_LIB $BOOST_THREAD_LIB $BOOST_CHRONO_LIB"
BOOST_LIBS="$BOOST_LDFLAGS $BOOST_SYSTEM_LIB $BOOST_FILESYSTEM_LIB $BOOST_THREAD_LIB "


dnl If boost (prior to 1.57) was built without c++11, it emulated scoped enums
Expand Down Expand Up @@ -998,56 +997,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

Expand Down
4 changes: 3 additions & 1 deletion src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <serialize.h>
#include <streams.h>

#include <cmath>

int CAddrInfo::GetTriedBucket(const uint256& nKey) const
{
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetHash().GetCheapHash();
Expand Down Expand Up @@ -60,7 +62,7 @@ double CAddrInfo::GetChance(int64_t nNow) const
fChance *= 0.01;

// deprioritize 66% after each failed attempt, but at most 1/28th to avoid the search taking forever or overly penalizing outages.
fChance *= pow(0.66, std::min(nAttempts, 8));
fChance *= std::pow(0.66, std::min(nAttempts, 8));

return fChance;
}
Expand Down
52 changes: 18 additions & 34 deletions src/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

#include <assert.h>
#include <utility>
#include <chrono>

#include <boost/thread/lock_types.hpp>
#include <boost/thread/shared_mutex.hpp>

CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false)
{
Expand All @@ -19,19 +23,9 @@ CScheduler::~CScheduler()
assert(nThreadsServicingQueue == 0);
}


#if BOOST_VERSION < 105000
static boost::system_time toPosixTime(const boost::chrono::system_clock::time_point& t)
{
// Creating the posix_time using from_time_t loses sub-second precision. So rather than exporting the time_point to time_t,
// start with a posix_time at the epoch (0) and add the milliseconds that have passed since then.
return boost::posix_time::from_time_t(0) + boost::posix_time::milliseconds(boost::chrono::duration_cast<boost::chrono::milliseconds>(t.time_since_epoch()).count());
}
#endif

void CScheduler::serviceQueue()
{
boost::unique_lock<boost::mutex> lock(newTaskMutex);
WaitableLock lock(newTaskMutex);
++nThreadsServicingQueue;

// newTaskMutex is locked throughout this loop EXCEPT
Expand All @@ -40,7 +34,7 @@ void CScheduler::serviceQueue()
while (!shouldStop()) {
try {
if (!shouldStop() && taskQueue.empty()) {
reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
reverse_lock<WaitableLock > rlock(lock);
}
while (!shouldStop() && taskQueue.empty()) {
// Wait until there is something to do.
Expand All @@ -49,22 +43,12 @@ void CScheduler::serviceQueue()

// Wait until either there is a new task, or until
// the time of the first item on the queue:

// wait_until needs boost 1.50 or later; older versions have timed_wait:
#if BOOST_VERSION < 105000
while (!shouldStop() && !taskQueue.empty() &&
newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) {
// Keep waiting until timeout
}
#else
// Some boost versions have a conflicting overload of wait_until that returns void.
// Explicitly use a template here to avoid hitting that overload.
while (!shouldStop() && !taskQueue.empty()) {
boost::chrono::system_clock::time_point timeToWaitFor = taskQueue.begin()->first;
if (newTaskScheduled.wait_until<>(lock, timeToWaitFor) == boost::cv_status::timeout)
std::chrono::steady_clock::time_point timeToWaitFor = taskQueue.begin()->first;
if (newTaskScheduled.wait_until(lock, timeToWaitFor) == std::cv_status::timeout)
break; // Exit loop after timeout, it means we reached the time of the event
}
#endif

// If there are multiple threads, the queue can empty while we're waiting (another
// thread may service the task we were waiting on).
if (shouldStop() || taskQueue.empty())
Expand All @@ -76,7 +60,7 @@ void CScheduler::serviceQueue()
{
// Unlock before calling f, so it can reschedule itself or another task
// without deadlocking:
reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
reverse_lock<WaitableLock > rlock(lock);
f();
}
} catch (...) {
Expand All @@ -91,7 +75,7 @@ void CScheduler::serviceQueue()
void CScheduler::stop(bool drain)
{
{
boost::unique_lock<boost::mutex> lock(newTaskMutex);
WaitableLock lock(newTaskMutex);
if (drain)
stopWhenEmpty = true;
else
Expand All @@ -100,18 +84,18 @@ void CScheduler::stop(bool drain)
newTaskScheduled.notify_all();
}

void CScheduler::schedule(CScheduler::Function f, boost::chrono::system_clock::time_point t)
void CScheduler::schedule(CScheduler::Function f, std::chrono::steady_clock::time_point t)
{
{
boost::unique_lock<boost::mutex> lock(newTaskMutex);
WaitableLock lock(newTaskMutex);
taskQueue.insert(std::make_pair(t, f));
}
newTaskScheduled.notify_one();
}

void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaMilliSeconds)
{
schedule(f, boost::chrono::system_clock::now() + boost::chrono::milliseconds(deltaMilliSeconds));
schedule(f, std::chrono::steady_clock::now() + std::chrono::milliseconds(deltaMilliSeconds));
}

static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaMilliSeconds)
Expand All @@ -125,10 +109,10 @@ void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaMilliSeconds
scheduleFromNow(std::bind(&Repeat, this, f, deltaMilliSeconds), deltaMilliSeconds);
}

size_t CScheduler::getQueueInfo(boost::chrono::system_clock::time_point &first,
boost::chrono::system_clock::time_point &last) const
size_t CScheduler::getQueueInfo(std::chrono::steady_clock::time_point &first,
std::chrono::steady_clock::time_point &last) const
{
boost::unique_lock<boost::mutex> lock(newTaskMutex);
WaitableLock lock(newTaskMutex);
size_t result = taskQueue.size();
if (!taskQueue.empty()) {
first = taskQueue.begin()->first;
Expand All @@ -138,7 +122,7 @@ size_t CScheduler::getQueueInfo(boost::chrono::system_clock::time_point &first,
}

bool CScheduler::AreThreadsServicingQueue() const {
boost::unique_lock<boost::mutex> lock(newTaskMutex);
WaitableLock lock(newTaskMutex);
return nThreadsServicingQueue;
}

Expand Down
16 changes: 9 additions & 7 deletions src/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
// boost::thread / boost::chrono should be ported to std::thread / std::chrono
// when we support C++11.
//
#include <boost/chrono/chrono.hpp>

#include <boost/thread.hpp>
#include <map>
#include <thread>
#include <utility>

#include <sync.h>

Expand Down Expand Up @@ -43,7 +45,7 @@ class CScheduler
typedef std::function<void(void)> Function;

// Call func at/after time t
void schedule(Function f, boost::chrono::system_clock::time_point t=boost::chrono::system_clock::now());
void schedule(Function f, std::chrono::steady_clock::time_point t=std::chrono::steady_clock::now());

// Convenience method: call f once deltaSeconds from now
void scheduleFromNow(Function f, int64_t deltaMilliSeconds);
Expand All @@ -68,16 +70,16 @@ class CScheduler

// Returns number of tasks waiting to be serviced,
// and first and last task times
size_t getQueueInfo(boost::chrono::system_clock::time_point &first,
boost::chrono::system_clock::time_point &last) const;
size_t getQueueInfo(std::chrono::steady_clock::time_point &first,
std::chrono::steady_clock::time_point &last) const;

// Returns true if there are threads actively running in serviceQueue()
bool AreThreadsServicingQueue() const;

private:
std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
boost::condition_variable newTaskScheduled;
mutable boost::mutex newTaskMutex;
std::multimap<std::chrono::steady_clock::time_point, Function> taskQueue;
std::condition_variable newTaskScheduled;
mutable std::mutex newTaskMutex;
int nThreadsServicingQueue;
bool stopRequested;
bool stopWhenEmpty;
Expand Down
Loading

0 comments on commit 434d8d7

Please sign in to comment.