llvm::CrashRecoveryContextCleanupRegistrar

Helper class for managing resource cleanups.

Synopsis

Declared in <llvm/Support/CrashRecoveryContext.h>

template<
    typename T,
    typename Cleanup = CrashRecoveryContextDeleteCleanup<T>>
class CrashRecoveryContextCleanupRegistrar;

Description

Clients create objects of this type in the code executed in a crash recovery context to ensure that the resource will be reclaimed even in the case of crash. For example:


void actual_work(void *) {
  ...
  std::unique_ptr<Resource> R(new Resource());
  CrashRecoveryContextCleanupRegistrar D(R.get());
  ...
}

void foo() {
  CrashRecoveryContext CRC;

  if (!CRC.RunSafely(actual_work, 0)) {
     ... a crash was detected, report error to user ...
  }

If the code of actual_work in the example above does not crash, the destructor of CrashRecoveryContextCleanupRegistrar removes cleanup code from the current CrashRecoveryContext and the resource is reclaimed by the destructor of std::unique_ptr. If crash happens, destructors are not called and the resource is reclaimed by cleanup object registered in the recovery context by the constructor of CrashRecoveryContextCleanupRegistrar.

Member Functions

NameDescription
CrashRecoveryContextCleanupRegistrar [constructor]Register a cleanup for x in the current recovery context.
~CrashRecoveryContextCleanupRegistrar [destructor]Unregister the cleanup and destroy the registrar.
unregister Remove the registered cleanup if it has not already fired.

Template Parameters

NameDescription
TType of resource been reclaimed.
CleanupClass that defines how the resource is reclaimed.