From 5e5b9ce1b7023de2033cf74e383ebe48f792572d Mon Sep 17 00:00:00 2001 From: Kevin Eady <8634912+KevinEady@users.noreply.github.com> Date: Tue, 24 Nov 2020 18:34:02 +0100 Subject: [PATCH] Apply formatting changes --- napi-inl.h | 432 ++++++++++++------ napi.h | 163 ++++--- test/binding.cc | 15 +- .../typed_threadsafe_function.cc | 50 +- .../typed_threadsafe_function_ctx.cc | 30 +- ...typed_threadsafe_function_existing_tsfn.cc | 56 ++- .../typed_threadsafe_function_ptr.cc | 2 +- .../typed_threadsafe_function_sum.cc | 83 ++-- .../typed_threadsafe_function_unref.cc | 28 +- 9 files changed, 542 insertions(+), 317 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index 396b8aeae..5a345df53 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -262,16 +262,17 @@ struct ThreadSafeFinalize { }; template -typename std::enable_if::type -static inline CallJsWrapper(napi_env env, napi_value jsCallback, void *context, void *data) { - call(env, Function(env, jsCallback), static_cast(context), - static_cast(data)); +typename std::enable_if::type static inline CallJsWrapper( + napi_env env, napi_value jsCallback, void* context, void* data) { + call(env, + Function(env, jsCallback), + static_cast(context), + static_cast(data)); } template -typename std::enable_if::type -static inline CallJsWrapper(napi_env env, napi_value jsCallback, void * /*context*/, - void * /*data*/) { +typename std::enable_if::type static inline CallJsWrapper( + napi_env env, napi_value jsCallback, void* /*context*/, void* /*data*/) { if (jsCallback != nullptr) { Function(env, jsCallback).Call(0, nullptr); } @@ -4399,104 +4400,156 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) { // `napi_create_threadsafe_function` is optional. #if NAPI_VERSION > 4 // static, with Callback [missing] Resource [missing] Finalizer [missing] -template +template template inline TypedThreadSafeFunction TypedThreadSafeFunction::New( - napi_env env, ResourceString resourceName, size_t maxQueueSize, - size_t initialThreadCount, ContextType *context) { + napi_env env, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context) { TypedThreadSafeFunction tsfn; - napi_status status = napi_create_threadsafe_function( - env, nullptr, nullptr, String::From(env, resourceName), maxQueueSize, - initialThreadCount, nullptr, nullptr, context, - CallJsInternal, &tsfn._tsfn); + napi_status status = + napi_create_threadsafe_function(env, + nullptr, + nullptr, + String::From(env, resourceName), + maxQueueSize, + initialThreadCount, + nullptr, + nullptr, + context, + CallJsInternal, + &tsfn._tsfn); if (status != napi_ok) { - NAPI_THROW_IF_FAILED(env, status, - TypedThreadSafeFunction()); + NAPI_THROW_IF_FAILED( + env, status, TypedThreadSafeFunction()); } return tsfn; } // static, with Callback [missing] Resource [passed] Finalizer [missing] -template +template template inline TypedThreadSafeFunction TypedThreadSafeFunction::New( - napi_env env, const Object &resource, ResourceString resourceName, - size_t maxQueueSize, size_t initialThreadCount, ContextType *context) { + napi_env env, + const Object& resource, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context) { TypedThreadSafeFunction tsfn; - napi_status status = napi_create_threadsafe_function( - env, nullptr, resource, String::From(env, resourceName), maxQueueSize, - initialThreadCount, nullptr, nullptr, context, CallJsInternal, - &tsfn._tsfn); + napi_status status = + napi_create_threadsafe_function(env, + nullptr, + resource, + String::From(env, resourceName), + maxQueueSize, + initialThreadCount, + nullptr, + nullptr, + context, + CallJsInternal, + &tsfn._tsfn); if (status != napi_ok) { - NAPI_THROW_IF_FAILED(env, status, - TypedThreadSafeFunction()); + NAPI_THROW_IF_FAILED( + env, status, TypedThreadSafeFunction()); } return tsfn; } // static, with Callback [missing] Resource [missing] Finalizer [passed] -template -template +template inline TypedThreadSafeFunction TypedThreadSafeFunction::New( - napi_env env, ResourceString resourceName, size_t maxQueueSize, - size_t initialThreadCount, ContextType *context, Finalizer finalizeCallback, - FinalizerDataType *data) { + napi_env env, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context, + Finalizer finalizeCallback, + FinalizerDataType* data) { TypedThreadSafeFunction tsfn; - auto *finalizeData = new details::ThreadSafeFinalize( - {data, finalizeCallback}); + auto* finalizeData = new details:: + ThreadSafeFinalize( + {data, finalizeCallback}); napi_status status = napi_create_threadsafe_function( - env, nullptr, nullptr, String::From(env, resourceName), maxQueueSize, - initialThreadCount, finalizeData, + env, + nullptr, + nullptr, + String::From(env, resourceName), + maxQueueSize, + initialThreadCount, + finalizeData, details::ThreadSafeFinalize:: FinalizeFinalizeWrapperWithDataAndContext, - context, CallJsInternal, &tsfn._tsfn); + context, + CallJsInternal, + &tsfn._tsfn); if (status != napi_ok) { delete finalizeData; - NAPI_THROW_IF_FAILED(env, status, - TypedThreadSafeFunction()); + NAPI_THROW_IF_FAILED( + env, status, TypedThreadSafeFunction()); } return tsfn; } // static, with Callback [missing] Resource [passed] Finalizer [passed] -template -template +template inline TypedThreadSafeFunction TypedThreadSafeFunction::New( - napi_env env, const Object &resource, ResourceString resourceName, - size_t maxQueueSize, size_t initialThreadCount, ContextType *context, - Finalizer finalizeCallback, FinalizerDataType *data) { + napi_env env, + const Object& resource, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context, + Finalizer finalizeCallback, + FinalizerDataType* data) { TypedThreadSafeFunction tsfn; - auto *finalizeData = new details::ThreadSafeFinalize( - {data, finalizeCallback}); + auto* finalizeData = new details:: + ThreadSafeFinalize( + {data, finalizeCallback}); napi_status status = napi_create_threadsafe_function( - env, nullptr, resource, String::From(env, resourceName), maxQueueSize, - initialThreadCount, finalizeData, + env, + nullptr, + resource, + String::From(env, resourceName), + maxQueueSize, + initialThreadCount, + finalizeData, details::ThreadSafeFinalize:: FinalizeFinalizeWrapperWithDataAndContext, - context, CallJsInternal, &tsfn._tsfn); + context, + CallJsInternal, + &tsfn._tsfn); if (status != napi_ok) { delete finalizeData; - NAPI_THROW_IF_FAILED(env, status, - TypedThreadSafeFunction()); + NAPI_THROW_IF_FAILED( + env, status, TypedThreadSafeFunction()); } return tsfn; @@ -4504,223 +4557,296 @@ TypedThreadSafeFunction::New( #endif // static, with Callback [passed] Resource [missing] Finalizer [missing] -template +template template inline TypedThreadSafeFunction TypedThreadSafeFunction::New( - napi_env env, const Function &callback, ResourceString resourceName, - size_t maxQueueSize, size_t initialThreadCount, ContextType *context) { + napi_env env, + const Function& callback, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context) { TypedThreadSafeFunction tsfn; - napi_status status = napi_create_threadsafe_function( - env, callback, nullptr, String::From(env, resourceName), maxQueueSize, - initialThreadCount, nullptr, nullptr, context, CallJsInternal, - &tsfn._tsfn); + napi_status status = + napi_create_threadsafe_function(env, + callback, + nullptr, + String::From(env, resourceName), + maxQueueSize, + initialThreadCount, + nullptr, + nullptr, + context, + CallJsInternal, + &tsfn._tsfn); if (status != napi_ok) { - NAPI_THROW_IF_FAILED(env, status, - TypedThreadSafeFunction()); + NAPI_THROW_IF_FAILED( + env, status, TypedThreadSafeFunction()); } return tsfn; } // static, with Callback [passed] Resource [passed] Finalizer [missing] -template +template template inline TypedThreadSafeFunction TypedThreadSafeFunction::New( - napi_env env, const Function &callback, const Object &resource, - ResourceString resourceName, size_t maxQueueSize, size_t initialThreadCount, - ContextType *context) { + napi_env env, + const Function& callback, + const Object& resource, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context) { TypedThreadSafeFunction tsfn; - napi_status status = napi_create_threadsafe_function( - env, callback, resource, String::From(env, resourceName), maxQueueSize, - initialThreadCount, nullptr, nullptr, context, CallJsInternal, - &tsfn._tsfn); + napi_status status = + napi_create_threadsafe_function(env, + callback, + resource, + String::From(env, resourceName), + maxQueueSize, + initialThreadCount, + nullptr, + nullptr, + context, + CallJsInternal, + &tsfn._tsfn); if (status != napi_ok) { - NAPI_THROW_IF_FAILED(env, status, - TypedThreadSafeFunction()); + NAPI_THROW_IF_FAILED( + env, status, TypedThreadSafeFunction()); } return tsfn; } // static, with Callback [passed] Resource [missing] Finalizer [passed] -template -template +template inline TypedThreadSafeFunction TypedThreadSafeFunction::New( - napi_env env, const Function &callback, ResourceString resourceName, - size_t maxQueueSize, size_t initialThreadCount, ContextType *context, - Finalizer finalizeCallback, FinalizerDataType *data) { + napi_env env, + const Function& callback, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context, + Finalizer finalizeCallback, + FinalizerDataType* data) { TypedThreadSafeFunction tsfn; - auto *finalizeData = new details::ThreadSafeFinalize( - {data, finalizeCallback}); + auto* finalizeData = new details:: + ThreadSafeFinalize( + {data, finalizeCallback}); napi_status status = napi_create_threadsafe_function( - env, callback, nullptr, String::From(env, resourceName), maxQueueSize, - initialThreadCount, finalizeData, + env, + callback, + nullptr, + String::From(env, resourceName), + maxQueueSize, + initialThreadCount, + finalizeData, details::ThreadSafeFinalize:: FinalizeFinalizeWrapperWithDataAndContext, - context, CallJsInternal, &tsfn._tsfn); + context, + CallJsInternal, + &tsfn._tsfn); if (status != napi_ok) { delete finalizeData; - NAPI_THROW_IF_FAILED(env, status, - TypedThreadSafeFunction()); + NAPI_THROW_IF_FAILED( + env, status, TypedThreadSafeFunction()); } return tsfn; } // static, with: Callback [passed] Resource [passed] Finalizer [passed] -template -template +template inline TypedThreadSafeFunction TypedThreadSafeFunction::New( - napi_env env, CallbackType callback, const Object &resource, - ResourceString resourceName, size_t maxQueueSize, size_t initialThreadCount, - ContextType *context, Finalizer finalizeCallback, FinalizerDataType *data) { + napi_env env, + CallbackType callback, + const Object& resource, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context, + Finalizer finalizeCallback, + FinalizerDataType* data) { TypedThreadSafeFunction tsfn; - auto *finalizeData = new details::ThreadSafeFinalize( - {data, finalizeCallback}); + auto* finalizeData = new details:: + ThreadSafeFinalize( + {data, finalizeCallback}); napi_status status = napi_create_threadsafe_function( - env, details::DefaultCallbackWrapper>(env, callback), resource, - String::From(env, resourceName), maxQueueSize, initialThreadCount, + env, + details::DefaultCallbackWrapper< + CallbackType, + TypedThreadSafeFunction>(env, + callback), + resource, + String::From(env, resourceName), + maxQueueSize, + initialThreadCount, finalizeData, details::ThreadSafeFinalize:: FinalizeFinalizeWrapperWithDataAndContext, - context, CallJsInternal, &tsfn._tsfn); + context, + CallJsInternal, + &tsfn._tsfn); if (status != napi_ok) { delete finalizeData; - NAPI_THROW_IF_FAILED(env, status, - TypedThreadSafeFunction()); + NAPI_THROW_IF_FAILED( + env, status, TypedThreadSafeFunction()); } return tsfn; } -template -inline TypedThreadSafeFunction::TypedThreadSafeFunction() +template +inline TypedThreadSafeFunction:: + TypedThreadSafeFunction() : _tsfn() {} -template +template inline TypedThreadSafeFunction:: TypedThreadSafeFunction(napi_threadsafe_function tsfn) : _tsfn(tsfn) {} -template +template inline TypedThreadSafeFunction:: operator napi_threadsafe_function() const { return _tsfn; } -template +template inline napi_status TypedThreadSafeFunction::BlockingCall( - DataType *data) const { + DataType* data) const { return napi_call_threadsafe_function(_tsfn, data, napi_tsfn_blocking); } -template +template inline napi_status TypedThreadSafeFunction::NonBlockingCall( - DataType *data) const { + DataType* data) const { return napi_call_threadsafe_function(_tsfn, data, napi_tsfn_nonblocking); } -template -inline void -TypedThreadSafeFunction::Ref(napi_env env) const { +template +inline void TypedThreadSafeFunction::Ref( + napi_env env) const { if (_tsfn != nullptr) { napi_status status = napi_ref_threadsafe_function(env, _tsfn); NAPI_THROW_IF_FAILED_VOID(env, status); } } -template -inline void -TypedThreadSafeFunction::Unref(napi_env env) const { +template +inline void TypedThreadSafeFunction::Unref( + napi_env env) const { if (_tsfn != nullptr) { napi_status status = napi_unref_threadsafe_function(env, _tsfn); NAPI_THROW_IF_FAILED_VOID(env, status); } } -template +template inline napi_status TypedThreadSafeFunction::Acquire() const { return napi_acquire_threadsafe_function(_tsfn); } -template +template inline napi_status TypedThreadSafeFunction::Release() { return napi_release_threadsafe_function(_tsfn, napi_tsfn_release); } -template +template inline napi_status TypedThreadSafeFunction::Abort() { return napi_release_threadsafe_function(_tsfn, napi_tsfn_abort); } -template -inline ContextType * +template +inline ContextType* TypedThreadSafeFunction::GetContext() const { - void *context; + void* context; napi_status status = napi_get_threadsafe_function_context(_tsfn, &context); - NAPI_FATAL_IF_FAILED(status, "TypedThreadSafeFunction::GetContext", + NAPI_FATAL_IF_FAILED(status, + "TypedThreadSafeFunction::GetContext", "napi_get_threadsafe_function_context"); - return static_cast(context); + return static_cast(context); } // static -template +template void TypedThreadSafeFunction::CallJsInternal( - napi_env env, napi_value jsCallback, void *context, void *data) { + napi_env env, napi_value jsCallback, void* context, void* data) { details::CallJsWrapper( env, jsCallback, context, data); } #if NAPI_VERSION == 4 // static -template +template Napi::Function TypedThreadSafeFunction::EmptyFunctionFactory( Napi::Env env) { - return Napi::Function::New(env, [](const CallbackInfo &cb) {}); + return Napi::Function::New(env, [](const CallbackInfo& cb) {}); } // static -template +template Napi::Function TypedThreadSafeFunction::FunctionOrEmpty( - Napi::Env env, Napi::Function &callback) { + Napi::Env env, Napi::Function& callback) { if (callback.IsEmpty()) { return EmptyFunctionFactory(env); } @@ -4729,8 +4855,9 @@ TypedThreadSafeFunction::FunctionOrEmpty( #else // static -template +template std::nullptr_t TypedThreadSafeFunction::EmptyFunctionFactory( Napi::Env /*env*/) { @@ -4738,11 +4865,12 @@ TypedThreadSafeFunction::EmptyFunctionFactory( } // static -template +template Napi::Function TypedThreadSafeFunction::FunctionOrEmpty( - Napi::Env /*env*/, Napi::Function &callback) { + Napi::Env /*env*/, Napi::Function& callback) { return callback; } diff --git a/napi.h b/napi.h index 9d813778b..cf4410bd9 100644 --- a/napi.h +++ b/napi.h @@ -2249,15 +2249,14 @@ namespace Napi { napi_threadsafe_function _tsfn; }; - // A TypedThreadSafeFunction by default has no context (nullptr) and can accept - // any type (void) to its CallJs. - template + // A TypedThreadSafeFunction by default has no context (nullptr) and can + // accept any type (void) to its CallJs. + template class TypedThreadSafeFunction { - - public: - + public: // This API may only be called from the main thread. // Helper function that returns nullptr if running N-API 5+, otherwise a // non-empty, no-op Function. This provides the ability to specify at @@ -2268,85 +2267,123 @@ namespace Napi { #else static Napi::Function EmptyFunctionFactory(Napi::Env env); #endif - static Napi::Function FunctionOrEmpty(Napi::Env env, Napi::Function& callback); + static Napi::Function FunctionOrEmpty(Napi::Env env, + Napi::Function& callback); #if NAPI_VERSION > 4 // This API may only be called from the main thread. // Creates a new threadsafe function with: // Callback [missing] Resource [missing] Finalizer [missing] template - static TypedThreadSafeFunction - New(napi_env env, ResourceString resourceName, size_t maxQueueSize, - size_t initialThreadCount, ContextType *context = nullptr); + static TypedThreadSafeFunction New( + napi_env env, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context = nullptr); // This API may only be called from the main thread. // Creates a new threadsafe function with: // Callback [missing] Resource [passed] Finalizer [missing] template - static TypedThreadSafeFunction - New(napi_env env, const Object &resource, ResourceString resourceName, - size_t maxQueueSize, size_t initialThreadCount, - ContextType *context = nullptr); + static TypedThreadSafeFunction New( + napi_env env, + const Object& resource, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context = nullptr); // This API may only be called from the main thread. // Creates a new threadsafe function with: // Callback [missing] Resource [missing] Finalizer [passed] - template - static TypedThreadSafeFunction - New(napi_env env, ResourceString resourceName, size_t maxQueueSize, - size_t initialThreadCount, ContextType *context, - Finalizer finalizeCallback, FinalizerDataType *data = nullptr); + static TypedThreadSafeFunction New( + napi_env env, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context, + Finalizer finalizeCallback, + FinalizerDataType* data = nullptr); // This API may only be called from the main thread. // Creates a new threadsafe function with: // Callback [missing] Resource [passed] Finalizer [passed] - template - static TypedThreadSafeFunction - New(napi_env env, const Object &resource, ResourceString resourceName, - size_t maxQueueSize, size_t initialThreadCount, ContextType *context, - Finalizer finalizeCallback, FinalizerDataType *data = nullptr); + static TypedThreadSafeFunction New( + napi_env env, + const Object& resource, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context, + Finalizer finalizeCallback, + FinalizerDataType* data = nullptr); #endif // This API may only be called from the main thread. // Creates a new threadsafe function with: // Callback [passed] Resource [missing] Finalizer [missing] template - static TypedThreadSafeFunction - New(napi_env env, const Function &callback, ResourceString resourceName, - size_t maxQueueSize, size_t initialThreadCount, - ContextType *context = nullptr); + static TypedThreadSafeFunction New( + napi_env env, + const Function& callback, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context = nullptr); // This API may only be called from the main thread. // Creates a new threadsafe function with: // Callback [passed] Resource [passed] Finalizer [missing] template - static TypedThreadSafeFunction - New(napi_env env, const Function &callback, const Object &resource, - ResourceString resourceName, size_t maxQueueSize, - size_t initialThreadCount, ContextType *context = nullptr); + static TypedThreadSafeFunction New( + napi_env env, + const Function& callback, + const Object& resource, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context = nullptr); // This API may only be called from the main thread. // Creates a new threadsafe function with: // Callback [passed] Resource [missing] Finalizer [passed] - template - static TypedThreadSafeFunction - New(napi_env env, const Function &callback, ResourceString resourceName, - size_t maxQueueSize, size_t initialThreadCount, ContextType *context, - Finalizer finalizeCallback, FinalizerDataType *data = nullptr); + static TypedThreadSafeFunction New( + napi_env env, + const Function& callback, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context, + Finalizer finalizeCallback, + FinalizerDataType* data = nullptr); // This API may only be called from the main thread. // Creates a new threadsafe function with: // Callback [passed] Resource [passed] Finalizer [passed] - template - static TypedThreadSafeFunction - New(napi_env env, CallbackType callback, const Object &resource, - ResourceString resourceName, size_t maxQueueSize, - size_t initialThreadCount, ContextType *context, - Finalizer finalizeCallback, FinalizerDataType *data = nullptr); + template + static TypedThreadSafeFunction New( + napi_env env, + CallbackType callback, + const Object& resource, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context, + Finalizer finalizeCallback, + FinalizerDataType* data = nullptr); TypedThreadSafeFunction(); TypedThreadSafeFunction( @@ -2355,10 +2392,10 @@ namespace Napi { operator napi_threadsafe_function() const; // This API may be called from any thread. - napi_status BlockingCall(DataType *data = nullptr) const; + napi_status BlockingCall(DataType* data = nullptr) const; // This API may be called from any thread. - napi_status NonBlockingCall(DataType *data = nullptr) const; + napi_status NonBlockingCall(DataType* data = nullptr) const; // This API may only be called from the main thread. void Ref(napi_env env) const; @@ -2376,22 +2413,30 @@ namespace Napi { napi_status Abort(); // This API may be called from any thread. - ContextType *GetContext() const; + ContextType* GetContext() const; - private: - template - static TypedThreadSafeFunction - New(napi_env env, const Function &callback, const Object &resource, - ResourceString resourceName, size_t maxQueueSize, - size_t initialThreadCount, ContextType *context, - Finalizer finalizeCallback, FinalizerDataType *data, + static TypedThreadSafeFunction New( + napi_env env, + const Function& callback, + const Object& resource, + ResourceString resourceName, + size_t maxQueueSize, + size_t initialThreadCount, + ContextType* context, + Finalizer finalizeCallback, + FinalizerDataType* data, napi_finalize wrapper); - static void CallJsInternal(napi_env env, napi_value jsCallback, - void *context, void *data); + static void CallJsInternal(napi_env env, + napi_value jsCallback, + void* context, + void* data); - protected: + protected: napi_threadsafe_function _tsfn; }; template diff --git a/test/binding.cc b/test/binding.cc index b46cb4df8..03661da35 100644 --- a/test/binding.cc +++ b/test/binding.cc @@ -115,11 +115,16 @@ Object Init(Env env, Object exports) { exports.Set("threadsafe_function_sum", InitThreadSafeFunctionSum(env)); exports.Set("threadsafe_function_unref", InitThreadSafeFunctionUnref(env)); exports.Set("threadsafe_function", InitTypedThreadSafeFunction(env)); - exports.Set("typed_threadsafe_function_ctx", InitTypedThreadSafeFunctionCtx(env)); - exports.Set("typed_threadsafe_function_existing_tsfn", InitTypedThreadSafeFunctionExistingTsfn(env)); - exports.Set("typed_threadsafe_function_ptr", InitTypedThreadSafeFunctionPtr(env)); - exports.Set("typed_threadsafe_function_sum", InitTypedThreadSafeFunctionSum(env)); - exports.Set("typed_threadsafe_function_unref", InitTypedThreadSafeFunctionUnref(env)); + exports.Set("typed_threadsafe_function_ctx", + InitTypedThreadSafeFunctionCtx(env)); + exports.Set("typed_threadsafe_function_existing_tsfn", + InitTypedThreadSafeFunctionExistingTsfn(env)); + exports.Set("typed_threadsafe_function_ptr", + InitTypedThreadSafeFunctionPtr(env)); + exports.Set("typed_threadsafe_function_sum", + InitTypedThreadSafeFunctionSum(env)); + exports.Set("typed_threadsafe_function_unref", + InitTypedThreadSafeFunctionUnref(env)); exports.Set("typed_threadsafe_function", InitTypedThreadSafeFunction(env)); #endif exports.Set("typedarray", InitTypedArray(env)); diff --git a/test/typed_threadsafe_function/typed_threadsafe_function.cc b/test/typed_threadsafe_function/typed_threadsafe_function.cc index 71f47b959..f9896db86 100644 --- a/test/typed_threadsafe_function/typed_threadsafe_function.cc +++ b/test/typed_threadsafe_function/typed_threadsafe_function.cc @@ -12,19 +12,17 @@ constexpr size_t MAX_QUEUE_SIZE = 2; static std::thread threads[2]; static struct ThreadSafeFunctionInfo { - enum CallType { - DEFAULT, - BLOCKING, - NON_BLOCKING - } type; + enum CallType { DEFAULT, BLOCKING, NON_BLOCKING } type; bool abort; bool startSecondary; FunctionReference jsFinalizeCallback; uint32_t maxQueueSize; } tsfnInfo; -static void TSFNCallJS(Env env, Function jsCallback, - ThreadSafeFunctionInfo * /* context */, int *data) { +static void TSFNCallJS(Env env, + Function jsCallback, + ThreadSafeFunctionInfo* /* context */, + int* data) { // A null environment signifies the threadsafe function has been finalized. if (!(env == nullptr || jsCallback == nullptr)) { // If called with no data @@ -82,24 +80,25 @@ static void DataSourceThread() { // chance to abort. auto start = std::chrono::high_resolution_clock::now(); constexpr auto MS_200 = std::chrono::milliseconds(200); - for (; std::chrono::high_resolution_clock::now() - start < MS_200;); + for (; std::chrono::high_resolution_clock::now() - start < MS_200;) + ; } switch (status) { - case napi_queue_full: - queueWasFull = true; - index++; - // fall through + case napi_queue_full: + queueWasFull = true; + index++; + // fall through - case napi_ok: - continue; + case napi_ok: + continue; - case napi_closing: - queueWasClosing = true; - break; + case napi_closing: + queueWasClosing = true; + break; - default: - Error::Fatal("DataSourceThread", "ThreadSafeFunction.*Call() failed"); + default: + Error::Fatal("DataSourceThread", "ThreadSafeFunction.*Call() failed"); } } @@ -141,14 +140,21 @@ static void JoinTheThreads(Env /* env */, } static Value StartThreadInternal(const CallbackInfo& info, - ThreadSafeFunctionInfo::CallType type) { + ThreadSafeFunctionInfo::CallType type) { tsfnInfo.type = type; tsfnInfo.abort = info[1].As(); tsfnInfo.startSecondary = info[2].As(); tsfnInfo.maxQueueSize = info[3].As().Uint32Value(); - tsfn = TSFN::New(info.Env(), info[0].As(), Object::New(info.Env()), - "Test", tsfnInfo.maxQueueSize, 2, &tsfnInfo, JoinTheThreads, threads); + tsfn = TSFN::New(info.Env(), + info[0].As(), + Object::New(info.Env()), + "Test", + tsfnInfo.maxQueueSize, + 2, + &tsfnInfo, + JoinTheThreads, + threads); threads[0] = std::thread(DataSourceThread); diff --git a/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc b/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc index 4ec3e368b..ee70bb352 100644 --- a/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc +++ b/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc @@ -10,30 +10,31 @@ using TSFN = TypedThreadSafeFunction; namespace { class TSFNWrap : public ObjectWrap { -public: + public: static Object Init(Napi::Env env, Object exports); - TSFNWrap(const CallbackInfo &info); + TSFNWrap(const CallbackInfo& info); - Napi::Value GetContext(const CallbackInfo & /*info*/) { - ContextType *ctx = _tsfn.GetContext(); + Napi::Value GetContext(const CallbackInfo& /*info*/) { + ContextType* ctx = _tsfn.GetContext(); return ctx->Value(); }; - Napi::Value Release(const CallbackInfo &info) { + Napi::Value Release(const CallbackInfo& info) { Napi::Env env = info.Env(); _deferred = std::unique_ptr(new Promise::Deferred(env)); _tsfn.Release(); return _deferred->Promise(); }; -private: + private: TSFN _tsfn; std::unique_ptr _deferred; }; Object TSFNWrap::Init(Napi::Env env, Object exports) { Function func = - DefineClass(env, "TSFNWrap", + DefineClass(env, + "TSFNWrap", {InstanceMethod("getContext", &TSFNWrap::GetContext), InstanceMethod("release", &TSFNWrap::Release)}); @@ -41,19 +42,24 @@ Object TSFNWrap::Init(Napi::Env env, Object exports) { return exports; } -TSFNWrap::TSFNWrap(const CallbackInfo &info) : ObjectWrap(info) { - ContextType *_ctx = new ContextType; +TSFNWrap::TSFNWrap(const CallbackInfo& info) : ObjectWrap(info) { + ContextType* _ctx = new ContextType; *_ctx = Persistent(info[0]); - _tsfn = TSFN::New(info.Env(), this->Value(), "Test", 1, 1, _ctx, - [this](Napi::Env env, void *, ContextType *ctx) { + _tsfn = TSFN::New(info.Env(), + this->Value(), + "Test", + 1, + 1, + _ctx, + [this](Napi::Env env, void*, ContextType* ctx) { _deferred->Resolve(env.Undefined()); ctx->Reset(); delete ctx; }); } -} // namespace +} // namespace Object InitTypedThreadSafeFunctionCtx(Env env) { return TSFNWrap::Init(env, Object::New(env)); diff --git a/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc b/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc index 799aacb9f..eccf87c93 100644 --- a/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc +++ b/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc @@ -1,5 +1,5 @@ -#include "napi.h" #include +#include "napi.h" #if (NAPI_VERSION > 3) @@ -8,23 +8,22 @@ using namespace Napi; namespace { struct TestContext { - TestContext(Promise::Deferred &&deferred) + TestContext(Promise::Deferred&& deferred) : deferred(std::move(deferred)), callData(nullptr){}; napi_threadsafe_function tsfn; Promise::Deferred deferred; - double *callData; + double* callData; ~TestContext() { - if (callData != nullptr) - delete callData; + if (callData != nullptr) delete callData; }; }; using TSFN = TypedThreadSafeFunction; -void FinalizeCB(napi_env env, void * /*finalizeData */, void *context) { - TestContext *testContext = static_cast(context); +void FinalizeCB(napi_env env, void* /*finalizeData */, void* context) { + TestContext* testContext = static_cast(context); if (testContext->callData != nullptr) { testContext->deferred.Resolve(Number::New(env, *testContext->callData)); } else { @@ -33,10 +32,12 @@ void FinalizeCB(napi_env env, void * /*finalizeData */, void *context) { delete testContext; } -void CallJSWithData(napi_env env, napi_value /* callback */, void *context, - void *data) { - TestContext *testContext = static_cast(context); - testContext->callData = static_cast(data); +void CallJSWithData(napi_env env, + napi_value /* callback */, + void* context, + void* data) { + TestContext* testContext = static_cast(context); + testContext->callData = static_cast(data); napi_status status = napi_release_threadsafe_function(testContext->tsfn, napi_tsfn_release); @@ -44,9 +45,11 @@ void CallJSWithData(napi_env env, napi_value /* callback */, void *context, NAPI_THROW_IF_FAILED_VOID(env, status); } -void CallJSNoData(napi_env env, napi_value /* callback */, void *context, - void * /*data*/) { - TestContext *testContext = static_cast(context); +void CallJSNoData(napi_env env, + napi_value /* callback */, + void* context, + void* /*data*/) { + TestContext* testContext = static_cast(context); testContext->callData = nullptr; napi_status status = @@ -55,7 +58,7 @@ void CallJSNoData(napi_env env, napi_value /* callback */, void *context, NAPI_THROW_IF_FAILED_VOID(env, status); } -static Value TestCall(const CallbackInfo &info) { +static Value TestCall(const CallbackInfo& info) { Napi::Env env = info.Env(); bool isBlocking = false; bool hasData = false; @@ -70,15 +73,22 @@ static Value TestCall(const CallbackInfo &info) { } // Allow optional callback passed from JS. Useful for testing. - Function cb = Function::New(env, [](const CallbackInfo & /*info*/) {}); + Function cb = Function::New(env, [](const CallbackInfo& /*info*/) {}); - TestContext *testContext = new TestContext(Napi::Promise::Deferred(env)); + TestContext* testContext = new TestContext(Napi::Promise::Deferred(env)); - napi_status status = napi_create_threadsafe_function( - env, cb, Object::New(env), String::New(env, "Test"), 0, 1, - nullptr, /*finalize data*/ - FinalizeCB, testContext, hasData ? CallJSWithData : CallJSNoData, - &testContext->tsfn); + napi_status status = + napi_create_threadsafe_function(env, + cb, + Object::New(env), + String::New(env, "Test"), + 0, + 1, + nullptr, /*finalize data*/ + FinalizeCB, + testContext, + hasData ? CallJSWithData : CallJSNoData, + &testContext->tsfn); NAPI_THROW_IF_FAILED(env, status, Value()); @@ -102,7 +112,7 @@ static Value TestCall(const CallbackInfo &info) { return testContext->deferred.Promise(); } -} // namespace +} // namespace Object InitTypedThreadSafeFunctionExistingTsfn(Env env) { Object exports = Object::New(env); diff --git a/test/typed_threadsafe_function/typed_threadsafe_function_ptr.cc b/test/typed_threadsafe_function/typed_threadsafe_function_ptr.cc index 27367e4ea..891fd560c 100644 --- a/test/typed_threadsafe_function/typed_threadsafe_function_ptr.cc +++ b/test/typed_threadsafe_function/typed_threadsafe_function_ptr.cc @@ -16,7 +16,7 @@ static Value Test(const CallbackInfo& info) { return info.Env().Undefined(); } -} +} // namespace Object InitTypedThreadSafeFunctionPtr(Env env) { Object exports = Object::New(env); diff --git a/test/typed_threadsafe_function/typed_threadsafe_function_sum.cc b/test/typed_threadsafe_function/typed_threadsafe_function_sum.cc index 6b33499a6..9add259c4 100644 --- a/test/typed_threadsafe_function/typed_threadsafe_function_sum.cc +++ b/test/typed_threadsafe_function/typed_threadsafe_function_sum.cc @@ -1,8 +1,8 @@ -#include "napi.h" #include #include #include #include +#include "napi.h" #if (NAPI_VERSION > 3) @@ -11,8 +11,7 @@ using namespace Napi; namespace { struct TestData { - - TestData(Promise::Deferred &&deferred) : deferred(std::move(deferred)){}; + TestData(Promise::Deferred&& deferred) : deferred(std::move(deferred)){}; // Native Promise returned to JavaScript Promise::Deferred deferred; @@ -25,9 +24,10 @@ struct TestData { bool mainWantsRelease = false; size_t expected_calls = 0; - static void CallJs(Napi::Env env, Function callback, TestData *testData, - double *data) { - + static void CallJs(Napi::Env env, + Function callback, + TestData* testData, + double* data) { // This lambda runs on the main thread so it's OK to access the variables // `expected_calls` and `mainWantsRelease`. testData->expected_calls--; @@ -42,7 +42,7 @@ struct TestData { using TSFN = TypedThreadSafeFunction; -void FinalizerCallback(Napi::Env env, void *, TestData *finalizeData) { +void FinalizerCallback(Napi::Env env, void*, TestData* finalizeData) { for (size_t i = 0; i < finalizeData->threads.size(); ++i) { finalizeData->threads[i].join(); } @@ -60,17 +60,23 @@ void entryWithTSFN(TSFN tsfn, int threadId) { tsfn.Release(); } -static Value TestWithTSFN(const CallbackInfo &info) { +static Value TestWithTSFN(const CallbackInfo& info) { int threadCount = info[0].As().Int32Value(); Function cb = info[1].As(); // We pass the test data to the Finalizer for cleanup. The finalizer is // responsible for deleting this data as well. - TestData *testData = new TestData(Promise::Deferred::New(info.Env())); - - TSFN tsfn = TSFN::New( - info.Env(), cb, "Test", 0, threadCount, testData, - std::function(FinalizerCallback), testData); + TestData* testData = new TestData(Promise::Deferred::New(info.Env())); + + TSFN tsfn = + TSFN::New(info.Env(), + cb, + "Test", + 0, + threadCount, + testData, + std::function(FinalizerCallback), + testData); for (int i = 0; i < threadCount; ++i) { // A copy of the ThreadSafeFunction will go to the thread entry point @@ -82,7 +88,7 @@ static Value TestWithTSFN(const CallbackInfo &info) { // Task instance created for each new std::thread class DelayedTSFNTask { -public: + public: // Each instance has its own tsfn TSFN tsfn; @@ -100,8 +106,7 @@ class DelayedTSFNTask { }; struct TestDataDelayed : TestData { - - TestDataDelayed(Promise::Deferred &&deferred) + TestDataDelayed(Promise::Deferred&& deferred) : TestData(std::move(deferred)){}; ~TestDataDelayed() { taskInsts.clear(); }; @@ -109,8 +114,9 @@ struct TestDataDelayed : TestData { std::vector> taskInsts = {}; }; -void FinalizerCallbackDelayed(Napi::Env env, TestDataDelayed *finalizeData, - TestData *) { +void FinalizerCallbackDelayed(Napi::Env env, + TestDataDelayed* finalizeData, + TestData*) { for (size_t i = 0; i < finalizeData->threads.size(); ++i) { finalizeData->threads[i].join(); } @@ -118,14 +124,19 @@ void FinalizerCallbackDelayed(Napi::Env env, TestDataDelayed *finalizeData, delete finalizeData; } -static Value TestDelayedTSFN(const CallbackInfo &info) { +static Value TestDelayedTSFN(const CallbackInfo& info) { int threadCount = info[0].As().Int32Value(); Function cb = info[1].As(); - TestDataDelayed *testData = + TestDataDelayed* testData = new TestDataDelayed(Promise::Deferred::New(info.Env())); - testData->tsfn = TSFN::New(info.Env(), cb, "Test", 0, threadCount, testData, + testData->tsfn = TSFN::New(info.Env(), + cb, + "Test", + 0, + threadCount, + testData, std::function( FinalizerCallbackDelayed), testData); @@ -139,7 +150,7 @@ static Value TestDelayedTSFN(const CallbackInfo &info) { } std::this_thread::sleep_for(std::chrono::milliseconds(std::rand() % 100 + 1)); - for (auto &task : testData->taskInsts) { + for (auto& task : testData->taskInsts) { std::lock_guard lk(task->mtx); task->tsfn = testData->tsfn; task->cv.notify_all(); @@ -148,8 +159,9 @@ static Value TestDelayedTSFN(const CallbackInfo &info) { return testData->deferred.Promise(); } -void AcquireFinalizerCallback(Napi::Env env, TestData *finalizeData, - TestData *context) { +void AcquireFinalizerCallback(Napi::Env env, + TestData* finalizeData, + TestData* context) { (void)context; for (size_t i = 0; i < finalizeData->threads.size(); ++i) { finalizeData->threads[i].join(); @@ -165,8 +177,8 @@ void entryAcquire(TSFN tsfn, int threadId) { tsfn.Release(); } -static Value CreateThread(const CallbackInfo &info) { - TestData *testData = static_cast(info.Data()); +static Value CreateThread(const CallbackInfo& info) { + TestData* testData = static_cast(info.Data()); // Counting expected calls like this only works because on the JS side this // binding is called from a synchronous loop. This means the main loop has no // chance to run the tsfn JS callback before we've counted how many threads @@ -179,21 +191,26 @@ static Value CreateThread(const CallbackInfo &info) { return Number::New(info.Env(), threadId); } -static Value StopThreads(const CallbackInfo &info) { - TestData *testData = static_cast(info.Data()); +static Value StopThreads(const CallbackInfo& info) { + TestData* testData = static_cast(info.Data()); testData->mainWantsRelease = true; return info.Env().Undefined(); } -static Value TestAcquire(const CallbackInfo &info) { +static Value TestAcquire(const CallbackInfo& info) { Function cb = info[0].As(); Napi::Env env = info.Env(); // We pass the test data to the Finalizer for cleanup. The finalizer is // responsible for deleting this data as well. - TestData *testData = new TestData(Promise::Deferred::New(info.Env())); - - testData->tsfn = TSFN::New(env, cb, "Test", 0, 1, testData, + TestData* testData = new TestData(Promise::Deferred::New(info.Env())); + + testData->tsfn = TSFN::New(env, + cb, + "Test", + 0, + 1, + testData, std::function( AcquireFinalizerCallback), testData); @@ -207,7 +224,7 @@ static Value TestAcquire(const CallbackInfo &info) { return result; } -} // namespace +} // namespace Object InitTypedThreadSafeFunctionSum(Env env) { Object exports = Object::New(env); diff --git a/test/typed_threadsafe_function/typed_threadsafe_function_unref.cc b/test/typed_threadsafe_function/typed_threadsafe_function_unref.cc index 3f81611fc..35345568d 100644 --- a/test/typed_threadsafe_function/typed_threadsafe_function_unref.cc +++ b/test/typed_threadsafe_function/typed_threadsafe_function_unref.cc @@ -17,23 +17,31 @@ static Value TestUnref(const CallbackInfo& info) { Function setTimeout = global.Get("setTimeout").As(); TSFN* tsfn = new TSFN; - *tsfn = TSFN::New(info.Env(), cb, resource, "Test", 1, 1, nullptr, [tsfn](Napi::Env /* env */, FinalizerDataType*, ContextType*) { - delete tsfn; - }, static_cast(nullptr)); + *tsfn = TSFN::New( + info.Env(), + cb, + resource, + "Test", + 1, + 1, + nullptr, + [tsfn](Napi::Env /* env */, FinalizerDataType*, ContextType*) { + delete tsfn; + }, + static_cast(nullptr)); tsfn->BlockingCall(); - setTimeout.Call( global, { - Function::New(env, [tsfn](const CallbackInfo& info) { - tsfn->Unref(info.Env()); - }), - Number::New(env, 100) - }); + setTimeout.Call( + global, + {Function::New( + env, [tsfn](const CallbackInfo& info) { tsfn->Unref(info.Env()); }), + Number::New(env, 100)}); return info.Env().Undefined(); } -} +} // namespace Object InitTypedThreadSafeFunctionUnref(Env env) { Object exports = Object::New(env);