diff options
author | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2020-08-17 20:13:00 +0300 |
---|---|---|
committer | Gabriel Schulhof <gabriel.schulhof@intel.com> | 2020-08-27 18:18:37 +0300 |
commit | 0848f56cb39432090cdb99af9b8541fbc1a2849c (patch) | |
tree | 12f0089eba17afcaa39a03de6482dd1a221e3b2e /doc/api/n-api.md | |
parent | 3089f96ed01653aa4d5e4bba5a5db473ffe788a0 (diff) |
n-api: re-implement async env cleanup hooks
* Avoid passing core `void*` and function pointers into add-on.
* Document `napi_async_cleanup_hook_handle` type.
* Render receipt of the handle mandatory from the point where the
hook gets called. Removal of the handle remains mandatory.
Fixes: https://github.com/nodejs/node/issues/34715
Signed-off-by: Gabriel Schulhof <gabriel.schulhof@intel.com>
Co-authored-by: Anna Henningsen <github@addaleax.net>
PR-URL: https://github.com/nodejs/node/pull/34819
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Diffstat (limited to 'doc/api/n-api.md')
-rw-r--r-- | doc/api/n-api.md | 65 |
1 files changed, 58 insertions, 7 deletions
diff --git a/doc/api/n-api.md b/doc/api/n-api.md index dd4f1329b4e..91341a2f6c5 100644 --- a/doc/api/n-api.md +++ b/doc/api/n-api.md @@ -623,6 +623,15 @@ typedef struct { } napi_type_tag; ``` +#### napi_async_cleanup_hook_handle +<!-- YAML +added: REPLACEME +--> + +An opaque value returned by [`napi_add_async_cleanup_hook`][]. It must be passed +to [`napi_remove_async_cleanup_hook`][] when the chain of asynchronous cleanup +events completes. + ### N-API callback types #### napi_callback_info @@ -751,6 +760,30 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env, Unless for reasons discussed in [Object Lifetime Management][], creating a handle and/or callback scope inside the function body is not necessary. +#### napi_async_cleanup_hook +<!-- YAML +added: REPLACEME +--> + +Function pointer used with [`napi_add_async_cleanup_hook`][]. It will be called +when the environment is being torn down. + +Callback functions must satisfy the following signature: + +```c +typedef void (*napi_async_cleanup_hook)(napi_async_cleanup_hook_handle handle, + void* data); +``` + +* `[in] handle`: The handle that must be passed to +[`napi_remove_async_cleanup_hook`][] after completion of the asynchronous +cleanup. +* `[in] data`: The data that was passed to [`napi_add_async_cleanup_hook`][]. + +The body of the function should initiate the asynchronous cleanup actions at the +end of which `handle` must be passed in a call to +[`napi_remove_async_cleanup_hook`][]. + ## Error handling N-API uses both return values and JavaScript exceptions for error handling. @@ -1580,6 +1613,10 @@ with `napi_add_env_cleanup_hook`, otherwise the process will abort. #### napi_add_async_cleanup_hook <!-- YAML added: v14.8.0 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/34819 + description: Changed signature of the `hook` callback. --> > Stability: 1 - Experimental @@ -1587,15 +1624,22 @@ added: v14.8.0 ```c NAPI_EXTERN napi_status napi_add_async_cleanup_hook( napi_env env, - void (*fun)(void* arg, void(* cb)(void*), void* cbarg), + napi_async_cleanup_hook hook, void* arg, napi_async_cleanup_hook_handle* remove_handle); ``` -Registers `fun` as a function to be run with the `arg` parameter once the -current Node.js environment exits. Unlike [`napi_add_env_cleanup_hook`][], -the hook is allowed to be asynchronous in this case, and must invoke the passed -`cb()` function with `cbarg` once all asynchronous activity is finished. +* `[in] env`: The environment that the API is invoked under. +* `[in] hook`: The function pointer to call at environment teardown. +* `[in] arg`: The pointer to pass to `hook` when it gets called. +* `[out] remove_handle`: Optional handle that refers to the asynchronous cleanup +hook. + +Registers `hook`, which is a function of type [`napi_async_cleanup_hook`][], as +a function to be run with the `remove_handle` and `arg` parameters once the +current Node.js environment exits. + +Unlike [`napi_add_env_cleanup_hook`][], the hook is allowed to be asynchronous. Otherwise, behavior generally matches that of [`napi_add_env_cleanup_hook`][]. @@ -1608,19 +1652,25 @@ is being torn down anyway. #### napi_remove_async_cleanup_hook <!-- YAML added: v14.8.0 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/34819 + description: Removed `env` parameter. --> > Stability: 1 - Experimental ```c NAPI_EXTERN napi_status napi_remove_async_cleanup_hook( - napi_env env, napi_async_cleanup_hook_handle remove_handle); ``` +* `[in] remove_handle`: The handle to an asynchronous cleanup hook that was +created with [`napi_add_async_cleanup_hook`][]. + Unregisters the cleanup hook corresponding to `remove_handle`. This will prevent the hook from being executed, unless it has already started executing. -This must be called on any `napi_async_cleanup_hook_handle` value retrieved +This must be called on any `napi_async_cleanup_hook_handle` value obtained from [`napi_add_async_cleanup_hook`][]. ## Module registration @@ -5763,6 +5813,7 @@ This API may only be called from the main thread. [`napi_add_async_cleanup_hook`]: #n_api_napi_add_async_cleanup_hook [`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook [`napi_add_finalizer`]: #n_api_napi_add_finalizer +[`napi_async_cleanup_hook`]: #n_api_napi_async_cleanup_hook [`napi_async_complete_callback`]: #n_api_napi_async_complete_callback [`napi_async_init`]: #n_api_napi_async_init [`napi_callback`]: #n_api_napi_callback |