@@ -5118,7 +5118,9 @@ The `context` given during the call to `napi_create_threadsafe_function()` can
5118
5118
be retrieved from any thread with a call to
5119
5119
`napi_get_threadsafe_function_context()`.
5120
5120
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
5122
5124
JavaScript. `napi_call_threadsafe_function()` accepts a parameter which controls
5123
5125
whether the API behaves blockingly. If set to `napi_tsfn_nonblocking`, the API
5124
5126
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.
5150
5152
It is not necessary to call into JavaScript via `napi_make_callback()` because
5151
5153
N-API runs `call_js_cb` in a context appropriate for callbacks.
5152
5154
5155
+ ### Reference counting of thread-safe functions
5156
+
5153
5157
Threads can be added to and removed from a `napi_threadsafe_function` object
5154
5158
during its existence. Thus, in addition to specifying an initial number of
5155
5159
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
5169
5173
after receiving a return value of `napi_closing` in response to a call to
5170
5174
`napi_call_threadsafe_function`. Data associated with the
5171
5175
`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.
5173
5180
5174
5181
Once the number of threads making use of a `napi_threadsafe_function` reaches
5175
5182
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
5189
5196
further use of the thread-safe function because it is no longer guaranteed to
5190
5197
be allocated.**
5191
5198
5199
+ ### Deciding whether to keep the process running
5200
+
5192
5201
Similarly to libuv handles, thread-safe functions can be "referenced" and
5193
5202
"unreferenced". A "referenced" thread-safe function will cause the event loop on
5194
5203
the thread on which it is created to remain alive until the thread-safe function
5195
5204
is destroyed. In contrast, an "unreferenced" thread-safe function will not
5196
5205
prevent the event loop from exiting. The APIs `napi_ref_threadsafe_function` and
5197
5206
`napi_unref_threadsafe_function` exist for this purpose.
5198
5207
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
+
5199
5212
### napi_create_threadsafe_function
5200
5213
5201
5214
<!-- YAML
@@ -5233,8 +5246,9 @@ napi_create_threadsafe_function(napi_env env,
5233
5246
the kind of resource that is being provided for diagnostic information exposed
5234
5247
by the `async_hooks` API.
5235
5248
* `[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.
5238
5252
* `[in] thread_finalize_data`: Optional data to be passed to `thread_finalize_cb`.
5239
5253
* `[in] thread_finalize_cb`: Optional function to call when the
5240
5254
`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
5376
5390
should not exit until `func` has been destroyed. Similar to [`uv_ref`][] it is
5377
5391
also idempotent.
5378
5392
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
+
5379
5398
This API may only be called from the main thread.
5380
5399
5381
5400
### napi_unref_threadsafe_function
0 commit comments