Skip to content

Commit 759a018

Browse files
Gabriel Schulhofrvagg
Gabriel Schulhof
authored andcommitted
n-api: add API for asynchronous functions
Bundle a `uv_async_t`, a `uv_idle_t`, a `uv_mutex_t`, a `uv_cond_t`, and a `v8::Persistent<v8::Function>` to make it possible to call into JS from another thread. The API accepts a void data pointer and a callback which will be invoked on the loop thread and which will receive the `napi_value` representing the JavaScript function to call so as to perform the call into JS. The callback is run inside a `node::CallbackScope`. A `std::queue<void*>` is used to store calls from the secondary threads, and an idle loop is started by the `uv_async_t` callback on the loop thread to drain the queue, calling into JS with each item. Items can be added to the queue blockingly or non-blockingly. The thread-safe function can be referenced or unreferenced, with the same semantics as libuv handles. Re: nodejs/help#1035 Re: #20964 Fixes: #13512 Backport-PR-URL: #25002 PR-URL: #17887 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent fbafe8d commit 759a018

File tree

8 files changed

+1330
-4
lines changed

8 files changed

+1330
-4
lines changed

doc/api/errors.md

+25
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,31 @@ multiple of the element size.
11071107
While calling `napi_create_typedarray()`, `(length * size_of_element) +
11081108
byte_offset` was larger than the length of given `buffer`.
11091109

1110+
<a id="ERR_NAPI_TSFN_CALL_JS"></a>
1111+
### ERR_NAPI_TSFN_CALL_JS
1112+
1113+
An error occurred while invoking the JavaScript portion of the thread-safe
1114+
function.
1115+
1116+
<a id="ERR_NAPI_TSFN_GET_UNDEFINED"></a>
1117+
### ERR_NAPI_TSFN_GET_UNDEFINED
1118+
1119+
An error occurred while attempting to retrieve the JavaScript `undefined`
1120+
value.
1121+
1122+
<a id="ERR_NAPI_TSFN_START_IDLE_LOOP"></a>
1123+
### ERR_NAPI_TSFN_START_IDLE_LOOP
1124+
1125+
On the main thread, values are removed from the queue associated with the
1126+
thread-safe function in an idle loop. This error indicates that an error
1127+
has occurred when attemping to start the loop.
1128+
1129+
<a id="ERR_NAPI_TSFN_STOP_IDLE_LOOP"></a>
1130+
### ERR_NAPI_TSFN_STOP_IDLE_LOOP
1131+
1132+
Once no more items are left in the queue, the idle loop must be suspended. This
1133+
error indicates that the idle loop has failed to stop.
1134+
11101135
<a id="ERR_NO_ICU"></a>
11111136
### ERR_NO_ICU
11121137

doc/api/n-api.md

+371-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)