diff --git a/include/mbgl/util/run_loop.hpp b/include/mbgl/util/run_loop.hpp index fef00743f..e3d9248f8 100644 --- a/include/mbgl/util/run_loop.hpp +++ b/include/mbgl/util/run_loop.hpp @@ -41,6 +41,9 @@ public: void runOnce(); void stop(); + void ref(); + void unref(); + // So far only needed by the libcurl backend. void addWatch(int fd, Event, std::function&& callback); void removeWatch(int fd); diff --git a/platform/default/run_loop.cpp b/platform/default/run_loop.cpp index 76c01b80b..27f97d65e 100644 --- a/platform/default/run_loop.cpp +++ b/platform/default/run_loop.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -72,6 +73,8 @@ public: return reinterpret_cast(holder); } + int refCount = 1; + uv_loop_t *loop = nullptr; uv_async_t* holder = new uv_async_t; @@ -161,7 +164,28 @@ void RunLoop::runOnce() { } void RunLoop::stop() { - invoke([&] { uv_unref(impl->holderHandle()); }); + invoke([&] { + unref(); + + if (impl->refCount) { + Log::Debug(mbgl::Event::General, "Blocking on pending events."); + } + }); +} + +void RunLoop::ref() { + ++impl->refCount; +} + +void RunLoop::unref() { + // Main loop already stopped. + if (impl->refCount == 0) { + return; + } + + if (!--impl->refCount) { + uv_unref(impl->holderHandle()); + } } void RunLoop::addWatch(int fd, Event event, std::function&& callback) {