aboutsummaryrefslogtreecommitdiff
path: root/src/scheduler.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/scheduler.h')
-rw-r--r--src/scheduler.h62
1 files changed, 37 insertions, 25 deletions
diff --git a/src/scheduler.h b/src/scheduler.h
index 7080adf34c..1e64195484 100644
--- a/src/scheduler.h
+++ b/src/scheduler.h
@@ -7,11 +7,12 @@
//
// NOTE:
-// boost::thread / boost::chrono should be ported to std::thread / std::chrono
+// boost::thread should be ported to std::thread
// when we support C++11.
//
-#include <boost/chrono/chrono.hpp>
-#include <boost/thread.hpp>
+#include <condition_variable>
+#include <functional>
+#include <list>
#include <map>
#include <sync.h>
@@ -23,12 +24,12 @@
// Usage:
//
// CScheduler* s = new CScheduler();
-// s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { }
-// s->scheduleFromNow(std::bind(Class::func, this, argument), 3);
+// s->scheduleFromNow(doSomething, std::chrono::milliseconds{11}); // Assuming a: void doSomething() { }
+// s->scheduleFromNow([=] { this->func(argument); }, std::chrono::milliseconds{3});
// boost::thread* t = new boost::thread(std::bind(CScheduler::serviceQueue, s));
//
-// ... then at program shutdown, clean up the thread running serviceQueue:
-// t->interrupt();
+// ... then at program shutdown, make sure to call stop() to clean up the thread(s) running serviceQueue:
+// s->stop();
// t->join();
// delete t;
// delete s; // Must be done after thread is interrupted/joined.
@@ -43,17 +44,28 @@ public:
typedef std::function<void()> Function;
// Call func at/after time t
- void schedule(Function f, boost::chrono::system_clock::time_point t=boost::chrono::system_clock::now());
+ void schedule(Function f, std::chrono::system_clock::time_point t);
- // Convenience method: call f once deltaMilliSeconds from now
- void scheduleFromNow(Function f, int64_t deltaMilliSeconds);
+ /** Call f once after the delta has passed */
+ void scheduleFromNow(Function f, std::chrono::milliseconds delta)
+ {
+ schedule(std::move(f), std::chrono::system_clock::now() + delta);
+ }
- // Another convenience method: call f approximately
- // every deltaMilliSeconds forever, starting deltaMilliSeconds from now.
- // To be more precise: every time f is finished, it
- // is rescheduled to run deltaMilliSeconds later. If you
- // need more accurate scheduling, don't use this method.
- void scheduleEvery(Function f, int64_t deltaMilliSeconds);
+ /**
+ * Repeat f until the scheduler is stopped. First run is after delta has passed once.
+ *
+ * The timing is not exact: Every time f is finished, it is rescheduled to run again after delta. If you need more
+ * accurate scheduling, don't use this method.
+ */
+ void scheduleEvery(Function f, std::chrono::milliseconds delta);
+
+ /**
+ * Mock the scheduler to fast forward in time.
+ * Iterates through items on taskQueue and reschedules them
+ * to be delta_seconds sooner.
+ */
+ void MockForward(std::chrono::seconds delta_seconds);
// To keep things as simple as possible, there is no unschedule.
@@ -68,20 +80,20 @@ public:
// Returns number of tasks waiting to be serviced,
// and first and last task times
- size_t getQueueInfo(boost::chrono::system_clock::time_point &first,
- boost::chrono::system_clock::time_point &last) const;
+ size_t getQueueInfo(std::chrono::system_clock::time_point &first,
+ std::chrono::system_clock::time_point &last) const;
// Returns true if there are threads actively running in serviceQueue()
bool AreThreadsServicingQueue() const;
private:
- std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
- boost::condition_variable newTaskScheduled;
- mutable boost::mutex newTaskMutex;
- int nThreadsServicingQueue;
- bool stopRequested;
- bool stopWhenEmpty;
- bool shouldStop() const { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
+ mutable Mutex newTaskMutex;
+ std::condition_variable newTaskScheduled;
+ std::multimap<std::chrono::system_clock::time_point, Function> taskQueue GUARDED_BY(newTaskMutex);
+ int nThreadsServicingQueue GUARDED_BY(newTaskMutex){0};
+ bool stopRequested GUARDED_BY(newTaskMutex){false};
+ bool stopWhenEmpty GUARDED_BY(newTaskMutex){false};
+ bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex) { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
};
/**