Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 30, 2026

Moves the GC bridge processing logic from native C++ to managed C#, reducing native code complexity and eliminating reverse p/invokes.

New C# Implementation

  • BridgeProcessing.cs: Port of bridge-processing.cc handling SCC preparation, cross-reference setup, weak/global ref conversion, and cleanup
  • ManagedValueManager.cs: Managed background thread replaces native thread; calls BridgeProcessing.Process() directly

Removed C++ Files

  • src/native/clr/host/bridge-processing.cc
  • src/native/clr/include/host/bridge-processing.hh
  • src/native/clr/include/host/bridge-processing-shared.hh
  • src/native/nativeaot/host/bridge-processing.cc
  • src/native/nativeaot/include/host/bridge-processing.hh

Simplified gc-bridge.hh/cc

  • Removed initialize_callback(), bridge_processing(), callback pointers, thread management
  • Removed unused includes (<thread>, <shared_mutex>, <unordered_map>)
  • Removed clr_initialize_gc_bridge P/Invoke (was never called)

P/Invoke Changes

Direction Old Mode New Mode
Native → Managed (reverse) 2 per GC cycle 0
Managed → Native (forward) 0 2 (wait_for_processing, trigger_java_gc)

The native mark_cross_references callback (invoked when managed world is stopped) remains in C++ as required by the runtime.

Original prompt

This section details on the original issue you should resolve

<issue_title>[NativeAOT] Try to move GC bridge code from C++ to C#</issue_title>
<issue_description>### Android framework version

net11.0-android (Preview)

Affected platform version

.NET 11

Description

The GC bridge for Native AOT and CoreCLR is currently implemented in C++ (#10198). I would like to explore the posibility of moving as much code from native to managed code. All the processing in the background thread could be in managed code (what is now void GCBridge::bridge_processing ()). The only thing that needs to be a native entry point (can be C, doesn't have to be C++) is now void GCBridge::log_mark_cross_references_args_if_enabled (MarkCrossReferencesArgs *args) which runs when the managed world is stopped.

Steps to Reproduce

Did you find any workaround?

No response

Relevant log output

```</issue_description>

<agent_instructions>Most relevant code is in these files:
- src/Mono.Android/Microsoft.Android.Runtime/ManagedValueManager.cs
- src/native/clr/host/gc-bridge.cc
- src/native/clr/host/bridge-processing.cc

The goal is to reduce the amount of C++ code and use C# code which can be better shared between Native AOT and CoreCLR builds of the .NET for Android runtime.</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- Add BridgeProcessing.cs with managed implementation of bridge processing
- Add new native methods for managed processing mode
- Update ManagedValueManager to use managed processing loop
- Update gc-bridge.hh with new managed processing APIs

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Copilot AI and others added 3 commits January 30, 2026 22:35
Removed files:
- src/native/clr/host/bridge-processing.cc
- src/native/clr/include/host/bridge-processing.hh
- src/native/clr/include/host/bridge-processing-shared.hh
- src/native/nativeaot/host/bridge-processing.cc
- src/native/nativeaot/include/host/bridge-processing.hh

Updated files to remove references to deleted files:
- src/native/clr/host/CMakeLists.txt
- src/native/nativeaot/host/CMakeLists.txt
- src/native/clr/host/gc-bridge.cc
- src/native/nativeaot/host/host.cc

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Copilot AI changed the title [WIP] Move GC bridge code from C++ to C# [NativeAOT] Move GC bridge processing from C++ to C# Jan 30, 2026
Copilot AI requested a review from simonrozsival January 30, 2026 22:42
Copilot AI and others added 2 commits January 31, 2026 04:13
- Remove clr_initialize_gc_bridge P/Invoke (old callback mode never used)
- Remove initialize_callback() function from gc-bridge.hh
- Remove bridge_processing() function (native background thread loop)
- Remove bridge_processing_started_callback and bridge_processing_finished_callback
- Remove bridge_processing_thread and managed_processing_mode flag
- Remove unused includes (thread, shared_mutex, unordered_map)
- Remove BridgeProcessingStartedFtn and BridgeProcessingFinishedFtn typedefs

This eliminates 2 reverse p/invokes per GC cycle that were never used.

Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Co-authored-by: simonrozsival <374616+simonrozsival@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[NativeAOT] Try to move GC bridge code from C++ to C#

2 participants