-
Notifications
You must be signed in to change notification settings - Fork 551
[Mono.Android] JNI handles are now in a "control block" #10179
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
Draft
jonathanpeppers
wants to merge
6
commits into
main
Choose a base branch
from
dev/peppers/control-block
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Originally from: #10125 Context: dotnet/java-interop#1339 These are changes to Mono's GC bridge implementation, such that the same code can be used on both Mono and CoreCLR. The base `JavaObject` type has changes such as: ```diff --[NonSerialized] IntPtr handle; --[NonSerialized] JniObjectReferenceType handle_type; --[NonSerialized] IntPtr weak_handle; --[NonSerialized] int refs_added; ++unsafe JniObjectReferenceControlBlock* jniObjectReferenceControlBlock; ``` And the new `JniObjectReferenceControlBlock` struct is defined as: internal struct JniObjectReferenceControlBlock { public IntPtr handle; public int handle_type; public IntPtr weak_handle; public int refs_added; } * Each `JavaObject` allocation now has a `NativeMemory.AllocZeroed()` call for this struct. * However, we only have to read a single field now using Mono's native embedding API: ```diff --info->handle = mono_class_get_field_from_name (info->klass, const_cast<char*> ("handle")); --info->handle_type = mono_class_get_field_from_name (info->klass, const_cast<char*> ("handle_type")); --info->refs_added = mono_class_get_field_from_name (info->klass, const_cast<char*> ("refs_added")); --info->weak_handle = mono_class_get_field_from_name (info->klass, const_cast<char*> ("weak_handle")); ++info->jniObjectReferenceControlBlock = mono_class_get_field_from_name (info->klass, const_cast<char*> ("jniObjectReferenceControlBlock")); ``` We are hoping this results in ~the same performance as before, with the flexibility to support multiple GC bridge implementations and runtimes. Co-authored-by: Marek Habersack <[email protected]>
grendello
approved these changes
Jun 9, 2025
This is close now, but crashes partway through test suite:
Some of the configurations didn't crash. |
We were using it incorrectly - there's no need to fetch/set values of managed object fields. The correct thing to do is to read/write them on the control block itself.
Conflicts: external/Java.Interop
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Originally from: #10125
Context: dotnet/java-interop#1339
These are changes to Mono's GC bridge implementation, such that the same code can be used on both Mono and CoreCLR.
The base
JavaObject
type has changes such as:And the new
JniObjectReferenceControlBlock
struct is defined as:Each
JavaObject
allocation now has aNativeMemory.AllocZeroed()
call for this struct.However, we only have to read a single field now using Mono's native embedding API:
We are hoping this results in ~the same performance as before, with the flexibility to support multiple GC bridge implementations and runtimes.