Skip to content

Commit

Permalink
Merge pull request #4186 from Sonicadvance1/remove_remaining_runninge…
Browse files Browse the repository at this point in the history
…vents

FEXCore: Removes remaining RunningEvents from InternalThreadState
  • Loading branch information
lioncash authored Nov 30, 2024
2 parents 95d5b14 + e7e5920 commit baddfe0
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 32 deletions.
7 changes: 0 additions & 7 deletions FEXCore/Source/Interface/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -868,15 +868,8 @@ uintptr_t ContextImpl::CompileBlock(FEXCore::Core::CpuStateFrame* Frame, uint64_
}

void ContextImpl::ExecutionThread(FEXCore::Core::InternalThreadState* Thread) {

Thread->RunningEvents.WaitingToStart = false;

Thread->RunningEvents.Running = true;

static_cast<ContextImpl*>(Thread->CTX)->Dispatcher->ExecuteDispatch(Thread->CurrentFrame);

Thread->RunningEvents.Running = false;

{
// Ensure the Code Object Serialization service has fully serialized this thread's data before clearing the cache
// Use the thread's object cache ref counter for this
Expand Down
7 changes: 0 additions & 7 deletions FEXCore/include/FEXCore/Debug/InternalThreadState.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,8 @@ static_assert(!std::is_move_assignable_v<NonMovableUniquePtr<int>>);
struct InternalThreadState : public FEXCore::Allocator::FEXAllocOperators {
FEXCore::Core::CpuStateFrame* const CurrentFrame = &BaseFrameState;

struct {
std::atomic_bool Running {false};
std::atomic_bool WaitingToStart {true};
std::atomic_bool ThreadSleeping {false};
} RunningEvents;

FEXCore::Context::Context* const CTX;


NonMovableUniquePtr<FEXCore::IR::OpDispatchBuilder> OpDispatcher;

NonMovableUniquePtr<FEXCore::CPU::CPUBackend> CPUBackend;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ bool SignalDelegator::HandleSignalPause(FEXCore::Core::InternalThreadState* Thre
// We need to be a little bit careful here
// If we were already paused (due to GDB) and we are immediately stopping (due to gdb kill)
// Then we need to ensure we don't double decrement our idle thread counter
if (Thread->RunningEvents.ThreadSleeping) {
if (ThreadObject->ThreadSleeping) {
// If the thread was sleeping then its idle counter was decremented
// Reincrement it here to not break logic
FEX::HLE::_SyscallHandler->TM.IncrementIdleRefCount();
Expand All @@ -500,10 +500,6 @@ bool SignalDelegator::HandleSignalPause(FEXCore::Core::InternalThreadState* Thre

void SignalDelegator::SignalThread(FEXCore::Core::InternalThreadState* Thread, SignalEvent Event) {
auto ThreadObject = FEX::HLE::ThreadManager::GetStateObjectFromFEXCoreThread(Thread);
if (Event == SignalEvent::Pause && Thread->RunningEvents.Running.load() == false) {
// Skip signaling a thread if it is already paused.
return;
}
ThreadObject->SignalReason.store(Event);
FHU::Syscalls::tgkill(ThreadObject->ThreadInfo.PID, ThreadObject->ThreadInfo.TID, SignalDelegator::SIGNAL_FOR_PAUSE);
}
Expand Down
17 changes: 4 additions & 13 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ void ThreadManager::DestroyThread(FEX::HLE::ThreadStateObject* Thread, bool Need
}

void ThreadManager::StopThread(FEX::HLE::ThreadStateObject* Thread) {
if (Thread->Thread->RunningEvents.Running.exchange(false)) {
SignalDelegation->SignalThread(Thread->Thread, SignalEvent::Stop);
}
SignalDelegation->SignalThread(Thread->Thread, SignalEvent::Stop);
}

void ThreadManager::HandleThreadDeletion(FEX::HLE::ThreadStateObject* Thread, bool NeedsTLSUninstall) {
Expand Down Expand Up @@ -154,9 +152,7 @@ void ThreadManager::Stop(bool IgnoreCurrentThread) {
continue;
}

if (Thread->Thread->RunningEvents.Running.load()) {
StopThread(Thread);
}
StopThread(Thread);
}
}

Expand All @@ -168,19 +164,17 @@ void ThreadManager::Stop(bool IgnoreCurrentThread) {

void ThreadManager::SleepThread(FEXCore::Context::Context* CTX, FEXCore::Core::CpuStateFrame* Frame) {
auto ThreadObject = FEX::HLE::ThreadManager::GetStateObjectFromCPUState(Frame);
auto Thread = Frame->Thread;

--IdleWaitRefCount;
IdleWaitCV.notify_all();

Thread->RunningEvents.ThreadSleeping = true;
ThreadObject->ThreadSleeping = true;

// Go to sleep
ThreadObject->ThreadPaused.Wait();

Thread->RunningEvents.Running = true;
++IdleWaitRefCount;
Thread->RunningEvents.ThreadSleeping = false;
ThreadObject->ThreadSleeping = false;

IdleWaitCV.notify_all();
}
Expand All @@ -201,9 +195,6 @@ void ThreadManager::UnlockAfterFork(FEXCore::Core::InternalThreadState* LiveThre
continue;
}

// Setting running to false ensures that when they are shutdown we won't send signals to kill them
DeadThread->Thread->RunningEvents.Running = false;

// Despite what google searches may susgest, glibc actually has special code to handle forks
// with multiple active threads.
// It cleans up the stacks of dead threads and marks them as terminated.
Expand Down
1 change: 1 addition & 0 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ struct ThreadStateObject : public FEXCore::Allocator::FEXAllocOperators {
std::atomic<SignalEvent> SignalReason {SignalEvent::Nothing};

// Thread pause handling
std::atomic_bool ThreadSleeping {false};
FEXCore::InterruptableConditionVariable ThreadPaused;

int StatusCode {};
Expand Down

0 comments on commit baddfe0

Please sign in to comment.