diff --git a/include/mbgl/storage/request.hpp b/include/mbgl/storage/request.hpp index 5d979a281..4b75f23f6 100644 --- a/include/mbgl/storage/request.hpp +++ b/include/mbgl/storage/request.hpp @@ -11,8 +11,8 @@ #include #include -typedef struct uv_async_s uv_async_t; typedef struct uv_loop_s uv_loop_t; +namespace uv { class async; } namespace mbgl { @@ -37,7 +37,7 @@ private: void notifyCallback(); private: - uv_async_t *async = nullptr; + std::unique_ptr async; struct Canceled; std::unique_ptr canceled; Callback callback; diff --git a/src/mbgl/storage/request.cpp b/src/mbgl/storage/request.cpp index 53a882f2a..a6d845ce4 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. }