[all] ensure runloop is the current scheduler

This commit is contained in:
Ivo van Dongen
2017-07-25 18:35:38 +03:00
committed by Ivo van Dongen
parent 2a8cc4b3e1
commit f4f4d13b70
7 changed files with 30 additions and 41 deletions

View File

@@ -1,38 +1,32 @@
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/thread_local.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <CoreFoundation/CoreFoundation.h>
namespace mbgl {
namespace util {
// Use a static function to avoid the static initialization order fiasco.
static auto& current() {
static ThreadLocal<RunLoop> tl;
return tl;
};
class RunLoop::Impl {
public:
std::unique_ptr<AsyncTask> async;
};
RunLoop* RunLoop::Get() {
assert(current().get());
return current().get();
assert(static_cast<RunLoop*>(Scheduler::GetCurrent()));
return static_cast<RunLoop*>(Scheduler::GetCurrent());
}
RunLoop::RunLoop(Type)
: impl(std::make_unique<Impl>()) {
assert(!current().get());
current().set(this);
assert(!Scheduler::GetCurrent());
Scheduler::SetCurrent(this);
impl->async = std::make_unique<AsyncTask>(std::bind(&RunLoop::process, this));
}
RunLoop::~RunLoop() {
assert(current().get());
current().set(nullptr);
assert(Scheduler::GetCurrent());
Scheduler::SetCurrent(nullptr);
}
void RunLoop::push(std::shared_ptr<WorkTask> task) {