diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2016-01-08 23:17:43 +0200 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2016-01-09 01:26:29 +0200 |
commit | d7fdcc73bfcab39f63e547ce2af8435a9859cb08 (patch) | |
tree | 223b8f6bb663fff22ea5f65e487c40d280630cb2 | |
parent | e7b0b31d58997ce0c849129d07a97cb0740beb7e (diff) | |
download | qtlocation-mapboxgl-d7fdcc73bfcab39f63e547ce2af8435a9859cb08.tar.gz |
[core] Get rid of ::unref() for Timer and AsyncTask
Not need, legacy from libuv. The RunLoop keep the main
loop running until is explicitly no longer needed.
-rw-r--r-- | platform/default/async_task.cpp | 9 | ||||
-rw-r--r-- | platform/default/online_file_source.cpp | 1 | ||||
-rw-r--r-- | platform/default/run_loop.cpp | 30 | ||||
-rw-r--r-- | platform/default/timer.cpp | 9 | ||||
-rw-r--r-- | src/mbgl/map/map_context.cpp | 3 | ||||
-rw-r--r-- | src/mbgl/util/async_task.hpp | 1 | ||||
-rw-r--r-- | src/mbgl/util/timer.hpp | 1 | ||||
-rw-r--r-- | test/fixtures/mock_file_source.cpp | 1 | ||||
-rw-r--r-- | test/miscellaneous/async_task.cpp | 3 | ||||
-rw-r--r-- | test/miscellaneous/timer.cpp | 10 | ||||
-rw-r--r-- | test/style/pending_resources.cpp | 1 |
11 files changed, 31 insertions, 38 deletions
diff --git a/platform/default/async_task.cpp b/platform/default/async_task.cpp index c8fb4dcd13..05cf759863 100644 --- a/platform/default/async_task.cpp +++ b/platform/default/async_task.cpp @@ -28,6 +28,7 @@ public: } handle()->data = this; + uv_unref(handle()); } ~Impl() { @@ -43,10 +44,6 @@ public: } } - void unref() { - uv_unref(handle()); - } - private: static void asyncCallback(UV_ASYNC_PARAMS(handle)) { reinterpret_cast<Impl*>(handle->data)->task(); @@ -71,9 +68,5 @@ void AsyncTask::send() { impl->maySend(); } -void AsyncTask::unref() { - impl->unref(); -} - } // namespace util } // namespace mbgl diff --git a/platform/default/online_file_source.cpp b/platform/default/online_file_source.cpp index 970929c1ed..72f334ab21 100644 --- a/platform/default/online_file_source.cpp +++ b/platform/default/online_file_source.cpp @@ -177,7 +177,6 @@ OnlineFileSource::Impl::Impl(FileCache* cache_, const std::string& root) // loop alive; otherwise our app wouldn't terminate. After all, we only need status change // notifications when our app is still running. NetworkStatus::Subscribe(&reachability); - reachability.unref(); } OnlineFileSource::Impl::~Impl() { diff --git a/platform/default/run_loop.cpp b/platform/default/run_loop.cpp index 158d54d2db..76c01b80b7 100644 --- a/platform/default/run_loop.cpp +++ b/platform/default/run_loop.cpp @@ -13,6 +13,12 @@ namespace { using namespace mbgl::util; static ThreadLocal<RunLoop>& current = *new ThreadLocal<RunLoop>; +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 +void dummyCallback(uv_async_t*, int) {}; +#else +void dummyCallback(uv_async_t*) {}; +#endif + } // namespace namespace mbgl { @@ -56,7 +62,19 @@ RunLoop* RunLoop::Get() { class RunLoop::Impl { public: + void closeHolder() { + uv_close(holderHandle(), [](uv_handle_t* h) { + delete reinterpret_cast<uv_async_t*>(h); + }); + } + + uv_handle_t* holderHandle() { + return reinterpret_cast<uv_handle_t*>(holder); + } + uv_loop_t *loop = nullptr; + uv_async_t* holder = new uv_async_t; + RunLoop::Type type; std::unique_ptr<AsyncTask> async; @@ -81,6 +99,12 @@ RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) { break; } + // Just for holding a ref to the main loop and keep + // it alive as required by libuv. + if (uv_async_init(impl->loop, impl->holder, dummyCallback) != 0) { + throw std::runtime_error("Failed to initialize async."); + } + impl->type = type; current.set(this); @@ -90,6 +114,10 @@ RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) { RunLoop::~RunLoop() { current.set(nullptr); + // Close the dummy handle that we have + // just to keep the main loop alive. + impl->closeHolder(); + if (impl->type == Type::Default) { return; } @@ -133,7 +161,7 @@ void RunLoop::runOnce() { } void RunLoop::stop() { - invoke([&] { impl->async->unref(); }); + invoke([&] { uv_unref(impl->holderHandle()); }); } void RunLoop::addWatch(int fd, Event event, std::function<void(int, Event)>&& callback) { diff --git a/platform/default/timer.cpp b/platform/default/timer.cpp index 774c1bb97e..4b7a455ccc 100644 --- a/platform/default/timer.cpp +++ b/platform/default/timer.cpp @@ -22,6 +22,7 @@ public: } handle()->data = this; + uv_unref(handle()); } ~Impl() { @@ -44,10 +45,6 @@ public: } } - void unref() { - uv_unref(handle()); - } - private: static void timerCallback(UV_TIMER_PARAMS(handle)) { reinterpret_cast<Impl*>(handle->data)->cb(); @@ -78,9 +75,5 @@ void Timer::stop() { impl->stop(); } -void Timer::unref() { - impl->unref(); -} - } // namespace util } // namespace mbgl diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index be6d32d2a0..6f12e968bd 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -39,9 +39,6 @@ MapContext::MapContext(View& view_, FileSource& fileSource, MapMode mode_, GLCon util::ThreadContext::setFileSource(&fileSource); util::ThreadContext::setGLObjectStore(&glObjectStore); - asyncUpdate.unref(); - asyncInvalidate.unref(); - view.activate(); } diff --git a/src/mbgl/util/async_task.hpp b/src/mbgl/util/async_task.hpp index 5159e6ce3c..ce273c664c 100644 --- a/src/mbgl/util/async_task.hpp +++ b/src/mbgl/util/async_task.hpp @@ -15,7 +15,6 @@ public: ~AsyncTask(); void send(); - void unref(); private: class Impl; diff --git a/src/mbgl/util/timer.hpp b/src/mbgl/util/timer.hpp index 44a516d168..783241847d 100644 --- a/src/mbgl/util/timer.hpp +++ b/src/mbgl/util/timer.hpp @@ -17,7 +17,6 @@ public: void start(Duration timeout, Duration repeat, std::function<void()>&&); void stop(); - void unref(); private: class Impl; diff --git a/test/fixtures/mock_file_source.cpp b/test/fixtures/mock_file_source.cpp index 22c906c173..791e5d314a 100644 --- a/test/fixtures/mock_file_source.cpp +++ b/test/fixtures/mock_file_source.cpp @@ -18,7 +18,6 @@ public: MockFileSource::MockFileSource(Type type_, const std::string& match_) : type(type_), match(match_) { - timer.unref(); timer.start(std::chrono::milliseconds(10), std::chrono::milliseconds(10), [this] { // Explicit move to avoid iterator invalidation if ~MockFileRequest gets called within the loop. auto pending_ = std::move(pending); diff --git a/test/miscellaneous/async_task.cpp b/test/miscellaneous/async_task.cpp index c32c2396fb..8073670fe1 100644 --- a/test/miscellaneous/async_task.cpp +++ b/test/miscellaneous/async_task.cpp @@ -40,7 +40,6 @@ TEST(AsyncTask, RequestCoalescing) { unsigned count = 0; AsyncTask async([&count] { ++count; }); - async.unref(); async.send(); async.send(); @@ -70,7 +69,6 @@ TEST(AsyncTask, RequestCoalescingMultithreaded) { unsigned count = 0; AsyncTask async([&count] { ++count; }); - async.unref(); std::vector<std::unique_ptr<Thread<TestWorker>>> threads; ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular}; @@ -97,7 +95,6 @@ TEST(AsyncTask, ThreadSafety) { unsigned count = 0; AsyncTask async([&count] { ++count; }); - async.unref(); unsigned numThreads = 25; diff --git a/test/miscellaneous/timer.cpp b/test/miscellaneous/timer.cpp index 7e5928406e..521d0e8cc4 100644 --- a/test/miscellaneous/timer.cpp +++ b/test/miscellaneous/timer.cpp @@ -12,7 +12,6 @@ TEST(Timer, Basic) { RunLoop loop; Timer timer; - timer.unref(); auto callback = [&loop] { loop.stop(); }; @@ -38,7 +37,6 @@ TEST(Timer, Repeat) { RunLoop loop; Timer timer; - timer.unref(); unsigned count = 10; auto callback = [&] { @@ -66,10 +64,7 @@ TEST(Timer, Stop) { RunLoop loop; Timer timer1; - timer1.unref(); - Timer timer2; - timer2.unref(); auto interval1 = std::chrono::milliseconds(50); auto interval2 = std::chrono::milliseconds(250); @@ -106,10 +101,7 @@ TEST(Timer, DestroyShouldStop) { RunLoop loop; auto timer1 = std::make_unique<Timer>(); - timer1->unref(); - Timer timer2; - timer2.unref(); auto interval1 = std::chrono::milliseconds(50); auto interval2 = std::chrono::milliseconds(250); @@ -146,7 +138,6 @@ TEST(Timer, StartOverrides) { RunLoop loop; Timer timer; - timer.unref(); auto interval1 = std::chrono::milliseconds(50); auto interval2 = std::chrono::milliseconds(250); @@ -182,6 +173,5 @@ TEST(Timer, CanStopNonStartedTimer) { RunLoop loop; Timer timer; - timer.unref(); timer.stop(); } diff --git a/test/style/pending_resources.cpp b/test/style/pending_resources.cpp index 62aee032da..1115274227 100644 --- a/test/style/pending_resources.cpp +++ b/test/style/pending_resources.cpp @@ -33,7 +33,6 @@ TEST_P(PendingResources, DeleteMapObjectWithPendingRequest) { loop.stop(); }); - endTest.unref(); fileSource.requestEnqueuedCallback = [&endTest] { endTest.send(); }; const std::string style = util::read_file("test/fixtures/resources/style.json"); |