summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-07-25 15:46:16 +0300
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-08-09 12:08:21 +0300
commitedaa002c3c23e84db8ca07c521118da37ba5bb38 (patch)
treecfde2f22898febaa06618cd3fb7aac0a8a675bc1
parentd88567ee0997b0430db3ac7cf6deb20a0615098e (diff)
downloadqtlocation-mapboxgl-edaa002c3c23e84db8ca07c521118da37ba5bb38.tar.gz
[core] current scheduler
- Adds a way to set the current scheduler on the thread to be used whenever a mailbox is created that needs to reply on this thread
-rw-r--r--cmake/core-files.cmake1
-rw-r--r--include/mbgl/actor/scheduler.hpp15
-rw-r--r--platform/default/thread_local.cpp2
-rw-r--r--src/mbgl/actor/scheduler.cpp19
4 files changed, 30 insertions, 7 deletions
diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake
index 1bbe739256..1a86da6f24 100644
--- a/cmake/core-files.cmake
+++ b/cmake/core-files.cmake
@@ -8,6 +8,7 @@ set(MBGL_CORE_FILES
include/mbgl/actor/message.hpp
include/mbgl/actor/scheduler.hpp
src/mbgl/actor/mailbox.cpp
+ src/mbgl/actor/scheduler.cpp
# algorithm
src/mbgl/algorithm/covered_by_children.hpp
diff --git a/include/mbgl/actor/scheduler.hpp b/include/mbgl/actor/scheduler.hpp
index 83689c3348..d8a26ebeab 100644
--- a/include/mbgl/actor/scheduler.hpp
+++ b/include/mbgl/actor/scheduler.hpp
@@ -21,18 +21,21 @@ class Mailbox;
Subject to these constraints, processing can happen on whatever thread in the
pool is available.
- * `RunLoop` is a `Scheduler` that is typically used to create a mailbox and
- `ActorRef` for an object that lives on the main thread and is not itself wrapped
- as an `Actor`:
-
- auto mailbox = std::make_shared<Mailbox>(*util::RunLoop::Get());
+ * `Scheduler::GetCurrent()` is typically used to create a mailbox and `ActorRef`
+ for an object that lives on the main thread and is not itself wrapped an
+ `Actor`. The underlying implementation of this Scheduler should usually be
+ a `RunLoop`
+ auto mailbox = std::make_shared<Mailbox>(*Scheduler::Get());
Actor<Worker> worker(threadPool, ActorRef<Foo>(*this, mailbox));
*/
-
class Scheduler {
public:
virtual ~Scheduler() = default;
virtual void schedule(std::weak_ptr<Mailbox>) = 0;
+
+ // Set/Get the current Scheduler for this thread
+ static Scheduler* GetCurrent();
+ static void SetCurrent(Scheduler*);
};
} // namespace mbgl
diff --git a/platform/default/thread_local.cpp b/platform/default/thread_local.cpp
index 7abbaa0146..db70773c12 100644
--- a/platform/default/thread_local.cpp
+++ b/platform/default/thread_local.cpp
@@ -58,8 +58,8 @@ void ThreadLocal<T>::set(T* ptr) {
}
}
-template class ThreadLocal<RunLoop>;
template class ThreadLocal<BackendScope>;
+template class ThreadLocal<Scheduler>;
template class ThreadLocal<int>; // For unit tests
} // namespace util
diff --git a/src/mbgl/actor/scheduler.cpp b/src/mbgl/actor/scheduler.cpp
new file mode 100644
index 0000000000..d7cdb2737b
--- /dev/null
+++ b/src/mbgl/actor/scheduler.cpp
@@ -0,0 +1,19 @@
+#include <mbgl/actor/scheduler.hpp>
+#include <mbgl/util/thread_local.hpp>
+
+namespace mbgl {
+
+static auto& current() {
+ static util::ThreadLocal<Scheduler> scheduler;
+ return scheduler;
+};
+
+void Scheduler::SetCurrent(Scheduler* scheduler) {
+ current().set(scheduler);
+}
+
+Scheduler* Scheduler::GetCurrent() {
+ return current().get();
+}
+
+} //namespace mbgl