Skip to content

Commit

Permalink
Merge pull request #3404 from Sonicadvance1/fix_early_thread_create_t…
Browse files Browse the repository at this point in the history
…racking

Linux: Decouple thread object creation and tracking
  • Loading branch information
lioncash committed Feb 5, 2024
2 parents 435b67a + 8cfbabd commit 5e5984a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions Source/Tools/FEXLoader/FEXLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ int main(int argc, char **argv, char **const envp) {
}

auto ParentThread = SyscallHandler->TM.CreateThread(Loader.DefaultRIP(), Loader.GetStackPointer());
SyscallHandler->TM.TrackThread(ParentThread);

// Pass in our VDSO thunks
CTX->AppendThunkDefinitions(FEX::VDSO::GetVDSOThunkDefinitions());
Expand Down
4 changes: 4 additions & 0 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class ThreadManager final {
~ThreadManager();

FEXCore::Core::InternalThreadState *CreateThread(uint64_t InitialRIP, uint64_t StackPointer, FEXCore::Core::CPUState *NewThreadState = nullptr, uint64_t ParentTID = 0);
void TrackThread(FEXCore::Core::InternalThreadState *Thread) {
std::lock_guard lk(ThreadCreationMutex);
Threads.emplace_back(Thread);
}

void DestroyThread(FEXCore::Core::InternalThreadState *Thread);
void StopThread(FEXCore::Core::InternalThreadState *Thread);
Expand Down
9 changes: 9 additions & 0 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/Syscalls/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,17 @@ namespace FEX::HLE {
}
}

FEX::HLE::_SyscallHandler->TM.TrackThread(NewThread);

return NewThread;
}

uint64_t HandleNewClone(FEXCore::Core::InternalThreadState *Thread, FEXCore::Context::Context *CTX, FEXCore::Core::CpuStateFrame *Frame, FEX::HLE::clone3_args *CloneArgs) {
auto GuestArgs = &CloneArgs->args;
uint64_t flags = GuestArgs->flags;
auto NewThread = Thread;
bool CreatedNewThreadObject{};

if (flags & CLONE_THREAD) {
FEXCore::Core::CPUState NewThreadState{};
// Clone copies the parent thread's state
Expand All @@ -168,6 +172,7 @@ namespace FEX::HLE {
// CLONE_PARENT_SETTID, CLONE_CHILD_SETTID, CLONE_CHILD_CLEARTID, CLONE_PIDFD will be handled by kernel
// Call execution thread directly since we already are on the new thread
NewThread->StartRunning.NotifyAll(); // Clear the start running flag
CreatedNewThreadObject = true;
}
else{
// If we don't have CLONE_THREAD then we are effectively a fork
Expand Down Expand Up @@ -211,6 +216,10 @@ namespace FEX::HLE {
Thread->ThreadManager.PID = ::getpid();
FEX::HLE::_SyscallHandler->FM.UpdatePID(Thread->ThreadManager.PID);

if (CreatedNewThreadObject) {
FEX::HLE::_SyscallHandler->TM.TrackThread(Thread);
}

// Start exuting the thread directly
// Our host clone starts in a new stack space, so it can't return back to the JIT space
CTX->ExecutionThread(Thread);
Expand Down
3 changes: 0 additions & 3 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ namespace FEX::HLE {
FEXCore::Core::InternalThreadState *ThreadManager::CreateThread(uint64_t InitialRIP, uint64_t StackPointer, FEXCore::Core::CPUState *NewThreadState, uint64_t ParentTID) {
auto Thread = CTX->CreateThread(InitialRIP, StackPointer, NewThreadState, ParentTID);

std::lock_guard lk(ThreadCreationMutex);
Threads.emplace_back(Thread);

++IdleWaitRefCount;
return Thread;
}
Expand Down

0 comments on commit 5e5984a

Please sign in to comment.