@@ -9,9 +9,9 @@ using namespace Napi;
99constexpr size_t ARRAY_LENGTH = 10 ;
1010constexpr size_t MAX_QUEUE_SIZE = 2 ;
1111
12- static std::thread threadsEx [2 ];
12+ static std::thread threads [2 ];
1313
14- struct ThreadSafeFunctionInfo {
14+ static struct ThreadSafeFunctionInfo {
1515 enum CallType {
1616 DEFAULT,
1717 BLOCKING,
@@ -21,40 +21,43 @@ struct ThreadSafeFunctionInfo {
2121 bool startSecondary;
2222 FunctionReference jsFinalizeCallback;
2323 uint32_t maxQueueSize;
24- } tsfnInfoEx ;
24+ } tsfnInfo ;
2525
2626static void TSFNCallJS (Env env, Function jsCallback,
2727 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+ }
3336 }
3437}
3538
3639using TSFN = ThreadSafeFunctionEx<ThreadSafeFunctionInfo, int , TSFNCallJS>;
37- static TSFN tsfnEx ;
40+ static TSFN tsfn ;
3841
3942// Thread data to transmit to JS
40- static int intsEx [ARRAY_LENGTH];
43+ static int ints [ARRAY_LENGTH];
4144
42- static void SecondaryThreadEx () {
43- if (tsfnEx .Release () != napi_ok) {
45+ static void SecondaryThread () {
46+ if (tsfn .Release () != napi_ok) {
4447 Error::Fatal (" SecondaryThread" , " ThreadSafeFunction.Release() failed" );
4548 }
4649}
4750
4851// Source thread producing the data
49- static void DataSourceThreadEx () {
50- ThreadSafeFunctionInfo* info = tsfnEx .GetContext ();
52+ static void DataSourceThread () {
53+ ThreadSafeFunctionInfo* info = tsfn .GetContext ();
5154
5255 if (info->startSecondary ) {
53- if (tsfnEx .Acquire () != napi_ok) {
56+ if (tsfn .Acquire () != napi_ok) {
5457 Error::Fatal (" DataSourceThread" , " ThreadSafeFunction.Acquire() failed" );
5558 }
5659
57- threadsEx [1 ] = std::thread (SecondaryThreadEx );
60+ threads [1 ] = std::thread (SecondaryThread );
5861 }
5962
6063 bool queueWasFull = false ;
@@ -64,13 +67,13 @@ static void DataSourceThreadEx() {
6467
6568 switch (info->type ) {
6669 case ThreadSafeFunctionInfo::DEFAULT:
67- status = tsfnEx .BlockingCall ();
70+ status = tsfn .BlockingCall ();
6871 break ;
6972 case ThreadSafeFunctionInfo::BLOCKING:
70- status = tsfnEx .BlockingCall (&intsEx [index]);
73+ status = tsfn .BlockingCall (&ints [index]);
7174 break ;
7275 case ThreadSafeFunctionInfo::NON_BLOCKING:
73- status = tsfnEx .NonBlockingCall (&intsEx [index]);
76+ status = tsfn .NonBlockingCall (&ints [index]);
7477 break ;
7578 }
7679
@@ -108,24 +111,24 @@ static void DataSourceThreadEx() {
108111 Error::Fatal (" DataSourceThread" , " Queue was never closing" );
109112 }
110113
111- if (!queueWasClosing && tsfnEx .Release () != napi_ok) {
114+ if (!queueWasClosing && tsfn .Release () != napi_ok) {
112115 Error::Fatal (" DataSourceThread" , " ThreadSafeFunction.Release() failed" );
113116 }
114117}
115118
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>());
118121 bool abort = info[1 ].As <Boolean>();
119122 if (abort) {
120- tsfnEx .Abort ();
123+ tsfn .Abort ();
121124 } else {
122- tsfnEx .Release ();
125+ tsfn .Release ();
123126 }
124127 return Value ();
125128}
126129
127130// Join the thread and inform JS that we're done.
128- static void JoinTheThreadsEx (Env /* env */ ,
131+ static void JoinTheThreads (Env /* env */ ,
129132 std::thread* theThreads,
130133 ThreadSafeFunctionInfo* info) {
131134 theThreads[0 ].join ();
@@ -137,54 +140,54 @@ static void JoinTheThreadsEx(Env /* env */,
137140 info->jsFinalizeCallback .Reset ();
138141}
139142
140- static Value StartThreadInternalEx (const CallbackInfo& info,
143+ static Value StartThreadInternal (const CallbackInfo& info,
141144 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 ();
146149
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 );
149152
150- threadsEx [0 ] = std::thread (DataSourceThreadEx );
153+ threads [0 ] = std::thread (DataSourceThread );
151154
152155 return Value ();
153156}
154157
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) {
157160 Error::Fatal (" Release" , " ThreadSafeFunction.Release() failed" );
158161 }
159162 return Value ();
160163}
161164
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);
164167}
165168
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);
168171}
169172
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);
172175}
173176
174177Object InitThreadSafeFunctionExThreadSafe (Env env) {
175178 for (size_t index = 0 ; index < ARRAY_LENGTH; index++) {
176- intsEx [index] = index;
179+ ints [index] = index;
177180 }
178181
179182 Object exports = Object::New (env);
180183 exports[" ARRAY_LENGTH" ] = Number::New (env, ARRAY_LENGTH);
181184 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 );
184187 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 );
188191
189192 return exports;
190193}
0 commit comments