From 1567c1c8d6c998e043f39c511e2acd562f7dbaa9 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 31 Oct 2023 14:15:35 +0100 Subject: [PATCH] Fix Mono AOT on iOS to not freeze during GC stop-the-world event on memory collection --- Source/Engine/Engine/NativeInterop.Unmanaged.cs | 3 --- Source/Engine/Scripting/Runtime/DotNet.cpp | 7 +++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Engine/NativeInterop.Unmanaged.cs b/Source/Engine/Engine/NativeInterop.Unmanaged.cs index 5af1569b9..0391974b6 100644 --- a/Source/Engine/Engine/NativeInterop.Unmanaged.cs +++ b/Source/Engine/Engine/NativeInterop.Unmanaged.cs @@ -1181,10 +1181,7 @@ namespace FlaxEngine.Interop [UnmanagedCallersOnly] internal static void GCCollect(int generation, int mode, bool blocking, bool compacting) { - // TODO: fix stall on iOS with AOT -#if !USE_AOT GC.Collect(generation, (GCCollectionMode)mode, blocking, compacting); -#endif } [UnmanagedCallersOnly] diff --git a/Source/Engine/Scripting/Runtime/DotNet.cpp b/Source/Engine/Scripting/Runtime/DotNet.cpp index b3e5a0072..91b1d3dde 100644 --- a/Source/Engine/Scripting/Runtime/DotNet.cpp +++ b/Source/Engine/Scripting/Runtime/DotNet.cpp @@ -46,6 +46,7 @@ #include #include #include +#include #include typedef char char_t; #define DOTNET_HOST_MONO_DEBUG 0 @@ -522,7 +523,8 @@ void MCore::GC::FreeMemory(void* ptr, bool coTaskMem) void MCore::Thread::Attach() { -#if DOTNET_HOST_MONO + // TODO: find a way to properly register native thread so Mono Stop The World (stw) won't freeze when native threads (eg. Job System) are running native code only +#if DOTNET_HOST_MONO && !USE_MONO_AOT if (!IsInMainThread() && !mono_domain_get()) { mono_thread_attach(MonoDomainHandle); @@ -2056,7 +2058,7 @@ bool InitHostfxr() // Setup debugger { int32 debuggerLogLevel = 0; - if (CommandLine::Options.MonoLog.IsTrue()) + if (CommandLine::Options.MonoLog.IsTrue() || DOTNET_HOST_MONO_DEBUG) { LOG(Info, "Using detailed Mono logging"); mono_trace_set_level_string("debug"); @@ -2139,6 +2141,7 @@ bool InitHostfxr() LOG(Fatal, "Failed to initialize Mono."); return true; } + mono_gc_init_finalizer_thread(); // Log info char* buildInfo = mono_get_runtime_build_info();