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>
|
|
|
|
|
2015-05-15 18:40:36 +02:00
|
|
|
CScheduler::CScheduler() : nThreadsServicingQueue(0), stopRequested(false), stopWhenEmpty(false)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CScheduler::~CScheduler()
|
|
|
|
{
|
|
|
|
assert(nThreadsServicingQueue == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
2017-05-23 17:53:00 +02:00
|
|
|
if (!shouldStop() && taskQueue.empty()) {
|
2020-03-06 20:47:49 +01:00
|
|
|
REVERSE_LOCK(lock);
|
2021-09-11 22:52:36 +02:00
|
|
|
// Use this chance to get more entropy
|
2017-05-23 17:53:00 +02:00
|
|
|
RandAddSeedSleep();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void CScheduler::stop(bool drain)
|
|
|
|
{
|
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
LOCK(newTaskMutex);
|
2015-05-15 18:40:36 +02:00
|
|
|
if (drain)
|
|
|
|
stopWhenEmpty = true;
|
|
|
|
else
|
|
|
|
stopRequested = true;
|
|
|
|
}
|
|
|
|
newTaskScheduled.notify_all();
|
2015-04-02 16:33:45 +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();
|
|
|
|
}
|
|
|
|
|
2017-03-07 11:00:46 +01:00
|
|
|
void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaMilliSeconds)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
2020-03-06 20:47:49 +01:00
|
|
|
schedule(f, std::chrono::system_clock::now() + std::chrono::milliseconds(deltaMilliSeconds));
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
|
|
|
|
2017-03-07 11:00:46 +01:00
|
|
|
static void Repeat(CScheduler* s, CScheduler::Function f, int64_t deltaMilliSeconds)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
|
|
|
f();
|
2021-08-17 22:31:09 +02:00
|
|
|
s->scheduleFromNow(std::bind(&Repeat, s, f, deltaMilliSeconds), deltaMilliSeconds);
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
|
|
|
|
2017-03-07 11:00:46 +01:00
|
|
|
void CScheduler::scheduleEvery(CScheduler::Function f, int64_t deltaMilliSeconds)
|
2015-04-02 16:33:45 +02:00
|
|
|
{
|
2021-08-17 22:31:09 +02:00
|
|
|
scheduleFromNow(std::bind(&Repeat, this, f, deltaMilliSeconds), deltaMilliSeconds);
|
2015-04-02 16:33:45 +02:00
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
|
2020-03-06 20:47:49 +01: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
|
|
|
|
|
|
|
bool CScheduler::AreThreadsServicingQueue() const {
|
2020-03-06 20:47:49 +01:00
|
|
|
LOCK(newTaskMutex);
|
2017-07-11 09:30:36 +02:00
|
|
|
return nThreadsServicingQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SingleThreadedSchedulerClient::MaybeScheduleProcessQueue() {
|
|
|
|
{
|
|
|
|
LOCK(m_cs_callbacks_pending);
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
void SingleThreadedSchedulerClient::ProcessQueue() {
|
2018-09-20 23:57:13 +02:00
|
|
|
std::function<void ()> callback;
|
2017-07-11 09:30:36 +02:00
|
|
|
{
|
|
|
|
LOCK(m_cs_callbacks_pending);
|
|
|
|
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) {}
|
2017-07-11 09:30:36 +02:00
|
|
|
~RAIICallbacksRunning() {
|
|
|
|
{
|
|
|
|
LOCK(instance->m_cs_callbacks_pending);
|
|
|
|
instance->m_are_callbacks_running = false;
|
|
|
|
}
|
|
|
|
instance->MaybeScheduleProcessQueue();
|
|
|
|
}
|
|
|
|
} raiicallbacksrunning(this);
|
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2018-09-20 23:57:13 +02:00
|
|
|
void SingleThreadedSchedulerClient::AddToProcessQueue(std::function<void ()> func) {
|
2017-07-11 09:30:36 +02:00
|
|
|
assert(m_pscheduler);
|
|
|
|
|
|
|
|
{
|
|
|
|
LOCK(m_cs_callbacks_pending);
|
|
|
|
m_callbacks_pending.emplace_back(std::move(func));
|
|
|
|
}
|
|
|
|
MaybeScheduleProcessQueue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SingleThreadedSchedulerClient::EmptyQueue() {
|
|
|
|
assert(!m_pscheduler->AreThreadsServicingQueue());
|
|
|
|
bool should_continue = true;
|
|
|
|
while (should_continue) {
|
|
|
|
ProcessQueue();
|
|
|
|
LOCK(m_cs_callbacks_pending);
|
|
|
|
should_continue = !m_callbacks_pending.empty();
|
|
|
|
}
|
|
|
|
}
|
2020-03-24 15:21:59 +01:00
|
|
|
|
|
|
|
size_t SingleThreadedSchedulerClient::CallbacksPending() {
|
|
|
|
LOCK(m_cs_callbacks_pending);
|
|
|
|
return m_callbacks_pending.size();
|
|
|
|
}
|