[core] [node] pass thread pool impl to Map constructor
Updates mbgl::Map constructor usage everywhere Adds NodeThreadPool implementation using AsyncQueue to call Nan::AsyncQueueWorker from main thread
This commit is contained in:
39
platform/node/src/node_thread_pool.cpp
Normal file
39
platform/node/src/node_thread_pool.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#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() {
|
||||
if (auto locked = mailbox.lock()) {
|
||||
locked->receive();
|
||||
}
|
||||
}
|
||||
|
||||
void NodeThreadPool::Worker::WorkComplete() {
|
||||
// no-op to avoid calling nullptr callback
|
||||
}
|
||||
|
||||
} // namespace node_mbgl
|
||||
Reference in New Issue
Block a user