diff --git a/llvm/lib/Support/CrashRecoveryContext.cpp b/llvm/lib/Support/CrashRecoveryContext.cpp --- a/llvm/lib/Support/CrashRecoveryContext.cpp +++ b/llvm/lib/Support/CrashRecoveryContext.cpp @@ -10,7 +10,6 @@ #include "llvm/Config/llvm-config.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ExitCodes.h" -#include "llvm/Support/ManagedStatic.h" #include "llvm/Support/Signals.h" #include "llvm/Support/ThreadLocal.h" #include "llvm/Support/thread.h" @@ -23,8 +22,10 @@ struct CrashRecoveryContextImpl; -static ManagedStatic< - sys::ThreadLocal > CurrentContext; +sys::ThreadLocal &getCurrentContext() { + static sys::ThreadLocal CurrentContext; + return CurrentContext; +} struct CrashRecoveryContextImpl { // When threads are disabled, this links up all active @@ -42,12 +43,12 @@ public: CrashRecoveryContextImpl(CrashRecoveryContext *CRC) noexcept : CRC(CRC), Failed(false), SwitchedThread(false), ValidJumpBuffer(false) { - Next = CurrentContext->get(); - CurrentContext->set(this); + Next = getCurrentContext().get(); + getCurrentContext().set(this); } ~CrashRecoveryContextImpl() { if (!SwitchedThread) - CurrentContext->set(Next); + getCurrentContext().set(Next); } /// Called when the separate crash-recovery thread was finished, to @@ -65,7 +66,7 @@ void HandleCrash(int RetCode, uintptr_t Context) { // Eliminate the current context entry, to avoid re-entering in case the // cleanup code crashes. - CurrentContext->set(Next); + getCurrentContext().set(Next); assert(!Failed && "Crash recovery context already failed!"); Failed = true; @@ -83,13 +84,20 @@ // this occurs when using SEH on Windows with MSVC or clang-cl. } }; -} // namespace -static ManagedStatic gCrashRecoveryContextMutex; +std::mutex &getCrashRecoveryContextMutex() { + static std::mutex CrashRecoveryContextMutex; + return CrashRecoveryContextMutex; +} + static bool gCrashRecoveryEnabled = false; -static ManagedStatic> - tlIsRecoveringFromCrash; +sys::ThreadLocal &getIsRecoveringFromCrash() { + static sys::ThreadLocal IsRecoveringFromCrash; + return IsRecoveringFromCrash; +} + +} // namespace static void installExceptionOrSignalHandlers(); static void uninstallExceptionOrSignalHandlers(); @@ -106,8 +114,8 @@ CrashRecoveryContext::~CrashRecoveryContext() { // Reclaim registered resources. CrashRecoveryContextCleanup *i = head; - const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get(); - tlIsRecoveringFromCrash->set(this); + const CrashRecoveryContext *PC = getIsRecoveringFromCrash().get(); + getIsRecoveringFromCrash().set(this); while (i) { CrashRecoveryContextCleanup *tmp = i; i = tmp->next; @@ -115,21 +123,21 @@ tmp->recoverResources(); delete tmp; } - tlIsRecoveringFromCrash->set(PC); + getIsRecoveringFromCrash().set(PC); CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl; delete CRCI; } bool CrashRecoveryContext::isRecoveringFromCrash() { - return tlIsRecoveringFromCrash->get() != nullptr; + return getIsRecoveringFromCrash().get() != nullptr; } CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { if (!gCrashRecoveryEnabled) return nullptr; - const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); + const CrashRecoveryContextImpl *CRCI = getCurrentContext().get(); if (!CRCI) return nullptr; @@ -137,7 +145,7 @@ } void CrashRecoveryContext::Enable() { - std::lock_guard L(*gCrashRecoveryContextMutex); + std::lock_guard L(getCrashRecoveryContextMutex()); // FIXME: Shouldn't this be a refcount or something? if (gCrashRecoveryEnabled) return; @@ -146,7 +154,7 @@ } void CrashRecoveryContext::Disable() { - std::lock_guard L(*gCrashRecoveryContextMutex); + std::lock_guard L(getCrashRecoveryContextMutex()); if (!gCrashRecoveryEnabled) return; gCrashRecoveryEnabled = false; @@ -199,7 +207,7 @@ // occur inside the __except evaluation block static int ExceptionFilter(_EXCEPTION_POINTERS *Except) { // Lookup the current thread local recovery object. - const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); + const CrashRecoveryContextImpl *CRCI = getCurrentContext().get(); if (!CRCI) { // Something has gone horribly wrong, so let's just tell everyone @@ -276,7 +284,7 @@ } // Lookup the current thread local recovery object. - const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); + const CrashRecoveryContextImpl *CRCI = getCurrentContext().get(); if (!CRCI) { // Something has gone horribly wrong, so let's just tell everyone @@ -350,7 +358,7 @@ static void CrashRecoverySignalHandler(int Signal) { // Lookup the current thread local recovery object. - const CrashRecoveryContextImpl *CRCI = CurrentContext->get(); + const CrashRecoveryContextImpl *CRCI = getCurrentContext().get(); if (!CRCI) { // We didn't find a crash recovery context -- this means either we got a