aboutsummaryrefslogtreecommitdiff
path: root/src/scheduler.h
diff options
context:
space:
mode:
authorGavin Andresen <gavinandresen@gmail.com>2015-05-14 14:23:13 -0400
committerGavin Andresen <gavinandresen@gmail.com>2015-05-14 14:27:07 -0400
commitb4c219b622235e66fecd5dee1883908d5336a6c0 (patch)
treeaee090d0cd5cfd0023ec23c9295b587920527585 /src/scheduler.h
parent339321522c5c8779ef1d4b1769b23ec7a69b18d9 (diff)
parent9a1dcea2df2a6a89acf79bc41972e937d783a080 (diff)
Merge pull request #5964
9a1dcea Use CScheduler for net's DumpAddresses (Gavin Andresen) ddd0acd Create a scheduler thread for lightweight tasks (Gavin Andresen) 68d370b CScheduler unit test (Gavin Andresen) cfefe5b scheduler: fix with boost <= 1.50 (Cory Fields) ca66717 build: make libboost_chrono mandatory (Cory Fields) 928b950 CScheduler class for lightweight task scheduling (Gavin Andresen) e656560 [Qt] add defaultConfirmTarget constant to sendcoinsdialog (Philip Kaufmann)
Diffstat (limited to 'src/scheduler.h')
-rw-r--r--src/scheduler.h70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/scheduler.h b/src/scheduler.h
new file mode 100644
index 0000000000..bb383ab9f9
--- /dev/null
+++ b/src/scheduler.h
@@ -0,0 +1,70 @@
+// 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.
+
+#ifndef BITCOIN_SCHEDULER_H
+#define BITCOIN_SCHEDULER_H
+
+//
+// NOTE:
+// boost::thread / boost::function / boost::chrono should be ported to
+// std::thread / std::function / std::chrono when we support C++11.
+//
+#include <boost/function.hpp>
+#include <boost/chrono/chrono.hpp>
+#include <boost/thread.hpp>
+#include <map>
+
+//
+// Simple class for background tasks that should be run
+// periodically or once "after a while"
+//
+// Usage:
+//
+// CScheduler* s = new CScheduler();
+// s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { }
+// s->scheduleFromNow(boost::bind(Class::func, this, argument), 3);
+// boost::thread* t = new boost::thread(boost::bind(CScheduler::serviceQueue, s));
+//
+// ... then at program shutdown, clean up the thread running serviceQueue:
+// t->interrupt();
+// t->join();
+// delete t;
+// delete s; // Must be done after thread is interrupted/joined.
+//
+
+class CScheduler
+{
+public:
+ CScheduler();
+ ~CScheduler();
+
+ typedef boost::function<void(void)> Function;
+
+ // Call func at/after time t
+ void schedule(Function f, boost::chrono::system_clock::time_point t);
+
+ // Convenience method: call f once deltaSeconds from now
+ void scheduleFromNow(Function f, int64_t deltaSeconds);
+
+ // Another convenience method: call f approximately
+ // every deltaSeconds forever, starting deltaSeconds from now.
+ // To be more precise: every time f is finished, it
+ // is rescheduled to run deltaSeconds later. If you
+ // need more accurate scheduling, don't use this method.
+ void scheduleEvery(Function f, int64_t deltaSeconds);
+
+ // To keep things as simple as possible, there is no unschedule.
+
+ // Services the queue 'forever'. Should be run in a thread,
+ // and interrupted using boost::interrupt_thread
+ void serviceQueue();
+
+private:
+ std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
+ boost::condition_variable newTaskScheduled;
+ boost::mutex newTaskMutex;
+ int nThreadsServicingQueue;
+};
+
+#endif