2020-01-14 16:17:38 -03:00
|
|
|
// Copyright (c) 2015-2020 The Bitcoin Core developers
|
2015-04-02 11:33:45 -03: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:
|
2020-02-27 23:04:04 -03:00
|
|
|
// boost::thread should be ported to std::thread
|
2017-05-13 12:52:14 -03:00
|
|
|
// when we support C++11.
|
2015-04-02 11:33:45 -03:00
|
|
|
//
|
2020-02-27 23:04:04 -03:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
2015-04-02 11:33:45 -03:00
|
|
|
#include <map>
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <sync.h>
|
2017-04-10 15:55:49 -03:00
|
|
|
|
2015-04-02 11:33:45 -03: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 17:36:13 -03:00
|
|
|
// s->scheduleFromNow(std::bind(Class::func, this, argument), 3);
|
2018-10-17 12:51:17 -03:00
|
|
|
// boost::thread* t = new boost::thread(std::bind(CScheduler::serviceQueue, s));
|
2015-04-02 11:33:45 -03:00
|
|
|
//
|
2020-02-27 23:04:04 -03:00
|
|
|
// ... then at program shutdown, make sure to call stop() to clean up the thread(s) running serviceQueue:
|
|
|
|
// s->stop();
|
2015-04-02 11:33:45 -03:00
|
|
|
// t->join();
|
|
|
|
// delete t;
|
|
|
|
// delete s; // Must be done after thread is interrupted/joined.
|
|
|
|
//
|
|
|
|
|
|
|
|
class CScheduler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CScheduler();
|
|
|
|
~CScheduler();
|
|
|
|
|
2018-09-13 14:36:41 -03:00
|
|
|
typedef std::function<void()> Function;
|
2015-04-02 11:33:45 -03:00
|
|
|
|
|
|
|
// Call func at/after time t
|
2020-02-27 23:04:04 -03:00
|
|
|
void schedule(Function f, std::chrono::system_clock::time_point t);
|
2015-04-02 11:33:45 -03:00
|
|
|
|
2019-02-10 19:17:32 -03:00
|
|
|
// Convenience method: call f once deltaMilliSeconds from now
|
2017-01-20 17:36:13 -03:00
|
|
|
void scheduleFromNow(Function f, int64_t deltaMilliSeconds);
|
2015-04-02 11:33:45 -03:00
|
|
|
|
|
|
|
// Another convenience method: call f approximately
|
2019-02-10 19:17:32 -03:00
|
|
|
// every deltaMilliSeconds forever, starting deltaMilliSeconds from now.
|
2015-04-02 11:33:45 -03:00
|
|
|
// To be more precise: every time f is finished, it
|
2019-02-10 19:17:32 -03:00
|
|
|
// is rescheduled to run deltaMilliSeconds later. If you
|
2015-04-02 11:33:45 -03:00
|
|
|
// need more accurate scheduling, don't use this method.
|
2017-01-20 17:36:13 -03:00
|
|
|
void scheduleEvery(Function f, int64_t deltaMilliSeconds);
|
2015-04-02 11:33:45 -03:00
|
|
|
|
2020-01-28 20:26:32 -03:00
|
|
|
/**
|
|
|
|
* Mock the scheduler to fast forward in time.
|
|
|
|
* Iterates through items on taskQueue and reschedules them
|
|
|
|
* to be delta_seconds sooner.
|
|
|
|
*/
|
2020-02-27 23:04:04 -03:00
|
|
|
void MockForward(std::chrono::seconds delta_seconds);
|
2020-01-28 20:26:32 -03:00
|
|
|
|
2015-04-02 11:33:45 -03: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 13:40:36 -03: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
|
2020-02-27 23:04:04 -03:00
|
|
|
size_t getQueueInfo(std::chrono::system_clock::time_point &first,
|
|
|
|
std::chrono::system_clock::time_point &last) const;
|
2015-05-15 13:40:36 -03:00
|
|
|
|
2017-07-10 21:08:19 -04:00
|
|
|
// Returns true if there are threads actively running in serviceQueue()
|
|
|
|
bool AreThreadsServicingQueue() const;
|
|
|
|
|
2015-04-02 11:33:45 -03:00
|
|
|
private:
|
2020-02-27 23:04:04 -03:00
|
|
|
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);
|
|
|
|
bool stopRequested GUARDED_BY(newTaskMutex);
|
|
|
|
bool stopWhenEmpty GUARDED_BY(newTaskMutex);
|
|
|
|
bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex) { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
|
2015-04-02 11:33:45 -03:00
|
|
|
};
|
|
|
|
|
2017-04-10 15:55:49 -03:00
|
|
|
/**
|
|
|
|
* Class used by CScheduler clients which may schedule multiple jobs
|
2018-05-16 11:10:31 -04:00
|
|
|
* which are required to be run serially. Jobs may not be run on the
|
|
|
|
* same thread, but no two jobs will be executed
|
|
|
|
* at the same time and memory will be release-acquire consistent
|
|
|
|
* (the scheduler will internally do an acquire before invoking a callback
|
|
|
|
* as well as a release at the end). In practice this means that a callback
|
|
|
|
* B() will be able to observe all of the effects of callback A() which executed
|
|
|
|
* before it.
|
2017-04-10 15:55:49 -03:00
|
|
|
*/
|
|
|
|
class SingleThreadedSchedulerClient {
|
|
|
|
private:
|
|
|
|
CScheduler *m_pscheduler;
|
|
|
|
|
2020-01-07 13:14:15 -03:00
|
|
|
RecursiveMutex m_cs_callbacks_pending;
|
2018-09-13 14:36:41 -03:00
|
|
|
std::list<std::function<void ()>> m_callbacks_pending GUARDED_BY(m_cs_callbacks_pending);
|
2018-04-30 06:17:06 -03:00
|
|
|
bool m_are_callbacks_running GUARDED_BY(m_cs_callbacks_pending) = false;
|
2017-04-10 15:55:49 -03:00
|
|
|
|
|
|
|
void MaybeScheduleProcessQueue();
|
|
|
|
void ProcessQueue();
|
|
|
|
|
|
|
|
public:
|
2017-08-01 06:22:41 -04:00
|
|
|
explicit SingleThreadedSchedulerClient(CScheduler *pschedulerIn) : m_pscheduler(pschedulerIn) {}
|
2018-05-16 11:10:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a callback to be executed. Callbacks are executed serially
|
2018-08-01 12:19:45 -04:00
|
|
|
* and memory is release-acquire consistent between callback executions.
|
2018-08-14 04:27:30 -03:00
|
|
|
* Practically, this means that callbacks can behave as if they are executed
|
2018-05-16 11:10:31 -04:00
|
|
|
* in order by a single thread.
|
|
|
|
*/
|
2018-09-13 14:36:41 -03:00
|
|
|
void AddToProcessQueue(std::function<void ()> func);
|
2017-06-27 19:07:52 -04:00
|
|
|
|
|
|
|
// Processes all remaining queue members on the calling thread, blocking until queue is empty
|
2017-07-10 21:08:19 -04:00
|
|
|
// Must be called after the CScheduler has no remaining processing threads!
|
2017-06-27 19:07:52 -04:00
|
|
|
void EmptyQueue();
|
2017-12-04 20:31:36 -03:00
|
|
|
|
|
|
|
size_t CallbacksPending();
|
2017-04-10 15:55:49 -03:00
|
|
|
};
|
|
|
|
|
2015-04-02 11:33:45 -03:00
|
|
|
#endif
|