Use uv::async

This commit is contained in:
John Firebaugh
2015-05-13 11:50:16 -07:00
parent 386be7c0fa
commit e6eb6c7fe4
2 changed files with 9 additions and 33 deletions

View File

@@ -11,8 +11,8 @@
#include <functional>
#include <memory>
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<uv::async> async;
struct Canceled;
std::unique_ptr<Canceled> canceled;
Callback callback;

View File

@@ -5,9 +5,7 @@
#include <mbgl/util/util.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/uv.hpp>
#include <uv.h>
#include <mbgl/util/uv_detail.hpp>
#include <cassert>
#include <functional>
@@ -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<Request *>(a->data)->notifyCallback(); });
#else
uv_async_init(loop, async, [](uv_async_t *a) { reinterpret_cast<Request *>(a->data)->notifyCallback(); });
#endif
}
: async(util::make_unique<uv::async>(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<uv_handle_t*>(async), [](uv_handle_t* handle) {
delete reinterpret_cast<uv_async_t*>(handle);
});
}
}
Request::~Request() = default;
// Called in the FileSource thread.
void Request::notify(const std::shared_ptr<const Response> &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<std::mutex> 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.
}