- Do not carry it over everywhere as parameter, it is a shared instance anyway and the lifecycle is pretty much the app lifecycle from the moment we instantiate a map. - Rename to BackgroundScheduler because it is a Scheduler that will do tasks in the background, we don't make assumptions if it is a thread pool or a single thread. - Most importantly, remove the dependency from `core` on `platform`.
52 lines
1.8 KiB
C++
52 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <memory>
|
|
|
|
namespace mbgl {
|
|
|
|
class Mailbox;
|
|
|
|
/*
|
|
A `Scheduler` is responsible for coordinating the processing of messages by
|
|
one or more actors via their mailboxes. It's an abstract interface. Currently,
|
|
the following concrete implementations exist:
|
|
|
|
* `ThreadPool` can coordinate an unlimited number of actors over any number of
|
|
threads via a pool, preserving the following behaviors:
|
|
|
|
- Messages from each individual mailbox are processed in order
|
|
- Only a single message from a mailbox is processed at a time; there is no
|
|
concurrency within a mailbox
|
|
|
|
Subject to these constraints, processing can happen on whatever thread in the
|
|
pool is available.
|
|
|
|
* `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;
|
|
|
|
// 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<Mailbox>) = 0;
|
|
|
|
// Set/Get the current Scheduler for this thread
|
|
static Scheduler* GetCurrent();
|
|
static void SetCurrent(Scheduler*);
|
|
|
|
// Get the scheduler for asynchronous tasks. This method
|
|
// will lazily initialize a shared worker pool when ran
|
|
// from the first time.
|
|
static std::shared_ptr<Scheduler> GetBackground();
|
|
};
|
|
|
|
} // namespace mbgl
|