node-addon-api 3.0.2 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,41 +1,10 @@
1
1
  # ThreadSafeFunction
2
2
 
3
- JavaScript functions can normally only be called from a native addon's main
4
- thread. If an addon creates additional threads, then node-addon-api functions
5
- that require a `Napi::Env`, `Napi::Value`, or `Napi::Reference` must not be
6
- called from those threads.
7
-
8
- When an addon has additional threads and JavaScript functions need to be invoked
9
- based on the processing completed by those threads, those threads must
10
- communicate with the addon's main thread so that the main thread can invoke the
11
- JavaScript function on their behalf. The thread-safe function APIs provide an
12
- easy way to do this.
13
-
14
- These APIs provide the type `Napi::ThreadSafeFunction` as well as APIs to
15
- create, destroy, and call objects of this type.
16
- `Napi::ThreadSafeFunction::New()` creates a persistent reference that holds a
17
- JavaScript function which can be called from multiple threads. The calls happen
18
- asynchronously. This means that values with which the JavaScript callback is to
19
- be called will be placed in a queue, and, for each value in the queue, a call
20
- will eventually be made to the JavaScript function.
21
-
22
- `Napi::ThreadSafeFunction` objects are destroyed when every thread which uses
23
- the object has called `Release()` or has received a return status of
24
- `napi_closing` in response to a call to `BlockingCall()` or `NonBlockingCall()`.
25
- The queue is emptied before the `Napi::ThreadSafeFunction` is destroyed. It is
26
- important that `Release()` be the last API call made in conjunction with a given
27
- `Napi::ThreadSafeFunction`, because after the call completes, there is no
28
- guarantee that the `Napi::ThreadSafeFunction` is still allocated. For the same
29
- reason it is also important that no more use be made of a thread-safe function
30
- after receiving a return value of `napi_closing` in response to a call to
31
- `BlockingCall()` or `NonBlockingCall()`. Data associated with the
32
- `Napi::ThreadSafeFunction` can be freed in its `Finalizer` callback which was
33
- passed to `ThreadSafeFunction::New()`.
34
-
35
- Once the number of threads making use of a `Napi::ThreadSafeFunction` reaches
36
- zero, no further threads can start making use of it by calling `Acquire()`. In
37
- fact, all subsequent API calls associated with it, except `Release()`, will
38
- return an error value of `napi_closing`.
3
+ The `Napi::ThreadSafeFunction` type provides APIs for threads to communicate
4
+ with the addon's main thread to invoke JavaScript functions on their behalf.
5
+ Documentation can be found for an [overview of the API](threadsafe.md), as well
6
+ as [differences between the two thread-safe function
7
+ APIs](threadsafe.md#implementation-differences).
39
8
 
40
9
  ## Methods
41
10
 
@@ -92,7 +61,8 @@ New(napi_env env,
92
61
  - `maxQueueSize`: Maximum size of the queue. `0` for no limit.
93
62
  - `initialThreadCount`: The initial number of threads, including the main
94
63
  thread, which will be making use of this function.
95
- - `[optional] context`: Data to attach to the resulting `ThreadSafeFunction`.
64
+ - `[optional] context`: Data to attach to the resulting `ThreadSafeFunction`. It
65
+ can be retreived by calling `GetContext()`.
96
66
  - `[optional] finalizeCallback`: Function to call when the `ThreadSafeFunction`
97
67
  is being destroyed. This callback will be invoked on the main thread when the
98
68
  thread-safe function is about to be destroyed. It receives the context and the
@@ -101,8 +71,8 @@ New(napi_env env,
101
71
  `uv_thread_join()`. It is important that, aside from the main loop thread,
102
72
  there be no threads left using the thread-safe function after the finalize
103
73
  callback completes. Must implement `void operator()(Env env, DataType* data,
104
- Context* hint)`, skipping `data` or `hint` if they are not provided.
105
- Can be retreived via `GetContext()`.
74
+ ContextType* hint)`, skipping `data` or `hint` if they are not provided. Can
75
+ be retrieved via `GetContext()`.
106
76
  - `[optional] data`: Data to be passed to `finalizeCallback`.
107
77
 
108
78
  Returns a non-empty `Napi::ThreadSafeFunction` instance.
@@ -110,7 +80,7 @@ Returns a non-empty `Napi::ThreadSafeFunction` instance.
110
80
  ### Acquire
111
81
 
112
82
  Add a thread to this thread-safe function object, indicating that a new thread
113
- will start making use of the thread-safe function.
83
+ will start making use of the thread-safe function.
114
84
 
115
85
  ```cpp
116
86
  napi_status Napi::ThreadSafeFunction::Acquire()
@@ -118,7 +88,7 @@ napi_status Napi::ThreadSafeFunction::Acquire()
118
88
 
119
89
  Returns one of:
120
90
  - `napi_ok`: The thread has successfully acquired the thread-safe function
121
- for its use.
91
+ for its use.
122
92
  - `napi_closing`: The thread-safe function has been marked as closing via a
123
93
  previous call to `Abort()`.
124
94
 
@@ -136,7 +106,7 @@ napi_status Napi::ThreadSafeFunction::Release()
136
106
  Returns one of:
137
107
  - `napi_ok`: The thread-safe function has been successfully released.
138
108
  - `napi_invalid_arg`: The thread-safe function's thread-count is zero.
139
- - `napi_generic_failure`: A generic error occurred when attemping to release
109
+ - `napi_generic_failure`: A generic error occurred when attempting to release
140
110
  the thread-safe function.
141
111
 
142
112
  ### Abort
@@ -158,7 +128,7 @@ napi_status Napi::ThreadSafeFunction::Abort()
158
128
  Returns one of:
159
129
  - `napi_ok`: The thread-safe function has been successfully aborted.
160
130
  - `napi_invalid_arg`: The thread-safe function's thread-count is zero.
161
- - `napi_generic_failure`: A generic error occurred when attemping to abort
131
+ - `napi_generic_failure`: A generic error occurred when attempting to abort
162
132
  the thread-safe function.
163
133
 
164
134
  ### BlockingCall / NonBlockingCall
@@ -210,7 +180,7 @@ Returns one of:
210
180
  - `napi_closing`: The thread-safe function is aborted and cannot accept more
211
181
  calls.
212
182
  - `napi_invalid_arg`: The thread-safe function is closed.
213
- - `napi_generic_failure`: A generic error occurred when attemping to add to the
183
+ - `napi_generic_failure`: A generic error occurred when attempting to add to the
214
184
  queue.
215
185
 
216
186
  ## Example
@@ -258,10 +228,10 @@ Value Start( const CallbackInfo& info )
258
228
  // Create a native thread
259
229
  nativeThread = std::thread( [count] {
260
230
  auto callback = []( Napi::Env env, Function jsCallback, int* value ) {
261
- // Transform native data into JS data, passing it to the provided
231
+ // Transform native data into JS data, passing it to the provided
262
232
  // `jsCallback` -- the TSFN's JavaScript function.
263
233
  jsCallback.Call( {Number::New( env, *value )} );
264
-
234
+
265
235
  // We're finished with the data.
266
236
  delete value;
267
237
  };
@@ -0,0 +1,307 @@
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
+ - `DataType = void*`: The data to use in the native callback. By default, a TSFN
10
+ can accept any data type.
11
+ - `Callback = void(*)(Napi::Env, Napi::Function jsCallback, ContextType*,
12
+ DataType*)`: The callback to run for each item added to the queue. If no
13
+ `Callback` is given, the API will call the function `jsCallback` with no
14
+ arguments.
15
+
16
+ Documentation can be found for an [overview of the API](threadsafe.md), as well
17
+ as [differences between the two thread-safe function
18
+ APIs](threadsafe.md#implementation-differences).
19
+
20
+ ## Methods
21
+
22
+ ### Constructor
23
+
24
+ Creates a new empty instance of `Napi::TypedThreadSafeFunction`.
25
+
26
+ ```cpp
27
+ Napi::Function::TypedThreadSafeFunction<ContextType, DataType, Callback>::TypedThreadSafeFunction();
28
+ ```
29
+
30
+ ### Constructor
31
+
32
+ Creates a new instance of the `Napi::TypedThreadSafeFunction` object.
33
+
34
+ ```cpp
35
+ Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::TypedThreadSafeFunction(napi_threadsafe_function tsfn);
36
+ ```
37
+
38
+ - `tsfn`: The `napi_threadsafe_function` which is a handle for an existing
39
+ thread-safe function.
40
+
41
+ Returns a non-empty `Napi::TypedThreadSafeFunction` instance. To ensure the API
42
+ statically handles the correct return type for `GetContext()` and
43
+ `[Non]BlockingCall()`, pass the proper template arguments to
44
+ `Napi::TypedThreadSafeFunction`.
45
+
46
+ ### New
47
+
48
+ Creates a new instance of the `Napi::TypedThreadSafeFunction` object. The `New`
49
+ function has several overloads for the various optional parameters: skip the
50
+ optional parameter for that specific overload.
51
+
52
+ ```cpp
53
+ New(napi_env env,
54
+ CallbackType callback,
55
+ const Object& resource,
56
+ ResourceString resourceName,
57
+ size_t maxQueueSize,
58
+ size_t initialThreadCount,
59
+ ContextType* context,
60
+ Finalizer finalizeCallback,
61
+ FinalizerDataType* data = nullptr);
62
+ ```
63
+
64
+ - `env`: The `napi_env` environment in which to construct the
65
+ `Napi::ThreadSafeFunction` object.
66
+ - `[optional] callback`: The `Function` to call from another thread.
67
+ - `[optional] resource`: An object associated with the async work that will be
68
+ passed to possible async_hooks init hooks.
69
+ - `resourceName`: A JavaScript string to provide an identifier for the kind of
70
+ resource that is being provided for diagnostic information exposed by the
71
+ async_hooks API.
72
+ - `maxQueueSize`: Maximum size of the queue. `0` for no limit.
73
+ - `initialThreadCount`: The initial number of threads, including the main
74
+ thread, which will be making use of this function.
75
+ - `[optional] context`: Data to attach to the resulting `ThreadSafeFunction`. It
76
+ can be retreived via `GetContext()`.
77
+ - `[optional] finalizeCallback`: Function to call when the
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
+ receives the context and the finalize data given during construction (if
81
+ given), and provides an opportunity for cleaning up after the threads e.g. by
82
+ calling `uv_thread_join()`. It is important that, aside from the main loop
83
+ thread, there be no threads left using the thread-safe function after the
84
+ finalize callback completes. Must implement `void operator()(Env env,
85
+ FinalizerDataType* data, ContextType* hint)`.
86
+ - `[optional] data`: Data to be passed to `finalizeCallback`.
87
+
88
+ Returns a non-empty `Napi::TypedThreadSafeFunction` instance.
89
+
90
+ Depending on the targetted `NAPI_VERSION`, the API has different implementations
91
+ for `CallbackType callback`.
92
+
93
+ When targetting version 4, `callback` may be:
94
+ - of type `const Function&`
95
+ - not provided as a parameter, in which case the API creates a new no-op
96
+ `Function`
97
+
98
+ When targetting version 5+, `callback` may be:
99
+ - of type `const Function&`
100
+ - of type `std::nullptr_t`
101
+ - not provided as a parameter, in which case the API passes `std::nullptr`
102
+
103
+ ### Acquire
104
+
105
+ Adds a thread to this thread-safe function object, indicating that a new thread
106
+ will start making use of the thread-safe function.
107
+
108
+ ```cpp
109
+ napi_status Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::Acquire()
110
+ ```
111
+
112
+ Returns one of:
113
+ - `napi_ok`: The thread has successfully acquired the thread-safe function for
114
+ its use.
115
+ - `napi_closing`: The thread-safe function has been marked as closing via a
116
+ previous call to `Abort()`.
117
+
118
+ ### Release
119
+
120
+ Indicates that an existing thread will stop making use of the thread-safe
121
+ function. A thread should call this API when it stops making use of this
122
+ thread-safe function. Using any thread-safe APIs after having called this API
123
+ has undefined results in the current thread, as the thread-safe function may
124
+ have been destroyed.
125
+
126
+ ```cpp
127
+ napi_status Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::Release()
128
+ ```
129
+
130
+ Returns one of:
131
+ - `napi_ok`: The thread-safe function has been successfully released.
132
+ - `napi_invalid_arg`: The thread-safe function's thread-count is zero.
133
+ - `napi_generic_failure`: A generic error occurred when attemping to release the
134
+ thread-safe function.
135
+
136
+ ### Abort
137
+
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
+
148
+ ```cpp
149
+ napi_status Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::Abort()
150
+ ```
151
+
152
+ Returns one of:
153
+ - `napi_ok`: The thread-safe function has been successfully aborted.
154
+ - `napi_invalid_arg`: The thread-safe function's thread-count is zero.
155
+ - `napi_generic_failure`: A generic error occurred when attemping to abort the
156
+ thread-safe function.
157
+
158
+ ### BlockingCall / NonBlockingCall
159
+
160
+ Calls the Javascript function in either a blocking or non-blocking fashion.
161
+ - `BlockingCall()`: the API blocks until space becomes available in the queue.
162
+ Will never block if the thread-safe function was created with a maximum queue
163
+ size of `0`.
164
+ - `NonBlockingCall()`: will return `napi_queue_full` if the queue was full,
165
+ preventing data from being successfully added to the queue.
166
+
167
+ ```cpp
168
+ napi_status Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::BlockingCall(DataType* data = nullptr) const
169
+
170
+ napi_status Napi::TypedThreadSafeFunction<ContextType, DataType, Callback>::NonBlockingCall(DataType* data = nullptr) const
171
+ ```
172
+
173
+ - `[optional] data`: Data to pass to the callback which was passed to
174
+ `TypedThreadSafeFunction::New()`.
175
+
176
+ Returns one of:
177
+ - `napi_ok`: `data` was successfully added to the queue.
178
+ - `napi_queue_full`: The queue was full when trying to call in a non-blocking
179
+ method.
180
+ - `napi_closing`: The thread-safe function is aborted and no further calls can
181
+ be made.
182
+ - `napi_invalid_arg`: The thread-safe function is closed.
183
+ - `napi_generic_failure`: A generic error occurred when attemping to add to the
184
+ queue.
185
+
186
+
187
+ ## Example
188
+
189
+ ```cpp
190
+ #include <chrono>
191
+ #include <napi.h>
192
+ #include <thread>
193
+
194
+ using namespace Napi;
195
+
196
+ using Context = Reference<Value>;
197
+ using DataType = int;
198
+ void CallJs(Napi::Env env, Function callback, Context *context, DataType *data);
199
+ using TSFN = TypedThreadSafeFunction<Context, DataType, CallJs>;
200
+ using FinalizerDataType = void;
201
+
202
+ std::thread nativeThread;
203
+ TSFN tsfn;
204
+
205
+ Value Start(const CallbackInfo &info) {
206
+ Napi::Env env = info.Env();
207
+
208
+ if (info.Length() < 2) {
209
+ throw TypeError::New(env, "Expected two arguments");
210
+ } else if (!info[0].IsFunction()) {
211
+ throw TypeError::New(env, "Expected first arg to be function");
212
+ } else if (!info[1].IsNumber()) {
213
+ throw TypeError::New(env, "Expected second arg to be number");
214
+ }
215
+
216
+ int count = info[1].As<Number>().Int32Value();
217
+
218
+ // Create a new context set to the the receiver (ie, `this`) of the function
219
+ // call
220
+ Context *context = new Reference<Value>(Persistent(info.This()));
221
+
222
+ // Create a ThreadSafeFunction
223
+ tsfn = TSFN::New(
224
+ env,
225
+ info[0].As<Function>(), // JavaScript function called asynchronously
226
+ "Resource Name", // Name
227
+ 0, // Unlimited queue
228
+ 1, // Only one thread will use this initially
229
+ context,
230
+ [](Napi::Env, FinalizerDataType *,
231
+ Context *ctx) { // Finalizer used to clean threads up
232
+ nativeThread.join();
233
+ delete ctx;
234
+ });
235
+
236
+ // Create a native thread
237
+ nativeThread = std::thread([count] {
238
+ for (int i = 0; i < count; i++) {
239
+ // Create new data
240
+ int *value = new int(clock());
241
+
242
+ // Perform a blocking call
243
+ napi_status status = tsfn.BlockingCall(value);
244
+ if (status != napi_ok) {
245
+ // Handle error
246
+ break;
247
+ }
248
+
249
+ std::this_thread::sleep_for(std::chrono::seconds(1));
250
+ }
251
+
252
+ // Release the thread-safe function
253
+ tsfn.Release();
254
+ });
255
+
256
+ return Boolean::New(env, true);
257
+ }
258
+
259
+ // Transform native data into JS data, passing it to the provided
260
+ // `callback` -- the TSFN's JavaScript function.
261
+ void CallJs(Napi::Env env, Function callback, Context *context,
262
+ DataType *data) {
263
+ // Is the JavaScript environment still available to call into, eg. the TSFN is
264
+ // not aborted
265
+ if (env != nullptr) {
266
+ // On N-API 5+, the `callback` parameter is optional; however, this example
267
+ // does ensure a callback is provided.
268
+ if (callback != nullptr) {
269
+ callback.Call(context->Value(), {Number::New(env, *data)});
270
+ }
271
+ }
272
+ if (data != nullptr) {
273
+ // We're finished with the data.
274
+ delete data;
275
+ }
276
+ }
277
+
278
+ Napi::Object Init(Napi::Env env, Object exports) {
279
+ exports.Set("start", Function::New(env, Start));
280
+ return exports;
281
+ }
282
+
283
+ NODE_API_MODULE(clock, Init)
284
+ ```
285
+
286
+ The above code can be used from JavaScript as follows:
287
+
288
+ ```js
289
+ const { start } = require('bindings')('clock');
290
+
291
+ start.call(new Date(), function (clock) {
292
+ const context = this;
293
+ console.log(context, clock);
294
+ }, 5);
295
+ ```
296
+
297
+ When executed, the output will show the value of `clock()` five times at one
298
+ second intervals, prefixed with the TSFN's context -- `start`'s receiver (ie,
299
+ `new Date()`):
300
+
301
+ ```
302
+ 2020-08-18T21:04:25.116Z 49824
303
+ 2020-08-18T21:04:25.116Z 62493
304
+ 2020-08-18T21:04:25.116Z 62919
305
+ 2020-08-18T21:04:25.116Z 63228
306
+ 2020-08-18T21:04:25.116Z 63531
307
+ ```
@@ -20,8 +20,8 @@ Returns the highest N-API version supported by Node.js runtime.
20
20
 
21
21
  ### GetNodeVersion
22
22
 
23
- Retrives information about Node.js version present on the system. All the
24
- information is stored in the `napi_node_version` structrue that is defined as
23
+ Retrieves information about Node.js version present on the system. All the
24
+ information is stored in the `napi_node_version` structure that is defined as
25
25
  shown below:
26
26
 
27
27
  ```cpp