callAsync and Executorfn.callAsync(...args) runs the call on a libuv worker and returns a promise;
the event loop stays free meanwhile. Arguments are converted on the
JavaScript thread before the call, results (and errcheck) after it, and the
memory passed by address stays referenced until the promise settles.
Buffer, typed array, DataView and ArrayBuffer arguments are passed in
place, as in synchronous calls: do not detach, resize or modify them until
the promise settles, and C must not keep the pointer after it returns (see
Memory passed to asynchronous calls).
callAsync() and callOn() never throw synchronously: argument conversion
and arity errors, paramflags binding errors, a closed library and a
released callback all reject the returned promise.
libuv workers are shared with fs, dns and crypto (4 by default), and
concurrent calls run in parallel in any order. For long or blocking calls, or
calls that must run in sequence on one thread (thread-local state,
thread-affine handles, libraries that are not thread-safe), use an
Executor: a native thread owned by node-ctypes with a FIFO queue.
fn.callOn(executor, ...args) is the same call as callAsync, run there:
import { Executor } from "node-ctypes";
const executor = new Executor({ name: "device" }); // thread name for debuggers
const read = await lib.device_read.callOn(executor, handle, buffer, buffer.length);
// Cancel a call that has not started yet: the promise rejects with signal.reason.
const controller = new AbortController();
const next = lib.device_read.callOn({ executor, signal: controller.signal }, handle, buffer, buffer.length);
controller.abort();
await executor.close(); // waits for the accepted calls, then stops the thread
callAsync), so large structures passed by value, which
the AOT call trampoline copies onto the stack, work as on the JavaScript thread.close() rejects new calls, waits for the accepted ones
and stops the thread (an executor cannot be reopened).executor.pending, completed, running and closed report its state.spin (microseconds, default 50, 0 to disable): after a call the thread
polls for the next one this long (at most 50 µs) before sleeping, and the JavaScript thread
waits this long for a call submitted to an idle executor, completing it
without a round trip through the event loop. Awaited calls in sequence then
cost a few microseconds instead of tens; its errcheck may run before
callOn returns. The wait is per function: spin for a function not
called yet, then about one and a half times its measured typical duration,
up to spinLimit (microseconds, default 500); a function that usually takes
longer completes through the event loop without delaying the others. Inline
completions are limited to 1 ms per turn of the event loop, so timers, I/O
and UI events keep running.worker.terminate()) or any other
teardown of the environment, queued calls are discarded, but the running
call is always waited for: the native thread
is joined before the environment's memory, callbacks and libraries are
released. A call that never returns therefore makes terminate() block.
While it waits, a threadSafeCallback of the closing environment invoked by
the running call returns zero at once (see
Callbacks from other threads),
so the wait cannot deadlock on it.threadSafeCallback, as with callAsync.An Executor also suits many short calls kept in flight. Each callAsync()
queues its own libuv work item, which wakes a worker, and completes
separately; the executor thread takes the next queued call without being
woken and delivers the completed calls to JavaScript together. With several
calls in flight this usually costs less than callAsync(): about a third on
Windows, where waking a thread is expensive, less of a difference elsewhere.
The calls then run one at a time: for short calls that is usually faster than
spreading them over the libuv workers, but blocking calls that must overlap
still need callAsync() or one executor each.