Line data Source code
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.
4 :
5 : #include "scheduler.h"
6 :
7 : #include "reverselock.h"
8 :
9 : #include <assert.h>
10 : #include <boost/bind.hpp>
11 : #include <utility>
12 :
13 190 : CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false)
14 : {
15 95 : }
16 :
17 190 : CScheduler::~CScheduler()
18 : {
19 95 : assert(nThreadsServicingQueue == 0);
20 95 : }
21 :
22 :
23 : #if BOOST_VERSION < 105000
24 : static boost::system_time toPosixTime(const boost::chrono::system_clock::time_point& t)
25 : {
26 : return boost::posix_time::from_time_t(boost::chrono::system_clock::to_time_t(t));
27 : }
28 : #endif
29 :
30 104 : void CScheduler::serviceQueue()
31 : {
32 104 : boost::unique_lock<boost::mutex> lock(newTaskMutex);
33 104 : ++nThreadsServicingQueue;
34 :
35 : // newTaskMutex is locked throughout this loop EXCEPT
36 : // when the thread is waiting or when the user's function
37 : // is called.
38 638 : while (!shouldStop()) {
39 : try {
40 1378 : while (!shouldStop() && taskQueue.empty()) {
41 : // Wait until there is something to do.
42 166 : newTaskScheduled.wait(lock);
43 : }
44 :
45 : // Wait until either there is a new task, or until
46 : // the time of the first item on the queue:
47 :
48 : // wait_until needs boost 1.50 or later; older versions have timed_wait:
49 : #if BOOST_VERSION < 105000
50 : while (!shouldStop() && !taskQueue.empty() &&
51 : newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) {
52 : // Keep waiting until timeout
53 : }
54 : #else
55 : // Some boost versions have a conflicting overload of wait_until that returns void.
56 : // Explicitly use a template here to avoid hitting that overload.
57 2635 : while (!shouldStop() && !taskQueue.empty() &&
58 1090 : newTaskScheduled.wait_until<>(lock, taskQueue.begin()->first) != boost::cv_status::timeout) {
59 : // Keep waiting until timeout
60 : }
61 : #endif
62 : // If there are multiple threads, the queue can empty while we're waiting (another
63 : // thread may service the task we were waiting on).
64 854 : if (shouldStop() || taskQueue.empty())
65 30 : continue;
66 :
67 1200 : Function f = taskQueue.begin()->second;
68 800 : taskQueue.erase(taskQueue.begin());
69 :
70 : {
71 : // Unlock before calling f, so it can reschedule itself or another task
72 : // without deadlocking:
73 : reverse_lock<boost::unique_lock<boost::mutex> > rlock(lock);
74 400 : f();
75 : }
76 94 : } catch (...) {
77 94 : --nThreadsServicingQueue;
78 94 : throw;
79 : }
80 : }
81 10 : --nThreadsServicingQueue;
82 10 : }
83 :
84 1 : void CScheduler::stop(bool drain)
85 : {
86 : {
87 1 : boost::unique_lock<boost::mutex> lock(newTaskMutex);
88 1 : if (drain)
89 1 : stopWhenEmpty = true;
90 : else
91 0 : stopRequested = true;
92 : }
93 1 : newTaskScheduled.notify_all();
94 1 : }
95 :
96 588 : void CScheduler::schedule(CScheduler::Function f, boost::chrono::system_clock::time_point t)
97 : {
98 : {
99 588 : boost::unique_lock<boost::mutex> lock(newTaskMutex);
100 2352 : taskQueue.insert(std::make_pair(t, f));
101 : }
102 588 : newTaskScheduled.notify_one();
103 588 : }
104 :
105 188 : void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaSeconds)
106 : {
107 564 : schedule(f, boost::chrono::system_clock::now() + boost::chrono::seconds(deltaSeconds));
108 188 : }
109 :
110 0 : static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaSeconds)
111 : {
112 0 : f();
113 0 : s->scheduleFromNow(boost::bind(&Repeat, s, f, deltaSeconds), deltaSeconds);
114 0 : }
115 :
116 188 : void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaSeconds)
117 : {
118 752 : scheduleFromNow(boost::bind(&Repeat, this, f, deltaSeconds), deltaSeconds);
119 188 : }
120 :
121 2 : size_t CScheduler::getQueueInfo(boost::chrono::system_clock::time_point &first,
122 : boost::chrono::system_clock::time_point &last) const
123 : {
124 2 : boost::unique_lock<boost::mutex> lock(newTaskMutex);
125 4 : size_t result = taskQueue.size();
126 4 : if (!taskQueue.empty()) {
127 3 : first = taskQueue.begin()->first;
128 2 : last = taskQueue.rbegin()->first;
129 : }
130 4 : return result;
131 288 : }
|