Today each EventQueue has its own napi_threadsafe_function to send tasks to the Node event loop. However, nodejs/node#38506 (by @indutny-signal) makes it faster to run multiple calls on the same napi_threadsafe_function than to hop between them. If Neon saves a single napi_threadsafe_function in its instance data, it can be shared among all EventQueues for a particular instance.
One complication with this is the reference/unref/has_ref API on EventQueue, which uses the lifetime of the napi_threadsafe_function to keep the main event loop alive. This would have to be implemented with refcounting alongside the single napi_threadsafe_function:
- The napi_threadsafe_function starts as unref with a refcount of 0.
- Creating an EventQueue adds one to the refcount.
- Going from 0 to 1 calls
napi_ref_threadsafe_function.
reference and unref modify the refcount only if they change the local has_ref flag.
- Going from 1 to 0 calls
napi_unref_threadsafe_function.