summaryrefslogtreecommitdiff
path: root/platform/node/src/node_thread_pool.cpp
blob: 1f37565e8ae5047fac38a234ad505e99b9c69c72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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(), [](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