Skip to content

Commit

Permalink
Revert "Handle attempting to suspend while inside of epoll"
Browse files Browse the repository at this point in the history
This reverts commit 1e14bed.
  • Loading branch information
Jarred-Sumner committed Jan 29, 2025
1 parent 40a1929 commit e32c635
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 97 deletions.
17 changes: 0 additions & 17 deletions Source/WTF/wtf/Threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,23 +281,6 @@ class WTF_CAPABILITY("is current") Thread : public ThreadSafeRefCounted<Thread>,
m_savedLastStackTop = lastStackTop;
}

#if USE(BUN_JSC_ADDITIONS) && OS(LINUX)
enum ThreadStateFlags : uint8_t {
IsBlockedInSyscall = 1 << 0,
HasPendingSuspension = 1 << 1
};
std::atomic<uint8_t> m_threadState { 0 };

void setIsEnteringSyscall();

bool isBlockedInSyscall() const
{
return m_threadState.load(std::memory_order_relaxed) & IsBlockedInSyscall;
}

void setIsExitingSyscall();
#endif

#if OS(DARWIN)
mach_port_t machThread() { return m_platformThread; }
#endif
Expand Down
92 changes: 12 additions & 80 deletions Source/WTF/wtf/posix/ThreadingPOSIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,49 +216,6 @@ void Thread::initializePlatformThreading()
#endif
}

#if USE(BUN_JSC_ADDITIONS) && OS(LINUX)
void Thread::setIsEnteringSyscall()
{
uint8_t oldState;
uint8_t newState;
do {
oldState = m_threadState.load(std::memory_order_relaxed);
newState = oldState | IsBlockedInSyscall;
} while (!m_threadState.compare_exchange_weak(oldState, newState, std::memory_order_release));
}

void Thread::setIsExitingSyscall()
{
uint8_t oldState;
uint8_t newState;
bool needsSuspend = false;

// Clear IsBlockedInSyscall flag and capture suspension state atomically
do {
oldState = m_threadState.load(std::memory_order_relaxed);
newState = oldState & ~IsBlockedInSyscall;
needsSuspend = oldState & HasPendingSuspension;
if (needsSuspend)
newState &= ~HasPendingSuspension;
} while (!m_threadState.compare_exchange_weak(oldState, newState, std::memory_order_acquire));

if (needsSuspend) {
// Signal that we're suspended
globalSemaphoreForSuspendResume->post();

sigset_t blockedSignalSet;
sigfillset(&blockedSignalSet);
sigdelset(&blockedSignalSet, g_wtfConfig.sigThreadSuspendResume);
sigsuspend(&blockedSignalSet);

m_platformRegisters = nullptr;

// Signal that we're resumed
globalSemaphoreForSuspendResume->post();
}
}
#endif

#if OS(LINUX)
ThreadIdentifier Thread::currentID()
{
Expand Down Expand Up @@ -498,33 +455,19 @@ auto Thread::suspend(const ThreadSuspendLocker&) -> Expected<void, PlatformSuspe
#else
if (!m_suspendCount) {
targetThread.store(this);
#if USE(BUN_JSC_ADDITIONS) && OS(LINUX)
// Try to mark suspension pending if in syscall
uint8_t oldState = m_threadState.load(std::memory_order_relaxed);
if (oldState & IsBlockedInSyscall) {
// Thread is in syscall, set pending suspension
uint8_t newState;
do {
newState = oldState | HasPendingSuspension;
} while (!m_threadState.compare_exchange_weak(oldState, newState, std::memory_order_release));
} else {
#endif
while (true) {
// We must use pthread_kill to avoid queue-overflow problem with real-time signals.
int result = pthread_kill(m_handle, g_wtfConfig.sigThreadSuspendResume);
if (result)
return makeUnexpected(result);
globalSemaphoreForSuspendResume->wait();
if (m_platformRegisters)
break;
// Because of an alternative signal stack, we failed to suspend this thread.
// Retry suspension again after yielding.
Thread::yield();
}

#if USE(BUN_JSC_ADDITIONS) && OS(LINUX)

while (true) {
// We must use pthread_kill to avoid queue-overflow problem with real-time signals.
int result = pthread_kill(m_handle, g_wtfConfig.sigThreadSuspendResume);
if (result)
return makeUnexpected(result);
globalSemaphoreForSuspendResume->wait();
if (m_platformRegisters)
break;
// Because of an alternative signal stack, we failed to suspend this thread.
// Retry suspension again after yielding.
Thread::yield();
}
#endif
}
++m_suspendCount;
return { };
Expand All @@ -546,17 +489,6 @@ void Thread::resume(const ThreadSuspendLocker&)
// In this implementaiton, we take (3). m_suspendCount is used to distinguish it.
// Note that we must use pthread_kill to avoid queue-overflow problem with real-time signals.
targetThread.store(this);

#if USE(BUN_JSC_ADDITIONS) && OS(LINUX)
// Clear any pending suspension flag
uint8_t oldState;
do {
oldState = m_threadState.load(std::memory_order_relaxed);
} while (!m_threadState.compare_exchange_weak(oldState,
oldState & ~HasPendingSuspension,
std::memory_order_release));
#endif

if (pthread_kill(m_handle, g_wtfConfig.sigThreadSuspendResume) == ESRCH)
return;
globalSemaphoreForSuspendResume->wait();
Expand Down

0 comments on commit e32c635

Please sign in to comment.