Skip to content

Commit 91384bf

Browse files
Gabriel Schulhoftargos
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 PR-URL: #17887 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent 0d98fe6 commit 91384bf

File tree

8 files changed

+1329
-4
lines changed

8 files changed

+1329
-4
lines changed

doc/api/errors.md

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

1389+
<a id="ERR_NAPI_TSFN_CALL_JS"></a>
1390+
### ERR_NAPI_TSFN_CALL_JS
1391+
1392+
An error occurred while invoking the JavaScript portion of the thread-safe
1393+
function.
1394+
1395+
<a id="ERR_NAPI_TSFN_GET_UNDEFINED"></a>
1396+
### ERR_NAPI_TSFN_GET_UNDEFINED
1397+
1398+
An error occurred while attempting to retrieve the JavaScript `undefined`
1399+
value.
1400+
1401+
<a id="ERR_NAPI_TSFN_START_IDLE_LOOP"></a>
1402+
### ERR_NAPI_TSFN_START_IDLE_LOOP
1403+
1404+
On the main thread, values are removed from the queue associated with the
1405+
thread-safe function in an idle loop. This error indicates that an error
1406+
has occurred when attemping to start the loop.
1407+
1408+
<a id="ERR_NAPI_TSFN_STOP_IDLE_LOOP"></a>
1409+
### ERR_NAPI_TSFN_STOP_IDLE_LOOP
1410+
1411+
Once no more items are left in the queue, the idle loop must be suspended. This
1412+
error indicates that the idle loop has failed to stop.
1413+
13891414
<a id="ERR_NO_CRYPTO"></a>
13901415
### ERR_NO_CRYPTO
13911416

doc/api/n-api.md

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

0 commit comments

Comments
 (0)