summaryrefslogtreecommitdiff
path: root/platform/default/timer.cpp
blob: 969f6e78905f35595e222ed7b1ff82e0589737af (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
78
79
80
81
82
83
84
85
86
87
#include <mbgl/util/timer.hpp>

#include <mbgl/util/run_loop.hpp>

#include <uv.h>

#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
#define UV_TIMER_PARAMS(timer) uv_timer_t *timer, int
#else
#define UV_TIMER_PARAMS(timer) uv_timer_t *timer
#endif

namespace mbgl {
namespace util {

class Timer::Impl {
public:
    Impl() : timer(new uv_timer_t) {
        uv_loop_t* loop = reinterpret_cast<uv_loop_t*>(RunLoop::getLoopHandle());
        if (uv_timer_init(loop, timer) != 0) {
            throw std::runtime_error("Failed to initialize timer.");
        }

        handle()->data = this;
    }

    ~Impl() {
        uv_close(handle(), [](uv_handle_t* h) {
            delete reinterpret_cast<uv_timer_t*>(h);
        });
    }

    void start(uint64_t timeout, uint64_t repeat, std::function<void ()>&& cb_) {
        cb = std::move(cb_);
        if (uv_timer_start(timer, timerCallback, timeout, repeat) != 0) {
            throw std::runtime_error("Failed to start timer.");
        }
    }

    void stop() {
        cb = nullptr;
        if (uv_timer_stop(timer) != 0) {
            throw std::runtime_error("Failed to stop timer.");
        }
    }

    void unref() {
        uv_unref(handle());
    }

private:
    static void timerCallback(UV_TIMER_PARAMS(handle)) {
        reinterpret_cast<Impl*>(handle->data)->cb();
    }

    uv_handle_t* handle() {
        return reinterpret_cast<uv_handle_t*>(timer);
    }

    uv_timer_t* timer;

    std::function<void()> cb;
};

Timer::Timer()
    : impl(std::make_unique<Impl>()) {
}

Timer::~Timer() {
}

void Timer::start(Duration timeout, Duration repeat, std::function<void()>&& cb) {
    impl->start(std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count(),
                std::chrono::duration_cast<std::chrono::milliseconds>(repeat).count(),
                std::move(cb));
}

void Timer::stop() {
    impl->stop();
}

void Timer::unref() {
    impl->unref();
}

} // namespace util
} // namespace mbgl