2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2015-2017 The Bitcoin Core developers
|
2015-04-02 16:33:45 +02:00
|
|
|
// 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:
|
2017-05-13 17:52:14 +02:00
|
|
|
// boost::thread / boost::chrono should be ported to std::thread / std::chrono
|
|
|
|
// when we support C++11.
|
2015-04-02 16:33:45 +02:00
|
|
|
//
|
|
|
|
#include <boost/chrono/chrono.hpp>
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
#include <map>
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <sync.h>
|
2017-04-10 20:55:49 +02:00
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
//
|
|
|
|
// 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() { }
|
2017-01-20 21:36:13 +01:00
|
|
|
// s->scheduleFromNow(std::bind(Class::func, this, argument), 3);
|
2015-04-02 16:33:45 +02:00
|
|
|
// 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();
|
|
|
|
|
2017-01-20 21:36:13 +01:00
|
|
|
typedef std::function<void(void)> Function;
|
2015-04-02 16:33:45 +02:00
|
|
|
|
|
|
|
// Call func at/after time t
|
2017-01-20 21:10:43 +01:00
|
|
|
void schedule(Function f, boost::chrono::system_clock::time_point t=boost::chrono::system_clock::now());
|
2015-04-02 16:33:45 +02:00
|
|
|
|
|
|
|
// Convenience method: call f once deltaSeconds from now
|
2017-01-20 21:36:13 +01:00
|
|
|
void scheduleFromNow(Function f, int64_t deltaMilliSeconds);
|
2015-04-02 16:33:45 +02:00
|
|
|
|
|
|
|
// 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.
|
2017-01-20 21:36:13 +01:00
|
|
|
void scheduleEvery(Function f, int64_t deltaMilliSeconds);
|
2015-04-02 16:33:45 +02:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2015-05-15 18:40:36 +02:00
|
|
|
// Tell any threads running serviceQueue to stop as soon as they're
|
|
|
|
// done servicing whatever task they're currently servicing (drain=false)
|
|
|
|
// or when there is no work left to be done (drain=true)
|
|
|
|
void stop(bool drain=false);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2017-07-11 03:08:19 +02:00
|
|
|
// Returns true if there are threads actively running in serviceQueue()
|
|
|
|
bool AreThreadsServicingQueue() const;
|
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
private:
|
|
|
|
std::multimap<boost::chrono::system_clock::time_point, Function> taskQueue;
|
|
|
|
boost::condition_variable newTaskScheduled;
|
2015-05-15 18:40:36 +02:00
|
|
|
mutable boost::mutex newTaskMutex;
|
2015-04-02 16:33:45 +02:00
|
|
|
int nThreadsServicingQueue;
|
2015-05-15 18:40:36 +02:00
|
|
|
bool stopRequested;
|
|
|
|
bool stopWhenEmpty;
|
2017-03-09 13:34:54 +01:00
|
|
|
bool shouldStop() const { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
|
2015-04-02 16:33:45 +02:00
|
|
|
};
|
|
|
|
|
2017-04-10 20:55:49 +02:00
|
|
|
/**
|
|
|
|
* Class used by CScheduler clients which may schedule multiple jobs
|
|
|
|
* which are required to be run serially. Does not require such jobs
|
|
|
|
* to be executed on the same thread, but no two jobs will be executed
|
|
|
|
* at the same time.
|
|
|
|
*/
|
|
|
|
class SingleThreadedSchedulerClient {
|
|
|
|
private:
|
|
|
|
CScheduler *m_pscheduler;
|
|
|
|
|
|
|
|
CCriticalSection m_cs_callbacks_pending;
|
|
|
|
std::list<std::function<void (void)>> m_callbacks_pending;
|
|
|
|
bool m_are_callbacks_running = false;
|
|
|
|
|
|
|
|
void MaybeScheduleProcessQueue();
|
|
|
|
void ProcessQueue();
|
|
|
|
|
|
|
|
public:
|
2017-08-01 12:22:41 +02:00
|
|
|
explicit SingleThreadedSchedulerClient(CScheduler *pschedulerIn) : m_pscheduler(pschedulerIn) {}
|
2017-04-10 20:55:49 +02:00
|
|
|
void AddToProcessQueue(std::function<void (void)> func);
|
2017-06-28 01:07:52 +02:00
|
|
|
|
|
|
|
// Processes all remaining queue members on the calling thread, blocking until queue is empty
|
2017-07-11 03:08:19 +02:00
|
|
|
// Must be called after the CScheduler has no remaining processing threads!
|
2017-06-28 01:07:52 +02:00
|
|
|
void EmptyQueue();
|
2017-12-05 00:31:36 +01:00
|
|
|
|
|
|
|
size_t CallbacksPending();
|
2017-04-10 20:55:49 +02:00
|
|
|
};
|
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
#endif
|