From 4c8f716f20bd95c9821b20a6c30ab7af35bf1497 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Fri, 1 May 2015 18:13:35 -0400 Subject: [PATCH] Eliminate uv::async leak (fixes #1393) --- src/mbgl/util/run_loop.cpp | 16 +++++++--------- src/mbgl/util/run_loop.hpp | 8 +++----- src/mbgl/util/thread.hpp | 20 +++++++++----------- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/mbgl/util/run_loop.cpp b/src/mbgl/util/run_loop.cpp index fd9ad4306..7f277c988 100644 --- a/src/mbgl/util/run_loop.cpp +++ b/src/mbgl/util/run_loop.cpp @@ -5,8 +5,13 @@ namespace util { uv::tls RunLoop::current; -RunLoop::RunLoop() - : async(*loop, std::bind(&RunLoop::process, this)) { +RunLoop::RunLoop(uv_loop_t* loop) + : async(loop, std::bind(&RunLoop::process, this)) { + current.set(this); +} + +RunLoop::~RunLoop() { + current.set(nullptr); } void RunLoop::withMutex(std::function&& fn) { @@ -24,13 +29,6 @@ void RunLoop::process() { } } -void RunLoop::run() { - assert(!current.get()); - current.set(this); - loop.run(); - current.set(nullptr); -} - void RunLoop::stop() { invoke([&] { async.unref(); }); } diff --git a/src/mbgl/util/run_loop.hpp b/src/mbgl/util/run_loop.hpp index 971ad2cac..1fc742372 100644 --- a/src/mbgl/util/run_loop.hpp +++ b/src/mbgl/util/run_loop.hpp @@ -14,9 +14,9 @@ namespace util { class RunLoop : private util::noncopyable { public: - RunLoop(); + RunLoop(uv_loop_t*); + ~RunLoop(); - void run(); void stop(); // Invoke fn() in the runloop thread. @@ -63,7 +63,7 @@ public: }); } - uv_loop_t* get() { return *loop; } + uv_loop_t* get() { return async.get()->loop; } static uv::tls current; @@ -90,8 +90,6 @@ private: Queue queue; std::mutex mutex; - - uv::loop loop; uv::async async; }; diff --git a/src/mbgl/util/thread.hpp b/src/mbgl/util/thread.hpp index 415bda38d..23b89c0ab 100644 --- a/src/mbgl/util/thread.hpp +++ b/src/mbgl/util/thread.hpp @@ -77,11 +77,6 @@ public: return future.get(); } - // Join the thread, but call the given function repeatedly in the current thread - // while waiting for the join to finish. This should be immediately followed by - // destroying the Thread. - void pumpingStop(std::function); - private: Thread(const Thread&) = delete; Thread(Thread&&) = delete; @@ -124,21 +119,24 @@ Thread::Thread(const std::string& name, Args&&... args) { template template void Thread::run(P&& params, index_sequence) { - RunLoop loop_; - loop = &loop_; + uv::loop l; { - Object object_(loop_.get(), std::get(std::forward

(params))...); + Object object_(l.get(), std::get(std::forward

(params))...); object = &object_; - running.set_value(); - loop_.run(); + RunLoop loop_(l.get()); + loop = &loop_; + running.set_value(); + l.run(); + + loop = nullptr; object = nullptr; } // Run the loop again to ensure that async close callbacks have been called. - loop_.run(); + l.run(); joinable.get_future().get(); }