llvm.org GIT mirror llvm / 943d1fd
Add support for arbitrary functors to CrashRecoveryContext. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202895 91177308-0d34-0410-b5e6-96231b3b80d8 Richard Smith 6 years ago
2 changed file(s) with 20 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
99 #ifndef LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
1010 #define LLVM_SUPPORT_CRASHRECOVERYCONTEXT_H
1111
12 #include
1213 #include
1314
1415 namespace llvm {
7475 /// make as little assumptions as possible about the program state when
7576 /// RunSafely has returned false. Clients can use getBacktrace() to retrieve
7677 /// the backtrace of the crash on failures.
78 bool RunSafely(std::function Fn);
7779 bool RunSafely(void (*Fn)(void*), void *UserData);
7880
7981 /// \brief Execute the provide callback function (with the given arguments) in
8385 /// See RunSafely() and llvm_execute_on_thread().
8486 bool RunSafelyOnThread(void (*Fn)(void*), void *UserData,
8587 unsigned RequestedStackSize = 0);
88 bool RunSafelyOnThread(std::function Fn,
89 unsigned RequestedStackSize = 0);
8690
8791 /// \brief Explicitly trigger a crash recovery in the current process, and
8892 /// return failure from RunSafely(). This function does not return.
301301 #endif
302302
303303 bool CrashRecoveryContext::RunSafely(void (*Fn)(void*), void *UserData) {
304 return RunSafely([&]() { Fn(UserData); });
305 }
306
307 bool CrashRecoveryContext::RunSafely(std::function Fn) {
304308 // If crash recovery is disabled, do nothing.
305309 if (gCrashRecoveryEnabled) {
306310 assert(!Impl && "Crash recovery context already initialized!");
312316 }
313317 }
314318
315 Fn(UserData);
319 Fn();
316320 return true;
317321 }
318322
331335
332336 //
333337
338 bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData,
339 unsigned RequestedStackSize) {
340 return RunSafelyOnThread([&]() { Fn(UserData); }, RequestedStackSize);
341 }
342
334343 namespace {
335344 struct RunSafelyOnThreadInfo {
336 void (*UserFn)(void*);
337 void *UserData;
345 std::function Fn;
338346 CrashRecoveryContext *CRC;
339347 bool Result;
340348 };
343351 static void RunSafelyOnThread_Dispatch(void *UserData) {
344352 RunSafelyOnThreadInfo *Info =
345353 reinterpret_cast(UserData);
346 Info->Result = Info->CRC->RunSafely(Info->UserFn, Info->UserData);
347 }
348 bool CrashRecoveryContext::RunSafelyOnThread(void (*Fn)(void*), void *UserData,
354 Info->Result = Info->CRC->RunSafely(Info->Fn);
355 }
356
357 bool CrashRecoveryContext::RunSafelyOnThread(std::function Fn,
349358 unsigned RequestedStackSize) {
350 RunSafelyOnThreadInfo Info = { Fn, UserData, this, false };
359 RunSafelyOnThreadInfo Info = { Fn, this, false };
351360 llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info, RequestedStackSize);
352361 if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl)
353362 CRC->setSwitchedThread();