Skip to content

Commit

Permalink
Merge pull request #4256 from bylaws/crtd
Browse files Browse the repository at this point in the history
Windows: Only deinit the thread CRT when destroying the current thread
  • Loading branch information
Sonicadvance1 authored Jan 7, 2025
2 parents 8913c59 + c00cef6 commit d2bac45
Showing 2 changed files with 19 additions and 16 deletions.
18 changes: 10 additions & 8 deletions Source/Windows/ARM64EC/Module.cpp
Original file line number Diff line number Diff line change
@@ -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<uint64_t>(Info.ClientId.UniqueThread);
const auto ThreadTID = reinterpret_cast<uint64_t>(Info.ClientId.UniqueThread);
{
std::scoped_lock Lock(ThreadCreationMutex);
Threads.erase(ThreadTID);
}

CTX->DestroyThread(OldThreadState);
::VirtualFree(reinterpret_cast<void*>(GetCPUArea().EmulatorStackLimit()), 0, MEM_RELEASE);
FEX::Windows::DeinitCRTThread();
::VirtualFree(reinterpret_cast<void*>(CPUArea.EmulatorStackLimit()), 0, MEM_RELEASE);
if (ThreadTID == GetCurrentThreadId()) {
FEX::Windows::DeinitCRTThread();
}
return STATUS_SUCCESS;
}

17 changes: 9 additions & 8 deletions Source/Windows/WOW64/Module.cpp
Original file line number Diff line number Diff line change
@@ -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<uint64_t>(Info.ClientId.UniqueThread);
const auto ThreadTID = reinterpret_cast<uint64_t>(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() {

0 comments on commit d2bac45

Please sign in to comment.