From 8c8c90b714756433b3333146a2893572b473dfd6 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 8 Aug 2017 20:18:16 +0200 Subject: [PATCH] n-api: use AsyncResource for Work tracking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enable combining N-API async work with async-hooks. PR-URL: https://github.com/nodejs/node/pull/14697 Reviewed-By: Tobias Nießen Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Jason Ginchereau Reviewed-By: Michael Dawson --- doc/api/n-api.md | 20 +++++++ src/node_api.cc | 32 ++++++++-- src/node_api.h | 2 + .../test_async/test-async-hooks.js | 60 +++++++++++++++++++ test/addons-napi/test_async/test.js | 4 +- test/addons-napi/test_async/test_async.cc | 31 ++++++---- 6 files changed, 129 insertions(+), 20 deletions(-) create mode 100644 test/addons-napi/test_async/test-async-hooks.js diff --git a/doc/api/n-api.md b/doc/api/n-api.md index b9d06079fd..0a74d89ea0 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -3256,10 +3256,16 @@ callback invocation, even when it was cancelled. ### napi_create_async_work ```C NAPI_EXTERN napi_status napi_create_async_work(napi_env env, + napi_value async_resource, + const char* async_resource_name, napi_async_execute_callback execute, napi_async_complete_callback complete, void* data, @@ -3267,6 +3273,10 @@ napi_status napi_create_async_work(napi_env env, ``` - `[in] env`: The environment that the API is invoked under. +- `[in] async_resource`: An optional object associated with the async work + that will be passed to possible async_hooks [`init` hooks][]. +- `[in] async_resource_name`: An identifier for the kind of resource that is +being provided for diagnostic information exposed by the `async_hooks` API. - `[in] execute`: The native function which should be called to excute the logic asynchronously. - `[in] complete`: The native function which will be called when the @@ -3282,6 +3292,14 @@ This API allocates a work object that is used to execute logic asynchronously. It should be freed using [`napi_delete_async_work`][] once the work is no longer required. +`async_resource_name` should be a null-terminated, UTF-8-encoded string. + +*Note*: The `async_resource_name` identifier is provided by the user and should +be representative of the type of async work being performed. It is also +recommended to apply namespacing to the identifier, e.g. by including the +module name. See the [`async_hooks` documentation][async_hooks `type`] +for more information. + ### napi_delete_async_work