I have a C library. When say_hello() in the library is invoked, it spawns a thread t1 to compute some task T on it and immediately returns from the called function. After the task T has been completed, the callback function received as input to say_hello() is called.
I am using N-API so that the function say_hello() can be used by JS via a napi layer. Since, the thread t1 will call a function in the napi layer as callback, I have to use threadsafe function to transfer control to the main thread so I can call JS functions.
- I understood that
async_work provided by N-API cannot be used without modifying the C library that I have because threads are created in the C layer and not in the napi layer. Is that correct?
My JS code calls napi_init_app() which creates a threadsafe function. Then napi_say_hello() is called from JS which in turn calls say_hello() and returns.
A napi_ref to the JS callback function js_hello_callback() which was passed as parameter to napi_say_hello() is passed down to the C layer so that it can be retrieved back when the napi_hello_callback() is called by the thread t1.
napi_hello_callback() calls the API napi_call_threadsafe_function() and passes the napi_ref to js_hello_callback() along. call_js_cb() is then invoked which retrieves js_hello_callback() as napi_value from napi_ref and invokes it.
I do not do napi_acquire_threadsafe_function or napi_release_threadsafe_function for every call to the threadsafe function and only call napi_release_threadsafe_function when JS layer calls napi_destroy_app().
I have the following queries regarding the threadsafe APIs:
func (The JavaScript function to call from another thread) - This is a mandatory parameter but I cannot find a use case for this in the above scenario because napi_say_hello() can be called each time with a different callback. Am I missing something or should this be optional?
initial_thread_count - The number of times napi_say_hello() will be called or the number of threads which will call it is unknown to me when I am creating the threadsafe function. So, I am passing 1 as the value and neither acquiring or releasing before napi_destroy_app() is called. Why do we actually need this count?
- context - Also, this could be of more use if we could store different context for each call. Could you please provide some example for utilising this parameter.
I have the following queries regarding my approach:
- Is creating one threadsafe function for each function similar to
say_hello(), the right approach?
- Am I creating and releasing the threadsafe function at the right time?
- Could I use a better approach for my use case.
I have a C library. When
say_hello()in the library is invoked, it spawns a threadt1to compute some taskTon it and immediately returns from the called function. After the taskThas been completed, the callback function received as input tosay_hello()is called.I am using N-API so that the function
say_hello()can be used by JS via a napi layer. Since, the threadt1will call a function in the napi layer as callback, I have to use threadsafe function to transfer control to the main thread so I can call JS functions.async_workprovided by N-API cannot be used without modifying the C library that I have because threads are created in the C layer and not in the napi layer. Is that correct?My JS code calls
napi_init_app()which creates a threadsafe function. Thennapi_say_hello()is called from JS which in turn callssay_hello()and returns.A
napi_refto the JS callback functionjs_hello_callback()which was passed as parameter tonapi_say_hello()is passed down to the C layer so that it can be retrieved back when thenapi_hello_callback()is called by the threadt1.napi_hello_callback()calls the APInapi_call_threadsafe_function()and passes thenapi_reftojs_hello_callback()along.call_js_cb()is then invoked which retrievesjs_hello_callback()asnapi_valuefromnapi_refand invokes it.I do not do
napi_acquire_threadsafe_functionornapi_release_threadsafe_functionfor every call to the threadsafe function and only callnapi_release_threadsafe_functionwhen JS layer callsnapi_destroy_app().I have the following queries regarding the threadsafe APIs:
func(The JavaScript function to call from another thread) - This is a mandatory parameter but I cannot find a use case for this in the above scenario becausenapi_say_hello()can be called each time with a different callback. Am I missing something or should this be optional?initial_thread_count- The number of timesnapi_say_hello()will be called or the number of threads which will call it is unknown to me when I am creating the threadsafe function. So, I am passing1as the value and neither acquiring or releasing beforenapi_destroy_app()is called. Why do we actually need this count?I have the following queries regarding my approach:
say_hello(), the right approach?