Files
MapLibreParticleLab/platform/default/timer.cpp
Thiago Marcos P. Santos 8a53c1a881 [core] libuv unref() not really needed
The run loop will be kept alive because it has an `AsyncTask`. We
also can simple stop the loop with `uv_stop()`.

The `RunLoop` is still gonna be the last object to be destroyed
because it is the first object to be created in the thread, so
by design it won't outlive `Timer`s and `AsyncTask`s.

This patch won't change the current behavior, will just make
the code simpler.
2016-02-20 13:33:13 +02:00

79 lines
1.8 KiB
C++

#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.");
}
}
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() = default;
void Timer::start(Duration timeout, Duration repeat, std::function<void()>&& cb) {
impl->start(std::chrono::duration_cast<Milliseconds>(timeout).count(),
std::chrono::duration_cast<Milliseconds>(repeat).count(),
std::move(cb));
}
void Timer::stop() {
impl->stop();
}
} // namespace util
} // namespace mbgl