You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Context parameter on thread safe function callback #653
Currently Napi::ThreadSafeFunction accepts a type implementing void operator()(Napi::Env env, Function jsCallback, DataType* data). It would be hard to retrieve the initial context parameter on Napi::ThreadSafeFunction::New since there is only one parameter DataType* data or we have to wrap the data with a reference to context again.
The context is not wrapped, so one could use ThreadSafeFunction::GetContext() but it is an additional call that shouldn't be necessary.
I've considered this option too. It seems on the user callback we don't have the reference to call ThreadSafeFunction::GetContext() if we don't wrap it in the data. So the option still relies on wrapping around data.
Sooo I have been working on this for some time now... After struggling with templated std::functions for hours, I really see two ways of implementing:
Allow the existing [Non]BlockingCall()s to take a three-argument callback, with the context being void*.
Create a new function [Non]BlockingCallEx which requires a three argument callback. I think I can make the context argument templated T *... but since the context is stored on the underlying napi_threadsafe_function and not the call itself, the node-addon-api wrapper to run the callback void(Env, Function, T*) would perform a cast to T* on the context, since it's void*.
I dunno, I am leaning towards option 1... i'm sure there are other options but there was no way I could figure out how to "combine" the two options (modify the existing callbacks to allow templated argument third T* context)...
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.
Activity
legendecas commentedon Jan 18, 2020
As a reference
napi_threadsafe_function_call_jshas a signature ofKevinEady commentedon Jan 20, 2020
Looking at the signature for our
CallJSwrapper passed tonapi_create_threadsafe_function:node-addon-api/napi-inl.h
Lines 4572 to 4575 in 4e88506
... we completely ignore the
contextvalue.The context is not wrapped, so one could use
ThreadSafeFunction::GetContext()but it is an additional call that shouldn't be necessary.legendecas commentedon Jan 20, 2020
I've considered this option too. It seems on the user callback we don't have the reference to call
ThreadSafeFunction::GetContext()if we don't wrap it in the data. So the option still relies on wrapping around data.gabrielschulhof commentedon Feb 3, 2020
From #594 (comment) it looks like we may need to substantially revamp
Napi::ThreadSafeFunction. When we do so, we should also address this issue.KevinEady commentedon Feb 16, 2020
Sooo I have been working on this for some time now... After struggling with templated std::functions for hours, I really see two ways of implementing:
[Non]BlockingCall()s to take a three-argument callback, with the context beingvoid*.[Non]BlockingCallExwhich requires a three argument callback. I think I can make the context argument templatedT *... but since the context is stored on the underlyingnapi_threadsafe_functionand not the call itself, the node-addon-api wrapper to run the callbackvoid(Env, Function, T*)would perform a cast toT*on the context, since it's void*.I dunno, I am leaning towards option 1... i'm sure there are other options but there was no way I could figure out how to "combine" the two options (modify the existing callbacks to allow templated argument third
T* context)...Thoughts?
cc: @gabrielschulhof @legendecas
github-actions commentedon Dec 16, 2020
This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.