2015-04-02 16:33:45 +02:00
|
|
|
// Copyright (c) 2015 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <scheduler.h>
|
2015-04-02 16:33:45 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <random.h>
|
2015-09-03 18:53:00 +02:00
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <utility>
|
|
|
|
|
2023-01-15 12:04:56 +01:00
|
|
|
CScheduler::CScheduler()
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CScheduler::~CScheduler()
|
|
|
|
{
|
|
|
|
assert(nThreadsServicingQueue == 0);
|
2023-01-15 12:04:56 +01:00
|
|
|
if (stopWhenEmpty) assert(taskQueue.empty());
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CScheduler::serviceQueue()
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
WAIT_LOCK(newTaskMutex, lock);
|
2015-04-02 16:33:45 +02:00
|
|
|
++nThreadsServicingQueue;
|
|
|
|
|
|
|
|
// newTaskMutex is locked throughout this loop EXCEPT
|
|
|
|
// when the thread is waiting or when the user's function
|
|
|
|
// is called.
|
2015-05-15 18:40:36 +02:00
|
|
|
while (!shouldStop()) {
|
2015-04-02 16:33:45 +02:00
|
|
|
try {
|
2015-05-15 18:40:36 +02:00
|
|
|
while (!shouldStop() && taskQueue.empty()) {
|
2015-04-02 16:33:45 +02:00
|
|
|
// Wait until there is something to do.
|
|
|
|
newTaskScheduled.wait(lock);
|
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
|
|
|
|
// Wait until either there is a new task, or until
|
|
|
|
// the time of the first item on the queue:
|
2015-04-02 16:33:45 +02:00
|
|
|
|
2016-11-19 16:18:19 +01:00
|
|
|
while (!shouldStop() && !taskQueue.empty()) {
|
2020-03-06 20:47:49 +01:00
|
|
|
std::chrono::system_clock::time_point timeToWaitFor = taskQueue.begin()->first;
|
|
|
|
if (newTaskScheduled.wait_until(lock, timeToWaitFor) == std::cv_status::timeout) {
|
2016-11-19 16:18:19 +01:00
|
|
|
break; // Exit loop after timeout, it means we reached the time of the event
|
2020-03-06 20:47:49 +01:00
|
|
|
}
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
2020-03-06 20:47:49 +01:00
|
|
|
|
2015-04-02 16:33:45 +02:00
|
|
|
// If there are multiple threads, the queue can empty while we're waiting (another
|
|
|
|
// thread may service the task we were waiting on).
|
2015-05-15 18:40:36 +02:00
|
|
|
if (shouldStop() || taskQueue.empty())
|
2015-04-02 16:33:45 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Function f = taskQueue.begin()->second;
|
|
|
|
taskQueue.erase(taskQueue.begin());
|
|
|
|
|
2015-08-17 23:30:46 +02:00
|
|
|
{
|
|
|
|
// Unlock before calling f, so it can reschedule itself or another task
|
|
|
|
// without deadlocking:
|
2020-03-06 20:47:49 +01:00
|
|
|
REVERSE_LOCK(lock);
|
2015-08-17 23:30:46 +02:00
|
|
|
f();
|
|
|
|
}
|
2015-04-02 16:33:45 +02:00
|
|
|
} catch (...) {
|
|
|
|
--nThreadsServicingQueue;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
--nThreadsServicingQueue;
|
2016-05-06 11:00:01 +02:00
|
|
|
newTaskScheduled.notify_one();
|
2015-05-15 18:40:36 +02:00
|
|
|
}
|
|
|
|
|
2020-03-06 20:47:49 +01:00
|
|
|
void CScheduler::schedule(CScheduler::Function f, std::chrono::system_clock::time_point t)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
LOCK(newTaskMutex);
|
2015-04-02 16:33:45 +02:00
|
|
|
taskQueue.insert(std::make_pair(t, f));
|
|
|
|
}
|
|
|
|
newTaskScheduled.notify_one();
|
|
|
|
}
|
|
|
|
|
2022-01-09 18:03:26 +01:00
|
|
|
void CScheduler::MockForward(std::chrono::seconds delta_seconds)
|
|
|
|
{
|
|
|
|
assert(delta_seconds.count() > 0 && delta_seconds < std::chrono::hours{1});
|
|
|
|
|
|
|
|
{
|
|
|
|
LOCK(newTaskMutex);
|
|
|
|
|
|
|
|
// use temp_queue to maintain updated schedule
|
|
|
|
std::multimap<std::chrono::system_clock::time_point, Function> temp_queue;
|
|
|
|
|
|
|
|
for (const auto& element : taskQueue) {
|
|
|
|
temp_queue.emplace_hint(temp_queue.cend(), element.first - delta_seconds, element.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// point taskQueue to temp_queue
|
|
|
|
taskQueue = std::move(temp_queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify that the taskQueue needs to be processed
|
|
|
|
newTaskScheduled.notify_one();
|
|
|
|
}
|
|
|
|
|
2023-01-15 12:04:56 +01:00
|
|
|
static void Repeat(CScheduler& s, CScheduler::Function f, std::chrono::milliseconds delta)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
|
|
|
f();
|
2023-01-15 12:04:56 +01:00
|
|
|
s.scheduleFromNow([=, &s] { Repeat(s, f, delta); }, delta);
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-15 12:04:56 +01:00
|
|
|
void CScheduler::scheduleEvery(CScheduler::Function f, std::chrono::milliseconds delta)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
2023-01-15 12:04:56 +01:00
|
|
|
scheduleFromNow([=] { Repeat(*this, f, delta); }, delta);
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
size_t CScheduler::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-03-06 20:47:49 +01:00
|
|
|
LOCK(newTaskMutex);
|
2015-05-15 18:40:36 +02:00
|
|
|
size_t result = taskQueue.size();
|
|
|
|
if (!taskQueue.empty()) {
|
|
|
|
first = taskQueue.begin()->first;
|
|
|
|
last = taskQueue.rbegin()->first;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-07-11 09:30:36 +02:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
bool CScheduler::AreThreadsServicingQueue() const
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
LOCK(newTaskMutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
return nThreadsServicingQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
void SingleThreadedSchedulerClient::MaybeScheduleProcessQueue()
|
|
|
|
{
|
2017-07-11 09:30:36 +02:00
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2020-03-06 20:47:49 +01:00
|
|
|
m_pscheduler->schedule(std::bind(&SingleThreadedSchedulerClient::ProcessQueue, this), std::chrono::system_clock::now());
|
2017-07-11 09:30:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
void SingleThreadedSchedulerClient::ProcessQueue()
|
|
|
|
{
|
|
|
|
std::function<void()> callback;
|
2017-07-11 09:30:36 +02:00
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
if (m_are_callbacks_running) return;
|
|
|
|
if (m_callbacks_pending.empty()) return;
|
|
|
|
m_are_callbacks_running = true;
|
|
|
|
|
|
|
|
callback = std::move(m_callbacks_pending.front());
|
|
|
|
m_callbacks_pending.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
// RAII the setting of fCallbacksRunning and calling MaybeScheduleProcessQueue
|
|
|
|
// to ensure both happen safely even if callback() throws.
|
|
|
|
struct RAIICallbacksRunning {
|
|
|
|
SingleThreadedSchedulerClient* instance;
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit RAIICallbacksRunning(SingleThreadedSchedulerClient* _instance) : instance(_instance) {}
|
2020-05-28 14:04:40 +02:00
|
|
|
~RAIICallbacksRunning()
|
|
|
|
{
|
2017-07-11 09:30:36 +02:00
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(instance->m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
instance->m_are_callbacks_running = false;
|
|
|
|
}
|
|
|
|
instance->MaybeScheduleProcessQueue();
|
|
|
|
}
|
|
|
|
} raiicallbacksrunning(this);
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
void SingleThreadedSchedulerClient::AddToProcessQueue(std::function<void()> func)
|
|
|
|
{
|
2017-07-11 09:30:36 +02:00
|
|
|
assert(m_pscheduler);
|
|
|
|
|
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
m_callbacks_pending.emplace_back(std::move(func));
|
|
|
|
}
|
|
|
|
MaybeScheduleProcessQueue();
|
|
|
|
}
|
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
void SingleThreadedSchedulerClient::EmptyQueue()
|
|
|
|
{
|
2017-07-11 09:30:36 +02:00
|
|
|
assert(!m_pscheduler->AreThreadsServicingQueue());
|
|
|
|
bool should_continue = true;
|
|
|
|
while (should_continue) {
|
|
|
|
ProcessQueue();
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
should_continue = !m_callbacks_pending.empty();
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 15:21:59 +01:00
|
|
|
|
2020-05-28 14:04:40 +02:00
|
|
|
size_t SingleThreadedSchedulerClient::CallbacksPending()
|
|
|
|
{
|
2022-01-17 08:53:42 +01:00
|
|
|
LOCK(m_callbacks_mutex);
|
2020-03-24 15:21:59 +01:00
|
|
|
return m_callbacks_pending.size();
|
|
|
|
}
|