Files
MapLibreParticleLab/include/mbgl/actor/scheduler.hpp

64 lines
2.2 KiB
C++

#pragma once
#include <mapbox/weak.hpp>
#include <functional>
#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;
// Enqueues a function for execution.
virtual void schedule(std::function<void()>) = 0;
// Makes a weak pointer to this Scheduler.
virtual mapbox::base::WeakPtr<Scheduler> makeWeakPtr() = 0;
// Returns a closure wrapping the given one.
//
// When the returned closure is invoked for the first time, it schedules
// the given closure to this scheduler, the consequent calls of the
// returned closure are ignored.
//
// If this scheduler is already deleted by the time the returnded closure is
// first invoked, the call is ignored.
std::function<void()> bindOnce(std::function<void()>);
// 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