-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
n-api,src: provide asynchronous cleanup hooks
Sometimes addons need to perform cleanup actions, for example closing libuv handles or waiting for requests to finish, that cannot be performed synchronously. Add C++ API and N-API functions that allow providing such asynchronous cleanup hooks. Fixes: #34567 PR-URL: #34572 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
- Loading branch information
Showing
13 changed files
with
368 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <assert.h> | ||
#include <node.h> | ||
#include <uv.h> | ||
|
||
void MustNotCall(void* arg, void(*cb)(void*), void* cbarg) { | ||
assert(0); | ||
} | ||
|
||
struct AsyncData { | ||
uv_async_t async; | ||
v8::Isolate* isolate; | ||
node::AsyncCleanupHookHandle handle; | ||
void (*done_cb)(void*); | ||
void* done_arg; | ||
}; | ||
|
||
void AsyncCleanupHook(void* arg, void(*cb)(void*), void* cbarg) { | ||
AsyncData* data = static_cast<AsyncData*>(arg); | ||
uv_loop_t* loop = node::GetCurrentEventLoop(data->isolate); | ||
assert(loop != nullptr); | ||
int err = uv_async_init(loop, &data->async, [](uv_async_t* async) { | ||
AsyncData* data = static_cast<AsyncData*>(async->data); | ||
// Attempting to remove the cleanup hook here should be a no-op since it | ||
// has already been started. | ||
node::RemoveEnvironmentCleanupHook(std::move(data->handle)); | ||
|
||
uv_close(reinterpret_cast<uv_handle_t*>(async), [](uv_handle_t* handle) { | ||
AsyncData* data = static_cast<AsyncData*>(handle->data); | ||
data->done_cb(data->done_arg); | ||
delete data; | ||
}); | ||
}); | ||
assert(err == 0); | ||
|
||
data->async.data = data; | ||
data->done_cb = cb; | ||
data->done_arg = cbarg; | ||
uv_async_send(&data->async); | ||
} | ||
|
||
void Initialize(v8::Local<v8::Object> exports, | ||
v8::Local<v8::Value> module, | ||
v8::Local<v8::Context> context) { | ||
AsyncData* data = new AsyncData(); | ||
data->isolate = context->GetIsolate(); | ||
auto handle = node::AddEnvironmentCleanupHook( | ||
context->GetIsolate(), | ||
AsyncCleanupHook, | ||
data); | ||
data->handle = std::move(handle); | ||
|
||
auto must_not_call_handle = node::AddEnvironmentCleanupHook( | ||
context->GetIsolate(), | ||
MustNotCall, | ||
nullptr); | ||
node::RemoveEnvironmentCleanupHook(std::move(must_not_call_handle)); | ||
} | ||
|
||
NODE_MODULE_CONTEXT_AWARE(NODE_GYP_MODULE_NAME, Initialize) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
'targets': [ | ||
{ | ||
'target_name': 'binding', | ||
'sources': [ 'binding.cc' ], | ||
'includes': ['../common.gypi'], | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
const common = require('../../common'); | ||
const path = require('path'); | ||
const { Worker } = require('worker_threads'); | ||
const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`); | ||
|
||
const w = new Worker(`require(${JSON.stringify(binding)})`, { eval: true }); | ||
w.on('exit', common.mustCall(() => require(binding))); |
Oops, something went wrong.