Skip to content

Commit 81f06ba

Browse files
author
Gabriel Schulhof
committed
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 <matteo.collina@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 1c30343 commit 81f06ba

File tree

8 files changed

+1329
-4
lines changed

8 files changed

+1329
-4
lines changed

doc/api/errors.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,31 @@ multiple of the element size.
13761376
While calling `napi_create_typedarray()`, `(length * size_of_element) +
13771377
byte_offset` was larger than the length of given `buffer`.
13781378

1379+
<a id="ERR_NAPI_TSFN_CALL_JS"></a>
1380+
### ERR_NAPI_TSFN_CALL_JS
1381+
1382+
An error occurred while invoking the JavaScript portion of the thread-safe
1383+
function.
1384+
1385+
<a id="ERR_NAPI_TSFN_GET_UNDEFINED"></a>
1386+
### ERR_NAPI_TSFN_GET_UNDEFINED
1387+
1388+
An error occurred while attempting to retrieve the JavaScript `undefined`
1389+
value.
1390+
1391+
<a id="ERR_NAPI_TSFN_START_IDLE_LOOP"></a>
1392+
### ERR_NAPI_TSFN_START_IDLE_LOOP
1393+
1394+
On the main thread, values are removed from the queue associated with the
1395+
thread-safe function in an idle loop. This error indicates that an error
1396+
has occurred when attemping to start the loop.
1397+
1398+
<a id="ERR_NAPI_TSFN_STOP_IDLE_LOOP"></a>
1399+
### ERR_NAPI_TSFN_STOP_IDLE_LOOP
1400+
1401+
Once no more items are left in the queue, the idle loop must be suspended. This
1402+
error indicates that the idle loop has failed to stop.
1403+
13791404
<a id="ERR_NO_CRYPTO"></a>
13801405
### ERR_NO_CRYPTO
13811406

doc/api/n-api.md

Lines changed: 373 additions & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)