From 943d1fdffe805f6e026529a30167acdc324fb667 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 4 Mar 2014 21:48:49 +0000 Subject: [PATCH] Add support for arbitrary functors to CrashRecoveryContext. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202895 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/CrashRecoveryContext.h | 4 ++++ lib/Support/CrashRecoveryContext.cpp | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/include/llvm/Support/CrashRecoveryContext.h b/include/llvm/Support/CrashRecoveryContext.h index 4c0a5e26f00..aa5e55eeab4 100644 --- a/include/llvm/Support/CrashRecoveryContext.h +++ b/include/llvm/Support/CrashRecoveryContext.h @@ -10,6 +10,7 @@ #ifndef LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H #define LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H +#include #include namespace llvm { @@ -75,6 +76,7 @@ public: /// make as little assumptions as possible about the program state when /// RunSafely has returned false. Clients can use getBacktrace() to retrieve /// the backtrace of the crash on failures. + bool RunSafely(std::function Fn); bool RunSafely(void (*Fn)(void*), void *UserData); /// \brief Execute the provide callback function (with the given arguments) in @@ -84,6 +86,8 @@ public: /// See RunSafely() and llvm_execute_on_thread(). bool RunSafelyOnThread(void (*Fn)(void*), void *UserData, unsigned RequestedStackSize = 0); + bool RunSafelyOnThread(std::function Fn, + unsigned RequestedStackSize = 0); /// \brief Explicitly trigger a crash recovery in the current process, and /// return failure from RunSafely(). This function does not return. diff --git a/lib/Support/CrashRecoveryContext.cpp b/lib/Support/CrashRecoveryContext.cpp index 29f73fc539c..614980f718a 100644 --- a/lib/Support/CrashRecoveryContext.cpp +++ b/lib/Support/CrashRecoveryContext.cpp @@ -302,6 +302,10 @@ void CrashRecoveryContext::Disable() { #endif bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) { + return RunSafely([&]() { Fn(UserData); }); +} + +bool CrashRecoveryContext::RunSafely(std::function Fn) { // If crash recovery is disabled, do nothing. if (gCrashRecoveryEnabled) { assert(!Impl && "Crash recovery context already initialized!"); @@ -313,7 +317,7 @@ bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) { } } - Fn(UserData); + Fn(); return true; } @@ -332,10 +336,14 @@ const std::string &CrashRecoveryContext::getBacktrace() const { // +bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData, + unsigned RequestedStackSize) { + return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize); +} + namespace { struct RunSafelyOnThreadInfo { - void (*UserFn)(void*); - void *UserData; + std::function Fn; CrashRecoveryContext *CRC; bool Result; }; @@ -344,11 +352,12 @@ struct RunSafelyOnThreadInfo { static void RunSafelyOnThread_Dispatch(void *UserData) { RunSafelyOnThreadInfo *Info = reinterpret_cast(UserData); - Info->Result = Info->CRC->RunSafely(Info->UserFn, Info->UserData); + Info->Result = Info->CRC->RunSafely(Info->Fn); } -bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData, + +bool CrashRecoveryContext::RunSafelyOnThread(std::function Fn, unsigned RequestedStackSize) { - RunSafelyOnThreadInfo Info = { Fn, UserData, this, false }; + RunSafelyOnThreadInfo Info = { Fn, this, false }; llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize); if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl) CRC->setSwitchedThread(); -- 2.11.0