diff --git a/Source/Engine/Platform/Base/ThreadBase.cpp b/Source/Engine/Platform/Base/ThreadBase.cpp index ddcba4f7a..a283831bb 100644 --- a/Source/Engine/Platform/Base/ThreadBase.cpp +++ b/Source/Engine/Platform/Base/ThreadBase.cpp @@ -4,6 +4,7 @@ #include "Engine/Threading/IRunnable.h" #include "Engine/Threading/ThreadRegistry.h" #include "Engine/Core/Log.h" +#include "Engine/Scripting/ManagedCLR/MCore.h" #if TRACY_ENABLE #include "Engine/Core/Math/Math.h" #include @@ -108,6 +109,8 @@ int32 ThreadBase::Run() _isRunning = false; ThreadExiting(thread, exitCode); ThreadRegistry::Remove(thread); + MCore::ExitThread(); // TODO: use mono_thread_detach instead of ext and unlink mono runtime from thread in ThreadExiting delegate + // mono terminates the native thread.. return exitCode; } diff --git a/Source/Engine/Scripting/ManagedCLR/MCore.cpp b/Source/Engine/Scripting/ManagedCLR/MCore.cpp index 6c71a5879..d58bcbd95 100644 --- a/Source/Engine/Scripting/ManagedCLR/MCore.cpp +++ b/Source/Engine/Scripting/ManagedCLR/MCore.cpp @@ -63,7 +63,7 @@ MDomain* MCore::CreateDomain(const MString& domainName) if (MDomains[i]->GetName() == domainName) return MDomains[i]; } - + auto domain = New(domainName); #if USE_MONO const auto monoDomain = mono_domain_create_appdomain((char*)domainName.Get(), nullptr); @@ -230,11 +230,6 @@ void OnGCEvent(MonoProfiler* profiler, MonoProfilerGCEvent event, uint32_t gener #endif -void OnThreadExiting(Thread* thread, int32 exitCode) -{ - MCore::ExitThread(); -} - void OnLogCallback(const char* logDomain, const char* logLevel, const char* message, mono_bool fatal, void* userData) { String currentDomain(logDomain); @@ -518,7 +513,7 @@ bool MCore::LoadEngine() } #endif -// Init Mono + // Init Mono #if PLATFORM_ANDROID const char* monoVersion = "mobile"; #else @@ -543,9 +538,6 @@ bool MCore::LoadEngine() mono_domain_set_config(monoRootDomain, configDir.Get(), configFilename.Get()); mono_thread_set_main(mono_thread_current()); - // Register for threads ending to cleanup after managed runtime usage - Thread::ThreadExiting.Bind(); - // Info char* buildInfo = mono_get_runtime_build_info(); LOG(Info, "Mono version: {0}", String(buildInfo)); @@ -564,8 +556,6 @@ long MonoHackSehExceptionHandler(class EXCEPTION_POINTERS* ep) void MCore::UnloadEngine() { - Thread::ThreadExiting.Unbind(); - // Only root domain should be alive at this point for (auto domain : MDomains) { @@ -640,6 +630,7 @@ void MCore::ExitThread() if (!IsInMainThread() && mono_domain_get()) { LOG(Info, "Thread 0x{0:x} exits the managed runtime", Platform::GetCurrentThreadID()); + // TODO: use mono_thread_detach but modify mono to call mono_thread_info_detach there so the thread goes into STATE_DETACHED mono_thread_exit(); } #endif