CrashRecovery: Add CrashRecoveryContext::GetCurrent(), so clients can find the active...
authorDaniel Dunbar <daniel@zuster.org>
Tue, 17 Aug 2010 22:32:37 +0000 (22:32 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Tue, 17 Aug 2010 22:32:37 +0000 (22:32 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111308 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/CrashRecoveryContext.h
lib/Support/CrashRecoveryContext.cpp

index 45a43fa19502f5d4b23c389a2fdbf5adf9f20ea2..d66609fddfec2e0d136df1e91360627153c89826 100644 (file)
@@ -53,6 +53,10 @@ public:
   /// \brief Disable crash recovery.
   static void Disable();
 
+  /// \brief Return the active context, if the code is currently executing in a
+  /// thread which is in a protected context.
+  static CrashRecoveryContext *GetCurrent();
+
   /// \brief Execute the provide callback function (with the given arguments) in
   /// a protected context.
   ///
index d1ecbb74dacab4affa59bf973e80deddbde03f02..296cc3e8f3bc1358bab4192bb6330e079842d7c1 100644 (file)
@@ -23,12 +23,14 @@ struct CrashRecoveryContextImpl;
 static sys::ThreadLocal<const CrashRecoveryContextImpl> CurrentContext;
 
 struct CrashRecoveryContextImpl {
+  CrashRecoveryContext *CRC;
   std::string Backtrace;
   ::jmp_buf JumpBuffer;
   volatile unsigned Failed : 1;
 
 public:
-  CrashRecoveryContextImpl() : Failed(false) {
+  CrashRecoveryContextImpl(CrashRecoveryContext *CRC) : CRC(CRC),
+                                                        Failed(false) {
     CurrentContext.set(this);
   }
   ~CrashRecoveryContextImpl() {
@@ -56,6 +58,14 @@ CrashRecoveryContext::~CrashRecoveryContext() {
   delete CRCI;
 }
 
+CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
+  const CrashRecoveryContextImpl *CRCI = CurrentContext.get();
+  if (!CRCI)
+    return 0;
+
+  return CRCI->CRC;
+}
+
 #ifdef LLVM_ON_WIN32
 
 // FIXME: No real Win32 implementation currently.
@@ -164,7 +174,7 @@ bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
   // If crash recovery is disabled, do nothing.
   if (gCrashRecoveryEnabled) {
     assert(!Impl && "Crash recovery context already initialized!");
-    CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl;
+    CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this);
     Impl = CRCI;
 
     if (setjmp(CRCI->JumpBuffer) != 0) {