diff options
author | github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> | 2022-03-08 23:59:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-08 23:59:44 +0300 |
commit | c746820a1ef21923c54e44c3e9488c934d9bef88 (patch) | |
tree | 3517db3d432842de8bdd01c6b3d305cff3dff6cc /src/coreclr | |
parent | 51ecbf333d20e1ed5c5ead3c85c1f5c958d8840a (diff) |
[release/6.0] Ensure that tls_destructionMonitor is initialized if a thread is attached to the runtime. (#65406)
* Ensure that tls_destructionMonitor is initialized if a thread is attached to the runtime.
* relax the assert
* no call to `EnsureTlsDestructionMonitor` on unsetting a thread.
* remove obsolete comment
Co-authored-by: vsadov <8218165+VSadov@users.noreply.github.com>
Diffstat (limited to 'src/coreclr')
-rw-r--r-- | src/coreclr/vm/ceemain.cpp | 52 | ||||
-rw-r--r-- | src/coreclr/vm/ceemain.h | 2 | ||||
-rw-r--r-- | src/coreclr/vm/threads.cpp | 4 |
3 files changed, 38 insertions, 20 deletions
diff --git a/src/coreclr/vm/ceemain.cpp b/src/coreclr/vm/ceemain.cpp index 87106bbff4b..bce2d8ae807 100644 --- a/src/coreclr/vm/ceemain.cpp +++ b/src/coreclr/vm/ceemain.cpp @@ -1878,35 +1878,42 @@ BOOL STDMETHODCALLTYPE EEDllMain( // TRUE on success, FALSE on error. struct TlsDestructionMonitor { - ~TlsDestructionMonitor() + bool m_activated = false; + + void Activate() { - // Don't destroy threads here if we're in shutdown (shutdown will - // clean up for us instead). + m_activated = true; + } - Thread* thread = GetThreadNULLOk(); - if (thread) + ~TlsDestructionMonitor() + { + if (m_activated) { + Thread* thread = GetThreadNULLOk(); + if (thread) + { #ifdef FEATURE_COMINTEROP - // reset the CoInitialize state - // so we don't call CoUninitialize during thread detach - thread->ResetCoInitialized(); + // reset the CoInitialize state + // so we don't call CoUninitialize during thread detach + thread->ResetCoInitialized(); #endif // FEATURE_COMINTEROP - // For case where thread calls ExitThread directly, we need to reset the - // frame pointer. Otherwise stackwalk would AV. We need to do it in cooperative mode. - // We need to set m_GCOnTransitionsOK so this thread won't trigger GC when toggle GC mode - if (thread->m_pFrame != FRAME_TOP) - { + // For case where thread calls ExitThread directly, we need to reset the + // frame pointer. Otherwise stackwalk would AV. We need to do it in cooperative mode. + // We need to set m_GCOnTransitionsOK so this thread won't trigger GC when toggle GC mode + if (thread->m_pFrame != FRAME_TOP) + { #ifdef _DEBUG - thread->m_GCOnTransitionsOK = FALSE; + thread->m_GCOnTransitionsOK = FALSE; #endif - GCX_COOP_NO_DTOR(); - thread->m_pFrame = FRAME_TOP; - GCX_COOP_NO_DTOR_END(); + GCX_COOP_NO_DTOR(); + thread->m_pFrame = FRAME_TOP; + GCX_COOP_NO_DTOR_END(); + } + thread->DetachThread(TRUE); } - thread->DetachThread(TRUE); - } - ThreadDetaching(); + ThreadDetaching(); + } } }; @@ -1914,6 +1921,11 @@ struct TlsDestructionMonitor // is called when a thread is being shut down. thread_local TlsDestructionMonitor tls_destructionMonitor; +void EnsureTlsDestructionMonitor() +{ + tls_destructionMonitor.Activate(); +} + #ifdef DEBUGGING_SUPPORTED // // InitializeDebugger initialized the Runtime-side COM+ Debugging Services diff --git a/src/coreclr/vm/ceemain.h b/src/coreclr/vm/ceemain.h index 1f96cdea85f..688f41c6deb 100644 --- a/src/coreclr/vm/ceemain.h +++ b/src/coreclr/vm/ceemain.h @@ -45,6 +45,8 @@ void ForceEEShutdown(ShutdownCompleteAction sca = SCA_ExitProcessWhenShutdownCom // Notification of a DLL_THREAD_DETACH or a Thread Terminate. void ThreadDetaching(); +void EnsureTlsDestructionMonitor(); + void SetLatchedExitCode (INT32 code); INT32 GetLatchedExitCode (void); diff --git a/src/coreclr/vm/threads.cpp b/src/coreclr/vm/threads.cpp index e454953181f..28446a25fc3 100644 --- a/src/coreclr/vm/threads.cpp +++ b/src/coreclr/vm/threads.cpp @@ -376,6 +376,10 @@ void SetThread(Thread* t) LIMITED_METHOD_CONTRACT gCurrentThreadInfo.m_pThread = t; + if (t != NULL) + { + EnsureTlsDestructionMonitor(); + } } void SetAppDomain(AppDomain* ad) |