diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-11-25 16:35:08 +0200 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-12-01 11:49:02 +0200 |
commit | ed29223e5a626980100b7dc4f85737852f1a5319 (patch) | |
tree | f82e6764ad1d668dcdc41389eb1dd7a933c9d0dc /platform/default | |
parent | 7caaebb16236ae014f534b490b05f5b5d6a90213 (diff) | |
download | qtlocation-mapboxgl-ed29223e5a626980100b7dc4f85737852f1a5319.tar.gz |
[core] Remove mbgl/util/uv.hpp
Now only a few things depend on libuv and they can use it directly.
Diffstat (limited to 'platform/default')
-rw-r--r-- | platform/default/async_task.cpp | 45 | ||||
-rw-r--r-- | platform/default/run_loop.cpp | 4 | ||||
-rw-r--r-- | platform/default/timer.cpp | 65 |
3 files changed, 95 insertions, 19 deletions
diff --git a/platform/default/async_task.cpp b/platform/default/async_task.cpp index 6dccd9bcb0..064e8dad06 100644 --- a/platform/default/async_task.cpp +++ b/platform/default/async_task.cpp @@ -1,35 +1,62 @@ #include <mbgl/util/async_task.hpp> #include <mbgl/util/run_loop.hpp> -#include <mbgl/util/uv.hpp> #include <atomic> #include <functional> +#include <uv.h> + +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 +#define UV_ASYNC_PARAMS(handle) uv_async_t *handle, int +#else +#define UV_ASYNC_PARAMS(handle) uv_async_t *handle +#endif + namespace mbgl { namespace util { class AsyncTask::Impl { public: Impl(std::function<void()>&& fn) - : async(reinterpret_cast<uv_loop_t*>(RunLoop::getLoopHandle()), [this] { runTask(); }) - , task(std::move(fn)) { + : async(new uv_async_t), + task(std::move(fn)) { + + uv_loop_t* loop = reinterpret_cast<uv_loop_t*>(RunLoop::getLoopHandle()); + if (uv_async_init(loop, async, asyncCallback) != 0) { + throw std::runtime_error("Failed to initialize async."); + } + + handle()->data = this; } - void maySend() { - async.send(); + ~Impl() { + uv_close(handle(), [](uv_handle_t* h) { + delete reinterpret_cast<uv_async_t*>(h); + }); } - void runTask() { - task(); + void maySend() { + // uv_async_send will do the call coalescing for us. + if (uv_async_send(async) != 0) { + throw std::runtime_error("Failed to async send."); + } } void unref() { - async.unref(); + uv_unref(handle()); } private: - uv::async async; + static void asyncCallback(UV_ASYNC_PARAMS(handle)) { + reinterpret_cast<Impl*>(handle->data)->task(); + } + + uv_handle_t* handle() { + return reinterpret_cast<uv_handle_t*>(async); + } + + uv_async_t* async; std::function<void()> task; }; diff --git a/platform/default/run_loop.cpp b/platform/default/run_loop.cpp index 9ca7cc9207..a37fbbb67e 100644 --- a/platform/default/run_loop.cpp +++ b/platform/default/run_loop.cpp @@ -1,8 +1,10 @@ #include <mbgl/util/run_loop.hpp> #include <mbgl/util/async_task.hpp> -#include <mbgl/util/uv.hpp> #include <mbgl/util/thread_local.hpp> +#include <uv.h> + +#include <cassert> #include <functional> #include <unordered_map> diff --git a/platform/default/timer.cpp b/platform/default/timer.cpp index 78275814a6..4df1a28bf9 100644 --- a/platform/default/timer.cpp +++ b/platform/default/timer.cpp @@ -1,18 +1,65 @@ #include <mbgl/util/timer.hpp> #include <mbgl/util/run_loop.hpp> -#include <mbgl/util/uv.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(reinterpret_cast<uv_loop_t*>(RunLoop::getLoopHandle())) { + 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 timer; + uv_timer_t* timer; + + std::function<void()> cb; }; Timer::Timer() @@ -23,17 +70,17 @@ Timer::~Timer() { } void Timer::start(Duration timeout, Duration repeat, std::function<void()>&& cb) { - impl->timer.start(std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count(), - std::chrono::duration_cast<std::chrono::milliseconds>(repeat).count(), - std::move(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->timer.stop(); + impl->stop(); } void Timer::unref() { - impl->timer.unref(); + impl->unref(); } } |