From 6e5887e7e6f5d7aa8bd85074788090de0378dcb0 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 4 Mar 2014 22:13:07 +0000 Subject: [PATCH] Remove dependence on std::function. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202902 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/CrashRecoveryContext.h | 25 +++++++++++++++++---- lib/Support/CrashRecoveryContext.cpp | 21 +++++------------ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/include/llvm/Support/CrashRecoveryContext.h b/include/llvm/Support/CrashRecoveryContext.h index aa5e55eeab4..4500efe7b8b 100644 --- a/include/llvm/Support/CrashRecoveryContext.h +++ b/include/llvm/Support/CrashRecoveryContext.h @@ -10,7 +10,6 @@ #ifndef LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H #define LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H -#include #include namespace llvm { @@ -47,6 +46,17 @@ class CrashRecoveryContext { void *Impl; CrashRecoveryContextCleanup *head; + /// An adaptor to convert an arbitrary functor into a void(void*), void* pair. + template struct FunctorAdaptor { + T Fn; + static void invoke(void *Data) { + return static_cast*>(Data)->Fn(); + } + typedef void Callback(void*); + Callback *fn() { return &invoke; } + void *arg() { return this; } + }; + public: CrashRecoveryContext() : Impl(0), head(0) {} ~CrashRecoveryContext(); @@ -76,8 +86,12 @@ 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); + template + bool RunSafely(Functor Fn) { + FunctorAdaptor Adaptor = { Fn }; + return RunSafely(Adaptor.fn(), Adaptor.arg()); + } /// \brief Execute the provide callback function (with the given arguments) in /// a protected context which is run in another thread (optionally with a @@ -86,8 +100,11 @@ 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); + template + bool RunSafelyOnThread(Functor Fn, unsigned RequestedStackSize = 0) { + FunctorAdaptor Adaptor = { Fn }; + return RunSafelyOnThread(Adaptor.fn(), Adaptor.arg(), RequestedStackSize); + } /// \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 614980f718a..ccc00894fb3 100644 --- a/lib/Support/CrashRecoveryContext.cpp +++ b/lib/Support/CrashRecoveryContext.cpp @@ -302,10 +302,6 @@ 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!"); @@ -317,7 +313,7 @@ bool CrashRecoveryContext::RunSafely(std::function Fn) { } } - Fn(); + Fn(UserData); return true; } @@ -336,14 +332,10 @@ const std::string &CrashRecoveryContext::getBacktrace() const { // -bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData, - unsigned RequestedStackSize) { - return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize); -} - namespace { struct RunSafelyOnThreadInfo { - std::function Fn; + void (*Fn)(void*); + void *Data; CrashRecoveryContext *CRC; bool Result; }; @@ -352,12 +344,11 @@ struct RunSafelyOnThreadInfo { static void RunSafelyOnThread_Dispatch(void *UserData) { RunSafelyOnThreadInfo *Info = reinterpret_cast(UserData); - Info->Result = Info->CRC->RunSafely(Info->Fn); + Info->Result = Info->CRC->RunSafely(Info->Fn, Info->Data); } - -bool CrashRecoveryContext::RunSafelyOnThread(std::function Fn, +bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData, unsigned RequestedStackSize) { - RunSafelyOnThreadInfo Info = { Fn, this, false }; + RunSafelyOnThreadInfo Info = { Fn, UserData, this, false }; llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize); if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl) CRC->setSwitchedThread();