diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2019-10-04 12:30:41 +0300 |
---|---|---|
committer | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2019-10-04 17:42:21 +0300 |
commit | bef635765a1bbec14d7732856c38c037ea8add6a (patch) | |
tree | 47443222a5982031b4c9ecd531bea14edc61a8c8 /platform | |
parent | 198e3453394ccb2b1f7db72d1858cfd18e302a1e (diff) | |
download | qtlocation-mapboxgl-bef635765a1bbec14d7732856c38c037ea8add6a.tar.gz |
[core] Decouple Scheduler interface from actor model
So that it is possible to schedule normal `std::function` and use `mapbox::base::WeakPtr`.
Diffstat (limited to 'platform')
-rw-r--r-- | platform/android/src/map_renderer.cpp | 2 | ||||
-rw-r--r-- | platform/android/src/map_renderer.hpp | 2 | ||||
-rw-r--r-- | platform/android/src/map_renderer_runnable.cpp | 7 | ||||
-rw-r--r-- | platform/android/src/map_renderer_runnable.hpp | 4 | ||||
-rw-r--r-- | platform/qt/src/qmapboxgl_scheduler.cpp | 10 | ||||
-rw-r--r-- | platform/qt/src/qmapboxgl_scheduler.hpp | 5 |
6 files changed, 14 insertions, 16 deletions
diff --git a/platform/android/src/map_renderer.cpp b/platform/android/src/map_renderer.cpp index 6be708b994..0c0e907f14 100644 --- a/platform/android/src/map_renderer.cpp +++ b/platform/android/src/map_renderer.cpp @@ -43,7 +43,7 @@ ActorRef<Renderer> MapRenderer::actor() const { return *rendererRef; } -void MapRenderer::schedule(std::weak_ptr<Mailbox> scheduled) { +void MapRenderer::schedule(std::function<void()> scheduled) { // Create a runnable android::UniqueEnv _env = android::AttachEnv(); auto runnable = std::make_unique<MapRendererRunnable>(*_env, std::move(scheduled)); diff --git a/platform/android/src/map_renderer.hpp b/platform/android/src/map_renderer.hpp index 5a8ddeeb91..664da20a94 100644 --- a/platform/android/src/map_renderer.hpp +++ b/platform/android/src/map_renderer.hpp @@ -67,7 +67,7 @@ public: // From Scheduler. Schedules by using callbacks to the // JVM to process the mailbox on the right thread. - void schedule(std::weak_ptr<Mailbox> scheduled) override; + void schedule(std::function<void()> scheduled) override; void requestRender(); diff --git a/platform/android/src/map_renderer_runnable.cpp b/platform/android/src/map_renderer_runnable.cpp index 77c3aa301d..227f49ee3f 100644 --- a/platform/android/src/map_renderer_runnable.cpp +++ b/platform/android/src/map_renderer_runnable.cpp @@ -5,9 +5,8 @@ namespace mbgl { namespace android { -MapRendererRunnable::MapRendererRunnable(jni::JNIEnv& env, std::weak_ptr<Mailbox> mailbox_) - : mailbox(std::move(mailbox_)) { - +MapRendererRunnable::MapRendererRunnable(jni::JNIEnv& env, std::function<void()> function_) + : function(std::move(function_)) { // Create the Java peer and hold on to a global reference // Not using a weak reference here as this might oerflow // the weak reference table on some devices @@ -21,7 +20,7 @@ MapRendererRunnable::MapRendererRunnable(jni::JNIEnv& env, std::weak_ptr<Mailbox MapRendererRunnable::~MapRendererRunnable() = default; void MapRendererRunnable::run(jni::JNIEnv&) { - Mailbox::maybeReceive(mailbox); + if (function) function(); } jni::Global<jni::Object<MapRendererRunnable>> MapRendererRunnable::peer() { diff --git a/platform/android/src/map_renderer_runnable.hpp b/platform/android/src/map_renderer_runnable.hpp index 21c4369b69..24d0f2af49 100644 --- a/platform/android/src/map_renderer_runnable.hpp +++ b/platform/android/src/map_renderer_runnable.hpp @@ -24,7 +24,7 @@ public: static void registerNative(jni::JNIEnv&); - MapRendererRunnable(jni::JNIEnv&, std::weak_ptr<Mailbox>); + MapRendererRunnable(jni::JNIEnv&, std::function<void()>); // Only for jni registration, unused MapRendererRunnable(jni::JNIEnv&) { @@ -40,7 +40,7 @@ public: private: jni::Global<jni::Object<MapRendererRunnable>> javaPeer; - std::weak_ptr<Mailbox> mailbox; + std::function<void()> function; }; } // namespace android diff --git a/platform/qt/src/qmapboxgl_scheduler.cpp b/platform/qt/src/qmapboxgl_scheduler.cpp index e2d39703ee..5fc3ab13de 100644 --- a/platform/qt/src/qmapboxgl_scheduler.cpp +++ b/platform/qt/src/qmapboxgl_scheduler.cpp @@ -13,10 +13,9 @@ QMapboxGLScheduler::~QMapboxGLScheduler() MBGL_VERIFY_THREAD(tid); } -void QMapboxGLScheduler::schedule(std::weak_ptr<mbgl::Mailbox> mailbox) -{ +void QMapboxGLScheduler::schedule(std::function<void()> function) { std::lock_guard<std::mutex> lock(m_taskQueueMutex); - m_taskQueue.push(mailbox); + m_taskQueue.push(std::move(function)); // Need to force the main thread to wake // up this thread and process the events. @@ -25,14 +24,15 @@ void QMapboxGLScheduler::schedule(std::weak_ptr<mbgl::Mailbox> mailbox) void QMapboxGLScheduler::processEvents() { - std::queue<std::weak_ptr<mbgl::Mailbox>> taskQueue; + std::queue<std::function<void()>> taskQueue; { std::unique_lock<std::mutex> lock(m_taskQueueMutex); std::swap(taskQueue, m_taskQueue); } while (!taskQueue.empty()) { - mbgl::Mailbox::maybeReceive(taskQueue.front()); + auto& function = taskQueue.front(); + if (function) function(); taskQueue.pop(); } } diff --git a/platform/qt/src/qmapboxgl_scheduler.hpp b/platform/qt/src/qmapboxgl_scheduler.hpp index 68636d0d11..b34dd3d5b8 100644 --- a/platform/qt/src/qmapboxgl_scheduler.hpp +++ b/platform/qt/src/qmapboxgl_scheduler.hpp @@ -1,6 +1,5 @@ #pragma once -#include <mbgl/actor/mailbox.hpp> #include <mbgl/actor/scheduler.hpp> #include <mbgl/util/util.hpp> @@ -19,7 +18,7 @@ public: virtual ~QMapboxGLScheduler(); // mbgl::Scheduler implementation. - void schedule(std::weak_ptr<mbgl::Mailbox> scheduled) final; + void schedule(std::function<void()> scheduled) final; void processEvents(); @@ -30,5 +29,5 @@ private: MBGL_STORE_THREAD(tid); std::mutex m_taskQueueMutex; - std::queue<std::weak_ptr<mbgl::Mailbox>> m_taskQueue; + std::queue<std::function<void()>> m_taskQueue; }; |