[llvm] d4cab87 - ManagedStatic: remove from CrashRecoveryContext

Nicolai Hähnle via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 3 05:35:47 PDT 2022


Author: Nicolai Hähnle
Date: 2022-08-03T14:35:30+02:00
New Revision: d4cab870940692bfda2433de83d1a4d0349bc9aa

URL: https://github.com/llvm/llvm-project/commit/d4cab870940692bfda2433de83d1a4d0349bc9aa
DIFF: https://github.com/llvm/llvm-project/commit/d4cab870940692bfda2433de83d1a4d0349bc9aa.diff

LOG: ManagedStatic: remove from CrashRecoveryContext

Differential Revision: https://reviews.llvm.org/D129126

Added: 
    

Modified: 
    llvm/lib/Support/CrashRecoveryContext.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/CrashRecoveryContext.cpp b/llvm/lib/Support/CrashRecoveryContext.cpp
index 292ba63d14aa..ae5841832d8f 100644
--- 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 @@ namespace {
 
 struct CrashRecoveryContextImpl;
 
-static ManagedStatic<
-    sys::ThreadLocal<const CrashRecoveryContextImpl> > CurrentContext;
+sys::ThreadLocal<const CrashRecoveryContextImpl> &getCurrentContext() {
+  static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext;
+  return CurrentContext;
+}
 
 struct CrashRecoveryContextImpl {
   // When threads are disabled, this links up all active
@@ -42,12 +43,12 @@ struct CrashRecoveryContextImpl {
 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 @@ struct CrashRecoveryContextImpl {
   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 @@ struct CrashRecoveryContextImpl {
     // this occurs when using SEH on Windows with MSVC or clang-cl.
   }
 };
-} // namespace
 
-static ManagedStatic<std::mutex> gCrashRecoveryContextMutex;
+std::mutex &getCrashRecoveryContextMutex() {
+  static std::mutex CrashRecoveryContextMutex;
+  return CrashRecoveryContextMutex;
+}
+
 static bool gCrashRecoveryEnabled = false;
 
-static ManagedStatic<sys::ThreadLocal<const CrashRecoveryContext>>
-       tlIsRecoveringFromCrash;
+sys::ThreadLocal<const CrashRecoveryContext> &getIsRecoveringFromCrash() {
+  static sys::ThreadLocal<const CrashRecoveryContext> IsRecoveringFromCrash;
+  return IsRecoveringFromCrash;
+}
+
+} // namespace
 
 static void installExceptionOrSignalHandlers();
 static void uninstallExceptionOrSignalHandlers();
@@ -106,8 +114,8 @@ CrashRecoveryContext::CrashRecoveryContext() {
 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 @@ CrashRecoveryContext::~CrashRecoveryContext() {
     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 @@ CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
 }
 
 void CrashRecoveryContext::Enable() {
-  std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex);
+  std::lock_guard<std::mutex> L(getCrashRecoveryContextMutex());
   // FIXME: Shouldn't this be a refcount or something?
   if (gCrashRecoveryEnabled)
     return;
@@ -146,7 +154,7 @@ void CrashRecoveryContext::Enable() {
 }
 
 void CrashRecoveryContext::Disable() {
-  std::lock_guard<std::mutex> L(*gCrashRecoveryContextMutex);
+  std::lock_guard<std::mutex> L(getCrashRecoveryContextMutex());
   if (!gCrashRecoveryEnabled)
     return;
   gCrashRecoveryEnabled = false;
@@ -199,7 +207,7 @@ static void uninstallExceptionOrSignalHandlers() {}
 // 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 @@ static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
   }
 
   // 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 struct sigaction PrevActions[NumSignals];
 
 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


        


More information about the llvm-commits mailing list