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.
9
9
- ` DataType = void* ` : The data to use in the native callback. By default, a TSFN
10
10
can accept any data type.
11
11
- `Callback = void(* )(Napi::Env, Napi::Function jsCallback, ContextType* ,
@@ -21,31 +21,31 @@ APIs](threadsafe.md#implementation-differences).
21
21
22
22
### Constructor
23
23
24
- Creates a new empty instance of ` Napi::ThreadSafeFunctionEx ` .
24
+ Creates a new empty instance of ` Napi::TypedThreadSafeFunction ` .
25
25
26
26
``` cpp
27
- Napi::Function::ThreadSafeFunctionEx <ContextType, DataType, Callback>::ThreadSafeFunctionEx ();
27
+ Napi::Function::TypedThreadSafeFunction <ContextType, DataType, Callback>::TypedThreadSafeFunction ();
28
28
```
29
29
30
30
### Constructor
31
31
32
- Creates a new instance of the ` Napi::ThreadSafeFunctionEx ` object.
32
+ Creates a new instance of the ` Napi::TypedThreadSafeFunction ` object.
33
33
34
34
``` cpp
35
- Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::ThreadSafeFunctionEx (napi_threadsafe_function tsfn);
35
+ Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::TypedThreadSafeFunction (napi_threadsafe_function tsfn);
36
36
```
37
37
38
38
- ` tsfn ` : The ` napi_threadsafe_function ` which is a handle for an existing
39
39
thread-safe function.
40
40
41
- Returns a non-empty ` Napi::ThreadSafeFunctionEx ` instance. To ensure the API
41
+ Returns a non-empty ` Napi::TypedThreadSafeFunction ` instance. To ensure the API
42
42
statically handles the correct return type for ` GetContext() ` and
43
43
` [Non]BlockingCall() ` , pass the proper template arguments to
44
- ` Napi::ThreadSafeFunctionEx ` .
44
+ ` Napi::TypedThreadSafeFunction ` .
45
45
46
46
### New
47
47
48
- Creates a new instance of the ` Napi::ThreadSafeFunctionEx ` object. The ` New `
48
+ Creates a new instance of the ` Napi::TypedThreadSafeFunction ` object. The ` New `
49
49
function has several overloads for the various optional parameters: skip the
50
50
optional parameter for that specific overload.
51
51
@@ -72,11 +72,11 @@ New(napi_env env,
72
72
- `maxQueueSize`: Maximum size of the queue. `0` for no limit.
73
73
- `initialThreadCount`: The initial number of threads, including the main
74
74
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()`.
77
77
- `[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
80
80
receives the context and the finalize data given during construction (if
81
81
given), and provides an opportunity for cleaning up after the threads e.g. by
82
82
calling `uv_thread_join()`. It is important that, aside from the main loop
@@ -85,15 +85,15 @@ New(napi_env env,
85
85
FinalizerDataType* data, ContextType* hint)`.
86
86
- `[optional] data`: Data to be passed to `finalizeCallback`.
87
87
88
- Returns a non-empty `Napi::ThreadSafeFunctionEx ` instance.
88
+ Returns a non-empty `Napi::TypedThreadSafeFunction ` instance.
89
89
90
90
Depending on the targetted `NAPI_VERSION`, the API has different implementations
91
91
for `CallbackType callback`.
92
92
93
93
When targetting version 4, `callback` may be:
94
94
- of type `const Function&`
95
95
- not provided as a parameter, in which case the API creates a new no-op
96
- `Function`
96
+ `Function`
97
97
98
98
When targetting version 5+, `callback` may be:
99
99
- of type `const Function&`
@@ -106,7 +106,7 @@ Adds a thread to this thread-safe function object, indicating that a new thread
106
106
will start making use of the thread-safe function.
107
107
108
108
```cpp
109
- napi_status Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::Acquire()
109
+ napi_status Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::Acquire()
110
110
```
111
111
112
112
Returns one of:
@@ -124,7 +124,7 @@ has undefined results in the current thread, as the thread-safe function may
124
124
have been destroyed.
125
125
126
126
``` cpp
127
- napi_status Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::Release()
127
+ napi_status Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::Release()
128
128
```
129
129
130
130
Returns one of:
@@ -135,18 +135,18 @@ Returns one of:
135
135
136
136
### Abort
137
137
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.
147
147
148
148
``` cpp
149
- napi_status Napi::ThreadSafeFunctionEx <ContextType, DataType, Callback>::Abort()
149
+ napi_status Napi::TypedThreadSafeFunction <ContextType, DataType, Callback>::Abort()
150
150
```
151
151
152
152
Returns one of:
@@ -165,13 +165,13 @@ Calls the Javascript function in either a blocking or non-blocking fashion.
165
165
preventing data from being successfully added to the queue.
166
166
167
167
``` 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
169
169
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
171
171
```
172
172
173
173
- ` [optional] data ` : Data to pass to the callback which was passed to
174
- ` ThreadSafeFunctionEx ::New()` .
174
+ ` TypedThreadSafeFunction ::New()` .
175
175
176
176
Returns one of:
177
177
- ` napi_ok ` : ` data ` was successfully added to the queue.
@@ -196,7 +196,7 @@ using namespace Napi;
196
196
using Context = Reference<Value>;
197
197
using DataType = int ;
198
198
void CallJs (Napi::Env env, Function callback, Context * context, DataType * data);
199
- using TSFN = ThreadSafeFunctionEx <Context, DataType, CallJs>;
199
+ using TSFN = TypedThreadSafeFunction <Context, DataType, CallJs>;
200
200
using FinalizerDataType = void;
201
201
202
202
std::thread nativeThread;
0 commit comments