summaryrefslogtreecommitdiff
path: root/platform/default/src
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <tmpsantos@gmail.com>2019-02-14 16:56:17 +0200
committerBruno de Oliveira Abinader <bruno@mapbox.com>2019-03-08 18:36:02 +0200
commit9af70cb127d190a9b8257942c6080c8a1b440004 (patch)
tree708b1b01b3577d05b06273e5fc5b93b501be9788 /platform/default/src
parent5e2b6bf636472a4464e6ab3ae0d9d01c68de041b (diff)
downloadqtlocation-mapboxgl-9af70cb127d190a9b8257942c6080c8a1b440004.tar.gz
[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`.
Diffstat (limited to 'platform/default/src')
-rw-r--r--platform/default/src/mbgl/gl/headless_frontend.cpp8
-rw-r--r--platform/default/src/mbgl/map/map_snapshotter.cpp11
-rw-r--r--platform/default/src/mbgl/platform/thread_pool.cpp (renamed from platform/default/src/mbgl/util/default_thread_pool.cpp)28
-rw-r--r--platform/default/src/mbgl/util/shared_thread_pool.cpp14
4 files changed, 34 insertions, 27 deletions
diff --git a/platform/default/src/mbgl/gl/headless_frontend.cpp b/platform/default/src/mbgl/gl/headless_frontend.cpp
index 37b0f91f32..71cd656e59 100644
--- a/platform/default/src/mbgl/gl/headless_frontend.cpp
+++ b/platform/default/src/mbgl/gl/headless_frontend.cpp
@@ -8,11 +8,11 @@
namespace mbgl {
-HeadlessFrontend::HeadlessFrontend(float pixelRatio_, FileSource& fileSource, Scheduler& scheduler, const optional<std::string> programCacheDir, GLContextMode mode, const optional<std::string> localFontFamily)
- : HeadlessFrontend({ 256, 256 }, pixelRatio_, fileSource, scheduler, programCacheDir, mode, localFontFamily) {
+HeadlessFrontend::HeadlessFrontend(float pixelRatio_, FileSource& fileSource, const optional<std::string> programCacheDir, GLContextMode mode, const optional<std::string> localFontFamily)
+ : HeadlessFrontend({ 256, 256 }, pixelRatio_, fileSource, programCacheDir, mode, localFontFamily) {
}
-HeadlessFrontend::HeadlessFrontend(Size size_, float pixelRatio_, FileSource& fileSource, Scheduler& scheduler, const optional<std::string> programCacheDir, GLContextMode mode, const optional<std::string> localFontFamily)
+HeadlessFrontend::HeadlessFrontend(Size size_, float pixelRatio_, FileSource& fileSource, const optional<std::string> programCacheDir, GLContextMode mode, const optional<std::string> localFontFamily)
: size(size_),
pixelRatio(pixelRatio_),
backend({ static_cast<uint32_t>(size.width * pixelRatio),
@@ -23,7 +23,7 @@ HeadlessFrontend::HeadlessFrontend(Size size_, float pixelRatio_, FileSource& fi
renderer->render(*updateParameters);
}
}),
- renderer(std::make_unique<Renderer>(backend, pixelRatio, fileSource, scheduler, mode, programCacheDir, localFontFamily)) {
+ renderer(std::make_unique<Renderer>(backend, pixelRatio, fileSource, mode, 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 56bc9bdb88..9532b01b7e 100644
--- a/platform/default/src/mbgl/map/map_snapshotter.cpp
+++ b/platform/default/src/mbgl/map/map_snapshotter.cpp
@@ -15,7 +15,6 @@ namespace mbgl {
class MapSnapshotter::Impl {
public:
Impl(FileSource*,
- std::shared_ptr<Scheduler>,
const std::pair<bool, std::string> style,
const Size&,
const float pixelRatio,
@@ -42,13 +41,11 @@ public:
void snapshot(ActorRef<MapSnapshotter::Callback>);
private:
- std::shared_ptr<Scheduler> scheduler;
HeadlessFrontend frontend;
Map map;
};
MapSnapshotter::Impl::Impl(FileSource* fileSource,
- std::shared_ptr<Scheduler> scheduler_,
const std::pair<bool, std::string> style,
const Size& size,
const float pixelRatio,
@@ -56,9 +53,8 @@ MapSnapshotter::Impl::Impl(FileSource* fileSource,
const optional<LatLngBounds> region,
const optional<std::string> programCacheDir,
const optional<std::string> localFontFamily)
- : scheduler(std::move(scheduler_))
- , frontend(size, pixelRatio, *fileSource, *scheduler, programCacheDir, GLContextMode::Unique, localFontFamily)
- , map(frontend, MapObserver::nullObserver(), size, pixelRatio, *fileSource, *scheduler, MapOptions().withMapMode(MapMode::Static)) {
+ , frontend(size, pixelRatio, programCacheDir, GLContextMode::Unique, localFontFamily)
+ , map(frontend, MapObserver::nullObserver(), size, pixelRatio, *fileSource, MapOptions().withMapMode(MapMode::Static)) {
if (style.first) {
map.getStyle().loadJSON(style.second);
@@ -165,7 +161,6 @@ LatLngBounds MapSnapshotter::Impl::getRegion() const {
}
MapSnapshotter::MapSnapshotter(FileSource* fileSource,
- std::shared_ptr<Scheduler> scheduler,
const std::pair<bool, std::string> style,
const Size& size,
const float pixelRatio,
@@ -173,7 +168,7 @@ MapSnapshotter::MapSnapshotter(FileSource* fileSource,
const optional<LatLngBounds> region,
const optional<std::string> programCacheDir,
const optional<std::string> localFontFamily)
- : impl(std::make_unique<util::Thread<MapSnapshotter::Impl>>("Map Snapshotter", fileSource, std::move(scheduler), style, size, pixelRatio, cameraOptions, region, programCacheDir, localFontFamily)) {
+ : impl(std::make_unique<util::Thread<MapSnapshotter::Impl>>("Map Snapshotter", fileSource, style, size, pixelRatio, cameraOptions, region, programCacheDir, localFontFamily)) {
}
MapSnapshotter::~MapSnapshotter() = default;
diff --git a/platform/default/src/mbgl/util/default_thread_pool.cpp b/platform/default/src/mbgl/platform/thread_pool.cpp
index d3950bb8aa..d213aab71c 100644
--- a/platform/default/src/mbgl/util/default_thread_pool.cpp
+++ b/platform/default/src/mbgl/platform/thread_pool.cpp
@@ -1,12 +1,33 @@
-#include <mbgl/util/default_thread_pool.hpp>
+#include <mbgl/actor/scheduler.hpp>
#include <mbgl/actor/mailbox.hpp>
#include <mbgl/util/platform.hpp>
#include <mbgl/util/string.hpp>
+#include <condition_variable>
+#include <mutex>
+#include <queue>
+#include <thread>
+
namespace mbgl {
+class ThreadPool final : public Scheduler {
+public:
+ explicit ThreadPool(std::size_t count);
+ ~ThreadPool() override;
+
+ void schedule(std::weak_ptr<Mailbox>) override;
+
+private:
+ std::vector<std::thread> threads;
+ std::queue<std::weak_ptr<Mailbox>> queue;
+ std::mutex mutex;
+ std::condition_variable cv;
+ bool terminate { false };
+};
+
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));
@@ -54,4 +75,9 @@ void ThreadPool::schedule(std::weak_ptr<Mailbox> mailbox) {
cv.notify_one();
}
+Scheduler& Scheduler::GetBackground() {
+ static std::unique_ptr<ThreadPool> pool(new ThreadPool(4));
+ return *pool;
+}
+
} // namespace mbgl
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 d7facbab94..0000000000
--- a/platform/default/src/mbgl/util/shared_thread_pool.cpp
+++ /dev/null
@@ -1,14 +0,0 @@
-#include <mbgl/util/shared_thread_pool.hpp>
-
-namespace mbgl {
-
-std::shared_ptr<ThreadPool> sharedThreadPool() {
- static std::weak_ptr<ThreadPool> weak;
- auto pool = weak.lock();
- if (!pool) {
- weak = pool = std::make_shared<ThreadPool>(4);
- }
- return pool;
-}
-
-} // namespace mbgl