I encountered the following issue with Ubuntu Linux/openjdk/Kotlin/Rust development:
I'm loading a uniffied Rust libexample.so in the main thread of my console application.
So far I thought and learned, that native code loaded in the main thread is executed in the main thread.
But, after a lot of examination on the Rust, the Java/Kotlin and the os side, I found out that the NativeLoading procedures produces a second os-thread for the java process, always consecutive to the main thread and with an offset of 4.
On the rust side, this has implications: windowing (winit) does a main thread check and it will fail because processId != threadId, and we all know that the main threadId = processId.
Having the library running on another thread can also have implications for OpenGl context sharing or things like this.
However, is it possible to omit a second jna thread and let the library functions execute in exactly the context of the java main thread (and it's OS counterpart)?
Thank you for some clarification!
I encountered the following issue with Ubuntu Linux/openjdk/Kotlin/Rust development:
...
But, after a lot of examination on the Rust, the Java/Kotlin and the os side, I found out that the NativeLoading procedures produces a second os-thread for the java process, always consecutive to the main thread and with an offset of 4.
On the rust side, this has implications: windowing (winit) does a main thread check and it will fail because processId != threadId, and we all know that the main threadId = processId.
Having the library running on another thread can also have implications for OpenGl context sharing or things like this.
However, is it possible to omit a second jna thread and let the library functions execute in exactly the context of the java main thread (and it's OS counterpart)?
as far as I understand this option right, then this is about on which OS-Thread runs the first Java Thread of an Application.
My question is about why my native library is not running in the first OS-Thread in which runs my first/Main Java Thread.
matthias@enterprise:~$ sudo /usr/lib/jvm/java-17-openjdk-amd64/bin/jhsdb clhsdb --pid 427997
Attaching to process 427997, please wait...
hsdb> threads
427999 main
State: IN_JAVA
Stack in use by Java: 0x00007212eabfe928 .. 0x00007212eabfe9d8
Base of Stack: 0x00007212eac00000
Last_Java_SP: null
Last_Java_FP: null
Last_Java_PC: null
Thread id: 427999
...
428007 Reference Handler
State: BLOCKED
Stack in use by Java: 0x00007212bc92b910 .. 0x00007212bc92ba58
Base of Stack: 0x00007212bc92d000
Last_Java_SP: 0x00007212bc92b910
Last_Java_FP: 0x00007212bc92b960
Last_Java_PC: 0x00007212d486c5ab
Thread id: 428007
...
428008 Finalizer
State: BLOCKED
Stack in use by Java: 0x00007212bc82b840 .. 0x00007212bc82ba48
Base of Stack: 0x00007212bc82d000
Last_Java_SP: 0x00007212bc82b840
Last_Java_FP: 0x00007212bc82b8a0
Last_Java_PC: 0x00007212d486c5ab
Thread id: 428008
...
428009 Signal Dispatcher
State: BLOCKED
No Java frames present
Base of Stack: 0x00007212bc72d000
Last_Java_SP: null
Last_Java_FP: null
Last_Java_PC: null
Thread id: 428009
...
428010 Service Thread
State: BLOCKED
No Java frames present
Base of Stack: 0x00007212bc62d000
Last_Java_SP: null
Last_Java_FP: null
Last_Java_PC: null
Thread id: 428010
...
428011 Monitor Deflation Thread
State: BLOCKED
No Java frames present
Base of Stack: 0x00007212bc52d000
Last_Java_SP: null
Last_Java_FP: null
Last_Java_PC: null
Thread id: 428011
...
428012 C2 CompilerThread0
State: BLOCKED
No Java frames present
Base of Stack: 0x00007212bc42d000
Last_Java_SP: null
Last_Java_FP: null
Last_Java_PC: null
Thread id: 428012
...
428013 C1 CompilerThread0
State: BLOCKED
No Java frames present
Base of Stack: 0x00007212bc32d000
Last_Java_SP: null
Last_Java_FP: null
Last_Java_PC: null
Thread id: 428013
...
428014 Sweeper thread
State: BLOCKED
No Java frames present
Base of Stack: 0x00007212bc22d000
Last_Java_SP: null
Last_Java_FP: null
Last_Java_PC: null
Thread id: 428014
...
428015 Notification Thread
State: BLOCKED
No Java frames present
Base of Stack: 0x00007212bc12d000
Last_Java_SP: null
Last_Java_FP: null
Last_Java_PC: null
Thread id: 428015
...
428017 Common-Cleaner
State: BLOCKED
Stack in use by Java: 0x00007212b58fd7d0 .. 0x00007212b58fda58
Base of Stack: 0x00007212b58ff000
Last_Java_SP: 0x00007212b58fd7d0
Last_Java_FP: 0x00007212b58fd838
Last_Java_PC: 0x00007212d486c5ab
Thread id: 428017
...
428019 JNA Cleaner
State: BLOCKED
Stack in use by Java: 0x00007212b56fd870 .. 0x00007212b56fda28
Base of Stack: 0x00007212b56ff000
Last_Java_SP: 0x00007212b56fd870
Last_Java_FP: 0x00007212b56fd8d0
Last_Java_PC: 0x00007212d486c5ab
Thread id: 428019
...
--
You received this message because you are subscribed to the Google Groups "Java Native Access" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jna-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jna-users/4236968d-b173-4dea-a538-704248056302n%40googlegroups.com.
thank you for the sample. And indeed you can verify what Neil already suggested. This is the thread dump generated by clhsdb (commandline debugger for hotspot).As you can see I attach to the process 427997, but the main thread is on thread id 427999 (so while I see a different offset, it matches you description):