Skip to content

Commit 5e5b9ce

Browse files
committed
Apply formatting changes
1 parent 559ad8c commit 5e5b9ce

9 files changed

+542
-317
lines changed

napi-inl.h

Lines changed: 280 additions & 152 deletions
Large diffs are not rendered by default.

napi.h

Lines changed: 104 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,15 +2249,14 @@ namespace Napi {
22492249
napi_threadsafe_function _tsfn;
22502250
};
22512251

2252-
// A TypedThreadSafeFunction by default has no context (nullptr) and can accept
2253-
// any type (void) to its CallJs.
2254-
template <typename ContextType = std::nullptr_t, typename DataType = void,
2255-
void (*CallJs)(Napi::Env, Napi::Function, ContextType *,
2256-
DataType *) = nullptr>
2252+
// A TypedThreadSafeFunction by default has no context (nullptr) and can
2253+
// accept any type (void) to its CallJs.
2254+
template <typename ContextType = std::nullptr_t,
2255+
typename DataType = void,
2256+
void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*) =
2257+
nullptr>
22572258
class TypedThreadSafeFunction {
2258-
2259-
public:
2260-
2259+
public:
22612260
// This API may only be called from the main thread.
22622261
// Helper function that returns nullptr if running N-API 5+, otherwise a
22632262
// non-empty, no-op Function. This provides the ability to specify at
@@ -2268,85 +2267,123 @@ namespace Napi {
22682267
#else
22692268
static Napi::Function EmptyFunctionFactory(Napi::Env env);
22702269
#endif
2271-
static Napi::Function FunctionOrEmpty(Napi::Env env, Napi::Function& callback);
2270+
static Napi::Function FunctionOrEmpty(Napi::Env env,
2271+
Napi::Function& callback);
22722272

22732273
#if NAPI_VERSION > 4
22742274
// This API may only be called from the main thread.
22752275
// Creates a new threadsafe function with:
22762276
// Callback [missing] Resource [missing] Finalizer [missing]
22772277
template <typename ResourceString>
2278-
static TypedThreadSafeFunction<ContextType, DataType, CallJs>
2279-
New(napi_env env, ResourceString resourceName, size_t maxQueueSize,
2280-
size_t initialThreadCount, ContextType *context = nullptr);
2278+
static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2279+
napi_env env,
2280+
ResourceString resourceName,
2281+
size_t maxQueueSize,
2282+
size_t initialThreadCount,
2283+
ContextType* context = nullptr);
22812284

22822285
// This API may only be called from the main thread.
22832286
// Creates a new threadsafe function with:
22842287
// Callback [missing] Resource [passed] Finalizer [missing]
22852288
template <typename ResourceString>
2286-
static TypedThreadSafeFunction<ContextType, DataType, CallJs>
2287-
New(napi_env env, const Object &resource, ResourceString resourceName,
2288-
size_t maxQueueSize, size_t initialThreadCount,
2289-
ContextType *context = nullptr);
2289+
static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2290+
napi_env env,
2291+
const Object& resource,
2292+
ResourceString resourceName,
2293+
size_t maxQueueSize,
2294+
size_t initialThreadCount,
2295+
ContextType* context = nullptr);
22902296

22912297
// This API may only be called from the main thread.
22922298
// Creates a new threadsafe function with:
22932299
// Callback [missing] Resource [missing] Finalizer [passed]
2294-
template <typename ResourceString, typename Finalizer,
2300+
template <typename ResourceString,
2301+
typename Finalizer,
22952302
typename FinalizerDataType = void>
2296-
static TypedThreadSafeFunction<ContextType, DataType, CallJs>
2297-
New(napi_env env, ResourceString resourceName, size_t maxQueueSize,
2298-
size_t initialThreadCount, ContextType *context,
2299-
Finalizer finalizeCallback, FinalizerDataType *data = nullptr);
2303+
static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2304+
napi_env env,
2305+
ResourceString resourceName,
2306+
size_t maxQueueSize,
2307+
size_t initialThreadCount,
2308+
ContextType* context,
2309+
Finalizer finalizeCallback,
2310+
FinalizerDataType* data = nullptr);
23002311

23012312
// This API may only be called from the main thread.
23022313
// Creates a new threadsafe function with:
23032314
// Callback [missing] Resource [passed] Finalizer [passed]
2304-
template <typename ResourceString, typename Finalizer,
2315+
template <typename ResourceString,
2316+
typename Finalizer,
23052317
typename FinalizerDataType = void>
2306-
static TypedThreadSafeFunction<ContextType, DataType, CallJs>
2307-
New(napi_env env, const Object &resource, ResourceString resourceName,
2308-
size_t maxQueueSize, size_t initialThreadCount, ContextType *context,
2309-
Finalizer finalizeCallback, FinalizerDataType *data = nullptr);
2318+
static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2319+
napi_env env,
2320+
const Object& resource,
2321+
ResourceString resourceName,
2322+
size_t maxQueueSize,
2323+
size_t initialThreadCount,
2324+
ContextType* context,
2325+
Finalizer finalizeCallback,
2326+
FinalizerDataType* data = nullptr);
23102327
#endif
23112328

23122329
// This API may only be called from the main thread.
23132330
// Creates a new threadsafe function with:
23142331
// Callback [passed] Resource [missing] Finalizer [missing]
23152332
template <typename ResourceString>
2316-
static TypedThreadSafeFunction<ContextType, DataType, CallJs>
2317-
New(napi_env env, const Function &callback, ResourceString resourceName,
2318-
size_t maxQueueSize, size_t initialThreadCount,
2319-
ContextType *context = nullptr);
2333+
static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2334+
napi_env env,
2335+
const Function& callback,
2336+
ResourceString resourceName,
2337+
size_t maxQueueSize,
2338+
size_t initialThreadCount,
2339+
ContextType* context = nullptr);
23202340

23212341
// This API may only be called from the main thread.
23222342
// Creates a new threadsafe function with:
23232343
// Callback [passed] Resource [passed] Finalizer [missing]
23242344
template <typename ResourceString>
2325-
static TypedThreadSafeFunction<ContextType, DataType, CallJs>
2326-
New(napi_env env, const Function &callback, const Object &resource,
2327-
ResourceString resourceName, size_t maxQueueSize,
2328-
size_t initialThreadCount, ContextType *context = nullptr);
2345+
static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2346+
napi_env env,
2347+
const Function& callback,
2348+
const Object& resource,
2349+
ResourceString resourceName,
2350+
size_t maxQueueSize,
2351+
size_t initialThreadCount,
2352+
ContextType* context = nullptr);
23292353

23302354
// This API may only be called from the main thread.
23312355
// Creates a new threadsafe function with:
23322356
// Callback [passed] Resource [missing] Finalizer [passed]
2333-
template <typename ResourceString, typename Finalizer,
2357+
template <typename ResourceString,
2358+
typename Finalizer,
23342359
typename FinalizerDataType = void>
2335-
static TypedThreadSafeFunction<ContextType, DataType, CallJs>
2336-
New(napi_env env, const Function &callback, ResourceString resourceName,
2337-
size_t maxQueueSize, size_t initialThreadCount, ContextType *context,
2338-
Finalizer finalizeCallback, FinalizerDataType *data = nullptr);
2360+
static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2361+
napi_env env,
2362+
const Function& callback,
2363+
ResourceString resourceName,
2364+
size_t maxQueueSize,
2365+
size_t initialThreadCount,
2366+
ContextType* context,
2367+
Finalizer finalizeCallback,
2368+
FinalizerDataType* data = nullptr);
23392369

23402370
// This API may only be called from the main thread.
23412371
// Creates a new threadsafe function with:
23422372
// Callback [passed] Resource [passed] Finalizer [passed]
2343-
template <typename CallbackType, typename ResourceString, typename Finalizer,
2344-
typename FinalizerDataType>
2345-
static TypedThreadSafeFunction<ContextType, DataType, CallJs>
2346-
New(napi_env env, CallbackType callback, const Object &resource,
2347-
ResourceString resourceName, size_t maxQueueSize,
2348-
size_t initialThreadCount, ContextType *context,
2349-
Finalizer finalizeCallback, FinalizerDataType *data = nullptr);
2373+
template <typename CallbackType,
2374+
typename ResourceString,
2375+
typename Finalizer,
2376+
typename FinalizerDataType>
2377+
static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2378+
napi_env env,
2379+
CallbackType callback,
2380+
const Object& resource,
2381+
ResourceString resourceName,
2382+
size_t maxQueueSize,
2383+
size_t initialThreadCount,
2384+
ContextType* context,
2385+
Finalizer finalizeCallback,
2386+
FinalizerDataType* data = nullptr);
23502387

23512388
TypedThreadSafeFunction<ContextType, DataType, CallJs>();
23522389
TypedThreadSafeFunction<ContextType, DataType, CallJs>(
@@ -2355,10 +2392,10 @@ namespace Napi {
23552392
operator napi_threadsafe_function() const;
23562393

23572394
// This API may be called from any thread.
2358-
napi_status BlockingCall(DataType *data = nullptr) const;
2395+
napi_status BlockingCall(DataType* data = nullptr) const;
23592396

23602397
// This API may be called from any thread.
2361-
napi_status NonBlockingCall(DataType *data = nullptr) const;
2398+
napi_status NonBlockingCall(DataType* data = nullptr) const;
23622399

23632400
// This API may only be called from the main thread.
23642401
void Ref(napi_env env) const;
@@ -2376,22 +2413,30 @@ namespace Napi {
23762413
napi_status Abort();
23772414

23782415
// This API may be called from any thread.
2379-
ContextType *GetContext() const;
2416+
ContextType* GetContext() const;
23802417

2381-
private:
2382-
template <typename ResourceString, typename Finalizer,
2418+
private:
2419+
template <typename ResourceString,
2420+
typename Finalizer,
23832421
typename FinalizerDataType>
2384-
static TypedThreadSafeFunction<ContextType, DataType, CallJs>
2385-
New(napi_env env, const Function &callback, const Object &resource,
2386-
ResourceString resourceName, size_t maxQueueSize,
2387-
size_t initialThreadCount, ContextType *context,
2388-
Finalizer finalizeCallback, FinalizerDataType *data,
2422+
static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2423+
napi_env env,
2424+
const Function& callback,
2425+
const Object& resource,
2426+
ResourceString resourceName,
2427+
size_t maxQueueSize,
2428+
size_t initialThreadCount,
2429+
ContextType* context,
2430+
Finalizer finalizeCallback,
2431+
FinalizerDataType* data,
23892432
napi_finalize wrapper);
23902433

2391-
static void CallJsInternal(napi_env env, napi_value jsCallback,
2392-
void *context, void *data);
2434+
static void CallJsInternal(napi_env env,
2435+
napi_value jsCallback,
2436+
void* context,
2437+
void* data);
23932438

2394-
protected:
2439+
protected:
23952440
napi_threadsafe_function _tsfn;
23962441
};
23972442
template <typename DataType>

test/binding.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,16 @@ Object Init(Env env, Object exports) {
115115
exports.Set("threadsafe_function_sum", InitThreadSafeFunctionSum(env));
116116
exports.Set("threadsafe_function_unref", InitThreadSafeFunctionUnref(env));
117117
exports.Set("threadsafe_function", InitTypedThreadSafeFunction(env));
118-
exports.Set("typed_threadsafe_function_ctx", InitTypedThreadSafeFunctionCtx(env));
119-
exports.Set("typed_threadsafe_function_existing_tsfn", InitTypedThreadSafeFunctionExistingTsfn(env));
120-
exports.Set("typed_threadsafe_function_ptr", InitTypedThreadSafeFunctionPtr(env));
121-
exports.Set("typed_threadsafe_function_sum", InitTypedThreadSafeFunctionSum(env));
122-
exports.Set("typed_threadsafe_function_unref", InitTypedThreadSafeFunctionUnref(env));
118+
exports.Set("typed_threadsafe_function_ctx",
119+
InitTypedThreadSafeFunctionCtx(env));
120+
exports.Set("typed_threadsafe_function_existing_tsfn",
121+
InitTypedThreadSafeFunctionExistingTsfn(env));
122+
exports.Set("typed_threadsafe_function_ptr",
123+
InitTypedThreadSafeFunctionPtr(env));
124+
exports.Set("typed_threadsafe_function_sum",
125+
InitTypedThreadSafeFunctionSum(env));
126+
exports.Set("typed_threadsafe_function_unref",
127+
InitTypedThreadSafeFunctionUnref(env));
123128
exports.Set("typed_threadsafe_function", InitTypedThreadSafeFunction(env));
124129
#endif
125130
exports.Set("typedarray", InitTypedArray(env));

test/typed_threadsafe_function/typed_threadsafe_function.cc

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@ constexpr size_t MAX_QUEUE_SIZE = 2;
1212
static std::thread threads[2];
1313

1414
static struct ThreadSafeFunctionInfo {
15-
enum CallType {
16-
DEFAULT,
17-
BLOCKING,
18-
NON_BLOCKING
19-
} type;
15+
enum CallType { DEFAULT, BLOCKING, NON_BLOCKING } type;
2016
bool abort;
2117
bool startSecondary;
2218
FunctionReference jsFinalizeCallback;
2319
uint32_t maxQueueSize;
2420
} tsfnInfo;
2521

26-
static void TSFNCallJS(Env env, Function jsCallback,
27-
ThreadSafeFunctionInfo * /* context */, int *data) {
22+
static void TSFNCallJS(Env env,
23+
Function jsCallback,
24+
ThreadSafeFunctionInfo* /* context */,
25+
int* data) {
2826
// A null environment signifies the threadsafe function has been finalized.
2927
if (!(env == nullptr || jsCallback == nullptr)) {
3028
// If called with no data
@@ -82,24 +80,25 @@ static void DataSourceThread() {
8280
// chance to abort.
8381
auto start = std::chrono::high_resolution_clock::now();
8482
constexpr auto MS_200 = std::chrono::milliseconds(200);
85-
for (; std::chrono::high_resolution_clock::now() - start < MS_200;);
83+
for (; std::chrono::high_resolution_clock::now() - start < MS_200;)
84+
;
8685
}
8786

8887
switch (status) {
89-
case napi_queue_full:
90-
queueWasFull = true;
91-
index++;
92-
// fall through
88+
case napi_queue_full:
89+
queueWasFull = true;
90+
index++;
91+
// fall through
9392

94-
case napi_ok:
95-
continue;
93+
case napi_ok:
94+
continue;
9695

97-
case napi_closing:
98-
queueWasClosing = true;
99-
break;
96+
case napi_closing:
97+
queueWasClosing = true;
98+
break;
10099

101-
default:
102-
Error::Fatal("DataSourceThread", "ThreadSafeFunction.*Call() failed");
100+
default:
101+
Error::Fatal("DataSourceThread", "ThreadSafeFunction.*Call() failed");
103102
}
104103
}
105104

@@ -141,14 +140,21 @@ static void JoinTheThreads(Env /* env */,
141140
}
142141

143142
static Value StartThreadInternal(const CallbackInfo& info,
144-
ThreadSafeFunctionInfo::CallType type) {
143+
ThreadSafeFunctionInfo::CallType type) {
145144
tsfnInfo.type = type;
146145
tsfnInfo.abort = info[1].As<Boolean>();
147146
tsfnInfo.startSecondary = info[2].As<Boolean>();
148147
tsfnInfo.maxQueueSize = info[3].As<Number>().Uint32Value();
149148

150-
tsfn = TSFN::New(info.Env(), info[0].As<Function>(), Object::New(info.Env()),
151-
"Test", tsfnInfo.maxQueueSize, 2, &tsfnInfo, JoinTheThreads, threads);
149+
tsfn = TSFN::New(info.Env(),
150+
info[0].As<Function>(),
151+
Object::New(info.Env()),
152+
"Test",
153+
tsfnInfo.maxQueueSize,
154+
2,
155+
&tsfnInfo,
156+
JoinTheThreads,
157+
threads);
152158

153159
threads[0] = std::thread(DataSourceThread);
154160

0 commit comments

Comments
 (0)