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

problem when using a FFI 'C' callback function #40529

Closed
AlexBelchUK opened this issue Feb 7, 2020 · 7 comments
Closed

problem when using a FFI 'C' callback function #40529

AlexBelchUK opened this issue Feb 7, 2020 · 7 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi

Comments

@AlexBelchUK
Copy link

I'm on dart 2.7.1 Windows 10 - I'm getting a problem when using a FFI 'C' callback function.
I tried to pass a function pointer to a 'C' method that uses a function call back
When to callback is performed, I'm getting an error message from dart c:\b\s\w\ir\cache\builder\sdk\runtime\vm\runtime_entry.cc: 3217: error: Cannot invoke native callback outside an isolate.
I've tried using an isolate in the registration call but the error still happens

I've seen in issue 37301 that someone has done a callback...

I was wondering if the patch was in 2.7.1 or still on a dev branch ?
Can FFI 'C' callbacks to dart be done yet ?

@vsmenon vsmenon added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-ffi area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. labels Feb 7, 2020
@dcharkes
Copy link
Contributor

Can FFI 'C' callbacks to dart be done yet ?

Yes.

From which thread are you trying to do a callback? Is this the Dart thread, or are you trying to do a callback from another thread?

@AlexBelchUK
Copy link
Author

I'm trying the callback from another thread in 'C' outside the Dart thread.
How does the error 'Cannot invoke native callback outside an isolate.'
happen ?

@dcharkes
Copy link
Contributor

Yes, doing a callback from another thread is not supported. Dart has one mutator thread per isolate. If a Dart thread is already running in the isolate, and you would call back from C into Dart with another thread, there would be 2 threads executing Dart code in a single isolate. That would break the concurrency assumptions.

Instead, you have to call back into Dart on the main Dart thread. One can notify the main Dart thread asynchronously through a SendPort. Please check this sample: https://dart-review.googlesource.com/c/sdk/+/134704.

@AlexBelchUK
Copy link
Author

Thanks for the help, I'll follow the example and add adaptor functions to register the port and then to receive callbacks on the 'C' side. I'll use the Dart send message 'C' functions to pass parameter messages into the Dart isolate. Thanks again.

@shuisheng
Copy link

shuisheng commented Feb 7, 2023

hi @dcharkes,
I have dart client and call c++ dll
I use async callback via port, pass data to dart from c++ thread with Dart_PostCObject.
after I compiled c++ code, then open c++ dll in dart, prompt cannot open the c++ dll
1675741052695

then when I remove the call to Dart_PostCObject, that issue not occur

@shuisheng
Copy link

It's my mistake, I should use Dart_PostCObject_DL instead of Dart_PostCObject. now it can work

@houzhj58
Copy link

houzhj58 commented Dec 5, 2023

since dart 3.1, can use NativeCallable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-ffi
Projects
None yet
Development

No branches or pull requests

5 participants