blob: 2d9ee49e9b2429322da35f0cff0e959cd0ae9502 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#include "run_loop_impl.hpp"
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/timer.hpp>
#include <functional>
namespace mbgl {
namespace util {
class Timer::Impl : public RunLoop::Impl::Runnable {
public:
Impl() {
loop->initRunnable(this);
}
~Impl() {
stop();
}
void start(Duration timeout, Duration repeat_, std::function<void ()>&& task_) {
stop();
repeat = repeat_;
task = std::move(task_);
// Prevent overflows when timeout is set to Duration::max()
due = (timeout == Duration::max()) ? std::chrono::time_point<Clock>::max() : Clock::now() + timeout;
loop->addRunnable(this);
}
void stop() {
loop->removeRunnable(this);
}
void reschedule() {
if (repeat != Duration::zero()) {
due = Clock::now() + repeat;
loop->wake();
} else {
stop();
}
}
TimePoint dueTime() const override {
return due;
}
void runTask() override {
reschedule();
task();
}
private:
TimePoint due;
Duration repeat;
RunLoop::Impl* loop = reinterpret_cast<RunLoop::Impl*>(RunLoop::getLoopHandle());
std::function<void()> task;
};
Timer::Timer()
: impl(std::make_unique<Impl>()) {
}
Timer::~Timer() = default;
void Timer::start(Duration timeout, Duration repeat, std::function<void()>&& cb) {
impl->start(timeout, repeat, std::move(cb));
}
void Timer::stop() {
impl->stop();
}
} // namespace util
} // namespace mbgl
|