From e6eb6c7fe421abe1fa6d2ca0934847cf326fa222 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 13 May 2015 11:50:16 -0700 Subject: Use uv::async --- src/mbgl/storage/request.cpp | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) (limited to 'src/mbgl/storage') diff --git a/src/mbgl/storage/request.cpp b/src/mbgl/storage/request.cpp index 53a882f2a6..a6d845ce4a 100644 --- a/src/mbgl/storage/request.cpp +++ b/src/mbgl/storage/request.cpp @@ -5,9 +5,7 @@ #include #include -#include - -#include +#include #include #include @@ -18,18 +16,9 @@ struct Request::Canceled { std::mutex mutex; bool confirmed = false; }; // Note: This requires that loop is running in the current thread (or not yet running). Request::Request(const Resource &resource_, uv_loop_t *loop, Callback callback_) - : callback(callback_), resource(resource_) { - // When there is no loop supplied (== nullptr), the callback will be fired in an arbitrary - // thread (the thread notify() is called from) rather than kicking back to the calling thread. - if (loop) { - async = new uv_async_t; - async->data = this; -#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 - uv_async_init(loop, async, [](uv_async_t *a, int) { reinterpret_cast(a->data)->notifyCallback(); }); -#else - uv_async_init(loop, async, [](uv_async_t *a) { reinterpret_cast(a->data)->notifyCallback(); }); -#endif - } + : async(util::make_unique(loop, [this] { notifyCallback(); })), + callback(callback_), + resource(resource_) { } // Called in the originating thread. @@ -59,27 +48,14 @@ void Request::invoke() { delete this; } -Request::~Request() { - if (async) { - uv_close(reinterpret_cast(async), [](uv_handle_t* handle) { - delete reinterpret_cast(handle); - }); - } -} +Request::~Request() = default; // Called in the FileSource thread. void Request::notify(const std::shared_ptr &response_) { assert(!response); response = response_; assert(response); - - if (async) { - uv_async_send(async); - } else { - // This request is not cancelable. This means that the callback will be executed in an - // arbitrary thread (== FileSource thread). - invoke(); - } + async->send(); } // Called in the originating thread. @@ -97,7 +73,7 @@ void Request::destruct() { assert(canceled); std::unique_lock lock(canceled->mutex); canceled->confirmed = true; - uv_async_send(async); + async->send(); // after this method returns, the FileSource thread has no knowledge of // this object anymore. } -- cgit v1.2.1