From 06f1dc48a2fb317979ab48ce323115be5bd48f16 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 14 Feb 2019 16:56:17 +0200 Subject: [PATCH] [core] Make the BackgroundScheduler a singleton - 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`. --- benchmark/api/query.benchmark.cpp | 6 +- benchmark/api/render.benchmark.cpp | 18 ++-- bin/render.cpp | 6 +- include/mbgl/actor/actor.hpp | 5 ++ include/mbgl/actor/scheduler.hpp | 5 ++ include/mbgl/map/map.hpp | 2 - include/mbgl/renderer/renderer.hpp | 3 +- include/mbgl/style/style.hpp | 3 +- platform/android/core-files.json | 6 +- platform/android/src/map_renderer.cpp | 4 +- platform/android/src/native_map_view.cpp | 6 +- platform/android/src/native_map_view.hpp | 2 - .../src/snapshotter/map_snapshotter.cpp | 7 +- .../src/snapshotter/map_snapshotter.hpp | 2 - .../src/style/sources/geojson_source.cpp | 7 +- platform/darwin/src/MGLMapSnapshotter.mm | 8 +- .../include/mbgl/gl/headless_frontend.hpp | 3 - .../include/mbgl/map/map_snapshotter.hpp | 3 +- .../include/mbgl/util/shared_thread_pool.hpp | 9 -- .../default/src/mbgl/gl/headless_frontend.cpp | 6 +- .../default/src/mbgl/map/map_snapshotter.cpp | 18 ++-- .../src/mbgl/util/shared_thread_pool.cpp | 14 --- platform/glfw/main.cpp | 7 +- platform/ios/core-files.json | 6 +- platform/ios/src/MGLMapView.mm | 10 +-- platform/linux/config.cmake | 5 -- platform/macos/core-files.json | 6 +- platform/macos/src/MGLMapView.mm | 8 +- platform/node/src/node_map.cpp | 8 +- platform/node/src/node_map.hpp | 3 - platform/node/src/node_thread_pool.cpp | 12 +++ platform/qt/qt.cmake | 6 -- platform/qt/src/qmapboxgl.cpp | 5 +- platform/qt/src/qmapboxgl_map_renderer.cpp | 4 +- platform/qt/src/qmapboxgl_map_renderer.hpp | 3 +- platform/qt/src/qmapboxgl_p.hpp | 2 - .../qt/src/qmapboxgl_renderer_backend.hpp | 1 - src/core-files.json | 2 + src/mbgl/actor/scheduler.cpp | 20 ++++- src/mbgl/map/map.cpp | 4 +- src/mbgl/map/map_impl.cpp | 4 +- src/mbgl/map/map_impl.hpp | 5 +- src/mbgl/renderer/renderer.cpp | 2 - src/mbgl/renderer/renderer_impl.cpp | 3 - src/mbgl/renderer/renderer_impl.hpp | 3 - src/mbgl/renderer/tile_parameters.hpp | 2 - src/mbgl/sprite/sprite_loader.cpp | 8 +- src/mbgl/sprite/sprite_loader.hpp | 3 +- .../style/sources/custom_geometry_source.cpp | 4 +- src/mbgl/style/style.cpp | 4 +- src/mbgl/style/style_impl.cpp | 7 +- src/mbgl/style/style_impl.hpp | 4 +- src/mbgl/tile/geometry_tile.cpp | 3 +- src/mbgl/tile/raster_dem_tile.cpp | 3 +- src/mbgl/tile/raster_tile.cpp | 3 +- .../mbgl/util/thread_pool.cpp | 5 +- .../mbgl/util/thread_pool.hpp | 7 +- test/actor/actor.test.cpp | 85 ++++++++----------- test/actor/actor_ref.test.cpp | 14 ++- test/api/annotations.test.cpp | 6 +- test/api/api_misuse.test.cpp | 6 +- test/api/custom_geometry_source.test.cpp | 6 +- test/api/custom_layer.test.cpp | 6 +- test/api/query.test.cpp | 6 +- test/api/recycle_map.cpp | 7 +- test/gl/context.test.cpp | 7 +- test/map/map.test.cpp | 15 ++-- test/map/prefetch.test.cpp | 6 +- test/renderer/image_manager.test.cpp | 1 - test/sprite/sprite_loader.test.cpp | 4 +- test/src/mbgl/test/map_adapter.hpp | 3 +- test/src/mbgl/test/test.cpp | 1 + test/style/source.test.cpp | 6 +- test/style/style.test.cpp | 10 +-- test/style/style_layer.test.cpp | 4 +- test/text/local_glyph_rasterizer.test.cpp | 6 +- test/tile/custom_geometry_tile.test.cpp | 5 +- test/tile/geojson_tile.test.cpp | 5 +- test/tile/raster_dem_tile.test.cpp | 5 +- test/tile/raster_tile.test.cpp | 5 +- test/tile/vector_tile.test.cpp | 5 +- test/util/async_task.test.cpp | 19 +++-- test/util/memory.test.cpp | 14 ++- test/util/thread.test.cpp | 15 ++-- 84 files changed, 220 insertions(+), 377 deletions(-) delete mode 100644 platform/default/include/mbgl/util/shared_thread_pool.hpp delete mode 100644 platform/default/src/mbgl/util/shared_thread_pool.cpp rename platform/default/src/mbgl/util/default_thread_pool.cpp => src/mbgl/util/thread_pool.cpp (93%) rename platform/default/include/mbgl/util/default_thread_pool.hpp => src/mbgl/util/thread_pool.hpp (73%) diff --git a/benchmark/api/query.benchmark.cpp b/benchmark/api/query.benchmark.cpp index 9b7c2d727..7f08da73d 100644 --- a/benchmark/api/query.benchmark.cpp +++ b/benchmark/api/query.benchmark.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -30,9 +29,8 @@ public: } util::RunLoop loop; - ThreadPool threadPool{ 4 }; - HeadlessFrontend frontend { { 1000, 1000 }, 1, threadPool }; - Map map { frontend, MapObserver::nullObserver(), threadPool, + HeadlessFrontend frontend { { 1000, 1000 }, 1 }; + Map map { frontend, MapObserver::nullObserver(), MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize()), ResourceOptions().withCachePath("benchmark/fixtures/api/cache.db").withAssetPath(".").withAccessToken("foobar") }; ScreenBox box{{ 0, 0 }, { 1000, 1000 }}; diff --git a/benchmark/api/render.benchmark.cpp b/benchmark/api/render.benchmark.cpp index 3cff0d905..f9d3911f0 100644 --- a/benchmark/api/render.benchmark.cpp +++ b/benchmark/api/render.benchmark.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -29,7 +28,6 @@ public: } util::RunLoop loop; - ThreadPool threadPool { 4 }; }; static void prepare(Map& map, optional json = {}) { @@ -44,8 +42,8 @@ static void prepare(Map& map, optional json = {}) { static void API_renderStill_reuse_map(::benchmark::State& state) { RenderBenchmark bench; - HeadlessFrontend frontend { size, pixelRatio, bench.threadPool }; - Map map { frontend, MapObserver::nullObserver(), bench.threadPool, + HeadlessFrontend frontend { size, pixelRatio }; + Map map { frontend, MapObserver::nullObserver(), MapOptions().withMapMode(MapMode::Static).withSize(size).withPixelRatio(pixelRatio), ResourceOptions().withCachePath(cachePath).withAccessToken("foobar") }; prepare(map); @@ -57,8 +55,8 @@ static void API_renderStill_reuse_map(::benchmark::State& state) { static void API_renderStill_reuse_map_formatted_labels(::benchmark::State& state) { RenderBenchmark bench; - HeadlessFrontend frontend { size, pixelRatio, bench.threadPool }; - Map map { frontend, MapObserver::nullObserver(), bench.threadPool, + HeadlessFrontend frontend { size, pixelRatio }; + Map map { frontend, MapObserver::nullObserver(), MapOptions().withMapMode(MapMode::Static).withSize(size).withPixelRatio(pixelRatio), ResourceOptions().withCachePath(cachePath).withAccessToken("foobar") }; prepare(map, util::read_file("benchmark/fixtures/api/style_formatted_labels.json")); @@ -70,8 +68,8 @@ static void API_renderStill_reuse_map_formatted_labels(::benchmark::State& state static void API_renderStill_reuse_map_switch_styles(::benchmark::State& state) { RenderBenchmark bench; - HeadlessFrontend frontend { size, pixelRatio, bench.threadPool }; - Map map { frontend, MapObserver::nullObserver(), bench.threadPool, + HeadlessFrontend frontend { size, pixelRatio }; + Map map { frontend, MapObserver::nullObserver(), MapOptions().withMapMode(MapMode::Static).withSize(size).withPixelRatio(pixelRatio), ResourceOptions().withCachePath(cachePath).withAccessToken("foobar") }; @@ -87,8 +85,8 @@ static void API_renderStill_recreate_map(::benchmark::State& state) { RenderBenchmark bench; while (state.KeepRunning()) { - HeadlessFrontend frontend { size, pixelRatio, bench.threadPool }; - Map map { frontend, MapObserver::nullObserver(), bench.threadPool, + HeadlessFrontend frontend { size, pixelRatio }; + Map map { frontend, MapObserver::nullObserver(), MapOptions().withMapMode(MapMode::Static).withSize(size).withPixelRatio(pixelRatio), ResourceOptions().withCachePath(cachePath).withAccessToken("foobar") }; prepare(map); diff --git a/bin/render.cpp b/bin/render.cpp index a80595369..48402ffce 100644 --- a/bin/render.cpp +++ b/bin/render.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include @@ -76,9 +75,8 @@ int main(int argc, char *argv[]) { util::RunLoop loop; - ThreadPool threadPool(4); - HeadlessFrontend frontend({ width, height }, pixelRatio, threadPool); - Map map(frontend, MapObserver::nullObserver(), threadPool, + HeadlessFrontend frontend({ width, height }, pixelRatio); + Map map(frontend, MapObserver::nullObserver(), MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize()).withPixelRatio(pixelRatio), ResourceOptions().withCachePath(cache_file).withAssetPath(asset_root).withAccessToken(std::string(token))); diff --git a/include/mbgl/actor/actor.hpp b/include/mbgl/actor/actor.hpp index 0052fad24..97964ae53 100644 --- a/include/mbgl/actor/actor.hpp +++ b/include/mbgl/actor/actor.hpp @@ -55,6 +55,10 @@ public: Actor(Scheduler& scheduler, Args&&... args) : target(scheduler, parent, std::forward(args)...) {} + template + Actor(std::shared_ptr scheduler, Args&&... args) + : retainer(std::move(scheduler)), target(*retainer, parent, std::forward(args)...) {} + Actor(const Actor&) = delete; ActorRef> self() { @@ -62,6 +66,7 @@ public: } private: + std::shared_ptr retainer; AspiringActor parent; EstablishedActor target; }; diff --git a/include/mbgl/actor/scheduler.hpp b/include/mbgl/actor/scheduler.hpp index 75ead29f0..6470ab124 100644 --- a/include/mbgl/actor/scheduler.hpp +++ b/include/mbgl/actor/scheduler.hpp @@ -41,6 +41,11 @@ public: // 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 GetBackground(); }; } // namespace mbgl diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 2fdd72dcb..b1d4b14e4 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -22,7 +22,6 @@ namespace mbgl { -class Scheduler; class RendererFrontend; namespace style { @@ -34,7 +33,6 @@ class Map : private util::noncopyable { public: explicit Map(RendererFrontend&, MapObserver&, - Scheduler&, const MapOptions&, const ResourceOptions&); ~Map(); diff --git a/include/mbgl/renderer/renderer.hpp b/include/mbgl/renderer/renderer.hpp index 50483fcf4..787f9dcee 100644 --- a/include/mbgl/renderer/renderer.hpp +++ b/include/mbgl/renderer/renderer.hpp @@ -14,7 +14,6 @@ namespace mbgl { class RendererObserver; class RenderedQueryOptions; -class Scheduler; class SourceQueryOptions; class UpdateParameters; @@ -24,7 +23,7 @@ class RendererBackend; class Renderer { public: - Renderer(gfx::RendererBackend&, float pixelRatio_, Scheduler&, + Renderer(gfx::RendererBackend&, float pixelRatio_, const optional programCacheDir = {}, const optional localFontFamily = {}); ~Renderer(); diff --git a/include/mbgl/style/style.hpp b/include/mbgl/style/style.hpp index d6fdbd8f2..4a6a542b8 100644 --- a/include/mbgl/style/style.hpp +++ b/include/mbgl/style/style.hpp @@ -11,7 +11,6 @@ namespace mbgl { class FileSource; -class Scheduler; namespace style { @@ -22,7 +21,7 @@ class Layer; class Style { public: - Style(Scheduler&, FileSource&, float pixelRatio); + Style(FileSource&, float pixelRatio); ~Style(); void loadJSON(const std::string&); diff --git a/platform/android/core-files.json b/platform/android/core-files.json index 20dcc12c7..ba56695bd 100644 --- a/platform/android/core-files.json +++ b/platform/android/core-files.json @@ -89,9 +89,7 @@ "platform/default/src/mbgl/gl/headless_frontend.cpp", "platform/default/src/mbgl/map/map_snapshotter.cpp", "platform/default/src/mbgl/text/bidi.cpp", - "platform/default/src/mbgl/util/default_thread_pool.cpp", "platform/default/src/mbgl/util/png_writer.cpp", - "platform/default/src/mbgl/util/shared_thread_pool.cpp", "platform/default/src/mbgl/util/thread_local.cpp", "platform/default/src/mbgl/util/utf.cpp", "platform/linux/src/headless_backend_egl.cpp" @@ -101,9 +99,7 @@ "mbgl/gl/headless_backend.hpp": "platform/default/include/mbgl/gl/headless_backend.hpp", "mbgl/gl/headless_frontend.hpp": "platform/default/include/mbgl/gl/headless_frontend.hpp", "mbgl/map/map_snapshotter.hpp": "platform/default/include/mbgl/map/map_snapshotter.hpp", - "mbgl/text/unaccent.hpp": "platform/default/include/mbgl/text/unaccent.hpp", - "mbgl/util/default_thread_pool.hpp": "platform/default/include/mbgl/util/default_thread_pool.hpp", - "mbgl/util/shared_thread_pool.hpp": "platform/default/include/mbgl/util/shared_thread_pool.hpp" + "mbgl/text/unaccent.hpp": "platform/default/include/mbgl/text/unaccent.hpp" }, "private_headers": { "android_renderer_backend.hpp": "platform/android/src/android_renderer_backend.hpp", diff --git a/platform/android/src/map_renderer.cpp b/platform/android/src/map_renderer.cpp index 64c00585f..92444c404 100644 --- a/platform/android/src/map_renderer.cpp +++ b/platform/android/src/map_renderer.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include @@ -23,7 +22,6 @@ MapRenderer::MapRenderer(jni::JNIEnv& _env, , pixelRatio(pixelRatio_) , programCacheDir(jni::Make(_env, programCacheDir_)) , localIdeographFontFamily(localIdeographFontFamily_ ? jni::Make(_env, localIdeographFontFamily_) : optional{}) - , threadPool(sharedThreadPool()) , mailbox(std::make_shared(*this)) { } @@ -175,7 +173,7 @@ void MapRenderer::onSurfaceCreated(JNIEnv&) { // Create the new backend and renderer backend = std::make_unique(); - renderer = std::make_unique(*backend, pixelRatio, *threadPool, programCacheDir, localIdeographFontFamily); + renderer = std::make_unique(*backend, pixelRatio, programCacheDir, localIdeographFontFamily); rendererRef = std::make_unique>(*renderer, mailbox); // Set the observer on the new Renderer implementation diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 8f1ec9c57..ae53bcc80 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -66,8 +65,7 @@ NativeMapView::NativeMapView(jni::JNIEnv& _env, jni::jboolean _crossSourceCollisions) : javaPeer(_env, _obj) , mapRenderer(MapRenderer::getNativePeer(_env, jMapRenderer)) - , pixelRatio(_pixelRatio) - , threadPool(sharedThreadPool()) { + , pixelRatio(_pixelRatio) { // Get a reference to the JavaVM for callbacks if (_env.GetJavaVM(&vm) < 0) { @@ -89,7 +87,7 @@ NativeMapView::NativeMapView(jni::JNIEnv& _env, // Create the core map map = std::make_unique( - *rendererFrontend, *this, *threadPool, options, + *rendererFrontend, *this, options, mbgl::android::FileSource::getSharedResourceOptions(_env, jFileSource)); } diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index c9ba6089c..9e6ad73dd 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -258,7 +257,6 @@ private: int height = 64; // Ensure these are initialised last - std::shared_ptr threadPool; std::unique_ptr map; mbgl::EdgeInsets insets; }; diff --git a/platform/android/src/snapshotter/map_snapshotter.cpp b/platform/android/src/snapshotter/map_snapshotter.cpp index 47a2781cb..2eca6595e 100644 --- a/platform/android/src/snapshotter/map_snapshotter.cpp +++ b/platform/android/src/snapshotter/map_snapshotter.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -27,8 +26,7 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, const jni::String& _programCacheDir, const jni::String& _localIdeographFontFamily) : javaPeer(_env, _obj) - , pixelRatio(_pixelRatio) - , threadPool(sharedThreadPool()) { + , pixelRatio(_pixelRatio) { // Get a reference to the JavaVM for callbacks if (_env.GetJavaVM(&vm) < 0) { @@ -58,8 +56,7 @@ MapSnapshotter::MapSnapshotter(jni::JNIEnv& _env, showLogo = _showLogo; // Create the core snapshotter - snapshotter = std::make_unique(threadPool, - style, + snapshotter = std::make_unique(style, size, pixelRatio, cameraOptions, diff --git a/platform/android/src/snapshotter/map_snapshotter.hpp b/platform/android/src/snapshotter/map_snapshotter.hpp index e8c0885ff..791aa61d6 100644 --- a/platform/android/src/snapshotter/map_snapshotter.hpp +++ b/platform/android/src/snapshotter/map_snapshotter.hpp @@ -1,7 +1,6 @@ #pragma once #include -#include #include #include "../file_source.hpp" @@ -63,7 +62,6 @@ private: float pixelRatio; bool showLogo; - std::shared_ptr threadPool; std::unique_ptr> snapshotCallback; std::unique_ptr snapshotter; diff --git a/platform/android/src/style/sources/geojson_source.cpp b/platform/android/src/style/sources/geojson_source.cpp index a9307afe6..5ff486427 100644 --- a/platform/android/src/style/sources/geojson_source.cpp +++ b/platform/android/src/style/sources/geojson_source.cpp @@ -17,7 +17,6 @@ #include "../conversion/url_or_tileset.hpp" #include -#include // GeoJSONSource uses a "coalescing" model for high frequency asynchronous data update calls, // which in practice means, that any update that started processing is going to finish @@ -48,16 +47,14 @@ namespace android { : Source(env, std::make_unique( jni::Make(env, sourceId), convertGeoJSONOptions(env, options))) - , threadPool(sharedThreadPool()) - , converter(std::make_unique>(*threadPool)) { + , converter(std::make_unique>(Scheduler::GetBackground())) { } GeoJSONSource::GeoJSONSource(jni::JNIEnv& env, mbgl::style::Source& coreSource, AndroidRendererFrontend& frontend) : Source(env, coreSource, createJavaPeer(env), frontend) - , threadPool(sharedThreadPool()) - , converter(std::make_unique>(*threadPool)) { + , converter(std::make_unique>(Scheduler::GetBackground())) { } GeoJSONSource::~GeoJSONSource() = default; diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm index 171f24e4d..65bed2cf4 100644 --- a/platform/darwin/src/MGLMapSnapshotter.mm +++ b/platform/darwin/src/MGLMapSnapshotter.mm @@ -8,9 +8,7 @@ #import #import #import -#import #import -#import #import "MGLOfflineStorage_Private.h" #import "MGLGeometry_Private.h" @@ -125,7 +123,6 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; @end @implementation MGLMapSnapshotter { - std::shared_ptr _mbglThreadPool; std::unique_ptr _mbglMapSnapshotter; std::unique_ptr> _snapshotCallback; } @@ -176,7 +173,6 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; _mbglMapSnapshotter.reset(); _snapshotCallback.reset(); - _mbglThreadPool.reset(); self.terminated = YES; } @@ -591,8 +587,6 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; _options = options; auto mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; - - _mbglThreadPool = mbgl::sharedThreadPool(); std::string styleURL = std::string([options.styleURL.absoluteString UTF8String]); std::pair style = std::make_pair(false, styleURL); @@ -630,7 +624,7 @@ const CGFloat MGLSnapshotterMinimumPixelSize = 64; // Create the snapshotter _mbglMapSnapshotter = std::make_unique( - _mbglThreadPool, style, size, pixelRatio, cameraOptions, coordinateBounds, config.cacheDir, config.localFontFamilyName, resourceOptions); + style, size, pixelRatio, cameraOptions, coordinateBounds, config.cacheDir, config.localFontFamilyName, resourceOptions); } @end diff --git a/platform/default/include/mbgl/gl/headless_frontend.hpp b/platform/default/include/mbgl/gl/headless_frontend.hpp index ff733f423..1e693bdef 100644 --- a/platform/default/include/mbgl/gl/headless_frontend.hpp +++ b/platform/default/include/mbgl/gl/headless_frontend.hpp @@ -10,7 +10,6 @@ namespace mbgl { -class Scheduler; class Renderer; class Map; class TransformState; @@ -22,13 +21,11 @@ class RendererBackend; class HeadlessFrontend : public RendererFrontend { public: HeadlessFrontend(float pixelRatio_, - Scheduler&, const optional programCacheDir = {}, gfx::ContextMode mode = gfx::ContextMode::Unique, const optional localFontFamily = {}); HeadlessFrontend(Size, float pixelRatio_, - Scheduler&, const optional programCacheDir = {}, gfx::ContextMode mode = gfx::ContextMode::Unique, const optional localFontFamily = {}); diff --git a/platform/default/include/mbgl/map/map_snapshotter.hpp b/platform/default/include/mbgl/map/map_snapshotter.hpp index ccc3ee17f..db1ba1f26 100644 --- a/platform/default/include/mbgl/map/map_snapshotter.hpp +++ b/platform/default/include/mbgl/map/map_snapshotter.hpp @@ -26,8 +26,7 @@ class Style; class MapSnapshotter { public: - MapSnapshotter(std::shared_ptr scheduler, - const std::pair style, + MapSnapshotter(const std::pair style, const Size&, const float pixelRatio, const optional cameraOptions, diff --git a/platform/default/include/mbgl/util/shared_thread_pool.hpp b/platform/default/include/mbgl/util/shared_thread_pool.hpp deleted file mode 100644 index 04a3cb58d..000000000 --- a/platform/default/include/mbgl/util/shared_thread_pool.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -namespace mbgl { - -std::shared_ptr sharedThreadPool(); - -} // namespace mbgl diff --git a/platform/default/src/mbgl/gl/headless_frontend.cpp b/platform/default/src/mbgl/gl/headless_frontend.cpp index 7e56996f6..101028b5c 100644 --- a/platform/default/src/mbgl/gl/headless_frontend.cpp +++ b/platform/default/src/mbgl/gl/headless_frontend.cpp @@ -10,17 +10,15 @@ namespace mbgl { HeadlessFrontend::HeadlessFrontend(float pixelRatio_, - Scheduler& scheduler, const optional programCacheDir, const gfx::ContextMode contextMode, const optional localFontFamily) : HeadlessFrontend( - { 256, 256 }, pixelRatio_, scheduler, programCacheDir, contextMode, localFontFamily) { + { 256, 256 }, pixelRatio_, programCacheDir, contextMode, localFontFamily) { } HeadlessFrontend::HeadlessFrontend(Size size_, float pixelRatio_, - Scheduler& scheduler, const optional programCacheDir, const gfx::ContextMode contextMode, const optional localFontFamily) @@ -40,7 +38,7 @@ HeadlessFrontend::HeadlessFrontend(Size size_, renderer->render(*updateParameters_); } }), - renderer(std::make_unique(backend, pixelRatio, scheduler, programCacheDir, localFontFamily)) { + renderer(std::make_unique(backend, pixelRatio, programCacheDir, localFontFamily)) { } HeadlessFrontend::~HeadlessFrontend() = default; diff --git a/platform/default/src/mbgl/map/map_snapshotter.cpp b/platform/default/src/mbgl/map/map_snapshotter.cpp index 6e02ac853..7bfa2462a 100644 --- a/platform/default/src/mbgl/map/map_snapshotter.cpp +++ b/platform/default/src/mbgl/map/map_snapshotter.cpp @@ -14,8 +14,7 @@ namespace mbgl { class MapSnapshotter::Impl { public: - Impl(std::shared_ptr, - const std::pair style, + Impl(const std::pair style, const Size&, const float pixelRatio, const optional cameraOptions, @@ -42,13 +41,11 @@ public: void snapshot(ActorRef); private: - std::shared_ptr scheduler; HeadlessFrontend frontend; Map map; }; -MapSnapshotter::Impl::Impl(std::shared_ptr scheduler_, - const std::pair style, +MapSnapshotter::Impl::Impl(const std::pair style, const Size& size, const float pixelRatio, const optional cameraOptions, @@ -56,12 +53,10 @@ MapSnapshotter::Impl::Impl(std::shared_ptr scheduler_, const optional programCacheDir, const optional localFontFamily, const ResourceOptions& resourceOptions) - : scheduler(std::move(scheduler_)), - frontend( - size, pixelRatio, *scheduler, programCacheDir, gfx::ContextMode::Unique, localFontFamily), + : frontend( + size, pixelRatio, programCacheDir, gfx::ContextMode::Unique, localFontFamily), map(frontend, MapObserver::nullObserver(), - *scheduler, MapOptions().withMapMode(MapMode::Static).withSize(size).withPixelRatio(pixelRatio), resourceOptions) { if (style.first) { @@ -168,8 +163,7 @@ LatLngBounds MapSnapshotter::Impl::getRegion() const { return map.latLngBoundsForCamera(getCameraOptions()); } -MapSnapshotter::MapSnapshotter(std::shared_ptr scheduler, - const std::pair style, +MapSnapshotter::MapSnapshotter(const std::pair style, const Size& size, const float pixelRatio, const optional cameraOptions, @@ -178,7 +172,7 @@ MapSnapshotter::MapSnapshotter(std::shared_ptr scheduler, const optional localFontFamily, const ResourceOptions& resourceOptions) : impl(std::make_unique>( - "Map Snapshotter", std::move(scheduler), style, size, pixelRatio, cameraOptions, + "Map Snapshotter", style, size, pixelRatio, cameraOptions, region, programCacheDir, localFontFamily, resourceOptions.clone())) {} MapSnapshotter::~MapSnapshotter() = default; diff --git a/platform/default/src/mbgl/util/shared_thread_pool.cpp b/platform/default/src/mbgl/util/shared_thread_pool.cpp deleted file mode 100644 index d7facbab9..000000000 --- a/platform/default/src/mbgl/util/shared_thread_pool.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include - -namespace mbgl { - -std::shared_ptr sharedThreadPool() { - static std::weak_ptr weak; - auto pool = weak.lock(); - if (!pool) { - weak = pool = std::make_shared(4); - } - return pool; -} - -} // namespace mbgl diff --git a/platform/glfw/main.cpp b/platform/glfw/main.cpp index 4e631ec89..04cff3962 100644 --- a/platform/glfw/main.cpp +++ b/platform/glfw/main.cpp @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -109,10 +108,9 @@ int main(int argc, char *argv[]) { mbgl::Log::Warning(mbgl::Event::Setup, "Application is offline. Press `O` to toggle online status."); } - mbgl::ThreadPool threadPool(4); - GLFWRendererFrontend rendererFrontend { std::make_unique(view->getRendererBackend(), view->getPixelRatio(), threadPool), *view }; + GLFWRendererFrontend rendererFrontend { std::make_unique(view->getRendererBackend(), view->getPixelRatio()), *view }; - mbgl::Map map(rendererFrontend, *view, threadPool, + mbgl::Map map(rendererFrontend, *view, mbgl::MapOptions().withSize(view->getSize()).withPixelRatio(view->getPixelRatio()), resourceOptions); backend.setMap(&map); @@ -199,5 +197,6 @@ int main(int argc, char *argv[]) { settings.latitude, settings.longitude, settings.zoom, settings.bearing); view = nullptr; + return 0; } diff --git a/platform/ios/core-files.json b/platform/ios/core-files.json index c3e54a45b..98833b0c9 100644 --- a/platform/ios/core-files.json +++ b/platform/ios/core-files.json @@ -14,9 +14,7 @@ "platform/default/src/mbgl/gl/headless_frontend.cpp", "platform/default/src/mbgl/map/map_snapshotter.cpp", "platform/default/src/mbgl/text/bidi.cpp", - "platform/default/src/mbgl/util/default_thread_pool.cpp", "platform/default/src/mbgl/util/png_writer.cpp", - "platform/default/src/mbgl/util/shared_thread_pool.cpp", "platform/default/src/mbgl/util/thread_local.cpp", "platform/default/src/mbgl/util/utf.cpp" ], @@ -26,9 +24,7 @@ "mbgl/gl/headless_backend.hpp": "platform/default/include/mbgl/gl/headless_backend.hpp", "mbgl/gl/headless_frontend.hpp": "platform/default/include/mbgl/gl/headless_frontend.hpp", "mbgl/map/map_snapshotter.hpp": "platform/default/include/mbgl/map/map_snapshotter.hpp", - "mbgl/util/default_styles.hpp": "platform/default/include/mbgl/util/default_styles.hpp", - "mbgl/util/default_thread_pool.hpp": "platform/default/include/mbgl/util/default_thread_pool.hpp", - "mbgl/util/shared_thread_pool.hpp": "platform/default/include/mbgl/util/shared_thread_pool.hpp" + "mbgl/util/default_styles.hpp": "platform/default/include/mbgl/util/default_styles.hpp" }, "private_headers": { "CFHandle.hpp": "platform/darwin/src/CFHandle.hpp" diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index a4f994a2f..fa49a2f28 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -29,7 +28,6 @@ #include #include #include -#include #include #include @@ -280,8 +278,6 @@ public: MBGLView *_mbglView; std::unique_ptr _rendererFrontend; - std::shared_ptr _mbglThreadPool; - BOOL _opaque; MGLAnnotationTagContextMap _annotationContextsByAnnotationTag; @@ -476,9 +472,8 @@ public: // setup mbgl map MGLRendererConfiguration *config = [MGLRendererConfiguration currentConfiguration]; - _mbglThreadPool = mbgl::sharedThreadPool(); - auto renderer = std::make_unique(*_mbglView, config.scaleFactor, *_mbglThreadPool, config.cacheDir, config.localFontFamilyName); + auto renderer = std::make_unique(*_mbglView, config.scaleFactor, config.cacheDir, config.localFontFamilyName); BOOL enableCrossSourceCollisions = !config.perSourceCollisions; _rendererFrontend = std::make_unique(std::move(renderer), self, *_mbglView); @@ -495,7 +490,7 @@ public: .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); NSAssert(!_mbglMap, @"_mbglMap should be NULL"); - _mbglMap = new mbgl::Map(*_rendererFrontend, *_mbglView, *_mbglThreadPool, mapOptions, resourceOptions); + _mbglMap = new mbgl::Map(*_rendererFrontend, *_mbglView, mapOptions, resourceOptions); // start paused if in IB if (background) { @@ -772,7 +767,6 @@ public: _mbglView = nullptr; _rendererFrontend.reset(); - _mbglThreadPool.reset(); } - (void)dealloc diff --git a/platform/linux/config.cmake b/platform/linux/config.cmake index 4405e0583..118dfe696 100644 --- a/platform/linux/config.cmake +++ b/platform/linux/config.cmake @@ -72,11 +72,6 @@ macro(mbgl_platform_core) # Snapshotting PRIVATE platform/default/src/mbgl/map/map_snapshotter.cpp PRIVATE platform/default/include/mbgl/map/map_snapshotter.hpp - - # Thread pool - PRIVATE platform/default/src/mbgl/util/default_thread_pool.cpp - PRIVATE platform/default/src/mbgl/util/default_thread_pool.cpp - PRIVATE platform/default/src/mbgl/util/shared_thread_pool.cpp ) target_include_directories(mbgl-core diff --git a/platform/macos/core-files.json b/platform/macos/core-files.json index 9ec41f651..abeda5796 100644 --- a/platform/macos/core-files.json +++ b/platform/macos/core-files.json @@ -13,9 +13,7 @@ "platform/default/src/mbgl/gl/headless_frontend.cpp", "platform/default/src/mbgl/map/map_snapshotter.cpp", "platform/default/src/mbgl/text/bidi.cpp", - "platform/default/src/mbgl/util/default_thread_pool.cpp", "platform/default/src/mbgl/util/png_writer.cpp", - "platform/default/src/mbgl/util/shared_thread_pool.cpp", "platform/default/src/mbgl/util/thread_local.cpp", "platform/default/src/mbgl/util/utf.cpp" ], @@ -24,9 +22,7 @@ "mbgl/util/image+MGLAdditions.hpp": "platform/darwin/include/mbgl/util/image+MGLAdditions.hpp", "mbgl/gl/headless_backend.hpp": "platform/default/include/mbgl/gl/headless_backend.hpp", "mbgl/gl/headless_frontend.hpp": "platform/default/include/mbgl/gl/headless_frontend.hpp", - "mbgl/map/map_snapshotter.hpp": "platform/default/include/mbgl/map/map_snapshotter.hpp", - "mbgl/util/default_thread_pool.hpp": "platform/default/include/mbgl/util/default_thread_pool.hpp", - "mbgl/util/shared_thread_pool.hpp": "platform/default/include/mbgl/util/shared_thread_pool.hpp" + "mbgl/map/map_snapshotter.hpp": "platform/default/include/mbgl/map/map_snapshotter.hpp" }, "private_headers": { "CFHandle.hpp": "platform/darwin/src/CFHandle.hpp" diff --git a/platform/macos/src/MGLMapView.mm b/platform/macos/src/MGLMapView.mm index e9259cf90..77eb300ae 100644 --- a/platform/macos/src/MGLMapView.mm +++ b/platform/macos/src/MGLMapView.mm @@ -31,7 +31,6 @@ #import #import #import -#import #import #import #import @@ -43,7 +42,6 @@ #import #import #import -#import #import #import @@ -164,7 +162,6 @@ public: mbgl::Map *_mbglMap; MGLMapViewImpl *_mbglView; std::unique_ptr _rendererFrontend; - std::shared_ptr _mbglThreadPool; NSPanGestureRecognizer *_panGestureRecognizer; NSMagnificationGestureRecognizer *_magnificationGestureRecognizer; @@ -283,10 +280,9 @@ public: NSURL *legacyCacheURL = [cachesDirectoryURL URLByAppendingPathComponent:@"cache.db"]; [[NSFileManager defaultManager] removeItemAtURL:legacyCacheURL error:NULL]; - _mbglThreadPool = mbgl::sharedThreadPool(); MGLRendererConfiguration *config = [MGLRendererConfiguration currentConfiguration]; - auto renderer = std::make_unique(*_mbglView, config.scaleFactor, *_mbglThreadPool, config.cacheDir, config.localFontFamilyName); + auto renderer = std::make_unique(*_mbglView, config.scaleFactor, config.cacheDir, config.localFontFamilyName); BOOL enableCrossSourceCollisions = !config.perSourceCollisions; _rendererFrontend = std::make_unique(std::move(renderer), self, *_mbglView, true); @@ -302,7 +298,7 @@ public: resourceOptions.withCachePath([[MGLOfflineStorage sharedOfflineStorage] mbglCachePath]) .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); - _mbglMap = new mbgl::Map(*_rendererFrontend, *_mbglView, *_mbglThreadPool, mapOptions, resourceOptions); + _mbglMap = new mbgl::Map(*_rendererFrontend, *_mbglView, mapOptions, resourceOptions); // Install the OpenGL layer. Interface Builder’s synchronous drawing means // we can’t display a map, so don’t even bother to have a map layer. diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 7156a1f1b..0a7da3a83 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -630,8 +630,8 @@ void NodeMap::cancel() { reinterpret_cast(h->data)->renderFinished(); }); - frontend = std::make_unique(mbgl::Size{ 256, 256 }, pixelRatio, threadpool); - map = std::make_unique(*frontend, mapObserver, threadpool, + frontend = std::make_unique(mbgl::Size{ 256, 256 }, pixelRatio); + map = std::make_unique(*frontend, mapObserver, mbgl::MapOptions().withSize(frontend->getSize()) .withPixelRatio(pixelRatio) .withMapMode(mode) @@ -1214,8 +1214,8 @@ NodeMap::NodeMap(v8::Local options) : true; }()) , mapObserver(NodeMapObserver()) - , frontend(std::make_unique(mbgl::Size { 256, 256 }, pixelRatio, threadpool)) - , map(std::make_unique(*frontend, mapObserver, threadpool, + , frontend(std::make_unique(mbgl::Size { 256, 256 }, pixelRatio)) + , map(std::make_unique(*frontend, mapObserver, mbgl::MapOptions().withSize(frontend->getSize()) .withPixelRatio(pixelRatio) .withMapMode(mode) diff --git a/platform/node/src/node_map.hpp b/platform/node/src/node_map.hpp index 65664b34b..d45dbec92 100644 --- a/platform/node/src/node_map.hpp +++ b/platform/node/src/node_map.hpp @@ -1,7 +1,5 @@ #pragma once -#include "node_thread_pool.hpp" - #include #include #include @@ -79,7 +77,6 @@ public: const float pixelRatio; mbgl::MapMode mode; bool crossSourceCollisions; - NodeThreadPool threadpool; NodeMapObserver mapObserver; std::unique_ptr frontend; std::unique_ptr map; diff --git a/platform/node/src/node_thread_pool.cpp b/platform/node/src/node_thread_pool.cpp index 1f37565e8..de403c605 100644 --- a/platform/node/src/node_thread_pool.cpp +++ b/platform/node/src/node_thread_pool.cpp @@ -2,6 +2,7 @@ #include "util/async_queue.hpp" #include +#include namespace node_mbgl { @@ -35,3 +36,14 @@ void NodeThreadPool::Worker::WorkComplete() { } } // namespace node_mbgl + +namespace mbgl { +namespace platform { + +Scheduler& GetBackgroundScheduler() { + static node_mbgl::NodeThreadPool pool; + return pool; +} + +} // namespace platform +} // namespace mbgl diff --git a/platform/qt/qt.cmake b/platform/qt/qt.cmake index 911af25bc..fb572b807 100644 --- a/platform/qt/qt.cmake +++ b/platform/qt/qt.cmake @@ -17,12 +17,6 @@ set(MBGL_QT_CORE_FILES PRIVATE platform/default/include/mbgl/gl/headless_backend.hpp PRIVATE platform/qt/src/headless_backend_qt.cpp - # Thread pool - PRIVATE platform/default/src/mbgl/util/shared_thread_pool.cpp - PRIVATE platform/default/include/mbgl/util/shared_thread_pool.hpp - PRIVATE platform/default/src/mbgl/util/default_thread_pool.cpp - PRIVATE platform/default/include/mbgl/util/default_thread_pool.hpp - # Thread PRIVATE platform/qt/src/thread_local.cpp diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index e54733aba..329bf2308 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -43,7 +43,6 @@ #include #include #include -#include #include #include @@ -1724,7 +1723,6 @@ mbgl::ResourceOptions resourceOptionsFromQMapboxGLSettings(const QMapboxGLSettin QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settings, const QSize &size, qreal pixelRatio_) : QObject(q) - , m_threadPool(mbgl::sharedThreadPool()) , m_mode(settings.contextMode()) , m_pixelRatio(pixelRatio_) , m_localFontFamily(settings.localFontFamily()) @@ -1741,7 +1739,7 @@ QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settin auto resourceOptions = resourceOptionsFromQMapboxGLSettings(settings); // Setup the Map object. - mapObj = std::make_unique(*this, *m_mapObserver, *m_threadPool, + mapObj = std::make_unique(*this, *m_mapObserver, mapOptionsFromQMapboxGLSettings(settings, size, m_pixelRatio), resourceOptions); @@ -1799,7 +1797,6 @@ void QMapboxGLPrivate::createRenderer() m_mapRenderer = std::make_unique( m_pixelRatio, - *m_threadPool, m_mode, m_localFontFamily ); diff --git a/platform/qt/src/qmapboxgl_map_renderer.cpp b/platform/qt/src/qmapboxgl_map_renderer.cpp index d72b5d0f2..eba50af6b 100644 --- a/platform/qt/src/qmapboxgl_map_renderer.cpp +++ b/platform/qt/src/qmapboxgl_map_renderer.cpp @@ -26,9 +26,9 @@ static auto *getScheduler() { return scheduler.localData().get(); }; -QMapboxGLMapRenderer::QMapboxGLMapRenderer(qreal pixelRatio, mbgl::ThreadPool &tp, QMapboxGLSettings::GLContextMode mode, const QString &localFontFamily) +QMapboxGLMapRenderer::QMapboxGLMapRenderer(qreal pixelRatio, QMapboxGLSettings::GLContextMode mode, const QString &localFontFamily) : m_backend(static_cast(mode)), - m_renderer(std::make_unique(m_backend, pixelRatio, tp, mbgl::optional {}, + m_renderer(std::make_unique(m_backend, pixelRatio, mbgl::optional {}, localFontFamily.isEmpty() ? mbgl::nullopt : mbgl::optional { localFontFamily.toStdString() })) , m_forceScheduler(needsToForceScheduler()) { diff --git a/platform/qt/src/qmapboxgl_map_renderer.hpp b/platform/qt/src/qmapboxgl_map_renderer.hpp index 7db0da99f..2769b2b87 100644 --- a/platform/qt/src/qmapboxgl_map_renderer.hpp +++ b/platform/qt/src/qmapboxgl_map_renderer.hpp @@ -5,7 +5,6 @@ #include #include -#include #include #include @@ -25,7 +24,7 @@ class QMapboxGLMapRenderer : public QObject Q_OBJECT public: - QMapboxGLMapRenderer(qreal pixelRatio, mbgl::ThreadPool &, QMapboxGLSettings::GLContextMode, const QString &localFontFamily); + QMapboxGLMapRenderer(qreal pixelRatio, QMapboxGLSettings::GLContextMode, const QString &localFontFamily); virtual ~QMapboxGLMapRenderer(); void render(); diff --git a/platform/qt/src/qmapboxgl_p.hpp b/platform/qt/src/qmapboxgl_p.hpp index 6a5b5ce04..7f7040d7a 100644 --- a/platform/qt/src/qmapboxgl_p.hpp +++ b/platform/qt/src/qmapboxgl_p.hpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -56,7 +55,6 @@ private: std::shared_ptr m_updateParameters; std::unique_ptr m_mapObserver; - std::shared_ptr m_threadPool; std::unique_ptr m_mapRenderer; std::unique_ptr> m_resourceTransform; diff --git a/platform/qt/src/qmapboxgl_renderer_backend.hpp b/platform/qt/src/qmapboxgl_renderer_backend.hpp index aba800332..f821eda96 100644 --- a/platform/qt/src/qmapboxgl_renderer_backend.hpp +++ b/platform/qt/src/qmapboxgl_renderer_backend.hpp @@ -4,7 +4,6 @@ #include #include -#include class QMapboxGLRendererBackend final : public mbgl::gl::RendererBackend, public mbgl::gfx::Renderable { diff --git a/src/core-files.json b/src/core-files.json index c0412dd3a..d72193526 100644 --- a/src/core-files.json +++ b/src/core-files.json @@ -302,6 +302,7 @@ "src/mbgl/util/rapidjson.cpp", "src/mbgl/util/stopwatch.cpp", "src/mbgl/util/string.cpp", + "src/mbgl/util/thread_pool.cpp", "src/mbgl/util/tile_cover.cpp", "src/mbgl/util/tile_cover_impl.cpp", "src/mbgl/util/tiny_sdf.cpp", @@ -762,6 +763,7 @@ "mbgl/util/std.hpp": "src/mbgl/util/std.hpp", "mbgl/util/stopwatch.hpp": "src/mbgl/util/stopwatch.hpp", "mbgl/util/thread_local.hpp": "src/mbgl/util/thread_local.hpp", + "mbgl/util/thread_pool.hpp": "src/mbgl/util/thread_pool.hpp", "mbgl/util/tile_coordinate.hpp": "src/mbgl/util/tile_coordinate.hpp", "mbgl/util/tile_cover.hpp": "src/mbgl/util/tile_cover.hpp", "mbgl/util/tile_cover_impl.hpp": "src/mbgl/util/tile_cover_impl.hpp", diff --git a/src/mbgl/actor/scheduler.cpp b/src/mbgl/actor/scheduler.cpp index d7cdb2737..cb0c7728e 100644 --- a/src/mbgl/actor/scheduler.cpp +++ b/src/mbgl/actor/scheduler.cpp @@ -1,8 +1,11 @@ #include #include +#include namespace mbgl { - + +util::ThreadLocal g_currentScheduler; + static auto& current() { static util::ThreadLocal scheduler; return scheduler; @@ -16,4 +19,19 @@ Scheduler* Scheduler::GetCurrent() { return current().get(); } +// static +std::shared_ptr Scheduler::GetBackground() { + static std::weak_ptr weak; + static std::mutex mtx; + + std::lock_guard lock(mtx); + std::shared_ptr scheduler = weak.lock(); + + if (!scheduler) { + weak = scheduler = std::make_shared(4); + } + + return scheduler; +} + } //namespace mbgl diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index c31bb8ca3..c35f33305 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #include #include @@ -29,10 +28,9 @@ using namespace style; Map::Map(RendererFrontend& frontend, MapObserver& observer, - Scheduler& scheduler, const MapOptions& mapOptions, const ResourceOptions& resourceOptions) - : impl(std::make_unique(frontend, observer, scheduler, + : impl(std::make_unique(frontend, observer, FileSource::getSharedFileSource(resourceOptions), mapOptions)) {} diff --git a/src/mbgl/map/map_impl.cpp b/src/mbgl/map/map_impl.cpp index 1ec725582..fa15461ff 100644 --- a/src/mbgl/map/map_impl.cpp +++ b/src/mbgl/map/map_impl.cpp @@ -9,18 +9,16 @@ namespace mbgl { Map::Impl::Impl(RendererFrontend& frontend_, MapObserver& observer_, - Scheduler& scheduler_, std::shared_ptr fileSource_, const MapOptions& mapOptions) : observer(observer_), rendererFrontend(frontend_), - scheduler(scheduler_), transform(observer, mapOptions.constrainMode(), mapOptions.viewportMode()), mode(mapOptions.mapMode()), pixelRatio(mapOptions.pixelRatio()), crossSourceCollisions(mapOptions.crossSourceCollisions()), fileSource(std::move(fileSource_)), - style(std::make_unique(scheduler, *fileSource, pixelRatio)), + style(std::make_unique(*fileSource, pixelRatio)), annotationManager(*style) { transform.setNorthOrientation(mapOptions.northOrientation()); style->impl->setObserver(this); diff --git a/src/mbgl/map/map_impl.hpp b/src/mbgl/map/map_impl.hpp index f233f7827..84b43c834 100644 --- a/src/mbgl/map/map_impl.hpp +++ b/src/mbgl/map/map_impl.hpp @@ -1,7 +1,5 @@ #pragma once -#include -#include #include #include #include @@ -30,7 +28,7 @@ struct StillImageRequest { class Map::Impl : public style::Observer, public RendererObserver { public: - Impl(RendererFrontend&, MapObserver&, Scheduler&, std::shared_ptr, const MapOptions&); + Impl(RendererFrontend&, MapObserver&, std::shared_ptr, const MapOptions&); ~Impl() final; // StyleObserver @@ -54,7 +52,6 @@ public: MapObserver& observer; RendererFrontend& rendererFrontend; - Scheduler& scheduler; Transform transform; diff --git a/src/mbgl/renderer/renderer.cpp b/src/mbgl/renderer/renderer.cpp index 18cb0ebc0..ba213c435 100644 --- a/src/mbgl/renderer/renderer.cpp +++ b/src/mbgl/renderer/renderer.cpp @@ -9,12 +9,10 @@ namespace mbgl { Renderer::Renderer(gfx::RendererBackend& backend, float pixelRatio_, - Scheduler& scheduler_, const optional programCacheDir_, const optional localFontFamily_) : impl(std::make_unique(backend, pixelRatio_, - scheduler_, std::move(programCacheDir_), std::move(localFontFamily_))) { } diff --git a/src/mbgl/renderer/renderer_impl.cpp b/src/mbgl/renderer/renderer_impl.cpp index c756234e8..3db9439b5 100644 --- a/src/mbgl/renderer/renderer_impl.cpp +++ b/src/mbgl/renderer/renderer_impl.cpp @@ -40,11 +40,9 @@ static RendererObserver& nullObserver() { Renderer::Impl::Impl(gfx::RendererBackend& backend_, float pixelRatio_, - Scheduler& scheduler_, const optional programCacheDir_, const optional localFontFamily_) : backend(backend_) - , scheduler(scheduler_) , observer(&nullObserver()) , pixelRatio(pixelRatio_) , programCacheDir(std::move(programCacheDir_)) @@ -114,7 +112,6 @@ void Renderer::Impl::render(const UpdateParameters& updateParameters) { updateParameters.pixelRatio, updateParameters.debugOptions, updateParameters.transformState, - scheduler, updateParameters.fileSource, updateParameters.mode, updateParameters.annotationManager, diff --git a/src/mbgl/renderer/renderer_impl.hpp b/src/mbgl/renderer/renderer_impl.hpp index f0d4720ce..270ea4d51 100644 --- a/src/mbgl/renderer/renderer_impl.hpp +++ b/src/mbgl/renderer/renderer_impl.hpp @@ -26,7 +26,6 @@ class UpdateParameters; class RenderStaticData; class RenderedQueryOptions; class SourceQueryOptions; -class Scheduler; class GlyphManager; class ImageManager; class LineAtlas; @@ -42,7 +41,6 @@ class Renderer::Impl : public GlyphManagerObserver, public: Impl(gfx::RendererBackend&, float pixelRatio_, - Scheduler&, const optional programCacheDir, const optional localFontFamily_); ~Impl() final; @@ -99,7 +97,6 @@ private: friend class Renderer; gfx::RendererBackend& backend; - Scheduler& scheduler; RendererObserver* observer; diff --git a/src/mbgl/renderer/tile_parameters.hpp b/src/mbgl/renderer/tile_parameters.hpp index 665c7490d..cbe8dfb3b 100644 --- a/src/mbgl/renderer/tile_parameters.hpp +++ b/src/mbgl/renderer/tile_parameters.hpp @@ -5,7 +5,6 @@ namespace mbgl { class TransformState; -class Scheduler; class FileSource; class AnnotationManager; class ImageManager; @@ -16,7 +15,6 @@ public: const float pixelRatio; const MapDebugOptions debugOptions; const TransformState& transformState; - Scheduler& workerScheduler; FileSource& fileSource; const MapMode mode; AnnotationManager& annotationManager; diff --git a/src/mbgl/sprite/sprite_loader.cpp b/src/mbgl/sprite/sprite_loader.cpp index ce8fbe589..bfb0c570d 100644 --- a/src/mbgl/sprite/sprite_loader.cpp +++ b/src/mbgl/sprite/sprite_loader.cpp @@ -20,9 +20,9 @@ namespace mbgl { static SpriteLoaderObserver nullObserver; struct SpriteLoader::Loader { - Loader(Scheduler& scheduler, SpriteLoader& imageManager) + Loader(SpriteLoader& imageManager) : mailbox(std::make_shared(*Scheduler::GetCurrent())), - worker(scheduler, ActorRef(imageManager, mailbox)) { + worker(Scheduler::GetBackground(), ActorRef(imageManager, mailbox)) { } std::shared_ptr image; @@ -40,14 +40,14 @@ SpriteLoader::SpriteLoader(float pixelRatio_) SpriteLoader::~SpriteLoader() = default; -void SpriteLoader::load(const std::string& url, Scheduler& scheduler, FileSource& fileSource) { +void SpriteLoader::load(const std::string& url, FileSource& fileSource) { if (url.empty()) { // Treat a non-existent sprite as a successfully loaded empty sprite. observer->onSpriteLoaded({}); return; } - loader = std::make_unique(scheduler, *this); + loader = std::make_unique(*this); loader->jsonRequest = fileSource.request(Resource::spriteJSON(url, pixelRatio), [this](Response res) { if (res.error) { diff --git a/src/mbgl/sprite/sprite_loader.hpp b/src/mbgl/sprite/sprite_loader.hpp index 0daf46be9..0b7d37fa1 100644 --- a/src/mbgl/sprite/sprite_loader.hpp +++ b/src/mbgl/sprite/sprite_loader.hpp @@ -12,7 +12,6 @@ namespace mbgl { -class Scheduler; class FileSource; class SpriteLoaderObserver; @@ -21,7 +20,7 @@ public: SpriteLoader(float pixelRatio); ~SpriteLoader(); - void load(const std::string& url, Scheduler&, FileSource&); + void load(const std::string& url, FileSource&); void setObserver(SpriteLoaderObserver*); diff --git a/src/mbgl/style/sources/custom_geometry_source.cpp b/src/mbgl/style/sources/custom_geometry_source.cpp index 7420be932..6e9d8d65f 100644 --- a/src/mbgl/style/sources/custom_geometry_source.cpp +++ b/src/mbgl/style/sources/custom_geometry_source.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include @@ -14,8 +13,7 @@ namespace style { CustomGeometrySource::CustomGeometrySource(std::string id, const CustomGeometrySource::Options options) : Source(makeMutable(std::move(id), options)), - threadPool(sharedThreadPool()), - loader(std::make_unique>(*threadPool, options.fetchTileFunction, options.cancelTileFunction)) { + loader(std::make_unique>(Scheduler::GetBackground(), options.fetchTileFunction, options.cancelTileFunction)) { } CustomGeometrySource::~CustomGeometrySource() = default; diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index bd8631fc5..783c85009 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -8,8 +8,8 @@ namespace mbgl { namespace style { -Style::Style(Scheduler& scheduler, FileSource& fileSource, float pixelRatio) - : impl(std::make_unique(scheduler, fileSource, pixelRatio)) { +Style::Style(FileSource& fileSource, float pixelRatio) + : impl(std::make_unique(fileSource, pixelRatio)) { } Style::~Style() = default; diff --git a/src/mbgl/style/style_impl.cpp b/src/mbgl/style/style_impl.cpp index d527b8440..10fee73cd 100644 --- a/src/mbgl/style/style_impl.cpp +++ b/src/mbgl/style/style_impl.cpp @@ -27,9 +27,8 @@ namespace style { static Observer nullObserver; -Style::Impl::Impl(Scheduler& scheduler_, FileSource& fileSource_, float pixelRatio) - : scheduler(scheduler_), - fileSource(fileSource_), +Style::Impl::Impl(FileSource& fileSource_, float pixelRatio) + : fileSource(fileSource_), spriteLoader(std::make_unique(pixelRatio)), light(std::make_unique()), observer(&nullObserver) { @@ -111,7 +110,7 @@ void Style::Impl::parse(const std::string& json_) { setLight(std::make_unique(parser.light)); spriteLoaded = false; - spriteLoader->load(parser.spriteURL, scheduler, fileSource); + spriteLoader->load(parser.spriteURL, fileSource); glyphURL = parser.glyphURL; loaded = true; diff --git a/src/mbgl/style/style_impl.hpp b/src/mbgl/style/style_impl.hpp index 3dc222bfa..4c56f6785 100644 --- a/src/mbgl/style/style_impl.hpp +++ b/src/mbgl/style/style_impl.hpp @@ -25,7 +25,6 @@ namespace mbgl { -class Scheduler; class FileSource; class AsyncRequest; class SpriteLoader; @@ -38,7 +37,7 @@ class Style::Impl : public SpriteLoaderObserver, public LightObserver, public util::noncopyable { public: - Impl(Scheduler&, FileSource&, float pixelRatio); + Impl(FileSource&, float pixelRatio); ~Impl() override; void loadJSON(const std::string&); @@ -98,7 +97,6 @@ public: private: void parse(const std::string&); - Scheduler& scheduler; FileSource& fileSource; std::string url; diff --git a/src/mbgl/tile/geometry_tile.cpp b/src/mbgl/tile/geometry_tile.cpp index 029c761e5..d110b2f82 100644 --- a/src/mbgl/tile/geometry_tile.cpp +++ b/src/mbgl/tile/geometry_tile.cpp @@ -1,4 +1,5 @@ #include + #include #include #include @@ -45,7 +46,7 @@ GeometryTile::GeometryTile(const OverscaledTileID& id_, ImageRequestor(parameters.imageManager), sourceID(std::move(sourceID_)), mailbox(std::make_shared(*Scheduler::GetCurrent())), - worker(parameters.workerScheduler, + worker(Scheduler::GetBackground(), ActorRef(*this, mailbox), id_, sourceID, diff --git a/src/mbgl/tile/raster_dem_tile.cpp b/src/mbgl/tile/raster_dem_tile.cpp index 224c5c906..8b4e0113c 100644 --- a/src/mbgl/tile/raster_dem_tile.cpp +++ b/src/mbgl/tile/raster_dem_tile.cpp @@ -1,4 +1,5 @@ #include + #include #include #include @@ -17,7 +18,7 @@ RasterDEMTile::RasterDEMTile(const OverscaledTileID& id_, : Tile(Kind::RasterDEM, id_), loader(*this, id_, parameters, tileset), mailbox(std::make_shared(*Scheduler::GetCurrent())), - worker(parameters.workerScheduler, + worker(Scheduler::GetBackground(), ActorRef(*this, mailbox)) { encoding = tileset.encoding; diff --git a/src/mbgl/tile/raster_tile.cpp b/src/mbgl/tile/raster_tile.cpp index ff0230102..9da11b359 100644 --- a/src/mbgl/tile/raster_tile.cpp +++ b/src/mbgl/tile/raster_tile.cpp @@ -1,4 +1,5 @@ #include + #include #include #include @@ -17,7 +18,7 @@ RasterTile::RasterTile(const OverscaledTileID& id_, : Tile(Kind::Raster, id_), loader(*this, id_, parameters, tileset), mailbox(std::make_shared(*Scheduler::GetCurrent())), - worker(parameters.workerScheduler, + worker(Scheduler::GetBackground(), ActorRef(*this, mailbox)) { } diff --git a/platform/default/src/mbgl/util/default_thread_pool.cpp b/src/mbgl/util/thread_pool.cpp similarity index 93% rename from platform/default/src/mbgl/util/default_thread_pool.cpp rename to src/mbgl/util/thread_pool.cpp index d3950bb8a..79c2f7501 100644 --- a/platform/default/src/mbgl/util/default_thread_pool.cpp +++ b/src/mbgl/util/thread_pool.cpp @@ -1,5 +1,5 @@ -#include -#include +#include + #include #include @@ -7,6 +7,7 @@ namespace mbgl { ThreadPool::ThreadPool(std::size_t count) { threads.reserve(count); + for (std::size_t i = 0; i < count; ++i) { threads.emplace_back([this, i]() { platform::setCurrentThreadName(std::string{ "Worker " } + util::toString(i + 1)); diff --git a/platform/default/include/mbgl/util/default_thread_pool.hpp b/src/mbgl/util/thread_pool.hpp similarity index 73% rename from platform/default/include/mbgl/util/default_thread_pool.hpp rename to src/mbgl/util/thread_pool.hpp index a14d16d77..509fd0606 100644 --- a/platform/default/include/mbgl/util/default_thread_pool.hpp +++ b/src/mbgl/util/thread_pool.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -9,9 +10,9 @@ namespace mbgl { -class ThreadPool : public Scheduler { +class ThreadPool final : public Scheduler { public: - ThreadPool(std::size_t count); + explicit ThreadPool(std::size_t count); ~ThreadPool() override; void schedule(std::weak_ptr) override; @@ -21,7 +22,7 @@ private: std::queue> queue; std::mutex mutex; std::condition_variable cv; - bool terminate { false }; + bool terminate{ false }; }; } // namespace mbgl diff --git a/test/actor/actor.test.cpp b/test/actor/actor.test.cpp index 02308b7ce..6db95a83f 100644 --- a/test/actor/actor.test.cpp +++ b/test/actor/actor.test.cpp @@ -1,8 +1,8 @@ #include -#include -#include +#include #include +#include #include #include @@ -20,9 +20,8 @@ TEST(Actor, Construction) { }; }; - ThreadPool pool { 1 }; bool constructed = false; - Actor test(pool, std::ref(constructed)); + Actor test(Scheduler::GetBackground(), std::ref(constructed)); EXPECT_TRUE(constructed); } @@ -33,14 +32,13 @@ TEST(Actor, Destruction) { ~Test() { destructed = true; } - + bool& destructed; }; - ThreadPool pool { 1 }; bool destructed = false; { - Actor test(pool, std::ref(destructed)); + Actor test(Scheduler::GetBackground(), std::ref(destructed)); } EXPECT_TRUE(destructed); @@ -72,15 +70,13 @@ TEST(Actor, DestructionBlocksOnReceive) { } }; - ThreadPool pool { 1 }; - std::promise enteredPromise; std::future enteredFuture = enteredPromise.get_future(); std::promise exitingPromise; std::future exitingFuture = exitingPromise.get_future(); - Actor test(pool, std::move(enteredPromise), std::move(exitingFuture)); + Actor test(Scheduler::GetBackground(), std::move(enteredPromise), std::move(exitingFuture)); test.self().invoke(&Test::wait); enteredFuture.wait(); @@ -157,11 +153,9 @@ TEST(Actor, DestructionAllowedInReceiveOnSameThread) { } }; - ThreadPool pool { 1 }; - std::promise callbackFiredPromise; - - auto test = std::make_unique>(pool); + auto retainer = Scheduler::GetBackground(); + auto test = std::make_unique>(retainer); // Callback (triggered while mutex is locked in Mailbox::receive()) test->self().invoke(&Test::callMeBack, [&]() { @@ -189,12 +183,10 @@ TEST(Actor, SelfDestructionDoesntCrashWaitingReceivingThreads) { }; - ThreadPool pool { 2 }; - std::promise actorClosedPromise; - auto closingActor = std::make_unique>(pool); - auto waitingActor = std::make_unique>(pool); + auto closingActor = std::make_unique>(Scheduler::GetBackground()); + auto waitingActor = std::make_unique>(Scheduler::GetBackground()); std::atomic waitingMessageProcessed {false}; @@ -251,11 +243,9 @@ TEST(Actor, OrderedMailbox) { } }; - ThreadPool pool { 1 }; - std::promise endedPromise; std::future endedFuture = endedPromise.get_future(); - Actor test(pool, std::move(endedPromise)); + Actor test(Scheduler::GetBackground(), std::move(endedPromise)); for (auto i = 1; i <= 10; ++i) { test.self().invoke(&Test::receive, i); @@ -287,11 +277,9 @@ TEST(Actor, NonConcurrentMailbox) { } }; - ThreadPool pool { 10 }; - std::promise endedPromise; std::future endedFuture = endedPromise.get_future(); - Actor test(pool, std::move(endedPromise)); + Actor test(Scheduler::GetBackground(), std::move(endedPromise)); for (auto i = 1; i <= 10; ++i) { test.self().invoke(&Test::receive, i); @@ -313,13 +301,12 @@ TEST(Actor, Ask) { } }; - ThreadPool pool { 2 }; - Actor test(pool); + Actor test(Scheduler::GetBackground()); auto result = test.self().ask(&Test::doubleIt, 1); ASSERT_TRUE(result.valid()); - + auto status = result.wait_for(std::chrono::seconds(1)); ASSERT_EQ(std::future_status::ready, status); ASSERT_EQ(2, result.get()); @@ -339,9 +326,8 @@ TEST(Actor, AskVoid) { } }; - ThreadPool pool { 1 }; bool executed = false; - Actor actor(pool, executed); + Actor actor(Scheduler::GetBackground(), executed); actor.self().ask(&Test::doIt).get(); EXPECT_TRUE(executed); @@ -349,31 +335,30 @@ TEST(Actor, AskVoid) { TEST(Actor, NoSelfActorRef) { // Not all actors need a reference to self - + // Trivially constructable struct Trivial {}; - - ThreadPool pool { 2 }; - Actor trivial(pool); - - + + Actor trivial(Scheduler::GetBackground()); + + // With arguments struct WithArguments { std::promise promise; - + WithArguments(std::promise promise_) : promise(std::move(promise_)) { } - + void receive() { promise.set_value(); } }; - + std::promise promise; auto future = promise.get_future(); - Actor withArguments(pool, std::move(promise)); - + Actor withArguments(Scheduler::GetBackground(), std::move(promise)); + withArguments.self().invoke(&WithArguments::receive); future.wait(); } @@ -386,32 +371,32 @@ TEST(Actor, TwoPhaseConstruction) { struct Test { Test(ActorRef, std::shared_ptr destroyed_) : destroyed(std::move(destroyed_)) {}; - + ~Test() { *destroyed = true; } - + void callMe(std::promise p) { p.set_value(); } - + void stop() { util::RunLoop::Get()->stop(); } - + std::shared_ptr destroyed; }; AspiringActor parent; - + auto destroyed = std::make_shared(false); - + std::promise queueExecuted; auto queueExecutedFuture = queueExecuted.get_future(); - + parent.self().invoke(&Test::callMe, std::move(queueExecuted)); parent.self().invoke(&Test::stop); - + auto thread = std::thread([ capturedArgs = std::make_tuple(destroyed), &parent @@ -420,11 +405,11 @@ TEST(Actor, TwoPhaseConstruction) { EstablishedActor test(loop, parent, capturedArgs); loop.run(); }); - + // should not hang queueExecutedFuture.get(); thread.join(); - + EXPECT_TRUE(*destroyed); } diff --git a/test/actor/actor_ref.test.cpp b/test/actor/actor_ref.test.cpp index 20aa1c35c..221a220ed 100644 --- a/test/actor/actor_ref.test.cpp +++ b/test/actor/actor_ref.test.cpp @@ -1,6 +1,6 @@ #include -#include +#include #include #include @@ -27,11 +27,10 @@ TEST(ActorRef, CanOutliveActor) { } }; - ThreadPool pool { 1 }; bool died = false; ActorRef test = [&] () { - return Actor(pool, std::ref(died)).self(); + return Actor(Scheduler::GetBackground(), std::ref(died)).self(); }(); EXPECT_TRUE(died); @@ -54,8 +53,7 @@ TEST(ActorRef, Ask) { } }; - ThreadPool pool { 1 }; - Actor actor(pool); + Actor actor(Scheduler::GetBackground()); ActorRef ref = actor.self(); EXPECT_EQ(20, ref.ask(&Test::gimme).get()); @@ -76,9 +74,8 @@ TEST(ActorRef, AskVoid) { } }; - ThreadPool pool { 1 }; bool executed = false; - Actor actor(pool, executed); + Actor actor(Scheduler::GetBackground(), executed); ActorRef ref = actor.self(); ref.ask(&Test::doIt).get(); @@ -104,8 +101,7 @@ TEST(ActorRef, AskOnDestroyedActor) { }; bool died = false; - ThreadPool pool { 1 }; - auto actor = std::make_unique>(pool, died); + auto actor = std::make_unique>(Scheduler::GetBackground(), died); ActorRef ref = actor->self(); actor.reset(); diff --git a/test/api/annotations.test.cpp b/test/api/annotations.test.cpp index 11920d162..2d76a3d15 100644 --- a/test/api/annotations.test.cpp +++ b/test/api/annotations.test.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -28,10 +27,9 @@ std::unique_ptr namedMarker(const std::string& name) { class AnnotationTest { public: util::RunLoop loop; - ThreadPool threadPool { 4 }; - HeadlessFrontend frontend { 1, threadPool }; + HeadlessFrontend frontend { 1 }; - MapAdapter map { frontend, MapObserver::nullObserver(), std::make_shared(), threadPool, + MapAdapter map { frontend, MapObserver::nullObserver(), std::make_shared(), MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize())}; void checkRendering(const char * name) { diff --git a/test/api/api_misuse.test.cpp b/test/api/api_misuse.test.cpp index b65bdf991..54d8eb591 100644 --- a/test/api/api_misuse.test.cpp +++ b/test/api/api_misuse.test.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -21,11 +20,10 @@ TEST(API, RenderWithoutCallback) { util::RunLoop loop; - ThreadPool threadPool(4); - HeadlessFrontend frontend { 1, threadPool }; + HeadlessFrontend frontend { 1 }; auto map = std::make_unique(frontend, MapObserver::nullObserver(), - std::make_shared(), threadPool, + std::make_shared(), MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize())); map->renderStill(nullptr); diff --git a/test/api/custom_geometry_source.test.cpp b/test/api/custom_geometry_source.test.cpp index f796e3086..df65dc270 100644 --- a/test/api/custom_geometry_source.test.cpp +++ b/test/api/custom_geometry_source.test.cpp @@ -2,7 +2,6 @@ #include #include -#include #include #include #include @@ -20,9 +19,8 @@ using namespace mbgl::style; TEST(CustomGeometrySource, Grid) { util::RunLoop loop; - auto threadPool = sharedThreadPool(); - HeadlessFrontend frontend { 1, *threadPool }; - Map map(frontend, MapObserver::nullObserver(), *threadPool, + HeadlessFrontend frontend { 1 }; + Map map(frontend, MapObserver::nullObserver(), MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize()), ResourceOptions().withCachePath(":memory:").withAssetPath("test/fixtures/api/assets")); map.getStyle().loadJSON(util::read_file("test/fixtures/api/water.json")); diff --git a/test/api/custom_layer.test.cpp b/test/api/custom_layer.test.cpp index 4a0a34baf..e9b9aee9b 100644 --- a/test/api/custom_layer.test.cpp +++ b/test/api/custom_layer.test.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -90,9 +89,8 @@ public: TEST(CustomLayer, Basic) { util::RunLoop loop; - ThreadPool threadPool(4); - HeadlessFrontend frontend { 1, threadPool }; - Map map(frontend, MapObserver::nullObserver(), threadPool, + HeadlessFrontend frontend { 1 }; + Map map(frontend, MapObserver::nullObserver(), MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize()), ResourceOptions().withCachePath(":memory:").withAssetPath("test/fixtures/api/assets")); map.getStyle().loadJSON(util::read_file("test/fixtures/api/water.json")); diff --git a/test/api/query.test.cpp b/test/api/query.test.cpp index ddf5df6aa..b80d3b7d8 100644 --- a/test/api/query.test.cpp +++ b/test/api/query.test.cpp @@ -1,7 +1,6 @@ #include #include -#include #include #include #include @@ -35,9 +34,8 @@ public: util::RunLoop loop; std::shared_ptr fileSource = std::make_shared(); - ThreadPool threadPool { 4 }; - HeadlessFrontend frontend { 1, threadPool }; - MapAdapter map { frontend, MapObserver::nullObserver(), fileSource, threadPool, + HeadlessFrontend frontend { 1 }; + MapAdapter map { frontend, MapObserver::nullObserver(), fileSource, MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize())}; }; diff --git a/test/api/recycle_map.cpp b/test/api/recycle_map.cpp index 18cc9c00a..55e19e3cd 100644 --- a/test/api/recycle_map.cpp +++ b/test/api/recycle_map.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -23,11 +22,9 @@ using namespace mbgl::style; TEST(API, RecycleMapUpdateImages) { util::RunLoop loop; - ThreadPool threadPool(4); - - HeadlessFrontend frontend { 1, threadPool }; + HeadlessFrontend frontend { 1 }; auto map = std::make_unique(frontend, MapObserver::nullObserver(), - std::make_shared(), threadPool, + std::make_shared(), MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize())); EXPECT_TRUE(map); diff --git a/test/gl/context.test.cpp b/test/gl/context.test.cpp index b8fe895c4..756f2c58d 100644 --- a/test/gl/context.test.cpp +++ b/test/gl/context.test.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -88,11 +87,9 @@ struct Buffer { TEST(GLContextMode, Shared) { util::RunLoop loop; - ThreadPool threadPool(4); + HeadlessFrontend frontend { 1, {}, gfx::ContextMode::Shared }; - HeadlessFrontend frontend { 1, threadPool, {}, gfx::ContextMode::Shared }; - - Map map(frontend, MapObserver::nullObserver(), threadPool, + Map map(frontend, MapObserver::nullObserver(), MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize()), ResourceOptions().withCachePath(":memory:").withAssetPath("test/fixtures/api/assets")); map.getStyle().loadJSON(util::read_file("test/fixtures/api/water.json")); diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index 3af2acdb8..ee86ce7f7 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -34,15 +33,14 @@ class MapTest { public: util::RunLoop runLoop; std::shared_ptr fileSource; - ThreadPool threadPool { 4 }; StubMapObserver observer; HeadlessFrontend frontend; MapAdapter map; MapTest(float pixelRatio = 1, MapMode mode = MapMode::Static) : fileSource(std::make_shared()) - , frontend(pixelRatio, threadPool) - , map(frontend, observer, fileSource, threadPool, + , frontend(pixelRatio) + , map(frontend, observer, fileSource, MapOptions().withMapMode(mode).withSize(frontend.getSize()).withPixelRatio(pixelRatio)) {} template @@ -50,8 +48,8 @@ public: float pixelRatio = 1, MapMode mode = MapMode::Static, typename std::enable_if::value>::type* = nullptr) : fileSource(std::make_shared(cachePath, assetPath)) - , frontend(pixelRatio, threadPool) - , map(frontend, observer, fileSource, threadPool, + , frontend(pixelRatio) + , map(frontend, observer, fileSource, MapOptions().withMapMode(mode).withSize(frontend.getSize()).withPixelRatio(pixelRatio)) {} }; @@ -744,7 +742,6 @@ TEST(Map, DontLoadUnneededTiles) { TEST(Map, TEST_DISABLED_ON_CI(ContinuousRendering)) { util::RunLoop runLoop; - ThreadPool threadPool { 4 }; using namespace std::chrono_literals; @@ -756,7 +753,7 @@ TEST(Map, TEST_DISABLED_ON_CI(ContinuousRendering)) { util::Timer timer; - HeadlessFrontend frontend(1, threadPool); + HeadlessFrontend frontend(1); StubMapObserver observer; observer.didFinishRenderingFrameCallback = [&] (MapObserver::RenderMode) { @@ -768,7 +765,7 @@ TEST(Map, TEST_DISABLED_ON_CI(ContinuousRendering)) { }); }; - Map map(frontend, observer, threadPool, + Map map(frontend, observer, MapOptions().withMapMode(MapMode::Continuous).withSize(frontend.getSize()), ResourceOptions().withCachePath(":memory:").withAssetPath("test/fixtures/api/assets")); map.getStyle().loadJSON(util::read_file("test/fixtures/api/water.json")); diff --git a/test/map/prefetch.test.cpp b/test/map/prefetch.test.cpp index 3232c99a2..071c91842 100644 --- a/test/map/prefetch.test.cpp +++ b/test/map/prefetch.test.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include #include @@ -22,7 +21,6 @@ using namespace std::chrono_literals; TEST(Map, PrefetchTiles) { util::RunLoop runLoop; - ThreadPool threadPool(4); std::shared_ptr fileSource = std::make_shared(); util::Timer emergencyShutoff; @@ -36,8 +34,8 @@ TEST(Map, PrefetchTiles) { runLoop.stop(); }; - HeadlessFrontend frontend { { 512, 512 }, 1, threadPool }; - MapAdapter map(frontend, observer, fileSource, threadPool, + HeadlessFrontend frontend { { 512, 512 }, 1 }; + MapAdapter map(frontend, observer, fileSource, MapOptions().withMapMode(MapMode::Continuous).withSize(frontend.getSize())); std::vector tiles; diff --git a/test/renderer/image_manager.test.cpp b/test/renderer/image_manager.test.cpp index 7f0d67084..87a24e3bd 100644 --- a/test/renderer/image_manager.test.cpp +++ b/test/renderer/image_manager.test.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include diff --git a/test/sprite/sprite_loader.test.cpp b/test/sprite/sprite_loader.test.cpp index 369157226..122e711f5 100644 --- a/test/sprite/sprite_loader.test.cpp +++ b/test/sprite/sprite_loader.test.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include @@ -37,7 +36,6 @@ public: util::RunLoop loop; StubFileSource fileSource; StubSpriteLoaderObserver observer; - ThreadPool threadPool { 1 }; SpriteLoader spriteLoader{ 1 }; void run() { @@ -45,7 +43,7 @@ public: Log::setObserver(std::make_unique()); spriteLoader.setObserver(&observer); - spriteLoader.load("test/fixtures/resources/sprite", threadPool, fileSource); + spriteLoader.load("test/fixtures/resources/sprite", fileSource); loop.run(); } diff --git a/test/src/mbgl/test/map_adapter.hpp b/test/src/mbgl/test/map_adapter.hpp index 5bdab1d16..d4af579ae 100644 --- a/test/src/mbgl/test/map_adapter.hpp +++ b/test/src/mbgl/test/map_adapter.hpp @@ -13,9 +13,8 @@ public: explicit MapAdapter(RendererFrontend& frontend, MapObserver& observer, std::shared_ptr fileSource, - Scheduler& scheduler, const MapOptions& options) - : Map(std::make_unique(frontend, observer, scheduler, std::move(fileSource), options)) {} + : Map(std::make_unique(frontend, observer, std::move(fileSource), options)) {} }; } // namespace mbgl diff --git a/test/src/mbgl/test/test.cpp b/test/src/mbgl/test/test.cpp index 4c3dad172..1c77aae2a 100644 --- a/test/src/mbgl/test/test.cpp +++ b/test/src/mbgl/test/test.cpp @@ -1,3 +1,4 @@ +#include #include #include diff --git a/test/style/source.test.cpp b/test/style/source.test.cpp index f0ff1f81b..289e41c75 100644 --- a/test/style/source.test.cpp +++ b/test/style/source.test.cpp @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -55,8 +54,7 @@ public: StubRenderSourceObserver renderSourceObserver; Transform transform; TransformState transformState; - ThreadPool threadPool { 1 }; - Style style { loop, fileSource, 1 }; + Style style { fileSource, 1 }; AnnotationManager annotationManager { style }; ImageManager imageManager; GlyphManager glyphManager { fileSource }; @@ -65,7 +63,6 @@ public: 1.0, MapDebugOptions(), transformState, - threadPool, fileSource, MapMode::Continuous, annotationManager, @@ -763,7 +760,6 @@ TEST(Source, ImageSourceImageUpdate) { TEST(Source, CustomGeometrySourceSetTileData) { SourceTest test; - std::shared_ptr threadPool = sharedThreadPool(); CustomGeometrySource source("source", CustomGeometrySource::Options()); source.loadDescription(test.fileSource); diff --git a/test/style/style.test.cpp b/test/style/style.test.cpp index 7f5765133..72f74d3b0 100644 --- a/test/style/style.test.cpp +++ b/test/style/style.test.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include @@ -19,9 +18,8 @@ using namespace mbgl::style; TEST(Style, Properties) { util::RunLoop loop; - ThreadPool threadPool{ 1 }; StubFileSource fileSource; - Style::Impl style { threadPool, fileSource, 1.0 }; + Style::Impl style { fileSource, 1.0 }; style.loadJSON(R"STYLE({"name": "Test"})STYLE"); ASSERT_EQ("Test", style.getName()); @@ -62,9 +60,8 @@ TEST(Style, Properties) { TEST(Style, DuplicateSource) { util::RunLoop loop; - ThreadPool threadPool{ 1 }; StubFileSource fileSource; - Style::Impl style { threadPool, fileSource, 1.0 }; + Style::Impl style { fileSource, 1.0 }; style.loadJSON(util::read_file("test/fixtures/resources/style-unused-sources.json")); @@ -84,9 +81,8 @@ TEST(Style, RemoveSourceInUse) { auto log = new FixtureLogObserver(); Log::setObserver(std::unique_ptr(log)); - ThreadPool threadPool{ 1 }; StubFileSource fileSource; - Style::Impl style { threadPool, fileSource, 1.0 }; + Style::Impl style { fileSource, 1.0 }; style.loadJSON(util::read_file("test/fixtures/resources/style-unused-sources.json")); diff --git a/test/style/style_layer.test.cpp b/test/style/style_layer.test.cpp index 7598d888e..d6a926c63 100644 --- a/test/style/style_layer.test.cpp +++ b/test/style/style_layer.test.cpp @@ -21,7 +21,6 @@ #include #include #include -#include #include #include @@ -283,9 +282,8 @@ TEST(Layer, DuplicateLayer) { util::RunLoop loop; // Setup style - ThreadPool threadPool{ 1 }; StubFileSource fileSource; - Style::Impl style { threadPool, fileSource, 1.0 }; + Style::Impl style { fileSource, 1.0 }; style.loadJSON(util::read_file("test/fixtures/resources/style-unused-sources.json")); // Add initial layer diff --git a/test/text/local_glyph_rasterizer.test.cpp b/test/text/local_glyph_rasterizer.test.cpp index f21ac0b88..4c0719e16 100644 --- a/test/text/local_glyph_rasterizer.test.cpp +++ b/test/text/local_glyph_rasterizer.test.cpp @@ -8,7 +8,6 @@ #include #include #include -#include #include /* @@ -34,15 +33,14 @@ namespace { class LocalGlyphRasterizerTest { public: LocalGlyphRasterizerTest(const optional fontFamily) - : frontend(1, threadPool, optional(), gfx::ContextMode::Unique, fontFamily) + : frontend(1, optional(), gfx::ContextMode::Unique, fontFamily) { } util::RunLoop loop; std::shared_ptr fileSource = std::make_shared(); - ThreadPool threadPool { 4 }; HeadlessFrontend frontend; - MapAdapter map { frontend, MapObserver::nullObserver(), fileSource, threadPool, + MapAdapter map { frontend, MapObserver::nullObserver(), fileSource, MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize())}; void checkRendering(const char * name) { diff --git a/test/tile/custom_geometry_tile.test.cpp b/test/tile/custom_geometry_tile.test.cpp index 7a627bad8..50bc7d0e8 100644 --- a/test/tile/custom_geometry_tile.test.cpp +++ b/test/tile/custom_geometry_tile.test.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -26,8 +25,7 @@ public: FakeFileSource fileSource; TransformState transformState; util::RunLoop loop; - ThreadPool threadPool { 1 }; - style::Style style { loop, fileSource, 1 }; + style::Style style { fileSource, 1 }; AnnotationManager annotationManager { style }; ImageManager imageManager; GlyphManager glyphManager { fileSource }; @@ -36,7 +34,6 @@ public: 1.0, MapDebugOptions(), transformState, - threadPool, fileSource, MapMode::Continuous, annotationManager, diff --git a/test/tile/geojson_tile.test.cpp b/test/tile/geojson_tile.test.cpp index 772590efb..4f4435182 100644 --- a/test/tile/geojson_tile.test.cpp +++ b/test/tile/geojson_tile.test.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -25,8 +24,7 @@ public: FakeFileSource fileSource; TransformState transformState; util::RunLoop loop; - ThreadPool threadPool { 1 }; - style::Style style { loop, fileSource, 1 }; + style::Style style { fileSource, 1 }; AnnotationManager annotationManager { style }; ImageManager imageManager; GlyphManager glyphManager { fileSource }; @@ -36,7 +34,6 @@ public: 1.0, MapDebugOptions(), transformState, - threadPool, fileSource, MapMode::Continuous, annotationManager, diff --git a/test/tile/raster_dem_tile.test.cpp b/test/tile/raster_dem_tile.test.cpp index 5a6f5a8c9..e2e7d44b9 100644 --- a/test/tile/raster_dem_tile.test.cpp +++ b/test/tile/raster_dem_tile.test.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -20,8 +19,7 @@ public: FakeFileSource fileSource; TransformState transformState; util::RunLoop loop; - ThreadPool threadPool { 1 }; - style::Style style { loop, fileSource, 1 }; + style::Style style { fileSource, 1 }; AnnotationManager annotationManager { style }; ImageManager imageManager; GlyphManager glyphManager { fileSource }; @@ -31,7 +29,6 @@ public: 1.0, MapDebugOptions(), transformState, - threadPool, fileSource, MapMode::Continuous, annotationManager, diff --git a/test/tile/raster_tile.test.cpp b/test/tile/raster_tile.test.cpp index 8b2b3dee6..2e35a9402 100644 --- a/test/tile/raster_tile.test.cpp +++ b/test/tile/raster_tile.test.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -20,8 +19,7 @@ public: FakeFileSource fileSource; TransformState transformState; util::RunLoop loop; - ThreadPool threadPool { 1 }; - style::Style style { loop, fileSource, 1 }; + style::Style style { fileSource, 1 }; AnnotationManager annotationManager { style }; ImageManager imageManager; GlyphManager glyphManager { fileSource }; @@ -31,7 +29,6 @@ public: 1.0, MapDebugOptions(), transformState, - threadPool, fileSource, MapMode::Continuous, annotationManager, diff --git a/test/tile/vector_tile.test.cpp b/test/tile/vector_tile.test.cpp index ed3eda786..c610d8c1a 100644 --- a/test/tile/vector_tile.test.cpp +++ b/test/tile/vector_tile.test.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -26,8 +25,7 @@ public: FakeFileSource fileSource; TransformState transformState; util::RunLoop loop; - ThreadPool threadPool { 1 }; - style::Style style { loop, fileSource, 1 }; + style::Style style { fileSource, 1 }; AnnotationManager annotationManager { style }; ImageManager imageManager; GlyphManager glyphManager { fileSource }; @@ -37,7 +35,6 @@ public: 1.0, MapDebugOptions(), transformState, - threadPool, fileSource, MapMode::Continuous, annotationManager, diff --git a/test/util/async_task.test.cpp b/test/util/async_task.test.cpp index f3025e895..682926a17 100644 --- a/test/util/async_task.test.cpp +++ b/test/util/async_task.test.cpp @@ -1,14 +1,15 @@ #include -#include -#include -#include +#include +#include #include +#include #include #include #include +using namespace mbgl; using namespace mbgl::util; namespace { @@ -104,11 +105,11 @@ TEST(AsyncTask, RequestCoalescingMultithreaded) { unsigned count = 0, numThreads = 25; AsyncTask async([&count] { ++count; }); - mbgl::ThreadPool threads(numThreads); - auto mailbox = std::make_shared(threads); + auto retainer = Scheduler::GetBackground(); + auto mailbox = std::make_shared(*retainer); TestWorker worker(&async); - mbgl::ActorRef workerRef(worker, mailbox); + ActorRef workerRef(worker, mailbox); for (unsigned i = 0; i < numThreads; ++i) { workerRef.invoke(&TestWorker::run); @@ -133,11 +134,11 @@ TEST(AsyncTask, ThreadSafety) { AsyncTask async([&count] { ++count; }); - mbgl::ThreadPool threads(numThreads); - auto mailbox = std::make_shared(threads); + auto retainer = Scheduler::GetBackground(); + auto mailbox = std::make_shared(*retainer); TestWorker worker(&async); - mbgl::ActorRef workerRef(worker, mailbox); + ActorRef workerRef(worker, mailbox); for (unsigned i = 0; i < numThreads; ++i) { // The callback runs on the worker, thus the atomic type. diff --git a/test/util/memory.test.cpp b/test/util/memory.test.cpp index e3ad97643..8602e6ac5 100644 --- a/test/util/memory.test.cpp +++ b/test/util/memory.test.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -37,7 +36,6 @@ public: util::RunLoop runLoop; std::shared_ptr fileSource = std::make_shared(); - ThreadPool threadPool { 4 }; private: Response response(const std::string& path) { @@ -72,8 +70,8 @@ TEST(Memory, Vector) { MemoryTest test; float ratio { 2 }; - HeadlessFrontend frontend { { 256, 256 }, ratio, test.threadPool }; - MapAdapter map(frontend, MapObserver::nullObserver(), test.fileSource, test.threadPool, + HeadlessFrontend frontend { { 256, 256 }, ratio }; + MapAdapter map(frontend, MapObserver::nullObserver(), test.fileSource, MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize()).withPixelRatio(ratio)); map.jumpTo(CameraOptions().withZoom(16)); map.getStyle().loadURL("mapbox://streets"); @@ -85,8 +83,8 @@ TEST(Memory, Raster) { MemoryTest test; float ratio { 2 }; - HeadlessFrontend frontend { { 256, 256 }, ratio, test.threadPool }; - MapAdapter map(frontend, MapObserver::nullObserver(), test.fileSource, test.threadPool, + HeadlessFrontend frontend { { 256, 256 }, ratio }; + MapAdapter map(frontend, MapObserver::nullObserver(), test.fileSource, MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize()).withPixelRatio(ratio)); map.getStyle().loadURL("mapbox://satellite"); @@ -123,8 +121,8 @@ TEST(Memory, Footprint) { class FrontendAndMap { public: FrontendAndMap(MemoryTest& test_, const char* style) - : frontend(Size{ 256, 256 }, 2, test_.threadPool) - , map(frontend, MapObserver::nullObserver(), test_.fileSource, test_.threadPool, + : frontend(Size{ 256, 256 }, 2) + , map(frontend, MapObserver::nullObserver(), test_.fileSource, MapOptions().withMapMode(MapMode::Static).withSize(frontend.getSize()).withPixelRatio(2)) { map.jumpTo(CameraOptions().withZoom(16)); map.getStyle().loadURL(style); diff --git a/test/util/thread.test.cpp b/test/util/thread.test.cpp index 2bcb9d895..49735585c 100644 --- a/test/util/thread.test.cpp +++ b/test/util/thread.test.cpp @@ -1,8 +1,9 @@ -#include -#include -#include -#include #include + +#include +#include +#include +#include #include #include @@ -139,8 +140,7 @@ TEST(Thread, Concurrency) { unsigned numMessages = 100000; std::atomic_uint completed(numMessages); - ThreadPool threadPool(10); - Actor poolWorker(threadPool); + Actor poolWorker(Scheduler::GetBackground()); auto poolWorkerRef = poolWorker.self(); Thread threadedObject("Test"); @@ -165,8 +165,7 @@ TEST(Thread, Concurrency) { TEST(Thread, ThreadPoolMessaging) { auto loop = std::make_shared(); - ThreadPool threadPool(1); - Actor poolWorker(threadPool); + Actor poolWorker(Scheduler::GetBackground()); auto poolWorkerRef = poolWorker.self(); Thread threadedObject("Test");