Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Current Scheduler #9728

Merged
merged 5 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(MBGL_CORE_FILES
include/mbgl/actor/message.hpp
include/mbgl/actor/scheduler.hpp
src/mbgl/actor/mailbox.cpp
src/mbgl/actor/scheduler.cpp

# algorithm
src/mbgl/algorithm/covered_by_children.hpp
Expand Down
15 changes: 9 additions & 6 deletions include/mbgl/actor/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ class Mailbox;
Subject to these constraints, processing can happen on whatever thread in the
pool is available.

* `RunLoop` is a `Scheduler` that is typically used to create a mailbox and
`ActorRef` for an object that lives on the main thread and is not itself wrapped
as an `Actor`:

auto mailbox = std::make_shared<Mailbox>(*util::RunLoop::Get());
* `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;
virtual void schedule(std::weak_ptr<Mailbox>) = 0;

// Set/Get the current Scheduler for this thread
static Scheduler* GetCurrent();
static void SetCurrent(Scheduler*);
};

} // namespace mbgl
12 changes: 6 additions & 6 deletions include/mbgl/util/run_loop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class RunLoop : public Scheduler,
push(task);
return std::make_unique<WorkRequest>(task);
}

void schedule(std::weak_ptr<Mailbox> mailbox) override {
invoke([mailbox] () {
Mailbox::maybeReceive(mailbox);
});
}

class Impl;

Expand All @@ -72,12 +78,6 @@ class RunLoop : public Scheduler,

void push(std::shared_ptr<WorkTask>);

void schedule(std::weak_ptr<Mailbox> mailbox) override {
invoke([mailbox] () {
Mailbox::maybeReceive(mailbox);
});
}

void withMutex(std::function<void()>&& fn) {
std::lock_guard<std::mutex> lock(mutex);
fn();
Expand Down
4 changes: 2 additions & 2 deletions platform/android/src/file_source.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "file_source.hpp"

#include <mbgl/actor/actor.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/storage/resource_transform.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/util/run_loop.hpp>

#include "asset_manager_file_source.hpp"
#include "jni/generic_global_ref_deleter.hpp"
Expand Down Expand Up @@ -45,7 +45,7 @@ void FileSource::setAPIBaseUrl(jni::JNIEnv& env, jni::String url) {

void FileSource::setResourceTransform(jni::JNIEnv& env, jni::Object<FileSource::ResourceTransformCallback> transformCallback) {
if (transformCallback) {
resourceTransform = std::make_unique<Actor<ResourceTransform>>(*util::RunLoop::Get(),
resourceTransform = std::make_unique<Actor<ResourceTransform>>(*Scheduler::GetCurrent(),
// Capture the ResourceTransformCallback object as a managed global into
// the lambda. It is released automatically when we're setting a new ResourceTransform in
// a subsequent call.
Expand Down
11 changes: 6 additions & 5 deletions platform/android/src/run_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <mbgl/util/thread_local.hpp>
#include <mbgl/util/thread.hpp>
#include <mbgl/util/timer.hpp>
#include <mbgl/actor/scheduler.hpp>

#include <android/looper.h>

Expand All @@ -23,7 +24,6 @@
namespace {

using namespace mbgl::util;
static ThreadLocal<RunLoop>& current = *new ThreadLocal<RunLoop>;

int looperCallbackNew(int fd, int, void* data) {
int buffer[1];
Expand Down Expand Up @@ -200,19 +200,20 @@ Milliseconds RunLoop::Impl::processRunnables() {
}

RunLoop* RunLoop::Get() {
return current.get();
assert(static_cast<RunLoop*>(Scheduler::GetCurrent()));
return static_cast<RunLoop*>(Scheduler::GetCurrent());
}

RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>(this, type)) {
current.set(this);
Scheduler::SetCurrent(this);
}

RunLoop::~RunLoop() {
current.set(nullptr);
Scheduler::SetCurrent(nullptr);
}

LOOP_HANDLE RunLoop::getLoopHandle() {
return current.get()->impl.get();
return Get()->impl.get();
}

void RunLoop::push(std::shared_ptr<WorkTask> task) {
Expand Down
3 changes: 2 additions & 1 deletion platform/darwin/src/MGLOfflineStorage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "NSValue+MGLAdditions.h"

#include <mbgl/actor/actor.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/storage/resource_transform.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/string.hpp>
Expand Down Expand Up @@ -81,7 +82,7 @@ - (void)unpauseFileSource:(__unused NSNotification *)notification {
- (void)setDelegate:(id<MGLOfflineStorageDelegate>)newValue {
_delegate = newValue;
if ([self.delegate respondsToSelector:@selector(offlineStorage:URLForResourceOfKind:withURL:)]) {
_mbglResourceTransform = std::make_unique<mbgl::Actor<mbgl::ResourceTransform>>(*mbgl::util::RunLoop::Get(), [offlineStorage = self](auto kind_, const std::string&& url_) -> std::string {
_mbglResourceTransform = std::make_unique<mbgl::Actor<mbgl::ResourceTransform>>(*mbgl::Scheduler::GetCurrent(), [offlineStorage = self](auto kind_, const std::string&& url_) -> std::string {
NSURL* url =
[NSURL URLWithString:[[NSString alloc] initWithBytes:url_.data()
length:url_.length()
Expand Down
20 changes: 7 additions & 13 deletions platform/darwin/src/run_loop.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/thread_local.hpp>
#include <mbgl/actor/scheduler.hpp>

#include <CoreFoundation/CoreFoundation.h>

namespace mbgl {
namespace util {

// Use a static function to avoid the static initialization order fiasco.
static auto& current() {
static ThreadLocal<RunLoop> tl;
return tl;
};

class RunLoop::Impl {
public:
std::unique_ptr<AsyncTask> async;
};

RunLoop* RunLoop::Get() {
assert(current().get());
return current().get();
assert(static_cast<RunLoop*>(Scheduler::GetCurrent()));
return static_cast<RunLoop*>(Scheduler::GetCurrent());
}

RunLoop::RunLoop(Type)
: impl(std::make_unique<Impl>()) {
assert(!current().get());
current().set(this);
assert(!Scheduler::GetCurrent());
Scheduler::SetCurrent(this);
impl->async = std::make_unique<AsyncTask>(std::bind(&RunLoop::process, this));
}

RunLoop::~RunLoop() {
assert(current().get());
current().set(nullptr);
assert(Scheduler::GetCurrent());
Scheduler::SetCurrent(nullptr);
}

void RunLoop::push(std::shared_ptr<WorkTask> task) {
Expand Down
2 changes: 1 addition & 1 deletion platform/default/online_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ ActorRef<OnlineFileRequest> OnlineFileRequest::actor() {
if (!mailbox) {
// Lazy constructed because this can be costly and
// the ResourceTransform is not used by many apps.
mailbox = std::make_shared<Mailbox>(*util::RunLoop::Get());
mailbox = std::make_shared<Mailbox>(*Scheduler::GetCurrent());
}

return ActorRef<OnlineFileRequest>(*this, mailbox);
Expand Down
13 changes: 6 additions & 7 deletions platform/default/run_loop.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/thread_local.hpp>
#include <mbgl/actor/scheduler.hpp>

#include <uv.h>

Expand All @@ -10,9 +11,6 @@

namespace {

using namespace mbgl::util;
static ThreadLocal<RunLoop>& current = *new ThreadLocal<RunLoop>;

void dummyCallback(uv_async_t*) {}

} // namespace
Expand Down Expand Up @@ -53,7 +51,8 @@ struct Watch {
};

RunLoop* RunLoop::Get() {
return current.get();
assert(static_cast<RunLoop*>(Scheduler::GetCurrent()));
return static_cast<RunLoop*>(Scheduler::GetCurrent());
}

class RunLoop::Impl {
Expand Down Expand Up @@ -98,12 +97,12 @@ RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) {

impl->type = type;

current.set(this);
Scheduler::SetCurrent(this);
impl->async = std::make_unique<AsyncTask>(std::bind(&RunLoop::process, this));
}

RunLoop::~RunLoop() {
current.set(nullptr);
Scheduler::SetCurrent(nullptr);

// Close the dummy handle that we have
// just to keep the main loop alive.
Expand All @@ -127,7 +126,7 @@ RunLoop::~RunLoop() {
}

LOOP_HANDLE RunLoop::getLoopHandle() {
return current.get()->impl->loop;
return Get()->impl->loop;
}

void RunLoop::push(std::shared_ptr<WorkTask> task) {
Expand Down
2 changes: 1 addition & 1 deletion platform/default/thread_local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ void ThreadLocal<T>::set(T* ptr) {
}
}

template class ThreadLocal<RunLoop>;
template class ThreadLocal<BackendScope>;
template class ThreadLocal<Scheduler>;
template class ThreadLocal<int>; // For unit tests

} // namespace util
Expand Down
16 changes: 5 additions & 11 deletions platform/qt/src/run_loop.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
#include "run_loop_impl.hpp"

#include <mbgl/util/thread_local.hpp>
#include <mbgl/actor/scheduler.hpp>

#include <QCoreApplication>

#include <cassert>
#include <functional>
#include <utility>

namespace {

using namespace mbgl::util;
static ThreadLocal<RunLoop>& current = *new ThreadLocal<RunLoop>;

}

namespace mbgl {
namespace util {

Expand All @@ -27,7 +20,8 @@ void RunLoop::Impl::onWriteEvent(int fd) {
}

RunLoop* RunLoop::Get() {
return current.get();
assert(static_cast<RunLoop*>(Scheduler::GetCurrent()));
return static_cast<RunLoop*>(Scheduler::GetCurrent());
}

RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) {
Expand All @@ -42,14 +36,14 @@ RunLoop::RunLoop(Type type) : impl(std::make_unique<Impl>()) {

impl->type = type;

current.set(this);
Scheduler::SetCurrent(this);
impl->async = std::make_unique<AsyncTask>(std::bind(&RunLoop::process, this));
}

RunLoop::~RunLoop() {
MBGL_VERIFY_THREAD(tid);

current.set(nullptr);
Scheduler::SetCurrent(nullptr);
}

LOOP_HANDLE RunLoop::getLoopHandle() {
Expand Down
4 changes: 2 additions & 2 deletions platform/qt/src/thread_local.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <mbgl/util/thread_local.hpp>

#include <mbgl/util/run_loop.hpp>
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/renderer/backend_scope.hpp>

#include <array>
Expand Down Expand Up @@ -41,7 +41,7 @@ void ThreadLocal<T>::set(T* ptr) {
impl->local.localData()[0] = ptr;
}

template class ThreadLocal<RunLoop>;
template class ThreadLocal<Scheduler>;
template class ThreadLocal<BackendScope>;
template class ThreadLocal<int>; // For unit tests

Expand Down
19 changes: 19 additions & 0 deletions src/mbgl/actor/scheduler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <mbgl/actor/scheduler.hpp>
#include <mbgl/util/thread_local.hpp>

namespace mbgl {

static auto& current() {
static util::ThreadLocal<Scheduler> scheduler;
return scheduler;
};

void Scheduler::SetCurrent(Scheduler* scheduler) {
current().set(scheduler);
}

Scheduler* Scheduler::GetCurrent() {
return current().get();
}

} //namespace mbgl
4 changes: 2 additions & 2 deletions src/mbgl/sprite/sprite_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <mbgl/storage/file_source.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/actor/actor.hpp>
#include <mbgl/actor/scheduler.hpp>

#include <cassert>

Expand All @@ -21,7 +21,7 @@ static SpriteLoaderObserver nullObserver;

struct SpriteLoader::Loader {
Loader(Scheduler& scheduler, SpriteLoader& imageManager)
: mailbox(std::make_shared<Mailbox>(*util::RunLoop::Get())),
: mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())),
worker(scheduler, ActorRef<SpriteLoader>(imageManager, mailbox)) {
}

Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/storage/file_source_request.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <mbgl/storage/file_source_request.hpp>

#include <mbgl/actor/mailbox.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/actor/scheduler.hpp>

namespace mbgl {

FileSourceRequest::FileSourceRequest(FileSource::Callback&& callback)
: responseCallback(callback)
, mailbox(std::make_shared<Mailbox>(*util::RunLoop::Get())) {
, mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())) {
}

FileSourceRequest::~FileSourceRequest() {
Expand Down
4 changes: 2 additions & 2 deletions src/mbgl/tile/geometry_tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
#include <mbgl/geometry/feature_index.hpp>
#include <mbgl/text/collision_tile.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/style/filter_evaluator.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/logging.hpp>
#include <mbgl/actor/scheduler.hpp>

#include <iostream>

Expand All @@ -33,7 +33,7 @@ GeometryTile::GeometryTile(const OverscaledTileID& id_,
const TileParameters& parameters)
: Tile(id_),
sourceID(std::move(sourceID_)),
mailbox(std::make_shared<Mailbox>(*util::RunLoop::Get())),
mailbox(std::make_shared<Mailbox>(*Scheduler::GetCurrent())),
worker(parameters.workerScheduler,
ActorRef<GeometryTile>(*this, mailbox),
id_,
Expand Down
Loading