2015-04-02 16:33:45 +02:00
|
|
|
// Copyright (c) 2015 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "scheduler.h"
|
|
|
|
|
2015-09-03 18:53:00 +02:00
|
|
|
#include "reverselock.h"
|
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <utility>
|
|
|
|
|
2015-05-15 18:40:36 +02:00
|
|
|
CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CScheduler::~CScheduler()
|
|
|
|
{
|
|
|
|
assert(nThreadsServicingQueue == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if BOOST_VERSION < 105000
|
|
|
|
static boost::system_time toPosixTime(const boost::chrono::system_clock::time_point& t)
|
|
|
|
{
|
2015-04-10 02:11:14 +02:00
|
|
|
return boost::posix_time::from_time_t(boost::chrono::system_clock::to_time_t(t));
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void CScheduler::serviceQueue()
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::mutex> lock(newTaskMutex);
|
|
|
|
++nThreadsServicingQueue;
|
|
|
|
|
|
|
|
// newTaskMutex is locked throughout this loop EXCEPT
|
|
|
|
// when the thread is waiting or when the user's function
|
|
|
|
// is called.
|
2015-05-15 18:40:36 +02:00
|
|
|
while (!shouldStop()) {
|
2015-04-02 16:33:45 +02:00
|
|
|
try {
|
2015-05-15 18:40:36 +02:00
|
|
|
while (!shouldStop() && taskQueue.empty()) {
|
2015-04-02 16:33:45 +02:00
|
|
|
// Wait until there is something to do.
|
|
|
|
newTaskScheduled.wait(lock);
|
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
|
|
|
|
// Wait until either there is a new task, or until
|
|
|
|
// the time of the first item on the queue:
|
2015-04-02 16:33:45 +02:00
|
|
|
|
|
|
|
// wait_until needs boost 1.50 or later; older versions have timed_wait:
|
|
|
|
#if BOOST_VERSION < 105000
|
2015-05-15 18:40:36 +02:00
|
|
|
while (!shouldStop() && !taskQueue.empty() &&
|
|
|
|
newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) {
|
2015-04-02 16:33:45 +02:00
|
|
|
// Keep waiting until timeout
|
|
|
|
}
|
|
|
|
#else
|
2015-06-15 21:46:02 +02:00
|
|
|
// Some boost versions have a conflicting overload of wait_until that returns void.
|
|
|
|
// Explicitly use a template here to avoid hitting that overload.
|
2015-05-15 18:40:36 +02:00
|
|
|
while (!shouldStop() && !taskQueue.empty() &&
|
2015-06-15 21:46:02 +02:00
|
|
|
newTaskScheduled.wait_until<>(lock, taskQueue.begin()->first) != boost::cv_status::timeout) {
|
2015-04-02 16:33:45 +02:00
|
|
|
// Keep waiting until timeout
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// If there are multiple threads, the queue can empty while we're waiting (another
|
|
|
|
// thread may service the task we were waiting on).
|
2015-05-15 18:40:36 +02:00
|
|
|
if (shouldStop() || taskQueue.empty())
|
2015-04-02 16:33:45 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Function f = taskQueue.begin()->second;
|
|
|
|
taskQueue.erase(taskQueue.begin());
|
|
|
|
|
2015-08-17 23:30:46 +02:00
|
|
|
{
|
|
|
|
// Unlock before calling f, so it can reschedule itself or another task
|
|
|
|
// without deadlocking:
|
2015-09-03 18:53:00 +02:00
|
|
|
reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
|
2015-08-17 23:30:46 +02:00
|
|
|
f();
|
|
|
|
}
|
2015-04-02 16:33:45 +02:00
|
|
|
} catch (...) {
|
|
|
|
--nThreadsServicingQueue;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
--nThreadsServicingQueue;
|
2016-05-06 11:01:51 +02:00
|
|
|
newTaskScheduled.notify_one();
|
2015-05-15 18:40:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CScheduler::stop(bool drain)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::mutex> lock(newTaskMutex);
|
|
|
|
if (drain)
|
|
|
|
stopWhenEmpty = true;
|
|
|
|
else
|
|
|
|
stopRequested = true;
|
|
|
|
}
|
|
|
|
newTaskScheduled.notify_all();
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CScheduler::schedule(CScheduler::Function f, boost::chrono::system_clock::time_point t)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::mutex> lock(newTaskMutex);
|
|
|
|
taskQueue.insert(std::make_pair(t, f));
|
|
|
|
}
|
|
|
|
newTaskScheduled.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaSeconds)
|
|
|
|
{
|
|
|
|
schedule(f, boost::chrono::system_clock::now() + boost::chrono::seconds(deltaSeconds));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaSeconds)
|
|
|
|
{
|
|
|
|
f();
|
|
|
|
s->scheduleFromNow(boost::bind(&Repeat, s, f, deltaSeconds), deltaSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaSeconds)
|
|
|
|
{
|
|
|
|
scheduleFromNow(boost::bind(&Repeat, this, f, deltaSeconds), deltaSeconds);
|
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
|
|
|
|
size_t CScheduler::getQueueInfo(boost::chrono::system_clock::time_point &first,
|
|
|
|
boost::chrono::system_clock::time_point &last) const
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::mutex> lock(newTaskMutex);
|
|
|
|
size_t result = taskQueue.size();
|
|
|
|
if (!taskQueue.empty()) {
|
|
|
|
first = taskQueue.begin()->first;
|
|
|
|
last = taskQueue.rbegin()->first;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|