Skip to content

Semantical difference between Napi::ThreadSafeFunction and napi_threadsafe_function #594

Description

@legendecas

Napi::ThreadSafeFunction now acts as a bridge between the JavaScript function callback and multiple runtime c++ function callbacks. That is to say, Napi::ThreadSafeFunction has built a 1:m relation on the JavaScript function and c++ function semantically. See call functions of Napi::ThreadSafeFunction and their variants:

napi_status Napi::ThreadSafeFunction::BlockingCall(DataType* data, Callback callback) const
napi_status Napi::ThreadSafeFunction::NonBlockingCall(DataType* data, Callback callback) const

With these signatures we could provide different c++ functions on each thread safe function call. It might be more convenient to be used. Yet while reviewing napi_threadsafe_function docs, we could find that napi_threadsafe_function has a 1:1 relation on JavaScript function and c++ function which is built on the creation function signature:

napi_status
napi_create_threadsafe_function(napi_env env,
                                napi_value func,
                                napi_value async_resource,
                                napi_value async_resource_name,
                                size_t max_queue_size,
                                size_t initial_thread_count,
                                void* thread_finalize_data,
                                napi_finalize thread_finalize_cb,
                                void* context,
                                napi_threadsafe_function_call_js call_js_cb,
                                napi_threadsafe_function* result);
napi_status
napi_call_threadsafe_function(napi_threadsafe_function func,
                              void* data,
                              napi_threadsafe_function_call_mode is_blocking);
/** no function provided on `napi_call_threadsafe_function` */

Either this behavior was intended or not, this may cause confusion on introducing Napi::ThreadSafeFunction to an existing N-API document reader. They may have the impression that a single thread safe function is going to be used for one purpose, and has 1:1 relation on JavaScript function and the c++ function.

Possible solutions (might not be the best solutions):

  • Add a caveat section on the doc of Napi::ThreadSafeFunction
  • Breaking change (Nah, nope, not this one)

Or it may not be a problem at all.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions