1 // Copyright (c) 2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 #include <boost/bind.hpp>
11 CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false)
15 CScheduler::~CScheduler()
17 assert(nThreadsServicingQueue == 0);
21 #if BOOST_VERSION < 105000
22 static boost::system_time toPosixTime(const boost::chrono::system_clock::time_point& t)
24 return boost::posix_time::from_time_t(boost::chrono::system_clock::to_time_t(t));
28 void CScheduler::serviceQueue()
30 boost::unique_lock<boost::mutex> lock(newTaskMutex);
31 ++nThreadsServicingQueue;
33 // newTaskMutex is locked throughout this loop EXCEPT
34 // when the thread is waiting or when the user's function
36 while (!shouldStop()) {
38 while (!shouldStop() && taskQueue.empty()) {
39 // Wait until there is something to do.
40 newTaskScheduled.wait(lock);
43 // Wait until either there is a new task, or until
44 // the time of the first item on the queue:
46 // wait_until needs boost 1.50 or later; older versions have timed_wait:
47 #if BOOST_VERSION < 105000
48 while (!shouldStop() && !taskQueue.empty() &&
49 newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) {
50 // Keep waiting until timeout
53 while (!shouldStop() && !taskQueue.empty() &&
54 newTaskScheduled.wait_until(lock, taskQueue.begin()->first) != boost::cv_status::timeout) {
55 // Keep waiting until timeout
58 // If there are multiple threads, the queue can empty while we're waiting (another
59 // thread may service the task we were waiting on).
60 if (shouldStop() || taskQueue.empty())
63 Function f = taskQueue.begin()->second;
64 taskQueue.erase(taskQueue.begin());
66 // Unlock before calling f, so it can reschedule itself or another task
67 // without deadlocking:
72 --nThreadsServicingQueue;
76 --nThreadsServicingQueue;
79 void CScheduler::stop(bool drain)
82 boost::unique_lock<boost::mutex> lock(newTaskMutex);
88 newTaskScheduled.notify_all();
91 void CScheduler::schedule(CScheduler::Function f, boost::chrono::system_clock::time_point t)
94 boost::unique_lock<boost::mutex> lock(newTaskMutex);
95 taskQueue.insert(std::make_pair(t, f));
97 newTaskScheduled.notify_one();
100 void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaSeconds)
102 schedule(f, boost::chrono::system_clock::now() + boost::chrono::seconds(deltaSeconds));
105 static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaSeconds)
108 s->scheduleFromNow(boost::bind(&Repeat, s, f, deltaSeconds), deltaSeconds);
111 void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaSeconds)
113 scheduleFromNow(boost::bind(&Repeat, this, f, deltaSeconds), deltaSeconds);
116 size_t CScheduler::getQueueInfo(boost::chrono::system_clock::time_point &first,
117 boost::chrono::system_clock::time_point &last) const
119 boost::unique_lock<boost::mutex> lock(newTaskMutex);
120 size_t result = taskQueue.size();
121 if (!taskQueue.empty()) {
122 first = taskQueue.begin()->first;
123 last = taskQueue.rbegin()->first;