Skip to content

Commit c24c455

Browse files
committed
Rename to TypedThreadSafeFunction
1 parent 4abe7cf commit c24c455

20 files changed

+196
-196
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ The following is the documentation for node-addon-api.
123123
- [AsyncWorker Variants](doc/async_worker_variants.md)
124124
- [Thread-safe Functions](doc/threadsafe.md)
125125
- [ThreadSafeFunction](doc/threadsafe_function.md)
126-
- [ThreadSafeFunctionEx](doc/threadsafe_function_ex.md)
126+
- [TypedThreadSafeFunction](doc/typed_threadsafe_function.md)
127127
- [Promises](doc/promises.md)
128128
- [Version management](doc/version_management.md)
129129

doc/threadsafe.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,40 @@ communicate with the addon's main thread so that the main thread can invoke the
1111
JavaScript function on their behalf. The thread-safe function APIs provide an
1212
easy way to do this. These APIs provide two types --
1313
[`Napi::ThreadSafeFunction`](threadsafe_function.md) and
14-
[`Napi::ThreadSafeFunctionEx`](threadsafe_function_ex.md) -- as well as APIs to
15-
create, destroy, and call objects of this type. The differences between the two
16-
are subtle and are [highlighted below](#implementation-differences). Regardless
17-
of which type you choose, the APIs between the two are similar.
14+
[`Napi::TypedThreadSafeFunction`](typed_threadsafe_function.md) -- as well as
15+
APIs to create, destroy, and call objects of this type. The differences between
16+
the two are subtle and are [highlighted below](#implementation-differences).
17+
Regardless of which type you choose, the APIs between the two are similar.
1818

19-
`Napi::ThreadSafeFunction[Ex]::New()` creates a persistent reference that holds
20-
a JavaScript function which can be called from multiple threads. The calls
19+
`Napi::[Typed]ThreadSafeFunction::New()` creates a persistent reference that
20+
holds a JavaScript function which can be called from multiple threads. The calls
2121
happen asynchronously. This means that values with which the JavaScript callback
2222
is to be called will be placed in a queue, and, for each value in the queue, a
2323
call will eventually be made to the JavaScript function.
2424

25-
`Napi::ThreadSafeFunction[Ex]` objects are destroyed when every thread which
25+
`Napi::[Typed]ThreadSafeFunction` objects are destroyed when every thread which
2626
uses the object has called `Release()` or has received a return status of
2727
`napi_closing` in response to a call to `BlockingCall()` or `NonBlockingCall()`.
28-
The queue is emptied before the `Napi::ThreadSafeFunction[Ex]` is destroyed. It
29-
is important that `Release()` be the last API call made in conjunction with a
30-
given `Napi::ThreadSafeFunction[Ex]`, because after the call completes, there is
31-
no guarantee that the `Napi::ThreadSafeFunction[Ex]` is still allocated. For the
32-
same reason it is also important that no more use be made of a thread-safe
33-
function after receiving a return value of `napi_closing` in response to a call
34-
to `BlockingCall()` or `NonBlockingCall()`. Data associated with the
35-
`Napi::ThreadSafeFunction[Ex]` can be freed in its `Finalizer` callback which
36-
was passed to `ThreadSafeFunction[Ex]::New()`.
37-
38-
Once the number of threads making use of a `Napi::ThreadSafeFunction[Ex]`
28+
The queue is emptied before the `Napi::[Typed]ThreadSafeFunction` is destroyed.
29+
It is important that `Release()` be the last API call made in conjunction with a
30+
given `Napi::[Typed]ThreadSafeFunction`, because after the call completes, there
31+
is no guarantee that the `Napi::[Typed]ThreadSafeFunction` is still allocated.
32+
For the same reason it is also important that no more use be made of a
33+
thread-safe function after receiving a return value of `napi_closing` in
34+
response to a call to `BlockingCall()` or `NonBlockingCall()`. Data associated
35+
with the `Napi::[Typed]ThreadSafeFunction` can be freed in its `Finalizer`
36+
callback which was passed to `[Typed]ThreadSafeFunction::New()`.
37+
38+
Once the number of threads making use of a `Napi::[Typed]ThreadSafeFunction`
3939
reaches zero, no further threads can start making use of it by calling
4040
`Acquire()`. In fact, all subsequent API calls associated with it, except
4141
`Release()`, will return an error value of `napi_closing`.
4242

4343
## Implementation Differences
4444

45-
The choice between `Napi::ThreadSafeFunction` and `Napi::ThreadSafeFunctionEx`
46-
depends largely on how you plan to execute your native C++ code (the "callback")
47-
on the Node.js thread.
45+
The choice between `Napi::ThreadSafeFunction` and
46+
`Napi::TypedThreadSafeFunction` depends largely on how you plan to execute your
47+
native C++ code (the "callback") on the Node.js thread.
4848

4949
### [`Napi::ThreadSafeFunction`](threadsafe_function.md)
5050

@@ -64,13 +64,13 @@ This API has some dynamic functionality, in that:
6464

6565
Note that this functionality comes with some **additional overhead** and
6666
situational **memory leaks**:
67-
- The API acts as a "broker" between the underlying
68-
`napi_threadsafe_function`, and dynamically constructs a wrapper for your
69-
callback on the heap for every call to `[Non]BlockingCall()`.
67+
- The API acts as a "broker" between the underlying `napi_threadsafe_function`,
68+
and dynamically constructs a wrapper for your callback on the heap for every
69+
call to `[Non]BlockingCall()`.
7070
- In acting in this "broker" fashion, the API will call the underlying "make
7171
call" N-API method on this packaged item. If the API has determined the
72-
thread-safe function is no longer accessible (eg. all threads have released yet
73-
there are still items on the queue), **the callback passed to
72+
thread-safe function is no longer accessible (eg. all threads have released
73+
yet there are still items on the queue), **the callback passed to
7474
[Non]BlockingCall will not execute**. This means it is impossible to perform
7575
clean-up for calls that never execute their `CallJs` callback. **This may lead
7676
to memory leaks** if you are dynamically allocating memory.
@@ -81,9 +81,9 @@ situational **memory leaks**:
8181
_type-safe_, as the method returns an object that can be "any-casted", instead
8282
of having a static type.
8383

84-
### [`Napi::ThreadSafeFunctionEx`](threadsafe_function_ex.md)
84+
### [`Napi::TypedThreadSafeFunction`](typed_threadsafe_function.md)
8585

86-
The `ThreadSafeFunctionEx` class is a new implementation to address the
86+
The `TypedThreadSafeFunction` class is a new implementation to address the
8787
drawbacks listed above. The API is designed with N-API 5's support of an
8888
optional function callback. The API will correctly allow developers to pass
8989
`std::nullptr` instead of a `const Function&` for the callback function
@@ -112,7 +112,7 @@ The removal of the dynamic call functionality has the following implications:
112112

113113
### Usage Suggestions
114114

115-
In summary, it may be best to use `Napi::ThreadSafeFunctionEx` if:
115+
In summary, it may be best to use `Napi::TypedThreadSafeFunction` if:
116116

117117
- static, compile-time support for targeting N-API 4 or 5+ with an optional
118118
JavaScript callback feature is desired;

doc/threadsafe_function_ex.md renamed to doc/typed_threadsafe_function.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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
4242
statically 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`
4949
function has several overloads for the various optional parameters: skip the
5050
optional 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
9090
Depending on the targetted `NAPI_VERSION`, the API has different implementations
9191
for `CallbackType callback`.
9292
9393
When 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
9898
When 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
106106
will 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

112112
Returns one of:
@@ -124,7 +124,7 @@ has undefined results in the current thread, as the thread-safe function may
124124
have 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

130130
Returns 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

152152
Returns 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

176176
Returns one of:
177177
- `napi_ok`: `data` was successfully added to the queue.
@@ -196,7 +196,7 @@ using namespace Napi;
196196
using Context = Reference<Value>;
197197
using DataType = int;
198198
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>;
200200
using FinalizerDataType = void;
201201

202202
std::thread nativeThread;

0 commit comments

Comments
 (0)