diff --git a/cmake/core-files.cmake b/cmake/core-files.cmake index 1bbe73925..1a86da6f2 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 83689c334..d8a26ebea 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(*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(*Scheduler::Get()); Actor worker(threadPool, ActorRef(*this, mailbox)); */ - class Scheduler { public: virtual ~Scheduler() = default; virtual void schedule(std::weak_ptr) = 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 7abbaa014..db70773c1 100644 --- a/platform/default/thread_local.cpp +++ b/platform/default/thread_local.cpp @@ -58,8 +58,8 @@ void ThreadLocal::set(T* ptr) { } } -template class ThreadLocal; template class ThreadLocal; +template class ThreadLocal; template class ThreadLocal; // For unit tests } // namespace util diff --git a/src/mbgl/actor/scheduler.cpp b/src/mbgl/actor/scheduler.cpp new file mode 100644 index 000000000..d7cdb2737 --- /dev/null +++ b/src/mbgl/actor/scheduler.cpp @@ -0,0 +1,19 @@ +#include +#include + +namespace mbgl { + +static auto& current() { + static util::ThreadLocal scheduler; + return scheduler; +}; + +void Scheduler::SetCurrent(Scheduler* scheduler) { + current().set(scheduler); +} + +Scheduler* Scheduler::GetCurrent() { + return current().get(); +} + +} //namespace mbgl