summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2014-11-26 17:03:40 -0800
committerJohn Firebaugh <john.firebaugh@gmail.com>2014-11-26 17:36:09 -0800
commit703a325518bfd3db7a25303102df6d7dd2cd3ce4 (patch)
treea23ec768a4321693be1743e8e55202440b5871b9 /include
parentbcc0db3742a71cb63e79f5f43317abdd34f189c2 (diff)
downloadqtlocation-mapboxgl-703a325518bfd3db7a25303102df6d7dd2cd3ce4.tar.gz
Introduce uv::async
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/util/uv.hpp1
-rw-r--r--include/mbgl/util/uv_detail.hpp38
2 files changed, 39 insertions, 0 deletions
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 <class T>
+void close(T* handle) {
+ uv_close(reinterpret_cast<uv_handle_t*>(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<void ()> 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<async*>(a->data)->fn();
+ }
+
+ uv_async_t a;
+ std::function<void ()> fn;
+};
+
class mutex : public mbgl::util::noncopyable {
public:
inline mutex() {