31
.travis.yml
31
.travis.yml
@@ -1,29 +1,42 @@
|
||||
language: cpp
|
||||
# to prevent Travis from exporting CXX after matrix env
|
||||
language: c
|
||||
|
||||
sudo: false
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- os: linux
|
||||
compiler: clang
|
||||
env: FLAVOR=linux CXX=clang++-3.5 BUILDTYPE=Release
|
||||
addons:
|
||||
apt:
|
||||
sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.5' ]
|
||||
packages: [ 'gdb', 'clang-3.5', 'libstdc++-4.9-dev', 'libstdc++6', 'libllvm3.4', 'xutils-dev', 'libxxf86vm-dev', 'x11proto-xf86vidmode-dev', 'mesa-utils' ]
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
env: JOBS=8
|
||||
env: FLAVOR=linux CXX=g++-4.9 BUILDTYPE=Release
|
||||
addons:
|
||||
apt:
|
||||
sources: [ 'ubuntu-toolchain-r-test' ]
|
||||
packages: [ 'gdb', 'g++-4.9', 'gcc-4.9', 'libllvm3.4', 'xutils-dev', 'libxxf86vm-dev', 'x11proto-xf86vidmode-dev', 'mesa-utils' ]
|
||||
- os: osx
|
||||
compiler: clang
|
||||
env: FLAVOR=osx BUILDTYPE=Release
|
||||
|
||||
env:
|
||||
global:
|
||||
- NODE_VERSION: 0.10.35
|
||||
- NODE_VERSION: 0.10.36
|
||||
- LD_LIBRARY_PATH: "/usr/local/lib"
|
||||
- secure: pz/HAMQpnde//JJi3f+RcW32APN6g3QyFAH41JlZwgsM5Daj9RRoXqUcNg4hEyTvlThtc5t+wQQ2ejYAjMwDu00GAzRFJ03Sm45w2fPvith9fu4crXsyPUvWUfWPC0ajTXzorN4cwFwOoMoeo9DihLwb0EC0n4T6jWdBCae3k+s=
|
||||
- secure: jYFAOQoMZkZVyc5AFPBKhR9oDqp5CciwCxFhVEHVjdImM+8V60loKazyw+bVLIjzKLnQbKKdiDudWL9TE1ylK/XSlF3K7o4gU2vIh5WzosKnU70Sanxd6tHt/Ui5eK+bAymKHbLdGiXIZtBZE0tML6+wgJ9vhV4ZKna5dM9tps4=
|
||||
|
||||
before_install:
|
||||
- cd ./vendor/mbgl && source ./scripts/local_mason.sh && cd ../../
|
||||
- source ./scripts/install_node.sh
|
||||
- source ./vendor/mbgl/scripts/travis_helper.sh
|
||||
- source ./vendor/mbgl/scripts/flags.sh
|
||||
- source ./vendor/mbgl/scripts/travis_before_install.sh
|
||||
- source ./scripts/setup_mesa.sh
|
||||
- pushd ./vendor/mbgl
|
||||
- source ./scripts/${TRAVIS_OS_NAME}/install.sh
|
||||
- source ./scripts/${TRAVIS_OS_NAME}/setup.sh
|
||||
- popd
|
||||
- source ./scripts/install_node.sh
|
||||
|
||||
install:
|
||||
- npm install --build-from-source
|
||||
@@ -39,5 +52,3 @@ after_success:
|
||||
|
||||
git:
|
||||
submodules: true
|
||||
|
||||
sudo: required
|
||||
|
||||
50
binding.gyp
50
binding.gyp
@@ -21,32 +21,50 @@
|
||||
'src/node_map.cpp',
|
||||
'src/node_request.hpp',
|
||||
'src/node_request.cpp',
|
||||
'src/util/async_queue.hpp',
|
||||
],
|
||||
|
||||
'variables': {
|
||||
'cflags_cc': [
|
||||
'-Wno-unused-parameter',
|
||||
],
|
||||
},
|
||||
|
||||
'conditions': [
|
||||
['OS == "mac"', {
|
||||
['OS=="mac"', {
|
||||
'xcode_settings': {
|
||||
"CLANG_CXX_LIBRARY": "libc++",
|
||||
"CLANG_CXX_LANGUAGE_STANDARD":"c++11",
|
||||
"GCC_VERSION": "com.apple.compilers.llvm.clang.1_0",
|
||||
'MACOSX_DEPLOYMENT_TARGET': '10.9',
|
||||
'OTHER_CPLUSPLUSFLAGS': [ '<@(cflags_cc)' ],
|
||||
'CLANG_CXX_LIBRARY': 'libc++',
|
||||
'GCC_VERSION': 'com.apple.compilers.llvm.clang.1_0',
|
||||
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
|
||||
}
|
||||
'GCC_ENABLE_CPP_RTTI': 'YES',
|
||||
'OTHER_CPLUSPLUSFLAGS': [
|
||||
'-std=c++1y',
|
||||
'-Werror',
|
||||
'-Wall',
|
||||
'-Wextra',
|
||||
'-Wshadow',
|
||||
'-Wno-variadic-macros',
|
||||
'-Wno-error=unused-parameter',
|
||||
'-frtti',
|
||||
'-fexceptions',
|
||||
],
|
||||
'GCC_WARN_PEDANTIC': 'YES',
|
||||
'GCC_WARN_UNINITIALIZED_AUTOS': 'YES_AGGRESSIVE',
|
||||
'MACOSX_DEPLOYMENT_TARGET': '10.9',
|
||||
},
|
||||
}, {
|
||||
'cflags_cc': [
|
||||
'-std=c++11',
|
||||
'-std=c++14',
|
||||
'-Werror',
|
||||
'-Wall',
|
||||
'-Wextra',
|
||||
'-Wshadow',
|
||||
'-Wno-variadic-macros',
|
||||
'-Wno-error=unused-parameter',
|
||||
'-frtti',
|
||||
'-fexceptions',
|
||||
'<@(cflags_cc)',
|
||||
],
|
||||
'libraries': [ '<@(glfw3_ldflags)' ],
|
||||
}]
|
||||
}],
|
||||
['OS=="linux"', {
|
||||
'cflags_cc': [
|
||||
'-Wno-unknown-pragmas', # We are using '#pragma mark', but it is only available on Darwin.
|
||||
],
|
||||
}],
|
||||
],
|
||||
},
|
||||
|
||||
|
||||
@@ -4,5 +4,12 @@ set -e
|
||||
set -o pipefail
|
||||
|
||||
if [ ! -z "${AWS_ACCESS_KEY_ID}" ] && [ ! -z "${AWS_SECRET_ACCESS_KEY}" ] ; then
|
||||
# Install and add awscli to PATH for uploading the results
|
||||
mapbox_time "install_awscli" \
|
||||
pip install --user awscli
|
||||
export PATH="`python -m site --user-base`/bin:${PATH}"
|
||||
|
||||
mapbox_time_start "deploy_results"
|
||||
(cd ./node_modules/mapbox-gl-test-suite/ && ./bin/deploy_results.sh)
|
||||
mapbox_time_finish
|
||||
fi
|
||||
|
||||
@@ -3,7 +3,11 @@
|
||||
set -e
|
||||
set -o pipefail
|
||||
|
||||
# Mason exists on PATH from sourcing mbgl install script
|
||||
mapbox_time "node" \
|
||||
mason install node $NODE_VERSION
|
||||
|
||||
export PATH="`mason prefix node $NODE_VERSION`/bin":"$PATH"
|
||||
|
||||
node --version
|
||||
npm --version
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
set -o pipefail
|
||||
|
||||
if [[ ${TRAVIS_OS_NAME} == "linux" ]]; then
|
||||
export LD_LIBRARY_PATH=`mason prefix mesa 10.4.3`/lib:$LD_LIBRARY_PATH;
|
||||
glxinfo;
|
||||
fi
|
||||
@@ -1,15 +1,13 @@
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
|
||||
#include "compress_png.hpp"
|
||||
#include <mbgl/util/image.hpp>
|
||||
|
||||
namespace node_mbgl {
|
||||
|
||||
class CompressPNGWorker : public NanAsyncWorker {
|
||||
public:
|
||||
CompressPNGWorker(NanCallback *callback, v8::Local<v8::Object> buffer_, uint32_t width_,
|
||||
CompressPNGWorker(NanCallback *callback_, v8::Local<v8::Object> buffer_, uint32_t width_,
|
||||
uint32_t height_)
|
||||
: NanAsyncWorker(callback),
|
||||
: NanAsyncWorker(callback_),
|
||||
buffer(v8::Persistent<v8::Object>::New(buffer_)),
|
||||
data(node::Buffer::Data(buffer_)),
|
||||
width(width_),
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic ignored "-Wshadow"
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
namespace node_mbgl {
|
||||
|
||||
void InitCompressPNG(v8::Handle<v8::Object> target);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "node_file_source.hpp"
|
||||
#include "node_request.hpp"
|
||||
#include "util/async_queue.hpp"
|
||||
|
||||
#include <mbgl/storage/request.hpp>
|
||||
#include <mbgl/util/async_queue.hpp>
|
||||
|
||||
namespace node_mbgl {
|
||||
|
||||
@@ -64,31 +64,31 @@ NodeFileSource::~NodeFileSource() {
|
||||
}
|
||||
|
||||
mbgl::Request* NodeFileSource::request(const mbgl::Resource& resource, uv_loop_t* loop, Callback callback) {
|
||||
auto request = new mbgl::Request(resource, loop, std::move(callback));
|
||||
auto req = new mbgl::Request(resource, loop, std::move(callback));
|
||||
|
||||
// This function can be called from any thread. Make sure we're executing the actual call in the
|
||||
// file source loop by sending it over the queue. It will be processed in processAction().
|
||||
queue->send(Action{ Action::Add, request });
|
||||
return request;
|
||||
queue->send(Action{ Action::Add, req });
|
||||
return req;
|
||||
}
|
||||
|
||||
void NodeFileSource::cancel(mbgl::Request* request) {
|
||||
request->cancel();
|
||||
void NodeFileSource::cancel(mbgl::Request* req) {
|
||||
req->cancel();
|
||||
|
||||
// This function can be called from any thread. Make sure we're executing the actual call in the
|
||||
// file source loop by sending it over the queue. It will be processed in processAction().
|
||||
queue->send(Action{ Action::Cancel, request });
|
||||
queue->send(Action{ Action::Cancel, req });
|
||||
}
|
||||
|
||||
void NodeFileSource::request(const mbgl::Resource& resource, Callback callback) {
|
||||
auto request = new mbgl::Request(resource, nullptr, std::move(callback));
|
||||
auto req = new mbgl::Request(resource, nullptr, std::move(callback));
|
||||
|
||||
// This function can be called from any thread. Make sure we're executing the actual call in the
|
||||
// file source loop by sending it over the queue. It will be processed in processAction().
|
||||
queue->send(Action{ Action::Add, request });
|
||||
queue->send(Action{ Action::Add, req });
|
||||
}
|
||||
|
||||
void NodeFileSource::processAdd(mbgl::Request *request) {
|
||||
void NodeFileSource::processAdd(mbgl::Request *req) {
|
||||
NanScope();
|
||||
|
||||
// Make sure the loop stays alive as long as request is pending.
|
||||
@@ -96,17 +96,17 @@ void NodeFileSource::processAdd(mbgl::Request *request) {
|
||||
queue->ref();
|
||||
}
|
||||
|
||||
auto requestHandle = v8::Local<v8::Object>::New(NodeRequest::Create(handle_, request));
|
||||
pending.emplace(request, std::move(v8::Persistent<v8::Object>::New(requestHandle)));
|
||||
auto requestHandle = v8::Local<v8::Object>::New(NodeRequest::Create(handle_, req));
|
||||
pending.emplace(req, std::move(v8::Persistent<v8::Object>::New(requestHandle)));
|
||||
|
||||
v8::Local<v8::Value> argv[] = { requestHandle };
|
||||
NanMakeCallback(handle_, NanNew("request"), 1, argv);
|
||||
}
|
||||
|
||||
void NodeFileSource::processCancel(mbgl::Request *request) {
|
||||
void NodeFileSource::processCancel(mbgl::Request *req) {
|
||||
NanScope();
|
||||
|
||||
auto it = pending.find(request);
|
||||
auto it = pending.find(req);
|
||||
if (it == pending.end()) {
|
||||
// The response callback was already fired. There is no point in calling the cancelation
|
||||
// callback because the request is already completed.
|
||||
@@ -132,12 +132,12 @@ void NodeFileSource::processCancel(mbgl::Request *request) {
|
||||
}
|
||||
|
||||
// Finally, destruct the request object
|
||||
request->destruct();
|
||||
req->destruct();
|
||||
}
|
||||
|
||||
void NodeFileSource::notify(mbgl::Request *request, const std::shared_ptr<const mbgl::Response>& response) {
|
||||
void NodeFileSource::notify(mbgl::Request *req, const std::shared_ptr<const mbgl::Response>& response) {
|
||||
// First, remove the request, since it might be destructed at any point now.
|
||||
auto it = pending.find(request);
|
||||
auto it = pending.find(req);
|
||||
if (it != pending.end()) {
|
||||
it->second.Dispose();
|
||||
pending.erase(it);
|
||||
@@ -148,7 +148,7 @@ void NodeFileSource::notify(mbgl::Request *request, const std::shared_ptr<const
|
||||
}
|
||||
}
|
||||
|
||||
request->notify(response);
|
||||
req->notify(response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,15 +2,19 @@
|
||||
|
||||
#include <mbgl/storage/file_source.hpp>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic ignored "-Wshadow"
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace mbgl { namespace util { template <typename T> class AsyncQueue; } }
|
||||
|
||||
namespace node_mbgl {
|
||||
|
||||
namespace util { template <typename T> class AsyncQueue; }
|
||||
|
||||
class NodeFileSource : public node::ObjectWrap, public mbgl::FileSource {
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Static Node Methods
|
||||
@@ -27,22 +31,22 @@ public:
|
||||
~NodeFileSource();
|
||||
|
||||
mbgl::Request* request(const mbgl::Resource&, uv_loop_t*, Callback);
|
||||
void cancel(mbgl::Request *request);
|
||||
void cancel(mbgl::Request*);
|
||||
|
||||
void request(const mbgl::Resource&, Callback);
|
||||
|
||||
// visiblity?
|
||||
void notify(mbgl::Request *request, const std::shared_ptr<const mbgl::Response> &response);
|
||||
void notify(mbgl::Request*, const std::shared_ptr<const mbgl::Response>&);
|
||||
|
||||
private:
|
||||
struct Action;
|
||||
using Queue = mbgl::util::AsyncQueue<Action>;
|
||||
using Queue = util::AsyncQueue<Action>;
|
||||
|
||||
void processAdd(mbgl::Request *request);
|
||||
void processCancel(mbgl::Request *request);
|
||||
void processAdd(mbgl::Request*);
|
||||
void processCancel(mbgl::Request*);
|
||||
|
||||
private:
|
||||
std::map<mbgl::Request *, v8::Persistent<v8::Object>> pending;
|
||||
std::map<mbgl::Request*, v8::Persistent<v8::Object>> pending;
|
||||
Queue *queue = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "node_log.hpp"
|
||||
#include "util/async_queue.hpp"
|
||||
|
||||
#include <mbgl/map/environment.hpp>
|
||||
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <mbgl/platform/log.hpp>
|
||||
#include <mbgl/util/async_queue.hpp>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic ignored "-Wshadow"
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
|
||||
namespace mbgl { namespace util { template <typename T> class AsyncQueue; } }
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
namespace node_mbgl {
|
||||
|
||||
namespace util { template <typename T> class AsyncQueue; }
|
||||
|
||||
class NodeLogObserver : public mbgl::Log::Observer {
|
||||
public:
|
||||
NodeLogObserver(v8::Handle<v8::Object> target);
|
||||
@@ -22,7 +25,7 @@ private:
|
||||
v8::Persistent<v8::Object> module;
|
||||
|
||||
struct LogMessage;
|
||||
using Queue = mbgl::util::AsyncQueue<LogMessage>;
|
||||
using Queue = util::AsyncQueue<LogMessage>;
|
||||
Queue *queue = nullptr;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,8 +3,12 @@
|
||||
#include <mbgl/map/map.hpp>
|
||||
#include <mbgl/platform/default/headless_view.hpp>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic ignored "-Wshadow"
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#include <queue>
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic ignored "-Wshadow"
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#include "node_file_source.hpp"
|
||||
#include "node_map.hpp"
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
||||
#pragma GCC diagnostic ignored "-Wshadow"
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
class NodeFileSource;
|
||||
namespace mbgl { class Request; }
|
||||
|
||||
@@ -1,85 +0,0 @@
|
||||
#include "thread_object.hpp"
|
||||
#include "node_file_source.hpp"
|
||||
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Static Node Methods
|
||||
|
||||
v8::Persistent<v8::FunctionTemplate> ThreadObject::constructorTemplate;
|
||||
|
||||
void ThreadObject::Init(v8::Handle<v8::Object> target) {
|
||||
NanScope();
|
||||
|
||||
v8::Local<v8::FunctionTemplate> t = NanNew<v8::FunctionTemplate>(New);
|
||||
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
t->SetClassName(NanNew("ThreadObject"));
|
||||
|
||||
NanAssignPersistent(constructorTemplate, t);
|
||||
|
||||
target->Set(NanNew("ThreadObject"), t->GetFunction());
|
||||
}
|
||||
|
||||
NAN_METHOD(ThreadObject::New) {
|
||||
NanScope();
|
||||
|
||||
if (!args.IsConstructCall()) {
|
||||
return NanThrowTypeError("Use the new operator to create new ThreadObject objects");
|
||||
}
|
||||
|
||||
if (args.Length() < 1 || !NanHasInstance(NodeFileSource::constructorTemplate, args[0])) {
|
||||
return NanThrowTypeError("Requires a FileSource as first argument");
|
||||
}
|
||||
|
||||
auto fs = ObjectWrap::Unwrap<NodeFileSource>(args[0]->ToObject());
|
||||
auto to = new ThreadObject(fs);
|
||||
to->Wrap(args.This());
|
||||
|
||||
NanReturnValue(args.This());
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Instance
|
||||
|
||||
ThreadObject::ThreadObject(NodeFileSource *fs) {
|
||||
self = v8::Persistent<v8::Object>::New(handle_);
|
||||
|
||||
auto async = new uv_async_t;
|
||||
async->data = this;
|
||||
uv_async_init(uv_default_loop(), async, [](uv_async_t *as, int) {
|
||||
NanScope();
|
||||
auto to = reinterpret_cast<ThreadObject *>(as->data);
|
||||
|
||||
to->self.Dispose();
|
||||
|
||||
uv_close((uv_handle_t *)as, [](uv_handle_t *handle) {
|
||||
delete reinterpret_cast<uv_async_t *>(handle);
|
||||
});
|
||||
});
|
||||
|
||||
thread = std::thread([this, fs, async]() {
|
||||
auto loop = uv_loop_new();
|
||||
|
||||
fs->request({ mbgl::Resource::Image, "http://example.com" }, loop, [&](const mbgl::Response &res) {
|
||||
fprintf(stderr, "response.status: %d\n", res.status);
|
||||
fprintf(stderr, "response.etag: %s\n", res.etag.c_str());
|
||||
fprintf(stderr, "response.modified: %lld\n", res.modified);
|
||||
fprintf(stderr, "response.expires: %lld\n", res.expires);
|
||||
fprintf(stderr, "response.data.size: %ld\n", res.data.size());
|
||||
fprintf(stderr, "response.message: %s\n", res.message.c_str());
|
||||
});
|
||||
|
||||
uv_run(loop, UV_RUN_DEFAULT);
|
||||
|
||||
uv_loop_delete(loop);
|
||||
|
||||
// Notify that the loop has finished executing.
|
||||
uv_async_send(async);
|
||||
});
|
||||
}
|
||||
|
||||
ThreadObject::~ThreadObject() {
|
||||
thread.join();
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <node.h>
|
||||
#include <nan.h>
|
||||
|
||||
#include <thread>
|
||||
|
||||
class NodeFileSource;
|
||||
|
||||
class ThreadObject : public node::ObjectWrap {
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Static Node Methods
|
||||
public:
|
||||
static void Init(v8::Handle<v8::Object> target);
|
||||
static NAN_METHOD(New);
|
||||
|
||||
static v8::Persistent<v8::FunctionTemplate> constructorTemplate;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Instance
|
||||
private:
|
||||
ThreadObject(NodeFileSource *fs);
|
||||
~ThreadObject();
|
||||
|
||||
private:
|
||||
std::thread thread;
|
||||
v8::Persistent<v8::Object> self;
|
||||
};
|
||||
90
src/util/async_queue.hpp
Normal file
90
src/util/async_queue.hpp
Normal file
@@ -0,0 +1,90 @@
|
||||
#pragma once
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
|
||||
|
||||
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
|
||||
#define UV_ASYNC_PARAMS(handle) uv_async_t *handle, int
|
||||
#else
|
||||
#define UV_ASYNC_PARAMS(handle) uv_async_t *handle
|
||||
#endif
|
||||
|
||||
namespace node_mbgl {
|
||||
namespace util {
|
||||
|
||||
template <typename T>
|
||||
class AsyncQueue {
|
||||
public:
|
||||
AsyncQueue(uv_loop_t *loop, std::function<void(T &)> fn) :
|
||||
callback(fn) {
|
||||
async.data = this;
|
||||
uv_async_init(loop, &async, [](UV_ASYNC_PARAMS(handle)) {
|
||||
auto q = reinterpret_cast<AsyncQueue *>(handle->data);
|
||||
q->process();
|
||||
});
|
||||
}
|
||||
|
||||
void send(T &&data) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
queue.push(std::make_unique<T>(std::move(data)));
|
||||
}
|
||||
uv_async_send(&async);
|
||||
}
|
||||
|
||||
void send(std::unique_ptr<T> data) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
queue.push(std::move(data));
|
||||
}
|
||||
uv_async_send(&async);
|
||||
}
|
||||
|
||||
void stop() {
|
||||
uv_close((uv_handle_t *)&async, [](uv_handle_t *handle) {
|
||||
delete reinterpret_cast<AsyncQueue *>(handle->data);
|
||||
});
|
||||
}
|
||||
|
||||
void ref() {
|
||||
uv_ref((uv_handle_t *)&async);
|
||||
}
|
||||
|
||||
void unref() {
|
||||
uv_unref((uv_handle_t *)&async);
|
||||
}
|
||||
|
||||
private:
|
||||
~AsyncQueue() {
|
||||
}
|
||||
|
||||
void process() {
|
||||
std::unique_ptr<T> item;
|
||||
while (true) {
|
||||
mutex.lock();
|
||||
if (queue.empty()) {
|
||||
mutex.unlock();
|
||||
break;
|
||||
}
|
||||
item = std::move(queue.front());
|
||||
queue.pop();
|
||||
mutex.unlock();
|
||||
callback(*item);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
uv_async_t async;
|
||||
std::queue<std::unique_ptr<T>> queue;
|
||||
std::function<void(T &)> callback;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
2
vendor/mbgl
vendored
2
vendor/mbgl
vendored
Submodule vendor/mbgl updated: 97fe50f35d...1d38b63640
Reference in New Issue
Block a user