aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp')
-rw-r--r--contrib/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp61
1 files changed, 31 insertions, 30 deletions
diff --git a/contrib/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp b/contrib/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp
index 292ba63d14aa..e96a9b59d834 100644
--- a/contrib/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp
+++ b/contrib/llvm-project/llvm/lib/Support/CrashRecoveryContext.cpp
@@ -10,10 +10,9 @@
#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"
+#include <cassert>
#include <mutex>
#include <setjmp.h>
@@ -22,9 +21,7 @@ using namespace llvm;
namespace {
struct CrashRecoveryContextImpl;
-
-static ManagedStatic<
- sys::ThreadLocal<const CrashRecoveryContextImpl> > CurrentContext;
+static LLVM_THREAD_LOCAL const CrashRecoveryContextImpl *CurrentContext;
struct CrashRecoveryContextImpl {
// When threads are disabled, this links up all active
@@ -42,12 +39,12 @@ struct CrashRecoveryContextImpl {
public:
CrashRecoveryContextImpl(CrashRecoveryContext *CRC) noexcept
: CRC(CRC), Failed(false), SwitchedThread(false), ValidJumpBuffer(false) {
- Next = CurrentContext->get();
- CurrentContext->set(this);
+ Next = CurrentContext;
+ CurrentContext = this;
}
~CrashRecoveryContextImpl() {
if (!SwitchedThread)
- CurrentContext->set(Next);
+ CurrentContext = Next;
}
/// Called when the separate crash-recovery thread was finished, to
@@ -65,7 +62,7 @@ public:
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);
+ CurrentContext = Next;
assert(!Failed && "Crash recovery context already failed!");
Failed = true;
@@ -83,13 +80,17 @@ public:
// 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;
+static LLVM_THREAD_LOCAL const CrashRecoveryContext *IsRecoveringFromCrash;
+
+} // namespace
static void installExceptionOrSignalHandlers();
static void uninstallExceptionOrSignalHandlers();
@@ -106,8 +107,8 @@ CrashRecoveryContext::CrashRecoveryContext() {
CrashRecoveryContext::~CrashRecoveryContext() {
// Reclaim registered resources.
CrashRecoveryContextCleanup *i = head;
- const CrashRecoveryContext *PC = tlIsRecoveringFromCrash->get();
- tlIsRecoveringFromCrash->set(this);
+ const CrashRecoveryContext *PC = IsRecoveringFromCrash;
+ IsRecoveringFromCrash = this;
while (i) {
CrashRecoveryContextCleanup *tmp = i;
i = tmp->next;
@@ -115,21 +116,21 @@ CrashRecoveryContext::~CrashRecoveryContext() {
tmp->recoverResources();
delete tmp;
}
- tlIsRecoveringFromCrash->set(PC);
+ IsRecoveringFromCrash = PC;
CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
delete CRCI;
}
bool CrashRecoveryContext::isRecoveringFromCrash() {
- return tlIsRecoveringFromCrash->get() != nullptr;
+ return IsRecoveringFromCrash != nullptr;
}
CrashRecoveryContext *CrashRecoveryContext::GetCurrent() {
if (!gCrashRecoveryEnabled)
return nullptr;
- const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
+ const CrashRecoveryContextImpl *CRCI = CurrentContext;
if (!CRCI)
return nullptr;
@@ -137,7 +138,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 +147,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 +200,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 = CurrentContext;
if (!CRCI) {
// Something has gone horribly wrong, so let's just tell everyone
@@ -276,7 +277,7 @@ static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
}
// Lookup the current thread local recovery object.
- const CrashRecoveryContextImpl *CRCI = CurrentContext->get();
+ const CrashRecoveryContextImpl *CRCI = CurrentContext;
if (!CRCI) {
// Something has gone horribly wrong, so let's just tell everyone
@@ -306,7 +307,7 @@ static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
// CrashRecoveryContext at all. So we make use of a thread-local
// exception table. The handles contained in here will either be
// non-NULL, valid VEH handles, or NULL.
-static sys::ThreadLocal<const void> sCurrentExceptionHandle;
+static LLVM_THREAD_LOCAL const void* sCurrentExceptionHandle;
static void installExceptionOrSignalHandlers() {
// We can set up vectored exception handling now. We will install our
@@ -314,17 +315,17 @@ static void installExceptionOrSignalHandlers() {
// it will remain at the front (another call could install itself before
// our handler). This 1) isn't likely, and 2) shouldn't cause problems.
PVOID handle = ::AddVectoredExceptionHandler(1, ExceptionHandler);
- sCurrentExceptionHandle.set(handle);
+ sCurrentExceptionHandle = handle;
}
static void uninstallExceptionOrSignalHandlers() {
- PVOID currentHandle = const_cast<PVOID>(sCurrentExceptionHandle.get());
+ PVOID currentHandle = const_cast<PVOID>(sCurrentExceptionHandle);
if (currentHandle) {
// Now we can remove the vectored exception handler from the chain
::RemoveVectoredExceptionHandler(currentHandle);
// Reset the handle in our thread-local set.
- sCurrentExceptionHandle.set(NULL);
+ sCurrentExceptionHandle = NULL;
}
}
@@ -345,12 +346,12 @@ static void uninstallExceptionOrSignalHandlers() {
static const int Signals[] =
{ SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
-static const unsigned NumSignals = array_lengthof(Signals);
+static const unsigned NumSignals = std::size(Signals);
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 = CurrentContext;
if (!CRCI) {
// We didn't find a crash recovery context -- this means either we got a
@@ -510,8 +511,8 @@ bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn,
bool UseBackgroundPriority = hasThreadBackgroundPriority();
RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false };
llvm::thread Thread(RequestedStackSize == 0
- ? llvm::None
- : llvm::Optional<unsigned>(RequestedStackSize),
+ ? std::nullopt
+ : std::optional<unsigned>(RequestedStackSize),
RunSafelyOnThread_Dispatch, &Info);
Thread.join();