My Project 3.2.0
C++ Distributed Hash Table
Loading...
Searching...
No Matches
scheduler.h
1/*
2 * Copyright (C) 2014-2023 Savoir-faire Linux Inc.
3 * Author(s) : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
4 * Simon Désaulniers <simon.desaulniers@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20
21#pragma once
22
23#include "utils.h"
24
25#include <functional>
26#include <map>
27
28namespace dht {
29
36class Scheduler {
37public:
38 struct Job {
39 Job(std::function<void()>&& f, time_point t) : do_(std::move(f)), t_(t) {}
40 std::function<void()> do_;
41 const time_point t_;
42 void cancel() { do_ = {}; }
43 };
44
53 Sp<Scheduler::Job> add(time_point t, std::function<void()>&& job_func) {
54 auto job = std::make_shared<Job>(std::move(job_func), t);
55 if (t != time_point::max())
56 timers.emplace(std::move(t), job);
57 return job;
58 }
59
66 void edit(Sp<Scheduler::Job>& job, time_point t) {
67 if (not job)
68 return;
69 // std::function move doesn't garantee to leave the object empty.
70 // Force clearing old value.
71 auto task = std::move(job->do_);
72 cancel(job);
73 job = add(t, std::move(task));
74 }
75
76 bool cancel(Sp<Scheduler::Job>& job) {
77 if (job) {
78 job->cancel();
79 for (auto r = timers.equal_range(job->t_); r.first != r.second; ++r.first) {
80 if (r.first->second == job) {
81 timers.erase(r.first);
82 job.reset();
83 return true;
84 }
85 }
86 }
87 return false;
88 }
89
95 time_point run() {
96 while (not timers.empty()) {
97 auto timer = timers.begin();
98 /*
99 * Running jobs scheduled before "now" prevents run+rescheduling
100 * loops before this method ends. It is garanteed by the fact that a
101 * job will at least be scheduled for "now" and not before.
102 */
103 if (timer->first > now)
104 break;
105
106 auto job = std::move(timer->second);
107 timers.erase(timer);
108
109 if (job->do_)
110 job->do_();
111 }
112 return getNextJobTime();
113 }
114
115 inline time_point getNextJobTime() const {
116 return timers.empty() ? time_point::max() : timers.begin()->first;
117 }
118
123 inline const time_point& time() const { return now; }
124 inline time_point syncTime() { return (now = clock::now()); }
125 inline void syncTime(const time_point& n) { now = n; }
126
127private:
128 time_point now {clock::now()};
129 std::multimap<time_point, Sp<Job>> timers {}; /* the jobs ordered by time */
130};
131
132}
Job scheduler.
Definition scheduler.h:36
Sp< Scheduler::Job > add(time_point t, std::function< void()> &&job_func)
Definition scheduler.h:53
const time_point & time() const
Definition scheduler.h:123
time_point run()
Definition scheduler.h:95
void edit(Sp< Scheduler::Job > &job, time_point t)
Definition scheduler.h:66