| 1 | #pragma once |
| 2 | |
| 3 | #include <memory> |
| 4 | |
| 5 | namespace mbgl { |
| 6 | |
| 7 | class Mailbox; |
| 8 | |
| 9 | /* |
| 10 | A `Scheduler` is responsible for coordinating the processing of messages by |
| 11 | one or more actors via their mailboxes. It's an abstract interface. Currently, |
| 12 | the following concrete implementations exist: |
| 13 | |
| 14 | * `ThreadPool` can coordinate an unlimited number of actors over any number of |
| 15 | threads via a pool, preserving the following behaviors: |
| 16 | |
| 17 | - Messages from each individual mailbox are processed in order |
| 18 | - Only a single message from a mailbox is processed at a time; there is no |
| 19 | concurrency within a mailbox |
| 20 | |
| 21 | Subject to these constraints, processing can happen on whatever thread in the |
| 22 | pool is available. |
| 23 | |
| 24 | * `Scheduler::GetCurrent()` is typically used to create a mailbox and `ActorRef` |
| 25 | for an object that lives on the main thread and is not itself wrapped an |
| 26 | `Actor`. The underlying implementation of this Scheduler should usually be |
| 27 | a `RunLoop` |
| 28 | auto mailbox = std::make_shared<Mailbox>(*Scheduler::Get()); |
| 29 | Actor<Worker> worker(threadPool, ActorRef<Foo>(*this, mailbox)); |
| 30 | */ |
| 31 | class Scheduler { |
| 32 | public: |
| 33 | virtual ~Scheduler() = default; |
| 34 | |
| 35 | // Used by a Mailbox when it has a message in its queue to request that it |
| 36 | // be scheduled. Specifically, the scheduler is expected to asynchronously |
| 37 | // call `receive() on the given mailbox, provided it still exists at that |
| 38 | // time. |
| 39 | virtual void schedule(std::weak_ptr<Mailbox>) = 0; |
| 40 | |
| 41 | // Set/Get the current Scheduler for this thread |
| 42 | static Scheduler* GetCurrent(); |
| 43 | static void SetCurrent(Scheduler*); |
| 44 | }; |
| 45 | |
| 46 | } // namespace mbgl |
| 47 | |