X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=include%2Fllvm%2FSupport%2FCrashRecoveryContext.h;h=1a1c74368761d729c8d60d76260425618388cb24;hb=83f50fab5317ea62c8298a82c1de5054d5795ac9;hp=4500efe7b8b952e0446c29fa4f79246063d58af8;hpb=6e5887e7e6f5d7aa8bd85074788090de0378dcb0;p=oota-llvm.git diff --git a/include/llvm/Support/CrashRecoveryContext.h b/include/llvm/Support/CrashRecoveryContext.h index 4500efe7b8b..1a1c7436876 100644 --- a/include/llvm/Support/CrashRecoveryContext.h +++ b/include/llvm/Support/CrashRecoveryContext.h @@ -10,13 +10,12 @@ #ifndef LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H #define LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H +#include "llvm/ADT/STLExtras.h" #include namespace llvm { -class StringRef; - class CrashRecoveryContextCleanup; - + /// \brief Crash recovery helper object. /// /// This class implements support for running operations in a safe context so @@ -40,27 +39,14 @@ class CrashRecoveryContextCleanup; /// /// ... no crash was detected ... /// } -/// -/// Crash recovery contexts may not be nested. 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() : Impl(nullptr), head(nullptr) {} ~CrashRecoveryContext(); - + void registerCleanup(CrashRecoveryContextCleanup *cleanup); void unregisterCleanup(CrashRecoveryContextCleanup *cleanup); @@ -86,11 +72,9 @@ 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(void (*Fn)(void*), void *UserData); - template - bool RunSafely(Functor Fn) { - FunctorAdaptor Adaptor = { Fn }; - return RunSafely(Adaptor.fn(), Adaptor.arg()); + bool RunSafely(function_ref Fn); + bool RunSafely(void (*Fn)(void*), void *UserData) { + return RunSafely([&]() { Fn(UserData); }); } /// \brief Execute the provide callback function (with the given arguments) in @@ -98,12 +82,13 @@ public: /// requested stack size). /// /// See RunSafely() and llvm_execute_on_thread(). + /// + /// On Darwin, if PRIO_DARWIN_BG is set on the calling thread, it will be + /// propagated to the new thread as well. + bool RunSafelyOnThread(function_ref, unsigned RequestedStackSize = 0); bool RunSafelyOnThread(void (*Fn)(void*), void *UserData, - unsigned RequestedStackSize = 0); - template - bool RunSafelyOnThread(Functor Fn, unsigned RequestedStackSize = 0) { - FunctorAdaptor Adaptor = { Fn }; - return RunSafelyOnThread(Adaptor.fn(), Adaptor.arg(), RequestedStackSize); + unsigned RequestedStackSize = 0) { + return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize); } /// \brief Explicitly trigger a crash recovery in the current process, and @@ -122,10 +107,11 @@ class CrashRecoveryContextCleanup { protected: CrashRecoveryContext *context; CrashRecoveryContextCleanup(CrashRecoveryContext *context) - : context(context), cleanupFired(false) {} + : context(context), cleanupFired(false) {} + public: bool cleanupFired; - + virtual ~CrashRecoveryContextCleanup(); virtual void recoverResources() = 0; @@ -142,15 +128,16 @@ template class CrashRecoveryContextCleanupBase : public CrashRecoveryContextCleanup { protected: T *resource; - CrashRecoveryContextCleanupBase(CrashRecoveryContext *context, T* resource) - : CrashRecoveryContextCleanup(context), resource(resource) {} + CrashRecoveryContextCleanupBase(CrashRecoveryContext *context, T *resource) + : CrashRecoveryContextCleanup(context), resource(resource) {} + public: static DERIVED *create(T *x) { if (x) { if (CrashRecoveryContext *context = CrashRecoveryContext::GetCurrent()) return new DERIVED(context, x); } - return 0; + return nullptr; } }; @@ -159,9 +146,9 @@ class CrashRecoveryContextDestructorCleanup : public CrashRecoveryContextCleanupBase, T> { public: CrashRecoveryContextDestructorCleanup(CrashRecoveryContext *context, - T *resource) - : CrashRecoveryContextCleanupBase< - CrashRecoveryContextDestructorCleanup, T>(context, resource) {} + T *resource) + : CrashRecoveryContextCleanupBase< + CrashRecoveryContextDestructorCleanup, T>(context, resource) {} virtual void recoverResources() { this->resource->~T(); @@ -176,9 +163,7 @@ public: : CrashRecoveryContextCleanupBase< CrashRecoveryContextDeleteCleanup, T>(context, resource) {} - virtual void recoverResources() { - delete this->resource; - } + void recoverResources() override { delete this->resource; } }; template @@ -186,19 +171,18 @@ class CrashRecoveryContextReleaseRefCleanup : public CrashRecoveryContextCleanupBase, T> { public: - CrashRecoveryContextReleaseRefCleanup(CrashRecoveryContext *context, + CrashRecoveryContextReleaseRefCleanup(CrashRecoveryContext *context, T *resource) : CrashRecoveryContextCleanupBase, T>(context, resource) {} - virtual void recoverResources() { - this->resource->Release(); - } + void recoverResources() override { this->resource->Release(); } }; template > class CrashRecoveryContextCleanupRegistrar { CrashRecoveryContextCleanup *cleanup; + public: CrashRecoveryContextCleanupRegistrar(T *x) : cleanup(Cleanup::create(x)) { @@ -206,16 +190,14 @@ public: cleanup->getContext()->registerCleanup(cleanup); } - ~CrashRecoveryContextCleanupRegistrar() { - unregister(); - } - + ~CrashRecoveryContextCleanupRegistrar() { unregister(); } + void unregister() { if (cleanup && !cleanup->cleanupFired) cleanup->getContext()->unregisterCleanup(cleanup); - cleanup = 0; + cleanup = nullptr; } }; -} +} // end namespace llvm -#endif +#endif // LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H