Commit 91384bf
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 0d98fe6 commit 91384bfCopy full SHA for 91384bf
File tree
Expand file treeCollapse file tree
8 files changed
+1329
-4
lines changedOpen diff view settings
Filter options
- doc/api
- src
- test/addons-napi/test_threadsafe_function
Expand file treeCollapse file tree
8 files changed
+1329
-4
lines changedOpen diff view settings
Collapse file
+25Lines changed: 25 additions & 0 deletions
- Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| ||
1386 | 1386 | |
1387 | 1387 | |
1388 | 1388 | |
| 1389 | + |
| 1390 | + |
| 1391 | + |
| 1392 | + |
| 1393 | + |
| 1394 | + |
| 1395 | + |
| 1396 | + |
| 1397 | + |
| 1398 | + |
| 1399 | + |
| 1400 | + |
| 1401 | + |
| 1402 | + |
| 1403 | + |
| 1404 | + |
| 1405 | + |
| 1406 | + |
| 1407 | + |
| 1408 | + |
| 1409 | + |
| 1410 | + |
| 1411 | + |
| 1412 | + |
| 1413 | + |
1389 | 1414 | |
1390 | 1415 | |
1391 | 1416 | |
|
Collapse file
+373-1Lines changed: 373 additions & 1 deletion
- Display the source diff
- Display the rich diff
Large diffs are not rendered by default.
0 commit comments