Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsfn: Implement copy constructor #546

Merged
merged 13 commits into from
Nov 1, 2019
Merged

Conversation

KevinEady
Copy link
Contributor

Removes the unique_ptr from ThreadSafeFunction, thereby allowing
copies.

Ref: #524

Removes the `unique_ptr` from `ThreadSafeFunction`, thereby allowing
copies.

Ref: nodejs#524
Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should copied ThreadSafeFunction be acquired automatically and requires a release?

@KevinEady
Copy link
Contributor Author

Hi @legendecas ,

So... IMO I don't think it should automatically acquire/release. I believe it adds an assumption that each copy must perform an Acquire, which I think is not true. In fact, I think it would actually cause problems in certain implementations.

Also, if Acquire() returns napi_closing, I would not consider this a fatal error so we shouldn't throw in the copy ctor. How would we handle this? Storing the status on the newly constructed object for the developer to check after copy, eg. in the thread entry point? I dunno, seems clunky...

@gabrielschulhof
Copy link
Contributor

@legendecas IINM passing to another thread may not be the only reason for copying a Napi::ThreadSafeFunction. For example, if you used it as a key in a hash table ...

@gabrielschulhof
Copy link
Contributor

Also IINM it should be easy to subclass Napi::ThreadSafeFunction and add the automatic Acquire()/Release() for those that need it.

@KevinEady
Copy link
Contributor Author

So I have no idea about the ambiguous function calls for New as mentioned in #544 (comment) but I don't think it should block this. If so, I believe this is ready.

@mhdawson
Copy link
Member

mhdawson commented Oct 3, 2019

I agree that the problem related to ambiguous function calls is independent and could be addressed in a follow on PR.

TestData* testData = static_cast<TestData*>(info.Data());
ThreadSafeFunction tsfn = testData->tsfn;
int threadId = testData->threads.size();
testData->threads.push_back( thread(entryAcquire, tsfn, threadId) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be useful to highlight with a comment that this is where the copy constructor is being tested. Since the main change is to add the copy constructor it would be good to make it obvious in the test that it is being used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@NickNaso NickNaso mentioned this pull request Oct 22, 2019
15 tasks
@KevinEady
Copy link
Contributor Author

Hey @mhdawson, can we get this landed so I can continue work on Ref/Unref as well as the incorrect [Non]BlockingCall with ThreadSafeFunction(napi_threadsafe_function) constructor

@mhdawson
Copy link
Member

I'm in the process of landing. It did not seem to compile with master and the older 4.8.5 compiler. That may just be because master need gcc 6, but I want to run a CI on 8.x before landing.

Unfortunately, the CI job is having some issues getting the Node binaries so I'll have to figure that out first.

@mhdawson
Copy link
Member

This seems to fail to build on 8.x with the 4.8.5 compiler level. You can see the compile failures her: https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api-new/nodes=ubuntu1604-64/822/console

@mhdawson
Copy link
Member

@KevinEady can you take a look:

../threadsafe_function/threadsafe_function_sum.cc: In function ‘Napi::Value {anonymous}::TestWithTSFN(const Napi::CallbackInfo&)’:
../threadsafe_function/threadsafe_function_sum.cc:51:4: error: no matching function for call to ‘{anonymous}::TestData::TestData(<brace-enclosed initializer list>)’
   });
    ^
../threadsafe_function/threadsafe_function_sum.cc:12:8: note: candidate: {anonymous}::TestData::TestData(const {anonymous}::TestData&)
 struct TestData {
        ^
../threadsafe_function/threadsafe_function_sum.cc:12:8: note:   no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const {anonymous}::TestData&’

@KevinEady KevinEady requested a review from mhdawson October 24, 2019 19:25
@KevinEady
Copy link
Contributor Author

Hey team,

So I was actually testing this on Windows, and it was failing because defining a move operator implicitly deletes the assignment operator... And, if you think about it, the ThreadSafeFunction wrapper only has for its member data this napi_threadsafe_function which behaves as a pointer: we can use the default compiler-generated functions anyways, right?

test\threadsafe_function\threadsafe_function_sum.cc(90): error C2280: 'Napi::ThreadSafeFunction &Napi::ThreadSafeFunction::operator =(const Napi::ThreadSafeFunction &)': attempting to reference a deleted function [test\build\binding_noexcept.vcxproj]
  napi.h(2088): note: compiler has generated 'Napi::ThreadSafeFunction::operator =' here
  napi.h(2088): note: 'Napi::ThreadSafeFunction &Napi::ThreadSafeFunction::operator =(const Napi::ThreadSafeFunction &)': function was implicitly deleted because 'Napi::ThreadSafeFunction' has a user-defined move constructor (compiling source file ..\threadsafe_function\threadsafe_function_sum.cc)

I did not think it was worthwhile to explicitly create these constructors, nor state them as default in napi.h , so I just removed them. Since this is structural change, I've dismissed your review @mhdawson

@mhdawson
Copy link
Member

@mhdawson
Copy link
Member

@KevinEady
Copy link
Contributor Author

Hi @mhdawson ,

Fixed some issues in the tests that I know were problematic, but this one is really stumping me..

https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api-new/nodes=osx1011/855/console :

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/future:1170:1: error: 'future<Napi::ThreadSafeFunction>' is unavailable: introduced in macOS 10.8
future<_Rp>::get()

I don't know, just looking at the node name os osx1011 ... but is this build targeting osx 10.8 ...?

@KevinEady
Copy link
Contributor Author

Hi @mhdawson / @gabrielschulhof ,

So I re-wrote my tests to use std::condition_variable and std::mutex instead of promises/futures. Let me know if this is suitable now.

@legendecas
Copy link
Member

So I re-wrote my tests to use std::condition_variable and std::mutex instead of promises/futures. Let me know if this is suitable now.

I noticed there are still usages on promises/futures in test/threadsafe_function/threadsafe_function_sum.cc. Are they intended?

@KevinEady
Copy link
Contributor Author

Hi @legendecas ,

I noticed there are still usages on promises/futures in test/threadsafe_function/threadsafe_function_sum.cc. Are they intended?

So I noticed I did not remove the include, which I now replaced with the correct ones for use with condition_variable/mutex.

However, the promise / deferred you see are Napi::Promises, not std library. They are used in the test to ensure proper synchronization. Does this address your concern?

@mhdawson
Copy link
Member

CI run to see if it works on 8.x https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api-new/906/

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mhdawson
Copy link
Member

@legendecas let us know if the latest changes address your concerns and then approve if appropriate. Will wait to hear from you before landing.

Copy link
Member

@legendecas legendecas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall with one nit...

}

inline ThreadSafeFunction::ConvertibleContext
ThreadSafeFunction::GetContext() const {
void* context;
napi_get_threadsafe_function_context(*_tsfn, &context);
napi_get_threadsafe_function_context(_tsfn, &context);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Abort if failed here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the change doesn't change the behavior here, we could address this issue in another PR. Opened issue here #581.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@legendecas I agree, can you open an issue to make sure we fix it later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhdawson the issue is above, and PR for fix (which is also conflicting with this PR) is #583

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KevinEady - oops should have read more carefullly. Thanks.

@mhdawson
Copy link
Member

mhdawson commented Nov 1, 2019

Landed as 2e71842

kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
* tsfn: Implement copy constructor

Refs: nodejs/node-addon-api#524

PR-URL:  nodejs/node-addon-api#546
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
* tsfn: Implement copy constructor

Refs: nodejs/node-addon-api#524

PR-URL:  nodejs/node-addon-api#546
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
* tsfn: Implement copy constructor

Refs: nodejs/node-addon-api#524

PR-URL:  nodejs/node-addon-api#546
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
* tsfn: Implement copy constructor

Refs: nodejs/node-addon-api#524

PR-URL:  nodejs/node-addon-api#546
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants