Skip to content

Commit ec07e61

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 c9489f2 commit ec07e61

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
@@ -5118,7 +5118,9 @@ The `context` given during the call to `napi_create_threadsafe_function()` can
51185118
be retrieved from any thread with a call to
51195119
`napi_get_threadsafe_function_context()`.
51205120

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

5155+
### Reference counting of thread-safe functions
5156+
51535157
Threads can be added to and removed from a `napi_threadsafe_function` object
51545158
during its existence. Thus, in addition to specifying an initial number of
51555159
threads upon creation, `napi_acquire_threadsafe_function` can be called to
@@ -5169,7 +5173,10 @@ reason, do not make use of a thread-safe function
51695173
after receiving a return value of `napi_closing` in response to a call to
51705174
`napi_call_threadsafe_function`. Data associated with the
51715175
`napi_threadsafe_function` can be freed in its `napi_finalize` callback which
5172-
was passed to `napi_create_threadsafe_function()`.
5176+
was passed to `napi_create_threadsafe_function()`. The parameter
5177+
`initial_thread_count` of `napi_create_threadsafe_function` marks the initial
5178+
number of aquisitions of the thread-safe functions, instead of calling
5179+
`napi_acquire_threadsafe_function` multiple times at creation.
51735180

51745181
Once the number of threads making use of a `napi_threadsafe_function` reaches
51755182
zero, no further threads can start making use of it by calling
@@ -5189,13 +5196,19 @@ of `napi_closing` from `napi_call_threadsafe_function()` a thread must make no
51895196
further use of the thread-safe function because it is no longer guaranteed to
51905197
be allocated.**
51915198

5199+
### Deciding whether to keep the process running
5200+
51925201
Similarly to libuv handles, thread-safe functions can be "referenced" and
51935202
"unreferenced". A "referenced" thread-safe function will cause the event loop on
51945203
the thread on which it is created to remain alive until the thread-safe function
51955204
is destroyed. In contrast, an "unreferenced" thread-safe function will not
51965205
prevent the event loop from exiting. The APIs `napi_ref_threadsafe_function` and
51975206
`napi_unref_threadsafe_function` exist for this purpose.
51985207

5208+
Neither does `napi_unref_threadsafe_function` mark the thread-safe functions as
5209+
able to be destroyed nor does `napi_ref_threadsafe_function` prevent it from
5210+
being destroyed.
5211+
51995212
### napi_create_threadsafe_function
52005213

52015214
<!-- YAML
@@ -5233,8 +5246,9 @@ napi_create_threadsafe_function(napi_env env,
52335246
the kind of resource that is being provided for diagnostic information exposed
52345247
by the `async_hooks` API.
52355248
* `[in] max_queue_size`: Maximum size of the queue. `0` for no limit.
5236-
* `[in] initial_thread_count`: The initial number of threads, including the main
5237-
thread, which will be making use of this function.
5249+
* `[in] initial_thread_count`: The initial number of acquisitions, i.e. the
5250+
initial number of threads, including the main thread, which will be making use
5251+
of this function.
52385252
* `[in] thread_finalize_data`: Optional data to be passed to `thread_finalize_cb`.
52395253
* `[in] thread_finalize_cb`: Optional function to call when the
52405254
`napi_threadsafe_function` is being destroyed.
@@ -5376,6 +5390,11 @@ This API is used to indicate that the event loop running on the main thread
53765390
should not exit until `func` has been destroyed. Similar to [`uv_ref`][] it is
53775391
also idempotent.
53785392

5393+
Neither does `napi_unref_threadsafe_function` mark the thread-safe functions as
5394+
able to be destroyed nor does `napi_ref_threadsafe_function` prevent it from
5395+
being destroyed. `napi_acquire_threadsafe_function` and
5396+
`napi_release_threadsafe_function` are available for that purpose.
5397+
53795398
This API may only be called from the main thread.
53805399

53815400
### napi_unref_threadsafe_function

0 commit comments

Comments
 (0)