-
Notifications
You must be signed in to change notification settings - Fork 462
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
Implement ThreadSafeFunction class #442
Conversation
@gabrielschulhof would be good if you took a look at this one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is still WIP AFAICT.
a78aff8
to
78b68be
Compare
@gabrielschulhof, @mhdawson PTAL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should ThreadSafeFunction::New
have multiple overloaded methods? According to the docs, async_resource
, thread_finalize_data
/thread_finalize_cb
, and context
are optional, so I would think ThreadSafeFunction::New
would have overloads allowing me to bypass them.
@romandev I'm ok with the docs being in a different PR, but I'd prefer if the two were landed together as opposed to landing one without the other. That might make it difficult to do a release as I think we want to have both in place before having the functionality go out in a release. |
Is there anything I can do to help with this PR? Maybe docs or something? I have taken a look, and have been successfully using @romandev 's fork in my own project now. |
@KevinEady maybe you can start writing a draft to document this new api. What do you |
Hi @NickNaso , that sounds good. I have started on the docs, and I noticed this piece of code in the threadsafe_function.cc: This got me thinking about the context handling, and I noticed that we pass to |
I'm working on templating the ThreadSafeFunction class with the Context, and a no-Context tsfn will have I am also adding overloaded methods for If these API changes make sense, then that'll be what I'll document. |
78b68be
to
f04ba10
Compare
@romandev So I did the work on templating the And it's used in the test class like https://github.com/KevinEady/node-addon-api/blob/8fc51e5cbeee30c6cdeac3fa0bc48c7cad9ef6d0/test/threadsafe_function/threadsafe_function.cc What are your thoughts? |
I'm currently working on the documentation https://github.com/KevinEady/node-addon-api/blob/threadfunction-doc/doc/threadsafe_function.md |
napi-inl.h
Outdated
size_t maxQueueSize, | ||
size_t initialThreadCount) { | ||
return New(env, callback, Object(), resourceName, maxQueueSize, | ||
initialThreadCount, nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nullptr
used here (and elsewhere) is ambiguous so shows up as mismatched types ‘Context*’ and ‘std::nullptr_t’
when the intended overload is considered as a candidate
apparently need an explicitly overload with std::nullptr_t
to resolve it, or I suppose cast it
Also, the whole thing probably needs to be wrapped in a NAPI_VERSION check, explodes on 10.6 without declaring NAPI_EXPERIMENTAL. |
@DuBistKomisch it probably needs a NAPI version 1.4 check instead as I believe the N-API methods it uses are non-experimental and that would check would exclude it from the earlier versions until we have the backports PRs landed. |
Although maybe that is what you suggested :) |
yeah I'm no n-api expert but the node docs page doesn't seem to list a version under these thread safe function methods |
hmm, we seem to be missing that info for those methods. I'll look at adding it as they should show as version 4. |
f04ba10
to
5fc684d
Compare
@romandev will you be implementing a |
@KevinEady If you're already implementing GetContext(), I'll leave a FIXME comment and then you can land your patch in follow-up PR. If you'd like to include the GetContext() implementation in this patch, I'll continue the work. Basically, I wanted to implement GetContext without explicit template. (But I'm not sure if it is possible or not.) |
@romandev I think if we should throw when one attempts to assign to a non-empty |
@gabrielschulhof, In fact, I already did that here: https://github.com/nodejs/node-addon-api/pull/442/files#diff-f546e4cc35b454813e5264fe9c9e6fc8R3898 However, I think we still need a way to reset the For example, in the current test, declare If there was no such logic, we would have to have a static |
@romandev a policy of "You cannot assign to a |
@@ -0,0 +1,185 @@ | |||
#include <uv.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we want a big comment here that says this using uv is not guaranteed to be ABI stable. That if you want to use the stability guarantee from N-API you need to use other means to implement threads. This is the first example where we have used uv in the testing and we want it to be clear that it is not what we recommend and that we only do it because we know the test suite will be build/run with Node.js as opposed to being built into a binary that will run across different Node.js versions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm ... we could use std::thread
instead, right? I think all our supported compilers now have C++11, right? In the test in core I used libuv because, well, we maintain core ourselves, and because in core I seek to keep N-API tests written in C as much as possible. @romandev could you convert this to std::thread
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM after the comment I suggested is added.
FWIW, I used `std::thread` in the example in the docs, so yeah makes sense
to standardize.
…On Fri, Jun 28, 2019, 7:27 PM Gabriel Schulhof ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In test/threadsafe_function/threadsafe_function.cc
<#442 (comment)>:
> @@ -0,0 +1,185 @@
+#include <uv.h>
Hmmm ... we could use std::thread instead, right? I think all our
supported compilers now have C++11, right? In the test in core I used libuv
because, well, we maintain core ourselves, and because in core I seek to
keep N-API tests written in C as much as possible. @romandev
<https://github.com/romandev> could you convert this to std::thread?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#442?email_source=notifications&email_token=ACB4EIAFPSHSH63NBZLVXGTP4ZCWTA5CNFSM4GUNSRX2YY3PNVWWK3TUL52HS4DFWFIHK3DMKJSXC5LFON2FEZLWNFSXPKTDN5WW2ZLOORPWSZGOB5AIP6A#discussion_r298683140>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACB4EIGY5DYJY2NU5S4WHFTP4ZCWTANCNFSM4GUNSRXQ>
.
|
... and I'm actually thinking of using C11's |
Well, it looks like |
@gabrielschulhof Done. Thanks. |
This PR is implementing ThreadSafeFunction class wraps napi_threadsafe_function features. FYI, the test files that included in this PR have come from Node.js repo[1]. They've been rewritten based on C++ and node-addon-api. Fixes nodejs#312. [1] https://github.com/nodejs/node/tree/master/test/node-api/test_threadsafe_function
FYI, I got rid of std::make_unique from latest patch because the make_unique() is C++ 14 feature.. |
New CI:
*edit: dropped non-LTS. |
Hi @gabrielschulhof , Looks like the node-9 tests are failing because the napi tsfn functions don't exist. Were tsfns backported to node 9? Don't see them listed on the n-api docs. |
This PR is implementing ThreadSafeFunction class wraps napi_threadsafe_function features. FYI, the test files that included in this PR have come from Node.js repo[1]. They've been rewritten based on C++ and node-addon-api. [1] https://github.com/nodejs/node/tree/e800f9d/test/node-api/test_threadsafe_function PR-URL: #442 Fixes: #312 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
This PR is implementing ThreadSafeFunction class wraps napi_threadsafe_function features. FYI, the test files that included in this PR have come from Node.js repo[1]. They've been rewritten based on C++ and node-addon-api. [1] https://github.com/nodejs/node/tree/e800f9d/test/node-api/test_threadsafe_function PR-URL: nodejs/node-addon-api#442 Fixes: nodejs/node-addon-api#312 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
This PR is implementing ThreadSafeFunction class wraps napi_threadsafe_function features. FYI, the test files that included in this PR have come from Node.js repo[1]. They've been rewritten based on C++ and node-addon-api. [1] https://github.com/nodejs/node/tree/e800f9d/test/node-api/test_threadsafe_function PR-URL: nodejs/node-addon-api#442 Fixes: nodejs/node-addon-api#312 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
This PR is implementing ThreadSafeFunction class wraps napi_threadsafe_function features. FYI, the test files that included in this PR have come from Node.js repo[1]. They've been rewritten based on C++ and node-addon-api. [1] https://github.com/nodejs/node/tree/e800f9d/test/node-api/test_threadsafe_function PR-URL: nodejs/node-addon-api#442 Fixes: nodejs/node-addon-api#312 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
This PR is implementing ThreadSafeFunction class wraps napi_threadsafe_function features. FYI, the test files that included in this PR have come from Node.js repo[1]. They've been rewritten based on C++ and node-addon-api. [1] https://github.com/nodejs/node/tree/e800f9d/test/node-api/test_threadsafe_function PR-URL: nodejs/node-addon-api#442 Fixes: nodejs/node-addon-api#312 Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Gabriel Schulhof <gabriel.schulhof@intel.com>
This PR is implementing ThreadSafeFunction class wraps
napi_threadsafe_function features.
FYI, the test files that included in this PR have come from Node.js
repo[1]. They've been rewritten based on C++ and node-addon-api.
Fixes #312.
[1] https://github.com/nodejs/node/tree/master/test/node-api/test_threadsafe_function