From f4f4d13b70cfc26e44c7cbc679acbe9609ae2936 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Tue, 25 Jul 2017 18:35:38 +0300 Subject: [all] ensure runloop is the current scheduler --- platform/android/src/file_source.cpp | 4 ++-- platform/android/src/run_loop.cpp | 11 ++++++----- platform/darwin/src/MGLOfflineStorage.mm | 3 ++- platform/darwin/src/run_loop.cpp | 20 +++++++------------- platform/default/run_loop.cpp | 13 ++++++------- platform/qt/src/run_loop.cpp | 16 +++++----------- platform/qt/src/thread_local.cpp | 4 ++-- 7 files changed, 30 insertions(+), 41 deletions(-) (limited to 'platform') diff --git a/platform/android/src/file_source.cpp b/platform/android/src/file_source.cpp index 5d19c506bc..262e3d3c6a 100644 --- a/platform/android/src/file_source.cpp +++ b/platform/android/src/file_source.cpp @@ -1,9 +1,9 @@ #include "file_source.hpp" #include +#include #include #include -#include #include "asset_manager_file_source.hpp" #include "jni/generic_global_ref_deleter.hpp" @@ -45,7 +45,7 @@ void FileSource::setAPIBaseUrl(jni::JNIEnv& env, jni::String url) { void FileSource::setResourceTransform(jni::JNIEnv& env, jni::Object transformCallback) { if (transformCallback) { - resourceTransform = std::make_unique>(*util::RunLoop::Get(), + resourceTransform = std::make_unique>(*Scheduler::GetCurrent(), // Capture the ResourceTransformCallback object as a managed global into // the lambda. It is released automatically when we're setting a new ResourceTransform in // a subsequent call. diff --git a/platform/android/src/run_loop.cpp b/platform/android/src/run_loop.cpp index 3c605b70e8..dff7d1d984 100644 --- a/platform/android/src/run_loop.cpp +++ b/platform/android/src/run_loop.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -23,7 +24,6 @@ namespace { using namespace mbgl::util; -static ThreadLocal& current = *new ThreadLocal; int looperCallbackNew(int fd, int, void* data) { int buffer[1]; @@ -200,19 +200,20 @@ Milliseconds RunLoop::Impl::processRunnables() { } RunLoop* RunLoop::Get() { - return current.get(); + assert(static_cast(Scheduler::GetCurrent())); + return static_cast(Scheduler::GetCurrent()); } RunLoop::RunLoop(Type type) : impl(std::make_unique(this, type)) { - current.set(this); + Scheduler::SetCurrent(this); } RunLoop::~RunLoop() { - current.set(nullptr); + Scheduler::SetCurrent(nullptr); } LOOP_HANDLE RunLoop::getLoopHandle() { - return current.get()->impl.get(); + return Get()->impl.get(); } void RunLoop::push(std::shared_ptr task) { diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 2ddfa649e9..7085aa58e5 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -11,6 +11,7 @@ #import "NSValue+MGLAdditions.h" #include +#include #include #include #include @@ -81,7 +82,7 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = MGLOfflinePackUserInfoK - (void)setDelegate:(id)newValue { _delegate = newValue; if ([self.delegate respondsToSelector:@selector(offlineStorage:URLForResourceOfKind:withURL:)]) { - _mbglResourceTransform = std::make_unique>(*mbgl::util::RunLoop::Get(), [offlineStorage = self](auto kind_, const std::string&& url_) -> std::string { + _mbglResourceTransform = std::make_unique>(*mbgl::Scheduler::GetCurrent(), [offlineStorage = self](auto kind_, const std::string&& url_) -> std::string { NSURL* url = [NSURL URLWithString:[[NSString alloc] initWithBytes:url_.data() length:url_.length() diff --git a/platform/darwin/src/run_loop.cpp b/platform/darwin/src/run_loop.cpp index bae8164ab6..2ba8f8415b 100644 --- a/platform/darwin/src/run_loop.cpp +++ b/platform/darwin/src/run_loop.cpp @@ -1,38 +1,32 @@ #include #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(); + assert(static_cast(Scheduler::GetCurrent())); + return static_cast(Scheduler::GetCurrent()); } RunLoop::RunLoop(Type) : impl(std::make_unique()) { - assert(!current().get()); - current().set(this); + assert(!Scheduler::GetCurrent()); + Scheduler::SetCurrent(this); impl->async = std::make_unique(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 task) { diff --git a/platform/default/run_loop.cpp b/platform/default/run_loop.cpp index 98d1badcb5..6375dba78e 100644 --- a/platform/default/run_loop.cpp +++ b/platform/default/run_loop.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -10,9 +11,6 @@ namespace { -using namespace mbgl::util; -static ThreadLocal& current = *new ThreadLocal; - void dummyCallback(uv_async_t*) {} } // namespace @@ -53,7 +51,8 @@ struct Watch { }; RunLoop* RunLoop::Get() { - return current.get(); + assert(static_cast(Scheduler::GetCurrent())); + return static_cast(Scheduler::GetCurrent()); } class RunLoop::Impl { @@ -98,12 +97,12 @@ RunLoop::RunLoop(Type type) : impl(std::make_unique()) { impl->type = type; - current.set(this); + Scheduler::SetCurrent(this); impl->async = std::make_unique(std::bind(&RunLoop::process, this)); } RunLoop::~RunLoop() { - current.set(nullptr); + Scheduler::SetCurrent(nullptr); // Close the dummy handle that we have // just to keep the main loop alive. @@ -127,7 +126,7 @@ RunLoop::~RunLoop() { } LOOP_HANDLE RunLoop::getLoopHandle() { - return current.get()->impl->loop; + return Get()->impl->loop; } void RunLoop::push(std::shared_ptr task) { diff --git a/platform/qt/src/run_loop.cpp b/platform/qt/src/run_loop.cpp index c44f284852..71ea19032a 100644 --- a/platform/qt/src/run_loop.cpp +++ b/platform/qt/src/run_loop.cpp @@ -1,6 +1,6 @@ #include "run_loop_impl.hpp" -#include +#include #include @@ -8,13 +8,6 @@ #include #include -namespace { - -using namespace mbgl::util; -static ThreadLocal& current = *new ThreadLocal; - -} - namespace mbgl { namespace util { @@ -27,7 +20,8 @@ void RunLoop::Impl::onWriteEvent(int fd) { } RunLoop* RunLoop::Get() { - return current.get(); + assert(static_cast(Scheduler::GetCurrent())); + return static_cast(Scheduler::GetCurrent()); } RunLoop::RunLoop(Type type) : impl(std::make_unique()) { @@ -42,14 +36,14 @@ RunLoop::RunLoop(Type type) : impl(std::make_unique()) { impl->type = type; - current.set(this); + Scheduler::SetCurrent(this); impl->async = std::make_unique(std::bind(&RunLoop::process, this)); } RunLoop::~RunLoop() { MBGL_VERIFY_THREAD(tid); - current.set(nullptr); + Scheduler::SetCurrent(nullptr); } LOOP_HANDLE RunLoop::getLoopHandle() { diff --git a/platform/qt/src/thread_local.cpp b/platform/qt/src/thread_local.cpp index bf2103c98f..467bfb0d05 100644 --- a/platform/qt/src/thread_local.cpp +++ b/platform/qt/src/thread_local.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include @@ -41,7 +41,7 @@ void ThreadLocal::set(T* ptr) { impl->local.localData()[0] = ptr; } -template class ThreadLocal; +template class ThreadLocal; template class ThreadLocal; template class ThreadLocal; // For unit tests -- cgit v1.2.1