#ifndef LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
#define LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
-#include <functional>
#include <string>
namespace llvm {
void *Impl;
CrashRecoveryContextCleanup *head;
+ /// An adaptor to convert an arbitrary functor into a void(void*), void* pair.
+ template<typename T> struct FunctorAdaptor {
+ T Fn;
+ static void invoke(void *Data) {
+ return static_cast<FunctorAdaptor<T>*>(Data)->Fn();
+ }
+ typedef void Callback(void*);
+ Callback *fn() { return &invoke; }
+ void *arg() { return this; }
+ };
+
public:
CrashRecoveryContext() : Impl(0), head(0) {}
~CrashRecoveryContext();
/// 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<void()> Fn);
bool RunSafely(void (*Fn)(void*), void *UserData);
+ template<typename Functor>
+ bool RunSafely(Functor Fn) {
+ FunctorAdaptor<Functor> 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
/// See RunSafely() and llvm_execute_on_thread().
bool RunSafelyOnThread(void (*Fn)(void*), void *UserData,
unsigned RequestedStackSize = 0);
- bool RunSafelyOnThread(std::function<void()> Fn,
- unsigned RequestedStackSize = 0);
+ template<typename Functor>
+ bool RunSafelyOnThread(Functor Fn, unsigned RequestedStackSize = 0) {
+ FunctorAdaptor<Functor> 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.
#endif
bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
- return RunSafely([&]() { Fn(UserData); });
-}
-
-bool CrashRecoveryContext::RunSafely(std::function<void()> Fn) {
// If crash recovery is disabled, do nothing.
if (gCrashRecoveryEnabled) {
assert(!Impl && "Crash recovery context already initialized!");
}
}
- Fn();
+ Fn(UserData);
return true;
}
//
-bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData,
- unsigned RequestedStackSize) {
- return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize);
-}
-
namespace {
struct RunSafelyOnThreadInfo {
- std::function<void()> Fn;
+ void (*Fn)(void*);
+ void *Data;
CrashRecoveryContext *CRC;
bool Result;
};
static void RunSafelyOnThread_Dispatch(void *UserData) {
RunSafelyOnThreadInfo *Info =
reinterpret_cast<RunSafelyOnThreadInfo*>(UserData);
- Info->Result = Info->CRC->RunSafely(Info->Fn);
+ Info->Result = Info->CRC->RunSafely(Info->Fn, Info->Data);
}
-
-bool CrashRecoveryContext::RunSafelyOnThread(std::function<void()> 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();