diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2017-03-01 09:50:34 -0800 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2017-03-15 23:22:46 +0200 |
commit | 46cb3a048d17134853cc7b9b08cd68520ac15da2 (patch) | |
tree | bedaa5f70663d1466e74afffd9db1978859cd3a4 | |
parent | af3898d980d43663d15884fa85d8fbb6339bfbdc (diff) | |
download | qtlocation-mapboxgl-upstream/tmpsantos-node_thread_pool.tar.gz |
[node] Use default thread pool instead of node thread poolupstream/tmpsantos-node_thread_pool
-rw-r--r-- | cmake/node.cmake | 6 | ||||
-rw-r--r-- | include/mbgl/util/constants.hpp | 2 | ||||
-rw-r--r-- | platform/default/mbgl/util/shared_thread_pool.cpp | 4 | ||||
-rw-r--r-- | platform/default/mbgl/util/shared_thread_pool.hpp | 4 | ||||
-rw-r--r-- | platform/node/src/node_map.cpp | 3 | ||||
-rw-r--r-- | platform/node/src/node_map.hpp | 5 | ||||
-rw-r--r-- | platform/node/src/node_thread_pool.cpp | 37 | ||||
-rw-r--r-- | platform/node/src/node_thread_pool.hpp | 37 |
8 files changed, 15 insertions, 83 deletions
diff --git a/cmake/node.cmake b/cmake/node.cmake index b9a4f68ecc..4d9ea887c6 100644 --- a/cmake/node.cmake +++ b/cmake/node.cmake @@ -19,8 +19,6 @@ target_sources(mbgl-node PRIVATE platform/node/src/node_request.cpp PRIVATE platform/node/src/node_feature.hpp PRIVATE platform/node/src/node_feature.cpp - PRIVATE platform/node/src/node_thread_pool.hpp - PRIVATE platform/node/src/node_thread_pool.cpp PRIVATE platform/node/src/util/async_queue.hpp # We are compiling with the uv loop, but since this target already has the headers for libuv, @@ -28,6 +26,10 @@ target_sources(mbgl-node PRIVATE platform/default/async_task.cpp PRIVATE platform/default/run_loop.cpp PRIVATE platform/default/timer.cpp + PRIVATE platform/default/mbgl/util/default_thread_pool.cpp + PRIVATE platform/default/mbgl/util/default_thread_pool.hpp + PRIVATE platform/default/mbgl/util/shared_thread_pool.cpp + PRIVATE platform/default/mbgl/util/shared_thread_pool.hpp ) target_compile_options(mbgl-node diff --git a/include/mbgl/util/constants.hpp b/include/mbgl/util/constants.hpp index 0f2779e33b..6abcc4eafc 100644 --- a/include/mbgl/util/constants.hpp +++ b/include/mbgl/util/constants.hpp @@ -4,6 +4,7 @@ #include <mbgl/util/unitbezier.hpp> #include <cmath> +#include <cstddef> #include <string> #include <vector> @@ -39,6 +40,7 @@ constexpr float MIN_ZOOM_F = MIN_ZOOM; constexpr float MAX_ZOOM_F = MAX_ZOOM; constexpr uint64_t DEFAULT_MAX_CACHE_SIZE = 50 * 1024 * 1024; +constexpr std::size_t DEFAULT_SHARED_THREAD_POOL_SIZE = 4; constexpr Duration DEFAULT_FADE_DURATION = Milliseconds(300); constexpr Seconds CLOCK_SKEW_RETRY_TIMEOUT { 30 }; diff --git a/platform/default/mbgl/util/shared_thread_pool.cpp b/platform/default/mbgl/util/shared_thread_pool.cpp index 7a42df21de..a10b314cd3 100644 --- a/platform/default/mbgl/util/shared_thread_pool.cpp +++ b/platform/default/mbgl/util/shared_thread_pool.cpp @@ -2,11 +2,11 @@ namespace mbgl { -std::shared_ptr<ThreadPool> sharedThreadPool() { +std::shared_ptr<ThreadPool> sharedThreadPool(std::size_t threadPoolSize) { static std::weak_ptr<ThreadPool> weak; auto pool = weak.lock(); if (!pool) { - weak = pool = std::make_shared<ThreadPool>(4); + weak = pool = std::make_shared<ThreadPool>(threadPoolSize); } return pool; } diff --git a/platform/default/mbgl/util/shared_thread_pool.hpp b/platform/default/mbgl/util/shared_thread_pool.hpp index 04a3cb58d5..b8815db617 100644 --- a/platform/default/mbgl/util/shared_thread_pool.hpp +++ b/platform/default/mbgl/util/shared_thread_pool.hpp @@ -1,9 +1,11 @@ #pragma once #include <mbgl/util/default_thread_pool.hpp> +#include <mbgl/util/constants.hpp> namespace mbgl { -std::shared_ptr<ThreadPool> sharedThreadPool(); +std::shared_ptr<ThreadPool> sharedThreadPool( + std::size_t threadPoolSize = mbgl::util::DEFAULT_SHARED_THREAD_POOL_SIZE); } // namespace mbgl diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 3046652c21..46852f7817 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -960,11 +960,12 @@ NodeMap::NodeMap(v8::Local<v8::Object> options) ->NumberValue() : 1.0; }()), + threadPool(mbgl::sharedThreadPool(8)), map(std::make_unique<mbgl::Map>(backend, mbgl::Size{ 256, 256 }, pixelRatio, *this, - threadpool, + *threadPool, mbgl::MapMode::Still)), async(new uv_async_t) { diff --git a/platform/node/src/node_map.hpp b/platform/node/src/node_map.hpp index 122e491442..106ef975f5 100644 --- a/platform/node/src/node_map.hpp +++ b/platform/node/src/node_map.hpp @@ -1,9 +1,8 @@ #pragma once -#include "node_thread_pool.hpp" - #include <mbgl/map/map.hpp> #include <mbgl/storage/file_source.hpp> +#include <mbgl/util/shared_thread_pool.hpp> #include <mbgl/gl/headless_backend.hpp> #include <mbgl/gl/offscreen_view.hpp> @@ -67,7 +66,7 @@ public: const float pixelRatio; NodeBackend backend; std::unique_ptr<mbgl::OffscreenView> view; - NodeThreadPool threadpool; + std::shared_ptr<mbgl::ThreadPool> threadPool; std::unique_ptr<mbgl::Map> map; std::exception_ptr error; diff --git a/platform/node/src/node_thread_pool.cpp b/platform/node/src/node_thread_pool.cpp deleted file mode 100644 index fd6df575fc..0000000000 --- a/platform/node/src/node_thread_pool.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "node_thread_pool.hpp" -#include "util/async_queue.hpp" - -#include <mbgl/actor/mailbox.hpp> - -namespace node_mbgl { - -NodeThreadPool::NodeThreadPool() - : queue(new util::AsyncQueue<std::weak_ptr<mbgl::Mailbox>>(uv_default_loop(), [this](std::weak_ptr<mbgl::Mailbox> mailbox) { - Worker* worker = new Worker(mailbox); - Nan::AsyncQueueWorker(worker); - })) { - // Don't keep the event loop alive. - queue->unref(); -} - -NodeThreadPool::~NodeThreadPool() { - queue->stop(); -} - -void NodeThreadPool::schedule(std::weak_ptr<mbgl::Mailbox> mailbox) { - queue->send(std::move(mailbox)); -} - -NodeThreadPool::Worker::Worker(std::weak_ptr<mbgl::Mailbox> mailbox_) - : AsyncWorker(nullptr), - mailbox(std::move(mailbox_)) {}; - -void NodeThreadPool::Worker::Execute() { - mbgl::Mailbox::maybeReceive(mailbox); -} - -void NodeThreadPool::Worker::WorkComplete() { - // no-op to avoid calling nullptr callback -} - -} // namespace node_mbgl diff --git a/platform/node/src/node_thread_pool.hpp b/platform/node/src/node_thread_pool.hpp deleted file mode 100644 index d412e53d3d..0000000000 --- a/platform/node/src/node_thread_pool.hpp +++ /dev/null @@ -1,37 +0,0 @@ -#pragma once - -#include <mbgl/actor/scheduler.hpp> - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-parameter" -#pragma GCC diagnostic ignored "-Wshadow" -#include <nan.h> -#pragma GCC diagnostic pop - -namespace node_mbgl { - -namespace util { template <typename T> class AsyncQueue; } - -class NodeThreadPool : public mbgl::Scheduler { -public: - NodeThreadPool(); - ~NodeThreadPool(); - - void schedule(std::weak_ptr<mbgl::Mailbox>) override; - -private: - util::AsyncQueue<std::weak_ptr<mbgl::Mailbox>>* queue; - - class Worker : public Nan::AsyncWorker { - public: - Worker(std::weak_ptr<mbgl::Mailbox>); - - void Execute(); - void WorkComplete(); - - private: - std::weak_ptr<mbgl::Mailbox> mailbox; - }; -}; - -} // namespace node_mbgl |