From bef635765a1bbec14d7732856c38c037ea8add6a Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Fri, 4 Oct 2019 12:30:41 +0300 Subject: [core] Decouple Scheduler interface from actor model So that it is possible to schedule normal `std::function` and use `mapbox::base::WeakPtr`. --- include/mbgl/actor/mailbox.hpp | 2 ++ include/mbgl/actor/scheduler.hpp | 10 ++++------ include/mbgl/util/run_loop.hpp | 8 ++------ 3 files changed, 8 insertions(+), 12 deletions(-) (limited to 'include/mbgl') diff --git a/include/mbgl/actor/mailbox.hpp b/include/mbgl/actor/mailbox.hpp index 23c579917a..2b9838ef29 100644 --- a/include/mbgl/actor/mailbox.hpp +++ b/include/mbgl/actor/mailbox.hpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -34,6 +35,7 @@ public: void receive(); static void maybeReceive(std::weak_ptr); + static std::function makeClosure(std::weak_ptr); private: optional scheduler; diff --git a/include/mbgl/actor/scheduler.hpp b/include/mbgl/actor/scheduler.hpp index 6470ab1245..ca34901cfd 100644 --- a/include/mbgl/actor/scheduler.hpp +++ b/include/mbgl/actor/scheduler.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include namespace mbgl { @@ -31,12 +32,9 @@ class Mailbox; class Scheduler { public: virtual ~Scheduler() = default; - - // Used by a Mailbox when it has a message in its queue to request that it - // be scheduled. Specifically, the scheduler is expected to asynchronously - // call `receive() on the given mailbox, provided it still exists at that - // time. - virtual void schedule(std::weak_ptr) = 0; + + // Enqueues a function for execution. + virtual void schedule(std::function) = 0; // Set/Get the current Scheduler for this thread static Scheduler* GetCurrent(); diff --git a/include/mbgl/util/run_loop.hpp b/include/mbgl/util/run_loop.hpp index 381e3ae213..961573fd87 100644 --- a/include/mbgl/util/run_loop.hpp +++ b/include/mbgl/util/run_loop.hpp @@ -72,12 +72,8 @@ public: push(Priority::Default, task); return std::make_unique(task); } - - void schedule(std::weak_ptr mailbox) override { - invoke([mailbox] () { - Mailbox::maybeReceive(mailbox); - }); - } + + void schedule(std::function fn) override { invoke(std::move(fn)); } class Impl; -- cgit v1.2.1