diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index afd3ae84f..5eefe402d 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -439,7 +439,6 @@ void NodeMap::release() { }); map.reset(nullptr); - loop.reset(); } NAN_METHOD(NodeMap::DumpDebugLogs) { @@ -459,7 +458,6 @@ NodeMap::NodeMap(v8::Local options) : Nan::HandleScope scope; return Nan::Has(options, Nan::New("ratio").ToLocalChecked()).FromJust() ? Nan::Get(options, Nan::New("ratio").ToLocalChecked()).ToLocalChecked()->NumberValue() : 1.0; }()), - loop(NodeRunLoop()), map(std::make_unique(view, *this, mbgl::MapMode::Still)), async(new uv_async_t) { @@ -486,7 +484,7 @@ std::unique_ptr NodeMap::request(const mbgl::Resource& resour // This function can be called from any thread. Make sure we're executing the // JS implementation in the node event loop. - req->workRequest = loop->invokeWithCallback([this] (mbgl::Resource res, Callback cb2) { + req->workRequest = NodeRunLoop().invokeWithCallback([this] (mbgl::Resource res, Callback cb2) { Nan::HandleScope scope; auto requestHandle = NodeRequest::Create(res, cb2)->ToObject(); diff --git a/platform/node/src/node_map.hpp b/platform/node/src/node_map.hpp index 42b312510..fe36ae7ed 100644 --- a/platform/node/src/node_map.hpp +++ b/platform/node/src/node_map.hpp @@ -3,7 +3,6 @@ #include #include #include -#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" @@ -46,7 +45,6 @@ public: std::unique_ptr request(const mbgl::Resource&, Callback); mbgl::HeadlessView view; - std::shared_ptr loop; std::unique_ptr map; std::exception_ptr error; diff --git a/platform/node/src/node_mapbox_gl_native.cpp b/platform/node/src/node_mapbox_gl_native.cpp index 313f16f28..e0b094d57 100644 --- a/platform/node/src/node_mapbox_gl_native.cpp +++ b/platform/node/src/node_mapbox_gl_native.cpp @@ -12,21 +12,19 @@ namespace node_mbgl { -std::shared_ptr NodeRunLoop() { - static std::weak_ptr nodeRunLoop; - - auto loop = nodeRunLoop.lock(); - if (!loop) { - loop = std::make_shared(); - nodeRunLoop = loop; - } - - return std::move(loop); +mbgl::util::RunLoop& NodeRunLoop() { + static mbgl::util::RunLoop nodeRunLoop; + return nodeRunLoop; } } NAN_MODULE_INIT(RegisterModule) { + // This has the effect of: + // a) Ensuring that the static local variable is initialized before any thread contention. + // b) unreffing an async handle, which otherwise would keep the default loop running. + node_mbgl::NodeRunLoop().stop(); + node_mbgl::NodeMap::Init(target); node_mbgl::NodeRequest::Init(target); diff --git a/platform/node/src/node_mapbox_gl_native.hpp b/platform/node/src/node_mapbox_gl_native.hpp index 0dcf8dcd0..b98b035fe 100644 --- a/platform/node/src/node_mapbox_gl_native.hpp +++ b/platform/node/src/node_mapbox_gl_native.hpp @@ -2,10 +2,8 @@ #include -#include - namespace node_mbgl { -std::shared_ptr NodeRunLoop(); +mbgl::util::RunLoop& NodeRunLoop(); }