Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
78196e0
tsfn: implement ThreadSafeFunctionEx<ContextType>
KevinEady Mar 22, 2020
90ebd80
fix unused parameter errors
KevinEady Mar 22, 2020
fc339c4
wip
KevinEady Mar 31, 2020
fd530b2
wip
KevinEady Apr 7, 2020
561b968
make CallJS template parameter
KevinEady Apr 7, 2020
630d88b
statically check CallJs; add no-Finalizer overload
KevinEady Apr 8, 2020
9ff352c
add tsfn test with tsfn cb function call
KevinEady Apr 17, 2020
6aeecd4
Merge remote-tracking branch 'upstream/master' into contexted-tsfn-api
KevinEady Apr 17, 2020
2da3023
test: Initial commit of TSFNEx threadsafe test
KevinEady Jun 8, 2020
91e8859
test: Modify TSFNEx threadsafe to use TSFNEx
KevinEady Jun 8, 2020
692fbe1
src,test: Add SIGTRAP to TSFNEx::CallJS
KevinEady Jun 8, 2020
0d26703
Merge remote-tracking branch 'upstream/master' into contexted-tsfn-ap…
KevinEady Jun 8, 2020
89181da
test: fix TSFNEx tests
KevinEady Jun 12, 2020
8fcdb4d
test: add barebones new tsfn test for use in docs
KevinEady Jun 12, 2020
cc8de12
implement optional function callback
KevinEady Jun 13, 2020
6706f96
clean up optional callback implementation
KevinEady Jun 13, 2020
89d2dea
test: wip with tsfnex tests
KevinEady Jun 14, 2020
d2bcc03
test: napi v4,v5 all tests pass
KevinEady Jun 14, 2020
6b7a7d0
test: consolidate duplicated code
KevinEady Jun 14, 2020
7467a3f
test: basic example, standardize identifier names
KevinEady Jun 14, 2020
a01c3c8
test: refactor the the 'empty' tsfnex test
KevinEady Jun 14, 2020
b0e7817
basic multi-threading
KevinEady Jun 14, 2020
44adeea
test: wip with example
KevinEady Jun 14, 2020
c20685b
test: wip with example test
KevinEady Jun 14, 2020
3b24e74
src: consolidate duplicated tsfnex code
KevinEady Jun 15, 2020
a7a5352
test: v4,v5+ tests pass
KevinEady Jun 15, 2020
d092a32
doc: wip with tsfn documentation
KevinEady Jun 17, 2020
75dd422
doc,test: finish TSFNEx
KevinEady Jun 21, 2020
717b602
Merge remote-tracking branch 'upstream/master' into contexted-tsfn-ap…
KevinEady Jul 13, 2020
0d84cf7
Test with longer timeout
KevinEady Jul 27, 2020
c8eea6b
Merge remote-tracking branch 'upstream/master' into contexted-tsfn-ap…
KevinEady Jul 27, 2020
807fb27
Apply suggestions from code review
KevinEady Aug 9, 2020
f3aa955
Merge remote-tracking branch 'upstream/master' into contexted-tsfn-ap…
KevinEady Aug 9, 2020
fd6a2b4
Additional changes from review
KevinEady Aug 9, 2020
ad9333e
test: tsfnex uses ported tsfn tests
KevinEady Aug 18, 2020
e223955
doc: tsfnex example uses ported tsfn example
KevinEady Aug 18, 2020
3405b2b
src,doc: final cleanup
KevinEady Aug 18, 2020
151a914
Apply documentation suggestions from code review
KevinEady Oct 9, 2020
59f27da
Fix common.gypi
KevinEady Oct 10, 2020
b54f5eb
Additional changes from review
KevinEady Oct 10, 2020
7ec9741
Merge remote-tracking branch 'upstream/master' into contexted-tsfn-ap…
KevinEady Oct 29, 2020
7a13f86
doc: fix additional typo
KevinEady Oct 29, 2020
4abe7cf
test: rename tsfnex test files
KevinEady Nov 6, 2020
c24c455
Rename to TypedThreadSafeFunction
KevinEady Nov 24, 2020
559ad8c
Merge remote-tracking branch 'upstream/master' into contexted-tsfn-ap…
KevinEady Nov 24, 2020
5e5b9ce
Apply formatting changes
KevinEady Nov 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
KevinEady committed Mar 31, 2020
commit fc339c4c46ec2d618d66a715bee9d1a5e7157514
31 changes: 28 additions & 3 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4272,7 +4272,7 @@ inline ThreadSafeFunctionEx<ContextType> ThreadSafeFunctionEx<ContextType>::New(
ContextType* context,
Finalizer finalizeCallback,
FinalizerDataType* data,
napi_threadsafe_function_call_js call_js_cb) {
ThreadSafeFunctionCallJS call_js_cb) {
return New(env, callback, resource, resourceName, maxQueueSize,
initialThreadCount, context, finalizeCallback, data,
details::ThreadSafeFinalize<ContextType, Finalizer,
Expand Down Expand Up @@ -4362,7 +4362,7 @@ inline ThreadSafeFunctionEx<ContextType> ThreadSafeFunctionEx<ContextType>::New(
Finalizer finalizeCallback,
FinalizerDataType* data,
napi_finalize wrapper,
napi_threadsafe_function_call_js call_js_cb) {
ThreadSafeFunctionCallJS call_js_cb) {
static_assert(details::can_make_string<ResourceString>::value
|| std::is_convertible<ResourceString, napi_value>::value,
"Resource name should be convertible to the string type");
Expand All @@ -4372,7 +4372,15 @@ inline ThreadSafeFunctionEx<ContextType> ThreadSafeFunctionEx<ContextType>::New(
FinalizerDataType>({ data, finalizeCallback });
napi_status status = napi_create_threadsafe_function(env, callback, resource,
Value::From(env, resourceName), maxQueueSize, initialThreadCount,
finalizeData, wrapper, context, call_js_cb, &tsfn._tsfn);
finalizeData, wrapper, context,
// [=](napi_env env, napi_value jsCallback, void* context, void* data) {
// if (env == nullptr && jsCallback == nullptr) {
// return;
// }
// call_js_cb( Napi::Env(env), Function(env, jsCallback), static_cast<ContextType*>(context), data);
// },
CallJS,
&tsfn._tsfn);
if (status != napi_ok) {
delete finalizeData;
NAPI_THROW_IF_FAILED(env, status, ThreadSafeFunctionEx<ContextType>());
Expand All @@ -4381,6 +4389,23 @@ inline ThreadSafeFunctionEx<ContextType> ThreadSafeFunctionEx<ContextType>::New(
return tsfn;
}

template <typename ContextType>
inline void
ThreadSafeFunctionEx<ContextType>::CallJS(napi_env env, napi_value jsCallback,
void *context, void *data) {
if (env == nullptr && jsCallback == nullptr) {
return;
}

if (data != nullptr) {
auto* callbackWrapper = static_cast<CallbackWrapper*>(data);
(*callbackWrapper)(env, Function(env, jsCallback));
delete callbackWrapper;
} else if (jsCallback != nullptr) {
Function(env, jsCallback).Call({});
}
}

////////////////////////////////////////////////////////////////////////////////
// ThreadSafeFunction class
////////////////////////////////////////////////////////////////////////////////
Expand Down
22 changes: 16 additions & 6 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2037,9 +2037,13 @@ namespace Napi {

#if (NAPI_VERSION > 3)

template <typename ContextType>

template <typename ContextType, typename DataType>
class ThreadSafeFunctionEx {
public:

using ThreadSafeFunctionCallJS = std::function<void(Napi::Env, Napi::Function, ContextType *context, DataType *data)>;

// This API may only be called from the main thread.
template <typename ResourceString, typename Finalizer, typename FinalizerDataType>
static ThreadSafeFunctionEx<ContextType> New(napi_env env,
Expand All @@ -2051,7 +2055,7 @@ namespace Napi {
ContextType* context,
Finalizer finalizeCallback,
FinalizerDataType* data,
napi_threadsafe_function_call_js call_js_cb);
ThreadSafeFunctionCallJS call_js_cb);

ThreadSafeFunctionEx<ContextType>();
ThreadSafeFunctionEx<ContextType>(napi_threadsafe_function tsFunctionValue);
Expand All @@ -2062,14 +2066,12 @@ namespace Napi {
// napi_status BlockingCall() const;

// This API may be called from any thread.
template <typename DataType = void>
napi_status BlockingCall(DataType* data = nullptr) const;

// // This API may be called from any thread.
// napi_status NonBlockingCall() const;

// This API may be called from any thread.
template <typename DataType = void>
napi_status NonBlockingCall(DataType* data = nullptr) const;

// This API may only be called from the main thread.
Expand All @@ -2091,7 +2093,7 @@ namespace Napi {
ContextType* GetContext() const;

private:
using CallbackWrapper = std::function<void(Napi::Env, Napi::Function, ContextType* context)>;
// using CallbackWrapper = std::function<void(Napi::Env, Napi::Function, ContextType* context)>;

template <typename ResourceString,
typename Finalizer, typename FinalizerDataType>
Expand All @@ -2105,7 +2107,15 @@ namespace Napi {
Finalizer finalizeCallback,
FinalizerDataType* data,
napi_finalize wrapper,
napi_threadsafe_function_call_js call_js_cb);
ThreadSafeFunctionCallJS call_js_cb);

static void CallJS(napi_env env,
napi_value jsCallback,
void* context,
void* data);

protected:
void CallJS

napi_threadsafe_function _tsfn;
};
Expand Down
4 changes: 2 additions & 2 deletions test/threadsafe_function/threadsafe_function_ex.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ TSFNWrap::TSFNWrap(const CallbackInfo &info)
delete ctx;
},
static_cast<void*>(nullptr), // FinalizerDataType* data,
[](napi_env, napi_value, void *context, void *data) { // call_js_cb
[](Napi::Env, Napi::Value, TSFNContext *context, void *data) { // call_js_cb
std::unique_ptr<CallJsData> callData(static_cast<CallJsData*>(data));
callData->resolve(static_cast<TSFNContext*>(context));
callData->resolve(context);
});
}
} // namespace
Expand Down