Eliminate uv::async leak (fixes #1393)

This commit is contained in:
John Firebaugh
2015-05-01 18:13:35 -04:00
parent 36f695b411
commit 4c8f716f20
3 changed files with 19 additions and 25 deletions

View File

@@ -5,8 +5,13 @@ namespace util {
uv::tls<RunLoop> 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<void()>&& 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(); });
}

View File

@@ -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<RunLoop> current;
@@ -90,8 +90,6 @@ private:
Queue queue;
std::mutex mutex;
uv::loop loop;
uv::async async;
};

View File

@@ -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<void ()>);
private:
Thread(const Thread&) = delete;
Thread(Thread&&) = delete;
@@ -124,21 +119,24 @@ Thread<Object>::Thread(const std::string& name, Args&&... args) {
template <class Object>
template <typename P, std::size_t... I>
void Thread<Object>::run(P&& params, index_sequence<I...>) {
RunLoop loop_;
loop = &loop_;
uv::loop l;
{
Object object_(loop_.get(), std::get<I>(std::forward<P>(params))...);
Object object_(l.get(), std::get<I>(std::forward<P>(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();
}