Skip to content

Commit ad451f9

Browse files
jonpryorgrendello
authored andcommitted
Try using the managed GC bridge.
dotnet publish --self-contained -p:UseMonoRuntime=true -p:DotNetTargetFramework=net8.0 -p:UseAppHost=true -p:ErrorOnDuplicatePublishOutputFiles=false samples/Hello-Java.Base/Hello-Java.Base.csproj -r osx-x64 && \ JAVA_INTEROP_GREF_LOG=g.txt ./samples/Hello-Java.Base/bin/Release/osx-x64/publish/Hello-Java.Base Does it work? No quite. What's present here "works", in that the managed `MarkCrossReferences` callback *is* invoked, which just keeps all instances alive. However, it *doesn't* fully work, for reasons I don't understand: if `MarkCrossReferences` calls managed code, e.g. `Console.WriteLine("here!")`, then it *hangs*. Looks like this prototype approach can't work.
1 parent 7174669 commit ad451f9

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/Java.Runtime.Environment/Java.Interop/MonoRuntimeValueManager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum GCBridgeUseWeakReferenceKind {
1414
Jni,
1515
}
1616

17-
class MonoRuntimeValueManager : JniRuntime.JniValueManager {
17+
partial class MonoRuntimeValueManager : JniRuntime.JniValueManager {
1818

1919
#pragma warning disable 0649
2020
// This field is mutated by the java-interop native lib
@@ -46,6 +46,10 @@ public override void OnSetRuntime (JniRuntime runtime)
4646
throw new NotSupportedException ("Could not register current AppDomain!");
4747
if (JreNativeMethods.java_interop_gc_bridge_set_current_once (bridge) < 0)
4848
throw new NotSupportedException ("Could not set GC Bridge instance!");
49+
unsafe {
50+
if (JreNativeMethods.java_interop_gc_bridge_set_mark_cross_references(bridge, &MarkCrossReferences) < 0)
51+
throw new NotSupportedException("Could not set MarkCrossReferences!");
52+
}
4953
}
5054
catch (Exception) {
5155
JreNativeMethods.java_interop_gc_bridge_free (bridge);

src/java-interop/java-interop-gc-bridge-mono.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,7 @@ gc_cross_references (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGC
12881288
static void
12891289
managed_gc_cross_references (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs, MonoGCBridgeXRef *xrefs)
12901290
{
1291+
fprintf (stderr, "# jonp: managed_gc_cross_references: %d sccs and %d xrefs\n", num_sccs, num_xrefs);
12911292
if (mono_bridge->mark_cross_references == NULL) {
12921293
assert (!"mono_bridge->mark_cross_references is NULL; WE SHOULD NOT BE EXECUTING");
12931294
return;
@@ -1317,12 +1318,14 @@ managed_gc_cross_references (int num_sccs, MonoGCBridgeSCC **sccs, int num_xrefs
13171318
xref->DestinationGroupIndex = (void*) (intptr_t) xrefs [i].dst_scc_index;
13181319
}
13191320

1321+
fprintf (stderr, "# jonp: calling managed mark_cross_references\n");
13201322
mono_bridge->mark_cross_references (&cross_references);
13211323

13221324
for (i = 0; i < num_sccs; ++i) {
13231325
Srij_StronglyConnectedComponent *scc = &cross_references.Components [i];
13241326
sccs [i]->is_alive = scc->IsAlive;
13251327
}
1328+
fprintf (stderr, "# jonp: calling done\n");
13261329
}
13271330

13281331
int

0 commit comments

Comments
 (0)