diff --git a/src/scheduler.h b/src/scheduler.h --- a/src/scheduler.h +++ b/src/scheduler.h @@ -26,8 +26,7 @@ // CScheduler* s = new CScheduler(); // s->scheduleFromNow(doSomething, 11); // Assuming a: void doSomething() { } // s->scheduleFromNow(std::bind(Class::func, this, argument), 3); -// boost::thread* t = new boost::thread(boost::bind(CScheduler::serviceQueue, -// s)); +// boost::thread* t = new boost::thread(std::bind(CScheduler::serviceQueue, s)); // // ... then at program shutdown, clean up the thread running serviceQueue: // t->interrupt(); @@ -41,8 +40,8 @@ CScheduler(); ~CScheduler(); - typedef std::function Function; - typedef std::function Predicate; + typedef std::function Function; + typedef std::function Predicate; // Call func at/after time t void schedule(Function f, boost::chrono::system_clock::time_point t = @@ -51,10 +50,11 @@ // Convenience method: call f once deltaMilliSeconds from now void scheduleFromNow(Function f, int64_t deltaMilliSeconds); - // 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. + // Another convenience method: call p approximately every deltaMilliSeconds + // forever, starting deltaMilliSeconds from now untill p returns false. To + // be more precise: every time p is finished, it is rescheduled to run + // deltaMilliSeconds later. If you need more accurate scheduling, don't use + // this method. void scheduleEvery(Predicate p, int64_t deltaMilliSeconds); // To keep things as simple as possible, there is no unschedule. @@ -99,7 +99,7 @@ CScheduler *m_pscheduler; CCriticalSection m_cs_callbacks_pending; - std::list> + std::list> m_callbacks_pending GUARDED_BY(m_cs_callbacks_pending); bool m_are_callbacks_running GUARDED_BY(m_cs_callbacks_pending) = false; @@ -109,11 +109,11 @@ public: explicit SingleThreadedSchedulerClient(CScheduler *pschedulerIn) : m_pscheduler(pschedulerIn) {} - void AddToProcessQueue(std::function func); + void AddToProcessQueue(std::function func); // Processes all remaining queue members on the calling thread, blocking - // until queue is empty - // Must be called after the CScheduler has no remaining processing threads! + // until queue is empty. Must be called after the CScheduler has no + // remaining processing threads! void EmptyQueue(); size_t CallbacksPending(); diff --git a/src/scheduler.cpp b/src/scheduler.cpp --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -7,8 +7,6 @@ #include #include -#include - #include #include @@ -107,14 +105,14 @@ static void Repeat(CScheduler *s, CScheduler::Predicate p, int64_t deltaMilliSeconds) { if (p()) { - s->scheduleFromNow(boost::bind(&Repeat, s, p, deltaMilliSeconds), + s->scheduleFromNow(std::bind(&Repeat, s, p, deltaMilliSeconds), deltaMilliSeconds); } } void CScheduler::scheduleEvery(CScheduler::Predicate p, int64_t deltaMilliSeconds) { - scheduleFromNow(boost::bind(&Repeat, this, p, deltaMilliSeconds), + scheduleFromNow(std::bind(&Repeat, this, p, deltaMilliSeconds), deltaMilliSeconds); } @@ -141,19 +139,27 @@ // Try to avoid scheduling too many copies here, but if we // accidentally have two ProcessQueue's scheduled at once its // not a big deal. - if (m_are_callbacks_running) return; - if (m_callbacks_pending.empty()) return; + if (m_are_callbacks_running) { + return; + } + if (m_callbacks_pending.empty()) { + return; + } } m_pscheduler->schedule( std::bind(&SingleThreadedSchedulerClient::ProcessQueue, this)); } void SingleThreadedSchedulerClient::ProcessQueue() { - std::function callback; + std::function callback; { LOCK(m_cs_callbacks_pending); - if (m_are_callbacks_running) return; - if (m_callbacks_pending.empty()) return; + if (m_are_callbacks_running) { + return; + } + if (m_callbacks_pending.empty()) { + return; + } m_are_callbacks_running = true; callback = std::move(m_callbacks_pending.front()); @@ -161,8 +167,8 @@ } // RAII the setting of fCallbacksRunning and calling - // MaybeScheduleProcessQueue - // to ensure both happen safely even if callback() throws. + // MaybeScheduleProcessQueue to ensure both happen safely even if callback() + // throws. struct RAIICallbacksRunning { SingleThreadedSchedulerClient *instance; explicit RAIICallbacksRunning(SingleThreadedSchedulerClient *_instance) @@ -180,7 +186,7 @@ } void SingleThreadedSchedulerClient::AddToProcessQueue( - std::function func) { + std::function func) { assert(m_pscheduler); {