diff --git a/Source/Windows/ARM64EC/Module.cpp b/Source/Windows/ARM64EC/Module.cpp index a24963412a..523b5bcae1 100644 --- a/Source/Windows/ARM64EC/Module.cpp +++ b/Source/Windows/ARM64EC/Module.cpp @@ -816,20 +816,22 @@ NTSTATUS ThreadTerm(HANDLE Thread, LONG ExitCode) { auto* OldThreadState = CPUArea.ThreadState(); CPUArea.ThreadState() = nullptr; - { - THREAD_BASIC_INFORMATION Info; - if (NTSTATUS Err = NtQueryInformationThread(Thread, ThreadBasicInformation, &Info, sizeof(Info), nullptr); Err) { - return Err; - } + THREAD_BASIC_INFORMATION Info; + if (NTSTATUS Err = NtQueryInformationThread(Thread, ThreadBasicInformation, &Info, sizeof(Info), nullptr); Err) { + return Err; + } - const auto ThreadTID = reinterpret_cast(Info.ClientId.UniqueThread); + const auto ThreadTID = reinterpret_cast(Info.ClientId.UniqueThread); + { std::scoped_lock Lock(ThreadCreationMutex); Threads.erase(ThreadTID); } CTX->DestroyThread(OldThreadState); - ::VirtualFree(reinterpret_cast(GetCPUArea().EmulatorStackLimit()), 0, MEM_RELEASE); - FEX::Windows::DeinitCRTThread(); + ::VirtualFree(reinterpret_cast(CPUArea.EmulatorStackLimit()), 0, MEM_RELEASE); + if (ThreadTID == GetCurrentThreadId()) { + FEX::Windows::DeinitCRTThread(); + } return STATUS_SUCCESS; } diff --git a/Source/Windows/WOW64/Module.cpp b/Source/Windows/WOW64/Module.cpp index ebf5f01b70..9e6cec16da 100644 --- a/Source/Windows/WOW64/Module.cpp +++ b/Source/Windows/WOW64/Module.cpp @@ -137,7 +137,6 @@ bool IsDispatcherAddress(uint64_t Address) { } bool IsAddressInJit(uint64_t Address) { - const auto& Config = SignalDelegator->GetConfig(); if (IsDispatcherAddress(Address)) { return true; } @@ -517,19 +516,21 @@ void BTCpuThreadTerm(HANDLE Thread, LONG ExitCode) { return; } - { - THREAD_BASIC_INFORMATION Info; - if (NTSTATUS Err = NtQueryInformationThread(Thread, ThreadBasicInformation, &Info, sizeof(Info), nullptr); Err) { - return; - } + THREAD_BASIC_INFORMATION Info; + if (NTSTATUS Err = NtQueryInformationThread(Thread, ThreadBasicInformation, &Info, sizeof(Info), nullptr); Err) { + return; + } - const auto ThreadTID = reinterpret_cast(Info.ClientId.UniqueThread); + const auto ThreadTID = reinterpret_cast(Info.ClientId.UniqueThread); + { std::scoped_lock Lock(ThreadCreationMutex); Threads.erase(ThreadTID); } CTX->DestroyThread(TLS.ThreadState()); - FEX::Windows::DeinitCRTThread(); + if (ThreadTID == GetCurrentThreadId()) { + FEX::Windows::DeinitCRTThread(); + } } void* BTCpuGetBopCode() {