@@ -9,9 +9,9 @@ using namespace Napi;
9
9
constexpr size_t ARRAY_LENGTH = 10 ;
10
10
constexpr size_t MAX_QUEUE_SIZE = 2 ;
11
11
12
- static std::thread threadsEx [2 ];
12
+ static std::thread threads [2 ];
13
13
14
- struct ThreadSafeFunctionInfo {
14
+ static struct ThreadSafeFunctionInfo {
15
15
enum CallType {
16
16
DEFAULT,
17
17
BLOCKING,
@@ -21,40 +21,43 @@ struct ThreadSafeFunctionInfo {
21
21
bool startSecondary;
22
22
FunctionReference jsFinalizeCallback;
23
23
uint32_t maxQueueSize;
24
- } tsfnInfoEx ;
24
+ } tsfnInfo ;
25
25
26
26
static void TSFNCallJS (Env env, Function jsCallback,
27
27
ThreadSafeFunctionInfo * /* context */ , int *data) {
28
- // If called with no data
29
- if (data == nullptr ) {
30
- jsCallback.Call ({});
31
- } else {
32
- jsCallback.Call ({Number::New (env, *data)});
28
+ // A null environment signifies the threadsafe function has been finalized.
29
+ if (!(env == nullptr || jsCallback == nullptr )) {
30
+ // If called with no data
31
+ if (data == nullptr ) {
32
+ jsCallback.Call ({});
33
+ } else {
34
+ jsCallback.Call ({Number::New (env, *data)});
35
+ }
33
36
}
34
37
}
35
38
36
39
using TSFN = ThreadSafeFunctionEx<ThreadSafeFunctionInfo, int , TSFNCallJS>;
37
- static TSFN tsfnEx ;
40
+ static TSFN tsfn ;
38
41
39
42
// Thread data to transmit to JS
40
- static int intsEx [ARRAY_LENGTH];
43
+ static int ints [ARRAY_LENGTH];
41
44
42
- static void SecondaryThreadEx () {
43
- if (tsfnEx .Release () != napi_ok) {
45
+ static void SecondaryThread () {
46
+ if (tsfn .Release () != napi_ok) {
44
47
Error::Fatal (" SecondaryThread" , " ThreadSafeFunction.Release() failed" );
45
48
}
46
49
}
47
50
48
51
// Source thread producing the data
49
- static void DataSourceThreadEx () {
50
- ThreadSafeFunctionInfo* info = tsfnEx .GetContext ();
52
+ static void DataSourceThread () {
53
+ ThreadSafeFunctionInfo* info = tsfn .GetContext ();
51
54
52
55
if (info->startSecondary ) {
53
- if (tsfnEx .Acquire () != napi_ok) {
56
+ if (tsfn .Acquire () != napi_ok) {
54
57
Error::Fatal (" DataSourceThread" , " ThreadSafeFunction.Acquire() failed" );
55
58
}
56
59
57
- threadsEx [1 ] = std::thread (SecondaryThreadEx );
60
+ threads [1 ] = std::thread (SecondaryThread );
58
61
}
59
62
60
63
bool queueWasFull = false ;
@@ -64,13 +67,13 @@ static void DataSourceThreadEx() {
64
67
65
68
switch (info->type ) {
66
69
case ThreadSafeFunctionInfo::DEFAULT:
67
- status = tsfnEx .BlockingCall ();
70
+ status = tsfn .BlockingCall ();
68
71
break ;
69
72
case ThreadSafeFunctionInfo::BLOCKING:
70
- status = tsfnEx .BlockingCall (&intsEx [index]);
73
+ status = tsfn .BlockingCall (&ints [index]);
71
74
break ;
72
75
case ThreadSafeFunctionInfo::NON_BLOCKING:
73
- status = tsfnEx .NonBlockingCall (&intsEx [index]);
76
+ status = tsfn .NonBlockingCall (&ints [index]);
74
77
break ;
75
78
}
76
79
@@ -108,24 +111,24 @@ static void DataSourceThreadEx() {
108
111
Error::Fatal (" DataSourceThread" , " Queue was never closing" );
109
112
}
110
113
111
- if (!queueWasClosing && tsfnEx .Release () != napi_ok) {
114
+ if (!queueWasClosing && tsfn .Release () != napi_ok) {
112
115
Error::Fatal (" DataSourceThread" , " ThreadSafeFunction.Release() failed" );
113
116
}
114
117
}
115
118
116
- static Value StopThreadEx (const CallbackInfo& info) {
117
- tsfnInfoEx .jsFinalizeCallback = Napi::Persistent (info[0 ].As <Function>());
119
+ static Value StopThread (const CallbackInfo& info) {
120
+ tsfnInfo .jsFinalizeCallback = Napi::Persistent (info[0 ].As <Function>());
118
121
bool abort = info[1 ].As <Boolean>();
119
122
if (abort) {
120
- tsfnEx .Abort ();
123
+ tsfn .Abort ();
121
124
} else {
122
- tsfnEx .Release ();
125
+ tsfn .Release ();
123
126
}
124
127
return Value ();
125
128
}
126
129
127
130
// Join the thread and inform JS that we're done.
128
- static void JoinTheThreadsEx (Env /* env */ ,
131
+ static void JoinTheThreads (Env /* env */ ,
129
132
std::thread* theThreads,
130
133
ThreadSafeFunctionInfo* info) {
131
134
theThreads[0 ].join ();
@@ -137,54 +140,54 @@ static void JoinTheThreadsEx(Env /* env */,
137
140
info->jsFinalizeCallback .Reset ();
138
141
}
139
142
140
- static Value StartThreadInternalEx (const CallbackInfo& info,
143
+ static Value StartThreadInternal (const CallbackInfo& info,
141
144
ThreadSafeFunctionInfo::CallType type) {
142
- tsfnInfoEx .type = type;
143
- tsfnInfoEx .abort = info[1 ].As <Boolean>();
144
- tsfnInfoEx .startSecondary = info[2 ].As <Boolean>();
145
- tsfnInfoEx .maxQueueSize = info[3 ].As <Number>().Uint32Value ();
145
+ tsfnInfo .type = type;
146
+ tsfnInfo .abort = info[1 ].As <Boolean>();
147
+ tsfnInfo .startSecondary = info[2 ].As <Boolean>();
148
+ tsfnInfo .maxQueueSize = info[3 ].As <Number>().Uint32Value ();
146
149
147
- tsfnEx = TSFN::New (info.Env (), info[0 ].As <Function>(), Object::New (info.Env ()),
148
- " Test" , tsfnInfoEx .maxQueueSize , 2 , &tsfnInfoEx, JoinTheThreadsEx, threadsEx );
150
+ tsfn = TSFN::New (info.Env (), info[0 ].As <Function>(), Object::New (info.Env ()),
151
+ " Test" , tsfnInfo .maxQueueSize , 2 , &tsfnInfo, JoinTheThreads, threads );
149
152
150
- threadsEx [0 ] = std::thread (DataSourceThreadEx );
153
+ threads [0 ] = std::thread (DataSourceThread );
151
154
152
155
return Value ();
153
156
}
154
157
155
- static Value ReleaseEx (const CallbackInfo& /* info */ ) {
156
- if (tsfnEx .Release () != napi_ok) {
158
+ static Value Release (const CallbackInfo& /* info */ ) {
159
+ if (tsfn .Release () != napi_ok) {
157
160
Error::Fatal (" Release" , " ThreadSafeFunction.Release() failed" );
158
161
}
159
162
return Value ();
160
163
}
161
164
162
- static Value StartThreadEx (const CallbackInfo& info) {
163
- return StartThreadInternalEx (info, ThreadSafeFunctionInfo::BLOCKING);
165
+ static Value StartThread (const CallbackInfo& info) {
166
+ return StartThreadInternal (info, ThreadSafeFunctionInfo::BLOCKING);
164
167
}
165
168
166
- static Value StartThreadNonblockingEx (const CallbackInfo& info) {
167
- return StartThreadInternalEx (info, ThreadSafeFunctionInfo::NON_BLOCKING);
169
+ static Value StartThreadNonblocking (const CallbackInfo& info) {
170
+ return StartThreadInternal (info, ThreadSafeFunctionInfo::NON_BLOCKING);
168
171
}
169
172
170
- static Value StartThreadNoNativeEx (const CallbackInfo& info) {
171
- return StartThreadInternalEx (info, ThreadSafeFunctionInfo::DEFAULT);
173
+ static Value StartThreadNoNative (const CallbackInfo& info) {
174
+ return StartThreadInternal (info, ThreadSafeFunctionInfo::DEFAULT);
172
175
}
173
176
174
177
Object InitThreadSafeFunctionExThreadSafe (Env env) {
175
178
for (size_t index = 0 ; index < ARRAY_LENGTH; index++) {
176
- intsEx [index] = index;
179
+ ints [index] = index;
177
180
}
178
181
179
182
Object exports = Object::New (env);
180
183
exports[" ARRAY_LENGTH" ] = Number::New (env, ARRAY_LENGTH);
181
184
exports[" MAX_QUEUE_SIZE" ] = Number::New (env, MAX_QUEUE_SIZE);
182
- exports[" startThread" ] = Function::New (env, StartThreadEx );
183
- exports[" startThreadNoNative" ] = Function::New (env, StartThreadNoNativeEx );
185
+ exports[" startThread" ] = Function::New (env, StartThread );
186
+ exports[" startThreadNoNative" ] = Function::New (env, StartThreadNoNative );
184
187
exports[" startThreadNonblocking" ] =
185
- Function::New (env, StartThreadNonblockingEx );
186
- exports[" stopThread" ] = Function::New (env, StopThreadEx );
187
- exports[" release" ] = Function::New (env, ReleaseEx );
188
+ Function::New (env, StartThreadNonblocking );
189
+ exports[" stopThread" ] = Function::New (env, StopThread );
190
+ exports[" release" ] = Function::New (env, Release );
188
191
189
192
return exports;
190
193
}
0 commit comments