From 95214472d433e3efa6d9045193271b38231387a2 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 30 Apr 2015 12:02:41 -0400 Subject: [PATCH 1/4] Use uv::async --- platform/darwin/http_request_nsurl.mm | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/platform/darwin/http_request_nsurl.mm b/platform/darwin/http_request_nsurl.mm index e3f0b5dad..9572970f2 100644 --- a/platform/darwin/http_request_nsurl.mm +++ b/platform/darwin/http_request_nsurl.mm @@ -72,7 +72,7 @@ private: std::unique_ptr response; const std::shared_ptr existingResponse; ResponseStatus status = ResponseStatus::PermanentError; - uv_async_t *async = nullptr; + uv::async async; int attempts = 0; uv_timer_t *timer = nullptr; enum : bool { PreemptImmediately, ExponentialBackoff } strategy = PreemptImmediately; @@ -134,27 +134,17 @@ HTTPRequest::HTTPRequest(HTTPNSURLContext* context_, const Resource& resource_, : RequestBase(resource_, callback_), context(context_), existingResponse(existingResponse_), - async(new uv_async_t) { + async(loop, [this] { handleResponse(); }) { context->addRequest(this); - - async->data = this; - uv_async_init(loop, async, [](uv_async_t *as, int) { - auto impl = reinterpret_cast(as->data); - impl->handleResponse(); - }); - start(); } HTTPRequest::~HTTPRequest() { assert(!task); - assert(async); // Stop the backoff timer to avoid re-triggering this request. cancelTimer(); - uv::close(async); - context->removeRequest(this); } @@ -364,7 +354,7 @@ void HTTPRequest::handleResult(NSData *data, NSURLResponse *res, NSError *error) response->message = "response class is not NSHTTPURLResponse"; } - uv_async_send(async); + async.send(); } void HTTPRequest::retry(uint64_t timeout) { @@ -373,7 +363,7 @@ void HTTPRequest::retry(uint64_t timeout) { assert(!timer); timer = new uv_timer_t; timer->data = this; - uv_timer_init(async->loop, timer); + uv_timer_init(async.get()->loop, timer); uv_timer_start(timer, restart, timeout, 0); } From 64251eec75dd4cd9ec6aacc1c1e7ecc379b8ad9e Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 30 Apr 2015 12:28:29 -0400 Subject: [PATCH 2/4] Introduce and use uv::timer --- platform/darwin/http_request_nsurl.mm | 44 +++++----------------- src/mbgl/util/uv_detail.hpp | 54 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 34 deletions(-) diff --git a/platform/darwin/http_request_nsurl.mm b/platform/darwin/http_request_nsurl.mm index 9572970f2..509e158af 100644 --- a/platform/darwin/http_request_nsurl.mm +++ b/platform/darwin/http_request_nsurl.mm @@ -58,14 +58,11 @@ public: void retry() override; private: - void cancelTimer(); void start(); void handleResult(NSData *data, NSURLResponse *res, NSError *error); void handleResponse(); void retry(uint64_t timeout); - static void restart(uv_timer_t *timer, int); - HTTPNSURLContext *context = nullptr; bool cancelled = false; NSURLSessionDataTask *task = nullptr; @@ -73,8 +70,8 @@ private: const std::shared_ptr existingResponse; ResponseStatus status = ResponseStatus::PermanentError; uv::async async; + uv::timer timer; int attempts = 0; - uv_timer_t *timer = nullptr; enum : bool { PreemptImmediately, ExponentialBackoff } strategy = PreemptImmediately; static const int maxAttempts = 4; @@ -134,7 +131,8 @@ HTTPRequest::HTTPRequest(HTTPNSURLContext* context_, const Resource& resource_, : RequestBase(resource_, callback_), context(context_), existingResponse(existingResponse_), - async(loop, [this] { handleResponse(); }) { + async(loop, [this] { handleResponse(); }), + timer(loop) { context->addRequest(this); start(); } @@ -143,7 +141,7 @@ HTTPRequest::~HTTPRequest() { assert(!task); // Stop the backoff timer to avoid re-triggering this request. - cancelTimer(); + timer.stop(); context->removeRequest(this); } @@ -220,7 +218,7 @@ void HTTPRequest::cancel() { cancelled = true; // Stop the backoff timer to avoid re-triggering this request. - cancelTimer(); + timer.stop(); if (task) { [task cancel]; @@ -231,14 +229,6 @@ void HTTPRequest::cancel() { } } -void HTTPRequest::cancelTimer() { - if (timer) { - uv_timer_stop(timer); - uv::close(timer); - timer = nullptr; - } -} - int64_t parseCacheControl(const char *value) { if (value) { unsigned long long seconds = 0; @@ -360,34 +350,20 @@ void HTTPRequest::handleResult(NSData *data, NSURLResponse *res, NSError *error) void HTTPRequest::retry(uint64_t timeout) { response.reset(); - assert(!timer); - timer = new uv_timer_t; - timer->data = this; - uv_timer_init(async.get()->loop, timer); - uv_timer_start(timer, restart, timeout, 0); + timer.stop(); + timer.start(timeout, 0, [this] { start(); }); } void HTTPRequest::retry() { // All batons get notified when the network status changed, but some of them // might not actually wait for the network to become available again. - if (timer && strategy == PreemptImmediately) { + if (strategy == PreemptImmediately) { // Triggers the timer upon the next event loop iteration. - uv_timer_stop(timer); - uv_timer_start(timer, restart, 0, 0); + timer.stop(); + timer.start(0, 0, [this] { start(); }); } } -void HTTPRequest::restart(uv_timer_t *timer, int) { - // Restart the request. - auto impl = reinterpret_cast(timer->data); - - // Get rid of the timer. - impl->timer = nullptr; - uv::close(timer); - - impl->start(); -} - std::unique_ptr HTTPContext::createContext(uv_loop_t* loop) { return util::make_unique(loop); } diff --git a/src/mbgl/util/uv_detail.hpp b/src/mbgl/util/uv_detail.hpp index fbc0ec5aa..f77bec0fa 100644 --- a/src/mbgl/util/uv_detail.hpp +++ b/src/mbgl/util/uv_detail.hpp @@ -121,6 +121,60 @@ private: std::function fn; }; +class timer : public mbgl::util::noncopyable { +public: + inline timer(uv_loop_t* loop) + : t(new uv_timer_t) + { + t->data = this; + if (uv_timer_init(loop, t.get()) != 0) { + throw std::runtime_error("failed to initialize timer"); + } + } + + inline timer() { + close(std::move(t)); + } + + inline void start(uint64_t timeout, uint64_t repeat, std::function fn_) { + fn = fn_; + if (uv_timer_start(t.get(), timer_cb, timeout, repeat) != 0) { + throw std::runtime_error("failed to start timer"); + } + } + + inline void stop() { + fn = nullptr; + if (uv_timer_stop(t.get()) != 0) { + throw std::runtime_error("failed to stop timer"); + } + } + + inline void ref() { + uv_ref(reinterpret_cast(t.get())); + } + + inline void unref() { + uv_unref(reinterpret_cast(t.get())); + } + + inline uv_timer_t* get() { + return t.get(); + } + +private: +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + static void timer_cb(uv_timer_t* t, int) { +#else + static void timer_cb(uv_timer_t* t) { +#endif + reinterpret_cast(t->data)->fn(); + } + + std::unique_ptr t; + std::function fn; +}; + class mutex : public mbgl::util::noncopyable { public: inline mutex() { From 7584599ea6349fccfb5b1b1b5113d1bf33957d27 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 30 Apr 2015 12:36:32 -0400 Subject: [PATCH 3/4] uv::handle base class --- src/mbgl/util/uv_detail.hpp | 94 +++++++++++++++---------------------- 1 file changed, 38 insertions(+), 56 deletions(-) diff --git a/src/mbgl/util/uv_detail.hpp b/src/mbgl/util/uv_detail.hpp index f77bec0fa..059d7c0dd 100644 --- a/src/mbgl/util/uv_detail.hpp +++ b/src/mbgl/util/uv_detail.hpp @@ -28,13 +28,6 @@ UV_EXTERN void uv_key_set(uv_key_t* key, void* value); namespace uv { -template -void close(std::unique_ptr ptr) { - uv_close(reinterpret_cast(ptr.release()), [](uv_handle_t* handle) { - delete reinterpret_cast(handle); - }); -} - class loop : public mbgl::util::noncopyable { public: inline loop() { @@ -74,40 +67,50 @@ private: uv_loop_t *l = nullptr; }; -class async : public mbgl::util::noncopyable { +template +class handle : public mbgl::util::noncopyable { +public: + inline handle() : t(reinterpret_cast(new T)) { + t->data = this; + } + + inline ~handle() { + uv_close(t.release(), [](uv_handle_t* handle) { + delete reinterpret_cast(handle); + }); + } + + inline void ref() { + uv_ref(t.get()); + } + + inline void unref() { + uv_unref(t.get()); + } + + inline T* get() { + return reinterpret_cast(t.get()); + } + +private: + std::unique_ptr t; +}; + +class async : public handle { public: inline async(uv_loop_t* loop, std::function fn_) - : a(new uv_async_t) - , fn(fn_) - { - a->data = this; - if (uv_async_init(loop, a.get(), async_cb) != 0) { + : fn(fn_) { + if (uv_async_init(loop, get(), async_cb) != 0) { throw std::runtime_error("failed to initialize async"); } } - inline ~async() { - close(std::move(a)); - } - inline void send() { - if (uv_async_send(a.get()) != 0) { + if (uv_async_send(get()) != 0) { throw std::runtime_error("failed to async send"); } } - inline void ref() { - uv_ref(reinterpret_cast(a.get())); - } - - inline void unref() { - uv_unref(reinterpret_cast(a.get())); - } - - inline uv_async_t* get() { - return a.get(); - } - private: #if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 static void async_cb(uv_async_t* a, int) { @@ -117,51 +120,31 @@ private: reinterpret_cast(a->data)->fn(); } - std::unique_ptr a; std::function fn; }; -class timer : public mbgl::util::noncopyable { +class timer : public handle { public: - inline timer(uv_loop_t* loop) - : t(new uv_timer_t) - { - t->data = this; - if (uv_timer_init(loop, t.get()) != 0) { + inline timer(uv_loop_t* loop) { + if (uv_timer_init(loop, get()) != 0) { throw std::runtime_error("failed to initialize timer"); } } - inline timer() { - close(std::move(t)); - } - inline void start(uint64_t timeout, uint64_t repeat, std::function fn_) { fn = fn_; - if (uv_timer_start(t.get(), timer_cb, timeout, repeat) != 0) { + if (uv_timer_start(get(), timer_cb, timeout, repeat) != 0) { throw std::runtime_error("failed to start timer"); } } inline void stop() { fn = nullptr; - if (uv_timer_stop(t.get()) != 0) { + if (uv_timer_stop(get()) != 0) { throw std::runtime_error("failed to stop timer"); } } - inline void ref() { - uv_ref(reinterpret_cast(t.get())); - } - - inline void unref() { - uv_unref(reinterpret_cast(t.get())); - } - - inline uv_timer_t* get() { - return t.get(); - } - private: #if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 static void timer_cb(uv_timer_t* t, int) { @@ -171,7 +154,6 @@ private: reinterpret_cast(t->data)->fn(); } - std::unique_ptr t; std::function fn; }; From f3f3e1b443e95e6e0b7c12cd979e4850ea19c458 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Thu, 30 Apr 2015 12:55:36 -0400 Subject: [PATCH 4/4] Copy resource Fixes #1368 --- src/mbgl/storage/request_base.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mbgl/storage/request_base.hpp b/src/mbgl/storage/request_base.hpp index 77c1d9422..c8dfad477 100644 --- a/src/mbgl/storage/request_base.hpp +++ b/src/mbgl/storage/request_base.hpp @@ -3,13 +3,13 @@ #include #include +#include #include #include namespace mbgl { -struct Resource; class Response; class RequestBase : private util::noncopyable { @@ -26,7 +26,7 @@ public: virtual void retry() {}; protected: - const Resource& resource; + Resource resource; Callback notify; };