From 46cb3a048d17134853cc7b9b08cd68520ac15da2 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Wed, 1 Mar 2017 09:50:34 -0800 Subject: [node] Use default thread pool instead of node thread pool --- cmake/node.cmake | 6 ++-- include/mbgl/util/constants.hpp | 2 ++ platform/default/mbgl/util/shared_thread_pool.cpp | 4 +-- platform/default/mbgl/util/shared_thread_pool.hpp | 4 ++- platform/node/src/node_map.cpp | 3 +- platform/node/src/node_map.hpp | 5 ++- platform/node/src/node_thread_pool.cpp | 37 ----------------------- platform/node/src/node_thread_pool.hpp | 37 ----------------------- 8 files changed, 15 insertions(+), 83 deletions(-) delete mode 100644 platform/node/src/node_thread_pool.cpp delete mode 100644 platform/node/src/node_thread_pool.hpp 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 #include +#include #include #include @@ -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 sharedThreadPool() { +std::shared_ptr sharedThreadPool(std::size_t threadPoolSize) { static std::weak_ptr weak; auto pool = weak.lock(); if (!pool) { - weak = pool = std::make_shared(4); + weak = pool = std::make_shared(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 +#include namespace mbgl { -std::shared_ptr sharedThreadPool(); +std::shared_ptr 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 options) ->NumberValue() : 1.0; }()), + threadPool(mbgl::sharedThreadPool(8)), map(std::make_unique(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 #include +#include #include #include @@ -67,7 +66,7 @@ public: const float pixelRatio; NodeBackend backend; std::unique_ptr view; - NodeThreadPool threadpool; + std::shared_ptr threadPool; std::unique_ptr 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 - -namespace node_mbgl { - -NodeThreadPool::NodeThreadPool() - : queue(new util::AsyncQueue>(uv_default_loop(), [this](std::weak_ptr 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 mailbox) { - queue->send(std::move(mailbox)); -} - -NodeThreadPool::Worker::Worker(std::weak_ptr 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 - -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-parameter" -#pragma GCC diagnostic ignored "-Wshadow" -#include -#pragma GCC diagnostic pop - -namespace node_mbgl { - -namespace util { template class AsyncQueue; } - -class NodeThreadPool : public mbgl::Scheduler { -public: - NodeThreadPool(); - ~NodeThreadPool(); - - void schedule(std::weak_ptr) override; - -private: - util::AsyncQueue>* queue; - - class Worker : public Nan::AsyncWorker { - public: - Worker(std::weak_ptr); - - void Execute(); - void WorkComplete(); - - private: - std::weak_ptr mailbox; - }; -}; - -} // namespace node_mbgl -- cgit v1.2.1