@@ -5095,7 +5095,9 @@ The `context` given during the call to `napi_create_threadsafe_function()` can
5095
5095
be retrieved from any thread with a call to
5096
5096
`napi_get_threadsafe_function_context()`.
5097
5097
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
5099
5101
JavaScript. `napi_call_threadsafe_function()` accepts a parameter which controls
5100
5102
whether the API behaves blockingly. If set to `napi_tsfn_nonblocking`, the API
5101
5103
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.
5123
5125
It is not necessary to call into JavaScript via `napi_make_callback()` because
5124
5126
N-API runs `call_js_cb` in a context appropriate for callbacks.
5125
5127
5128
+ ### Reference counting of thread-safe functions
5129
+
5126
5130
Threads can be added to and removed from a `napi_threadsafe_function` object
5127
5131
during its existence. Thus, in addition to specifying an initial number of
5128
5132
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
5142
5146
after receiving a return value of `napi_closing` in response to a call to
5143
5147
`napi_call_threadsafe_function`. Data associated with the
5144
5148
`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.
5146
5153
5147
5154
Once the number of threads making use of a `napi_threadsafe_function` reaches
5148
5155
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
5162
5169
further use of the thread-safe function because it is no longer guaranteed to
5163
5170
be allocated.**
5164
5171
5172
+ ### Deciding whether to keep the process running
5173
+
5165
5174
Similarly to libuv handles, thread-safe functions can be "referenced" and
5166
5175
"unreferenced". A "referenced" thread-safe function will cause the event loop on
5167
5176
the thread on which it is created to remain alive until the thread-safe function
5168
5177
is destroyed. In contrast, an "unreferenced" thread-safe function will not
5169
5178
prevent the event loop from exiting. The APIs `napi_ref_threadsafe_function` and
5170
5179
`napi_unref_threadsafe_function` exist for this purpose.
5171
5180
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
+
5172
5185
### napi_create_threadsafe_function
5173
5186
5174
5187
<!-- YAML
@@ -5204,8 +5217,9 @@ napi_create_threadsafe_function(napi_env env,
5204
5217
the kind of resource that is being provided for diagnostic information exposed
5205
5218
by the `async_hooks` API.
5206
5219
* `[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.
5209
5223
* `[in] thread_finalize_data`: Optional data to be passed to `thread_finalize_cb`.
5210
5224
* `[in] thread_finalize_cb`: Optional function to call when the
5211
5225
`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
5333
5347
should not exit until `func` has been destroyed. Similar to [`uv_ref`][] it is
5334
5348
also idempotent.
5335
5349
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
+
5336
5355
This API may only be called from the main thread.
5337
5356
5338
5357
### napi_unref_threadsafe_function
0 commit comments