18 assert(nThreadsServicingQueue == 0);
19 if (stopWhenEmpty)
assert(taskQueue.empty());
26 ++nThreadsServicingQueue;
42 std::chrono::steady_clock::time_point timeToWaitFor = taskQueue.begin()->first;
43 if (
newTaskScheduled.wait_until(lock, timeToWaitFor) == std::cv_status::timeout) {
53 Function f = taskQueue.begin()->second;
54 taskQueue.erase(taskQueue.begin());
63 --nThreadsServicingQueue;
67 --nThreadsServicingQueue;
75 taskQueue.insert(std::make_pair(
t, f));
82 assert(delta_seconds > 0
s && delta_seconds <= 1h);
88 std::multimap<std::chrono::steady_clock::time_point, Function> temp_queue;
90 for (
const auto& element : taskQueue) {
91 temp_queue.emplace_hint(temp_queue.cend(), element.first - delta_seconds, element.second);
95 taskQueue = std::move(temp_queue);
105 s.scheduleFromNow([=, &
s] {
Repeat(
s, f, delta); }, delta);
114 std::chrono::steady_clock::time_point& last)
const
117 size_t result = taskQueue.size();
118 if (!taskQueue.empty()) {
119 first = taskQueue.begin()->first;
120 last = taskQueue.rbegin()->first;
128 return nThreadsServicingQueue;
139 if (m_are_callbacks_running)
return;
140 if (m_callbacks_pending.empty())
return;
147 std::function<void()> callback;
150 if (m_are_callbacks_running)
return;
151 if (m_callbacks_pending.empty())
return;
152 m_are_callbacks_running =
true;
154 callback = std::move(m_callbacks_pending.front());
155 m_callbacks_pending.pop_front();
160 struct RAIICallbacksRunning {
162 explicit RAIICallbacksRunning(
SerialTaskRunner* _instance) : instance(_instance) {}
163 ~RAIICallbacksRunning()
167 instance->m_are_callbacks_running =
false;
171 } raiicallbacksrunning(
this);
180 m_callbacks_pending.emplace_back(std::move(func));
188 bool should_continue =
true;
189 while (should_continue) {
192 should_continue = !m_callbacks_pending.empty();
199 return m_callbacks_pending.size();
Simple class for background tasks that should be run periodically or once "after a while".
void MockForward(std::chrono::seconds delta_seconds) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Mock the scheduler to fast forward in time.
void serviceQueue() EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Services the queue 'forever'.
void scheduleEvery(Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Repeat f until the scheduler is stopped.
size_t getQueueInfo(std::chrono::steady_clock::time_point &first, std::chrono::steady_clock::time_point &last) const EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Returns number of tasks waiting to be serviced, and first and last task times.
std::function< void()> Function
bool AreThreadsServicingQueue() const EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Returns true if there are threads actively running in serviceQueue()
bool shouldStop() const EXCLUSIVE_LOCKS_REQUIRED(newTaskMutex)
std::condition_variable newTaskScheduled
void scheduleFromNow(Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Call f once after the delta has passed.
void schedule(Function f, std::chrono::steady_clock::time_point t) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Call func at/after time t.
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
void MaybeScheduleProcessQueue() EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex)
void ProcessQueue() EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex)
size_t size() override EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex)
Returns the number of currently pending events.
void insert(std::function< void()> func) override EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex)
Add a callback to be executed.
void flush() override EXCLUSIVE_LOCKS_REQUIRED(!m_callbacks_mutex)
Processes all remaining queue members on the calling thread, blocking until queue is empty Must be ca...
static void Repeat(CScheduler &s, CScheduler::Function f, std::chrono::milliseconds delta)
#define WAIT_LOCK(cs, name)