[core] Use an actor model for tile worker concurrency
This commit is contained in:
@@ -1,129 +0,0 @@
|
||||
#include <mbgl/util/worker.hpp>
|
||||
#include <mbgl/util/work_task.hpp>
|
||||
#include <mbgl/util/work_request.hpp>
|
||||
#include <mbgl/platform/platform.hpp>
|
||||
#include <mbgl/renderer/raster_bucket.hpp>
|
||||
#include <mbgl/tile/geometry_tile_data.hpp>
|
||||
#include <mbgl/style/layer.hpp>
|
||||
#include <mbgl/text/collision_tile.hpp>
|
||||
|
||||
#include <cassert>
|
||||
#include <future>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class Worker::Impl {
|
||||
public:
|
||||
Impl() = default;
|
||||
|
||||
void parseRasterTile(std::unique_ptr<RasterBucket> bucket,
|
||||
std::shared_ptr<const std::string> data,
|
||||
std::function<void(RasterTileParseResult)> callback) {
|
||||
try {
|
||||
bucket->setImage(decodeImage(*data));
|
||||
// Destruct the shared pointer before calling the callback.
|
||||
data.reset();
|
||||
callback(RasterTileParseResult(std::move(bucket)));
|
||||
} catch (...) {
|
||||
callback(std::current_exception());
|
||||
}
|
||||
}
|
||||
|
||||
void parseGeometryTile(TileWorker* worker,
|
||||
std::vector<std::unique_ptr<style::Layer>> layers,
|
||||
std::unique_ptr<const GeometryTileData> tileData,
|
||||
PlacementConfig config,
|
||||
std::function<void(TileParseResult)> callback) {
|
||||
try {
|
||||
callback(worker->parseAllLayers(std::move(layers), std::move(tileData), config));
|
||||
} catch (...) {
|
||||
callback(std::current_exception());
|
||||
}
|
||||
}
|
||||
|
||||
void parsePendingGeometryTileLayers(TileWorker* worker,
|
||||
PlacementConfig config,
|
||||
std::function<void(TileParseResult)> callback) {
|
||||
try {
|
||||
callback(worker->parsePendingLayers(config));
|
||||
} catch (...) {
|
||||
callback(std::current_exception());
|
||||
}
|
||||
}
|
||||
|
||||
void redoLayout(TileWorker* worker,
|
||||
std::vector<std::unique_ptr<style::Layer>> layers,
|
||||
PlacementConfig config,
|
||||
std::function<void(TileParseResult)> callback) {
|
||||
try {
|
||||
callback(worker->redoLayout(std::move(layers), config));
|
||||
} catch (...) {
|
||||
callback(std::current_exception());
|
||||
}
|
||||
}
|
||||
|
||||
void redoPlacement(TileWorker* worker,
|
||||
PlacementConfig config,
|
||||
std::function<void(TilePlacementResult)> callback) {
|
||||
callback(worker->redoPlacement(config));
|
||||
}
|
||||
};
|
||||
|
||||
Worker::Worker(std::size_t count) {
|
||||
util::ThreadContext context = { "Worker", util::ThreadPriority::Low };
|
||||
for (std::size_t i = 0; i < count; i++) {
|
||||
threads.emplace_back(std::make_unique<util::Thread<Impl>>(context));
|
||||
}
|
||||
}
|
||||
|
||||
Worker::~Worker() = default;
|
||||
|
||||
std::unique_ptr<AsyncRequest>
|
||||
Worker::parseRasterTile(std::unique_ptr<RasterBucket> bucket,
|
||||
const std::shared_ptr<const std::string> data,
|
||||
std::function<void(RasterTileParseResult)> callback) {
|
||||
current = (current + 1) % threads.size();
|
||||
return threads[current]->invokeWithCallback(&Worker::Impl::parseRasterTile, std::move(bucket),
|
||||
data, callback);
|
||||
}
|
||||
|
||||
std::unique_ptr<AsyncRequest>
|
||||
Worker::parseGeometryTile(TileWorker& worker,
|
||||
std::vector<std::unique_ptr<style::Layer>> layers,
|
||||
std::unique_ptr<const GeometryTileData> tileData,
|
||||
PlacementConfig config,
|
||||
std::function<void(TileParseResult)> callback) {
|
||||
current = (current + 1) % threads.size();
|
||||
return threads[current]->invokeWithCallback(&Worker::Impl::parseGeometryTile, &worker,
|
||||
std::move(layers), std::move(tileData), config, callback);
|
||||
}
|
||||
|
||||
std::unique_ptr<AsyncRequest>
|
||||
Worker::parsePendingGeometryTileLayers(TileWorker& worker,
|
||||
PlacementConfig config,
|
||||
std::function<void(TileParseResult)> callback) {
|
||||
current = (current + 1) % threads.size();
|
||||
return threads[current]->invokeWithCallback(&Worker::Impl::parsePendingGeometryTileLayers,
|
||||
&worker, config, callback);
|
||||
}
|
||||
|
||||
std::unique_ptr<AsyncRequest>
|
||||
Worker::redoLayout(TileWorker& worker,
|
||||
std::vector<std::unique_ptr<style::Layer>> layers,
|
||||
PlacementConfig config,
|
||||
std::function<void(TileParseResult)> callback) {
|
||||
current = (current + 1) % threads.size();
|
||||
return threads[current]->invokeWithCallback(&Worker::Impl::redoLayout, &worker,
|
||||
std::move(layers), config, callback);
|
||||
}
|
||||
|
||||
std::unique_ptr<AsyncRequest>
|
||||
Worker::redoPlacement(TileWorker& worker,
|
||||
PlacementConfig config,
|
||||
std::function<void(TilePlacementResult)> callback) {
|
||||
current = (current + 1) % threads.size();
|
||||
return threads[current]->invokeWithCallback(&Worker::Impl::redoPlacement, &worker,
|
||||
config, callback);
|
||||
}
|
||||
|
||||
} // end namespace mbgl
|
||||
@@ -1,66 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/tile/tile_worker.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class AsyncRequest;
|
||||
class RasterBucket;
|
||||
class GeometryTileLoader;
|
||||
class CollisionTile;
|
||||
|
||||
using RasterTileParseResult = variant<
|
||||
std::unique_ptr<Bucket>, // success
|
||||
std::exception_ptr>; // error
|
||||
|
||||
class Worker : public mbgl::util::noncopyable {
|
||||
public:
|
||||
explicit Worker(std::size_t count);
|
||||
~Worker();
|
||||
|
||||
// Request work be done on a thread pool. Callbacks are executed on the invoking
|
||||
// thread, which must have a run loop, after the work is complete.
|
||||
//
|
||||
// The return value represents the request to perform the work asynchronously.
|
||||
// Its destructor guarantees that the work function has finished executing, and
|
||||
// that the after function has either finished executing or will not execute.
|
||||
// Together, this means that an object may make a work request with lambdas which
|
||||
// bind references to itself, and if and when those lambdas execute, the references
|
||||
// will still be valid.
|
||||
|
||||
using Request = std::unique_ptr<AsyncRequest>;
|
||||
|
||||
Request parseRasterTile(std::unique_ptr<RasterBucket> bucket,
|
||||
std::shared_ptr<const std::string> data,
|
||||
std::function<void(RasterTileParseResult)> callback);
|
||||
|
||||
Request parseGeometryTile(TileWorker&,
|
||||
std::vector<std::unique_ptr<style::Layer>>,
|
||||
std::unique_ptr<const GeometryTileData>,
|
||||
PlacementConfig,
|
||||
std::function<void(TileParseResult)> callback);
|
||||
|
||||
Request parsePendingGeometryTileLayers(TileWorker&,
|
||||
PlacementConfig config,
|
||||
std::function<void(TileParseResult)> callback);
|
||||
|
||||
Request redoLayout(TileWorker&,
|
||||
std::vector<std::unique_ptr<style::Layer>>,
|
||||
PlacementConfig,
|
||||
std::function<void(TileParseResult)> callback);
|
||||
|
||||
Request redoPlacement(TileWorker&,
|
||||
PlacementConfig config,
|
||||
std::function<void(TilePlacementResult)> callback);
|
||||
|
||||
private:
|
||||
class Impl;
|
||||
std::vector<std::unique_ptr<util::Thread<Impl>>> threads;
|
||||
std::size_t current = 0;
|
||||
};
|
||||
} // namespace mbgl
|
||||
Reference in New Issue
Block a user