From 5b37a7ec211f029482f00b27348b67b3733e4f5e Mon Sep 17 00:00:00 2001 From: ergo720 <45463469+ergo720@users.noreply.github.com> Date: Sat, 11 Mar 2023 16:40:59 +0100 Subject: [PATCH] Fixed thread order initialization when a thread starts suspended --- src/core/kernel/exports/EmuKrnlPs.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/core/kernel/exports/EmuKrnlPs.cpp b/src/core/kernel/exports/EmuKrnlPs.cpp index e324251b4..230c94995 100644 --- a/src/core/kernel/exports/EmuKrnlPs.cpp +++ b/src/core/kernel/exports/EmuKrnlPs.cpp @@ -121,8 +121,8 @@ static unsigned int WINAPI PCSTProxy params.Ethread, params.TlsDataSize); - eThread->Tcb.State = xbox::Running; xbox::KiExecuteKernelApc(); + eThread->Tcb.State = xbox::Running; auto routine = (xbox::PKSYSTEM_ROUTINE)StartFrame->SystemRoutine; // Debugging notice : When the below line shows up with an Exception dialog and a @@ -411,16 +411,20 @@ XBSYSAPI EXPORTNUM(255) xbox::ntstatus_xt NTAPI xbox::PsCreateSystemThreadEx g_AffinityPolicy->SetAffinityXbox(handle); - // Now that ThreadId is populated and affinity is changed, resume the thread (unless the guest passed CREATE_SUSPENDED) + // Now that ThreadId is populated and affinity is changed, resume the thread (unless the guest passed CREATE_SUSPENDED), then wait until the new thread has + // finished initialization if (CreateSuspended) { KeSuspendThread(&eThread->Tcb); } - ResumeThread(handle); - // Log ThreadID identical to how GetCurrentThreadID() is rendered : EmuLog(LOG_LEVEL::DEBUG, "Created Xbox proxy thread. Handle : 0x%X, ThreadId : [0x%.4X], Native Handle : 0x%X, Native ThreadId : [0x%.4X]", *ThreadHandle, eThread->UniqueThread, handle, ThreadId); + + ResumeThread(handle); + while (eThread->Tcb.State == Initialized) { + std::this_thread::yield(); + } } RETURN(X_STATUS_SUCCESS);