[core] Add ref counting to the main loop

Should not be used at all, added for a corner case
on OSX network backend where the request is processed on
a worker thread managed by OSX and we need to wait for
that reply before destroying the thread that pushed the
request.
This commit is contained in:
Thiago Marcos P. Santos
2016-01-20 17:27:35 +02:00
parent 07f6dee616
commit 55c31493af
2 changed files with 28 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/thread_local.hpp>
@@ -72,6 +73,8 @@ public:
return reinterpret_cast<uv_handle_t*>(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<void(int, Event)>&& callback) {