2023-04-25 13:51:26 +02:00
|
|
|
// Copyright (c) 2015-2020 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
|
|
|
|
|
2022-05-04 10:54:19 +02:00
|
|
|
#include <attributes.h>
|
|
|
|
#include <sync.h>
|
|
|
|
#include <threadsafety.h>
|
|
|
|
|
|
|
|
#include <chrono>
|
2020-03-06 20:47:49 +01:00
|
|
|
#include <condition_variable>
|
2022-05-04 10:54:19 +02:00
|
|
|
#include <cstddef>
|
2020-03-06 20:47:49 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <list>
|
2015-04-02 16:33:45 +02:00
|
|
|
#include <map>
|
2022-12-25 09:25:19 +01:00
|
|
|
#include <thread>
|
2022-05-04 10:54:19 +02:00
|
|
|
#include <utility>
|
2017-07-11 09:30:36 +02:00
|
|
|
|
2020-05-28 14:04:40 +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, std::chrono::milliseconds{11}); // Assuming a: void doSomething() { }
|
|
|
|
* s->scheduleFromNow([=] { this->func(argument); }, std::chrono::milliseconds{3});
|
|
|
|
* std::thread* t = new std::thread([&] { s->serviceQueue(); });
|
|
|
|
*
|
|
|
|
* ... 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.
|
|
|
|
*/
|
2015-04-02 16:33:45 +02:00
|
|
|
class CScheduler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CScheduler();
|
|
|
|
~CScheduler();
|
|
|
|
|
2022-12-25 09:25:19 +01:00
|
|
|
std::thread m_service_thread;
|
|
|
|
|
2018-09-20 23:57:13 +02:00
|
|
|
typedef std::function<void()> Function;
|
2015-04-02 16:33:45 +02:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
/** Call func at/after time t */
|
2020-03-06 20:47:49 +01:00
|
|
|
void schedule(Function f, std::chrono::system_clock::time_point t);
|
2015-04-02 16:33:45 +02:00
|
|
|
|
2023-01-15 12:04:56 +01:00
|
|
|
/** 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);
|
|
|
|
}
|
2015-04-02 16:33:45 +02:00
|
|
|
|
2023-01-15 12:04:56 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
2015-04-02 16:33:45 +02:00
|
|
|
|
2022-01-09 18:03:26 +01:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
/**
|
2022-12-25 09:25:19 +01:00
|
|
|
* Services the queue 'forever'. Should be run in a thread.
|
2020-05-28 14:04:40 +02:00
|
|
|
*/
|
2015-04-02 16:33:45 +02:00
|
|
|
void serviceQueue();
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
/** Tell any threads running serviceQueue to stop as soon as the current task is done */
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
WITH_LOCK(newTaskMutex, stopRequested = true);
|
|
|
|
newTaskScheduled.notify_all();
|
2022-12-25 09:25:19 +01:00
|
|
|
if (m_service_thread.joinable()) m_service_thread.join();
|
2020-05-28 14:04:40 +02:00
|
|
|
}
|
|
|
|
/** Tell any threads running serviceQueue to stop when there is no work left to be done */
|
|
|
|
void StopWhenDrained()
|
|
|
|
{
|
|
|
|
WITH_LOCK(newTaskMutex, stopWhenEmpty = true);
|
|
|
|
newTaskScheduled.notify_all();
|
2022-12-25 09:25:19 +01:00
|
|
|
if (m_service_thread.joinable()) m_service_thread.join();
|
2020-05-28 14:04:40 +02:00
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
/**
|
|
|
|
* Returns number of tasks waiting to be serviced,
|
|
|
|
* and first and last task times
|
|
|
|
*/
|
|
|
|
size_t getQueueInfo(std::chrono::system_clock::time_point& first,
|
|
|
|
std::chrono::system_clock::time_point& last) const;
|
2015-05-15 18:40:36 +02:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
/** Returns true if there are threads actively running in serviceQueue() */
|
2017-07-11 09:30:36 +02:00
|
|
|
bool AreThreadsServicingQueue() const;
|
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
private:
|
2020-03-06 20:47:49 +01:00
|
|
|
mutable Mutex newTaskMutex;
|
|
|
|
std::condition_variable newTaskScheduled;
|
|
|
|
std::multimap<std::chrono::system_clock::time_point, Function> taskQueue GUARDED_BY(newTaskMutex);
|
2023-01-15 12:04:56 +01:00
|
|
|
int nThreadsServicingQueue GUARDED_BY(newTaskMutex){0};
|
|
|
|
bool stopRequested GUARDED_BY(newTaskMutex){false};
|
|
|
|
bool stopWhenEmpty GUARDED_BY(newTaskMutex){false};
|
2020-03-06 20:47:49 +01:00
|
|
|
bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex) { return stopRequested || (stopWhenEmpty && taskQueue.empty()); }
|
2015-04-02 16:33:45 +02:00
|
|
|
};
|
|
|
|
|
2017-07-11 09:30:36 +02:00
|
|
|
/**
|
|
|
|
* Class used by CScheduler clients which may schedule multiple jobs
|
2018-08-01 02:50:18 +02: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-07-11 09:30:36 +02:00
|
|
|
*/
|
2020-05-28 14:04:40 +02:00
|
|
|
class SingleThreadedSchedulerClient
|
|
|
|
{
|
2017-07-11 09:30:36 +02:00
|
|
|
private:
|
2022-05-04 10:54:19 +02:00
|
|
|
CScheduler& m_scheduler;
|
2017-07-11 09:30:36 +02:00
|
|
|
|
2022-01-17 08:53:42 +01:00
|
|
|
Mutex m_callbacks_mutex;
|
|
|
|
std::list<std::function<void()>> m_callbacks_pending GUARDED_BY(m_callbacks_mutex);
|
|
|
|
bool m_are_callbacks_running GUARDED_BY(m_callbacks_mutex) = false;
|
2017-07-11 09:30:36 +02:00
|
|
|
|
|
|
|
void MaybeScheduleProcessQueue();
|
|
|
|
void ProcessQueue();
|
|
|
|
|
|
|
|
public:
|
2022-05-04 10:54:19 +02:00
|
|
|
explicit SingleThreadedSchedulerClient(CScheduler& scheduler LIFETIMEBOUND) : m_scheduler{scheduler} {}
|
2018-08-01 02:50:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a callback to be executed. Callbacks are executed serially
|
2018-08-02 10:05:39 +02:00
|
|
|
* and memory is release-acquire consistent between callback executions.
|
2018-09-06 00:12:39 +02:00
|
|
|
* Practically, this means that callbacks can behave as if they are executed
|
2018-08-01 02:50:18 +02:00
|
|
|
* in order by a single thread.
|
|
|
|
*/
|
2020-05-28 14:04:40 +02:00
|
|
|
void AddToProcessQueue(std::function<void()> func);
|
2017-07-11 09:30:36 +02:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
/**
|
|
|
|
* 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!
|
|
|
|
*/
|
2017-07-11 09:30:36 +02:00
|
|
|
void EmptyQueue();
|
2020-03-24 15:21:59 +01:00
|
|
|
|
|
|
|
size_t CallbacksPending();
|
2017-07-11 09:30:36 +02:00
|
|
|
};
|
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
#endif
|