#include #include #include #include namespace mbgl { namespace util { // Use a static function to avoid the static initialization order fiasco. static auto& current() { static ThreadLocal tl; return tl; }; class RunLoop::Impl { public: std::unique_ptr async; }; RunLoop* RunLoop::Get() { assert(current().get()); return current().get(); } RunLoop::RunLoop(Type) : impl(std::make_unique()) { assert(!current().get()); current().set(this); impl->async = std::make_unique(std::bind(&RunLoop::process, this)); } RunLoop::~RunLoop() { assert(current().get()); current().set(nullptr); } void RunLoop::push(std::shared_ptr task) { withMutex([&] { queue.push(std::move(task)); }); impl->async->send(); } void RunLoop::run() { CFRunLoopRun(); } void RunLoop::runOnce() { CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false); } void RunLoop::stop() { invoke([&] { CFRunLoopStop(CFRunLoopGetCurrent()); }); } } // namespace util } // namespace mbgl