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

Java: Sync callback #388

Open
kwek20 opened this issue May 24, 2021 · 2 comments · Fixed by iotaledger/iota.rs#919
Open

Java: Sync callback #388

kwek20 opened this issue May 24, 2021 · 2 comments · Fixed by iotaledger/iota.rs#919
Labels
enhancement Java/JNI Case specific only for Java/JNI interface generation question

Comments

@kwek20
Copy link

kwek20 commented May 24, 2021

Im trying to interact with an MQTT stream from the rust side, but it only accepts <Send + Sync + 'static>

When trying to add the trait to my callback; like so:
fn MqttTopicManager::subscribe(&self, cb: Box<dyn MqttListener + Send + Sync + 'static>) -> Result<()>;

I receive the following error below.

Is this at all possible or do I need to find another way to do this?

rror[E0277]: `*mut *const jni_c_header::JNIInvokeInterface_` cannot be shared between threads safely
    --> /home/java/target/debug/build/client-db63cdf19f965487/out/java_glue.rs:5948:9
     |
5948 |         Box::new(cb)
     |         ^^^^^^^^^^^^ `*mut *const jni_c_header::JNIInvokeInterface_` cannot be shared between threads safely
     |
     = help: within `JavaCallback`, the trait `Sync` is not implemented for `*mut *const jni_c_header::JNIInvokeInterface_`
     = note: required because it appears within the type `JavaCallback`
     = note: required for the cast to the object type `dyn classes::mqtt::MqttListener + Sync + std::marker::Send`

error[E0277]: `*mut jni_c_header::_jobject` cannot be shared between threads safely
    --> /home/java/target/debug/build/client-db63cdf19f965487/out/java_glue.rs:5948:9
     |
5948 |         Box::new(cb)
     |         ^^^^^^^^^^^^ `*mut jni_c_header::_jobject` cannot be shared between threads safely
     |
     = help: within `JavaCallback`, the trait `Sync` is not implemented for `*mut jni_c_header::_jobject`
     = note: required because it appears within the type `JavaCallback`
     = note: required for the cast to the object type `dyn classes::mqtt::MqttListener + Sync + std::marker::Send`

error[E0277]: `*mut jni_c_header::_jmethodID` cannot be shared between threads safely
    --> /home/java/target/debug/build/client-db63cdf19f965487/out/java_glue.rs:5948:9
     |
5948 |         Box::new(cb)
     |         ^^^^^^^^^^^^ `*mut jni_c_header::_jmethodID` cannot be shared between threads safely
     |
     = help: the trait `Sync` is not implemented for `*mut jni_c_header::_jmethodID`
     = note: required because of the requirements on the impl of `Sync` for `Unique<*mut jni_c_header::_jmethodID>`
     = note: required because it appears within the type `alloc::raw_vec::RawVec<*mut jni_c_header::_jmethodID>`
     = note: required because it appears within the type `Vec<*mut jni_c_header::_jmethodID>`
     = note: required because it appears within the type `JavaCallback`
     = note: required for the cast to the object type `dyn classes::mqtt::MqttListener + Sync + std::marker::Send`
@Dushistov Dushistov added enhancement Java/JNI Case specific only for Java/JNI interface generation question and removed enhancement labels Oct 6, 2021
@Dushistov
Copy link
Owner

You can add

unsafe impl Sync for JavaCallback {}

to fix compilation error.
But I am not sure how "safe" it is, this is depend on how you implement subscribe method in Java.
If this method is ready to be called from several threads at the same time, then it should be safe.

@Dushistov
Copy link
Owner

What would be nice, is the ability to make JavaCallback Sync only for specific traits, not for all at the same time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Java/JNI Case specific only for Java/JNI interface generation question
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants