diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2019-10-28 17:12:58 +0200 |
---|---|---|
committer | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2019-11-05 11:45:38 +0200 |
commit | 31421f8448d0192d04b95e31a0aecde2675b5988 (patch) | |
tree | 6fa71a14f10728e49997d8581e00929bce59c414 /include/mbgl/actor | |
parent | d8b1b2bacf0dd9812d62077a825067441037791b (diff) | |
download | qtlocation-mapboxgl-31421f8448d0192d04b95e31a0aecde2675b5988.tar.gz |
[core] Add Scheduler::scheduleAndReplyValue() API
Diffstat (limited to 'include/mbgl/actor')
-rw-r--r-- | include/mbgl/actor/scheduler.hpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/include/mbgl/actor/scheduler.hpp b/include/mbgl/actor/scheduler.hpp index bb2cf124b8..1115328797 100644 --- a/include/mbgl/actor/scheduler.hpp +++ b/include/mbgl/actor/scheduler.hpp @@ -50,6 +50,18 @@ public: // first invoked, the call is ignored. std::function<void()> bindOnce(std::function<void()>); + // Enqueues the given |task| for execution into this scheduler's task queue and + // then enqueues the |reply| with the captured task result to the current + // task queue. + // + // The |TaskFn| return type must be compatible with the |ReplyFn| argument type. + // Note: the task result is copied and passed by value. + template <typename TaskFn, typename ReplyFn> + void scheduleAndReplyValue(const TaskFn& task, const ReplyFn& reply) { + assert(GetCurrent()); + scheduleAndReplyValue(task, reply, GetCurrent()->makeWeakPtr()); + } + // Set/Get the current Scheduler for this thread static Scheduler* GetCurrent(); static void SetCurrent(Scheduler*); @@ -58,6 +70,21 @@ public: // will lazily initialize a shared worker pool when ran // from the first time. static std::shared_ptr<Scheduler> GetBackground(); + +protected: + template <typename TaskFn, typename ReplyFn> + void scheduleAndReplyValue(const TaskFn& task, + const ReplyFn& reply, + mapbox::base::WeakPtr<Scheduler> replyScheduler) { + auto scheduled = [replyScheduler, task, reply] { + auto lock = replyScheduler.lock(); + if (!replyScheduler) return; + auto scheduledReply = [reply, result = task()] { reply(result); }; + replyScheduler->schedule(std::move(scheduledReply)); + }; + + schedule(std::move(scheduled)); + } }; } // namespace mbgl |