Skip to content

Commit ee36c87

Browse files
legendecascodebytere
authored andcommitted
doc: clarify thread-safe function references
PR-URL: #33871 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
1 parent 30b5e76 commit ee36c87

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

doc/api/n-api.md

+23-4
Original file line numberDiff line numberDiff line change
@@ -5095,7 +5095,9 @@ The `context` given during the call to `napi_create_threadsafe_function()` can
50955095
be retrieved from any thread with a call to
50965096
`napi_get_threadsafe_function_context()`.
50975097

5098-
`napi_call_threadsafe_function()` can then be used for initiating a call into
5098+
### Calling a thread-safe function
5099+
5100+
`napi_call_threadsafe_function()` can be used for initiating a call into
50995101
JavaScript. `napi_call_threadsafe_function()` accepts a parameter which controls
51005102
whether the API behaves blockingly. If set to `napi_tsfn_nonblocking`, the API
51015103
behaves non-blockingly, returning `napi_queue_full` if the queue was full,
@@ -5123,6 +5125,8 @@ Node.js process exits while there is a thread-safe function still active.
51235125
It is not necessary to call into JavaScript via `napi_make_callback()` because
51245126
N-API runs `call_js_cb` in a context appropriate for callbacks.
51255127

5128+
### Reference counting of thread-safe functions
5129+
51265130
Threads can be added to and removed from a `napi_threadsafe_function` object
51275131
during its existence. Thus, in addition to specifying an initial number of
51285132
threads upon creation, `napi_acquire_threadsafe_function` can be called to
@@ -5142,7 +5146,10 @@ reason, do not make use of a thread-safe function
51425146
after receiving a return value of `napi_closing` in response to a call to
51435147
`napi_call_threadsafe_function`. Data associated with the
51445148
`napi_threadsafe_function` can be freed in its `napi_finalize` callback which
5145-
was passed to `napi_create_threadsafe_function()`.
5149+
was passed to `napi_create_threadsafe_function()`. The parameter
5150+
`initial_thread_count` of `napi_create_threadsafe_function` marks the initial
5151+
number of aquisitions of the thread-safe functions, instead of calling
5152+
`napi_acquire_threadsafe_function` multiple times at creation.
51465153

51475154
Once the number of threads making use of a `napi_threadsafe_function` reaches
51485155
zero, no further threads can start making use of it by calling
@@ -5162,13 +5169,19 @@ of `napi_closing` from `napi_call_threadsafe_function()` a thread must make no
51625169
further use of the thread-safe function because it is no longer guaranteed to
51635170
be allocated.**
51645171

5172+
### Deciding whether to keep the process running
5173+
51655174
Similarly to libuv handles, thread-safe functions can be "referenced" and
51665175
"unreferenced". A "referenced" thread-safe function will cause the event loop on
51675176
the thread on which it is created to remain alive until the thread-safe function
51685177
is destroyed. In contrast, an "unreferenced" thread-safe function will not
51695178
prevent the event loop from exiting. The APIs `napi_ref_threadsafe_function` and
51705179
`napi_unref_threadsafe_function` exist for this purpose.
51715180

5181+
Neither does `napi_unref_threadsafe_function` mark the thread-safe functions as
5182+
able to be destroyed nor does `napi_ref_threadsafe_function` prevent it from
5183+
being destroyed.
5184+
51725185
### napi_create_threadsafe_function
51735186

51745187
<!-- YAML
@@ -5204,8 +5217,9 @@ napi_create_threadsafe_function(napi_env env,
52045217
the kind of resource that is being provided for diagnostic information exposed
52055218
by the `async_hooks` API.
52065219
* `[in] max_queue_size`: Maximum size of the queue. `0` for no limit.
5207-
* `[in] initial_thread_count`: The initial number of threads, including the main
5208-
thread, which will be making use of this function.
5220+
* `[in] initial_thread_count`: The initial number of acquisitions, i.e. the
5221+
initial number of threads, including the main thread, which will be making use
5222+
of this function.
52095223
* `[in] thread_finalize_data`: Optional data to be passed to `thread_finalize_cb`.
52105224
* `[in] thread_finalize_cb`: Optional function to call when the
52115225
`napi_threadsafe_function` is being destroyed.
@@ -5333,6 +5347,11 @@ This API is used to indicate that the event loop running on the main thread
53335347
should not exit until `func` has been destroyed. Similar to [`uv_ref`][] it is
53345348
also idempotent.
53355349

5350+
Neither does `napi_unref_threadsafe_function` mark the thread-safe functions as
5351+
able to be destroyed nor does `napi_ref_threadsafe_function` prevent it from
5352+
being destroyed. `napi_acquire_threadsafe_function` and
5353+
`napi_release_threadsafe_function` are available for that purpose.
5354+
53365355
This API may only be called from the main thread.
53375356

53385357
### napi_unref_threadsafe_function

0 commit comments

Comments
 (0)