C++ Distributed Hash Table
scheduler.h
1 /*
2  * Copyright (C) 2014-2017 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 #include "log_enable.h"
25 
26 #include <functional>
27 #include <map>
28 
29 namespace dht {
30 
37 class Scheduler {
38 public:
39  struct Job {
40  Job(std::function<void()>&& f) : do_(std::move(f)) {}
41  std::function<void()> do_;
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));
55  if (t != time_point::max())
56  timers.emplace(std::move(t), job);
57  return job;
58  }
59 
60  void add(const Sp<Scheduler::Job>& job, time_point t) {
61  if (t != time_point::max())
62  timers.emplace(std::move(t), job);
63  }
64 
71  void edit(Sp<Scheduler::Job>& job, time_point t) {
72  if (not job) {
73  return;
74  }
75  // std::function move doesn't garantee to leave the object empty.
76  // Force clearing old value.
77  auto task = std::move(job->do_);
78  job->do_ = {};
79  job = add(t, std::move(task));
80  }
81 
87  time_point run() {
88  syncTime();
89  while (not timers.empty()) {
90  auto timer = timers.begin();
91  /*
92  * Running jobs scheduled before "now" prevents run+rescheduling
93  * loops before this method ends. It is garanteed by the fact that a
94  * job will at least be scheduled for "now" and not before.
95  */
96  if (timer->first > now)
97  break;
98 
99  auto job = std::move(timer->second);
100  timers.erase(timer);
101 
102  if (job->do_)
103  job->do_();
104  }
105  return getNextJobTime();
106  }
107 
108  inline time_point getNextJobTime() const {
109  return timers.empty() ? time_point::max() : timers.begin()->first;
110  }
111 
116  inline const time_point& time() const { return now; }
117  inline time_point syncTime() { return (now = clock::now()); }
118 
119 private:
120  time_point now {clock::now()};
121  std::multimap<time_point, Sp<Job>> timers {}; /* the jobs ordered by time */
122 };
123 
124 }
Sp< Scheduler::Job > add(time_point t, std::function< void()> &&job_func)
Definition: scheduler.h:53
const time_point & time() const
Definition: scheduler.h:116
void edit(Sp< Scheduler::Job > &job, time_point t)
Definition: scheduler.h:71
Job scheduler.
Definition: scheduler.h:37
time_point run()
Definition: scheduler.h:87
Definition: callbacks.h:34