From 703a325518bfd3db7a25303102df6d7dd2cd3ce4 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 26 Nov 2014 17:03:40 -0800 Subject: Introduce uv::async --- include/mbgl/util/uv.hpp | 1 + include/mbgl/util/uv_detail.hpp | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) (limited to 'include/mbgl') diff --git a/include/mbgl/util/uv.hpp b/include/mbgl/util/uv.hpp index a4252e5cb6..2fc3e0dc30 100644 --- a/include/mbgl/util/uv.hpp +++ b/include/mbgl/util/uv.hpp @@ -15,6 +15,7 @@ std::string cwd(); class thread; class rwlock; class loop; +class async; class worker; } diff --git a/include/mbgl/util/uv_detail.hpp b/include/mbgl/util/uv_detail.hpp index a68d493101..7d9e657f5c 100644 --- a/include/mbgl/util/uv_detail.hpp +++ b/include/mbgl/util/uv_detail.hpp @@ -13,6 +13,11 @@ namespace uv { +template +void close(T* handle) { + uv_close(reinterpret_cast(handle), nullptr); +} + class thread : public mbgl::util::noncopyable { public: inline operator uv_thread_t *() { return &t; } @@ -51,6 +56,39 @@ private: uv_loop_t *l = nullptr; }; +class async : public mbgl::util::noncopyable { +public: + inline async(uv_loop_t* loop, std::function fn_) + : fn(fn_) { + a.data = this; + if (uv_async_init(loop, &a, async_cb) != 0) { + throw std::runtime_error("failed to initialize async"); + } + } + + inline ~async() { + close(&a); + } + + inline void send() { + if (uv_async_send(&a) != 0) { + throw std::runtime_error("failed to async send"); + } + } + +private: +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + static void async_cb(uv_async_t* a, int) { +#else + static void async_cb(uv_async_t* a) { +#endif + reinterpret_cast(a->data)->fn(); + } + + uv_async_t a; + std::function fn; +}; + class mutex : public mbgl::util::noncopyable { public: inline mutex() { -- cgit v1.2.1