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

Make main thread check adjustible in case of jna lib #4099

Open
szsoftware opened this issue Jan 29, 2025 · 4 comments
Open

Make main thread check adjustible in case of jna lib #4099

szsoftware opened this issue Jan 29, 2025 · 4 comments
Labels
DS - wayland S - enhancement Wouldn't this be the coolest?

Comments

@szsoftware
Copy link

szsoftware commented Jan 29, 2025

Description

Hi there,
I have this use case:

transitive winit dependency in a rust library, consumed by my cdylib libexample.so.

This exports uniffi bindings for Kotlin.

Consuming Kotlin project is console or Swing application, but let's stick here on example console app with no other UI parts, except rust windowing.

libexample.so is loaded with jna.

OS is Ubuntu/Linux with openjdk.

Unfortunately, even no threading or coroutines are involved on no side, the native lib runs always in a second, consecutive thread having always a 3 numbers higher pid/tid than the main thread.
Here we talk about two java os-threads.

Therefore os thread checking like if(rustix::thread::id == rustix::process::id) will fail.

For my Kotlin Application everything runs on the java main thread.

Meanwhile I patched the winit crate locally and just return true in is_main_thread().
Everything runs smooth with that hack.

My question is: Can we consider above usecases somehow?
Can we have a feature or other Cargo.toml configuration option to circumvent other solutions like EventloopBuilder/extension/hooks (with_any_thread)?

The thing is, I want to be agnostic of wayland or whatever target and just want to use transitive code or examples as provided.

Thank you so much for your help and understanding!

P.S.: On which platforms is this feature relevant?
I would suggest to put "Linux" and "Jna/Jni" in the list.
I find the mechanism/use case is similar to Android architecture, but... It's not Android.

Relevant platforms

No response

@szsoftware szsoftware added the S - enhancement Wouldn't this be the coolest? label Jan 29, 2025
@madsmtm
Copy link
Member

madsmtm commented Jan 30, 2025

You mean, you're compiling for Android, but running the code on Linux?

@szsoftware
Copy link
Author

No, I compile for x86_64 Linux target.

The consuming project of the Rust library is a Kotlin/Java desktop or console project.

No Android involved.

@kchibisov
Copy link
Member

if it's linux, then use new_any_thread api.

Though, I do agree that this main thread check really only matters on macOS/ios.

@szsoftware
Copy link
Author

Okay, I looked again over with_any_thread api and adapted my code.
I was confused about to choose x11 or wayland and I'm still unsure which extension to use.
During my learning and hacking journey I noticed
winit::platform::unix::EventLoopBuilderExtUnix but lateron, probably in a newer version of winit, that was'nt resolved anymore.

Thanks for hints and comments!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DS - wayland S - enhancement Wouldn't this be the coolest?
Development

No branches or pull requests

3 participants