Skip to content

Commit

Permalink
No need to try hijack/suspend GC threads (#75810)
Browse files Browse the repository at this point in the history
* No need to try suspend GC threads

* Same change for CoreClr + some cleanup.
  • Loading branch information
VSadov committed Sep 19, 2022
1 parent b5f1ad4 commit 774324e
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/coreclr/gc/sample/gcenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Thread
return (alloc_context *)&m_alloc_context;
}

void SetGCSpecial(bool fGCSpecial)
void SetGCSpecial()
{
}
};
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/nativeaot/Runtime/eventtrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,9 +992,6 @@ HRESULT ETW::GCLog::ForceGCForDiagnostics()
ThreadStore::AttachCurrentThread();
Thread* pThread = ThreadStore::GetCurrentThread();

// Doing this prevents the GC from trying to walk this thread's stack for roots.
pThread->SetGCSpecial(true);

// While doing the GC, much code assumes & asserts the thread doing the GC is in
// cooperative mode.
pThread->DisablePreemptiveMode();
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/nativeaot/Runtime/gcrhenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ bool GCToEEInterface::CreateThread(void (*threadStart)(void*), void* arg, bool i
ThreadStore::AttachCurrentThread(false);
}

ThreadStore::RawGetCurrentThread()->SetGCSpecial(true);
ThreadStore::RawGetCurrentThread()->SetGCSpecial();

auto realStartRoutine = pStartContext->m_pRealStartRoutine;
void* realContext = pStartContext->m_pRealContext;
Expand Down
14 changes: 9 additions & 5 deletions src/coreclr/nativeaot/Runtime/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,12 @@ bool Thread::IsInitialized()
// -----------------------------------------------------------------------------------------------------------
// GC support APIs - do not use except from GC itself
//
void Thread::SetGCSpecial(bool isGCSpecial)
void Thread::SetGCSpecial()
{
if (!IsInitialized())
Construct();
if (isGCSpecial)
SetState(TSF_IsGcSpecialThread);
else
ClearState(TSF_IsGcSpecialThread);

SetState(TSF_IsGcSpecialThread);
}

bool Thread::IsGCSpecial()
Expand Down Expand Up @@ -603,6 +601,12 @@ void Thread::Hijack()
return;
}

if (IsGCSpecial())
{
// GC threads can not be forced to run preemptively, so we will not try.
return;
}

#ifdef FEATURE_SUSPEND_REDIRECTION
// if the thread is redirected, leave it as-is.
if (IsStateSet(TSF_Redirected))
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/nativeaot/Runtime/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class Thread : private ThreadBuffer
//
// GC support APIs - do not use except from GC itself
//
void SetGCSpecial(bool isGCSpecial);
void SetGCSpecial();
bool IsGCSpecial();
bool CatchAtSafePoint();

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/gcenv.ee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ namespace
assert(args != nullptr);

ClrFlsSetThreadType(ThreadType_GC);
args->Thread->SetGCSpecial(true);
args->Thread->SetGCSpecial();
STRESS_LOG_RESERVE_MEM(GC_STRESSLOG_MULTIPLY);
args->HasStarted = !!args->Thread->HasStarted();

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -4260,7 +4260,7 @@ class Thread

// GC calls this when creating special threads that also happen to have an EE Thread
// object associated with them (e.g., the bgc thread).
void SetGCSpecial(bool fGCSpecial);
void SetGCSpecial();

private:

Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/threads.inl
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ inline bool Thread::IsGCSpecial()
return m_fGCSpecial;
}

inline void Thread::SetGCSpecial(bool fGCSpecial)
inline void Thread::SetGCSpecial()
{
LIMITED_METHOD_CONTRACT;
m_fGCSpecial = fGCSpecial;
m_fGCSpecial = true;
}

#if !defined(DACCESS_COMPILE)
Expand Down
6 changes: 6 additions & 0 deletions src/coreclr/vm/threadsuspend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3394,6 +3394,12 @@ void ThreadSuspend::SuspendRuntime(ThreadSuspend::SUSPEND_REASON reason)
continue;
}

if (thread->IsGCSpecial())
{
// GC threads can not be forced to run preemptively, so we will not try.
continue;
}

// this is an interesting thread in cooperative mode, let's guide it to preemptive

if (!Thread::UseContextBasedThreadRedirection())
Expand Down

0 comments on commit 774324e

Please sign in to comment.