Skip to content

Commit 928b950

Browse files
committed
CScheduler class for lightweight task scheduling
Simple class to manage a task queue that is serviced by one or more threads.
1 parent e656560 commit 928b950

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ BITCOIN_CORE_H = \
115115
rpcclient.h \
116116
rpcprotocol.h \
117117
rpcserver.h \
118+
scheduler.h \
118119
script/interpreter.h \
119120
script/script_error.h \
120121
script/script.h \
@@ -257,6 +258,7 @@ libbitcoin_common_a_SOURCES = \
257258
netbase.cpp \
258259
protocol.cpp \
259260
pubkey.cpp \
261+
scheduler.cpp \
260262
script/interpreter.cpp \
261263
script/script.cpp \
262264
script/sign.cpp \

src/scheduler.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 <assert.h>
8+
#include <boost/bind.hpp>
9+
#include <utility>
10+
11+
CScheduler::CScheduler() : nThreadsServicingQueue(0)
12+
{
13+
}
14+
15+
CScheduler::~CScheduler()
16+
{
17+
assert(nThreadsServicingQueue == 0);
18+
}
19+
20+
21+
#if BOOST_VERSION < 105000
22+
static boost::system_time toPosixTime(const boost::chrono::system_clock::time_point& t)
23+
{
24+
boost::chrono::system_clock::duration d = t.time_since_epoch();
25+
boost::chrono::microseconds usecs = boost::chrono::duration_cast<boost::chrono::microseconds>(d);
26+
boost::system_time result = boost::posix_time::from_time_t(0) +
27+
boost::posix_time::microseconds(usecs.count());
28+
return result;
29+
}
30+
#endif
31+
32+
void CScheduler::serviceQueue()
33+
{
34+
boost::unique_lock<boost::mutex> lock(newTaskMutex);
35+
++nThreadsServicingQueue;
36+
37+
// newTaskMutex is locked throughout this loop EXCEPT
38+
// when the thread is waiting or when the user's function
39+
// is called.
40+
while (1) {
41+
try {
42+
while (taskQueue.empty()) {
43+
// Wait until there is something to do.
44+
newTaskScheduled.wait(lock);
45+
}
46+
// Wait until either there is a new task, or until
47+
// the time of the first item on the queue:
48+
49+
// wait_until needs boost 1.50 or later; older versions have timed_wait:
50+
#if BOOST_VERSION < 105000
51+
while (!taskQueue.empty() && newTaskScheduled.timed_wait(lock, toPosixTime(taskQueue.begin()->first))) {
52+
// Keep waiting until timeout
53+
}
54+
#else
55+
while (!taskQueue.empty() && newTaskScheduled.wait_until(lock, taskQueue.begin()->first) != boost::cv_status::timeout) {
56+
// Keep waiting until timeout
57+
}
58+
#endif
59+
// If there are multiple threads, the queue can empty while we're waiting (another
60+
// thread may service the task we were waiting on).
61+
if (taskQueue.empty())
62+
continue;
63+
64+
Function f = taskQueue.begin()->second;
65+
taskQueue.erase(taskQueue.begin());
66+
67+
// Unlock before calling f, so it can reschedule itself or another task
68+
// without deadlocking:
69+
lock.unlock();
70+
f();
71+
lock.lock();
72+
} catch (...) {
73+
--nThreadsServicingQueue;
74+
throw;
75+
}
76+
}
77+
}
78+
79+
void CScheduler::schedule(CScheduler::Function f, boost::chrono::system_clock::time_point t)
80+
{
81+
{
82+
boost::unique_lock<boost::mutex> lock(newTaskMutex);
83+
taskQueue.insert(std::make_pair(t, f));
84+
}
85+
newTaskScheduled.notify_one();
86+
}
87+
88+
void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaSeconds)
89+
{
90+
schedule(f, boost::chrono::system_clock::now() + boost::chrono::seconds(deltaSeconds));
91+
}
92+
93+
static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaSeconds)
94+
{
95+
f();
96+
s->scheduleFromNow(boost::bind(&Repeat, s, f, deltaSeconds), deltaSeconds);
97+
}
98+
99+
void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaSeconds)
100+
{
101+
scheduleFromNow(boost::bind(&Repeat, this, f, deltaSeconds), deltaSeconds);
102+
}

src/scheduler.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
#ifndef BITCOIN_SCHEDULER_H
6+
#define BITCOIN_SCHEDULER_H
7+
8+
//
9+
// NOTE:
10+
// boost::thread / boost::function / boost::chrono should be ported to
11+
// std::thread / std::function / std::chrono when we support C++11.
12+
//
13+
#include <boost/function.hpp>
14+
#include <boost/chrono/chrono.hpp>
15+
#include <boost/thread.hpp>
16+
#include <map>
17+
18+
//
19+
// Simple class for background tasks that should be run
20+
// periodically or once "after a while"
21+
//
22+
// Usage:
23+
//
24+
// CScheduler* s = new CScheduler();
25+
// s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { }
26+
// s->scheduleFromNow(boost::bind(Class::func, this, argument), 3);
27+
// boost::thread* t = new boost::thread(boost::bind(CScheduler::serviceQueue, s));
28+
//
29+
// ... then at program shutdown, clean up the thread running serviceQueue:
30+
// t->interrupt();
31+
// t->join();
32+
// delete t;
33+
// delete s; // Must be done after thread is interrupted/joined.
34+
//
35+
36+
class CScheduler
37+
{
38+
public:
39+
CScheduler();
40+
~CScheduler();
41+
42+
typedef boost::function<void(void)> Function;
43+
44+
// Call func at/after time t
45+
void schedule(Function f, boost::chrono::system_clock::time_point t);
46+
47+
// Convenience method: call f once deltaSeconds from now
48+
void scheduleFromNow(Function f, int64_t deltaSeconds);
49+
50+
// Another convenience method: call f approximately
51+
// every deltaSeconds forever, starting deltaSeconds from now.
52+
// To be more precise: every time f is finished, it
53+
// is rescheduled to run deltaSeconds later. If you
54+
// need more accurate scheduling, don't use this method.
55+
void scheduleEvery(Function f, int64_t deltaSeconds);
56+
57+
// To keep things as simple as possible, there is no unschedule.
58+
59+
// Services the queue 'forever'. Should be run in a thread,
60+
// and interrupted using boost::interrupt_thread
61+
void serviceQueue();
62+
63+
private:
64+
std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
65+
boost::condition_variable newTaskScheduled;
66+
boost::mutex newTaskMutex;
67+
int nThreadsServicingQueue;
68+
};
69+
70+
#endif

0 commit comments

Comments
 (0)