Skip to content

Commit

Permalink
ThreadManager: Add some sanity asserts
Browse files Browse the repository at this point in the history
These couple of functions have some footguns that I'm encountering while
rewriting gdbserver. Ensure that assertion builds capture the problems
  • Loading branch information
Sonicadvance1 committed Dec 17, 2024
1 parent 57178ab commit e58f67b
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Source/Tools/LinuxEmulation/LinuxSyscalls/ThreadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ void ThreadManager::HandleThreadDeletion(FEX::HLE::ThreadStateObject* Thread, bo
}

if (NeedsTLSUninstall) {
#if defined(ASSERTIONS_ENABLED) && ASSERTIONS_ENABLED
// Sanity check. This can only be called from the owning thread.
{
const auto pid = ::getpid();
const auto tid = FHU::Syscalls::gettid();
LOGMAN_THROW_A_FMT(Thread->ThreadInfo.PID == pid && Thread->ThreadInfo.TID == tid, "Can't delete TLS data from a different thread!");
}
#endif
FEXCore::Allocator::UninstallTLSData(Thread->Thread);
}

Expand All @@ -76,6 +84,18 @@ void ThreadManager::NotifyPause() {
}

void ThreadManager::Pause() {
#if defined(ASSERTIONS_ENABLED) && ASSERTIONS_ENABLED
// Sanity check. This can't be called from an emulation thread.
{
const auto pid = ::getpid();
const auto tid = FHU::Syscalls::gettid();
std::lock_guard lk(ThreadCreationMutex);
for (auto& Thread : Threads) {
LOGMAN_THROW_A_FMT(!(Thread->ThreadInfo.PID == pid && Thread->ThreadInfo.TID == tid), "Can't put threads to sleep from inside "
"emulation thread!");
}
}
#endif
NotifyPause();
WaitForIdle();
}
Expand Down Expand Up @@ -164,6 +184,15 @@ void ThreadManager::Stop(bool IgnoreCurrentThread) {

void ThreadManager::SleepThread(FEXCore::Context::Context* CTX, FEXCore::Core::CpuStateFrame* Frame) {
auto ThreadObject = FEX::HLE::ThreadManager::GetStateObjectFromCPUState(Frame);
#if defined(ASSERTIONS_ENABLED) && ASSERTIONS_ENABLED
// Sanity check. This can only be called from the owning thread.
{
const auto pid = ::getpid();
const auto tid = FHU::Syscalls::gettid();
LOGMAN_THROW_A_FMT(ThreadObject->ThreadInfo.PID == pid && ThreadObject->ThreadInfo.TID == tid, "Can't delete TLS data from a different "
"thread!");
}
#endif

--IdleWaitRefCount;
IdleWaitCV.notify_all();
Expand Down

0 comments on commit e58f67b

Please sign in to comment.