1- # ThreadSafeFunctionEx
2-
3- The ` Napi::ThreadSafeFunctionEx ` type provides APIs for threads to communicate
4- with the addon's main thread to invoke JavaScript functions on their behalf. The
5- type is a three-argument templated class, each argument representing the type
6- of:
7- - ` ContextType = std::nullptr_t ` : The thread-safe function's context. By default,
8- a TSFN has no context.
1+ # TypedThreadSafeFunction
2+
3+ The ` Napi::TypedThreadSafeFunction ` type provides APIs for threads to
4+ communicate with the addon's main thread to invoke JavaScript functions on their
5+ behalf. The type is a three-argument templated class, each argument representing
6+ the type of:
7+ - ` ContextType = std::nullptr_t ` : The thread-safe function's context. By
8+ default, a TSFN has no context.
99- ` DataType = void* ` : The data to use in the native callback. By default, a TSFN
1010 can accept any data type.
1111- `Callback = void(* )(Napi::Env, Napi::Function jsCallback, ContextType* ,
@@ -21,31 +21,31 @@ APIs](threadsafe.md#implementation-differences).
2121
2222### Constructor
2323
24- Creates a new empty instance of ` Napi::ThreadSafeFunctionEx ` .
24+ Creates a new empty instance of ` Napi::TypedThreadSafeFunction ` .
2525
2626``` cpp
27- Napi::Function::ThreadSafeFunctionEx <ContextType, DataType, Callback>::ThreadSafeFunctionEx ();
27+ Napi::Function::TypedThreadSafeFunction <ContextType, DataType, Callback>::TypedThreadSafeFunction ();
2828```
2929
3030### Constructor
3131
32- Creates a new instance of the ` Napi::ThreadSafeFunctionEx ` object.
32+ Creates a new instance of the ` Napi::TypedThreadSafeFunction ` object.
3333
3434``` cpp
35- Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::ThreadSafeFunctionEx (napi_threadsafe_function tsfn);
35+ Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::TypedThreadSafeFunction (napi_threadsafe_function tsfn);
3636```
3737
3838- ` tsfn ` : The ` napi_threadsafe_function ` which is a handle for an existing
3939 thread-safe function.
4040
41- Returns a non-empty ` Napi::ThreadSafeFunctionEx ` instance. To ensure the API
41+ Returns a non-empty ` Napi::TypedThreadSafeFunction ` instance. To ensure the API
4242statically handles the correct return type for ` GetContext() ` and
4343` [Non]BlockingCall() ` , pass the proper template arguments to
44- ` Napi::ThreadSafeFunctionEx ` .
44+ ` Napi::TypedThreadSafeFunction ` .
4545
4646### New
4747
48- Creates a new instance of the ` Napi::ThreadSafeFunctionEx ` object. The ` New `
48+ Creates a new instance of the ` Napi::TypedThreadSafeFunction ` object. The ` New `
4949function has several overloads for the various optional parameters: skip the
5050optional parameter for that specific overload.
5151
@@ -72,11 +72,11 @@ New(napi_env env,
7272- `maxQueueSize`: Maximum size of the queue. `0` for no limit.
7373- `initialThreadCount`: The initial number of threads, including the main
7474 thread, which will be making use of this function.
75- - `[optional] context`: Data to attach to the resulting `ThreadSafeFunction`.
76- It can be retreived via `GetContext()`.
75+ - `[optional] context`: Data to attach to the resulting `ThreadSafeFunction`. It
76+ can be retreived via `GetContext()`.
7777- `[optional] finalizeCallback`: Function to call when the
78- `ThreadSafeFunctionEx ` is being destroyed. This callback will be invoked on
79- the main thread when the thread-safe function is about to be destroyed. It
78+ `TypedThreadSafeFunction ` is being destroyed. This callback will be invoked
79+ on the main thread when the thread-safe function is about to be destroyed. It
8080 receives the context and the finalize data given during construction (if
8181 given), and provides an opportunity for cleaning up after the threads e.g. by
8282 calling `uv_thread_join()`. It is important that, aside from the main loop
@@ -85,15 +85,15 @@ New(napi_env env,
8585 FinalizerDataType* data, ContextType* hint)`.
8686- `[optional] data`: Data to be passed to `finalizeCallback`.
8787
88- Returns a non-empty `Napi::ThreadSafeFunctionEx ` instance.
88+ Returns a non-empty `Napi::TypedThreadSafeFunction ` instance.
8989
9090Depending on the targetted `NAPI_VERSION`, the API has different implementations
9191for `CallbackType callback`.
9292
9393When targetting version 4, `callback` may be:
9494- of type `const Function&`
9595- not provided as a parameter, in which case the API creates a new no-op
96- `Function`
96+ `Function`
9797
9898When targetting version 5+, `callback` may be:
9999- of type `const Function&`
@@ -106,7 +106,7 @@ Adds a thread to this thread-safe function object, indicating that a new thread
106106will start making use of the thread-safe function.
107107
108108```cpp
109- napi_status Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::Acquire()
109+ napi_status Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::Acquire()
110110```
111111
112112Returns one of:
@@ -124,7 +124,7 @@ has undefined results in the current thread, as the thread-safe function may
124124have been destroyed.
125125
126126``` cpp
127- napi_status Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::Release()
127+ napi_status Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::Release()
128128```
129129
130130Returns one of:
@@ -135,18 +135,18 @@ Returns one of:
135135
136136### Abort
137137
138- "Aborts" the thread-safe function. This will cause all subsequent APIs associated
139- with the thread-safe function except ` Release() ` to return ` napi_closing ` even
140- before its reference count reaches zero. In particular, ` BlockingCall ` and
141- ` NonBlockingCall() ` will return ` napi_closing ` , thus informing the threads that
142- it is no longer possible to make asynchronous calls to the thread-safe function.
143- This can be used as a criterion for terminating the thread. Upon receiving a
144- return value of ` napi_closing ` from a thread-safe function call a thread must
145- make no further use of the thread-safe function because it is no longer
146- guaranteed to be allocated.
138+ "Aborts" the thread-safe function. This will cause all subsequent APIs
139+ associated with the thread-safe function except ` Release() ` to return
140+ ` napi_closing ` even before its reference count reaches zero. In particular,
141+ ` BlockingCall ` and ` NonBlockingCall() ` will return ` napi_closing ` , thus
142+ informing the threads that it is no longer possible to make asynchronous calls
143+ to the thread-safe function. This can be used as a criterion for terminating the
144+ thread. Upon receiving a return value of ` napi_closing ` from a thread-safe
145+ function call a thread must make no further use of the thread-safe function
146+ because it is no longer guaranteed to be allocated.
147147
148148``` cpp
149- napi_status Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::Abort()
149+ napi_status Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::Abort()
150150```
151151
152152Returns one of:
@@ -165,13 +165,13 @@ Calls the Javascript function in either a blocking or non-blocking fashion.
165165 preventing data from being successfully added to the queue.
166166
167167``` cpp
168- napi_status Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::BlockingCall(DataType* data = nullptr ) const
168+ napi_status Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::BlockingCall(DataType* data = nullptr ) const
169169
170- napi_status Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::NonBlockingCall(DataType* data = nullptr ) const
170+ napi_status Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::NonBlockingCall(DataType* data = nullptr ) const
171171```
172172
173173- ` [optional] data ` : Data to pass to the callback which was passed to
174- ` ThreadSafeFunctionEx ::New()` .
174+ ` TypedThreadSafeFunction ::New()` .
175175
176176Returns one of:
177177- ` napi_ok ` : ` data ` was successfully added to the queue.
@@ -196,7 +196,7 @@ using namespace Napi;
196196using Context = Reference<Value>;
197197using DataType = int ;
198198void CallJs (Napi::Env env, Function callback, Context * context, DataType * data);
199- using TSFN = ThreadSafeFunctionEx <Context, DataType, CallJs>;
199+ using TSFN = TypedThreadSafeFunction <Context, DataType, CallJs>;
200200using FinalizerDataType = void;
201201
202202std::thread nativeThread;
0 commit comments