[runtime] Call into managed code to call GC.Collect instead of using embedding API. (#10828)

Any performance difference will be neglible compared to running the GC, so
there's no compelling reason to use the embedding API.

This makes things a bit easier with CoreCLR, since the new code works there too.

This also required a few changes in delegates.t4 to make code generation for
functions without arguments work correctly.
This commit is contained in:
Rolf Bjarne Kvinge 2021-03-11 07:37:45 +01:00 коммит произвёл GitHub
Родитель 603746c33f
Коммит a8b3d9c296
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 31 добавлений и 22 удалений

Просмотреть файл

@ -6,6 +6,10 @@
<#@ import namespace="System.Collections.Generic" #>
<#
var delegates = new XDelegates {
new XDelegate ("void", "void", "xamarin_gc_collect") {
WrappedManagedFunction = "GCCollect",
},
new XDelegate ("void", "void", "xamarin_register_assembly",
"GCHandle->MonoReflectionAssembly *", "IntPtr", "assembly"
) {
@ -341,9 +345,6 @@
ReturnType = new Arg (cReturnType, mReturnType, string.Empty);
EntryPoint = entryPoint;
if (arguments == null || arguments.Length == 0)
return;
if (arguments.Length % 3 != 0)
throw new Exception (string.Format ("Export arguments params must be a multiple of 3 to form a set of (c type, managed name, name) triples for {0}", entryPoint));
@ -416,8 +417,11 @@
sb.Append (EntryPoint.Substring ("xamarin_".Length));
sb.Append (" (");
sb.Append (invoke_args);
if (ExceptionHandling)
sb.Append (", exception_gchandle");
if (ExceptionHandling) {
if (Arguments.Count > 0)
sb.Append (", ");
sb.Append ("exception_gchandle");
}
if (ReturnType.IsGCHandleConversion)
sb.Append (")");
@ -454,9 +458,6 @@
string CFormatArgs (string empty, bool nameOnly, bool exposed = false)
{
if (Arguments == null || Arguments.Count == 0)
return empty;
var builder = new StringBuilder ();
foreach (var arg in Arguments) {
@ -469,13 +470,16 @@
builder.Append (", ");
}
if (Arguments.Count > 0)
builder.Length -= 2;
if (ExceptionHandling) {
if (Arguments.Count > 0)
builder.Append (", ");
if (nameOnly) {
builder.Append (", exception_gchandle");
builder.Append ("exception_gchandle");
} else {
builder.Append (", GCHandle *exception_gchandle");
builder.Append ("GCHandle *exception_gchandle");
}
}
@ -484,9 +488,6 @@
string MFormatArgs (string empty, bool nameOnly)
{
if (Arguments == null || Arguments.Count == 0)
return empty;
var builder = new StringBuilder ();
foreach (var arg in Arguments) {
@ -503,13 +504,16 @@
builder.Append (", ");
}
if (Arguments.Count > 0)
builder.Length -= 2;
if (ExceptionHandling) {
if (nameOnly) {
// nothing to do
} else {
builder.Append (", out IntPtr exception_gchandle");
if (Arguments.Count > 0)
builder.Append (", ");
builder.Append ("out IntPtr exception_gchandle");
}
}

Просмотреть файл

@ -410,10 +410,6 @@
"MonoDebugFormat", "format"
),
new Export ("void", "mono_gc_collect",
"int", "generation"
),
new Export ("mono_bool", "mono_is_debugger_attached"),
#endregion

Просмотреть файл

@ -223,7 +223,9 @@ extern void mono_gc_init_finalizer_thread (void);
- (void) memoryWarning: (NSNotification *) sender
{
// COOP: ?
mono_gc_collect (mono_gc_max_generation ());
GCHandle exception_gchandle = INVALID_GCHANDLE;
xamarin_gc_collect (&exception_gchandle);
xamarin_process_managed_exception_gchandle (exception_gchandle);
}
@end

Просмотреть файл

@ -1233,7 +1233,9 @@ pump_gc (void *context)
mono_thread_attach (mono_get_root_domain ());
while (xamarin_gc_pump) {
mono_gc_collect (mono_gc_max_generation ());
GCHandle exception_gchandle = INVALID_GCHANDLE;
xamarin_gc_collect (&exception_gchandle);
xamarin_process_managed_exception_gchandle (exception_gchandle);
MONO_ENTER_GC_SAFE;
usleep (1000000);
MONO_EXIT_GC_SAFE;

Просмотреть файл

@ -1706,6 +1706,11 @@ namespace ObjCRuntime {
throw ErrorHelper.CreateError (8003, "Failed to find the closed generic method '{0}' on the type '{1}'.", open_method.Name, closed_type.FullName);
}
static void GCCollect ()
{
GC.Collect ();
}
[EditorBrowsable (EditorBrowsableState.Never)]
#if MONOMAC
public static void ReleaseBlockOnMainThread (IntPtr block)